Skip to content

perf: drop dead dtype loop in move_to_and_update_config - #2057

Open
faisalhacks wants to merge 1 commit into
TransformerLensOrg:devfrom
faisalhacks:fix-bridge-to-dtype-noop
Open

faisalhacks wants to merge 1 commit into
TransformerLensOrg:devfrom
faisalhacks:fix-bridge-to-dtype-noop

Conversation

@faisalhacks

Copy link
Copy Markdown

Description

move_to_and_update_config has a dead loop in its torch.dtype branch:

for k, v in model.state_dict().items():
    model.state_dict()[k] = v.to(device_or_dtype)

state_dict() returns a fresh dict on every call, so the assignment never reaches the model. The weights are actually converted by the nn.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.
  • Every tensor is cast to the target dtype and the result is thrown away, which is a transient allocation per parameter.

TransformerBridge.to() calls this helper for every dtype change, so it is on the current code path.

Measured on a toy nn.Module with a counting state_dict(), calling move_to_and_update_config(m, torch.float16) on the base commit:

layers=50  params=100 state_dict_calls=101 secs=0.0210 param_dtypes={torch.float16} cfg.dtype=torch.float16
layers=100 params=200 state_dict_calls=201 secs=0.0758 param_dtypes={torch.float16} cfg.dtype=torch.float16
layers=200 params=400 state_dict_calls=401 secs=0.3254 param_dtypes={torch.float16} cfg.dtype=torch.float16
layers=400 params=800 state_dict_calls=801 secs=1.2314 param_dtypes={torch.float16} cfg.dtype=torch.float16

Parameters and cfg.dtype end up correct, but state_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(), and cfg.dtype is still updated first. No behaviour change.

Type of change

  • Bug fix (non-breaking change which fixes an issue). This is a performance fix: it removes dead code with quadratic cost, not a correctness fix.
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • This change requires a documentation update

Checklist:

  • I have commented my code, particularly in hard-to-understand areas
  • I have made corresponding changes to the documentation
  • My changes generate no new warnings
  • I have added tests that prove my fix is effective or that my feature works
  • New and existing unit tests pass locally with my changes (ran tests/unit/utilities/test_devices.py, 23 passed; I did not run the full unit suite)
  • I have not rewritten tests relating to key interfaces which would affect backward compatibility

The new test uses a real nn.Module with a counting state_dict(). It passes with the fix and fails on the base source (state_dict_calls is nonzero). I also ran black --check, isort --check-only and mypy on the changed files.

🤖 Generated with Claude Code

…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

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants