Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
f00e943
[HiSparse] Support Kimi-K3.1's hybrid KDA + DSA layout
MatthewBonanni Nov 12, 2026
ece197b
[Kimi K3.1] Derive indexer write slots without the group slot mapping
MatthewBonanni Nov 12, 2026
a13816c
[HiSparse] Keep the packed block only for self-addressed indexers
MatthewBonanni Nov 12, 2026
7b8a238
[Bugfix][KV Connector] Pass attention metadata to the connector on pi…
MatthewBonanni Nov 12, 2026
e089867
Merge upstream/main into hisparse-kimi-k31
MatthewBonanni Nov 13, 2026
375136a
[HiSparse] Spread indexer pages only when every indexer layer fits
MatthewBonanni Nov 13, 2026
683228a
Merge remote-tracking branch 'upstream/main' into hisparse-kimi-k31
MatthewBonanni Nov 13, 2026
09ae85d
[Bugfix] FlashMLA sparse: align quantized KV block strides to 64-toke…
MatthewBonanni Nov 13, 2026
05d3541
[HiSparse] Spread indexer pages only in whole rows
MatthewBonanni Nov 13, 2026
7db47bb
Merge remote-tracking branch 'upstream/main' into hisparse-kimi-k31
MatthewBonanni Nov 16, 2026
cfc04ec
[Kimi K3.1] Warm up and test the self-addressed indexer slot mapping
MatthewBonanni Nov 16, 2026
8879346
Drop runner connector test already covered by main's behavior
MatthewBonanni Nov 16, 2026
d11ced0
Check self-addressed warmup key against the runtime dispatch key
MatthewBonanni Nov 17, 2026
9d0ee5e
Bound the manager-block view by its kernel blocks instead of whole-ro…
MatthewBonanni Nov 17, 2026
b1baf1e
Fit HiSparse K3.1 device memory per request within the no-HiSparse bu…
MatthewBonanni Nov 18, 2026
52bd211
Merge remote-tracking branch 'upstream/main' into hisparse-kimi-k31
MatthewBonanni Nov 24, 2026
b362768
Keep resident blocks pinned until the hot region is allocated
MatthewBonanni Nov 24, 2026
090cc01
Merge remote-tracking branch 'upstream/main' into hisparse-kimi-k31
MatthewBonanni Nov 24, 2026
8839a13
Merge remote-tracking branch 'upstream/main' into hisparse-kimi-k31
MatthewBonanni Nov 24, 2026
841f8cb
Merge remote-tracking branch 'upstream/main' into hisparse-kimi-k31
MatthewBonanni Nov 25, 2026
88fb108
[HiSparse] Account joint device peak via a generic manager hook
MatthewBonanni Nov 25, 2026
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
14 changes: 7 additions & 7 deletions tests/benchmarks/test_forward_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -613,23 +613,23 @@ def test_bench_packed_kv_blocks():

