[GH-ISSUE #4] Cannot inference model #1

Closed
opened 2026-10-26 21:09:46 +00:00 by bsakaguchi · 20 comments
Owner

Originally created by @williamium3000 on GitHub (Dec 15, 2024).
Original GitHub issue: https://github.com/deepseek-ai/DeepSeek-VL2/issues/4

When I try to inference, I get the following error.

Traceback (most recent call last):
File "/william/project/cognitive-dev-bench/test.py", line 57, in
outputs = vl_gpt.language.generate(
File "/home/miniconda3/envs/deepseek/lib/python3.10/site-packages/torch/utils/_contextlib.py", line 116, in decorate_context
return func(*args, **kwargs)
File "/home/miniconda3/envs/deepseek/lib/python3.10/site-packages/transformers/generation/utils.py", line 2231, in generate
result = self._sample(
File "/home/miniconda3/envs/deepseek/lib/python3.10/site-packages/transformers/generation/utils.py", line 3222, in _sample
outputs = self(**model_inputs, return_dict=True)
File "/home/miniconda3/envs/deepseek/lib/python3.10/site-packages/torch/nn/modules/module.py", line 1736, in _wrapped_call_impl
return self._call_impl(*args, **kwargs)
File "/home/miniconda3/envs/deepseek/lib/python3.10/site-packages/torch/nn/modules/module.py", line 1747, in _call_impl
return forward_call(*args, **kwargs)
File "/william/repo/DeepSeek-VL2/deepseek_vl/models/modeling_deepseek.py", line 1723, in forward
outputs = self.model(
File "/home/miniconda3/envs/deepseek/lib/python3.10/site-packages/torch/nn/modules/module.py", line 1736, in _wrapped_call_impl
return self._call_impl(*args, **kwargs)
File "/home/miniconda3/envs/deepseek/lib/python3.10/site-packages/torch/nn/modules/module.py", line 1747, in _call_impl
return forward_call(*args, **kwargs)
File "/william/repo/DeepSeek-VL2/deepseek_vl/models/modeling_deepseek.py", line 1592, in forward
layer_outputs = decoder_layer(
File "/home/miniconda3/envs/deepseek/lib/python3.10/site-packages/torch/nn/modules/module.py", line 1736, in _wrapped_call_impl
return self._call_impl(*args, **kwargs)
File "/home/miniconda3/envs/deepseek/lib/python3.10/site-packages/torch/nn/modules/module.py", line 1747, in _call_impl
return forward_call(*args, **kwargs)
File "/william/repo/DeepSeek-VL2/deepseek_vl/models/modeling_deepseek.py", line 1306, in forward
hidden_states, self_attn_weights, present_key_value = self.self_attn(
File "/home/miniconda3/envs/deepseek/lib/python3.10/site-packages/torch/nn/modules/module.py", line 1736, in _wrapped_call_impl
return self._call_impl(*args, **kwargs)
File "/home/miniconda3/envs/deepseek/lib/python3.10/site-packages/torch/nn/modules/module.py", line 1747, in _call_impl
return forward_call(*args, **kwargs)
File "/william/repo/DeepSeek-VL2/deepseek_vl/models/modeling_deepseek.py", line 893, in forward
q_pe, k_pe = apply_rotary_pos_emb(q_pe, k_pe, cos, sin, position_ids)
File "/william/repo/DeepSeek-VL2/deepseek_vl/models/modeling_deepseek.py", line 366, in apply_rotary_pos_emb
cos = cos[position_ids].unsqueeze(unsqueeze_dim)
IndexError: index is out of bounds for dimension with size 0

Originally created by @williamium3000 on GitHub (Dec 15, 2024). Original GitHub issue: https://github.com/deepseek-ai/DeepSeek-VL2/issues/4 When I try to inference, I get the following error. Traceback (most recent call last): File "/william/project/cognitive-dev-bench/test.py", line 57, in <module> outputs = vl_gpt.language.generate( File "/home/miniconda3/envs/deepseek/lib/python3.10/site-packages/torch/utils/_contextlib.py", line 116, in decorate_context return func(*args, **kwargs) File "/home/miniconda3/envs/deepseek/lib/python3.10/site-packages/transformers/generation/utils.py", line 2231, in generate result = self._sample( File "/home/miniconda3/envs/deepseek/lib/python3.10/site-packages/transformers/generation/utils.py", line 3222, in _sample outputs = self(**model_inputs, return_dict=True) File "/home/miniconda3/envs/deepseek/lib/python3.10/site-packages/torch/nn/modules/module.py", line 1736, in _wrapped_call_impl return self._call_impl(*args, **kwargs) File "/home/miniconda3/envs/deepseek/lib/python3.10/site-packages/torch/nn/modules/module.py", line 1747, in _call_impl return forward_call(*args, **kwargs) File "/william/repo/DeepSeek-VL2/deepseek_vl/models/modeling_deepseek.py", line 1723, in forward outputs = self.model( File "/home/miniconda3/envs/deepseek/lib/python3.10/site-packages/torch/nn/modules/module.py", line 1736, in _wrapped_call_impl return self._call_impl(*args, **kwargs) File "/home/miniconda3/envs/deepseek/lib/python3.10/site-packages/torch/nn/modules/module.py", line 1747, in _call_impl return forward_call(*args, **kwargs) File "/william/repo/DeepSeek-VL2/deepseek_vl/models/modeling_deepseek.py", line 1592, in forward layer_outputs = decoder_layer( File "/home/miniconda3/envs/deepseek/lib/python3.10/site-packages/torch/nn/modules/module.py", line 1736, in _wrapped_call_impl return self._call_impl(*args, **kwargs) File "/home/miniconda3/envs/deepseek/lib/python3.10/site-packages/torch/nn/modules/module.py", line 1747, in _call_impl return forward_call(*args, **kwargs) File "/william/repo/DeepSeek-VL2/deepseek_vl/models/modeling_deepseek.py", line 1306, in forward hidden_states, self_attn_weights, present_key_value = self.self_attn( File "/home/miniconda3/envs/deepseek/lib/python3.10/site-packages/torch/nn/modules/module.py", line 1736, in _wrapped_call_impl return self._call_impl(*args, **kwargs) File "/home/miniconda3/envs/deepseek/lib/python3.10/site-packages/torch/nn/modules/module.py", line 1747, in _call_impl return forward_call(*args, **kwargs) File "/william/repo/DeepSeek-VL2/deepseek_vl/models/modeling_deepseek.py", line 893, in forward q_pe, k_pe = apply_rotary_pos_emb(q_pe, k_pe, cos, sin, position_ids) File "/william/repo/DeepSeek-VL2/deepseek_vl/models/modeling_deepseek.py", line 366, in apply_rotary_pos_emb cos = cos[position_ids].unsqueeze(unsqueeze_dim) IndexError: index is out of bounds for dimension with size 0
Author
Owner

@NuiMrme commented on GitHub (Dec 15, 2024):

vl_gpt.language.generate( cause errors for me as well but in different files

<!-- gh-comment-id:2543994904 --> @NuiMrme commented on GitHub (Dec 15, 2024): `vl_gpt.language.generate(` cause errors for me as well but in different files
Author
Owner

@learningpro commented on GitHub (Dec 16, 2024):

please check your torch related packages version, My project works well with the following version

torch                    2.5.1
torchaudio               2.3.0
torchvision              0.20.1
<!-- gh-comment-id:2544409890 --> @learningpro commented on GitHub (Dec 16, 2024): please check your torch related packages version, My project works well with the following version ``` torch 2.5.1 torchaudio 2.3.0 torchvision 0.20.1 ```
Author
Owner

@weiaicunzai commented on GitHub (Dec 16, 2024):

I am getting the similar error, something is wrong with this cos variable.

packages/transformers/models/llama/modeling_llama.py", line 276, in apply_rotary_pos_emb
    q_embed = (q * cos) + (rotate_half(q) * sin)
RuntimeError: The size of tensor a (0) must match the size of tensor b (634) at non-singleton dimension 2
<!-- gh-comment-id:2544995793 --> @weiaicunzai commented on GitHub (Dec 16, 2024): I am getting the similar error, something is wrong with this cos variable. ``` packages/transformers/models/llama/modeling_llama.py", line 276, in apply_rotary_pos_emb q_embed = (q * cos) + (rotate_half(q) * sin) RuntimeError: The size of tensor a (0) must match the size of tensor b (634) at non-singleton dimension 2 ```
Author
Owner

@ApolloRay commented on GitHub (Dec 16, 2024):

cos = cos[position_ids].unsqueeze(unsqueeze_dim)

IndexError: index is out of bounds for dimension with size 0

same problem

<!-- gh-comment-id:2545034224 --> @ApolloRay commented on GitHub (Dec 16, 2024): cos = cos[position_ids].unsqueeze(unsqueeze_dim) IndexError: index is out of bounds for dimension with size 0 same problem
Author
Owner

@williamium3000 commented on GitHub (Dec 16, 2024):

@learningpro I use the following env
image
I cannot use the 12.4 cuda since my cuda is only 12.3 (using 12.4 cuda torch gives me

undefined symbol: __nvJitLinkComplete_12_4, version libnvJitLink.so.12

problem.
However, I believe the cuda is not a major issue here.

<!-- gh-comment-id:2545248240 --> @williamium3000 commented on GitHub (Dec 16, 2024): @learningpro I use the following env ![image](https://github.com/user-attachments/assets/a2523dd9-8d68-4b43-9d86-eaacda42f84a) I cannot use the 12.4 cuda since my cuda is only 12.3 (using 12.4 cuda torch gives me ``` undefined symbol: __nvJitLinkComplete_12_4, version libnvJitLink.so.12 ``` problem. However, I believe the cuda is not a major issue here.
Author
Owner

@ApolloRay commented on GitHub (Dec 16, 2024):

fix it.
you can add input_ids = prepare_inputs["input_ids"], at line 106 in inference.py
截屏2024-12-16 20 00 35

<!-- gh-comment-id:2545441738 --> @ApolloRay commented on GitHub (Dec 16, 2024): fix it. you can add input_ids = prepare_inputs["input_ids"], at line 106 in inference.py <img width="415" alt="截屏2024-12-16 20 00 35" src="https://github.com/user-attachments/assets/5dba04fb-fb17-4909-b122-3cfa79f0cd2a" />
Author
Owner

@robinren03 commented on GitHub (Dec 16, 2024):

fix it. you can add input_ids = prepare_inputs["input_ids"], at line 106 in inference.py 截屏2024-12-16 20 00 35

Thank you! It works for me.

<!-- gh-comment-id:2545802633 --> @robinren03 commented on GitHub (Dec 16, 2024): > fix it. you can add input_ids = prepare_inputs["input_ids"], at line 106 in inference.py <img alt="截屏2024-12-16 20 00 35" width="415" src="https://private-user-images.githubusercontent.com/36093263/396086643-5dba04fb-fb17-4909-b122-3cfa79f0cd2a.png?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3MzQzNTkyNjYsIm5iZiI6MTczNDM1ODk2NiwicGF0aCI6Ii8zNjA5MzI2My8zOTYwODY2NDMtNWRiYTA0ZmItZmIxNy00OTA5LWIxMjItM2NmYTc5ZjBjZDJhLnBuZz9YLUFtei1BbGdvcml0aG09QVdTNC1ITUFDLVNIQTI1NiZYLUFtei1DcmVkZW50aWFsPUFLSUFWQ09EWUxTQTUzUFFLNFpBJTJGMjAyNDEyMTYlMkZ1cy1lYXN0LTElMkZzMyUyRmF3czRfcmVxdWVzdCZYLUFtei1EYXRlPTIwMjQxMjE2VDE0MjI0NlomWC1BbXotRXhwaXJlcz0zMDAmWC1BbXotU2lnbmF0dXJlPWQwZDQ3Y2I1NDc3ZjE4ZTQ4MDk1NWMyZTFlYTNkZDllNzAxYTRkYWYzMTZlZDY2MTZiNmIxMWQ4YTMxNGZjNzgmWC1BbXotU2lnbmVkSGVhZGVycz1ob3N0In0.xCahjC3u9Pv28TBVe4gfh--LSTZ3UEEz_5nlyRDYL1A"> Thank you! It works for me.
Author
Owner

@Adenialzz commented on GitHub (Dec 17, 2024):

What is the expected output of the demo about two giraffes images ?

<!-- gh-comment-id:2547450406 --> @Adenialzz commented on GitHub (Dec 17, 2024): What is the expected output of the demo about two giraffes images ?
Author
Owner

@HubHop commented on GitHub (Dec 18, 2024):

Hi guys, if you're encountering the issue mentioned, please try running pip install transformers==4.38.2.

File "/william/repo/DeepSeek-VL2/deepseek_vl/models/modeling_deepseek.py", line 366, in apply_rotary_pos_emb
cos = cos[position_ids].unsqueeze(unsqueeze_dim)
IndexError: index is out of bounds for dimension with size 0

<!-- gh-comment-id:2550928148 --> @HubHop commented on GitHub (Dec 18, 2024): Hi guys, if you're encountering the issue mentioned, please try running `pip install transformers==4.38.2`. > File "/william/repo/DeepSeek-VL2/deepseek_vl/models/modeling_deepseek.py", line 366, in apply_rotary_pos_emb > cos = cos[position_ids].unsqueeze(unsqueeze_dim) > IndexError: index is out of bounds for dimension with size 0
Author
Owner

@HubHop commented on GitHub (Dec 18, 2024):

@Adenialzz Here is one example from DeepSeek-VL2-tiny:

Input:
<|User|>:
<|ref|>The giraffe at the back.<|/ref|>.

Expected output:
<|Assistant|>: <|ref|>The giraffe at the back.<|/ref|><|det|>582, 271, 942, 888<|/det|><|end▁of▁sentence|>

Please make sure you have set skip_special_tokens=False.

<!-- gh-comment-id:2550936444 --> @HubHop commented on GitHub (Dec 18, 2024): @Adenialzz Here is one example from DeepSeek-VL2-tiny: **Input:** <|User|>: <image> <|ref|>The giraffe at the back.<|/ref|>. **Expected output:** <|Assistant|>: <|ref|>The giraffe at the back.<|/ref|><|det|>[[582, 271, 942, 888]]<|/det|><|end▁of▁sentence|> Please make sure you have set `skip_special_tokens=False`.
Author
Owner

@williamium3000 commented on GitHub (Dec 18, 2024):

@HubHop Can you give an example of interleaved image text? The case in the hugging face won't work. Thanks!

<!-- gh-comment-id:2550980731 --> @williamium3000 commented on GitHub (Dec 18, 2024): @HubHop Can you give an example of interleaved image text? The case in the hugging face won't work. Thanks!
Author
Owner

@HubHop commented on GitHub (Dec 18, 2024):

@williamium3000 Sure! Below is one example of interleaved image-texts. No other changes required in the demo from the readme.

conversation = [
    {
        "role": "<|User|>",
        "content": "This is image_1: <image>\n"
                   "This is image_2: <image>\n"
                   "This is image_3: <image>\n If I am a vegetarian, what can I cook with these ingredients?",
        "images": [
            "images/multi_image_1.png",
            "images/multi_image_2.jpg",
            "images/multi_image_3.jpg",
        ],
    },
    {"role": "<|Assistant|>", "content": ""}
]

Expected output:

<|User|>: This is image_1: <image>
This is image_2: <image>
This is image_3: <image>
 If I am a vegetarian, what can I cook with these ingredients?

<|Assistant|>: Vegetarian dishes such as stir-fries, soups, stews, or roasted vegetables could be made using carrots and corn.<|end▁of▁sentence|>

Example images:

multi_image_1 multi_image_2 multi_image_3
<!-- gh-comment-id:2551110060 --> @HubHop commented on GitHub (Dec 18, 2024): @williamium3000 Sure! Below is one example of interleaved image-texts. No other changes required in the demo from the readme. ```python conversation = [ { "role": "<|User|>", "content": "This is image_1: <image>\n" "This is image_2: <image>\n" "This is image_3: <image>\n If I am a vegetarian, what can I cook with these ingredients?", "images": [ "images/multi_image_1.png", "images/multi_image_2.jpg", "images/multi_image_3.jpg", ], }, {"role": "<|Assistant|>", "content": ""} ] ``` **Expected output:** ``` <|User|>: This is image_1: <image> This is image_2: <image> This is image_3: <image> If I am a vegetarian, what can I cook with these ingredients? <|Assistant|>: Vegetarian dishes such as stir-fries, soups, stews, or roasted vegetables could be made using carrots and corn.<|end▁of▁sentence|> ``` **Example images:** <img src="https://github.com/user-attachments/assets/b4e7f85e-11f4-4b32-9dec-0da7342f07c3" alt="multi_image_1" width="200" /> <img src="https://github.com/user-attachments/assets/696af725-1884-4c36-88d8-900ed2831f38" alt="multi_image_2" width="200" /> <img src="https://github.com/user-attachments/assets/29657e83-5571-4470-bc54-f7f04219653e" alt="multi_image_3" width="200" />
Author
Owner

@williamium3000 commented on GitHub (Dec 18, 2024):

thanks

<!-- gh-comment-id:2551198504 --> @williamium3000 commented on GitHub (Dec 18, 2024): thanks
Author
Owner

@williamium3000 commented on GitHub (Dec 20, 2024):

Hi, can you please provide a multi-GPU inference (splitting the model to multiple GPUs) code as the standard deepseek-vl2 is too large to be handled on one single GPU?

<!-- gh-comment-id:2557882454 --> @williamium3000 commented on GitHub (Dec 20, 2024): Hi, can you please provide a multi-GPU inference (splitting the model to multiple GPUs) code as the standard deepseek-vl2 is too large to be handled on one single GPU?
Author
Owner

@williamium3000 commented on GitHub (Dec 22, 2024):

any clues? @HubHop

<!-- gh-comment-id:2558401878 --> @williamium3000 commented on GitHub (Dec 22, 2024): any clues? @HubHop
Author
Owner

@HubHop commented on GitHub (Dec 22, 2024):

Hi @williamium3000, thanks for mentioning this feature. Due to many schedules, please kindly wait. We will consider adding this support later.

<!-- gh-comment-id:2558408772 --> @HubHop commented on GitHub (Dec 22, 2024): Hi @williamium3000, thanks for mentioning this feature. Due to many schedules, please kindly wait. We will consider adding this support later.
Author
Owner

@HubHop commented on GitHub (Dec 23, 2024):

Hi @williamium3000, please let me know if this works for you: https://github.com/deepseek-ai/DeepSeek-VL2/issues/8#issuecomment-2558914207

<!-- gh-comment-id:2558915851 --> @HubHop commented on GitHub (Dec 23, 2024): Hi @williamium3000, please let me know if this works for you: https://github.com/deepseek-ai/DeepSeek-VL2/issues/8#issuecomment-2558914207
Author
Owner

@hzgdeerHo commented on GitHub (Dec 23, 2024):

The same requests for infer with multi-GPU

<!-- gh-comment-id:2559774930 --> @hzgdeerHo commented on GitHub (Dec 23, 2024): The same requests for infer with multi-GPU
Author
Owner

@ccx1997 commented on GitHub (Dec 26, 2024):

It is due to the version of transformers. I do not wanna downgrade the version, so I solved it by do the following modification in def prepare_inputs_for_generation in deepseek_vl2/models/modeling_deepseek.py:

...
        inputs_embeds=None,
        **kwargs,
    ):
        cache_length = 0 ###### newly added line
        if past_key_values is not None:
...
        # if `inputs_embeds` are passed, we only want to use them in the 1st generation step
        if inputs_embeds is not None and (past_key_values is None or cache_length == 0): ##### modified here
            model_inputs = {"inputs_embeds": inputs_embeds}
        else:
            model_inputs = {"input_ids": input_ids}

It worked for the latest transformers==4.46.3

<!-- gh-comment-id:2562709343 --> @ccx1997 commented on GitHub (Dec 26, 2024): It is due to the version of transformers. I do not wanna downgrade the version, so I solved it by do the following modification in `def prepare_inputs_for_generation` in `deepseek_vl2/models/modeling_deepseek.py`: ``` ... inputs_embeds=None, **kwargs, ): cache_length = 0 ###### newly added line if past_key_values is not None: ... # if `inputs_embeds` are passed, we only want to use them in the 1st generation step if inputs_embeds is not None and (past_key_values is None or cache_length == 0): ##### modified here model_inputs = {"inputs_embeds": inputs_embeds} else: model_inputs = {"input_ids": input_ids} ``` It worked for the latest transformers==4.46.3
Author
Owner

@williamium3000 commented on GitHub (Dec 27, 2024):

Thanks. Exactly what I need. @HubHop

<!-- gh-comment-id:2563600785 --> @williamium3000 commented on GitHub (Dec 27, 2024): Thanks. Exactly what I need. @HubHop
Sign in to join this conversation.
No labels
pull-request
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
deepseek-ai/DeepSeek-VL2#1
No description provided.