GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Fix hook count performance regression from v0.18.5 by tohtana · Pull Request #7886 · deepspeedai/DeepSpeed

Conversation

@tohtana

@tohtana tohtana commented Mar 4, 2026

Copy link
Copy Markdown
Collaborator

Fixes performance regressions reported in #7882 and #7885.

PR #7780 added dynamic hook count computation for reentrant checkpointing correctness, but placed the call inside every gradient hook closure. For a model with n parameter tensors, this creates significant overhead per backward pass.

Summary:

  1. Added should_refresh_expected_hook_count() predicate that returns true only at backward phase boundaries (first hook, or new reentrant phase), so count_used_parameters_in_backward() is called once per phase instead of once per hook.
  2. Applied this predicate in ZeRO-1/2 (stage_1_and_2.py) and both ZeRO-3 hook sites (stage3.py), reusing the cached_max_expected_hooks_seen value when refresh isn't needed.
  3. Changed enter_backward() to reset hook counters on first real backward entry, preventing pollution from pre-user-backward autograd calls (e.g., TiledFusedLogitsLoss).

With 24-layer transformer, ~267M params (147 parameter tensors), ZeRO-2, 8×H100 80GB, bf16, batch size 8, 20 warmup + 20 measured iterations:

  • Before fix: 0.1265s/iter
  • After fix: 0.0505s/iter

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

jue-jue-zi and chatgpt-codex-connector[bot] reacted with thumbs up emoji
All reactions
  • 👍 2 reactions
Masahiro Tanaka added 3 commits March 4, 2026 12:49
Masahiro Tanaka
Add should_refresh_expected_hook_count and harden enter_backward
e1b99f7
Add phase-refresh predicate to BackwardHookStateManager to avoid
calling count_used_parameters_in_backward() on every gradient hook.
Harden enter_backward() to reset counters on first real backward
entry, preventing pre-user-backward hook pollution.

Signed-off-by: Masahiro Tanaka <[email protected]>
Masahiro Tanaka
Fix ZeRO-2 hook to cache count_used_parameters_in_backward result
91b591b
Use should_refresh_expected_hook_count() to only recompute the
expected hook count at phase boundaries instead of every hook
invocation, reducing O(n^2) overhead to O(p*n).

Signed-off-by: Masahiro Tanaka <[email protected]>
Masahiro Tanaka
Fix ZeRO-3 hooks to cache count_used_parameters_in_backward result
d3a6e99
Apply the same refresh/cache pattern in both reduce_partition_and_remove_grads
and reduce_leaf_module_grads to avoid per-hook O(n) overhead.

Signed-off-by: Masahiro Tanaka <[email protected]>
@tohtana

tohtana commented Mar 4, 2026

Copy link
Copy Markdown
Collaborator Author

@codex

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Mar 4, 2026

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. More of your lovely PRs please.

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@tohtana tohtana mentioned this pull request Mar 4, 2026
Fix performance regression in grad_handling_hook #7882
Closed
@jue-jue-zi jue-jue-zi mentioned this pull request Mar 5, 2026
[BUG] Training with Zero-2 slow down a lot from v0.18.4 to v0.18.5 #7885
Closed
delock
delock reviewed Mar 5, 2026
View reviewed changes
Comment thread deepspeed/runtime/zero/stage3.py

@instrument_w_nvtx
def reduce_partition_and_remove_grads(*notneeded):
refresh_expected = self.should_refresh_expected_hook_count()

@delock delock Mar 5, 2026

Copy link
Copy Markdown
Collaborator

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @tohtana the refresh_expected = self.should_refresh_expected_hook_count() line is used on L1289, why it is placed before L1284 (reenter_backward_if_needed()) and L1286 (reduce_ready_partitions_and_remove_grads()). Is there implicit dependency here?

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

tohtana reacted with thumbs up emoji
All reactions
  • 👍 1 reaction

@tohtana tohtana Mar 5, 2026

Copy link
Copy Markdown
Collaborator Author

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for checking, @delock!

Yes, there's actually a dependency. should_refresh_expected_hook_count() detects reentrant phase boundaries by checking backward_active_depth == 0. reenter_backward_if_needed() increments backward_active_depth from 0 → 1 when it detects a new phase. If we called reenter_backward first, it would set backward_active_depth = 1 before the predicate runs, making the condition always false.

I added a comment to clarify it.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

delock reacted with thumbs up emoji
All reactions
  • 👍 1 reaction
delock
delock approved these changes Mar 5, 2026
View reviewed changes
@mjkvaak-amd

mjkvaak-amd commented Mar 5, 2026

Copy link
Copy Markdown

Confirming that this fix improves AMD 8× MI300X training performance of Qwen-Image by ~10× compared to DeepSpeed v0.18.6.

tohtana reacted with heart emoji
All reactions
  • ❤️ 1 reaction

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@rraminen

