5.1. Framework migration and library upgrades¶
5.1.1. Where we are¶
You have a model that one stack trains or serves, and you are moving it to another stack: from Megatron to TorchTitan, from an in-house trainer to a library, or to the same framework after an upgrade of torch, CUDA or a fused-kernel library. Two questions follow. Does the new stack compute the same thing? And where it does not, is the difference a bug or a choice? This page answers both. It uses the pre-training workflow with the old stack as the reference, and the rounding-point method of engine parity to decide each difference.
5.1.2. The idea: the same model written as two programs¶
Two stacks that implement the same model are two different programs. They draw module boundaries in different places, fuse different operators, and round at different points. Here is a tiny example, one attention block:
| Step | Stack A | Stack B |
|---|---|---|
| Before attention | RMSNorm, then three projections q, k and v | One fused kernel that does the norm and a packed QKV projection |
| Residual add | Done in bf16 | Done in fp32, and cast once later |
| Loss | log_softmax over the full vocabulary | Vocab-parallel cross entropy, summed across ranks |
Three things follow from this table.
- The traces do not line up. Stack A has a norm boundary and three projection boundaries, while stack B has a single boundary. If you pair events by their position, you invent differences. Pair them by content instead: a row that holds the same bits on both sides is the same value, wherever it appears.
- Each difference is either a bug or a choice. Using the wrong RoPE variant is a bug; keeping the residual in fp32 is a choice. Both show up as different bits, so you decide each difference on its own and write down what it costs.
- An upgrade is the easy case. The same framework at two versions has the same boundaries and one change, and the first divergence usually names the kernel that the new version picked.
So the rule is: compare at the boundaries both stacks share, pair rows by their content, and turn every remaining difference into a named rounding point with a decision. This is the controlled experiment of part 1 applied to two implementations, together with the engine-parity principle that equal results need equal arithmetic, not just equal formulas.
- 1. Diff the preflight reports to confirm that the two runs differ only in the stack.
- 2. Make both stacks see the same tokens and start from the same weights.
- 3. Capture both stacks at the boundaries they share. Stack B fused the norm and the projections, so those are not compared.
- 4. Pair rows by content at the shared boundaries, and find the first boundary where the same input rows give different output rows.
- 5. List every difference between the two forwards as a rounding point, such as a residual add in bf16 on one side and in fp32 on the other.
- 6. Decide each difference: either it is a bug to fix, or it is a choice to keep, recorded with its measured effect.
- 7. Keep the pair as a regression check, so that the next upgrade shows what moved.
5.1.3. The step¶
- Diff the preflight reports, to confirm that the two runs differ only
in the stack. Run determinism_preflight.py
reportinside each job and diff the two reports, declaring the stack as the intended difference (determinism and preflight). Two frameworks often name the same control differently (deterministic flags, collective algorithms, fused-kernel switches), so compare the settings by meaning and list what each side sets. The diff should list the stack and its own switches, and nothing else. - Align the data and the weights, so that both stacks see the same tokens in the same order and start from the same weights. Align the token stream as described in data alignment. When the checkpoint formats differ, convert once and check the conversion bit for bit with ckpt_compare.py (checkpoint conversion). The token ids of the first steps must be equal on both sides, and every row of every weight must have a matching row.
- Capture both stacks, with fingerprints at every boundary both stacks
have: the embedding output, each layer's attention and MLP outputs, the
residual stream, the logits, and, in training, the gradient of each
parameter. Use
opguard_bridge.run_pairin opguard_bridge.py with OpGuard's per-vector fingerprints switched on (OPGUARD_XOR_ROWS=1), so that rows can be paired by content; for code that runs under torch.compile or CUDA graphs, use the graph probe.opguard_bridge.coverage(run)should show those boundaries captured on both sides. - Find the first divergence: the first shared boundary where the same
input rows give a different output row. OpGuard pairs events automatically
when the two call orders agree
(localize with OpGuard), but
across stacks they often do not. In that case, pair rows by their
fingerprints with OpGuard's token matching, and walk the data flow from an
anchor where both sides agree, such as the embedding output, the way its
rl-alignmentskill does. Record every correspondence you rely on as an entry in an OpGuard alignment map, together with its evidence (OpGuard'sdocs/AlignmentMaps.md). You end with a pivot whose inputs are equal on both sides and whose output differs, and an alignment-map entry that says why the two boundaries correspond. - Write the rounding-point table, one named line for every difference between the two forwards, and between the two backwards in training. Follow steps 1 and 2 of the engine-parity skill: take an inventory of what actually runs on each side, then write down both rounding sequences, using the rounding points table as the checklist, and mark each line "read from source" or "measured". The difference at the pivot must be one of the lines.
- Decide each difference, and measure its effect. Change one side, one rounding point at a time, and capture again, as in engine parity; after each change, the first differing boundary should move later in the model. No difference may be left without a decision.
- Keep it as a regression check, so that the next upgrade shows what moved. Keep the smallest pair that shows each decision (one layer, one step) as an example in the regression suite, with the decisions written next to it. The suite runs the pair and reports it equal, or reports the line that moved.
| Decision | What it gives | Cost class | When to choose it |
|---|---|---|---|
| Change the new stack to round like the old one | Bitwise equality with the reference | small to moderate, per rounding point | The old stack is the validated reference, or the difference is a bug |
| Change the old stack to round like the new one | A new reference that both stacks agree on | moderate, because the old results must be established again | The new form is better (fewer roundings, or faster) and you will retire the old stack |
| Accept the difference and document it | A known, measured difference | free | The two forms are both exact versions of the same math, and neither side can change (a closed kernel, a fusion done by the compiler) |
These cost classes are judgements based on the work each decision adds, not measurements from a real migration.
For an upgrade within one framework, steps 1, 3 and 4 are usually enough. Run the regression suite first, because it tells you which stock kernels changed; then capture the old and the new version and read the pivot.
5.1.4. What can go wrong¶
Pairing events by position across stacks
The two stacks have different boundaries, so the n-th event on one side is not the n-th event on the other. Pairs forced this way report differences that do not exist. Pair rows by content, and leave an event without a partner as a gap in the record.
Weights or data that are not actually the same
A conversion that changed a few rows, or a different shuffle, makes every boundary differ from the first one on. The pivot then names the embedding or the first projection instead of the real cause. Check the conversion and the token ids before you capture.
Treating a choice as a bug
A fused kernel and an unfused chain that compute the same math round differently by design. Forcing them to agree can cost more than the difference is worth. Name both forms, measure the gap, and decide.
Using loss curves as the only evidence
Two loss curves can agree while one boundary is wrong, and they can drift apart after many steps because of a single harmless rounding point. A curve does not tell you where the difference is. Compare at boundaries.
5.1.5. Summary¶
- Two stacks are two programs whose traces do not line up, so pair rows by content and record each correspondence with its evidence.
- Remove every other difference first: diff the preflight reports, use the same data, and check the weights bit for bit.
- Find the first boundary where equal inputs give a different output, and name that difference as a rounding point.
- Decide each difference: either change one side, or accept and document it with its measured effect. Keep the pair as a regression check.
5.1.6. Go deeper¶
- The pre-training workflow and localize with OpGuard cover capture, pairing and the first divergence.
- Engine parity and the engine-parity skill describe the rounding-point method that this page reuses.
- Checkpoint conversion covers the weights, and data alignment covers the tokens.
- OpGuard's alignment maps (
docs/AlignmentMaps.md) and itsrl-alignmentskill (harness/skills/rl-alignment) handle pairing across implementations. - Not yet shown: a full migration between two training frameworks. The report on the cross-engine case shows the rounding-point method between a trainer and an engine.