aien-train
AIEN training programme: language-model history, GB10 constraints and the route to training on the project engine. Plans and runnable checks are distinguished.
Brought by drakestapleton
aien-train / docs / omegallm / CONTRACT-BF16.md
| 1 | # Omega LLM BF16 tensor-core contract |
| 2 | |
| 3 | This contract adds a BF16 tensor-core GEMM path to the GB10 build (`TARGET=gb10`). It extends |
| 4 | `CONTRACT-V2.md`, which remains normative for everything this file does not change. The CPU |
| 5 | product and the Python referee do not change. The BF16 path is opt-in, and every existing command |
| 6 | keeps its exact behaviour. |
| 7 | |
| 8 | Compatibility rule: every kernel slot except the new BF16 slots 27..36 keeps its exact code and |
| 9 | register bytes. The AArch64 build is byte-identical to the base 273ca2a build. Host evidence for both |
| 10 | statements is in `evidence/OMEGALLM/gpu-bf/` (`f32-identity.txt`, `build.txt`). |
| 11 | |
| 12 | ## 1. Numerics |
| 13 | |
| 14 | | Quantity | Format | |
| 15 | |---|---| |
| 16 | | Master weights, Adam moments, checkpoint | f32 / f64 as v2 (unchanged) | |
| 17 | | GEMM operands on the BF16 path | f32 in memory, rounded to bf16 by round to nearest even (F2FP.BF16.F32.PACK_AB) while staged into shared memory | |
| 18 | | GEMM accumulation | f32 (HMMA.16816.F32.BF16) | |
| 19 | | GEMM epilogue (alpha, beta, fused epilogues 1 and 2) | f32, identical to the f32 kernels' gkepi64 | |
| 20 | | GEMMs with n = E (32x32 tiles) | f32 kernels, unchanged | |
| 21 | | geval | f32 kernels only, so the held-out loss metric does not change | |
| 22 | |
| 23 | The bf16 unit roundoff is u = 2^-8, compared with 2^-24 for f32. All weight updates stay in f32, |
| 24 | and only GEMM inputs lose precision. |
| 25 | |
| 26 | ## 2. Kernels |
| 27 | |
| 28 | There are ten kernels, slots 27..36 (`gk.om` gkgemmb). Each 128-thread block computes one 64x64 |
| 29 | tile of C. K advances in tiles of 32, with two 8192-byte shared-memory buffers. Each warp issues |
| 30 | eight HMMA.16816 per k16 step. |
| 31 | |
| 32 | | Slot | Operand modes / epilogue | |
| 33 | |---|---| |
| 34 | | 27 + ma + 2 mb + 4 beta | the eight plain kernels (ma, mb in {0, 1}: 1 = K-contiguous) | |
| 35 | | 35 | fused epilogue 1 (ma 1, mb 0) | |
| 36 | | 36 | fused epilogue 2 (ma 1, mb 1) | |
| 37 | |
| 38 | Preconditions are checked on the host. Operand pointers and batch strides must be 16-byte |
| 39 | aligned. Loads are 16-byte: mode 1 needs a stride that is a multiple of 16, and mode 0 needs |
| 40 | outer stride 4 with a depth stride that is a multiple of 16. k must be a multiple of 32. There is |
| 41 | no fallback: a GEMM the kernels cannot take fails with nverr -22. |
| 42 | |
| 43 | `gkbf 1` selects the path. Callers control this flag for BF16 training, diagnostic |
| 44 | gates, and the selected inference projections described below. With `gkbf 0`, |
| 45 | dispatch remains f32. Existing commands retain their previous selection. |
| 46 | |
| 47 | Encoding provenance: the instruction words were verified with `nvdisasm -b SM121` against ptxas |
| 48 | 13.0 sm_121 output for `mma.sync.aligned.m16n8k16.row.col.f32.bf16.bf16.f32`, |
| 49 | `cvt.rn.bf16x2.f32` and `ld.global.v4.f32`. |
| 50 | |
| 51 | | Instruction | Words | |
| 52 | |---|---| |
| 53 | | HMMA.16816.F32.BF16 | `0x000000060c08723c 0x010fe20000041808` | |
| 54 | | F2FP.BF16.F32.PACK_AB | `0x000000000309723e 0x000fe200000010ff` | |
| 55 | |
| 56 | ## 3. Commands |
| 57 | |
| 58 | | Command | Arguments | Meaning | Failure exit | |
| 59 | |---|---|---|---| |
| 60 | | `gtrainb` | CK TRAIN UNTIL EVERY | `gtrainf` with gkbf 1 | as gtrainf | |
| 61 | | `gprofb` | CK TRAIN N | `gprof` with gkbf 1 | as gprof | |
| 62 | | `gbcmp` | CK DATA S | sampled check of every BF16 GEMM of one gradient pass | 48 | |
| 63 | | `gbcmp2` | CK DATA S | `gcmp2` step parity with the BF16 bound | 49 | |
| 64 | | `gbfrag` | CK | exact check of all ten kernels on fixed data | 50 | |
| 65 | |
| 66 | ## 4. Gates |
| 67 | |
| 68 | ### 4.1 gbfrag (exact) |
| 69 | |
| 70 | gbfrag runs thirteen cases: eight plain kernels, epilogues 1 and 2, and cz 1, 2 and 3 (cz 1 and 2 |
| 71 | need k = m, so those cases use k = 128). Each case runs twice, once with the operand roles swapped. |
| 72 | The data uses nz 6, h2 3 and padded strides, and every operand word the kernel must not read is NaN. |
| 73 | These cases reach at most four K tiles (k = 128). |
| 74 | |
| 75 | - One operand holds integers in [-5, 5]. |
| 76 | - The other holds (n - 2) + j·2^-9, with n < 5 and j < 64. |
| 77 | |
| 78 | Every product is therefore a multiple of 2^-9 (2^-10 with alpha 1/2), and every partial sum stays |
| 79 | below 2^11, so the f32 accumulation is exact. The reference applies bf16 RNE to the operands, and |
| 80 | the chip result must equal it element by element. The fractional values need more than eight |
| 81 | significand bits, so a kernel that skips the rounding or rounds toward zero differs from the |
| 82 | reference. |
| 83 | |
| 84 | Every C row has eight padding columns after column n - 1 (row stride n + 8). Before each case |
| 85 | gbfrag writes NaN into them, and into the same columns of Y (epilogue 1) or M (epilogue 2). After |
| 86 | the launch, every padding word that is no longer NaN counts as a mismatch, so a store outside |
| 87 | columns 0..n-1 of its row, such as a wrong column offset, fails the case. The BF16 dispatch takes |
| 88 | only m and n that are multiples of 64 (`gk.om` gktile), so these cases have no partial tiles. This |
| 89 | check was added after bf-g1 (2026-11-04, `evidence/OMEGALLM/gpu-bf/fix-pad/`); its chip run is |
| 90 | NOT_RUN until the integrator's pre-registered window. Mutant n (section 5) is its negative control: |
| 91 | after each short case's launch the host writes 0.0 into C's first padding word (block 0, row 0, |
| 92 | column n), so gbfrag must report exactly one mismatch per short case (26 `GF` lines with bad 1, |
| 93 | 8 `GFL` lines with bad 0, `GFS 26`) and fail with exit 50. |
| 94 | |
| 95 | Eight long-K cases (`GFL pass ma mb bad`) then cover the K loop: m = n = 64, k = 16384 (512 K |
| 96 | tiles), nz 1, beta 0, alpha 1, all four operand-mode pairs, both role passes. Operands are zero |
| 97 | except at kk ≡ 0 (mod 29), 565 terms, at least one in every 32-wide K tile, so a dropped, repeated |
| 98 | or misplaced tile changes C. Sums stay below 565·5·17/8 < 2^13 (`565*5*17/8 -> 48025/8 = 6003.125`, |
| 99 | Wolfram) in multiples of 2^-9, so the f32 result |
| 100 | is exact. The logits buffer is NaN-filled first (A at 0, B at 6 MiB, C at 12 MiB, at least 4 Mi |
| 101 | floats needed), and C's eight padding columns per row must still be NaN afterwards. |
| 102 | |
| 103 | ### 4.2 gbcmp (sampled, every BF16 GEMM of a step) |
| 104 | |
| 105 | gbcmp runs one gradient pass (gggrad, no Adam) with every BF16 launch checked. Each launch checks |
| 106 | 66 samples: 64 random positions (31-bit LCG) plus the two corners. Each sample is compared with an |
| 107 | f64 sum over the bf16-rounded operands, over the K range that the kernel reads. The bound is |
| 108 | |
| 109 | |chip - ref| <= (kn·2^-23 + 2^-22)·(|alpha|·Σ|a·b| + |old C|) |
| 110 | |
| 111 | The first term is the f32 accumulation error over kn terms, with kn·u_f32 and u_f32 = 2^-24 |
| 112 | doubled for slack; the second covers the epilogue's rounding. Wolfram: |
| 113 | `N[2^-23] -> 1.1920928955078125*^-7`, `N[2^-22] -> 2.384185791015625*^-7`. |
| 114 | |
| 115 | Fused epilogues are checked exactly: |
| 116 | |
| 117 | - Epilogue 1: Y = C when C > 0, otherwise +0. |
| 118 | - Epilogue 2: C is +0 where M is not > 0. |
| 119 | |
| 120 | The gate also fails when no BF16 GEMM ran, or when coverage misses any combination the step uses: |
| 121 | epilogues 1 and 2, cz 3, and beta false and true. Which kernel ids ran is printed, not gated; |
| 122 | gbfrag covers every id. |
| 123 | |
| 124 | The bound grows with kn. At B 64 (kn = 16384) it is about one K tile's share of Σ|a·b| |
| 125 | (`16384*2^-23+2^-22 -> 0.00195336` against `32/16384 -> 0.001953125`), so gbcmp at the training |
| 126 | shape cannot see a single wrong K tile; at B 8 (kn = 2048) the margin is 64x. Long K loops are |
| 127 | therefore checked exactly by gbfrag's long-K cases, not by gbcmp. |
| 128 | |
| 129 | Because the bf16 rounding is in the reference, gbcmp checks the kernel against the stated |
| 130 | numerics. It does not measure how far bf16 is from f32. That is the job of 4.3 and 4.4. |
| 131 | |
| 132 | ### 4.3 gbcmp2 (step parity, heuristic bound) |
| 133 | |
| 134 | gbcmp2 is gcmp2 (the chip gradient compared with the CPU f64 gradient on the same batch) run with |
| 135 | gkbf 1. Its tolerance is the first-order normwise heuristic tolb = n_c·(2u + u²), with u = 2^-8 and |
| 136 | n_c = 10L + 3. n_c counts the BF16 GEMMs on a step's longest dependency chain: |
| 137 | |
| 138 | - per layer, forward: QKV, S, WO, W1, W2; |
| 139 | - per layer, backward: dF, dA of W1, dN, DP, dA of QKV; |
| 140 | - plus the logits GEMM, dlogits to dH, and the last weight gradient. |
| 141 | |
| 142 | The fail bits are 1 (non-finite), 2 (rho >= tolb), 4 (rel1 >= tolb) and 8 (|dloss| >= tolb·loss). |
| 143 | At L 8: `u=2^-8; nc=10*8+3; nc*(2u+u^2) -> 42579/65536 = 0.6497039794921875` (Wolfram). The bound |
| 144 | is loose: it catches gross failures, such as non-finite values or a wrong layout. No mutant shows |
| 145 | what it catches, and it is not a precision claim. |
| 146 | |
| 147 | ### 4.4 Training (decisive) |
| 148 | |
| 149 | BF16 is adopted for training only if a pre-registered `gtrainb` run from the rung-7 start |
| 150 | checkpoint reaches a chip geval held-out loss at step 2000 within +0.5% of the f32 rung-7 value. |
| 151 | The rung-7 value is E 2000 = 1756935 µnats, so the bound is 1765719. Wolfram: |
| 152 | `Floor[1756935*1.005] -> 1765719`. Step time is compared with rung 7's 0.2390 s/step. The |
| 153 | pre-registration fixes the exact procedure. |
| 154 | |
| 155 | ### 4.5 f32 regressions |
| 156 | |
| 157 | On the BF16 binary, the f32 commands must reproduce the rung-7 gate receipts exactly: |
| 158 | |
| 159 | - geval of r6 ck30000; |
| 160 | - the bit-identical t200 checkpoint; |
| 161 | - the gcmp2 small GC2 fields; |
| 162 | - gcmp2 and gacmp at the v2 maximum. |
| 163 | |
| 164 | ## 5. Mutants |
| 165 | |
| 166 | Each mutant is a build flag (`--mutant X`). Each must fail at least one gate on the chip. |
| 167 | |
| 168 | | Mutant | Constant | Defect | Expected to fail | |
| 169 | |---|---|---|---| |
| 170 | | u | gb.om OMUTU 0 | the reference skips the bf16 rounding | gbfrag (exit 50); gbcmp reported only | |
| 171 | | l | gk.om OMUTL 0 | A rows of lanes g and g ^ 1 swapped in the LDS layout | gbfrag, gbcmp | |
| 172 | | o | gk.om OMUTO 0 | one K tile fewer than the tile count | gbfrag (k ≥ 64 cases, long-K cases), gbcmp | |
| 173 | | v | gk.om OMUTV 0 | F2FP rounds toward zero instead of to nearest even | gbfrag, gbcmp | |
| 174 | | n | gb.om OMUTN 0 | host overwrites C's padding word (0, n) with 0.0 after each short case (no kernel change) | gbfrag (padding check only, `GFS 26`) | |
| 175 | |
| 176 | For mutant u, gbcmp is reported only, because the RNE-vs-exact difference per sample can fall |
| 177 | within the f32 accumulation bound when Σ|a·b| is large. Mutant o cannot change a GEMM with a single |
| 178 | K tile (k = 32): the K loop is a do-while and runs once regardless. |
| 179 | |
| 180 | ## 6. Host evidence |
| 181 | |
| 182 | - `evidence/OMEGALLM/gpu-bf/disasm-27-36.txt`: in each of slots 27..36, GPR max R142, need 145, |
| 183 | declared 152, OK; SMEM target 17; 16 HMMA.16816.F32.BF16 per slot (160 in total); no illegal |
| 184 | instruction. |
| 185 | - `evidence/OMEGALLM/gpu-bf/f32-identity.txt`: compared with base 273ca2a, gpu-code.bin differs |
| 186 | only in slots 27..36, and gpu-gpr.bin differs only at register offsets 27..36 and SMEM offsets |
| 187 | 91..100 (the same slots). |
| 188 | - `evidence/OMEGALLM/gpu-bf/build.txt`: build hashes at e329675 for the product, the AArch64 CPU |
| 189 | build (identical to the base) and the four mutants. |
| 190 | - `evidence/OMEGALLM/gpu-bf/build-2.txt`: build hashes after the gbfrag amendment (case 10 shape, |
| 191 | long-K cases); gpu-dump code_sha256 is unchanged, so no kernel byte differs from e329675. |
| 192 | - `evidence/OMEGALLM/gpu-bf/fix-pad/`: the C padding check of 4.1 (2026-11-04). gpu-dump |
| 193 | code_sha256 and gpu-gpr.bin are unchanged, so no kernel byte differs; new build hashes |
| 194 | (`build.txt` at f5a6941, `build-n.txt` with mutant n); chip commands and expected results for |
| 195 | its pending run. |
| 196 | |
| 197 | Chip gates 4.1-4.5 ran in two pre-registered windows, each EXERCISED on one GB10 (non-quiet), |
| 198 | before the padding check existed: |
| 199 | |
| 200 | - bf-g1 (`evidence/OMEGALLM/gpu-bf/g1/REPORT.txt`): B1-B12 PASS. Mutants l, o and v fail both |
| 201 | gbfrag and gbcmp. Mutant u fails gbfrag; gbcmp only reports it (u failed both; gbcmp is not |
| 202 | required to catch u). |
| 203 | - bf-t1 (`evidence/OMEGALLM/gpu-bf/t1/REPORT.txt`): T1-T3 PASS. E 2000 is 1749889, within the 4.4 |
| 204 | bound of 1765719, and the 250→2000 leg took 367.6 s against 434.2 s for f32. |
| 205 | |
| 206 | ## 7. Adoption |
| 207 | |
| 208 | Drake approved adopting BF16 for training on 2026-11-03, after bf-t1 met the 4.4 rule. From then |
| 209 | on, long training runs use `gtrainb`. The rest of the contract does not change: |
| 210 | |
| 211 | - `gtrainf` and every f32 command keep their exact behaviour. |
| 212 | - `geval` stays on the f32 kernels, so held-out numbers stay comparable across f32 and BF16 runs. |
| 213 | - The CPU `eval` of a final checkpoint must still print the chip `geval` line, since both evaluate |
| 214 | in f32. |
| 215 | |
| 216 | What adoption gives up: a `gtrainb` step no longer matches the f64 CPU step bitwise. For BF16 |
| 217 | runs, gbfrag and gbcmp check the kernels against the bf16 numerics instead, and gbcmp2 is the |
| 218 | heuristic step check of 4.3. A binary used for `gtrainb` runs must pass 4.1-4.5 on the chip first. |
| 219 | |
| 220 | Adoption is not a quality claim. 4.4 compares step 2000 only. The first long `gtrainb` run carries |
| 221 | its own pre-registration and compares its final value with the f32 run on the same schedule. |
| 222 | |
| 223 | |
| 224 | ## BF16 inference with sensitive operations retained in f32 |
| 225 | |
| 226 | The opt-in `gserveb` and `gsampb` commands use BF16 tensor-core operands for |
| 227 | the attention-output projection and the two feed-forward projections. |
| 228 | Accumulation and epilogues remain f32. QKV projection, attention scores and |
| 229 | value reduction, normalization and final logits stay f32. This is a mixed |
| 230 | BF16/f32 inference path, not an all-BF16 model or checkpoint conversion. |
| 231 | |
| 232 | They take the same arguments as `gserve` and `gsample`. Existing commands, |
| 233 | training, evaluation and checkpoint formats retain their behavior. |
| 234 | `gdbench` mode 7 executes this path; mode 8 compares it with the existing f32 |
| 235 | forward on the same contexts. Mode 8 retains the existing 0.25-nat maximum |
| 236 | logit difference and high-margin token-divergence rule. Modes 3 and 4 retain |
| 237 | the original all-eligible-GEMM BF16 experiment, including its known failed |
| 238 | accuracy result. No failed threshold was relaxed to admit the new path. |
| 239 | |
| 240 | The external `tests/omegallm/bf16-inference.py` gate runs within an authorized |
| 241 | Spark window. It checks both numerical and execution evidence: four prompts |
| 242 | per supplied checkpoint, 250 steps each including context sliding; exact |
| 243 | f32 and legacy-mode regressions; three BF16 projection dispatches per layer; |
| 244 | CLI repeatability; and a compiled no-BF16 mutant that must lose those |
| 245 | dispatches. The supplied baseline, candidate, source, compiler, checkpoints |
| 246 | and tokenizer are hashed before and after the run. Its output is an |
| 247 | acceptance record for those inputs, not a universal numerical error bound |
| 248 | or a timing claim. |
| 249 | |
| 250 | The existing serving referee accepts `VERB=gserveb` and compares responses |
| 251 | with `gsampb`, so protocol and sampler parity exercise the same precision |
| 252 | path. Source alone is not an adoption claim: preserve the actual gate |
| 253 | receipts and independent review before installing this command. |
| 254 | |
| 255 | The GB10 run is retained in [bf16-inference-20261113](../../evidence/OMEGALLM/bf16-inference-20261113/README.md). The receipt records the numerical, execution and serving check results. It does not establish a speedup. |