Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RuntimeError: Expected all tensors to be on the same device, but found at least two devices, cuda:0 and cpu! in ch05/01_main-chapter-code/gpt_generate.py #435

Closed
xfpg21421 opened this issue Nov 14, 2024 · 1 comment · Fixed by #436
Assignees
Labels
bug Something isn't working

Comments

@xfpg21421
Copy link

Bug description

gpt = GPTModel(gpt_config)
load_weights_into_gpt(gpt, params)
gpt.to(device)
gpt.eval()

tokenizer = tiktoken.get_encoding("gpt2")
torch.manual_seed(123)

token_ids = generate(
    model=gpt,
    idx=text_to_token_ids(input_prompt, tokenizer),
    max_new_tokens=25,
    context_size=gpt_config["context_length"],
    top_k=50,
    temperature=1.0
)

print("Output text:\n", token_ids_to_text(token_ids, tokenizer))`

If code above runs in GPU device, error below would appear:
RuntimeError: Expected all tensors to be on the same device, but found at least two devices, cuda:0 and cpu! (when checking argument for argument index in method wrapper_CUDA__index_select)

Since the gpt model has been put to the GPU, the idx tensor from text_to_token_ids(input_prompt, tokenizer) has not been put to the GPU. So it should be modified as text_to_token_ids(input_prompt, tokenizer).to(device)

What operating system are you using?

Linux

Where do you run your code?

Other cloud environment (AWS, Azure, GCP)

Environment




@rasbt
Copy link
Owner

rasbt commented Nov 14, 2024

Good catch, thanks for reporting this. Looks like it was missing a .to(device) for the prompt.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants