Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
13 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2238,48 +2238,71 @@ def kernel(
s_tile_live = storage.tile_live.get_tensor(cute.make_layout((1,), stride=(1,)))
s_tile_full = storage.tile_full.get_tensor(cute.make_layout((1,), stride=(1,)))

if tx == 0:
cute.arch.mbarrier_init(mbar_ptr_k, Int32(1))
row_linear = tx
while row_linear < Int32(_PREFILL512_BLOCK_Q):
q_row = q_tile_base + row_linear
s_k_start[row_linear] = (
Int32(k_start[q_row]) if q_row < valid_q_rows else Int32(0)
)
s_k_end[row_linear] = (
Int32(k_end[q_row]) if q_row < valid_q_rows else Int32(0)
)
row_linear += Int32(_PREFILL512_THREADS_PER_CTA)
cute.arch.sync_threads()

if warp_idx == Int32(0):
if cutlass.const_expr(TILED_OUTPUT):
tile_k_end = k_tile_base + Int32(_PREFILL512_BLOCK_K)
if tile_k_end > k_total_rows:
tile_k_end = k_total_rows
row_start = Int32(s_k_start[tx])
row_end = Int32(s_k_end[tx])
physical_tile_full = tile_k_end == k_tile_base + Int32(_PREFILL512_BLOCK_K)
row_live = (
(row_end > k_tile_base)
& (row_start < tile_k_end)
& (k_tile_base < tile_k_end)
q_row_lane = q_tile_base + lane
row_start_l = (
Int32(k_start[q_row_lane]) if q_row_lane < valid_q_rows else Int32(0)
)
row_full = (
(q_tile_base + tx < valid_q_rows)
& physical_tile_full
& (row_start <= k_tile_base)
& (row_end >= tile_k_end)
row_end_l = (
Int32(k_end[q_row_lane]) if q_row_lane < valid_q_rows else Int32(0)
)
ballot = cute.arch.vote_ballot_sync(Boolean(row_live))
full_ballot = cute.arch.vote_ballot_sync(Boolean(row_full))
if tx == Int32(0):
s_tile_live[Int32(0)] = ballot
s_tile_full[Int32(0)] = (
Int32(1) if full_ballot == Int32(-1) else Int32(0)
row_live_l = (
(row_end_l > k_tile_base)
& (row_start_l < tile_k_end)
& (k_tile_base < tile_k_end)
)
tile_live_mask = cute.arch.vote_ballot_sync(Boolean(row_live_l))
if tile_live_mask != Int32(0):
if tx == 0:
cute.arch.mbarrier_init(mbar_ptr_k, Int32(1))
cute.arch.sync_threads()
else:
if tx == 0:
cute.arch.mbarrier_init(mbar_ptr_k, Int32(1))
row_linear = tx
while row_linear < Int32(_PREFILL512_BLOCK_Q):
q_row = q_tile_base + row_linear
s_k_start[row_linear] = (
Int32(k_start[q_row]) if q_row < valid_q_rows else Int32(0)
)
cute.arch.sync_threads()
s_k_end[row_linear] = (
Int32(k_end[q_row]) if q_row < valid_q_rows else Int32(0)
)
row_linear += Int32(_PREFILL512_THREADS_PER_CTA)
cute.arch.sync_threads()

if s_tile_live[Int32(0)] != Int32(0):
if warp_idx == Int32(0):
tile_k_end = k_tile_base + Int32(_PREFILL512_BLOCK_K)
if tile_k_end > k_total_rows:
tile_k_end = k_total_rows
row_start = Int32(s_k_start[tx])
row_end = Int32(s_k_end[tx])
physical_tile_full = tile_k_end == k_tile_base + Int32(_PREFILL512_BLOCK_K)
row_live = (
(row_end > k_tile_base)
& (row_start < tile_k_end)
& (k_tile_base < tile_k_end)
)
row_full = (
(q_tile_base + tx < valid_q_rows)
& physical_tile_full
& (row_start <= k_tile_base)
& (row_end >= tile_k_end)
)
ballot = cute.arch.vote_ballot_sync(Boolean(row_live))
full_ballot = cute.arch.vote_ballot_sync(Boolean(row_full))
if tx == Int32(0):
s_tile_live[Int32(0)] = ballot
s_tile_full[Int32(0)] = (
Int32(1) if full_ballot == Int32(-1) else Int32(0)
)
cute.arch.sync_threads()
tile_live_mask = s_tile_live[Int32(0)]

if tile_live_mask != Int32(0):
q_smem_base = k_perm_base_addr + Int32(
_PREFILL512_BLOCK_K * _INDEX_HEAD_DIM
)
Expand Down Expand Up @@ -3190,7 +3213,7 @@ def run_contiguous_logits_kernel(
)
compile_spec = KernelCompileSpec.from_facts(
"attention.indexer.contiguous_logits",
3,
4,
*facts,
)
if launcher is not None:
Expand Down
157 changes: 90 additions & 67 deletions flashinfer/experimental/b12x/attention/dsa_indexer/paged.py
Original file line number Diff line number Diff line change
Expand Up @@ -241,49 +241,6 @@ def _gather_shared_paged_supertile_kernel(
token_offsets = pid * block_tokens + tl.arange(0, block_tokens)
token_mask = token_offsets < supertile_tokens

page_cols = source_page_offset + token_offsets // page_size
slot_offsets = token_offsets % page_size
page_ids = tl.load(
real_page_table + page_cols,
mask=token_mask & (page_cols < page_table_width),
other=-1,
)
valid_tokens = token_mask & (page_ids >= 0)
# Packed multi-group allocations can span more than 4 GiB. Keep address
# math 64-bit even though the logical page IDs themselves are int32.
page_byte_offsets = page_ids.to(tl.int64) * cache_row_stride_bytes

byte_offsets = tl.arange(0, index_head_dim)
k_bytes = tl.load(
index_k_cache
+ page_byte_offsets[:, None]
+ slot_offsets[:, None] * index_head_dim
+ byte_offsets[None, :],
mask=valid_tokens[:, None],
other=0,
)
tl.store(
k_quant_out + token_offsets[:, None] * index_head_dim + byte_offsets[None, :],
k_bytes,
mask=token_mask[:, None],
)

scale_byte_offsets = tl.arange(0, 4)
scale_bytes = tl.load(
index_k_cache
+ page_byte_offsets[:, None]
+ cache_data_bytes
+ slot_offsets[:, None] * 4
+ scale_byte_offsets[None, :],
mask=valid_tokens[:, None],
other=0,
)
tl.store(
k_scale_bytes_out + token_offsets[:, None] * 4 + scale_byte_offsets[None, :],
scale_bytes,
mask=token_mask[:, None],
)

row_offsets = token_offsets
row_mask = row_offsets < q_rows
row_lengths = tl.load(seqlens_per_query + row_offsets, mask=row_mask, other=0)
Expand All @@ -294,6 +251,69 @@ def _gather_shared_paged_supertile_kernel(
)
tl.store(k_end_out + row_offsets, local_ends, mask=row_mask)

# Skip gathering KV pages for 512-token K-tiles past every query row's live
# sequence length. Scanning seqlens from the tail exits on the first load
# for causal active tiles and eliminates all DRAM traffic on empty supertiles.
tile_start_global = (
source_page_offset * page_size + ((pid * block_tokens) // 512) * 512
)
any_live = False
r_base = ((q_rows - 1) // block_tokens) * block_tokens
while (r_base >= 0) and (not any_live):
r_offs = r_base + tl.arange(0, block_tokens)
r_lens = tl.load(seqlens_per_query + r_offs, mask=r_offs < q_rows, other=0)
if tl.max(r_lens, axis=0) > tile_start_global:
any_live = True
r_base -= block_tokens

if any_live:
page_cols = source_page_offset + token_offsets // page_size
slot_offsets = token_offsets % page_size
page_ids = tl.load(
real_page_table + page_cols,
mask=token_mask & (page_cols < page_table_width),
other=-1,
)
valid_tokens = token_mask & (page_ids >= 0)
# Packed multi-group allocations can span more than 4 GiB. Keep address
# math 64-bit even though the logical page IDs themselves are int32.
page_byte_offsets = page_ids.to(tl.int64) * cache_row_stride_bytes

byte_offsets = tl.arange(0, index_head_dim)
k_bytes = tl.load(
index_k_cache
+ page_byte_offsets[:, None]
+ slot_offsets[:, None] * index_head_dim
+ byte_offsets[None, :],
mask=valid_tokens[:, None],
other=0,
)
tl.store(
k_quant_out
+ token_offsets[:, None] * index_head_dim
+ byte_offsets[None, :],
k_bytes,
mask=token_mask[:, None],
)

scale_byte_offsets = tl.arange(0, 4)
scale_bytes = tl.load(
index_k_cache
+ page_byte_offsets[:, None]
+ cache_data_bytes
+ slot_offsets[:, None] * 4
+ scale_byte_offsets[None, :],
mask=valid_tokens[:, None],
other=0,
)
tl.store(
k_scale_bytes_out
+ token_offsets[:, None] * 4
+ scale_byte_offsets[None, :],
scale_bytes,
mask=token_mask[:, None],
)


@dataclass(frozen=True)
class IndexerPagedMetadata:
Expand Down Expand Up @@ -1131,31 +1151,33 @@ def index_topk_fp8(

is_first = chunk_idx == 0
is_last = chunk_idx == num_chunks - 1
in_place_carry = False
if carry_buf_values is not None:
carry_values = (
carry_buf_values.select(0, (chunk_idx - 1) % 2)
.narrow(0, 0, q_rows)
.narrow(1, 0, topk)
)
carry_indices = (
carry_buf_indices.select(0, (chunk_idx - 1) % 2)
.narrow(0, 0, q_rows)
.narrow(1, 0, topk)
)
out_values = (
final_values
if is_last
else carry_buf_values.select(0, chunk_idx % 2)
.narrow(0, 0, q_rows)
.narrow(1, 0, topk)
)
out_indices = (
final_raw_indices
if is_last
else carry_buf_indices.select(0, chunk_idx % 2)
.narrow(0, 0, q_rows)
.narrow(1, 0, topk)
)
if not output_physical_slots:
# Fold in-place directly on (final_values, final_raw_indices):
# chunk 0 writes them, and subsequent empty chunks return
# immediately with zero global traffic.
carry_values = None if is_first else final_values
carry_indices = None if is_first else final_raw_indices
out_values = final_values
out_indices = final_raw_indices
in_place_carry = not is_first
else:
carry_v0 = (
carry_buf_values.select(0, 0)
.narrow(0, 0, q_rows)
.narrow(1, 0, topk)
)
carry_i0 = (
carry_buf_indices.select(0, 0)
.narrow(0, 0, q_rows)
.narrow(1, 0, topk)
)
carry_values = None if is_first else carry_v0
carry_indices = None if is_first else carry_i0
out_values = final_values if is_last else carry_v0
out_indices = final_raw_indices if is_last else carry_i0
in_place_carry = (not is_first) and (not is_last)
else:
# Single chunk: is_first folds nothing and writes straight to the output.
carry_values = None
Expand Down Expand Up @@ -1224,6 +1246,7 @@ def index_topk_fp8(
),
output_page_size=page_size,
write_values=not is_last or write_final_values,
in_place_carry=in_place_carry,
launcher=None if launchers is None else launchers["tiled"],
launcher_sink=launcher_sink,
)
Expand Down
Loading
Loading