| # Megakernel family — measured calibration |
|
|
| All numbers from a Llama-3.2-1B-shaped model (16 layers, d=2048, ffn=8192, 32 q / 8 kv heads, |
| head_dim 64, vocab 128256, tied lm_head) on one H200, torch 2.11 / triton 3.6. |
| Scripts: `scratchpad/mk_calib{,2,3,4}.py`. |
|
|
| ## 1. Seeded random weights are numerically sound |
|
|
| `1/sqrt(fan_in)` scaled init. Activation RMS 1.13 (layer 0) -> 4.65 (layer 15), a 4.1x growth over |
| 16 layers. Logits: mean -0.005, std 1.000, max|.| 4.13, all finite. |
|
|
| Naive `randn` (unscaled) is NOT usable — it diverges over depth and the logit comparison degenerates |
| into comparing noise. |
|
|
| ## 2/3. The kernel-count gate is sound |
|
|
| | | kernel launches / step | dominant share | |
| |---|---|---| |
| | eager torch | **628** | 0.184 | |
| | CUDA-graphed torch | **628** | 0.184 | |
| | gate | <= 8 | >= 0.90 | |
|
|
| **CUDA Graphs do not reduce kernel count** — a graph replays the same nodes, it only removes launch |
| overhead. Eager and graphed both miss the gate by ~78x. This is what makes the gate enforceable |
| without any source inspection: a submission either fused the model or it did not. |
|
|
| ## 4. There is real headroom to win |
|
|
| | | ms/step | tok/s | x floor | |
| |---|---|---|---| |
| | weight-bandwidth floor (2.47 GB @ 4.8 TB/s) | 0.515 | 1942 | 1.0 | |
| | eager wall (python-dispatch bound) | 8.294 | 121 | 16.1 | |
| | eager GPU busy | 2.063 | 485 | 4.0 | |
| | **CUDA-graphed wall (the real bar)** | **2.119** | **472** | **4.1** | |
|
|
| Eager wall-clock is python-bound and is NOT a meaningful baseline — quote the graphed number. |
| 4.1x of headroom between graphed torch and the bandwidth floor is what a megakernel competes for. |
|
|
| ## 5. Correctness gate: logit relative error, NOT top-k agreement |
|
|
| Top-k agreement was the original proposal. It does not work: random weights give near-uniform logits, |
| so top-1/top-2 are often near-tied and flip on any numerical noise. |
|
|
| | variant | top1 agree | top10 overlap | relerr top10 | relerr all | |
| |---|---|---|---|---| |
| | bf16 vs fp32 | 1.000 | 0.971 | 0.0042 | **0.0161** | |
| | fp8 weights vs **fp32 weights** | 0.833 | 0.767 | 0.0317 | 0.1369 | |
| | fp8 weights vs **same fp8 bytes** | 0.917 | 0.967 | 0.0038 | **0.0141** | |
| | bf16 rerun (determinism floor) | 1.000 | — | — | **0.0000** | |
|
|
| Depth sensitivity (bf16 vs fp32), relerr is stable while top-1 collapses: |
|
|
| | layers | top1 | relerr all | |
| |---|---|---| |
| | 16 | 1.000 | 0.0161 | |
| | 32 | 1.000 | 0.0186 | |
| | 48 | 0.792 | 0.0209 | |
|
|
| **Rule: gate on full-logit relative error, per-task tol set at ~2x the measured bf16 value.** |
| 16L -> 3e-2, 32L -> 3.5e-2, 48L -> 4e-2. |
|
|
| ## 6. fp8 tasks must ship PRE-QUANTISED weights |
|
|
| Quantisation error is not the kernel's error. If the fixture is fp32 and the agent quantises, a |
| correct fp8 megakernel disagrees with the reference on 17% of steps (relerr 0.137). Shipping the |
| weights already in e4m3 + per-channel scales, and having the reference dequantise those same bytes, |
| brings it to relerr 0.014 — as tight as bf16. |
|
|
| ## 7. Harness validation — the gate matrix |
|
|
| Three probe submissions, run against the generated grader: |
|
|
| | submission | gate 1 correct | gate 2 kernels/step | gate 3 dominant | reward | |
| |---|---|---|---|---| |
| | stub (`NotImplementedError`) | error | — | — | **0** | |
| | unfused reference | PASS (relerr 0.0) | FAIL (644) | FAIL (0.18) | **0** | |
| | one Triton kernel, wrong math | FAIL (relerr 1.40) | PASS (1.0) | PASS (1.0) | **0** | |
|
|
| The gates are independent and only a submission that is *both* correct *and* fused can score. Note the |
| usual lane check ("reference-as-submission must score > 0") does NOT apply to this family by design — |
| the reference is not a megakernel, so it must score 0. |
|
|
| ## 8. Allocator-induced noise floor (fp8 only) |
|
|
| Two **independent but identical** fp8 builds diverge by relerr ~1.4e-2 over 8 decode steps. Cause: |
| dequantisation allocates ~2.5 GB of fresh tensors, the two builds land at different addresses, cuBLAS |
| selects different GEMV algorithms, and the resulting 1-ULP difference compounds through the KV cache. |
| bf16 and long-context (no dequant allocation) are bit-identical at 0.0. |
|
|
| Tolerances are therefore set per task, at ~2x the combined expected error: |
|
|
| | task | noise floor | expected impl error | combined | tol | |
| |---|---|---|---|---| |
| | bf16 | 0.000 | 0.016 | 0.016 | 3e-2 | |
| | long-context | 0.000 | 0.016 | 0.016 | 3e-2 | |
| | fp8 | 0.014 | 0.016 | 0.021 | **4e-2** | |
|
|
| ## 9. Measured rooflines |
|
|
| | task | weights | KV | floor | eager us/step | x floor | |
| |---|---|---|---|---|---| |
| | bf16 | 2.47 GB | 0.07 GB | 529 us | 8418 | 15.9 | |
| | fp8 | 1.24 GB | 0.07 GB | 272 us | 7859 | 28.9 | |
| | long-context | 2.47 GB | 1.07 GB | 739 us | 8684 | 11.8 | |
|
|
| (eager is python-dispatch bound; the honest bar is CUDA-graphed torch at ~4x the floor.) |
|
|
| ## 10. Solvability proof (private — not shipped to agents) |
|
|
| A compliant megakernel was written to confirm the three gates are simultaneously satisfiable: |
| ONE persistent Triton kernel, grid of 64 co-resident blocks, all 16 layers + attention + the tied 128k |
| LM head inside it, cross-layer dependencies as grid-wide atomic barriers (5 barriers/layer). |
|
|
| | pilot | reward | correct | k/step | dominant | |
| |---|---|---|---|---| |
| | bf16 | **334.98 tok/s** | 1.0 | 3.0 | 0.9993 | |
| | fp8 | **335.17 tok/s** | 1.0 | 3.0 | 0.999 | |
| | long-context 32k | **81.15 tok/s** | 1.0 | 3.0 | 0.999 | |
|
|
| (3 launches, not 1, because `bar.zero_()` and the token `copy_()` are each a kernel. Both trivial, so |
| the dominant share stays at 0.999.) |
|
|
| Two things this proved that guesswork would not have: |
|
|
| 1. **A Triton grid-wide barrier works** (verified standalone first, then in situ) — so Triton is a |
| viable path, not just CUDA. Requires a co-resident grid or the spinning blocks deadlock. |
| 2. **The tolerance was mis-set.** The proof kernel keeps the residual stream in fp32; the reference |
| keeps it in bf16 (what production serving stacks do). Both are legitimate and they differ by 0.031, |
| which FAILED the original 3e-2 gate. A tolerance that rejects the *more precise* implementation is |
| simply wrong, so all three pilots moved to 5e-2 and the precision policy now states explicitly that |
| either residual dtype passes. |
|
|
| Note the proof kernel (335 tok/s) does NOT beat CUDA-graphed torch (472 tok/s) — it round-trips every |
| intermediate through HBM scratch. Graphed torch scores 0 (fails gate 2), so 335 is a valid leaderboard |
| entry, but it shows the headroom is real and unclaimed: the floor is 1942 tok/s. |
|
|
| ## 11. The bf16 output floor — corrected formula (measured) |
|
|
| A tempting but WRONG rule: "a bf16 output implies a relative-error floor of `eps_bf16/(2*sqrt(3))` |
| = 2.3e-3, so any tolerance under that rejects correct kernels." That figure is the error between a bf16 |
| value and an *exact* one. It is NOT the floor between two implementations that both round to bf16. |
|
|
| Measured (`scratchpad/floor.py`): two implementations that both accumulate in **fp32** and round **once** |
| at the end disagree only on elements straddling a rounding boundary, giving |
|
|
| relerr ~= sqrt(delta * ulp_bf16) |
| |
| where `delta` is their fp32-level disagreement: |
|
|
| | fp32-level `delta` | resulting bf16-output disagreement | |
| |---|---| |
| | 1e-7 | 2.3e-5 | |
| | 1e-5 | 2.1e-4 | |
| | 1e-3 | 2.1e-3 | |
|
|
| Reaching 2.3e-3 requires `delta ~ 1e-3` — i.e. doing the arithmetic itself in bf16, which the specs |
| already forbid. So **a bf16 output does not imply a 2.3e-3 floor**; measured floors for fp32-accumulating |
| kernels are 2e-5 to 1e-4, and tolerances of 1e-4..5e-4 on bf16 outputs can be perfectly correct. |
|
|
| Nine tasks were audited against the wrong rule; eight were false alarms and two were bit-exact |
| (`gradient-accumulation-fused`, `dist-tp-embedding-allreduce`) — for those a very tight gate is a |
| correct exactness check, not a sub-floor mistake. |
|
|
| The hunt still paid: **`quantized-optimizer-state` had a 1.01x margin** (correct Triton kernel 4.93e-4 |
| vs a 5e-4 gate) — the same defect class as megakernel-mamba-hybrid. Raised to 2e-3. |
|
|
| ### Known weakness, not yet fixed |
|
|
| `quantized-optimizer-state`'s discrimination is only 4.3x (target is >=10x), and the cause is the |
| FIXTURE, not the tolerance: when an element's `v_code` is 0 and its gradient is ~0, `v_new` underflows, |
| the AdamW denominator collapses to `eps=1e-8`, and that element's update becomes ~1e4x typical. The top |
| 8 elements then carry 11-50% of the whole tensor's energy, so the relative-Frobenius gate is effectively |
| reading a handful of hypersensitive values and E swings 10x between seeds. The proper fix is to clamp |
| `v_absmax` away from zero in the input generator. |
|
|
| ## 12. Quantisation fixtures: constrain the input span, and make SCALES exactly representable |
|
|
| Quantisation tasks have a failure mode the rest of the lane does not: the measured error depends on |
| the input span, so a tolerance measured on a few seeds can be a lottery. |
|
|
| ### The rule that matters: per-TENSOR scales must be exactly representable |
|
|
| A scale drawn from a continuous distribution is not representable in bf16/fp16, so a kernel that |
| carries it in anything narrower than fp32 incurs a rounding error. Whether that error is visible |
| depends entirely on the scale's GRANULARITY: |
|
|
| * **per-tensor (a scalar)** -- the rounding error multiplies the WHOLE output coherently, so it shows |
| up at full magnitude and is pure seed lottery. |
| * **per-row / per-block** -- independent rounding errors average out over the reduction, so the |
| aggregate effect is small. |
|
|
| Measured on `nvfp4-dequant-gemm`, whose `global_scale` was an arbitrary fp32 scalar in [0.02, 0.06]: |
|
|
| | fixture | E over 80 seeds | spread | headroom at worst seed (tol 5e-3) | |
| |---|---|---|---| |
| | `(0.02 + 0.04*rand()).float()` | 1.3e-05 .. 2.9e-03 | **221x** | **1.73x** | |
| | `.to(torch.bfloat16).float()` | 0.0 .. 0.0 | 1x | deterministic | |
|
|
| Note the tail kept GROWING with more seeds (max 2.4e-3 at 20 seeds, 2.9e-3 at 80): measuring a |
| tolerance on a handful of seeds understates it. And the grader validates the last TIMED rep on seeds |
| 10000+i, which are different from the correctness seeds 100+i -- so an unstable E can pass correctness |
| and fail the timed check. |
|
|
| Scales are METADATA. These tasks grade the dequant-GEMM, not the rounding of one scalar. Round |
| per-tensor scales onto the bf16 grid at generation so bf16, fp16 and fp32 all agree exactly. |
| By contrast `int8-w8a8-gemm`'s per-row `a_scale`/`b_scale` measured only a 1.29x spread and were |
| deliberately LEFT alone -- rounding them would misrepresent real int8 dequant, which is fp32. |
|
|
| ### The other two span effects, measured and found mild here |
|
|
| * **Energy concentration.** Relative Frobenius error is dominated by the largest output elements -- |
| which in a quantised kernel are exactly the ones that SET absmax and are best represented, so the |
| metric under-weights the small elements quantisation damages most. Measured across the family the |
| top 0.1% of elements carry 1-11% of the energy, which is not pathological. It DOES become |
| pathological when a fixture admits denormal/underflow outliers: see section 11, where 8 elements |
| carried 86% of the energy. |
| * **Block dynamic range.** `absmax/rms` over 128-element blocks measured 2.4-5.1 across the family; a |
| gaussian block of that size expects ~3.2, so no task is currently outlier-dominated. If a task ever |
| wants to model real activation outliers (AWQ/SmoothQuant), inject them at a FIXED count and |
| magnitude rather than relying on the tail of `randn`, so the fixture is reproducible. |
|
|
| ### Span audit of the 23 remaining precision findings |
|
|
| `_factory/span_check.py` was run over all 23. **No new span defects were found** — nvfp4's per-tensor |
| scale remains the only one. |
|
|
| | outcome | n | detail | |
| |---|---|---| |
| | bit-exact, span irrelevant | 9 | E = 0.0 across 60 seeds (permutation/gather/layout/integer tasks) | |
| | audited, healthy | 4 | seed ratio 1.0-1.1x; energy_top0.1% 1.3-15.7%; absmax/rms 5.2-6.8 | |
| | generic bf16 twin not meaningful | 6 | fp8/packed inputs, where a blanket bf16 round-trip is near-identity | |
| | separate harness | 3 | megakernel family: weights are seeded random and quantised fixtures are pre-quantised by rule (section 6) | |
| | legacy grader layout | 1 | muon-newton-schulz keeps no embedded reference | |
| |
| Reference thresholds observed across the lane, for calibrating future audits: |
| `energy_top0.1%` runs 1-16% (>30% means the gate is reading a handful of elements — see section 11's |
| 86% underflow case), and `absmax/rms` over 128-blocks runs 2.4-6.8 against ~3.2 for a gaussian. |
|
|
| The 6 not covered by the generic twin all had their tolerances measured by purpose-built independent |
| implementations during the precision pass, which is the stronger check; the span twin would only have |
| added seed-stability evidence. |
|
|