[bugfix] fix partition context unpatch #7566
Merged
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.
Fix asymmetric patching/unpatching in InsertPostInitMethodToModuleSubClasses
Problem Description
The
InsertPostInitMethodToModuleSubClassescontext manager patches__init__methods of model classes during entry and unpatches them during exit.However, asymmetric condition checks between patching and unpatching can introduce subtle inheritance bugs.
Root Cause Analysis
The issue occurs with classes that have multiple inheritance where:
__init__nn.Modulenn.ModuleCurrent asymmetric logic:
Execution flow:
__init__), Parent C is patched_old_initfrom Parent C and gets incorrectly "restored"Result: Child A's
__init__points to Parent C's original__init__, bypassing Parent B and breaking the inheritance chain.Reproduction Case
This pattern is common in Hugging Face models:
Solution
Apply symmetric condition checking in both patch and unpatch operations:
This ensures that only classes that were explicitly patched during entry get restored during exit.
Testing
The fix has been validated against the Qwen3ForSequenceClassification reproduction case and resolves the inheritance chain corruption.
Related Issues