Skip to content

Fix: preserve device index in CrossLayerTranscoder lazy loading#63

Merged
hannamw merged 2 commits intosafety-research:mainfrom
Xwwan:fix/transcoder-device-index-loss
Jan 29, 2026
Merged

Fix: preserve device index in CrossLayerTranscoder lazy loading#63
hannamw merged 2 commits intosafety-research:mainfrom
Xwwan:fix/transcoder-device-index-loss

Conversation

@Xwwan
Copy link
Contributor

@Xwwan Xwwan commented Dec 20, 2025

What is the current behavior?

When initializing CrossLayerTranscoder with a specific device index (e.g., device="cuda:1") AND enabling lazy_decoder or lazy_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 (usually cuda: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 specifies cuda:1, the encoder/decoder is correctly initialized on cuda:1.

Steps to reproduce

import torch
from circuit_tracer.transcoder.cross_layer_transcoder import load_clt
from transformers import AutoModelForCausalLM, AutoTokenizer
from circuit_tracer.replacement_model import ReplacementModel
from circuit_tracer.attribution.attribute import attribute

model_path = "/PATH/TO/gemma-2-2b"
clt_path = "/PATH/TO/clt-gemma-2-2b-426k"
device = torch.device("cuda:1")
dtype = torch.bfloat16

clt = load_clt(
    clt_path,
    device=device,
    dtype=dtype,
    lazy_decoder=True,
    lazy_encoder=True,
)


hf_model = AutoModelForCausalLM.from_pretrained(
    model_path,
    torch_dtype=dtype,
)

tokenizer = AutoTokenizer.from_pretrained(model_path)
tokenizer.padding_side = "left"
if tokenizer.pad_token_id is None:
    tokenizer.pad_token_id = tokenizer.eos_token_id


model = ReplacementModel.from_pretrained_and_transcoders(
    model_name="google/gemma-2-2b",
    transcoders=clt,
    hf_model=hf_model,
    tokenizer=tokenizer,
    device=device,
    dtype=dtype,
    default_prepend_bos=True,
    default_padding_side="left",
)

prompt = "The capital of France is Paris."

graph = attribute(
    model=model,
    prompt=prompt,
    max_n_logits=5,
    desired_logit_prob=0.9,
    batch_size=64,
    max_feature_nodes=100,
    offload=None,
    verbose=False,
)

# Run Result: RuntimeError: Expected all tensors to be on the same device, but got mat2 is on cuda:0, different from other tensors on cuda:1 (when checking argument in method wrapper_CUDA_bmm)

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.
@hannamw
Copy link
Collaborator

hannamw commented Jan 24, 2026

Thanks for opening this PR - this seems like a real issue! Would you mind making a couple of changes?:

  • Instead of using the pattern
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.

  • Could you apply these changes to single_layer_transcoder.py as well? It suffers from the same issues.

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

@hannamw hannamw merged commit a052c97 into safety-research:main Jan 29, 2026
1 check 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.

2 participants