def test_padded_page_spreads_kernel_blocks():
"""A padded page's kernel blocks sit evenly spaced, and its manager block
spans all of them."""
addresses all of them without reaching past a page that ends mid-row."""
spec = MLAAttentionSpec(
block_size=16,
num_kv_heads=1,
head_size=8,
dtype=torch.float32,
tokens_per_state=4,
page_size_padded=4 * 48,
page_size_padded=4 * 44,
)
group = AttentionGroup(CPUMLABackend, ["cache"], spec, 0, kernel_block_size=4)
storage = torch.arange(4 * 60.0).view(4, 60)
manager_view = storage.as_strided((4, 1, 4, 8), (60, 32, 8, 1))
storage = torch.arange(4 * 44.0)
manager_view = storage.as_strided((4, 1, 4, 8), (44, 32, 8, 1))
kernel_view = group.map_kv_cache_to_kernel_blocks(manager_view)
assert kernel_view[:, 0, 0, 0].tolist() == list(range(0, 4 * 60 - 12, 12))
assert kernel_view[:, 0, 0, 0].tolist() == list(range(0, 4 * 44, 11))
manager_view = group.map_kv_cache_to_manager_blocks(kernel_view, 4)
assert manager_view[:, 0, 0, 0].tolist() == [0, 60, 120, 180]
assert manager_view[0].numel() >= 3 * 12 + 8
assert manager_view[:, 0, 0, 0, 0].tolist() == [0, 44, 88, 132]
assert manager_view[0, 0, :, 0, 0].tolist() == [0, 11, 22, 33]


def test_bench_speculative_mamba_blocks():
Expand Down
74 changes: 71 additions & 3 deletions tests/v1/attention/test_indexer_deepseek_v4_slot_mapping.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
from vllm.models.deepseek_v41.sparse_mla import (
DeepseekV4SparseMLABackend as DeepseekV41SparseMLABackend,
)
from vllm.models.kimi_k3.common.dsa_cache import KimiK3IndexerMetadataBuilder
from vllm.platforms import current_platform
from vllm.utils.deep_gemm import mqa_logits_use_mx_scales
from vllm.v1.attention.backend import CommonAttentionMetadata
Expand Down Expand Up @@ -497,9 +498,11 @@ def test_compressed_slot_mapping_inherits_padded_token_slots():


@requires_accelerator
def test_compressed_slot_mapping_dcp_uses_rank_local_state_positions():
@pytest.mark.parametrize("self_addressed", [False, True])
def test_compressed_slot_mapping_dcp_uses_rank_local_state_positions(self_addressed):
"""Under DCP each rank writes only the states it owns, at the state's
rank-local position."""
rank-local position, whether ownership comes from the tokens' slots or,
for a self-addressed cache, from the rank."""
device = torch.device(DEVICE_TYPE)
dcp, interleave, ratio, block_size = 2, 4, 2, 4
num_tokens = 32
Expand All @@ -512,14 +515,15 @@ def test_compressed_slot_mapping_dcp_uses_rank_local_state_positions():
slot_mapping = torch.where(owned, 0, -1).to(torch.int64).to(device)
compressed = get_compressed_slot_mapping(
num_tokens,
slot_mapping,
None if self_addressed else slot_mapping,
query_start_loc,
seq_lens,
block_table,
block_size=block_size,
compress_ratio=ratio,
dcp_world_size=dcp,
cp_interleave=interleave,
dcp_rank=rank,
)
local_pos = pos // (interleave * dcp) * interleave + pos % interleave
state = local_pos // ratio
Expand All @@ -531,6 +535,70 @@ def test_compressed_slot_mapping_dcp_uses_rank_local_state_positions():
assert compressed.tolist() == expected.tolist()


@requires_accelerator
def test_kimi_k3_indexer_writes_block_table_slots_without_group_slot_mapping(
monkeypatch,
):
"""K3.1's self-addressed indexer writes at block-table slots even when its
group's slot mapping is all padding, as in a group of its own."""
device = torch.device(DEVICE_TYPE)
builder = object.__new__(KimiK3IndexerMetadataBuilder)
builder.kv_cache_spec = SimpleNamespace(block_size=4)
builder.slot_mapping_buffer = torch.zeros(16, dtype=torch.int64, device=device)
builder.dcp_world_size = 1
builder.dcp_rank = 0
builder.cp_kv_cache_interleave_size = 1
builder.indexer_uses_fp4 = False
built = []
monkeypatch.setattr(
DeepseekV32IndexerMetadataBuilder,
"build",
lambda self, common_prefix_len, common, fast_build=False: built.append(common),
)
query_start_loc = torch.tensor([0, 6], dtype=torch.int32, device=device)
seq_lens = torch.tensor([10], dtype=torch.int32, device=device)
common = CommonAttentionMetadata(
query_start_loc=query_start_loc,
query_start_loc_cpu=query_start_loc.cpu(),
seq_lens=seq_lens,
seq_lens_cpu_upper_bound=seq_lens.cpu(),
num_reqs=1,
num_actual_tokens=6,
max_query_len=6,
max_seq_len=10,
block_table_tensor=torch.tensor([[5, 7, 9]], dtype=torch.int32, device=device),
slot_mapping=torch.full((6,), -1, dtype=torch.int64, device=device),
causal=True,
)

builder.build(0, common)

(written,) = built
assert written.slot_mapping[:6].tolist() == [28, 29, 30, 31, 36, 37]


@pytest.mark.parametrize("dcp_world_size", [1, 2])
def test_compressed_slot_mapping_warmup_covers_self_addressed_cache(dcp_world_size):
config = SimpleNamespace(
parallel_config=SimpleNamespace(
decode_context_parallel_size=dcp_world_size,
cp_kv_cache_interleave_size=1,
),
)
kernel = CompressedSlotMappingKernel()

