vlm: streamline vision sdpa reshapes (#34991)

This commit is contained in:
Mick
2026-08-16 19:05:53 +08:00
committed by GitHub
parent 0761d3f3a4
commit 968b355f12
2 changed files with 30 additions and 3 deletions
+5 -3
View File
@@ -401,7 +401,9 @@ class VisionSdpaAttention(nn.Module):
else:
attention_mask = attention_mask.to(device=q.device)
q, k, v = [rearrange(x, "(b s) h d -> b h s d", b=bsz) for x in [q, k, v]]
q = q.reshape(bsz, s, self.num_heads, self.head_size).transpose(1, 2)
k = k.reshape(bsz, s, self.num_kv_heads, self.head_size).transpose(1, 2)
v = v.reshape(bsz, s, self.num_kv_heads, self.head_size).transpose(1, 2)
if self.softmax_in_single_precision:
k = rearrange(k, "b h s d -> b h d s")
@@ -434,7 +436,7 @@ class VisionSdpaAttention(nn.Module):
)
# [b, h, s, head_size] --> [b * s, h, head_size]
output = rearrange(output, "b h s d -> (b s) h d")
output = output.transpose(1, 2).reshape(bsz * s, self.num_heads, self.head_size)
return output
@@ -1477,7 +1479,7 @@ class VisionAttention(nn.Module):
if self.use_qkv_parallel:
# [b * s, h, head_size] --> [b, s, h * head_size]
output = rearrange(output, "(b s) ... h d -> b s ... (h d)", b=bsz)
output = output.reshape(bsz, s, -1)
# [b, s, h * head_size] --> [b, s, h * head_size]
output, _ = self.proj(output)