{"_id":"@aigentic/attention-wasm","_rev":"3-5267141ae05c185565f5fa4ba6c13de8","name":"@aigentic/attention-wasm","dist-tags":{"latest":"2.2.3"},"versions":{"2.2.2":{"name":"@aigentic/attention-wasm","version":"2.2.2","keywords":["wasm","attention","transformer","flash-attention","llm"],"license":"MIT","_id":"@aigentic/attention-wasm@2.2.2","maintainers":[{"name":"aigentic","email":"engineering@aigentic.net"}],"homepage":"https://ruv.io/ruvector","bugs":{"url":"https://github.com/ruvnet/ruvector/issues"},"dist":{"shasum":"ebc645f5b1e7070c0e4c0324d3fb53d918c122d7","tarball":"https://registry.npmjs.org/@aigentic/attention-wasm/-/attention-wasm-2.2.2.tgz","fileCount":6,"integrity":"sha512-dN1GlVGd6xoHDHsZvHp4rI5oIU5XncmFN8DfAf6nrvwgAZUQ0lNg8Yylzs9Z/XUIB7VHGhTrBhjWY/xhzJ7OQQ==","signatures":[{"sig":"MEQCIBk/8BQ0pEePWNjE1XltA8cVfLPvDTBxvxduXAhdKS09AiA+U0a9vz3HqdypfrNDeLaQbBDcUEQDJWdtopp+siQpGw==","keyid":"SHA256:DhQ8wR5APBvFHLF/+Tc+AYvPOdTpcIDqOhxsBHRwC7U"}],"unpackedSize":227956},"main":"ruvector_attention_wasm.js","type":"module","types":"ruvector_attention_wasm.d.ts","gitHead":"06426cbc6e70f09762e996a98517b0b1fd1c7c4e","_npmUser":{"name":"aigentic","email":"engineering@aigentic.net"},"repository":{"url":"git+https://github.com/ruvnet/ruvector.git","type":"git"},"_npmVersion":"11.12.0","description":"High-performance WebAssembly attention mechanisms: Multi-Head, Flash, Hyperbolic, MoE, CGT Sheaf Attention with GPU acceleration for transformers and LLMs","directories":{},"sideEffects":["./snippets/*"],"_nodeVersion":"22.22.1","collaborators":["Ruvector Team"],"_hasShrinkwrap":false,"_npmOperationalInternal":{"tmp":"tmp/attention-wasm_2.2.2_1779090441171_0.9806167199243454","host":"s3://npm-registry-packages-npm-production"}},"2.2.3":{"name":"@aigentic/attention-wasm","version":"2.2.3","keywords":["wasm","attention","transformer","flash-attention","llm"],"license":"MIT","_id":"@aigentic/attention-wasm@2.2.3","maintainers":[{"name":"aigentic","email":"engineering@aigentic.net"}],"homepage":"https://ruv.io/ruvector","bugs":{"url":"https://github.com/ruvnet/ruvector/issues"},"dist":{"shasum":"7bf6e41ef0632b3e3621c93b4e072e5214edb0e0","tarball":"https://registry.npmjs.org/@aigentic/attention-wasm/-/attention-wasm-2.2.3.tgz","fileCount":6,"integrity":"sha512-PU7hDgIrrjWGXfsTntFTcBMSYrK8lbIm4IuyOaREDmZy/b2uugeNSqg1oIuqyXemssFnwXOsegkHRONwPBtFNw==","signatures":[{"sig":"MEUCIQDM/lThDKEJSsE0Stbp9ISR0o10MgLJYTBKUNmaDiwOeQIgS2oxX98vx7qTApAx3syviUbxdfMhlJEU3Yh3KMV01Vw=","keyid":"SHA256:DhQ8wR5APBvFHLF/+Tc+AYvPOdTpcIDqOhxsBHRwC7U"}],"unpackedSize":227956},"main":"ruvector_attention_wasm.js","type":"module","types":"ruvector_attention_wasm.d.ts","gitHead":"06426cbc6e70f09762e996a98517b0b1fd1c7c4e","_npmUser":{"name":"aigentic","email":"engineering@aigentic.net"},"repository":{"url":"git+https://github.com/ruvnet/ruvector.git","type":"git"},"_npmVersion":"11.12.0","description":"High-performance WebAssembly attention mechanisms: Multi-Head, Flash, Hyperbolic, MoE, CGT Sheaf Attention with GPU acceleration for transformers and LLMs","directories":{},"sideEffects":["./snippets/*"],"_nodeVersion":"22.22.1","collaborators":["Ruvector Team"],"_hasShrinkwrap":false,"_npmOperationalInternal":{"tmp":"tmp/attention-wasm_2.2.3_1779092801336_0.5777642607930906","host":"s3://npm-registry-packages-npm-production"}}},"time":{"created":"2026-05-18T07:47:21.094Z","modified":"2026-09-13T15:30:25.609Z","2.2.2":"2026-05-18T07:47:21.333Z","2.2.3":"2026-05-18T08:26:41.489Z"},"bugs":{"url":"https://github.com/ruvnet/ruvector/issues"},"license":"MIT","homepage":"https://ruv.io/ruvector","keywords":["wasm","attention","transformer","flash-attention","llm"],"repository":{"url":"git+https://github.com/ruvnet/ruvector.git","type":"git"},"description":"High-performance WebAssembly attention mechanisms: Multi-Head, Flash, Hyperbolic, MoE, CGT Sheaf Attention with GPU acceleration for transformers and LLMs","maintainers":[{"email":"engineering@aigentic.net","name":"aiggy"}],"readme":"# ruvector-attention-wasm\n\nWebAssembly bindings for the ruvector-attention package, providing high-performance attention mechanisms for browser and Node.js environments.\n\n## Features\n\n- **Multiple Attention Mechanisms**:\n  - Scaled Dot-Product Attention\n  - Multi-Head Attention\n  - Hyperbolic Attention (for hierarchical data)\n  - Linear Attention (Performer-style)\n  - Flash Attention (memory-efficient)\n  - Local-Global Attention\n  - Mixture of Experts (MoE) Attention\n  - **CGT Sheaf Attention** (coherence-gated via Prime-Radiant)\n\n- **Training Utilities**:\n  - InfoNCE contrastive loss\n  - Adam optimizer\n  - AdamW optimizer (with decoupled weight decay)\n  - Learning rate scheduler (warmup + cosine decay)\n\n- **TypeScript Support**: Full type definitions and modern API\n\n## Installation\n\n```bash\nnpm install ruvector-attention-wasm\n```\n\n## Usage\n\n### TypeScript/JavaScript\n\n```typescript\nimport { initialize, MultiHeadAttention, utils } from 'ruvector-attention-wasm';\n\n// Initialize WASM module\nawait initialize();\n\n// Create multi-head attention\nconst attention = new MultiHeadAttention({ dim: 64, numHeads: 8 });\n\n// Prepare inputs\nconst query = new Float32Array(64);\nconst keys = [new Float32Array(64), new Float32Array(64)];\nconst values = [new Float32Array(64), new Float32Array(64)];\n\n// Compute attention\nconst output = attention.compute(query, keys, values);\n\n// Use utilities\nconst similarity = utils.cosineSimilarity(query, keys[0]);\n```\n\n### Advanced Examples\n\n#### Hyperbolic Attention\n\n```typescript\nimport { HyperbolicAttention } from 'ruvector-attention-wasm';\n\nconst hyperbolic = new HyperbolicAttention({\n  dim: 128,\n  curvature: 1.0\n});\n\nconst output = hyperbolic.compute(query, keys, values);\n```\n\n#### MoE Attention with Expert Stats\n\n```typescript\nimport { MoEAttention } from 'ruvector-attention-wasm';\n\nconst moe = new MoEAttention({\n  dim: 64,\n  numExperts: 4,\n  topK: 2\n});\n\nconst output = moe.compute(query, keys, values);\n\n// Get expert utilization\nconst stats = moe.getExpertStats();\nconsole.log('Load balance:', stats.loadBalance);\n```\n\n#### Training with InfoNCE Loss\n\n```typescript\nimport { InfoNCELoss, Adam } from 'ruvector-attention-wasm';\n\nconst loss = new InfoNCELoss(0.07);\nconst optimizer = new Adam(paramCount, {\n  learningRate: 0.001,\n  beta1: 0.9,\n  beta2: 0.999,\n});\n\n// Training loop\nconst lossValue = loss.compute(anchor, positive, negatives);\noptimizer.step(params, gradients);\n```\n\n#### Learning Rate Scheduling\n\n```typescript\nimport { LRScheduler, AdamW } from 'ruvector-attention-wasm';\n\nconst scheduler = new LRScheduler({\n  initialLR: 0.001,\n  warmupSteps: 1000,\n  totalSteps: 10000,\n});\n\nconst optimizer = new AdamW(paramCount, {\n  learningRate: scheduler.getLR(),\n  weightDecay: 0.01,\n});\n\n// Training loop\nfor (let step = 0; step < 10000; step++) {\n  optimizer.learningRate = scheduler.getLR();\n  optimizer.step(params, gradients);\n  scheduler.step();\n}\n```\n\n## Building from Source\n\n### Prerequisites\n\n- Rust 1.70+\n- wasm-pack\n\n### Build Commands\n\n```bash\n# Build for web (ES modules)\nwasm-pack build --target web --out-dir pkg\n\n# Build for Node.js\nwasm-pack build --target nodejs --out-dir pkg-node\n\n# Build for bundlers (webpack, vite, etc.)\nwasm-pack build --target bundler --out-dir pkg-bundler\n\n# Run tests\nwasm-pack test --headless --firefox\n```\n\n## API Reference\n\n### Attention Mechanisms\n\n- `MultiHeadAttention` - Standard multi-head attention\n- `HyperbolicAttention` - Attention in hyperbolic space\n- `LinearAttention` - Linear complexity attention (Performer)\n- `FlashAttention` - Memory-efficient attention\n- `LocalGlobalAttention` - Combined local and global attention\n- `MoEAttention` - Mixture of Experts attention\n- `CGTSheafAttention` - Coherence-gated via Prime-Radiant energy\n- `scaledDotAttention()` - Functional API for basic attention\n\n### CGT Sheaf Attention (Prime-Radiant Integration)\n\nThe CGT (Coherence-Gated Transformer) Sheaf Attention mechanism uses Prime-Radiant's sheaf Laplacian energy to gate attention based on mathematical consistency:\n\n```typescript\nimport { CGTSheafAttention } from 'ruvector-attention-wasm';\n\nconst cgtAttention = new CGTSheafAttention({\n  dim: 128,\n  numHeads: 8,\n  coherenceThreshold: 0.3,  // Block if energy > threshold\n});\n\n// Attention is gated by coherence energy\nconst result = cgtAttention.compute(query, keys, values);\nconsole.log('Coherence energy:', result.energy);\nconsole.log('Is coherent:', result.isCoherent);\n```\n\n**Key features:**\n- Energy-weighted attention: Lower coherence energy → higher attention\n- Automatic hallucination detection via residual analysis\n- GPU-accelerated with wgpu WGSL shaders (vec4 optimized)\n- SIMD fallback (AVX-512/AVX2/NEON)\n\n### Training\n\n- `InfoNCELoss` - Contrastive loss function\n- `Adam` - Adam optimizer\n- `AdamW` - AdamW optimizer with weight decay\n- `LRScheduler` - Learning rate scheduler\n\n### Utilities\n\n- `utils.cosineSimilarity()` - Cosine similarity between vectors\n- `utils.l2Norm()` - L2 norm of a vector\n- `utils.normalize()` - Normalize vector to unit length\n- `utils.softmax()` - Apply softmax transformation\n- `utils.attentionWeights()` - Compute attention weights from scores\n- `utils.batchNormalize()` - Batch normalization\n- `utils.randomOrthogonalMatrix()` - Generate random orthogonal matrix\n- `utils.pairwiseDistances()` - Compute pairwise distances\n\n## Performance\n\nThe WASM bindings provide near-native performance for attention computations:\n\n- Optimized with `opt-level = \"s\"` and LTO\n- SIMD acceleration where available\n- Efficient memory management\n- Zero-copy data transfer where possible\n\n## License\n\nMIT OR Apache-2.0\n","readmeFilename":"README.md"}