Skip to content

[PyTorch] Schedule delayed-scaling updates after backward - #3456

Open
pggPL wants to merge 20 commits into
NVIDIA:mainfrom
pggPL:backward_quantization_update_scheduler
Open

pggPL wants to merge 20 commits into
NVIDIA:mainfrom
pggPL:backward_quantization_update_scheduler

Conversation

@pggPL

@pggPL pggPL commented Sep 1, 2026 •

Copy link
Copy Markdown
Collaborator

Description

Delayed-scaling (amax/scale) updates are currently owned by the first FP8 module seen in forward and executed from that module's backward. This depends on backward traversal order and breaks with unused branches, reentrant checkpointing, or schedules that delay weight-gradient computation.

This PR moves the update to the end of the autograd task instead. Each participating module requests an update and TE queues a single callback on the enclosing GraphTask, so the update runs once after the whole backward has finished. Reentrant TE checkpointing registers the callback on the outer task before entering its nested backward.

Plain .backward() needs no changes. A new quantization_backward_scope() lets applications define a larger logical backward, so the update runs once when the outermost scope exits:

with te.quantization_backward_scope():
    for loss in microbatch_losses:  # e.g. a 1F1B pipeline schedule
        loss.backward()

The update always runs on scope exit, even on ranks that ran no quantized backward inside it, so every rank joins the amax reduction. Like autocast, the scope must be entered and exited on all ranks.

New API

transformer_engine.pytorch.quantization_backward_scope() (context manager, documented in the PyTorch API reference and on the delayed scaling feature page):

  • Delays the delayed-scaling update (amax reduction across the reduction group and scale recomputation for the gradient quantizers) until the outermost scope exits. Nested scopes are no-ops. A scope entered inside a running backward defers the update to the end of that backward.
  • Always runs the update on exit, even if no quantized module ran backward inside the scope. Like autocast, it must be entered and exited on all ranks.
  • Has no effect for recipes without delayed-scaling state.

Internal hooks used by the modules: FP8GlobalStateManager.backward_quantization_update_needed() (decided in forward, stored as a bool on the autograd ctx) and FP8GlobalStateManager.request_backward_quantization_update() (called from backward, queues one callback per autograd task).

Type of change

  • Documentation change (change only to the documentation, either a fix or a new content)
  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • Infra/Build change
  • Code refactoring

Changes

  • Queue one delayed-scaling update at the end of each autograd task instead of in the first module's backward.
  • Decide in forward whether a module must request the update (backward_quantization_update_needed()), carry only a bool through the autograd ctx, and request from backward with request_backward_quantization_update().
  • Handle reentrant TE checkpoints by registering on the outer GraphTask.
  • Add quantization_backward_scope() for custom logical-backward boundaries; it always updates on exit.
  • Keep the existing FP8GlobalStateManager.reduce_and_update_fp8_tensors() implementation and CUDA graph update path.
  • Add tests for plain, reentrant, non-reentrant, nested, branched, unused-branch, multi-backward, and delayed-wgrad cases, plus a distributed test where some ranks skip backward.

Checklist:

  • I have read and followed the contributing guidelines
  • The functionality is complete
  • I have commented my code, particularly in hard-to-understand areas
  • I have made corresponding changes to the documentation
  • My changes generate no new warnings
  • I have added tests that prove my fix is effective or that my feature works
  • New and existing unit tests pass locally with my changes

Validation

  • Black 24.4.2, Python syntax checks, and git diff --check pass for the scheduler-only revision.
  • AST comparison confirms the existing update implementation and RecipeState classes match the merged main.
  • GPU tests have not been rerun locally for this revision; CI results are pending.

pggPL and others added 14 commits August 20, 2026 17:23
Keep FP8GlobalStateManager responsible for iterating registered delayed-scaling buckets, while RecipeState subclasses define the corresponding update algorithm. Extract recipe-to-state dispatch so construction and global updates share one mapping.

Signed-off-by: Pawel Gadzinski <pgadzinski@nvidia.com>
Queue one quantization state update at the autograd boundary instead of assigning it to the first FP8 module seen in forward. Add an optional logical-backward scope for multi-backward schedules and delayed weight-gradient computation.

Co-authored-by: AlbertYang514 <201034045+AlbertYang514@users.noreply.github.com>
Signed-off-by: Pawel Gadzinski <pgadzinski@nvidia.com>
Document that graphs produced under one autocast need an explicit logical-backward scope when their backward calls should share one delayed-scaling update.

Signed-off-by: Pawel Gadzinski <pgadzinski@nvidia.com>
Signed-off-by: Pawel Gadzinski <pgadzinski@nvidia.com>
…pe always update

Modules call FP8GlobalStateManager.request_backward_quantization_update(recipe)
from backward whenever they ran in FP8; the recipe and graph-capture checks
live in that helper instead of being repeated per module.

