fix: compile inner model before DDP wrapping to prevent Dynamo tracing DDP internals#4017
Open
anishesg wants to merge 1 commit intohuggingface:mainfrom
Open
fix: compile inner model before DDP wrapping to prevent Dynamo tracing DDP internals#4017anishesg wants to merge 1 commit intohuggingface:mainfrom
anishesg wants to merge 1 commit intohuggingface:mainfrom
Conversation
…g DDP internals ## What does this PR do? Signed-off-by: anish k <ak8686@princeton.edu>
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?
When using
torch.compilewith multi-GPU (DDP) training via Accelerate, users hit a crash during the forward pass:The root cause is in
accelerator.py'sprepare_model: the code was wrapping the model withDistributedDataParallelfirst, then applyingtorch.compileto the DDP wrapper. This caused Dynamo to trace into DDP's internal_pre_forwardhook which callsself.logger.set_runtime_stats_and_log()— a method on a user-defined object that Dynamo cannot trace.The fix follows the PyTorch-recommended pattern for DDP +
torch.compile: compile the inner model before wrapping it with DDP. DDP then operates outside the compiled region, so its internal logging and communication hooks are never seen by Dynamo. This is applied to both theMULTI_GPUandMULTI_CPUDDP paths inprepare_model. The final compile guard is also updated to skip models that already have compiled submodules (viahas_compiled_regions), preventing the DDP wrapper from being double-compiled.Fixes #3991