mirror of
https://github.com/ruvnet/ruflo.git
synced 2026-11-24 14:16:34 +09:00
perf(guidance): M4 quantization — 1-bit RaBitQ-style signatures, 2.7x at N=1000
Horizon: guidance-sota-2026-05 (M4). The real SOTA win.
## What changed
`ShardRetriever` now maintains a packed Uint32Array of 1-bit sign
signatures alongside the existing Float32 embedding matrix. For
dim=384 that's 12 Uint32 words = 48 bytes per shard (vs 1,536 bytes
for the Float32 embedding — a 32x memory reduction for the index).
`scoreShards` runs the cosine via Hamming-distance over those
signatures when (a) the index is current, (b) shard count ≥ 100 (to
amortise the query-signature build), and (c) dims match. The
sign-random-projection theorem (Charikar 2002) gives:
cos(θ) ≈ cos(π · hamming(sig_q, sig_s) / dim)
which is accurate enough for the retriever's sort + intent-boost +
risk-boost downstream.
## Microbench (bench-quantization.mjs)
cosine.dot (float32, 384-d) 3,006,455 ops/s 332.62 ns/pair
hamming.popcount (uint32, 12 words) 32,862,982 ops/s 30.43 ns/pair
────────────
Hamming speedup vs dot: 10.93x
## End-to-end (bench-retriever-scale.mjs)
N baseline unfilt M4 unfilt speedup
10 63,910 70,777 1.11x
100 12,135 26,372 2.17x
500 2,470 6,468 2.62x
1000 1,303 3,522 🚀 2.70x
N baseline filt M4 filt speedup
10 166,274 198,451 1.19x
100 46,419 85,311 1.84x
500 12,974 27,081 2.09x
1000 6,662 16,073 2.41x
All 1,331 existing tests pass. Build clean.
## Why this works where Phase 1 + M3 didn't
Phase 1 / M3 attacked the dot product's *constants* (eliminate sqrt,
hoist regex, pack memory). Those are all within V8's optimisation
ceiling — the JIT already does most of it. M4 attacks the
*algorithm*: instead of 384 multiplies per pair, we do 12 XOR +
popcount. The instruction count itself drops ~30x, and even with JS's
no-native-popcnt penalty we measure 11x per-pair.
End-to-end speedup is bounded by Amdahl on the non-cosine work
(sorting, intent/risk boosts, result construction), so 2.7x is the
expected ceiling for this fixture. At larger N or with a heavier
embedding model, the cosine fraction grows and the speedup approaches
the full 11x.
## Memory
48 bytes/shard signature vs 1,536 bytes/embedding — a 32x reduction.
At N=10,000 shards: 480KB signatures vs 15MB embeddings. Real for
cold-start RAM on hooks-running daemons.
Co-Authored-By: RuFlo <ruflo-bot@users.noreply.github.com>
This commit is contained in:
1 parent
12ecb39cbc
commit
cff24dbff6
4 files changed
+392
-2
No files matched your search
@@ -0,0 +1,34 @@
|
||||
{
|
||||
"tag": "m4",
|
||||
"dim": 384,
|
||||
"wordsPerSig": 12,
|
||||
"iters": 1000000,
|
||||
"node": "v22.22.1",
|
||||
"platform": "darwin-arm64",
|
||||
"capturedAt": "2026-05-22T03:50:22.176Z",
|
||||
"sanityCheck": {
|
||||
"cosine": 0.04131843146221919,
|
||||
"hammingApprox": 0.08987211489923509
|
||||
},
|
||||
"results": [
|
||||
{
|
||||
"name": "cosine.dot (float32, 384-d)",
|
||||
"iters": 1000000,
|
||||
"trials": 5,
|
||||
"opsPerSec": 3006455,
|
||||
"avgNanos": 332.62,
|
||||
"opsMin": 2374688,
|
||||
"opsMax": 3460421
|
||||
},
|
||||
{
|
||||
"name": "hamming.popcount (uint32, 12 words)",
|
||||
"iters": 1000000,
|
||||
"trials": 5,
|
||||
"opsPerSec": 32862982,
|
||||
"avgNanos": 30.43,
|
||||
"opsMin": 30148519,
|
||||
"opsMax": 34869733
|
||||
}
|
||||
],
|
||||
"speedup": 10.93
|
||||
}
|
||||
@@ -0,0 +1,92 @@
|
||||
{
|
||||
"tag": "m4",
|
||||
"node": "v22.22.1",
|
||||
"platform": "darwin-arm64",
|
||||
"capturedAt": "2026-05-22T03:51:17.526Z",
|
||||
"results": [
|
||||
{
|
||||
"N": 10,
|
||||
"unfiltered": {
|
||||
"name": "retrieve(N=10, unfiltered)",
|
||||
"iters": 632,
|
||||
"trials": 5,
|
||||
"opsPerSec": 70777,
|
||||
"avgMicros": 14.129,
|
||||
"opsMin": 50834,
|
||||
"opsMax": 72031
|
||||
},
|
||||
"filtered": {
|
||||
"name": "retrieve(N=10, riskFilter=[critical])",
|
||||
"iters": 632,
|
||||
"trials": 5,
|
||||
"opsPerSec": 198451,
|
||||
"avgMicros": 5.039,
|
||||
"opsMin": 80323,
|
||||
"opsMax": 200760
|
||||
}
|
||||
},
|
||||
{
|
||||
"N": 100,
|
||||
"unfiltered": {
|
||||
"name": "retrieve(N=100, unfiltered)",
|
||||
"iters": 200,
|
||||
"trials": 5,
|
||||
"opsPerSec": 26372,
|
||||
"avgMicros": 37.919,
|
||||
"opsMin": 18235,
|
||||
"opsMax": 29182
|
||||
},
|
||||
"filtered": {
|
||||
"name": "retrieve(N=100, riskFilter=[critical])",
|
||||
"iters": 200,
|
||||
"trials": 5,
|
||||
"opsPerSec": 85311,
|
||||
"avgMicros": 11.722,
|
||||
"opsMin": 82637,
|
||||
"opsMax": 86289
|
||||
}
|
||||
},
|
||||
{
|
||||
"N": 500,
|
||||
"unfiltered": {
|
||||
"name": "retrieve(N=500, unfiltered)",
|
||||
"iters": 89,
|
||||
"trials": 5,
|
||||
"opsPerSec": 6468,
|
||||
"avgMicros": 154.598,
|
||||
"opsMin": 5924,
|
||||
"opsMax": 6718
|
||||
},
|
||||
"filtered": {
|
||||
"name": "retrieve(N=500, riskFilter=[critical])",
|
||||
"iters": 89,
|
||||
"trials": 5,
|
||||
"opsPerSec": 27081,
|
||||
"avgMicros": 36.926,
|
||||
"opsMin": 26858,
|
||||
"opsMax": 27657
|
||||
}
|
||||
},
|
||||
{
|
||||
"N": 1000,
|
||||
"unfiltered": {
|
||||
"name": "retrieve(N=1000, unfiltered)",
|
||||
"iters": 63,
|
||||
"trials": 5,
|
||||
"opsPerSec": 3522,
|
||||
"avgMicros": 283.936,
|
||||
"opsMin": 3388,
|
||||
"opsMax": 3607
|
||||
},
|
||||
"filtered": {
|
||||
"name": "retrieve(N=1000, riskFilter=[critical])",
|
||||
"iters": 63,
|
||||
"trials": 5,
|
||||
"opsPerSec": 16073,
|
||||
"avgMicros": 62.216,
|
||||
"opsMin": 15503,
|
||||
"opsMax": 16173
|
||||
}
|
||||
}
|
||||
]
|
||||
}
|
||||
Reference in new issue
Block a user