@tduncan / Nvidia-Optimization-Stack-VLLM Mirror · read-only / commits / e283976f3a
Fork 0

A high-throughput and memory-efficient inference and serving engine for LLMs

Mirrored from https://github.com/tduncan-lum/Nvidia-Optimization-Stack---VLLM.git

Synced Nov 14, 05:30 UTC

[Performance][MM] Building the inverse permutation in O(n) time in Qwen2_5_VisionTransformer (#24443)

Signed-off-by: Junhong <liujunhong11@huawei.com> Co-authored-by: Junhong <liujunhong11@huawei.com>

WeiQing Chen committed Sep 9, 2025 at 15:24 UTC e283976f3a3bacbe10cc22365b149cc11e8c0dff
1 file changed +11 -1
vllm/model_executor/models/qwen2_5_vl.py
+11 -1
@@ -717,6 +717,15 @@ class Qwen2_5_VisionTransformer(nn.Module):
717 seqlens = (cu_seqlens[1:] - cu_seqlens[:-1]).tolist()
718 return max_seqlen, seqlens
719
720 + @staticmethod
721 + def invert_permutation(perm: torch.Tensor) -> torch.Tensor:
722 + # building the inverse permutation in O(n) time
723 + inv = torch.empty_like(perm)
724 + inv[perm] = torch.arange(perm.numel(),
725 + device=perm.device,
726 + dtype=perm.dtype)
727 + return inv
728 +
729 def forward(
730 self,
731 x: torch.Tensor,
@@ -760,6 +769,8 @@ class Qwen2_5_VisionTransformer(nn.Module):
769
770 rotary_pos_emb = torch.cat(rotary_pos_emb)
771 window_index = torch.cat(window_index)
772 + # compute reverse indices
773 + reverse_indices = self.invert_permutation(window_index)
774 cu_window_seqlens = torch.cat(cu_window_seqlens)
775 cu_window_seqlens = torch.unique_consecutive(cu_window_seqlens)
776 cu_seqlens = torch.cat(cu_seqlens)
@@ -813,7 +824,6 @@ class Qwen2_5_VisionTransformer(nn.Module):
824
825 # adapter
826 hidden_states = self.merger(hidden_states)
816 - reverse_indices = torch.argsort(window_index)
827 hidden_states = hidden_states[reverse_indices, :]
828 return hidden_states
829