diff --git a/tests/kernels/quantization/test_rdna_hybrid_w4a16.py b/tests/kernels/quantization/test_rdna_hybrid_w4a16.py index 76f432a66f9d..1af45d14ded1 100644 --- a/tests/kernels/quantization/test_rdna_hybrid_w4a16.py +++ b/tests/kernels/quantization/test_rdna_hybrid_w4a16.py @@ -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) @@ -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, @@ -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 # --------------------------------------------------------------------------- diff --git a/vllm/model_executor/kernels/linear/mixed_precision/rdna_hybrid_w4a16.py b/vllm/model_executor/kernels/linear/mixed_precision/rdna_hybrid_w4a16.py index cfa9d53d6894..f12ab449cb1d 100644 --- a/vllm/model_executor/kernels/linear/mixed_precision/rdna_hybrid_w4a16.py +++ b/vllm/model_executor/kernels/linear/mixed_precision/rdna_hybrid_w4a16.py @@ -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: