QwenLM / Qwen2-VL

Qwen2-VL is the multimodal large language model series developed by Qwen team, Alibaba Cloud.
Apache License 2.0
3.16k stars 193 forks source link

RuntimeError: probability tensor contains either `inf`, `nan` or element < 0 #299

Open varungupta31 opened 1 month ago

varungupta31 commented 1 month ago

I have been trying to fix this error for a while now, and the ongoing threads are of NO help.

I have checked these (and ALL issue on the HF community page for this model):

Minimal Working Example

As taken directly from the model card on Huggingface,

from transformers import Qwen2VLForConditionalGeneration, AutoTokenizer, AutoProcessor
from qwen_vl_utils import process_vision_info

# default: Load the model on the available device(s)
model = Qwen2VLForConditionalGeneration.from_pretrained(
    "Qwen/Qwen2-VL-7B-Instruct", torch_dtype="auto", device_map="auto"
)

# default processer
processor = AutoProcessor.from_pretrained("Qwen/Qwen2-VL-7B-Instruct")

processor = AutoProcessor.from_pretrained("Qwen/Qwen2-VL-7B-Instruct", min_pixels=min_pixels, max_pixels=max_pixels)

messages = [
    {
        "role": "user",
        "content": [
            {
                "type": "image",
                "image": "https://qianwen-res.oss-cn-beijing.aliyuncs.com/Qwen-VL/assets/demo.jpeg",
            },
            {"type": "text", "text": "Describe this image."},
        ],
    }
]

# Preparation for inference
text = processor.apply_chat_template(
    messages, tokenize=False, add_generation_prompt=True
)
image_inputs, video_inputs = process_vision_info(messages)
inputs = processor(
    text=[text],
    images=image_inputs,
    videos=video_inputs,
    padding=True,
    return_tensors="pt",
)
inputs = inputs.to("cuda")

# Inference: Generation of the output
generated_ids = model.generate(**inputs, max_new_tokens=128)
generated_ids_trimmed = [
    out_ids[len(in_ids) :] for in_ids, out_ids in zip(inputs.input_ids, generated_ids)
]
output_text = processor.batch_decode(
    generated_ids_trimmed, skip_special_tokens=True, clean_up_tokenization_spaces=False
)
print(output_text)

Error

Unrecognized keys in `rope_scaling` for 'rope_type'='default': {'mrope_section'}
`Qwen2VLRotaryEmbedding` can now be fully parameterized by passing the model config through the `config` argument. All other arguments will be removed in v4.46
Loading checkpoint shards: 100%|_________________________________________________________________________________________________________| 5/5 [00:03<00:00,  1.54it/s]
Traceback (most recent call last):
  File "/home/varun/qwen2vlm/official.py", line 53, in <module>
    generated_ids = model.generate(**inputs, max_new_tokens=128)
                    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/varun/.conda/envs/qwen2/lib/python3.12/site-packages/torch/utils/_contextlib.py", line 116, in decorate_context
    return func(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^
  File "/home/varun/.conda/envs/qwen2/lib/python3.12/site-packages/transformers/generation/utils.py", line 2048, in generate
    result = self._sample(
             ^^^^^^^^^^^^^
  File "/home/varun/.conda/envs/qwen2/lib/python3.12/site-packages/transformers/generation/utils.py", line 3044, in _sample
    next_tokens = torch.multinomial(probs, num_samples=1).squeeze(1)
                  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
RuntimeError: probability tensor contains either `inf`, `nan` or element < 0

I have tried :

So far I have not been able to get this model working.

Relevant Package Version

torch                     2.4.1
transformers              4.46.0.dev0
flash-attn                2.6.3
qwen-vl-utils             0.0.8

GPUs

Trying this on RTX A6000

Kindly help,

Thanks.

zhaolj commented 1 month ago

the same issue

varungupta31 commented 1 month ago

For the time being,

Changing:

generated_ids = model.generate(**inputs, max_new_tokens=128)

To:

generated_ids = model.generate(**inputs, max_new_tokens=128, do_sample=False)

i.e., greedy decoding solved the issue, but I am not sure why this is happening; not everyone may want to do greedy decoding.

This 'fix' was quite obvious from the error stack trace that sampling is where something is going wrong.