<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Vahana</title>
    <description>The latest articles on DEV Community by Vahana (@vahana).</description>
    <link>https://dev.to/vahana</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F4272664%2F5051f18f-c2e9-4718-914d-e0b0cab17005.png</url>
      <title>DEV Community: Vahana</title>
      <link>https://dev.to/vahana</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/vahana"/>
    <language>en</language>
    <item>
      <title>Introducing Vahana: run MoE models bigger than your GPU, with the same bits on every device</title>
      <dc:creator>Vahana</dc:creator>
      <pubDate>Sun, 29 Nov 2026 17:09:57 +0000</pubDate>
      <link>https://dev.to/vahana/introducing-vahana-run-moe-models-bigger-than-your-gpu-with-the-same-bits-on-every-device-4d67</link>
      <guid>https://dev.to/vahana/introducing-vahana-run-moe-models-bigger-than-your-gpu-with-the-same-bits-on-every-device-4d67</guid>
      <description>&lt;p&gt;My 4 GB laptop GPU runs a 30-billion-parameter model faster than llama.cpp, and the same laptop's CPU can check every answer bit for bit. This post introduces the engine that does it: &lt;strong&gt;Vahana&lt;/strong&gt;, open source under Apache-2.0.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F4uircsyicpv15x81v98z.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F4uircsyicpv15x81v98z.gif" alt="Generate an answer with Qwen3-30B on a 4 GB laptop GPU, then verify it bit for bit on the same laptop's CPU (real output of Vahana 1.4.0, waits sped up)" width="599" height="458"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Real output, unedited, waits sped up: Qwen3-30B 4-bit answers on the laptop's RTX 3050 Ti (4 GB) and writes a receipt; then &lt;code&gt;vahana verify&lt;/code&gt; recomputes every token's logits on the CPU and says PASS.&lt;/em&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  The problem: big MoE models, small GPUs, and answers that don't reproduce
&lt;/h2&gt;

&lt;p&gt;Mixture-of-Experts models like Qwen3-30B-A3B, Mixtral or DeepSeek-V2 are huge on disk but only use a few experts per token. That makes them a good fit for ordinary hardware: keep attention and the most-used experts on the GPU, and the rest in RAM or on disk.&lt;/p&gt;

&lt;p&gt;The catch is that once a model is split across a GPU and a CPU, the arithmetic is split too. GPU kernels and CPU kernels add numbers in different orders, so they round differently, and the output depends on where each expert happened to run. The same is true of batching: in most engines, a request's output changes with the requests batched around it (I measured this in my &lt;a href="https://dev.to/vahana/same-model-same-prompt-different-answer-where-llm-nondeterminism-really-comes-from-480m"&gt;previous post&lt;/a&gt;).&lt;/p&gt;

&lt;p&gt;Vahana's answer to both is the same: &lt;strong&gt;fix the arithmetic, everywhere&lt;/strong&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  What Vahana guarantees
&lt;/h2&gt;

&lt;p&gt;The same request gives &lt;strong&gt;bit-identical&lt;/strong&gt; tokens and logits regardless of:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;the machine:&lt;/strong&gt; a CPU-only laptop and an H100 agree bit for bit, on Linux, Windows and macOS, on AVX-512, AVX2, ARM NEON or plain C;&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;where the weights live:&lt;/strong&gt; GPU, the GPU expert cache, RAM, or streamed from disk;&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;what else is running:&lt;/strong&gt; batching, continuous batching in the server, padding, speculative decoding, prefill vs decode.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Not "close". The same bits. Every rule in the next section exists to make that true.&lt;/p&gt;

&lt;h2&gt;
  
  
  How: one arithmetic rule, implemented twice
&lt;/h2&gt;

&lt;p&gt;Every dot product follows one rule: 16 fp32 FMA lanes accumulate in ascending order, a fixed reduction tree combines them, and bf16 rounding happens at the same points. That rule is implemented identically in C++ (AVX-512, AVX2, NEON, scalar) and in Triton for NVIDIA GPUs, so an expert gives the same bits on either device. Kernels reduce each row in an order that depends only on that row, so batch neighbours and padding can't move a rounding.&lt;/p&gt;

