ruvector/crates/ruvector-attention-wasm
github-actions[bot] cf074121e5
Some checks failed
ruvector-verified CI / check (--features coherence-proofs) (push) Has been cancelled
ruvector-verified CI / check (--features hnsw-proofs) (push) Has been cancelled
ruvector-verified CI / check (--features rvf-proofs) (push) Has been cancelled
ruvector-verified CI / check (--features serde) (push) Has been cancelled
ruvector-verified CI / check (--features ultra) (push) Has been cancelled
ruvector-verified CI / clippy (push) Has been cancelled
Clippy + fmt / Clippy (deny warnings) (push) Has been cancelled
Clippy + fmt / Rustfmt (push) Has been cancelled
Benchmarks / Rust Benchmarks (push) Has been cancelled
Benchmarks / SQL Benchmarks (push) Has been cancelled
Build Attention Native Modules / Build darwin-arm64 (push) Has been cancelled
Build Attention Native Modules / Build darwin-x64 (push) Has been cancelled
Build Attention Native Modules / Build linux-arm64-gnu (push) Has been cancelled
Build Attention Native Modules / Build linux-x64-gnu (push) Has been cancelled
Build Attention Native Modules / Build win32-x64-msvc (push) Has been cancelled
ruvector-verified CI / check () (push) Has been cancelled
ruvector-verified CI / check (--all-features) (push) Has been cancelled
ruvector-verified CI / check (--features all-proofs) (push) Has been cancelled
supply-chain / dependency-review (PRs only) (push) Has been cancelled
supply-chain / cargo audit (RustSec advisories) (push) Has been cancelled
supply-chain / cargo deny (license + source + ban policy) (push) Has been cancelled
supply-chain / npm audit (npm/ workspace) (push) Has been cancelled
RuvLLM Benchmarks / macOS ARM64 Benchmarks (M-series) (push) Has been cancelled
supply-chain / lockfile integrity (Cargo.lock) (push) Has been cancelled
Build DiskANN Native Modules / Build DiskANN linux-arm64-gnu (push) Has been cancelled
Build DiskANN Native Modules / Build DiskANN linux-x64-gnu (push) Has been cancelled
Build DiskANN Native Modules / Build DiskANN win32-x64-msvc (push) Has been cancelled
Workspace CI / Security audit (push) Has been cancelled
Build GNN Native Modules / Build GNN darwin-arm64 (push) Has been cancelled
Build GNN Native Modules / Build GNN darwin-x64 (push) Has been cancelled
Build GNN Native Modules / Build GNN linux-arm64-gnu (push) Has been cancelled
Build GNN Native Modules / Build GNN linux-x64-gnu (push) Has been cancelled
RuvLTRA-Small Tests / Test Coverage (push) Has been cancelled
Build GNN Native Modules / Build GNN linux-x64-musl (push) Has been cancelled
Build GNN Native Modules / Build GNN win32-x64-msvc (push) Has been cancelled
Build Graph Node Native Modules / Build Graph darwin-arm64 (push) Has been cancelled
Build Graph Node Native Modules / Build Graph darwin-x64 (push) Has been cancelled
Build Graph Node Native Modules / Build Graph linux-arm64-gnu (push) Has been cancelled
Build Graph Node Native Modules / Build Graph linux-x64-gnu (push) Has been cancelled
Build Graph Node Native Modules / Build Graph win32-x64-msvc (push) Has been cancelled
Build Graph Transformer Native Modules / Build darwin-arm64 (push) Has been cancelled
Build Graph Transformer Native Modules / Build linux-x64-musl (push) Has been cancelled
Build Graph Transformer Native Modules / Build win32-x64-msvc (push) Has been cancelled
Build Graph Transformer Native Modules / Build WASM (push) Has been cancelled
Build Attention Native Modules / Build WASM (push) Has been cancelled
Build DiskANN Native Modules / Build DiskANN darwin-arm64 (push) Has been cancelled
Build DiskANN Native Modules / Build DiskANN darwin-x64 (push) Has been cancelled
Build Graph Transformer Native Modules / Build darwin-x64 (push) Has been cancelled
Build Graph Transformer Native Modules / Build linux-arm64-gnu (push) Has been cancelled
Build Graph Transformer Native Modules / Build linux-arm64-musl (push) Has been cancelled
Build Graph Transformer Native Modules / Build linux-x64-gnu (push) Has been cancelled
Workspace CI / Tests (ruqu-quantum) (push) Has been cancelled
Workspace CI / Tests (ruvix) (push) Has been cancelled
Workspace CI / Tests (rvagent) (push) Has been cancelled
Workspace CI / Tests (vector-index) (push) Has been cancelled
RuvLTRA-Small Tests / Apple Silicon Tests (push) Has been cancelled
regression-guard / reentrant-rwlock-double-write (push) Has been cancelled
regression-guard / case-insensitive-collisions (push) Has been cancelled
regression-guard / ruvector-core-no-avx512-builds-on-stable (push) Has been cancelled
RuvLTRA-Small Tests / Quantization Accuracy (push) Has been cancelled
regression-guard / hnsw-recall-at-1 (push) Has been cancelled
regression-guard / vector-db-rebuilds-index-on-open (push) Has been cancelled
regression-guard / npm-publish-pipeline (npm/packages/pi-brain) (push) Has been cancelled
regression-guard / npm-publish-pipeline (npm/packages/ruvector) (push) Has been cancelled
regression-guard / npm-publish-pipeline (npm/packages/rvf-wasm) (push) Has been cancelled
regression-guard / no-npx-execSync-in-route-enhanced (push) Has been cancelled
regression-guard / shell-injection-in-mcp-server (push) Has been cancelled
regression-guard / no-systemtime-in-wasm-crates (push) Has been cancelled
regression-guard / brain-hydration-counters-present (push) Has been cancelled
regression-guard / optional-deps-resolvable-on-npm (push) Has been cancelled
RuvLLM Benchmarks / Linux Benchmarks (NEON baseline) (push) Has been cancelled
RuvLTRA-Small Tests / Unit Tests (ubuntu-latest) (push) Has been cancelled
Build Native Modules / Build darwin-arm64 (push) Has been cancelled
Build Native Modules / Build linux-arm64-gnu (push) Has been cancelled
Build Native Modules / Build darwin-x64 (push) Has been cancelled
Build Native Modules / Build win32-x64-msvc (push) Has been cancelled
Build Native Modules / Build linux-x64-gnu (push) Has been cancelled
Build Router Native Modules / Build Router darwin-arm64 (push) Has been cancelled
Build Router Native Modules / Build Router darwin-x64 (push) Has been cancelled
Build Router Native Modules / Build Router linux-arm64-gnu (push) Has been cancelled
Build Router Native Modules / Build Router linux-x64-gnu (push) Has been cancelled
Build Router Native Modules / Build Router win32-x64-msvc (push) Has been cancelled
Build Tiny Dancer Native Modules / Build Tiny Dancer darwin-arm64 (push) Has been cancelled
Build Tiny Dancer Native Modules / Build Tiny Dancer darwin-x64 (push) Has been cancelled
Build Tiny Dancer Native Modules / Build Tiny Dancer linux-arm64-gnu (push) Has been cancelled
Build Tiny Dancer Native Modules / Build Tiny Dancer linux-x64-gnu (push) Has been cancelled
Build Tiny Dancer Native Modules / Build Tiny Dancer win32-x64-msvc (push) Has been cancelled
Workspace CI / Rustfmt (push) Has been cancelled
Workspace CI / Cargo check (push) Has been cancelled
Workspace CI / Clippy (push) Has been cancelled
Workspace CI / Tests (core-and-rest) (push) Has been cancelled
Workspace CI / Tests (core-and-rest-heavy) (push) Has been cancelled
Workspace CI / Tests (core-and-rest-wasm) (push) Has been cancelled
Workspace CI / Tests (ml-research-heavy) (push) Has been cancelled
regression-guard / no-hardcoded-workspaces-paths (push) Has been cancelled
Workspace CI / Tests (ml-research-rest) (push) Has been cancelled
regression-guard / hnsw-insert-beam-no-m2-clamp (push) Has been cancelled
regression-guard / hnsw-distance-based-neighbor-pruning (push) Has been cancelled
RuvLTRA-Small Tests / Performance Benchmarks (push) Has been cancelled
RuvLTRA-Small Tests / Stress Tests (push) Has been cancelled
RuvLTRA-Small Tests / Code Quality (push) Has been cancelled
RuvLTRA-Small Tests / Unit Tests (windows-latest) (push) Has been cancelled
RuvLTRA-Small Tests / Unit Tests (macos-latest) (push) Has been cancelled
RuvLTRA-Small Tests / E2E Tests (macos-latest) (push) Has been cancelled
RuvLTRA-Small Tests / E2E Tests (ubuntu-latest) (push) Has been cancelled
RuvLTRA-Small Tests / Thread Safety (push) Has been cancelled
Benchmarks / Compare with Baseline (push) Has been cancelled
Build Attention Native Modules / Commit Built Binaries (push) Has been cancelled
Build Attention Native Modules / Publish Attention Platform Packages (push) Has been cancelled
Build Graph Node Native Modules / Publish Graph Node Platform Packages (push) Has been cancelled
Build DiskANN Native Modules / Publish DiskANN Platform Packages (push) Has been cancelled
Build GNN Native Modules / Commit Built GNN Binaries (push) Has been cancelled
Build GNN Native Modules / Publish GNN Platform Packages (push) Has been cancelled
RuvLTRA-Small Tests / Test Summary (push) Has been cancelled
Build Graph Transformer Native Modules / Commit Built Binaries (push) Has been cancelled
Build Graph Transformer Native Modules / Publish Platform Packages (push) Has been cancelled
Build Native Modules / Commit Built Binaries (push) Has been cancelled
Build Router Native Modules / Publish Router Platform Packages (push) Has been cancelled
Build Tiny Dancer Native Modules / Publish Tiny Dancer Platform Packages (push) Has been cancelled
RuvLLM Benchmarks / Compare Benchmarks (push) Has been cancelled
thermorust CI / Test (macos-latest) (push) Has been cancelled
thermorust CI / Test (ubuntu-latest) (push) Has been cancelled
thermorust CI / Test (windows-latest) (push) Has been cancelled
thermorust CI / Benchmarks compile (push) Has been cancelled
WASM Dedup Check / check-wasm-dedup (push) Has been cancelled
ruvector-verified CI / test (push) Has been cancelled
ruvector-verified CI / bench (push) Has been cancelled
chore: Update attention NAPI-RS binaries for all platforms
Built from commit eafba64fa5

  Platforms updated:
  - linux-x64-gnu
  - linux-arm64-gnu
  - darwin-x64
  - darwin-arm64
  - win32-x64-msvc
  - wasm

  🤖 Generated by GitHub Actions
