RuntimeError: Tensor.item() cannot be called on meta tensors

#6
by kimyong95 - opened

Anyone encountered the following error?

Traceback (most recent call last):
  File "/scratch/users/ntu/kimyong0/code/guide-language-diffusion/test-gemma.py", line 22, in <module>
    output = model.generate(**input_ids, max_new_tokens=512)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/users/ntu/kimyong0/scratch/.conda/envs/guide-language-diffusion/lib/python3.12/site-packages/torch/utils/_contextlib.py", line 120, in decorate_context
    return func(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^
  File "/home/users/ntu/kimyong0/scratch/.conda/envs/guide-language-diffusion/lib/python3.12/site-packages/transformers/models/diffusion_gemma/generation_diffusion_gemma.py", line 646, in generate
    and (input_ids == self.config.image_token_id).any()
        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/users/ntu/kimyong0/scratch/.conda/envs/guide-language-diffusion/lib/python3.12/site-packages/torch/_meta_registrations.py", line 7457, in meta_local_scalar_dense
    raise RuntimeError("Tensor.item() cannot be called on meta tensors")
RuntimeError: Tensor.item() cannot be called on meta tensors

Environment (Python 3.12):

transformers==5.11.0
torch==2.8.0+cu128

Hardware: NVIDIA A100 40GB

We are seeing this with behaviour when selecting the accelerator as "NVIDIA A100 40GB".

Hi folks! One of the situations this error shows up is when your machine has more than 1 GPU. If that's your case, follow this GH issue: https://github.com/huggingface/transformers/issues/46566

Otherwise, let us know :)

Changing:

model = DiffusionGemmaForBlockDiffusion.from_pretrained(
MODEL_ID,
dtype="auto",
device_map="auto",
)

to:

model = DiffusionGemmaForBlockDiffusion.from_pretrained(
MODEL_ID,
dtype="auto",
).to("cuda")

fixes this issue on Jetson Thor (transformers=5.12.0).

This transformers PR fixes this issue 🤗 Make sure to install transformers from main until 5.13 gets released.

Sign up or log in to comment