If you add the same numbers in a different order, floating-point gives you a different answer. A CPU and a GPU almost never add in the same order. So how can a CPU-only laptop and an H100 produce bit-identical logits for the same model and prompt?
Vahana does it with one rule for every dot product. I call it the "twin" rule, because each kernel has a twin on the other device that does exactly the same arithmetic. This post explains the rule, shows why it gives identical bits by construction, and lists what it cost, with numbers from the repo's experiment files and one check I ran on my laptop today.
The rule
Every dot product y = sum_k w[k] * x[k] is computed like this:
- 16 fp32 lanes. Lane
lowns the elementsk = 16j + l. - Ascending order, fused multiply-add. Each lane walks its elements in ascending
jand doesacc = fma(w[k], x[k], acc), one rounding per step. - A fixed tree. The 16 lane sums are combined by halving: for
s = 8, 4, 2, 1:a[i] = a[i] + a[i+s]fori < s. The answer isa[0]. - Fixed rounding points. The result is rounded to bf16 at the same places on every device.
That's it. The point is not that this order is the most accurate one. The point is that it is one specific order, written down, and every implementation follows it exactly.
Why this gives identical bits
IEEE-754 defines fma(a, b, c) as the exact a*b + c, rounded once. Ordinary fp32 addition is also correctly rounded. So if two machines perform the same sequence of fma and add operations on the same inputs, they must get the same bits. There is nothing left for the hardware to choose.
What usually breaks this is that "the same sequence" is not actually the same: a BLAS library picks a blocking that depends on the CPU, a GPU kernel splits the sum across threads, the compiler fuses a multiply and an add on its own. The twin rule takes all those choices away.
In the CPU kernel (src/vahana/csrc/cpu_moe.cpp), a type called V16 holds the 16 lanes, and it is implemented four ways:
- AVX-512: one 512-bit register;
- AVX2 + FMA3: two 8-lane registers, lanes 0-7 and 8-15;
- ARM NEON: four 4-lane registers, using
vfmaq_f32; - scalar C: 16 floats and
std::fma.
The register width changes, but the arithmetic doesn't: lane 11 does the same fma chain on AVX-512 as on NEON or in plain C. The tree is one shared function:
static inline float tree16(V16 v) { float a[16]; v_store(a, v); for (int s = 8; s > 0; s >>= 1) for (int i = 0; i < s; i++) a[i] = a[i] + a[i + s]; return a[0]; } On the GPU, the Triton twin (_tree16 in src/vahana/twinlin.py) keeps a (rows, 16) fp32 accumulator, runs the same fma chain, and then reduces by reshaping to (rows, 2, 8) and summing the pair axis, then (rows, 2, 4), and so on. Summing an axis of length 2 is a single add, so it is exactly a[i] + a[i+8], then a[i] + a[i+4]: the same tree.
Two compiler rules keep it honest. Floating-point contraction is off everywhere (-ffp-contract=off and the equivalents in csrc/build.py), so the compiler can't fuse or split operations on its own. And exp is Vahana's own polynomial built from fmas (dexp), because every library's exp is a little different.
The glue was the hard part
Dot products are the easy half. A model also has norms, rotary embeddings, router softmax, activations and divisions, and those are where the devices disagreed in ways I didn't expect. From experiments/p2_twin/README.md, the first unit run matched only 26 of 43 glue ops. The causes:
- Scalar division. PyTorch's CUDA backend computes
tensor / 3.0astensor * (1/3.0), which rounds twice; the CPU divides once. Vahana now passes the divisor as a device tensor. - sqrt. PyTorch's CUDA float32
sqrtis not correctly rounded: 3124 of 16000 random inputs differed from the CPU's correct result. The twinsqrtis computed in float64 and rounded to float32, which gives the correctly rounded answer. - float64 to bf16 is rounded differently on the two devices, so results now go float64 → float32 → bf16.
After those fixes, the final unit file (p2_unit.json) shows glue 52/52 identical, attention 120/120 cases bit-identical CPU vs GPU, and every twin transcendental within 0.5 ulp of float64. A torch function mode (TwinMode) also audits every float op the model runs, and flags any op that is neither twinned nor known to be exact. For all 19 test cases the audit list is empty.
The results
Every CPU instruction set, same hashes. experiments/p1_portable_kernel/README.md hashes the output of 8 kernel paths (raw experts, twin linear, compressed NX12 experts, a fused MoE job, gpt-oss activations with biases) on five builds: AVX-512, AVX2 and scalar on an H100 server's EPYC 9354, and AVX2 and scalar on my Ryzen 7 4800H laptop. Every path gives one hash across all five builds; for example, experts with one token hash to 8279cdb07591a881 everywhere.
CPU-only equals GPU, for real models too. From experiments/p2_twin/README.md:
- 19 of 19 test cases (17 MoE families plus gpt-oss and a width-96 Mixtral variant): a CPU-only run equals the H100's CUDA-graph run, ids and every step's logits, bit for bit.
- The laptop, with no CUDA and no Triton, reproduced the H100's GPU logits for 19 of 19, under both the AVX2 and the plain-C builds.
- On the real OLMoE-1B-7B checkpoint, CPU-only Vahana equals GPU Vahana for 4 prompts × 24 greedy tokens, and for the prefill logits of all 50 prompts (603 positions).
Every OS. experiments/p3_portable/README.md: the CI wheels for Linux x86-64, Linux arm64, macOS arm64 and Windows x86-64 run vahana selftest (18 tiny families, hashes compared with golden hashes recorded on the H100 GPU). All 4 platforms and 9 ISA builds gave 18/18. That was also the first time the NEON code ran on ARM, and it matched.
A check on my laptop today
To show why the rule has to be exact, I emulated it in Python (exact fp32 fma via fractions, correct round-to-nearest-even) on 32 rows of 2048 bf16-valued weights against a random fp32 vector, and compared it with small changes to the rule and with torch matmul. Ryzen 7 4800H, Windows, torch 2.10 CPU. The script is about 60 lines of NumPy and PyTorch; it is not in the repo, but I am happy to share it on request.
- 8 lanes instead of 16 (same tree idea): 29 of 32 results differ from the twin rule.
- 16 lanes, but the lanes summed left to right instead of the tree: 22 of 32 differ.
-
torchmatmul: 28 of 32 differ. Its hash was the same at 1, 4 and 8 threads, so on this matrix-vector case torch's own result didn't depend on the thread count. - Ran twice: the twin hash was the same both times (
141470d7c15b5f8a).
None of these orders is "wrong". Against the exact sum, the largest relative error was 5.44e-06 for the twin rule, 2.75e-06 for 8 lanes, and 9.56e-06 for torch. The twin rule's accuracy is ordinary. What it gives you is that the same 16-lane, fixed-tree answer comes out on every device.
What it costs
Honest accounting from the same experiment files:
- Kernel speed: the portable AVX-512 build is +0.2% against the pre-portable kernel (1.654/1.658 ms vs 1.650/1.654 ms per MoE layer call on real DeepSeek-V2 236B layers).
- GPU decode: Mixtral-8x7B at a 4 GB GPU budget, steady graph decode step 81.28/81.42 ms vs 80.26/79.59 ms before twin attention (two runs each), +1.8%.
- Eager work is slower: the eager decode step went from about 154 ms to about 223 ms (+45%), because TwinMode dispatches the glue ops in Python. Prefill and the families that can't use CUDA graphs pay this.
- Quality: on OLMoE, top-1 agreement with a stock fp32 run over 603 positions is 98.18% for the twin build, vs 98.01% before it and 98.51% for stock bf16. Exact does not mean closer to fp32 than stock bf16; it means the same everywhere.
- Not covered: the server's seeded sampler still uses stock torch on the host, so sampled output is reproducible on one machine but not yet across CPUs. Greedy decoding is exact.
Why it matters
Once every device computes the same bits, where a layer runs stops mattering. Vahana can put experts on a GPU, in RAM or on disk, or run everything on the CPU, and the answer doesn't change. It also makes answers checkable: vahana run --receipt records a hash of every step's logits, and vahana verify recomputes them on a different machine and passes only on an exact match. That gets its own post.
Vahana is open source (Apache-2.0): https://github.com/vahana-ai/Vahana
pip install vahana vahana selftest If you run vahana selftest on a CPU or OS I haven't listed, or you find a case where two devices disagree, please open an issue with the output. A mismatch report is the most useful thing you can send.