quantization_backward_scope now marks the update pending on entry, so ranks
that ran no quantized backward inside the scope still join the amax
reduction. Add a distributed test covering a module skipped on some ranks and
a rank with no backward inside the scope.

Signed-off-by: Pawel Gadzinski <pgadzinski@nvidia.com>
Signed-off-by: Pawel Gadzinski <pgadzinski@nvidia.com>
Decide in forward via FP8GlobalStateManager.backward_quantization_update_needed()
and carry only a bool through ctx; request_backward_quantization_update()
takes no arguments.

Signed-off-by: Pawel Gadzinski <pgadzinski@nvidia.com>
Signed-off-by: Pawel Gadzinski <pgadzinski@nvidia.com>
…lobal state in distributed test

Signed-off-by: Pawel Gadzinski <pgadzinski@nvidia.com>
…test_numerics.py

Signed-off-by: Pawel Gadzinski <pgadzinski@nvidia.com>
Signed-off-by: Pawel Gadzinski <pgadzinski@nvidia.com>
…ture page

Signed-off-by: Pawel Gadzinski <pgadzinski@nvidia.com>
Gradient quantization happens in backward(); backward_dw() only runs the
stored GEMM, so it does not motivate the scope.

Signed-off-by: Pawel Gadzinski <pgadzinski@nvidia.com>
@pggPL
pggPL marked this pull request as ready for review September 2, 2026 12:37
@greptile-apps

greptile-apps Bot commented Sep 2, 2026 •

Copy link
Copy Markdown
Contributor

RetriggerConfidence Score: 5/5

[High risk] Refactors when FP8 quantization updates run during backward.

The PR appears safe to merge based on the reviewed changes.

Summary

The PR schedules delayed-scaling updates after backward and adds a scope for training steps containing multiple backward calls.

  • Modules request an update; an autograd callback performs it after the task, or the outermost scope performs it on exit.
  • Tests cover checkpointing, CUDA graphs, delayed weight gradients, and ranks that skip backward.
  • The change since the previous review corrects the CUDA graph test’s method lookup.

Diagram

%%{init: {'theme': 'neutral'}}%%
flowchart LR
  A[Quantized module backward] --> B[Request update]
  B --> C{Backward scope active?}
  C -- No --> D[End-of-task callback]
  C -- Yes --> E[Outermost scope exit]
  D --> F[Reduce amax and update scales]
  E --> F
Loading

Reviews (7) · Last reviewed commit: "Fix CUDA graph update test method name"

@pggPL
pggPL force-pushed the backward_quantization_update_scheduler branch from 578a133 to 1ea58d0 Compare September 17, 2026 10:00
Signed-off-by: Pawel Gadzinski <pgadzinski@nvidia.com>
@pggPL

pggPL commented Sep 17, 2026

Copy link
Copy Markdown
Collaborator Author

Hi @ksivaman ,

This PR changes the logic of delayed amax update - instead of relying on first in forward/last in backward module, we use PyTorch callback at the end of backward. This has multiple benefits:

  • It removes the assumption that the first module in forward is the last to finish backward, which is fragile for branched computation graphs.
  • It avoids losing the update when the module that would own it belongs to a branch that does not participate in backward.
  • It simplifies coordination with reentrant TE checkpointing by deferring the update until the enclosing backward finishes.
  • The new quantization_backward_scope() allows multiple backward calls, such as pipeline microbatches, to share one amax reduction and scale update.
  • The scope also ensures that ranks which skip quantized backward still participate in the reduction, provided all ranks enter and exit the scope consistently.

The existing amax reduction and scale-update algorithm remains unchanged; this PR changes its scheduling.

@ksivaman

Copy link
Copy Markdown
Member

@pggPL Is this tested with cuda graphs and/or pipeline parallelism configs?

We'll have to change the make_graphed_callables functionality to accommodate the new design.

Signed-off-by: Pawel Gadzinski <pgadzinski@nvidia.com>
Signed-off-by: Pawel Gadzinski <pgadzinski@nvidia.com>
Comment thread tests/pytorch/test_cuda_graphs.py Resolved
@pggPL

pggPL commented Sep 29, 2026

Copy link
Copy Markdown
Collaborator Author

@ksivaman I added change to make_graphed_callabes(). To accommodate PP to new design change in MCore is needed here NVIDIA/Megatron-LM#7051

Maybe we should move this change to 3.0?

Signed-off-by: Pawel Gadzinski <pgadzinski@nvidia.com>
@ksivaman

Copy link
Copy Markdown
Member

Maybe we should move this change to 3.0?

I think that makes sense so we can coordinate the PRs in both repos and make a clean cut at 3.0 in MLM.

@pggPL pggPL added the 3.0 label Sep 29, 2026

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants