[Bugfix][MiMo] Apply vision attention sinks in the window attention path (#49815)
Signed-off-by: almogtavor <almogtavor@gmail.com>
This commit is contained in:
@@ -193,7 +193,7 @@ class MiMoVisionAttention(nn.Module):
|
||||
# Rotary embeddings applied separately to Q and K
|
||||
self.apply_rotary_emb = ApplyRotaryEmb(enforce_enable=True)
|
||||
|
||||
# Sink attention weights (loaded but not used in vLLM flash_attn)
|
||||
# Per-head sink logits, applied in the window attention path.
|
||||
# The checkpoint stores these only for non-full-attention blocks
|
||||
self.use_sink = use_sink
|
||||
if use_sink:
|
||||
@@ -214,21 +214,38 @@ class MiMoVisionAttention(nn.Module):
|
||||
cu_seqlens: torch.Tensor,
|
||||
max_seqlen: torch.Tensor,
|
||||
) -> torch.Tensor:
|
||||
"""Window attention via flash_attn_varlen_func with window_size."""
|
||||
from vllm.vllm_flash_attn import flash_attn_varlen_func
|
||||
"""Window attention with the per-head sink applied to key 0.
|
||||
|
||||
The reference adds ``sinks[h]`` to the logit of each sequence's first
|
||||
key, which the Triton prefill kernel supports directly, so the softmax
|
||||
normalizes over the biased scores in one pass.
|
||||
"""
|
||||
from vllm.v1.attention.ops.triton_prefill_attention import (
|
||||
context_attention_fwd,
|
||||
)
|
||||
|
||||
w = self.visual_token_window_size
|
||||
output = flash_attn_varlen_func(
|
||||
output = torch.empty_like(q)
|
||||
head_start = self.tp_rank * self.num_heads_per_partition
|
||||
sinks = (
|
||||
self.sinks[head_start : head_start + self.num_heads_per_partition]
|
||||
if self.sinks is not None
|
||||
else None
|
||||
)
|
||||
context_attention_fwd(
|
||||
q,
|
||||
k,
|
||||
v,
|
||||
cu_seqlens_q=cu_seqlens,
|
||||
cu_seqlens_k=cu_seqlens,
|
||||
max_seqlen_q=max_seqlen,
|
||||
max_seqlen_k=max_seqlen,
|
||||
output,
|
||||
b_start_loc=cu_seqlens[:-1],
|
||||
b_seq_len=cu_seqlens[1:] - cu_seqlens[:-1],
|
||||
max_input_len=max_seqlen,
|
||||
is_causal=False,
|
||||
softmax_scale=self.scale,
|
||||
causal=False,
|
||||
window_size=[w, w],
|
||||
sliding_window_q=w,
|
||||
sliding_window_k=w,
|
||||
sinks=sinks,
|
||||
sinks_bias_key0=True,
|
||||
)
|
||||
return output
|
||||
|
||||
|
||||
@@ -58,6 +58,7 @@ def _fwd_kernel(
|
||||
IS_CAUSAL: tl.constexpr,
|
||||
SLIDING_WINDOW_Q: tl.constexpr,
|
||||
SLIDING_WINDOW_K: tl.constexpr,
|
||||
SINKS_BIAS_KEY0: tl.constexpr,
|
||||
USE_SINKS: tl.constexpr,
|
||||
Lk: tl.constexpr,
|
||||
):
|
||||
@@ -98,8 +99,15 @@ def _fwd_kernel(
|
||||
# initialize pointer to m and l
|
||||
if USE_SINKS:
|
||||
sink = tl.load(Sinks + cur_head) * 1.4426950408889634
|
||||
m_i = tl.full([BLOCK_M], sink, dtype=tl.float32)
|
||||
l_i = tl.full([BLOCK_M], 1.0, dtype=tl.float32)
|
||||
if SINKS_BIAS_KEY0:
|
||||
# Sinks bias the logit of key 0, so the softmax starts empty and
|
||||
# normalizes over the biased scores as usual.
|
||||
m_i = tl.zeros([BLOCK_M], dtype=tl.float32) - float("inf")
|
||||
l_i = tl.zeros([BLOCK_M], dtype=tl.float32)
|
||||
else:
|
||||
# Sinks are a null logit that only inflates the denominator.
|
||||
m_i = tl.full([BLOCK_M], sink, dtype=tl.float32)
|
||||
l_i = tl.full([BLOCK_M], 1.0, dtype=tl.float32)
|
||||
else:
|
||||
m_i = tl.zeros([BLOCK_M], dtype=tl.float32) - float("inf")
|
||||
l_i = tl.zeros([BLOCK_M], dtype=tl.float32)
|
||||
@@ -160,6 +168,8 @@ def _fwd_kernel(
|
||||
|
||||
qk = tl.dot(q, k)
|
||||
qk = tl.where(mask, qk * sm_scale, -1.0e8)
|
||||
if USE_SINKS and SINKS_BIAS_KEY0:
|
||||
qk = tl.where(mask & (pos_k == 0), qk + sink, qk)
|
||||
m_ij = tl.maximum(m_i, tl.max(qk, 1))
|
||||
qk -= m_ij[:, None]
|
||||
p = tl.math.exp2(qk)
|
||||
@@ -217,6 +227,7 @@ def context_attention_fwd(
|
||||
sliding_window_q: int | None = None,
|
||||
sliding_window_k: int | None = None,
|
||||
sinks: torch.Tensor | None = None,
|
||||
sinks_bias_key0: bool = False,
|
||||
):
|
||||
"""
|
||||
q, k, v: [b * s, head, head_dim]
|
||||
@@ -268,6 +279,7 @@ def context_attention_fwd(
|
||||
SLIDING_WINDOW_Q=sliding_window_q,
|
||||
SLIDING_WINDOW_K=sliding_window_k,
|
||||
USE_SINKS=sinks is not None,
|
||||
SINKS_BIAS_KEY0=sinks_bias_key0,
|
||||
num_warps=num_warps,
|
||||
num_stages=1,
|
||||
Lk=Lk,
|
||||
|
||||
Reference in New Issue
Block a user