← Back to Pull requests

Release each BF16 gradient once immediate_grad_update has accumulated it #8654

Closed
unknown closed this pull request without merging on Oct 09, 2026 05:59 UTC
View on GitHub
unknown unknown commented on Sep 24, 2026 09:09 UTC
Author

Summary

With bf16.immediate_grad_update, BF16_Optimizer accumulates each completed BF16 parameter gradient in the configured accumulation buffer (FP32 in the measured configuration). Previously it retained the BF16 param.grad beside that buffer until the optimizer step. The hook now releases the consumed gradient with param.grad = None. With the option disabled, accumulation still happens in the backward epilogue; its default remains unchanged.

For folded AutoEP+AutoTP (TP>1), the BF16 hook also applies folding correction before accumulation. A correction marker left on the parameter after param.grad becomes None cannot be cleared by the engine's boundary reducer, which skips missing gradients; a later backward could therefore miss correction. The hook now clears that marker when it consumes and releases the corresponding BF16 gradient.

This branch includes current upstream master, which removed graph harvesting in #8579. The release hook no longer accesses that removed attribute. It also fixes the BF16 gradient-reconstruction issue exposed by releasing param.grad: each rank contributes only its optimizer-owned fragment, even though BF16 keeps a full accumulation buffer on every rank. BF16's mapping indexes its full parameter list rather than ZeRO's local fragment list.

Why

In a Qwen3-30B-A3B AutoEP training workload (S8192, EP16, 48 layers, 16 NVIDIA H100 GPUs, ZeRO Stage 1, FP32 gradient accumulation), an allocator trace locates peak allocation at the end of backward, when BF16 gradients occupy 6.7 GB per rank beside the accumulation buffers.

Results (16 NVIDIA H100 80GB HBM3)

Same allocation, four mirrored repeats per arm:

| | epilogue accumulation | immediate accumulation + BF16 grad release |
| --- | --- | --- |
| peak allocated | 54.439 GB | 50.434 GB |
| peak reserved | 57.481 GB | 56.762 GB |
| median step | 2654.2 ms | 2615.6 ms (-1.45%; 95% CI crosses zero) |

This A/B comparison changes the accumulation mode together with BF16 gradient release. It does not isolate the speedup or memory effect of the single param.grad = None assignment. Step-0 losses match; the maximum cross-arm loss difference across 24 steps is 0.0091, versus 0.0072-0.0085 within an arm. Peak allocation falls by about 4.0 GB, less than the 6.7 GB of freed BF16 gradients because the peak moves to another point in backward.

Numerical contract

After backward, param.grad may be None with immediate accumulation. In that case deepspeed.utils.safe_get_full_grad(param) reconstructs the accumulation buffer from owned fragments before engine.step() (FP32 when FP32 accumulation is configured). All participating DP ranks must call this collective getter in the same order. Reduction uses the accumulation buffers via get_grads_for_reduction; norm, clipping and the optimizer step use them via get_grads_for_norm and the FP32 partitions.

Testing Done

  • Current-master targeted run on an 8-GPU NVIDIA H100 80GB HBM3 node (two GPU ranks used): 18 passed, 0 skipped, with torch 2.10/CUDA 13.0. This includes two owned-fragment getter unit cases, four split/unsplit BF16 Stage-1 and ZeRO Stage-2 training cases (GAS=1/2, two optimizer steps, independent gradient/norm/clipping/SGD references), the release-vs-epilogue comparison, five folding/checkpoint cases, and six multiple-model/shared-loss cases.

  • The focused TP2 lifecycle test uses a controlled doubled expert gradient and an independent non-folded reference. GAS=1 and GAS=2 each run two optimizer steps; GAS=2 also exercises reentrant activation checkpointing. The obsolete graph-harvesting case was removed together with upstream's feature.

  • The earlier marker-cleanup negative control is retained as evidence: removing only the cleanup makes GAS=1 fail at the second step's first micro-batch and GAS=2 at the first step's second micro-batch. That evidence predates the upstream synchronization; it is not a new E2E run.

  • With only the owned-fragment slicing removed, the full-buffer getter unit case and both BF16 Stage-1 training cases fail as expected; the fragment-local unit case and both ZeRO Stage-2 cases still pass. This isolates the duplicate-contribution defect without changing the release hook or optimizer math.

  • All PR-changed files pass changed-files pre-commit checks.

  • Published-head checks: DCO and test collection passed. The full DeepSpeedAI GPU CI job was skipped; the targeted H100 execution above is the device-validation evidence.
  • Dependencies and scope

    The non-immediate folded path's separate GAS>1 marker behavior is not changed; the two-micro-batch regression compares against a non-folded reference instead. Full folded-MoE BF16 Stage-1 integration has a separate pre-existing empty non-expert norm issue on non-zero TP ranks; the focused TP2 test isolates the gradient-lifecycle contract and does not claim full folded-MoE E2E acceptance. The FP16_Optimizer MoE grad-norm fix (#8635) has merged upstream and is inherited from master, not included as a separate change in this PR. No default-option change or full E2E rerun is part of this follow-up.

    Diff: 643 lines (+482 / -161) across 8 files Reviewer: 0z5a Review duration: 21410m
    All Vouch review confidence checks passed
    Residual risk 0.21 = Change Risk (0.52) × (1 − Review Confidence (0.60)). Review depth and reviewer attention state are adequate.
    Model 1 · Change Risk (XGBoost)
    Score: 0.52 · 643 lines diff across 8 files
    0.52
    Model 2 · Review Depth (DistilBERT)
    Depth Score: 0.17 · 1 substantive comment(s)
    0.17
    Model 3 · Review Timing & Attention
    Session adequacy: 100% · Reviewer familiarity: 35%
    60%
    Bedrock Claude 3 · Risk Narration
    (disconnected)
    Groq
    Groq Risk Narration
    AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
    Model 1 · Change Risk Attribution
    SHAP Weights
    Diff Size diff_size
    0.387
    Shallow Review Depth shallow_review_depth
    0.289
    Real PR Conversation Timeline
    1 comments
    "Review active on #8654"
    general weight: 0.35
    Reviewers
    Assignees
    Unassigned
    Labels
    risk: low confidence: 60.5% reviewed
    Vouch Risk Breakdown
    AI-powered review risk analysis
    Change Risk
    Change Risk · Model 1 (XGBoost)
    Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
    Score: 51.9% (MEDIUM) · Higher = larger blast radius
    51.9%
    Review Confidence
    Review Confidence · Models 2 & 3
    Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
    Score: 60.5% · Lower = rushed or shallow review
    60.5%
    Residual Risk
    Residual Risk · Core Equation
    Residual = Change × (1 − Confidence)
    Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
    Score: 20.5% · Safe to merge
    20.5%