| Crates.io | ruvector-attention-wasm |
| lib.rs | ruvector-attention-wasm |
| version | 0.1.32 |
| created_at | 2025-12-09 01:22:41.070087+00 |
| updated_at | 2026-01-23 01:20:55.906004+00 |
| description | High-performance WebAssembly attention mechanisms: Multi-Head, Flash, Hyperbolic, MoE, CGT Sheaf Attention with GPU acceleration for transformers and LLMs |
| homepage | https://ruv.io/ruvector |
| repository | https://github.com/ruvnet/ruvector |
| max_upload_size | |
| id | 1974669 |
| size | 64,971 |
WebAssembly bindings for the ruvector-attention package, providing high-performance attention mechanisms for browser and Node.js environments.
Multiple Attention Mechanisms:
Training Utilities:
TypeScript Support: Full type definitions and modern API
npm install ruvector-attention-wasm
import { initialize, MultiHeadAttention, utils } from 'ruvector-attention-wasm';
// Initialize WASM module
await initialize();
// Create multi-head attention
const attention = new MultiHeadAttention({ dim: 64, numHeads: 8 });
// Prepare inputs
const query = new Float32Array(64);
const keys = [new Float32Array(64), new Float32Array(64)];
const values = [new Float32Array(64), new Float32Array(64)];
// Compute attention
const output = attention.compute(query, keys, values);
// Use utilities
const similarity = utils.cosineSimilarity(query, keys[0]);
import { HyperbolicAttention } from 'ruvector-attention-wasm';
const hyperbolic = new HyperbolicAttention({
dim: 128,
curvature: 1.0
});
const output = hyperbolic.compute(query, keys, values);
import { MoEAttention } from 'ruvector-attention-wasm';
const moe = new MoEAttention({
dim: 64,
numExperts: 4,
topK: 2
});
const output = moe.compute(query, keys, values);
// Get expert utilization
const stats = moe.getExpertStats();
console.log('Load balance:', stats.loadBalance);
import { InfoNCELoss, Adam } from 'ruvector-attention-wasm';
const loss = new InfoNCELoss(0.07);
const optimizer = new Adam(paramCount, {
learningRate: 0.001,
beta1: 0.9,
beta2: 0.999,
});
// Training loop
const lossValue = loss.compute(anchor, positive, negatives);
optimizer.step(params, gradients);
import { LRScheduler, AdamW } from 'ruvector-attention-wasm';
const scheduler = new LRScheduler({
initialLR: 0.001,
warmupSteps: 1000,
totalSteps: 10000,
});
const optimizer = new AdamW(paramCount, {
learningRate: scheduler.getLR(),
weightDecay: 0.01,
});
// Training loop
for (let step = 0; step < 10000; step++) {
optimizer.learningRate = scheduler.getLR();
optimizer.step(params, gradients);
scheduler.step();
}
# Build for web (ES modules)
wasm-pack build --target web --out-dir pkg
# Build for Node.js
wasm-pack build --target nodejs --out-dir pkg-node
# Build for bundlers (webpack, vite, etc.)
wasm-pack build --target bundler --out-dir pkg-bundler
# Run tests
wasm-pack test --headless --firefox
MultiHeadAttention - Standard multi-head attentionHyperbolicAttention - Attention in hyperbolic spaceLinearAttention - Linear complexity attention (Performer)FlashAttention - Memory-efficient attentionLocalGlobalAttention - Combined local and global attentionMoEAttention - Mixture of Experts attentionCGTSheafAttention - Coherence-gated via Prime-Radiant energyscaledDotAttention() - Functional API for basic attentionThe CGT (Coherence-Gated Transformer) Sheaf Attention mechanism uses Prime-Radiant's sheaf Laplacian energy to gate attention based on mathematical consistency:
import { CGTSheafAttention } from 'ruvector-attention-wasm';
const cgtAttention = new CGTSheafAttention({
dim: 128,
numHeads: 8,
coherenceThreshold: 0.3, // Block if energy > threshold
});
// Attention is gated by coherence energy
const result = cgtAttention.compute(query, keys, values);
console.log('Coherence energy:', result.energy);
console.log('Is coherent:', result.isCoherent);
Key features:
InfoNCELoss - Contrastive loss functionAdam - Adam optimizerAdamW - AdamW optimizer with weight decayLRScheduler - Learning rate schedulerutils.cosineSimilarity() - Cosine similarity between vectorsutils.l2Norm() - L2 norm of a vectorutils.normalize() - Normalize vector to unit lengthutils.softmax() - Apply softmax transformationutils.attentionWeights() - Compute attention weights from scoresutils.batchNormalize() - Batch normalizationutils.randomOrthogonalMatrix() - Generate random orthogonal matrixutils.pairwiseDistances() - Compute pairwise distancesThe WASM bindings provide near-native performance for attention computations:
opt-level = "s" and LTOMIT OR Apache-2.0