[Kernel][Gemma4] Prune Triton sliding-window tiles for multimodal prefixes (#53147)
Signed-off-by: mobicham <hichamb@dropbox.com>
This commit is contained in:
@@ -157,6 +157,11 @@ def compute_tile_loop_bounds(
|
||||
USE_PER_SEQ_CAUSAL: tl.constexpr = False,
|
||||
CHUNK_LOOKBACK: tl.constexpr = -1,
|
||||
CHUNK_SIZE: tl.constexpr = -1,
|
||||
USE_R_SWA: tl.constexpr = False,
|
||||
MM_PREFIX_CLAMP_SW: tl.constexpr = False,
|
||||
MAX_MM_RANGES: tl.constexpr = 0,
|
||||
mm_prefix_range_ptr=None,
|
||||
seq_idx=0,
|
||||
):
|
||||
"""Compute the tile-loop bounds ``(loop_lo, loop_hi)`` and the
|
||||
derived ``max_seq_prefix_len`` used for per-tile masking.
|
||||
@@ -168,8 +173,10 @@ def compute_tile_loop_bounds(
|
||||
mm_prefix is active or non-causal sequences need the full
|
||||
sequence.
|
||||
2. Sliding-window pruning: narrows ``[tile_start, tile_end)`` to
|
||||
only tiles that can contain an allowed key under SWA.
|
||||
For non-causal sequences, the window extends in both directions.
|
||||
only tiles that can contain an allowed key under SWA. For non-causal
|
||||
sequences, the window extends in both directions. For Gemma4's
|
||||
window-clamped multimodal prefix mask, the bounds include the union of
|
||||
the base mask and each image range intersecting the query block.
|
||||
3. 3D scoping: when ``IS_3D`` is True, further narrows to the
|
||||
segment's slice via ``(segm_idx * tiles_per_segment,
|
||||
(segm_idx + 1) * tiles_per_segment)``.
|
||||
@@ -182,7 +189,7 @@ def compute_tile_loop_bounds(
|
||||
+ (BLOCK_M - 1) // num_queries_per_kv
|
||||
+ 1
|
||||
)
|
||||
if USE_MM_PREFIX or USE_PER_SEQ_CAUSAL or (not USE_CAUSAL):
|
||||
if USE_MM_PREFIX or USE_R_SWA or USE_PER_SEQ_CAUSAL or (not USE_CAUSAL):
|
||||
# Read the full sequence but never past seq_len: the causal-style
|
||||
# formula above can overshoot for non-causal sequences, and slots
|
||||
# >= seq_len are unwritten KV (last-block tail) that may hold NaN
|
||||
@@ -198,8 +205,13 @@ def compute_tile_loop_bounds(
|
||||
# Default: keep previous global behavior
|
||||
tile_start = 0
|
||||
tile_end = num_tiles
|
||||
# TODO(Isotr0py): sliding window pruning with image bidirectional mask
|
||||
if SLIDING_WINDOW > 0 and not USE_MM_PREFIX:
|
||||
# Prefix ranges normally override the sliding window, so they require the
|
||||
# complete sequence. Gemma4 instead clamps prefix attention to the left
|
||||
# edge of the sliding window. In that case, the union of possible keys is
|
||||
# bounded by the normal left window and the end of any multimodal range
|
||||
# intersecting this query block.
|
||||
can_prune_sliding = (not USE_R_SWA) and ((not USE_MM_PREFIX) or MM_PREFIX_CLAMP_SW)
|
||||
if SLIDING_WINDOW > 0 and can_prune_sliding:
|
||||
# Query rows covered by this Q-block
|
||||
qpos_lo = q_block_local_idx * BLOCK_Q
|
||||
qpos_hi = tl.minimum(
|
||||
@@ -218,12 +230,40 @@ def compute_tile_loop_bounds(
|
||||
first_allowed_key = q_abs - SLIDING_WINDOW + 1
|
||||
if USE_PER_SEQ_CAUSAL or (not USE_CAUSAL):
|
||||
# Non-causal: keys can be AHEAD of query within the window
|
||||
last_allowed_key = tl.minimum(
|
||||
context_len + qpos_hi + SLIDING_WINDOW - 1,
|
||||
seq_len - 1,
|
||||
)
|
||||
last_allowed_key = context_len + qpos_hi + SLIDING_WINDOW - 1
|
||||
else:
|
||||
last_allowed_key = context_len + qpos_hi
|
||||
if USE_MM_PREFIX and MM_PREFIX_CLAMP_SW:
|
||||
query_abs_lo = context_len + qpos_lo
|
||||
query_abs_hi = context_len + qpos_hi
|
||||
for i in range(MAX_MM_RANGES):
|
||||
range_start = tl.load(
|
||||
mm_prefix_range_ptr + seq_idx * MAX_MM_RANGES * 2 + i * 2
|
||||
)
|
||||
range_end = tl.load(
|
||||
mm_prefix_range_ptr + seq_idx * MAX_MM_RANGES * 2 + i * 2 + 1
|
||||
)
|
||||
intersects_query_block = (
|
||||
(range_start < range_end)
|
||||
& (range_start <= query_abs_hi)
|
||||
& (range_end >= query_abs_lo)
|
||||
)
|
||||
mm_first_allowed_key = tl.maximum(
|
||||
range_start, query_abs_lo - SLIDING_WINDOW + 1
|
||||
)
|
||||
first_allowed_key = tl.minimum(
|
||||
first_allowed_key,
|
||||
tl.where(
|
||||
intersects_query_block,
|
||||
mm_first_allowed_key,
|
||||
first_allowed_key,
|
||||
),
|
||||
)
|
||||
last_allowed_key = tl.maximum(
|
||||
last_allowed_key,
|
||||
tl.where(intersects_query_block, range_end, last_allowed_key),
|
||||
)
|
||||
last_allowed_key = tl.minimum(last_allowed_key, seq_len - 1)
|
||||
# Convert to tile indices and clamp
|
||||
tile_start = tl.maximum(0, first_allowed_key // TILE_SIZE)
|
||||
tile_end = tl.minimum((last_allowed_key // TILE_SIZE) + 1, num_tiles)
|
||||
|
||||
@@ -408,12 +408,17 @@ def kernel_unified_attention(
|
||||
BLOCK_Q,
|
||||
num_queries_per_kv,
|
||||
SLIDING_WINDOW,
|
||||
USE_MM_PREFIX or USE_R_SWA,
|
||||
USE_MM_PREFIX,
|
||||
IS_3D,
|
||||
USE_CAUSAL,
|
||||
USE_PER_SEQ_CAUSAL,
|
||||
CHUNK_LOOKBACK,
|
||||
CHUNK_SIZE,
|
||||
USE_R_SWA,
|
||||
MM_PREFIX_CLAMP_SW,
|
||||
MAX_MM_RANGES,
|
||||
mm_prefix_range_ptr,
|
||||
seq_idx,
|
||||
)
|
||||
|
||||
# iterate through tiles (now limited to the sliding window range)
|
||||
|
||||
Reference in New Issue
Block a user