diff --git a/src/transformers/models/blip_2/modeling_blip_2.py b/src/transformers/models/blip_2/modeling_blip_2.py index 4a5f1a178d5c..7d4e27b042aa 100644 --- a/src/transformers/models/blip_2/modeling_blip_2.py +++ b/src/transformers/models/blip_2/modeling_blip_2.py @@ -602,7 +602,8 @@ def forward( **kwargs, ) - attn_output = attn_output.reshape(*input_shape, -1).contiguous() + # NOTE: KV has a different bsz than Q so we take the broadcasted shapes instead of the input shape + attn_output = attn_output.reshape(*attn_output.shape[:2], -1).contiguous() return attn_output, attn_weights diff --git a/src/transformers/models/instructblip/modeling_instructblip.py b/src/transformers/models/instructblip/modeling_instructblip.py index 8f829a270e45..19f399266e35 100644 --- a/src/transformers/models/instructblip/modeling_instructblip.py +++ b/src/transformers/models/instructblip/modeling_instructblip.py @@ -510,7 +510,8 @@ def forward( **kwargs, ) - attn_output = attn_output.reshape(*input_shape, -1).contiguous() + # NOTE: KV has a different bsz than Q so we take the broadcasted shapes instead of the input shape + attn_output = attn_output.reshape(*attn_output.shape[:2], -1).contiguous() return attn_output, attn_weights diff --git a/src/transformers/models/instructblipvideo/modeling_instructblipvideo.py b/src/transformers/models/instructblipvideo/modeling_instructblipvideo.py index 25a3dee6b973..843653c6539a 100644 --- a/src/transformers/models/instructblipvideo/modeling_instructblipvideo.py +++ b/src/transformers/models/instructblipvideo/modeling_instructblipvideo.py @@ -514,7 +514,8 @@ def forward( **kwargs, ) - attn_output = attn_output.reshape(*input_shape, -1).contiguous() + # NOTE: KV has a different bsz than Q so we take the broadcasted shapes instead of the input shape + attn_output = attn_output.reshape(*attn_output.shape[:2], -1).contiguous() return attn_output, attn_weights