[PyTorch] Pair delayed-scaling FP8 recompute metadata per module - #3394
[PyTorch] Pair delayed-scaling FP8 recompute metadata per module#3394nvegesna-netizen wants to merge 4 commits into
Conversation
The delayed-scaling stash and its two restore sites made independent decisions. Module mode changes between the original forward and checkpoint replay could therefore leak a stash or restore one that was never created. Track pending stashes per module and record whether each prepare_forward call swapped one in, so end_forward performs exactly the matching restore. Stash every delayed-scaling FP8 module encountered in checkpoint phase 1: in reentrant checkpointing the original forward runs under no_grad, so an eval module receiving an intermediate tensor has no module-local autograd signal even though backward will replay it. Tests cover training and eval modules, both checkpoint implementations, mode changes in both directions, repeated iterations, multi-module reentrant replay, and exact FIFO drainage. Signed-off-by: Nitin Vegesna <nvegesna@nvidia.com>
Greptile SummaryThe PR fixes delayed-scaling FP8 metadata pairing for checkpointed modules whose training mode differs between the original and recompute forwards.
Confidence Score: 5/5The PR appears safe to merge. No blocking failure remains. Important Files Changed
Sequence DiagramsequenceDiagram
participant User
participant Checkpoint as TE checkpoint
participant Module as FP8 module
participant Buffer as Recompute metadata FIFO
User->>Checkpoint: checkpointed forward
Checkpoint->>Module: phase 1 forward
Module->>Buffer: stash scale and amax metadata
User->>Checkpoint: backward
Checkpoint->>Module: phase 2 recompute
Module->>Buffer: consume matching metadata
Buffer-->>Module: original scale and amax
Module->>Module: recompute forward
Module->>Module: restore live metadata
Reviews (3): Last reviewed commit: "Test explicit grad inside no-grad checkp..." | Re-trigger Greptile |
|
After a longer discussion with Codex, we came to the following conclusion:
What do you think? |
Signed-off-by: Nitin Vegesna <nvegesna@nvidia.com>
Signed-off-by: Nitin Vegesna <nvegesna@nvidia.com>
Signed-off-by: Nitin Vegesna <nvegesna@nvidia.com>
|
Thank you for the careful review. I agree with the core conclusion. I could not identify a supported execution path where a delayed-scaling module legitimately appears in checkpoint phase 2 without having appeared and stashed in phase 1. Such a path would require divergent checkpoint execution, FP8 state replacement, or another invariant violation, and it should fail rather than silently recompute using live metadata. I revised the PR accordingly:
if is_fp8_activation_recompute_enabled():
FP8GlobalStateManager.copy_forward_fp8_meta_tensors_for_recompute(self.fp8_meta)Testing that minimal change independently confirmed your analysis: all ten original eval/mode-change cases pass without the additional module state. That discriminator also exposed a separate phase-1-without-phase-2 case. If I addressed that at the checkpoint boundary rather than in the module. For TE-containing callables, The revised validation covers 18 focused cases across:
For the explicit-gradient cases, output, input gradient, and weight gradient match direct execution, and the recompute FIFO remains empty. The broader recompute/checkpoint selection completes with 450 passes and 90 expected capability skips. One boundary is now documented in the PR description: if checkpoint is entered under outer The current head is |
Description
Fix delayed-scaling FP8 metadata stash/restore pairing when a checkpointed module is in eval mode or changes mode between the original forward and recompute forward.
The original forward stashed metadata only when
self.trainingwas true, while recompute restored metadata from every FP8 module in the recompute phase. An eval module could therefore try to restore a stash it never created. Module training mode is not a valid pairing signal: reentrant checkpointing deliberately runs the original forward underno_grad, and a module may also change mode before recompute.Every delayed-scaling FP8 module in checkpoint phase 1 now stashes its metadata, independent of module training mode. Phase 2 retains the existing strict FIFO restore behavior, so an execution mismatch remains visible rather than being silently skipped.
When
te.checkpoint()is entered with outer autograd disabled, no backward recompute is normally possible. TE-containing callables therefore execute directly under the supplied forward context, avoiding FP8 recompute snapshots that could never be consumed. Non-TE callables continue to use native PyTorch checkpointing.Type of change
Changes
Validation
context_fnor the callable.Grad-mode boundary
Checkpoint-entry grad mode is authoritative. If
te.checkpoint()is called under outertorch.no_grad()but itscontext_fnor callable explicitly re-enables gradients internally, execution remains numerically correct, but the direct-forward path bypasses activation checkpointing and may retain more activations.Callers that need checkpoint recomputation for such a gradient-enabled region should enable gradients around the checkpoint call itself:
Checklist