[Bugfix][HiSparse] Size the KV cache from the groups HiSparse allocates (#59450)

Signed-off-by: Matthew Bonanni <mbonanni@redhat.com>
Signed-off-by: Lucas Wilkinson <lwilkins@redhat.com>
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
Co-authored-by: Lucas Wilkinson <LucasWilkinson@users.noreply.github.com>
This commit is contained in:
Matthew Bonanni
2026-10-02 04:11:55 +00:00
committed by GitHub
co-authored by Claude Opus 5.5 Lucas Wilkinson
parent 1a9eaa3a68
commit f7999d2e44
8 changed files with 187 additions and 71 deletions
+115 -23
View File
@@ -59,7 +59,7 @@ from vllm.v1.core.kv_cache_utils import (
)
from vllm.v1.hisparse.layout import (
create_hisparse_layout,
get_hisparse_gpu_memory_usage,
get_hisparse_kv_cache_groups,
)
from vllm.v1.kv_cache_interface import (
ChunkedLocalAttentionSpec,
@@ -118,9 +118,6 @@ def test_hisparse_hma_uses_resolved_gpu_block_size(
cache_role=SparseCacheRole.INDEXER,
),
}
group_spec = UniformTypeKVCacheSpecs.from_specs(specs)
assert group_spec is not None
group = KVCacheGroupSpec(list(specs), group_spec)
config = SimpleNamespace(
attention_config=SimpleNamespace(hisparse_config=HiSparseConfig()),
model_config=SimpleNamespace(
@@ -142,14 +139,9 @@ def test_hisparse_hma_uses_resolved_gpu_block_size(
get_resolved_kv_cache_layout=lambda: KVCacheLayout.BLHNC,
),
)
indexer_spec = specs["model.layers.0.self_attn.indexer"]
assert get_hisparse_gpu_memory_usage(config, [group]) == (
indexer_spec.max_memory_usage_bytes(config)
)
monkeypatch.setattr(kv_cache_utils, "get_hisparse_host_pool_bytes", lambda _: 2**30)
cache_config = kv_cache_utils.get_kv_cache_config_from_groups(
config, [group], available_memory=2**30
config, get_hisparse_kv_cache_groups(config, specs), available_memory=2**30
)
assert cache_config.num_blocks == 7
assert cache_config.hisparse_host_num_blocks is not None
@@ -228,8 +220,6 @@ def test_hisparse_host_pool_must_fit_max_model_len(monkeypatch, extra_blocks, ok
cache_role=SparseCacheRole.INDEXER,
),
}
group_spec = UniformTypeKVCacheSpecs.from_specs(specs)
assert group_spec is not None
config = SimpleNamespace(
attention_config=SimpleNamespace(hisparse_config=HiSparseConfig()),
model_config=SimpleNamespace(
@@ -240,14 +230,123 @@ def test_hisparse_host_pool_must_fit_max_model_len(monkeypatch, extra_blocks, ok
)
host_page = specs["model.layers.0.self_attn"].page_size_bytes
host_budget = (4 + extra_blocks) * host_page
group = KVCacheGroupSpec(list(specs), group_spec)
groups = get_hisparse_kv_cache_groups(config, specs)
if ok:
layout = create_hisparse_layout(config, [group], host_budget=host_budget)
layout = create_hisparse_layout(config, groups, host_budget=host_budget)
assert layout.host_num_blocks == 4 + extra_blocks
else:
with pytest.raises(ValueError, match="increase host_pool_gib"):
create_hisparse_layout(config, [group], host_budget=host_budget)
create_hisparse_layout(config, groups, host_budget=host_budget)
@pytest.mark.parametrize(
"max_model_len,num_gpu_blocks,ok",
[
(8192, 160, False),
(8192, 1000, True),
(32768, 656, False),
(32768, 657, True),
(-1, 657, True),
],
)
def test_hisparse_pool_must_fit_max_model_len(
monkeypatch, max_model_len, num_gpu_blocks, ok
):
"""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."""
monkeypatch.setattr(
hisparse_runtime_module.current_platform, "is_cuda_alike", lambda: True
)
monkeypatch.setattr(kv_cache_utils, "get_hisparse_host_pool_bytes", lambda _: 2**30)
specs: dict[str, KVCacheSpec] = {}
for i in range(4):
specs[f"model.layers.{i}.self_attn"] = MLAAttentionSpec(
block_size=64,
num_kv_heads=1,
head_size=576,
dtype=torch.bfloat16,
is_index_group_leader=True,
)
specs[f"model.layers.{i}.self_attn.indexer"] = MLAAttentionSpec(
block_size=64,
num_kv_heads=1,
head_size=128,
dtype=torch.bfloat16,
cache_role=SparseCacheRole.INDEXER,
)
config = VllmConfig(model_config=ModelConfig(max_model_len=max_model_len))
config.scheduler_config.max_num_batched_tokens = 2048
# Async scheduling doubles the in-flight window, and with it the bound.
config.scheduler_config.async_scheduling = False
config.attention_config.hisparse_config = HiSparseConfig()
config.model_config.hf_config.index_topk = 128
config.cache_config.num_gpu_blocks_override = num_gpu_blocks
config.cache_config.kv_cache_layout = "BLHNC"
# Workers report the resident/hot caches HiSparse derives.
specs |= {
name: group.kv_cache_spec
for group in get_hisparse_kv_cache_groups(config, specs) or []
if isinstance(group.kv_cache_spec, (HiSparseResidentSpec, HiSparseHotSpec))
for name in group.layer_names
}
if ok:
kv_cache_utils.get_kv_cache_configs(config, [specs], [2**34])
else:
with pytest.raises(ValueError, match="max seq len"):
kv_cache_utils.get_kv_cache_configs(config, [specs], [2**34])
def test_hisparse_derived_specs_do_not_affect_kv_cache_layout(monkeypatch):
"""Workers report HiSparse's resident/hot caches, which lay out their own raw
backing; KV cache layout resolution must skip them."""
from vllm.v1.attention.backends.utils import resolve_kv_cache_layout
monkeypatch.setattr(
hisparse_runtime_module.current_platform, "is_cuda_alike", lambda: True
)
monkeypatch.delenv("VLLM_KV_CACHE_LAYOUT", raising=False)
specs: dict[str, KVCacheSpec] = {
"model.layers.0.self_attn": MLAAttentionSpec(
block_size=64,
num_kv_heads=1,
head_size=576,
dtype=torch.bfloat16,
is_index_group_leader=True,
),
"model.layers.0.self_attn.indexer": MLAAttentionSpec(
block_size=64,
num_kv_heads=1,
head_size=128,
dtype=torch.bfloat16,
cache_role=SparseCacheRole.INDEXER,
),
}
config = VllmConfig(model_config=ModelConfig(max_model_len=1024))
config.attention_config.hisparse_config = HiSparseConfig()
config.model_config.hf_config.index_topk = 128
reported = specs | {
name: group.kv_cache_spec
for group in get_hisparse_kv_cache_groups(config, specs) or []
if isinstance(group.kv_cache_spec, (HiSparseResidentSpec, HiSparseHotSpec))
for name in group.layer_names
}
assert len(reported) > len(specs)
layouts = []
for worker_specs in (specs, reported):
config.cache_config.kv_cache_layout = None
layouts.append(
resolve_kv_cache_layout(
config, [["BLHNC", "LBNHC"]], list(worker_specs.values())
)
)
assert layouts[0] == layouts[1]
def test_hisparse_rejects_deepseek_v4():
@@ -260,9 +359,6 @@ def test_hisparse_rejects_deepseek_v4():
model_version="deepseek_v4",
)
}
full_uniform = UniformTypeKVCacheSpecs.from_specs(full_specs)
assert full_uniform is not None
group = KVCacheGroupSpec(list(full_specs), full_uniform)
config = SimpleNamespace(
attention_config=SimpleNamespace(hisparse_config=HiSparseConfig()),
model_config=SimpleNamespace(hf_config=SimpleNamespace(index_topk=512)),
@@ -270,11 +366,7 @@ def test_hisparse_rejects_deepseek_v4():
)
with pytest.raises(ValueError, match="does not support DeepSeek V4"):
create_hisparse_layout(
config,
[group],
host_budget=2**30,
)
get_hisparse_kv_cache_groups(config, full_specs)
@pytest.fixture(autouse=True)
+3
View File
@@ -86,6 +86,9 @@ def test_get_kv_cache_spec_resolves_hisparse_block_size(
get_attn_backend=lambda backend=backend: backend,
)
monkeypatch.setattr(attn_utils, "get_layers_from_vllm_config", lambda *_: layers)
monkeypatch.setattr(
attn_utils_module, "get_hisparse_kv_cache_groups", lambda *_: []
)
config = SimpleNamespace(
attention_config=SimpleNamespace(hisparse_config=object() if enabled else None)
)