Skip to content

Commit

Permalink
Only cast cu_seqlens when tracing (huggingface#35016)
Browse files Browse the repository at this point in the history
* Only cast `cu_seqlens` when tracing

* Formatting
  • Loading branch information
xenova authored and BernardZach committed Dec 5, 2024
1 parent 99f031d commit fbe0d1b
Showing 1 changed file with 6 additions and 1 deletion.
7 changes: 6 additions & 1 deletion src/transformers/models/qwen2_vl/modeling_qwen2_vl.py
Original file line number Diff line number Diff line change
Expand Up @@ -1025,7 +1025,12 @@ def forward(self, hidden_states: torch.Tensor, grid_thw: torch.Tensor) -> torch.
rotary_pos_emb = self.rot_pos_emb(grid_thw)

cu_seqlens = torch.repeat_interleave(grid_thw[:, 1] * grid_thw[:, 2], grid_thw[:, 0]).cumsum(
dim=0, dtype=grid_thw.dtype
dim=0,
# Select dtype based on the following factors:
# - FA2 requires that cu_seqlens_q must have dtype int32
# - torch.onnx.export requires that cu_seqlens_q must have same dtype as grid_thw
# See https://github.com/huggingface/transformers/pull/34852 for more information
dtype=grid_thw.dtype if torch.jit.is_tracing() else torch.int32,
)
cu_seqlens = F.pad(cu_seqlens, (1, 0), value=0)

Expand Down

0 comments on commit fbe0d1b

Please sign in to comment.