Fix compile_regions running the uncompiled module - #4188
Merged
SunMarc merged 1 commit intoSep 7, 2026
Merged
Conversation
Under mixed precision, prepare_model installs its autocast forward as an instance attribute bound to the module, then calls compile_regions. The copy inherits that binding through new_module.__dict__.update(), so it re-enters the original: the compiled blocks never run, and state set after prepare() lands on an object that never executes. Re-bind copied values whose __self__ is the module being cloned.
|
The docs for this PR live here. All of your documentation changes will be reflected on that endpoint. The docs are available until 30 days after the last update. |
4 tasks
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.
What does this PR do?
Under mixed precision,
compile_regionsreturns a module that runs the uncompiled original: nothing is traced, and gradient checkpointing set afterprepare()never reaches the executing object. Its__dict__copy carries instance methods still bound to the source module; this PR re-binds them.Problem
The autocast wrapper is an instance attribute bound to
model; the copycompile_regionsbuilds afterwards (accelerator.py:2064) inherits the binding, so_call_implreadsself.forwardfrom__dict__and runs the original with its own uncompiled children.Trigger:
mixed_precision != "no"withuse_regional_compilationon a model with repeated blocks. Unaffected: plaintorch.compileand the in-placecompile_regions_fsdp2/compile_regions_deepspeed.Reproduction
CPU only, single process.
Fix
__self__ is moduleguard limits the rewrite to entries left pointing at the source;MethodTypeis already imported, and the_compile_regionsrecursion covers nested copies._original_forwardis rebound too, sounwrap_model(keep_fp32_wrapper=False)restores the right binding.Tests
tests/test_compile.pygainsRegionalCompilationRebindTester: CPU-only,backend="eager", separate from the@skippedRegionalCompilationTester.test_instance_bound_methods_are_rebound— the returned module runs, sees post-compile state, blocks areOptimizedModule. Fails onmain.test_no_instance_bound_methods_is_a_no_op— negative control; passes either way.pytest tests/test_compile.py: 2 passed, 5 skipped.Measured effect
40 steps, bf16 LoRA on a diffusion transformer, one B200, checkpointing and
use_regional_compilationon; onlyacceleratediffers.v1.14.0{}On
v1.14.0neither checkpointing nor compilation took effect.Note for reviewers
prepare_modelwraps before compiling, leaving the binding on a nested copy.