2026-05-23 10:52:21 +00:00
..
js fix: Fix PQ integration test failures and add v0.1.18 release 2025-11-30 20:45:43 +00:00
pkg chore: Update attention NAPI-RS binaries for all platforms 2026-05-23 10:52:21 +00:00
src fix(training): WASM contrastive loss + NAPI optimizer step (#339) 2026-04-06 21:41:54 -04:00
tests fix(ci): Fix formatting and workflow permission issues 2025-12-26 22:11:57 +00:00
.gitignore fix: Fix PQ integration test failures and add v0.1.18 release 2025-11-30 20:45:43 +00:00
Cargo.toml fix(security): RUSTSEC advisories + clippy hardening in RuVector (#504) 2026-05-23 05:40:24 -04:00
LICENSE feat: Add attention mechanisms documentation and fix CLI bugs 2025-12-01 15:41:17 +00:00
package.json feat(prime-radiant): Universal Coherence Engine with Sheaf Laplacian AI Safety (#131) 2026-01-22 21:27:27 -05:00
README.md feat(prime-radiant): Universal Coherence Engine with Sheaf Laplacian AI Safety (#131) 2026-01-22 21:27:27 -05:00
tsconfig.json fix: Fix PQ integration test failures and add v0.1.18 release 2025-11-30 20:45:43 +00:00

ruvector-attention-wasm

WebAssembly bindings for the ruvector-attention package, providing high-performance attention mechanisms for browser and Node.js environments.

Features

  • Multiple Attention Mechanisms:

    • Scaled Dot-Product Attention
    • Multi-Head Attention
    • Hyperbolic Attention (for hierarchical data)
    • Linear Attention (Performer-style)
    • Flash Attention (memory-efficient)
    • Local-Global Attention
    • Mixture of Experts (MoE) Attention
    • CGT Sheaf Attention (coherence-gated via Prime-Radiant)
  • Training Utilities:

    • InfoNCE contrastive loss
    • Adam optimizer
    • AdamW optimizer (with decoupled weight decay)
    • Learning rate scheduler (warmup + cosine decay)
  • TypeScript Support: Full type definitions and modern API

Installation

npm install ruvector-attention-wasm

Usage

TypeScript/JavaScript

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]);

Advanced Examples

Hyperbolic Attention

import { HyperbolicAttention } from 'ruvector-attention-wasm';

const hyperbolic = new HyperbolicAttention({
  dim: 128,
  curvature: 1.0
});

const output = hyperbolic.compute(query, keys, values);

MoE Attention with Expert Stats

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);

Training with InfoNCE Loss

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);

