Skip to content

fix: take cfg.device from a pre-loaded hf_model instead of get_device() - #1949

Merged
jlarson4 merged 2 commits into
TransformerLensOrg:devfrom
tivris:fix-hf-model-device
Oct 27, 2026
Merged

jlarson4 merged 2 commits into
TransformerLensOrg:devfrom
tivris:fix-hf-model-device

Conversation

@tivris

@tivris tivris commented Oct 26, 2026

Copy link
Copy Markdown
Contributor

Description

Fixes #1945.

Without device=, boot kept cfg.device at get_device() for an hf_model with no hf_device_map, but it never moves that model. When the weights were on another device, calls that move their inputs to cfg.device failed, including bridge("text"), run_with_hooks on text and generate.

get_device() is now called only when boot loads the model itself. Without device=, an hf_model with no hf_device_map gets cfg.device from the existing fallback that reads its first parameter, as in build_bridge_from_module. An explicit device= and models with an hf_device_map are unchanged.

test_boot_device.py boots a tiny from_config GPT-NeoX built on the meta device through hf_model=, with get_device patched to return "cuda", and checks that cfg.device is "meta", which neither get_device() nor a CPU default would give. It fails on dev (cfg.device is "cuda") and passes with the fix. On MPS, the issue's example and its CPU variant with TRANSFORMERLENS_ALLOW_MPS=1 now run.

Type of change

  • Bug fix (non-breaking change which fixes an issue)

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
  • I have not rewritten tests relating to key interfaces which would affect backward compatibility

@jlarson4 jlarson4 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good! I'll merge once CI passes

@jlarson4

Copy link
Copy Markdown
Collaborator

Update: there is a bug with the Qwen3.5 testing model we use for CI that is preventing Full Code Coverage from passing, I will patch dev and push the patch to your branch when it is verified.

@jlarson4
jlarson4 merged commit 11197c2 into TransformerLensOrg:dev Oct 27, 2026
29 checks passed
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.

[Bug Report] boot_transformers(hf_model=...) without device= can record the wrong cfg.device

2 participants