Skip to content

perf(attention): native faster than a JS loop (2.0-5.5x), computeFlat, full typings; @ruvector/attention 2.3.0 - #1390

Merged
ruvnet merged 2 commits into
mainfrom
weaver/rv-attention-native-perf
Nov 6, 2026
Merged

ruvnet merged 2 commits into
mainfrom
weaver/rv-attention-native-perf

Conversation

@ruvnet

@ruvnet ruvnet commented Nov 6, 2026 •

Copy link
Copy Markdown
Owner

Problem

agentic-flow measured @ruvector/attention native slower than a naive JS loop at dim 384 (flash 20/200/1000 keys native 19.8 µs / 358 µs / 3.4 ms vs JS 17.1 / 149 / 730 µs; multi_head 20 keys 47.4 vs 16.6 µs). Reproduced here, then split kernel vs boundary:

  • Rust kernel alone (no NAPI), flash 1000 keys: 163 µs. Through NAPI: 1117 µs. So about 85% of the native call was marshaling.
  • Vec<Float32Array>: napi-rs 2 creates and deletes a persistent reference for every element, plus an Arc and a finalizer box. The binding then copied every key and value into a Vec<f32> (validate::to_vecs). At 1000 keys that is 2000 reference round trips and 2000 copies per call.
  • Kernel: the dot product was one iter().zip().map().sum() dependency chain, which LLVM may not reassociate. Flash also allocated a Vec<&[f32]> and a score Vec per block, and multi-head copied every key and value into per-head Vecs.

The published index.d.ts (2.2.3) declared 4 functions (scaledDotProductAttention, multiHeadAttention, flashAttention, hyperbolicAttention) that the binary never exported. publish-all.yml wrote that file by hand, and none of the 39 real exports were typed.

Changes

  • validate::F32Rows borrows a JS Float32Array[] for the length of one synchronous call (napi_get_element + napi_get_typedarray_info, no references, no copies). Every sync compute / computeWithMask / computeWithEdges / computeWithPositions / getSpaceContributions in attention.rs and graph.rs uses it. TS signatures are unchanged (Float32Array[], via ts_arg_type). Async/batch paths still copy, because they leave the JS thread.
  • New computeFlat(query, keys, values) on Dot/MultiHead/Hyperbolic/Flash/Linear/LocalGlobal/MoE. keys and values are row-major n * dim Float32Arrays, so 3 typed arrays cross the boundary instead of 2n + 1.
  • Kernel (ruvector-attention): utils::dot_f32 (8 independent accumulators) and utils::axpy; flash reuses one score buffer and slices keys in place; multi-head runs heads on borrowed sub-slices. Outputs match the old ones to max |Δ| ≤ 5e-8 at dim 384 (summation order differs).
  • Typings: the napi-generated index.js / index.d.ts are committed (55 declarations, every native export), using the same pattern as gnn-node. publish-all.yml now copies them and README.md into the main package and sets files. It also asserts that class FlashAttention and computeFlat( are present. build-attention.yml runs node --test against the committed files on linux-x64.
  • New test test/flat-and-types.test.js: flat == nested for all 7 mechanisms, malformed flat input throws, non-Float32Array rows throw, and every Object.keys(native) export is declared in index.d.ts (the 2.2.3 file fails this). The npm test script is fixed for node 22 (node --test test/*.test.js).
  • @ruvector/attention and the platform manifests go to 2.3.0. computeFlat is new API, and ruvector's ^2.2.3 range picks it up.
  • README: computeFlat section with measured numbers. The nonexistent MultiHeadAttention.computeAsync is removed from the docs.

Breaking for TypeScript only: the 4 phantom function declarations are gone. They never existed at runtime.

Measurements

Host ruvultra (x86-64, 32 cores). Load average was 90–92 during every run (uptime 06:39: load average: 91.65, 90.56, 88.91). Every run was taskset-pinned to one core, and before/after binaries ran interleaved in the same script. Each value is the median of 3 runs of per-call medians (1500–6000 iterations each). Latencies are noisy under this load, so the ratios carry the result.

Rust kernel alone (cargo run --release -p ruvector-attention --example kernel_timing, µs):

case before after speedup
flash(384, block 64), 20 keys 3.12 1.40 2.2x
multi_head(384, 8 heads), 20 keys 9.06 2.78 3.3x
flash, 200 keys 32.96 13.84 2.4x
flash, 1000 keys 163.15 71.37 2.3x

Through Node (node 22, µs per call; JS = a straightforward softmax-attention loop over the same Float32Array[]):

case plain JS 2.2.3-equivalent compute (before) compute (after) computeFlat (after) before→after after vs JS (nested) after vs JS (flat)
flash, 20 keys 8.59 15.14 5.57 3.91 2.7x 1.54x faster 2.20x faster
multi_head(8), 20 keys 23.96 31.72 6.69 4.99 4.7x 3.58x faster 4.80x faster
flash, 200 keys 115.9 178.96 43.23 31.3 4.1x 2.68x faster 3.70x faster
flash, 1000 keys 412.88 1116.94 204.54 155.8 5.5x 2.02x faster 2.65x faster

Rerun on the final commit (after the clippy fix to dot_f32, load 90.4–90.7; JS / before / after-nested / after-flat µs): flash 20 = 8.82 / 15.22 / 5.82 / 4.76, multi_head 20 = 26.19 / 34.22 / 6.85 / 4.98, flash 200 = 98.18 / 173.8 / 60.7 / 27.14, flash 1000 = 615.52 / 1343.73 / 328.16 / 165.09. The conclusion is the same.

Native now beats the JS loop at every size, through both the nested and the flat API. Before, it was 1.3–2.7x slower.

Tests

  • cargo test -p ruvector-attention --all-features -p ruvector-attention-node: 279 passed, 0 failed
  • node --test test/*.test.js (local release build): 12/12 pass
  • cargo clippy -p ruvector-attention -p ruvector-attention-node --all-targets -- -D warnings: clean

Release plan

After merge: publish-all.yml workflow_dispatch with version=2.3.0, publish_crates=false, publish_wasm=false. Then a fresh-install verification, a benchmark rerun against the published artifact, a tarball diff and a leak scan.

🤖 Generated with claude-flow

… typings; @ruvector/attention 2.3.0

The native binding was slower than a naive JS loop (dim 384, flash 1000 keys:
1117 us native vs 413 us JS). The Rust kernel alone took 163 us, so ~85% of
the time was the boundary: Vec<Float32Array> makes napi-rs create and delete a
persistent reference (plus an Arc and finalizer box) per element, and the
binding then copied every key and value into a Vec<f32>.

- validate::F32Rows borrows a Float32Array[] for one sync call
  (napi_get_element + napi_get_typedarray_info, no references, no copies);
  used by every sync compute in attention.rs and graph.rs.
- computeFlat(query, keysFlat, valuesFlat) on the seven core mechanisms:
  three typed arrays cross the boundary instead of 2n + 1.
- Kernel: 8-lane dot product (utils::dot_f32) and axpy, a reused score
  buffer in flash, borrowed head sub-slices in multi-head (no split copies).
- Commit the napi-generated index.js/index.d.ts (55 exports) and make
  publish-all ship them instead of a hand-written 4-function index.d.ts;
  build-attention runs the node tests against the committed files.

Co-Authored-By: claude-flow <ruv@ruv.net>
…th_const_size under -D warnings)

Same codegen class; kernel timing within noise of the previous version.

Co-Authored-By: claude-flow <ruv@ruv.net>
@ruvnet
ruvnet merged commit 4404a3c into main Nov 6, 2026
62 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant