Repository navigation
perf: drop dead dtype loop in move_to_and_update_config - #2057
Open
faisalhacks wants to merge 1 commit into
Open
faisalhacks wants to merge 1 commit into
faisalhacks wants to merge 1 commit into
Conversation
…anch The loop wrote into a fresh state_dict() copy, so it never changed the model; nn.Module.to() already converts the parameters. It rebuilt the state_dict once per parameter (quadratic) and discarded a cast of every tensor. Remove it and add a test. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This branch has not been deployed
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.
Description
move_to_and_update_confighas a dead loop in itstorch.dtypebranch:state_dict()returns a fresh dict on every call, so the assignment never reaches the model. The weights are actually converted by thenn.Module.to(model, device_or_dtype)call at the end of the function, so behaviour is correct today. The loop only costs time:model.state_dict()is rebuilt once per parameter, so the cost is quadratic in the number of parameters.TransformerBridge.to()calls this helper for every dtype change, so it is on the current code path.Measured on a toy
nn.Modulewith a countingstate_dict(), callingmove_to_and_update_config(m, torch.float16)on the base commit:Parameters and
cfg.dtypeend up correct, butstate_dict()is called once per parameter plus one, and time grows about 4x per doubling.This PR removes the loop. The dtype is still applied by
nn.Module.to(), andcfg.dtypeis still updated first. No behaviour change.Type of change
Checklist:
tests/unit/utilities/test_devices.py, 23 passed; I did not run the full unit suite)The new test uses a real
nn.Modulewith a countingstate_dict(). It passes with the fix and fails on the base source (state_dict_callsis nonzero). I also ranblack --check,isort --check-onlyandmypyon the changed files.🤖 Generated with Claude Code