Fix ZeRO-3 hooks for attribute-delegating modules#8144
Open
Functionhx wants to merge 2 commits into
Open
Conversation
Signed-off-by: Yuchen Fan <functionhx@gmail.com>
Functionhx
marked this pull request as ready for review
July 15, 2026 08:41
tohtana
requested changes
Jul 16, 2026
tohtana
left a comment
Collaborator
There was a problem hiding this comment.
Hi @Functionhx,
Thank you for the fix! Overall this looks good to me. I would like to have a few changes before merge.
For the new test file, we now use this copyright (please refer to existing files):
# Copyright (c) DeepSpeed Team.
# SPDX-License-Identifier: Apache-2.0
# DeepSpeed TeamThe tests should assert that the wrapper and child own distinct pre/post backward hook classes and that both modules' applied_pre_backward_ref_cnt values return to zero after each backward. The current training loop can still pass if the counter lookup runs based on the existing hasattr behavior. (draft of the fix)
Signed-off-by: Yuchen Fan <functionhx@gmail.com>
Author
|
@tohtana Thanks for the review! I've addressed the requested changes. Could you please take another look?🩷 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Summary
Root cause
ZeRO-3 registers child modules before their parents. Wrappers such as PEFT's
ModulesToSaveWrapperdelegate unknown attributes to a wrapped child, sohasattr(wrapper, "pre_bwd_fn")andhasattr(wrapper, "post_bwd_fn")can incorrectly find the child's hook classes. DeepSpeed then skips creating wrapper-local classes even though those classes capture module-specific closures and counters. During forward, the delegated post-backward class can access the child before itsds_grads_remainingcounter is initialized, raisingAttributeError.Check the module's own
__dict__for these internal attributes so each module receives hook classes and counters bound to itself.User impact
This restores ZeRO-3 training with attribute-delegating wrappers, including PEFT LoRA configurations that use
modules_to_save=["lm_head"].Fixes #7615.
Testing
pre-commit run --files deepspeed/runtime/zero/parameter_offload.py tests/unit/runtime/zero/test_zero_hook_attribute_delegation.pyPYTEST_DISABLE_PLUGIN_AUTOLOAD=1 pytest -q tests/unit/runtime/zero/test_zero_hook_attribute_delegation.py tests/unit/runtime/zero/test_zero_dynamic_class.py -s(3 passed)ModulesToSaveWrapper