From 43b34c09312f1f157a1924647d74daf335314860 Mon Sep 17 00:00:00 2001 From: Zohaib Moti Date: Wed, 28 Oct 2026 12:59:46 -0600 Subject: [PATCH 1/2] [ROCm] Flush allocator cache before failing W4A16 dense-cache check Signed-off-by: Zohaib Moti --- .../quantization/test_rdna_hybrid_w4a16.py | 71 +++++++++++++++++++ .../mixed_precision/rdna_hybrid_w4a16.py | 9 ++- 2 files changed, 79 insertions(+), 1 deletion(-) diff --git a/tests/kernels/quantization/test_rdna_hybrid_w4a16.py b/tests/kernels/quantization/test_rdna_hybrid_w4a16.py index 76f432a66f9d..41a5a82d67aa 100644 --- a/tests/kernels/quantization/test_rdna_hybrid_w4a16.py +++ b/tests/kernels/quantization/test_rdna_hybrid_w4a16.py @@ -689,6 +689,77 @@ def test_rdna_hybrid_w4a16_prefill_dequant_reserves_min_kv( kernel.process_weights_after_loading(layer) +@pytest.mark.parametrize("free_after_flush_gib, fits", [(60, True), (40, False)]) +def test_rdna_hybrid_w4a16_prefill_dequant_flushes_allocator_cache( + free_after_flush_gib, fits, dist_init, monkeypatch +): + """Cached allocator blocks are released before declaring the budget full.""" + 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 + + free = {"bytes": 40 * 2**30} + flushes = [] + + def empty_cache(): + flushes.append(True) + free["bytes"] = free_after_flush_gib * 2**30 + + # 40 GiB free leaves nothing inside a 0.5 budget; 60 GiB leaves 6 GiB + # after the activation headroom. + monkeypatch.setattr( + hybrid_module, + "MemorySnapshot", + lambda device: SimpleNamespace( + free_memory=free["bytes"], total_memory=100 * 2**30 + ), + ) + monkeypatch.setattr(torch.accelerator, "empty_cache", empty_cache) + 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)): + if fits: + kernel.process_weights_after_loading(layer) + assert hasattr(layer, _W_DEQUANT_ATTR) + else: + with pytest.raises(ValueError, match="gpu_memory_utilization"): + kernel.process_weights_after_loading(layer) + assert flushes + + +def test_rdna_hybrid_w4a16_prefill_dequant_no_flush_when_fits(dist_init, monkeypatch): + """A layer that fits never pays for an allocator flush.""" + 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 + + monkeypatch.setattr( + hybrid_module, + "MemorySnapshot", + lambda device: SimpleNamespace( + free_memory=60 * 2**30, total_memory=100 * 2**30 + ), + ) + monkeypatch.setattr( + torch.accelerator, + "empty_cache", + lambda: pytest.fail("empty_cache called although the layer fits"), + ) + 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..46491d850619 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,14 @@ 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 + if snapshot.free_memory - reserved >= nbytes: + return True + # Blocks the torch allocator caches after load-time temporaries still count + # as used device memory: 15 GiB by the last layer of Gemma-4-31B MTP-3, + # enough to fail on its own. Flush only when short, since flushing every + # layer adds ~11 s to that model's load. + torch.accelerator.empty_cache() + return MemorySnapshot(device=device).free_memory - reserved >= nbytes def _profile_scope(name: str) -> AbstractContextManager: From 2dcc34fd862d3be9ab70b5f47c3c4472817c9e88 Mon Sep 17 00:00:00 2001 From: Zohaib Moti Date: Thu, 29 Oct 2026 11:55:03 -0600 Subject: [PATCH 2/2] [ROCm] Count cached allocator blocks as free in W4A16 budget check Signed-off-by: Zohaib Moti --- .../quantization/test_rdna_hybrid_w4a16.py | 97 +++++-------------- .../mixed_precision/rdna_hybrid_w4a16.py | 14 ++- 2 files changed, 31 insertions(+), 80 deletions(-) diff --git a/tests/kernels/quantization/test_rdna_hybrid_w4a16.py b/tests/kernels/quantization/test_rdna_hybrid_w4a16.py index 41a5a82d67aa..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,68 +688,22 @@ def test_rdna_hybrid_w4a16_prefill_dequant_reserves_min_kv( kernel.process_weights_after_loading(layer) -@pytest.mark.parametrize("free_after_flush_gib, fits", [(60, True), (40, False)]) -def test_rdna_hybrid_w4a16_prefill_dequant_flushes_allocator_cache( - free_after_flush_gib, fits, dist_init, monkeypatch +def test_rdna_hybrid_w4a16_prefill_dequant_counts_allocator_cache_as_free( + dist_init, monkeypatch ): - """Cached allocator blocks are released before declaring the budget full.""" + """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 types import SimpleNamespace - from vllm.config import set_current_vllm_config - free = {"bytes": 40 * 2**30} - flushes = [] - - def empty_cache(): - flushes.append(True) - free["bytes"] = free_after_flush_gib * 2**30 - - # 40 GiB free leaves nothing inside a 0.5 budget; 60 GiB leaves 6 GiB - # after the activation headroom. - monkeypatch.setattr( - hybrid_module, - "MemorySnapshot", - lambda device: SimpleNamespace( - free_memory=free["bytes"], total_memory=100 * 2**30 - ), - ) - monkeypatch.setattr(torch.accelerator, "empty_cache", empty_cache) - 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)): - if fits: - kernel.process_weights_after_loading(layer) - assert hasattr(layer, _W_DEQUANT_ATTR) - else: - with pytest.raises(ValueError, match="gpu_memory_utilization"): - kernel.process_weights_after_loading(layer) - assert flushes - - -def test_rdna_hybrid_w4a16_prefill_dequant_no_flush_when_fits(dist_init, monkeypatch): - """A layer that fits never pays for an allocator flush.""" - 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 - - monkeypatch.setattr( - hybrid_module, - "MemorySnapshot", - lambda device: SimpleNamespace( - free_memory=60 * 2**30, total_memory=100 * 2**30 - ), - ) + # 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("empty_cache called although the layer fits"), + 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) 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 46491d850619..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,14 +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 ) - if snapshot.free_memory - reserved >= nbytes: - return True - # Blocks the torch allocator caches after load-time temporaries still count - # as used device memory: 15 GiB by the last layer of Gemma-4-31B MTP-3, - # enough to fail on its own. Flush only when short, since flushing every - # layer adds ~11 s to that model's load. - torch.accelerator.empty_cache() - return MemorySnapshot(device=device).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: