diff --git a/python/sglang/kernels/ops/speculative/dspark/dspark_verify_window.py b/python/sglang/kernels/ops/speculative/dspark/dspark_verify_window.py index f733ba2bdf..ed475e657b 100644 --- a/python/sglang/kernels/ops/speculative/dspark/dspark_verify_window.py +++ b/python/sglang/kernels/ops/speculative/dspark/dspark_verify_window.py @@ -702,32 +702,20 @@ def build_commit_inject_layout( commit_lens: torch.Tensor, stride: int, ) -> CommitInjectLayoutResult: - from sglang.kernels.ops.speculative.cache_locs import ( - assign_extend_cache_locs_func, - ) - bs = req_pool_indices.shape[0] device = req_pool_indices.device - positions_2d = prefix_lens.unsqueeze(1) + block_pos_offsets[:stride] positions = positions_2d.reshape(-1).to(dtype=torch.int64) - - cache_loc = assign_extend_cache_locs_func( - req_pool_indices=req_pool_indices, - req_to_token=req_to_token, - start_offset=prefix_lens, - end_offset=prefix_lens + stride, - batch_size=bs, - draft_token_num=stride, - device=device, - ).to(dtype=torch.int64) - swa_loc = full_to_swa_mapping[cache_loc].to(torch.int32) - col = torch.arange(stride, device=device).view(1, -1) - committed = (col < commit_lens.to(torch.long).view(-1, 1)).reshape(-1) - swa_loc = torch.where(committed, swa_loc, torch.full_like(swa_loc, -1)) - - return CommitInjectLayoutResult(swa_loc=swa_loc, positions=positions) + committed = col < commit_lens.to(torch.long).view(-1, 1) + req_rows = req_pool_indices.to(torch.long).view(-1, 1).expand(bs, stride) + rows = torch.where(committed, req_rows, 0) + cols = torch.where(committed, positions_2d.long(), 0) + cache_loc = torch.where(committed, req_to_token[rows, cols].long(), 0) + swa_loc = torch.where(committed, full_to_swa_mapping[cache_loc], -1) + return CommitInjectLayoutResult( + swa_loc=swa_loc.to(torch.int32).reshape(-1), positions=positions + ) @triton.jit @@ -753,13 +741,14 @@ def _commit_inject_layout_kernel( prefix = tl.load(prefix_lens_ptr + r, mask=mask, other=0).to(tl.int64) pos_off = tl.load(block_pos_offsets_ptr + c, mask=mask, other=0).to(tl.int64) rp = tl.load(req_pool_ptr + r, mask=mask, other=0).to(tl.int64) - full_loc = tl.load( - req_to_token_ptr + rp * rt_stride + prefix + pos_off, mask=mask, other=0 - ).to(tl.int64) - swa = tl.load(full_to_swa_ptr + full_loc, mask=mask, other=-1).to(tl.int32) - commit_len = tl.load(commit_lens_ptr + r, mask=mask, other=0).to(tl.int64) - swa = tl.where(c.to(tl.int64) < commit_len, swa, -1) + committed = mask & (c.to(tl.int64) < commit_len) + full_loc = tl.load( + req_to_token_ptr + rp * rt_stride + prefix + pos_off, + mask=committed, + other=0, + ).to(tl.int64) + swa = tl.load(full_to_swa_ptr + full_loc, mask=committed, other=-1).to(tl.int32) tl.store(swa_loc_ptr + offs, swa, mask=mask) tl.store(positions_ptr + offs, prefix + pos_off, mask=mask) diff --git a/test/registered/spec/dspark/test_dspark_kernel_parity.py b/test/registered/spec/dspark/test_dspark_kernel_parity.py index ed583d08af..7ab073fa2a 100644 --- a/test/registered/spec/dspark/test_dspark_kernel_parity.py +++ b/test/registered/spec/dspark/test_dspark_kernel_parity.py @@ -209,12 +209,15 @@ def _case_commit_inject_layout(tc): ) tc._parity(dspark_verify_window.BuildCommitInjectLayout, **kw) # commit_len edges: 0 masks the whole row to -1, stride keeps it all. + # The uncommitted row holds out-of-range entries that must not be loaded. + rp0, p0 = int(kw["req_pool_indices"][0]), int(kw["prefix_lens"][0]) + kw["req_to_token"][rp0, p0 : p0 + stride] = 1 << 40 kw.update( req_pool_indices=kw["req_pool_indices"][:2], prefix_lens=kw["prefix_lens"][:2], commit_lens=torch.tensor([0, stride], device=DEVICE, dtype=torch.int32), ) - edge = dspark_verify_window.BuildCommitInjectLayout.triton(**kw) + edge, _ = tc._parity(dspark_verify_window.BuildCommitInjectLayout, **kw) swa_2d = edge.swa_loc.view(2, stride) tc.assertTrue(bool((swa_2d[0] == -1).all())) tc.assertTrue(bool((swa_2d[1] >= 0).all()))