rraminen commented Mar 5, 2026 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Contributor

This fix resolves the ~20% performance regression introduced in DeepSpeed v0.18.5 (311674f) for the DeepSpeed-Megatron GPT-3 13B workload.

tohtana reacted with heart emoji
All reactions
  • ❤️ 1 reaction

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@tohtana
tohtana marked this pull request as ready for review March 5, 2026 17:06
@tohtana
tohtana requested review from loadams and tjruwase as code owners March 5, 2026 17:06
Masahiro Tanaka and others added 2 commits March 5, 2026 09:09
Masahiro Tanaka @rraminen
Add regression tests for hook count performance fix
e1b41ee
Co-authored-by: Ramya Ramineni <[email protected]>
Signed-off-by: Masahiro Tanaka <[email protected]>
Masahiro Tanaka
Add comment clarifying refresh-before-reenter ordering
c43e423
Signed-off-by: Masahiro Tanaka <[email protected]>
@tohtana
tohtana force-pushed the tohtana/fix-perf-regression branch from fd933f3 to c43e423 Compare March 5, 2026 17:10
@tohtana
tohtana merged commit 6c59d54 into deepspeedai:master Mar 5, 2026
1 check passed

Uh oh!

There was an error while loading. Please reload this page.

nathon-lee pushed a commit to nathon-lee/DeepSpeed_woo that referenced this pull request Mar 7, 2026
@tohtana @rraminen @nathon-lee
Fix hook count performance regression from v0.18.5 (deepspeedai#7886)
306dea2
Fixes performance regressions reported in deepspeedai#7882 and deepspeedai#7885.

PR deepspeedai#7780 added dynamic hook count computation for reentrant
checkpointing correctness, but placed the call inside every gradient
hook closure. For a model with n parameter tensors, this creates
significant overhead per backward pass.

Summary:

1. Added `should_refresh_expected_hook_count()` predicate that returns
true only at backward phase boundaries (first hook, or new reentrant
phase), so `count_used_parameters_in_backward()` is called once per
phase instead of once per hook.
2. Applied this predicate in ZeRO-1/2 (stage_1_and_2.py) and both ZeRO-3
hook sites (stage3.py), reusing the `cached_max_expected_hooks_seen`
value when refresh isn't needed.
3. Changed enter_backward() to reset hook counters on first real
backward entry, preventing pollution from pre-user-backward autograd
calls (e.g., TiledFusedLogitsLoss).

With 24-layer transformer, ~267M params (147 parameter tensors), ZeRO-2,
8×H100 80GB, bf16, batch size 8, 20 warmup + 20 measured iterations:
  - Before fix: 0.1265s/iter
  - After fix: 0.0505s/iter

---------

Signed-off-by: Masahiro Tanaka <[email protected]>
Co-authored-by: Ramya Ramineni <[email protected]>
Signed-off-by: nathon-lee <[email protected]>
nathon-lee pushed a commit to nathon-lee/DeepSpeed_woo that referenced this pull request Mar 28, 2026
@tohtana @rraminen @nathon-lee
Fix hook count performance regression from v0.18.5 (deepspeedai#7886)
66554bf
Fixes performance regressions reported in deepspeedai#7882 and deepspeedai#7885.

PR deepspeedai#7780 added dynamic hook count computation for reentrant
checkpointing correctness, but placed the call inside every gradient
hook closure. For a model with n parameter tensors, this creates
significant overhead per backward pass.

Summary:

1. Added `should_refresh_expected_hook_count()` predicate that returns
true only at backward phase boundaries (first hook, or new reentrant
phase), so `count_used_parameters_in_backward()` is called once per
phase instead of once per hook.
2. Applied this predicate in ZeRO-1/2 (stage_1_and_2.py) and both ZeRO-3
hook sites (stage3.py), reusing the `cached_max_expected_hooks_seen`
value when refresh isn't needed.
3. Changed enter_backward() to reset hook counters on first real
backward entry, preventing pollution from pre-user-backward autograd
calls (e.g., TiledFusedLogitsLoss).

With 24-layer transformer, ~267M params (147 parameter tensors), ZeRO-2,
8×H100 80GB, bf16, batch size 8, 20 warmup + 20 measured iterations:
  - Before fix: 0.1265s/iter
  - After fix: 0.0505s/iter

---------

Signed-off-by: Masahiro Tanaka <[email protected]>
Co-authored-by: Ramya Ramineni <[email protected]>
Signed-off-by: nathon-lee <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@delock delock delock approved these changes
@tjruwase tjruwase Awaiting requested review from tjruwase tjruwase is a code owner
@loadams loadams Awaiting requested review from loadams loadams is a code owner

Uh oh!

There was an error while loading. Please reload this page.

Assignees

No one assigned

Labels

None yet

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

Uh oh!

There was an error while loading. Please reload this page.

4 participants

@tohtana @mjkvaak-amd @rraminen @delock
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.