[Bugfix] Support non-uniform page sizes in KVBlockZeroer (#49704)

Signed-off-by: Elvir Crncevic <elvircrn@gmail.com>
Co-authored-by: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
Elvir Crnčević
2026-07-24 13:38:59 -07:00
committed by GitHub
co-authored by Claude Opus 4.6
parent 2279575cd9
commit 972848f276
3 changed files with 92 additions and 31 deletions
+50 -3
View File
@@ -20,9 +20,10 @@ def test_block_ids_are_not_overwritten_while_copy_is_in_flight():
zeroer.device = device
zeroer._meta = (
torch.tensor([storage.data_ptr()], dtype=torch.uint64, device=device),
page_size_el,
page_size_el,
1,
torch.tensor([page_size_el], dtype=torch.int64, device=device),
page_size_el // page_size_el, # max_chunks = 1
page_size_el, # blk_size
1, # n_segs
)
stream = torch.cuda.Stream()
@@ -39,3 +40,49 @@ def test_block_ids_are_not_overwritten_while_copy_is_in_flight():
assert torch.all(storage[1] == 0)
assert torch.all(storage[2] == 0)
assert torch.all(storage[3] == 1)
@pytest.mark.skipif(not torch.cuda.is_available(), reason="CUDA required")
def test_non_uniform_page_sizes():
"""Two segments with different page sizes (e.g. MLA + DSA indexer)."""
device = torch.device("cuda")
num_blocks = 4
page_size_a = 10496 # int32 elements
page_size_b = 2112
storage_a = torch.ones((num_blocks, page_size_a), dtype=torch.int32, device=device)
storage_b = torch.ones((num_blocks, page_size_b), dtype=torch.int32, device=device)
zeroer = KVBlockZeroer.__new__(KVBlockZeroer)
zeroer.device = device
seg_page_sizes = [page_size_a, page_size_b]
max_ps = max(seg_page_sizes)
def largest_power_of_2_divisor(n):
return n & -n
blk_size = min(min(largest_power_of_2_divisor(ps) for ps in seg_page_sizes), 1024)
zeroer._meta = (
torch.tensor(
[storage_a.data_ptr(), storage_b.data_ptr()],
dtype=torch.uint64,
device=device,
),
torch.tensor(seg_page_sizes, dtype=torch.int64, device=device),
max_ps // blk_size,
blk_size,
2,
)
stream = torch.cuda.Stream()
with torch.cuda.stream(stream):
zeroer.zero_block_ids([1, 2])
stream.synchronize()
for storage in (storage_a, storage_b):
assert torch.all(storage[0] == 1)
assert torch.all(storage[1] == 0)
assert torch.all(storage[2] == 0)
assert torch.all(storage[3] == 1)