&lt;p&gt;For 4-bit checkpoints, the product of a 4-bit weight and its group scale is exact in fp32, so the same rule carries over unchanged.&lt;/p&gt;

&lt;h2&gt;
  
  
  Receipts: verifiable answers
&lt;/h2&gt;

&lt;p&gt;Because the bits don't depend on the machine, an answer can be checked by anyone with the same weights:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;vahana run    models/Qwen3-30B-A3B-w4a16 --prompt &lt;span class="s2"&gt;"..."&lt;/span&gt; --receipt answer.json
vahana verify answer.json --model models/Qwen3-30B-A3B-w4a16 --device cpu   &lt;span class="c"&gt;# PASS or FAIL&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;A receipt records the prompt's token ids, the decoding settings, a fingerprint of the weights, the output tokens, and a SHA-256 of every step's logits. &lt;code&gt;vahana verify&lt;/code&gt; recomputes all of it in one teacher-forced pass and passes only on an exact match. There is no tolerance. In the GIF, 48 tokens of Qwen3-30B check in 8.3 s on the laptop CPU.&lt;/p&gt;

&lt;h2&gt;
  
  
  Does exactness cost speed? I measured it
&lt;/h2&gt;

&lt;p&gt;Against llama.cpp (b12494, Q4_K_M) on the same machines, each engine at its own tuned best setting, alternating runs:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Laptop, RTX 3050 Ti 4 GB, Qwen3-30B 4-bit:&lt;/strong&gt; decode 1.57× faster on five real prompts (15.3 vs 9.7 tok/s) and 1.86× on the benchmark text (21.4 vs 11.6 tok/s); first token 2.8× sooner (2.9 vs 8.2 s).&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Free Colab T4:&lt;/strong&gt; decode 1.96× on the benchmark text and 1.65× on real prompts; first token 3.4× sooner.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;16 GB Apple M4 (Vahana on the CPU, llama.cpp on Metal):&lt;/strong&gt; decode 1.98× on the benchmark text and 5.4× on real prompts; first token 1.9× sooner.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Mixtral-8x7B on a T4:&lt;/strong&gt; decode about level; first token 2.0× sooner.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Plain 2-core CPU (Kaggle), no GPU:&lt;/strong&gt; &lt;strong&gt;llama.cpp is about 3.5× faster&lt;/strong&gt; (7.91 vs 2.21 tok/s). llama.cpp's int8 dot products handle many weights per instruction; a fixed fp32 order can't.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Quality, against the bf16 original on WikiText-2: the 4-bit checkpoint Vahana runs has a mean KL divergence of 0.065, vs 0.093 for Q4_K_M (both scored with llama.cpp's own statistics).&lt;/p&gt;

&lt;p&gt;On a server: DeepSeek-Coder-V2 236B (438 GB) runs at 7.49 tok/s with a 16 GB GPU budget, and Qwen3-30B-A3B serves 64 requests at once at 130 tok/s aggregate, with every response bit-identical to running it alone. On the GPU, exactness costs +1.8% per decode step.&lt;/p&gt;

&lt;p&gt;Every number above comes from a pre-registered experiment in the repo, including the bars it missed. The sources: &lt;a href="https://github.com/vahana-ai/Vahana/blob/main/docs/benchmarks.md" rel="noopener noreferrer"&gt;docs/benchmarks.md&lt;/a&gt;, &lt;a href="https://github.com/vahana-ai/Vahana/blob/main/experiments/v15_decode/README.md" rel="noopener noreferrer"&gt;experiments/v15_decode&lt;/a&gt;, &lt;a href="https://github.com/vahana-ai/Vahana/blob/main/experiments/v15_quality/README.md" rel="noopener noreferrer"&gt;experiments/v15_quality&lt;/a&gt;, and the one-command reproduction in &lt;a href="https://github.com/vahana-ai/Vahana/blob/main/experiments/reproduce/README.md" rel="noopener noreferrer"&gt;experiments/reproduce&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Which models
&lt;/h2&gt;

&lt;p&gt;18 MoE families pass the exactness selftest. Real checkpoints are validated for Mixtral, DeepSeek-V2 (Lite and 236B), Qwen1.5-MoE, Qwen3-MoE, Qwen3.5-MoE and OLMoE; DeepSeek-V3, GLM-4-MoE, MiniMax-M2, gpt-oss (bf16 experts) and others are validated on the real architecture with tiny weights. Not yet supported: MXFP4 and FP8 checkpoints, Llama 4, DBRX. One NVIDIA GPU or none; Macs run on the CPU.&lt;/p&gt;

&lt;h2&gt;
  
  
  Try it in five minutes
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;pip install torch &lt;span class="s2"&gt;"vahana[gpu]"&lt;/span&gt;     &lt;span class="c"&gt;# omit [gpu] without an NVIDIA GPU&lt;/span&gt;
vahana selftest                     &lt;span class="c"&gt;# does this machine give the reference bits?&lt;/span&gt;
hf download allenai/OLMoE-1B-7B-0924 --local-dir models/OLMoE-1B-7B-0924
vahana run models/OLMoE-1B-7B-0924 --prompt &lt;span class="s2"&gt;"The capital of France is"&lt;/span&gt; --max-tokens 16 --receipt answer.json
vahana verify answer.json --model models/OLMoE-1B-7B-0924 --device cpu
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;There's also an OpenAI-compatible server (&lt;code&gt;vahana serve ... --slots 32&lt;/code&gt;) with continuous batching, where every response equals the same request run alone.&lt;/p&gt;

&lt;h2&gt;
  
  
  Where I'd love help
&lt;/h2&gt;

&lt;p&gt;Run it on your machine and tell me what you get, especially where it loses. Speed numbers from other GPUs and CPUs, models that fail to load, and receipts that don't verify across machines are all exactly the reports I want.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;GitHub: &lt;a href="https://github.com/vahana-ai/Vahana" rel="noopener noreferrer"&gt;https://github.com/vahana-ai/Vahana&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;&lt;code&gt;pip install vahana&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Next posts: how the CPU and GPU kernels are made to agree bit for bit, and how the expert cache fits a 30B model in 4 GB.&lt;/p&gt;

</description>
      <category>opensource</category>
      <category>llm</category>
      <category>machinelearning</category>
      <category>python</category>
    </item>
    <item>
      <title>Same model, same prompt, different answer: where LLM nondeterminism really comes from</title>
      <dc:creator>Vahana</dc:creator>
      <pubDate>Sun, 29 Nov 2026 16:34:27 +0000</pubDate>
      <link>https://dev.to/vahana/same-model-same-prompt-different-answer-where-llm-nondeterminism-really-comes-from-480m</link>
      <guid>https://dev.to/vahana/same-model-same-prompt-different-answer-where-llm-nondeterminism-really-comes-from-480m</guid>
      <description>&lt;p&gt;&lt;em&gt;Every number below comes from a script I ran on my laptop (PyTorch 2.10, CPU, 8 threads). The script is at the end, so you can run it on yours.&lt;/em&gt;&lt;/p&gt;




&lt;p&gt;Set temperature to 0. Send the same prompt to the same model twice. You'd expect the same answer twice. Often you don't get it, and not because of sampling. Greedy decoding is deterministic by definition: take the highest-scoring token, every time.&lt;/p&gt;

&lt;p&gt;So where does the difference come from? Not from randomness. It comes from &lt;strong&gt;arithmetic order&lt;/strong&gt;, and once you see it, it shows up everywhere.&lt;/p&gt;

&lt;h2&gt;
  
  
  1. Floating-point addition doesn't care what you learned in school
&lt;/h2&gt;

&lt;p&gt;In real numbers, (a + b) + c = a + (b + c). In floating point it isn't guaranteed:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="o"&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mf"&gt;0.1&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mf"&gt;0.2&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mf"&gt;0.3&lt;/span&gt;
&lt;span class="mf"&gt;0.6000000000000001&lt;/span&gt;
&lt;span class="o"&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="mf"&gt;0.1&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mf"&gt;0.2&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mf"&gt;0.3&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="mf"&gt;0.6&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Every addition rounds to the nearest representable number, and &lt;em&gt;where&lt;/em&gt; the rounding happens depends on the order. With three numbers the error is tiny. A transformer layer adds thousands of products for every output value.&lt;/p&gt;

&lt;p&gt;Here are 200,000 random fp32 numbers summed three ways:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Order&lt;/th&gt;
&lt;th&gt;Result&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Left to right&lt;/td&gt;
&lt;td&gt;26.134367&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Right to left&lt;/td&gt;
&lt;td&gt;26.134922&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Pairwise (NumPy)&lt;/td&gt;
&lt;td&gt;26.135086&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Exact (fp64)&lt;/td&gt;
&lt;td&gt;26.135116&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;Same numbers, three different answers. None of them is "wrong"; they're all legal fp32 results. They're just different.&lt;/p&gt;

&lt;h2&gt;
  
  
  2. Your batch size changes the order
&lt;/h2&gt;

&lt;p&gt;You never choose the order of additions inside a matrix multiply: the library does. And libraries choose differently depending on the &lt;strong&gt;shape&lt;/strong&gt; of the problem, because a different tiling or blocking is faster for a different shape.&lt;/p&gt;

&lt;p&gt;The experiment: take one row &lt;code&gt;x&lt;/code&gt;, multiply it by a 4096×4096 weight matrix, then do the same row again inside a batch of other rows, and compare only that row's result.&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Batch the row was computed in&lt;/th&gt;
&lt;th&gt;Bit-identical to batch of 1?&lt;/th&gt;
&lt;th&gt;Output values that differ (of 4096)&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;1, 2, 3&lt;/td&gt;
&lt;td&gt;yes&lt;/td&gt;
&lt;td&gt;0&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;8, 16, 32, 64&lt;/td&gt;
&lt;td&gt;&lt;strong&gt;no&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;&lt;strong&gt;3,993&lt;/strong&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;Same row, same weights, same machine, same library. Put the row in a batch of 8 and 97% of its outputs change in the last bits (the largest difference was 0.00047). Somewhere between batch 3 and batch 8 the library switched to a different kernel path, and the reduction order changed with it.&lt;/p&gt;

&lt;p&gt;This is why an inference server can give a different answer to &lt;em&gt;your&lt;/em&gt; request depending on &lt;strong&gt;who else is using it at the same moment&lt;/strong&gt;. Your request gets batched with strangers; the batch size changes the kernel; the kernel changes the order; the order changes the bits.&lt;/p&gt;

&lt;h2&gt;
  
  
  3. "It's only the last bits", until it isn't
&lt;/h2&gt;

&lt;p&gt;Models usually run in bf16, which keeps only 8 bits of mantissa. Rounding to bf16 hides most of those tiny differences: of the 3,993 differing values above, &lt;strong&gt;only 2 still differed&lt;/strong&gt; after rounding to bf16.&lt;/p&gt;

&lt;p&gt;That sounds reassuring. It's actually the dangerous part. The differences don't disappear evenly; they survive exactly where a value sits near a rounding boundary. And a language model makes a hard decision every token: the argmax. When two candidate tokens are close (which happens constantly in long generations), a difference in the last bit of a logit picks a different word. From that token on, the two runs are reading different text, and they diverge completely.&lt;/p&gt;

&lt;p&gt;So: tiny arithmetic difference → rarely visible → occasionally flips one token → the rest of the answer is different.&lt;/p&gt;

&lt;h2&gt;
  
  
  4. The full list of things that change the order
&lt;/h2&gt;

&lt;p&gt;Once you think in terms of "who decides the summation order", the sources line up:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Batch size and padding.&lt;/strong&gt; Different shapes, different kernels (shown above).&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Hardware.&lt;/strong&gt; A different GPU generation or CPU instruction set (AVX2 vs AVX-512 vs ARM NEON) has different vector widths, so the natural way to split a sum changes.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Library and driver versions.&lt;/strong&gt; A new cuBLAS, oneDNN or PyTorch release can pick a new kernel for the same shape.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Thread count.&lt;/strong&gt; Parallel reductions split work by thread; 8 threads and 14 threads split a sum differently.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Kernel tricks for speed.&lt;/strong&gt; Split-K matmuls, atomics in reductions, fused attention kernels that tile the sequence differently for prefill and decode.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Where the weights live.&lt;/strong&gt; Offloading systems that compute some layers on the CPU and some on the GPU are mixing two different arithmetic implementations by construction.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Note what's &lt;em&gt;not&lt;/em&gt; on the list: true randomness. Run the exact same thing on the exact same setup and you usually do get the same bits. The problem is that "the exact same setup" almost never holds in production.&lt;/p&gt;

&lt;h2&gt;
  
  
  5. Why anyone should care
&lt;/h2&gt;

&lt;p&gt;If outputs only matched to within a tolerance, you might shrug. Three places where you can't:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Evaluations.&lt;/strong&gt; Benchmark scores move between inference backends and batch sizes. If your eval number changes when you change the serving setup, you're measuring the setup, not the model.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Debugging.&lt;/strong&gt; "It produced this bad output yesterday" is hard to investigate when you can't reproduce yesterday's output.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Verification.&lt;/strong&gt; If a provider says "this answer came from model X with these settings", you can only check that claim by recomputing it. Recomputing only proves something if the bits are supposed to match.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  6. Can it be fixed?
&lt;/h2&gt;

&lt;p&gt;Yes, and the fix is conceptually simple: &lt;strong&gt;stop letting the shape, the hardware or the library choose the order.&lt;/strong&gt; Pick one summation order for every dot product, and implement that exact order everywhere the model runs: every batch size, every kernel, every device. Then "the same request gives the same bits" stops being luck and becomes a property you can test.&lt;/p&gt;

&lt;p&gt;The cost is that you give up some of the freedom kernels use to go fast. How much that costs, and where it costs nothing at all, is the interesting part. I'll write about that next.&lt;/p&gt;

&lt;p&gt;Further reading: Horace He and Thinking Machines Lab, &lt;em&gt;Defeating Nondeterminism in LLM Inference&lt;/em&gt; (2025), on batch invariance in GPU kernels.&lt;/p&gt;




&lt;h2&gt;
  
  
  The script (reproduce every number above)
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="n"&gt;numpy&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="n"&gt;np&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;torch&lt;/span&gt;

&lt;span class="c1"&gt;# 1. order of summation&lt;/span&gt;
&lt;span class="n"&gt;rng&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;np&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;random&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;default_rng&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;rng&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;standard_normal&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;200_000&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;astype&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;np&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;float32&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;fwd&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;np&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;float32&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;v&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;fwd&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;np&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;float32&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;fwd&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;v&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;rev&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;np&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;float32&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;v&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="o"&gt;::-&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;rev&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;np&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;float32&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;rev&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;v&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;fwd&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;rev&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;np&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nb"&gt;sum&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;dtype&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;np&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;float32&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;np&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nb"&gt;sum&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;astype&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;np&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;float64&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="c1"&gt;# 2. batch size changes the bits of one row&lt;/span&gt;
&lt;span class="n"&gt;torch&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;manual_seed&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="no"&gt;W&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;torch&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;randn&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;4096&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;4096&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="no"&gt;X&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;torch&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;randn&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;64&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;4096&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;one&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="no"&gt;X&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; @ &lt;span class="no"&gt;W&lt;/span&gt;
&lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;bs&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;8&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;16&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;32&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;64&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="n"&gt;row&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="no"&gt;X&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="n"&gt;bs&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; @ &lt;span class="no"&gt;W&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
    &lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;bs&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;torch&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;equal&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;row&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;one&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;row&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="n"&gt;one&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nb"&gt;sum&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="c1"&gt;# 3. after bf16 rounding&lt;/span&gt;
&lt;span class="n"&gt;full&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="no"&gt;X&lt;/span&gt; @ &lt;span class="no"&gt;W&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;one&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;bfloat16&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="n"&gt;full&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;bfloat16&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nb"&gt;sum&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Results depend on your CPU and PyTorch build; that's the point. Run it and see where &lt;em&gt;your&lt;/em&gt; machine switches kernels.&lt;/p&gt;

</description>
      <category>llm</category>
      <category>machinelearning</category>
      <category>python</category>
      <category>ai</category>
    </item>
  </channel>
</rss>