warmup_keys = kernel.get_warmup_keys(config, self_addressed_block_size=2304)

runtime_key = kernel.dispatch(
compress_ratio=1,
block_size=2304,
dcp_world_size=dcp_world_size,
cp_interleave=1,
HAS_SLOT_MAPPING=False,
)
assert warmup_keys == [runtime_key]


def test_index_conversion_warmup_uses_physical_block_stride():
config = SimpleNamespace(
attention_config=SimpleNamespace(hisparse_config=None),
Expand Down
30 changes: 24 additions & 6 deletions tests/v1/attention/test_sparse_mla_backends.py
Original file line number Diff line number Diff line change
Expand Up @@ -3841,7 +3841,7 @@ def test_hisparse_row_dma_copies_discontiguous_spans_across_layers():
worker = object.__new__(HiSparseConnectorWorker)
worker.backend = get_hisparse_backend()
worker.is_host_writer = True
worker.kernel_block_size = block_size
worker.resident_block_size = worker.host_block_size = block_size
worker.resident_caches = resident_caches
worker.host_caches = host_caches
worker.cache_handles = [
Expand Down Expand Up @@ -4408,7 +4408,9 @@ def test_hisparse_prefill_staging_plan_resolves_resident_sources():
)

state_indices = torch.arange(2, dtype=torch.int32)
plan.ensure_gpu_sources(resident_table, state_indices, resident_block_size)
plan.ensure_gpu_sources(
resident_table, state_indices, resident_block_size, resident_block_size
)

assert plan.gpu_row_ids is not None
unique_hosts = (plan.row_ids[0].view(-1, block_size)[:, 0] // block_size).tolist()
Expand Down Expand Up @@ -4439,7 +4441,10 @@ def test_hisparse_prefill_staging_plan_resolves_resident_sources():
)

plan.ensure_gpu_sources(
torch.zeros_like(resident_table), state_indices, resident_block_size
torch.zeros_like(resident_table),
state_indices,
resident_block_size,
resident_block_size,
)

assert plan.gpu_row_ids is not None
Expand Down Expand Up @@ -4471,7 +4476,9 @@ def test_hisparse_prefill_resident_sources_follow_each_resident_group():
host_ids = (plan.row_ids[0].view(-1, block_size)[:, 0] // block_size).tolist()

for group in (0, 1, 0, 1):
plan.ensure_gpu_sources(state_rows[:, group], state_indices, block_size)
plan.ensure_gpu_sources(
state_rows[:, group], state_indices, block_size, block_size
)
assert plan.gpu_row_ids is not None
gpu_blocks = plan.gpu_row_ids[0].view(-1, block_size)[:, 0] // block_size
staged = {
Expand Down Expand Up @@ -4508,6 +4515,7 @@ def test_hisparse_gather_prefill_cache_prefers_resident_rows(row_width, kv_dtype
resident_table,
torch.arange(2, dtype=torch.int32, device=device),
resident_block_size,
resident_block_size,
)

num_host_blocks, num_res_blocks = 10, 24
Expand Down Expand Up @@ -5894,6 +5902,7 @@ def test_hisparse_prefill_reuses_builder_staging_plan():
"""Every layer must reuse the batch plan instead of synchronizing to dedupe."""
plan = SimpleNamespace(
block_table=torch.tensor([[0]], dtype=torch.int32),
block_size=1,
ensure_gpu_sources=MagicMock(),
)
staged = torch.empty((1, 1, 8))
Expand All @@ -5910,7 +5919,11 @@ def gather(kv_cache, staging_plan, resident_cache=None):
gather_prefill_cache=gather,
request_state_indices=torch.tensor([0], dtype=torch.int32),
),
view=SimpleNamespace(cache=resident_cache, block_size=1),
view=SimpleNamespace(
cache=resident_cache,
block_size=1,
pages=lambda page_size: (resident_cache, page_size),
),
block_table=resident_block_table,
)
index_group = object.__new__(HiSparseMLAIndexGroup)
Expand Down Expand Up @@ -6052,7 +6065,11 @@ def test_hisparse_fp8_prefill_gather_uses_dedicated_stream(monkeypatch):
resident_cache = torch.empty((2, 4, 656), dtype=torch.uint8)
ready = MagicMock()
cache = _StubHiSparseCacheHandle(
view=SimpleNamespace(cache=resident_cache, block_size=4),
view=SimpleNamespace(
cache=resident_cache,
block_size=4,
pages=lambda page_size: (resident_cache, page_size),
),
block_table=torch.tensor([[2, 3]], dtype=torch.int32),
runtime=SimpleNamespace(
request_state_indices=torch.tensor([0], dtype=torch.int32),
Expand All @@ -6070,6 +6087,7 @@ def test_hisparse_fp8_prefill_gather_uses_dedicated_stream(monkeypatch):
block_table=torch.tensor([[0, 1]], dtype=torch.int32),
row_ids=torch.arange(8, dtype=torch.int32).view(1, -1),
gpu_row_ids=torch.full((1, 8), -1, dtype=torch.int32),
block_size=4,
ensure_gpu_sources=MagicMock(),
)
metadata = SimpleNamespace(
Expand Down
4 changes: 2 additions & 2 deletions tests/v1/attention/test_sparse_mla_kv_cache_layout.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,8 @@
),
(AttentionBackendEnum.FLASHMLA_SPARSE, "auto", True, 1152),
(AttentionBackendEnum.FLASHMLA_SPARSE, "fp8_ds_mla", False, MultipleOf(64)),
(AttentionBackendEnum.FLASHMLA_SPARSE, "fp8_ds_mla", True, 656),
(AttentionBackendEnum.FLASHMLA_SPARSE, "nvfp4_ds_mla", True, 352),
(AttentionBackendEnum.FLASHMLA_SPARSE, "fp8_ds_mla", True, MultipleOf(64)),
(AttentionBackendEnum.FLASHMLA_SPARSE, "nvfp4_ds_mla", True, MultipleOf(64)),
],
)
def test_allocation_and_warmup_follow_addressing_mode(
Expand Down
11 changes: 8 additions & 3 deletions tests/v1/core/test_kv_cache_config_builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,11 +119,16 @@ def test_model_classmethod_owns_grouping(self):
cfg = _make_vllm_config(supports_grouping=True)
assert DefaultKVCacheConfigBuilder().get_kv_cache_groups(cfg, {}) == []

def test_model_grouping_rejects_hisparse(self):
def test_hisparse_owns_grouping_over_model(self):
cfg = _make_vllm_config(supports_grouping=True)
cfg.attention_config.hisparse_config = object()
with pytest.raises(ValueError, match="HiSparse"):
DefaultKVCacheConfigBuilder().get_kv_cache_groups(cfg, {})
with patch(
"vllm.v1.core.kv_cache_planning.get_kv_cache_groups",
return_value=["default"],
):
groups = DefaultKVCacheConfigBuilder().get_kv_cache_groups(cfg, {})
assert groups == ["default"]
cfg.model_config.registry.resolve_model_cls.assert_not_called()


class TestCoreChecksGrouping:
Expand Down
12 changes: 6 additions & 6 deletions tests/v1/core/test_kv_cache_planning.py
Original file line number Diff line number Diff line change
Expand Up @@ -299,9 +299,9 @@ def test_hisparse_sparse_full_attention_owns_rank_local_complete_kv(monkeypatch)
[
(8192, 160, False),
(8192, 1000, True),
(32768, 656, False),
(32768, 657, True),
(-1, 657, True),
(32768, 640, False),
(32768, 641, True),
(-1, 641, True),
],
)
def test_hisparse_pool_must_fit_max_model_len(
Expand All @@ -310,9 +310,9 @@ def test_hisparse_pool_must_fit_max_model_len(
"""Each resident group takes its own blocks from HiSparse's shared GPU pool,
so a pool that fits only the indexer pages must be rejected at startup.
Resident pages are capped at the in-flight window: at 32768 tokens with 2048
in flight, one request needs 512 indexer + 4 x 32 resident + 16 hot blocks,
plus the null block. Auto-fit (-1) must pick a length that passes the same
check."""
in flight, one request needs 512 indexer + 4 x 32 resident blocks, plus the
null block. Its 16 hot blocks fit in the resident pages it releases for
them. Auto-fit (-1) must pick a length that passes the same check."""
monkeypatch.setattr(
current_platform,
"get_hisparse_backend_cls",
Expand Down
Loading