Skip to content

.to on a PreTrainedModel throws a Pyright type check error. What is the correct way to put a model to the device that does not throw type check errors? #38052

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

Open
4 tasks
nickeisenberg opened this issue May 9, 2025 · 1 comment
Labels

Comments

@nickeisenberg
Copy link

System Info

(venv) nicholas@B367309:tmp(master)$ transformers-cli env

Copy-and-paste the text below in your GitHub issue and FILL OUT the two last points.

  • transformers version: 4.51.1
  • Platform: Linux-5.10.16.3-microsoft-standard-WSL2-x86_64-with-glibc2.39
  • Python version: 3.12.3
  • Huggingface_hub version: 0.30.2
  • Safetensors version: 0.5.3
  • Accelerate version: 1.6.0
  • Accelerate config: not found
  • DeepSpeed version: not installed
  • PyTorch version (GPU?): 2.6.0+cu126 (True)
  • Tensorflow version (GPU?): not installed (NA)
  • Flax version (CPU?/GPU?/TPU?): not installed (NA)
  • Jax version: not installed
  • JaxLib version: not installed
  • Using distributed or parallel set-up in script?:
  • Using GPU in script?:
  • GPU type: NVIDIA RTX 2000 Ada Generation Laptop GPU

Who can help?

No response

Information

  • The official example scripts
  • My own modified scripts

Tasks

  • An officially supported task in the examples folder (such as GLUE/SQuAD, ...)
  • My own task or dataset (give details below)

Reproduction

Here is a small snippet

from transformers.models.auto.modeling_auto import AutoModelForCausalLM
from transformers.models.llama.modeling_llama import LlamaForCausalLM

model = AutoModelForCausalLM.from_pretrained(
    "deepseek-ai/deepseek-coder-1.3b-instruct", torch_dtype=torch.float16
)
assert isinstance(model, LlamaForCausalLM)
model.to("cuda:0")

This code runs fine and correctly puts the model to the device, however, Pyright throws a pre-runtime type check error on the `model.to("cuda:0") call. This is the error,

Pyright: Argument of type "Literal['cuda:0']" cannot be assigned to parameter "self" of 
type "LlamaForCausalLM" in function "__call__".
"Literal['cuda:0']" is not assignable to "LlamaForCausalLM" [reportArgumentType]  

What is the correct way to put a model to the device that will satisfy the type checker?

Expected behavior

There should be know static type check error when doing model.to(<device>)

@nickeisenberg nickeisenberg changed the title .to on a PreTrainedModel throws a PyRight type check error. What is the correct way to but a model to the device that does not throw type check errors? .to on a PreTrainedModel throws a Pyright type check error. What is the correct way to put a model to the device that does not throw type check errors? May 9, 2025
@Rocketknight1
Copy link
Member

I'm afraid we don't have complete type hint support! If you run with strict type checking, you will almost certainly encounter these errors. We recommend relaxing your typing requirements.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants