Add materialize_meta_tensors to reverse dispatch_model (#3866)#4078
Open
discobot wants to merge 1 commit into
Open
Add materialize_meta_tensors to reverse dispatch_model (#3866)#4078discobot wants to merge 1 commit into
materialize_meta_tensors to reverse dispatch_model (#3866)#4078discobot wants to merge 1 commit into
Conversation
…ce#3866) Models dispatched with offloaded modules keep their offloaded weights on the meta device, so they cannot be moved with .to() afterwards. This adds materialize_meta_tensors, which loads the offloaded weights back from the hooks' weights_map, removes all the hooks added by Accelerate and places the model on the requested device, making it a regular movable module again. It also covers weights that were never on a real device (the load_checkpoint_and_dispatch flow), which detaching the hooks alone cannot recover. Requested in huggingface#3866.
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?
Fixes #3866.
The blocker is twofold:
AlignDevicesHook.init_hookkeeps the real values of offloaded weights only inhook.weights_mapand leaves meta placeholders on the module, anddispatch_modelwrapsmodel.toto raise as soon as any parameter is on meta. Notably, removing the hooks is not enough:AlignDevicesHook.detach_hookonly restores weights that were on a real device before dispatch, so for models whose weights went straight from the checkpoint to the offload folder (load_checkpoint_and_dispatch,from_pretrained(device_map=...)) the offloaded weights stay on meta with no public way back.This PR adds
materialize_meta_tensors(model, target_device="cpu")inbig_modeling.py: it detaches all hooks (which also unwrapsto/cuda), loads the weights still on meta from each offload hook'sweights_map(mirroringAlignDevicesHook.pre_forward, including the int8 fp16 statistics), reties tied parameters, dropshf_device_map, and moves the model totarget_device. The model is then a regular module again and.to()works for the CPU-parking round-trip described in the issue.Four new CPU-runnable tests in
tests/test_big_modeling.pycover thedispatch_modelandload_checkpoint_and_dispatchflows, tied weights, andoffload_buffers=True; outputs match the un-dispatched reference and.to()no longer raises or warns afterwards.On the API shape: @SunMarc suggested a context manager or a
dispatch_modelflag — I went with the plain function from the issue since it is the primitive both of those can be built on (a context manager only needs to re-dispatch with the saved device map on exit). Happy to add either on top if preferred.Before submitting
Pull Request section?
to it if that's the case. Feature Request: Add API to Materialize Meta Tensors for Device Movement #3866
documentation guidelines, and
here are tips on formatting docstrings.
Who can review?
@SunMarc