Fix: preserve device index in CrossLayerTranscoder lazy loading#63
Merged
hannamw merged 2 commits intosafety-research:mainfrom Jan 29, 2026
Merged
Conversation
Previously, using self.device.type caused the encoder/decoder to load on the default device (e.g., 'cuda') instead of the specified device (e.g., 'cuda:1'). This commit ensures the correct device index is used.
Collaborator
|
Thanks for opening this PR - this seems like a real issue! Would you mind making a couple of changes?:
with safe_open(enc_file, framework="pt", device=self.device.type) as f:
return f.get_tensor(f"W_enc_{layer_id}").to(dtype=self.dtype, device=self.device)could you instead use this? with safe_open(enc_file, framework="pt", device=str(self.device)) as f:
return f.get_tensor(f"W_enc_{layer_id}").to(dtype)This way, it'll just be loaded directly onto the correct device.
Sorry to take so long to respond to this! Happy to do this myself as well if you don't have the time, but I thought I'd ask first |
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 is the current behavior?
When initializing
CrossLayerTranscoderwith a specific device index (e.g.,device="cuda:1") AND enablinglazy_decoderorlazy_encoder, the sub-modules are loaded onto the wrong device.This happens because the code was accessing
self.device.type(which returns just"cuda") instead of the full device object or string. This strips away the device index information, causing tensors/models to fall back to the default device (usuallycuda:0).What is the new behavior?
I updated the logic to use
self.device(or the correct device string) during the lazy loading process. This ensures that if a user specifiescuda:1, the encoder/decoder is correctly initialized oncuda:1.Steps to reproduce