Learning Rate Scheduling

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();
}

Building from Source

Prerequisites

  • Rust 1.70+
  • wasm-pack

Build Commands

# 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

API Reference

Attention Mechanisms

  • MultiHeadAttention - Standard multi-head attention
  • HyperbolicAttention - Attention in hyperbolic space
  • LinearAttention - Linear complexity attention (Performer)
  • FlashAttention - Memory-efficient attention
  • LocalGlobalAttention - Combined local and global attention
  • MoEAttention - Mixture of Experts attention
  • CGTSheafAttention - Coherence-gated via Prime-Radiant energy
  • scaledDotAttention() - Functional API for basic attention

CGT Sheaf Attention (Prime-Radiant Integration)

The 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:

  • Energy-weighted attention: Lower coherence energy → higher attention
  • Automatic hallucination detection via residual analysis
  • GPU-accelerated with wgpu WGSL shaders (vec4 optimized)
  • SIMD fallback (AVX-512/AVX2/NEON)

Training

  • InfoNCELoss - Contrastive loss function
  • Adam - Adam optimizer
  • AdamW - AdamW optimizer with weight decay
  • LRScheduler - Learning rate scheduler

Utilities

  • utils.cosineSimilarity() - Cosine similarity between vectors
  • utils.l2Norm() - L2 norm of a vector
  • utils.normalize() - Normalize vector to unit length
  • utils.softmax() - Apply softmax transformation
  • utils.attentionWeights() - Compute attention weights from scores
  • utils.batchNormalize() - Batch normalization
  • utils.randomOrthogonalMatrix() - Generate random orthogonal matrix
  • utils.pairwiseDistances() - Compute pairwise distances

Performance

The WASM bindings provide near-native performance for attention computations:

  • Optimized with opt-level = "s" and LTO
  • SIMD acceleration where available
  • Efficient memory management
  • Zero-copy data transfer where possible

License

MIT OR Apache-2.0