Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
62 changes: 43 additions & 19 deletions tests/kernels/quantization/test_rdna_hybrid_w4a16.py
Original file line number Diff line number Diff line change
Expand Up @@ -624,24 +624,31 @@ def test_rdna_hybrid_w4a16_prefill_dequant_cached(
assert getattr(layer, _W_DEQUANT_ATTR).stride(0) > K


def test_rdna_hybrid_w4a16_prefill_dequant_budget_exhausted(dist_init, monkeypatch):
"""Over budget, enabling the cache raises instead of caching a subset."""
if not torch.cuda.is_available():
pytest.skip("CUDA/HIP device not available")

def _stub_memory_snapshot(monkeypatch, free_gib, cached_free_gib=0):
from types import SimpleNamespace

from vllm.config import set_current_vllm_config

# The real free memory is machine dependent; 40 GiB free with 50 GiB
# outside a 0.5 budget leaves nothing to spend.
# The real free memory is machine dependent.
monkeypatch.setattr(
hybrid_module,
"MemorySnapshot",
lambda device: SimpleNamespace(
free_memory=40 * 2**30, total_memory=100 * 2**30
free_memory=free_gib * 2**30,
total_memory=100 * 2**30,
torch_memory=cached_free_gib * 2**30,
torch_allocated=0,
),
)


def test_rdna_hybrid_w4a16_prefill_dequant_budget_exhausted(dist_init, monkeypatch):
"""Over budget, enabling the cache raises instead of caching a subset."""
if not torch.cuda.is_available():
pytest.skip("CUDA/HIP device not available")

from vllm.config import set_current_vllm_config

# 40 GiB free with 50 GiB outside a 0.5 budget leaves nothing to spend.
_stub_memory_snapshot(monkeypatch, free_gib=40)
set_random_seed(0)
layer, kernel, *_ = _build_prefill_dequant_case(256, 128, 128, has_zp=False)

Expand All @@ -660,19 +667,11 @@ def test_rdna_hybrid_w4a16_prefill_dequant_reserves_min_kv(
if not torch.cuda.is_available():
pytest.skip("CUDA/HIP device not available")

from types import SimpleNamespace

from vllm.config import set_current_vllm_config

# 60 GiB free with 50 GiB outside a 0.5 budget leaves 10 GiB, 6 GiB after
# the activation headroom: enough for the layer unless KV claims 8 GiB.
monkeypatch.setattr(
hybrid_module,
"MemorySnapshot",
lambda device: SimpleNamespace(
free_memory=60 * 2**30, total_memory=100 * 2**30
),
)
_stub_memory_snapshot(monkeypatch, free_gib=60)
monkeypatch.setattr(
"vllm.v1.core.kv_cache_utils.max_memory_usage_bytes",
lambda vllm_config, specs: min_kv_gib * 2**30,
Expand All @@ -689,6 +688,31 @@ def test_rdna_hybrid_w4a16_prefill_dequant_reserves_min_kv(
kernel.process_weights_after_loading(layer)


def test_rdna_hybrid_w4a16_prefill_dequant_counts_allocator_cache_as_free(
dist_init, monkeypatch
):
"""Blocks cached by the torch allocator are spendable, without a flush."""
if not torch.cuda.is_available():
pytest.skip("CUDA/HIP device not available")

from vllm.config import set_current_vllm_config

# Only 40 GiB is free on the device, but 20 GiB more sits in the allocator
# cache: the same 10 GiB of headroom as 60 GiB free.
_stub_memory_snapshot(monkeypatch, free_gib=40, cached_free_gib=20)
monkeypatch.setattr(
torch.accelerator,
"empty_cache",
lambda: pytest.fail("the budget check must not flush the allocator"),
)
set_random_seed(0)
layer, kernel, *_ = _build_prefill_dequant_case(256, 128, 128, has_zp=False)

with set_current_vllm_config(_prefill_dequant_vllm_config("enable", 0.5)):
kernel.process_weights_after_loading(layer)
assert hasattr(layer, _W_DEQUANT_ATTR)


# ---------------------------------------------------------------------------
# can_implement enforces the supported-group-size policy
# ---------------------------------------------------------------------------
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,12 @@ def _fits_in_memory_budget(nbytes: int, device: torch.device) -> bool:
reserved = (
snapshot.total_memory * (1.0 - util) + activation_headroom_bytes + min_kv_bytes
)
return snapshot.free_memory - reserved >= nbytes
# free_memory counts blocks the torch allocator cached after load-time
# temporaries as used (15 GiB by the last layer of Gemma-4-31B MTP-3), but
# the allocator reuses them, or releases them on an allocation failure, and
# KV sizing excludes them too.
cached_free = snapshot.torch_memory - snapshot.torch_allocated
return snapshot.free_memory + cached_free - reserved >= nbytes


def _profile_scope(name: str) -> AbstractContextManager:
Expand Down
Loading