2.1. Whole-model invariance (L1–L3)¶
2.1.1. Where we are¶
This is the first step of the RL workflow. You have a model that both the trainer and the engine run, and their logprobs disagree even with identical weights. In this step you audit the model's operators and make one side give a sequence the same bits however that sequence is batched and served: from run to run (L1), in any batch (L2) and in any serving mode (L3). Making the two sides agree with each other comes later, in engine parity.
2.1.2. The idea¶
The trainer and the engine compute the same tokens in different shapes. The trainer processes the whole sequence in one pass (prefill), inside a batch of training sequences. The engine generates one token per step (decode) from a KV cache, inside a batch whose members join and leave at every step. The engine may also split the prompt into chunks, reuse a cached prefix, or verify speculative tokens.
- Take one sequence: a prompt (p) followed by four sampled tokens, t1 to t4.
- The trainer computes every token in a single prefill, inside a batch of B sequences. The engine decodes t1 in a batch with requests A and B.
- The trainer now has a logprob for every token. The engine decodes t2; A has left the batch and C has joined.
- The engine decodes t3, now alone in the batch.
- The engine decodes t4 together with two new requests, D and E.
- The tokens are the same, but the shapes and the batches differ. This step makes the logprobs bitwise equal all the same.
A floating-point sum depends on the order of its additions. Add 1e8, -1e8 and 1 in fp32: going left to right gives 1, but if 1 is first added to -1e8, it is rounded away and the result is 0. A kernel that chooses its reduction order from the number of rows will therefore give the same row different bits in a batch of 1 and in a batch of 64. Three things change the order between the two sides:
| Source | What happens | Level it breaks |
|---|---|---|
| Kernels that vary with the batch | When the number of rows changes, a GEMM library or a reduction picks a different kernel or a different split | L2, batch |
| Kernels that depend on the mode | Prefill, chunked prefill and decode go through different attention code | L3, mode |
| Rounding points | The two sides cast to bf16 at different places, for example before or after multiplying by the norm weight | L4, cross-engine |
So the rule is that every operator on the path from a token to its logprob must add things up in an order fixed by that row's own data, never by the batch or the mode. This is the batch-invariance principle of Part 3, applied to a whole model. The last row of the table shows why invariant kernels alone are not enough: an engine that reaches L2 and L3 but rounds differently at one place is as far from the trainer as an unmodified engine. A later step settles that row.
2.1.3. The step¶
- Audit the operators. Run tools/bi_audit.py: it replays every module on one row alone and on the same row inside a batch, and names the first module whose output differs, including fp32 reductions hidden behind a bf16 output. You end with a list of modules to replace; if it is empty, skip the next step.
- Replace the operators that vary, following the batch-invariant-kernel skill and its operator recipes, until the audit passes.
- Check every serving mode the engine uses. Invariant operators do not yet make an invariant model, so compare decode in a changing batch, chunked prefill, a prefix-cache hit, speculative verification, preemption, padding and variable-length batches against the trainer's forward. The logits and the activation gradients must be bitwise equal in every mode; whole_model.py runs this check for the reference model.
- Write down the rounding sequence, one line per rounding point in the order they happen: every cast, which operations run in fp32, the RoPE formula, and where the norm weight is multiplied in. The engine is aligned to this record later. The rounding points checklist says what to record, and model.py is a worked example.
| Option | What it gives | Cost class | When to choose it |
|---|---|---|---|
| Keep the stock operators | No bitwise guarantee; most tokens differ between batches and between modes | free | Only to measure the gap |
| A batch-invariant operator set on both sides | L1–L3 on each side, for the forward and for activation gradients | moderate (under 2x), up to large for big prefills | You own the model code on both sides |
| A batch-invariant operator set plus one shared rounding sequence | The precondition for L4 | as above, plus the work of aligning the engine's casts | The trainer and the engine are different code (engine parity) |
What has been shown: a model built from batch-invariant kernels gave
bitwise-equal logits and activation gradients in every serving mode tested,
for four architectures, on one GPU type in eager mode. With stock operators,
nearly every logprob differed
(report: invariance,
what RL would see,
cost). Not yet shown:
torch.compile, CUDA graphs, tensor parallelism (the next step) and a real
engine.
2.1.4. What can go wrong¶
A bf16 output can hide an fp32 reduction that varies with the batch
An fp32 reduction inside a module, such as the variance in RMSNorm, can change with the number of rows while the module's bf16 output stays the same. A check that only looks at outputs then passes at one shape and fails at another. Replace such a reduction anyway; the audit reports it as "variant inside" (report).
A RoPE table built under the compiler differs from the eager one
A table built inside a compiled region uses the compiler's math, so a few of its entries differ from the eager table, and later eager runs keep using it. Build the table in eager mode before compiling.
Parameter gradients can only reach L1
A parameter gradient is a sum over the whole batch, so it cannot be compared row by row. Test it for repeatability from run to run, and test the activation gradients for L2.
Two Hugging Face references are not the same model
A model that is cast to bf16 after loading has its RoPE frequencies rounded to bf16, while a model loaded directly in bf16 does not, and the two give visibly different logits. State which of them an engine is compared against.
Test the sequence lengths you actually run
An attention kernel can be invariant on short rows and still change with the batch on long ones. A pass at short lengths tells you nothing about long ones.
2.1.5. Summary¶
- The trainer and the engine run the same tokens in different batches and modes, and stock kernels change their reduction order with both.
- Audit the model, replace every operator that varies, and then check every serving mode against the trainer's forward.
- Batch invariance on each side is necessary but not sufficient: the two sides must also round at the same places.
- Write the rounding sequence down now, because the engine is aligned to it later.
2.1.6. Go deeper¶
- Lab report: the model variants, operator sets and execution modes, every result table, the audit tool, the cost and the limits.
- Code: model.py, whole_model.py, tools/bi_audit.py.
- Skills: batch-invariant-kernel, engine-parity.
- Next step: parallelism.