Skip to content

Commit 1f86d56

Browse files
Isotr0pylulmer
authored andcommitted
[Bugfix] Fix qwen2.5-vl overflow issue (vllm-project#13968)
Signed-off-by: Isotr0py <[email protected]> Signed-off-by: Louis Ulmer <[email protected]>
1 parent 536ed00 commit 1f86d56

File tree

4 files changed

+22
-15
lines changed

4 files changed

+22
-15
lines changed

vllm/model_executor/models/minicpmo.py

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@
4747
MiniCPMVMultiModalDataParser,
4848
MiniCPMVMultiModalProcessor, MiniCPMVProcessingInfo,
4949
_minicpmv_field_config)
50-
from .utils import AutoWeightsLoader, maybe_prefix
50+
from .utils import AutoWeightsLoader, cast_overflow_tensors, maybe_prefix
5151

5252
CPU_DEVICE = torch.device("cpu")
5353

@@ -469,13 +469,8 @@ def forward(
469469
training=self.training)
470470
hidden_states = residual + hidden_states
471471

472-
if hidden_states.dtype == torch.float16 and (
473-
torch.isinf(hidden_states).any()
474-
or torch.isnan(hidden_states).any()):
475-
clamp_value = torch.finfo(hidden_states.dtype).max - 1000
476-
hidden_states = torch.clamp(hidden_states,
477-
min=-clamp_value,
478-
max=clamp_value)
472+
if hidden_states.dtype == torch.float16:
473+
hidden_states = cast_overflow_tensors(hidden_states)
479474

480475
outputs = (hidden_states, )
481476

vllm/model_executor/models/qwen2_5_vl.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@
6363
from .qwen2_vl import Qwen2VLDummyInputsBuilder as Qwen2_5_VLDummyInputsBuilder
6464
from .qwen2_vl import (Qwen2VLMultiModalProcessor, Qwen2VLProcessingInfo,
6565
apply_rotary_pos_emb_vision)
66-
from .utils import (AutoWeightsLoader, WeightsMapper,
66+
from .utils import (AutoWeightsLoader, WeightsMapper, cast_overflow_tensors,
6767
init_vllm_registered_model, maybe_prefix,
6868
merge_multimodal_embeddings)
6969
from .vision import get_vit_attn_backend
@@ -641,6 +641,11 @@ def forward(
641641
cu_seqlens=cu_seqlens_now,
642642
rotary_pos_emb=rotary_pos_emb)
643643

644+
# For Qwen2.5-VL-3B, float16 will overflow at last block
645+
# for long visual tokens sequences.
646+
if hidden_states.dtype == torch.float16:
647+
hidden_states = cast_overflow_tensors(hidden_states)
648+
644649
# adapter
645650
hidden_states = self.merger(hidden_states)
646651
reverse_indices = torch.argsort(window_index)

vllm/model_executor/models/utils.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -641,3 +641,13 @@ def extract_layer_index(layer_name: str) -> int:
641641
assert len(int_vals) == 1, (f"layer name {layer_name} should"
642642
" only contain one integer")
643643
return int_vals[0]
644+
645+
646+
def cast_overflow_tensors(
647+
tensors: torch.Tensor,
648+
offset: float = 1000,
649+
) -> torch.Tensor:
650+
if tensors.isinf().any() or tensors.isnan().any():
651+
clamp_value = torch.finfo(tensors.dtype).max - offset
652+
tensors = torch.clamp(tensors, min=-clamp_value, max=clamp_value)
653+
return tensors

vllm/model_executor/models/whisper.py

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,8 @@
3535
from vllm.multimodal.profiling import BaseDummyInputsBuilder, ProcessorInputs
3636

3737
from .interfaces import SupportsMultiModal, SupportsTranscription
38-
from .utils import AutoWeightsLoader, WeightsMapper, make_layers
38+
from .utils import (AutoWeightsLoader, WeightsMapper, cast_overflow_tensors,
39+
make_layers)
3940

4041
logger = init_logger(__name__)
4142

@@ -285,11 +286,7 @@ def forward(
285286
hidden_states = self.mlp(hidden_states)
286287
hidden_states = residual + hidden_states
287288

288-
if hidden_states.isinf().any() or hidden_states.isnan().any():
289-
clamp_value = torch.finfo(hidden_states.dtype).max - 1000
290-
hidden_states = torch.clamp(hidden_states,
291-
min=-clamp_value,
292-
max=clamp_value)
289+
hidden_states = cast_overflow_tensors(hidden_states)
293290

294291
return hidden_states
295292

0 commit comments

Comments
 (0)