Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
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
5 changes: 3 additions & 2 deletions python/sglang/kernels/ops/attention/fla/chunk_delta_h.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,8 @@ def chunk_gated_delta_rule_fwd_kernel_h_blockdim64(
ht = ht + i_h * V * K

# load initial state
if USE_INITIAL_STATE:
# Padded rows carry index == -1; skip them (see fused_recurrent).
if USE_INITIAL_STATE and index >= 0:
p_h0_1 = tl.make_block_ptr(h0, (V, K), (K, 1), (i_v * BV, 0), (BV, 64), (1, 0))
b_h1 += tl.load(p_h0_1, boundary_check=(0, 1)).to(tl.float32)
if K > 64:
Expand Down Expand Up @@ -290,7 +291,7 @@ def chunk_gated_delta_rule_fwd_kernel_h_blockdim64(
b_h4 += tl.trans(tl.dot(b_k, b_v))

# epilogue
if INPLACE_UPDATE:
if INPLACE_UPDATE and index >= 0:
p_ht = tl.make_block_ptr(ht, (V, K), (K, 1), (i_v * BV, 0), (BV, 64), (1, 0))
tl.store(p_ht, b_h1.to(p_ht.dtype.element_ty), boundary_check=(0, 1))
if K > 64:
Expand Down
47 changes: 47 additions & 0 deletions test/registered/attention/test_chunk_gated_delta_rule.py
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,53 @@ def test_seq_idx_stress(self):
sequential_indices=True,
)

# ------------------------------------------------------------------
# Padded rows (idle DP ranks under breakable CUDA graph prefill)
# ------------------------------------------------------------------

def test_padded_state_index_is_skipped(self):
"""Rows carrying state index -1 must be skipped, not addressed.

Without the guard the sentinel reaches pointer arithmetic and
addresses before the state pool.
"""
device = get_device()
dtype = torch.bfloat16
B, T_per_seq, H, K, V, pool_size = 4, 64, 4, 128, 128, 8
T = B * T_per_seq
torch.manual_seed(0)

pool_init = (
torch.randn(pool_size, H, V, K, dtype=torch.float32, device=device) * 0.1
)
cu_seqlens = torch.zeros(B + 1, dtype=torch.long, device=device)
cu_seqlens[1:] = (
torch.arange(1, B + 1, dtype=torch.long, device=device) * T_per_seq
)
q = torch.randn(1, T, H, K, dtype=dtype, device=device)
k = torch.randn(1, T, H, K, dtype=dtype, device=device)
v = torch.randn(1, T, H, V, dtype=dtype, device=device)
g = torch.nn.functional.logsigmoid(
torch.randn(1, T, H, dtype=dtype, device=device)
)
beta = torch.sigmoid(torch.randn(1, T, H, dtype=dtype, device=device))

# "all padded" is the idle-DP-rank case that faulted.
for label, indices in (("mixed", [0, -1, 2, -1]), ("all_padded", [-1] * B)):
with self.subTest(label):
cache_indices = torch.tensor(indices, dtype=torch.int32, device=device)
o, pool = self._run_chunk(
pool_init, cache_indices, q, k, v, g, beta, cu_seqlens
)
# Forces a sync: an out-of-bounds access surfaces here.
self.assertTrue(torch.isfinite(o.float()).all())

untouched = [s for s in range(pool_size) if s not in indices]
self.assertTrue(
torch.equal(pool[untouched], pool_init[untouched]),
f"{label}: padded rows wrote into the state pool",
)


if __name__ == "__main__":
unittest.main()
Loading