[Model] Add block-local attention and YaRN for local layers to Gemma3 (#39823)
Signed-off-by: Philip Monk <169196560+philip-essential@users.noreply.github.com>
This commit is contained in:
@@ -458,6 +458,7 @@ class TritonAttentionImpl(AttentionImpl):
|
||||
kv_sharing_target_layer_name: int | None = None,
|
||||
sinks: torch.Tensor | None = None,
|
||||
use_alibi_sqrt: bool = False,
|
||||
chunk_lookback: int = -1,
|
||||
) -> None:
|
||||
self.num_heads = num_heads
|
||||
self.head_size = head_size
|
||||
@@ -492,6 +493,7 @@ class TritonAttentionImpl(AttentionImpl):
|
||||
f"num_heads: {num_heads}."
|
||||
)
|
||||
self.use_alibi_sqrt = use_alibi_sqrt
|
||||
self.chunk_lookback = chunk_lookback
|
||||
self.supports_quant_query_input = current_platform.is_cuda()
|
||||
|
||||
self._kv_quant_mode = get_kv_quant_mode(kv_cache_dtype)
|
||||
@@ -631,6 +633,7 @@ class TritonAttentionImpl(AttentionImpl):
|
||||
kv_quant_mode=self._kv_quant_mode,
|
||||
k_scale_cache=k_scale_cache,
|
||||
v_scale_cache=v_scale_cache,
|
||||
chunk_lookback=self.chunk_lookback,
|
||||
)
|
||||
|
||||
return output
|
||||
|
||||
@@ -177,6 +177,8 @@ def kernel_unified_attention_2d(
|
||||
stride_vs_blk=0,
|
||||
stride_vs_slot=0,
|
||||
stride_vs_head=0,
|
||||
CHUNK_LOOKBACK: tl.constexpr = -1,
|
||||
CHUNK_SIZE: tl.constexpr = -1,
|
||||
):
|
||||
q_block_global_idx = tl.program_id(0)
|
||||
kv_head_idx = tl.program_id(1)
|
||||
@@ -293,7 +295,11 @@ def kernel_unified_attention_2d(
|
||||
# where q_abs = context_len + q
|
||||
# The union of allowed key positions for this Q-block is:
|
||||
# [context_len + qpos_lo - SLIDING_WINDOW + 1, context_len + qpos_hi]
|
||||
first_allowed_key = context_len + qpos_lo - SLIDING_WINDOW + 1
|
||||
q_abs = context_len + qpos_lo
|
||||
if CHUNK_LOOKBACK > -1:
|
||||
first_allowed_key = ((q_abs // CHUNK_SIZE) - CHUNK_LOOKBACK) * CHUNK_SIZE
|
||||
else:
|
||||
first_allowed_key = q_abs - SLIDING_WINDOW + 1
|
||||
last_allowed_key = context_len + qpos_hi
|
||||
# Convert to tile indices and clamp
|
||||
tile_start = tl.maximum(0, first_allowed_key // TILE_SIZE)
|
||||
@@ -370,9 +376,19 @@ def kernel_unified_attention_2d(
|
||||
query_abs_pos = context_len + query_pos[:, None]
|
||||
seq_mask = seq_offset[None, :] <= query_abs_pos
|
||||
|
||||
# Apply sliding window to base mask BEFORE mm_prefix OR.
|
||||
# Order must match FlexAttention: (causal AND sliding_window) OR mm_prefix
|
||||
if SLIDING_WINDOW > 0:
|
||||
# Apply sliding window / chunked attention to base mask
|
||||
# BEFORE mm_prefix OR.
|
||||
# Order must match FlexAttention:
|
||||
# (causal AND sliding_window) OR mm_prefix
|
||||
if CHUNK_LOOKBACK > -1:
|
||||
seq_mask = seq_mask & (
|
||||
(
|
||||
(context_len + query_pos[:, None]) // CHUNK_SIZE
|
||||
- (seq_offset[None, :] // CHUNK_SIZE)
|
||||
)
|
||||
<= CHUNK_LOOKBACK
|
||||
)
|
||||
elif SLIDING_WINDOW > 0:
|
||||
seq_mask = seq_mask & ((query_abs_pos - seq_offset) < SLIDING_WINDOW)
|
||||
|
||||
# PrefixLM: extend mask with bidirectional ranges for multimodal tokens.
|
||||
@@ -559,6 +575,8 @@ def kernel_unified_attention_3d(
|
||||
stride_vs_blk=0,
|
||||
stride_vs_slot=0,
|
||||
stride_vs_head=0,
|
||||
CHUNK_LOOKBACK: tl.constexpr = -1,
|
||||
CHUNK_SIZE: tl.constexpr = -1,
|
||||
):
|
||||
q_block_global_idx = tl.program_id(0)
|
||||
kv_head_idx = tl.program_id(1)
|
||||
@@ -681,7 +699,11 @@ def kernel_unified_attention_3d(
|
||||
# where q_abs = context_len + q
|
||||
# The union of allowed key positions for this Q-block is:
|
||||
# [context_len + qpos_lo - SLIDING_WINDOW + 1, context_len + qpos_hi]
|
||||
first_allowed_key = context_len + qpos_lo - SLIDING_WINDOW + 1
|
||||
q_abs = context_len + qpos_lo
|
||||
if CHUNK_LOOKBACK > -1:
|
||||
first_allowed_key = ((q_abs // CHUNK_SIZE) - CHUNK_LOOKBACK) * CHUNK_SIZE
|
||||
else:
|
||||
first_allowed_key = q_abs - SLIDING_WINDOW + 1
|
||||
last_allowed_key = context_len + qpos_hi
|
||||
# Convert to tile indices and clamp
|
||||
tile_start = tl.maximum(0, first_allowed_key // TILE_SIZE)
|
||||
@@ -761,9 +783,19 @@ def kernel_unified_attention_3d(
|
||||
query_abs_pos = context_len + query_pos[:, None]
|
||||
seq_mask = seq_offset[None, :] <= query_abs_pos
|
||||
|
||||
# Apply sliding window to base mask BEFORE mm_prefix OR.
|
||||
# Order must match FlexAttention: (causal AND sliding_window) OR mm_prefix
|
||||
if SLIDING_WINDOW > 0:
|
||||
# Apply sliding window / chunked attention to base mask
|
||||
# BEFORE mm_prefix OR.
|
||||
# Order must match FlexAttention:
|
||||
# (causal AND sliding_window) OR mm_prefix
|
||||
if CHUNK_LOOKBACK > -1:
|
||||
seq_mask = seq_mask & (
|
||||
(
|
||||
(context_len + query_pos[:, None]) // CHUNK_SIZE
|
||||
- (seq_offset[None, :] // CHUNK_SIZE)
|
||||
)
|
||||
<= CHUNK_LOOKBACK
|
||||
)
|
||||
elif SLIDING_WINDOW > 0:
|
||||
seq_mask = seq_mask & ((query_abs_pos - seq_offset) < SLIDING_WINDOW)
|
||||
|
||||
# PrefixLM: extend mask with bidirectional ranges for multimodal tokens.
|
||||
@@ -1046,6 +1078,8 @@ def unified_attention(
|
||||
kv_quant_mode: KVQuantMode = KVQuantMode.NONE,
|
||||
k_scale_cache=None, # [num_blocks, block_size, num_kv_heads] float32
|
||||
v_scale_cache=None, # [num_blocks, block_size, num_kv_heads] float32
|
||||
# Chunked attention: restrict attention to aligned blocks with lookback.
|
||||
chunk_lookback=-1,
|
||||
):
|
||||
assert causal, "Only causal attention is supported"
|
||||
assert q_descale is None, "Q scales not supported"
|
||||
@@ -1093,6 +1127,15 @@ def unified_attention(
|
||||
# Tile sizes for prefill and decode. Gemma3 models use optimized values.
|
||||
# Note: tile size must be at least 32 for fp8 (element_size == 1).
|
||||
sliding_window_val = 1 + window_size[0] if window_size[0] >= 0 else 0
|
||||
|
||||
# Compute chunked block size from sliding window if needed.
|
||||
chunk_size = -1
|
||||
if sliding_window_val > 0 and chunk_lookback > -1:
|
||||
chunk_size = sliding_window_val // (chunk_lookback + 1)
|
||||
assert chunk_size > 0, "sliding_window must be > chunk_lookback+1"
|
||||
elif sliding_window_val <= 0:
|
||||
chunk_lookback = -1
|
||||
|
||||
TILE_SIZE_PREFILL = _get_tile_size(
|
||||
head_size,
|
||||
sliding_window_val,
|
||||
@@ -1184,6 +1227,8 @@ def unified_attention(
|
||||
stride_vs_blk=v_scale_cache.stride(0) if v_scale_cache is not None else 0,
|
||||
stride_vs_slot=v_scale_cache.stride(1) if v_scale_cache is not None else 0,
|
||||
stride_vs_head=v_scale_cache.stride(2) if v_scale_cache is not None else 0,
|
||||
CHUNK_LOOKBACK=chunk_lookback,
|
||||
CHUNK_SIZE=chunk_size,
|
||||
)
|
||||
else:
|
||||
kernel_unified_attention_3d[
|
||||
@@ -1245,6 +1290,8 @@ def unified_attention(
|
||||
stride_vs_blk=v_scale_cache.stride(0) if v_scale_cache is not None else 0,
|
||||
stride_vs_slot=v_scale_cache.stride(1) if v_scale_cache is not None else 0,
|
||||
stride_vs_head=v_scale_cache.stride(2) if v_scale_cache is not None else 0,
|
||||
CHUNK_LOOKBACK=chunk_lookback,
|
||||
CHUNK_SIZE=chunk_size,
|
||||
)
|
||||
reduce_segments[(q.shape[0], num_query_heads)](
|
||||
output_ptr=out,
|
||||
|
||||
Reference in New Issue
Block a user