Skip to content

fix: release the AWQ per-layer ROCm allocator cache (#4313) - #4342

Merged
Qubitium merged 1 commit into
mainfrom
pr/4313
Nov 16, 2026
Merged

Qubitium merged 1 commit into
mainfrom
pr/4313

Conversation

@Qubitium

Copy link
Copy Markdown
Collaborator

Summary

AWQ's clip search (_compute_best_clip) pre-allocates a scratch_prod buffer per clip-eligible module from the GPU MSE budget (MSEConfig defaults to 256 MiB on GPU). For Qwen3-0.6B those requests are 255 MiB (v_proj, gate_proj, up_proj, down_proj) and 250 MiB (o_proj), which PyTorch rounds to 256.0 MiB and 250.0 MiB segments. On ROCm the default PYTORCH_ALLOC_CONF='max_split_size_mb:256,garbage_collection_threshold:0.7' makes the native caching allocator refuse to serve any request smaller than max_split_size from a free block of at least that size (get_free_block: "Do not return an oversized block for a large request"), and it also refuses to split such a block. Each clip module therefore allocates a fresh 256 MiB segment that no later 255 MiB request can reuse. garbage_collection_threshold is inert without a memory fraction, and AWQ never released the cache at layer boundaries, so reserved VRAM grew by ~1 GiB per decoder layer while active memory stayed flat.

Evidence:

  • Static sizing for Qwen3-0.6B (hidden 1024, intermediate 3072, group 128, 512 sampled rows, 256 MiB budget): v/gate/up/down request 267,386,880 B = 255.00 MiB -> 256.0 MiB segment, o_proj 262,144,000 B = 250.00 MiB -> 250.0 MiB segment: four 256 MiB + one 250 MiB segment per layer, matching the issue's snapshot.
  • Allocator probe with the ROCm default conf on the same native caching-allocator code (CUDA build, CUDA_VISIBLE_DEVICES=1, <1 GiB): alternating 255 MiB / 250 MiB requests grow reserved 258 -> 508 -> 764 -> 1020 MiB; memory_snapshot() shows segment_total=256.0 MiB, segment_allocated=0.0, one inactive block, stream 0 - the exact signature in the issue. Without max_split_size_mb:256 (or with 1024, or with expandable_segments:True) reserved stays flat at 258 MiB for the same sequence.
  • GPTQ stays flat because its per-layer buffers have identical sizes across layers and are reused; AWQ mixes 255/250 MiB scratch within one layer.

What Changed

  • apply_clip() now calls a small _release_rocm_layer_cache() helper once per AWQ layer. apply_clip is the once-per-layer boundary where all clip-search scratch has already been freed and the next layer has not allocated yet; releasing the unused cache there keeps reserved memory flat. The helper is ROCm-only (torch.version.hip is not None) and best-effort, so CUDA keeps its allocator cache and pays no extra cudaMalloc, and a cleanup failure can never abort quantization.
  • Added tests/test_awq_rocm_layer_cache_4313.py covering: exactly one release per layer (two clipped modules -> one call), release on the resolved quantization device, no release on CUDA, no release without CUDA, and failure tolerance.

Tests

$ export GPTQ_BYPASS_SAFETY_CHECKS=1 PYTHONPATH=$PWD CUDA_VISIBLE_DEVICES=
$ python3 -m pytest tests/test_awq_rocm_layer_cache_4313.py -q
5 passed

$ python3 -m pytest tests/test_awq_rocm_layer_cache_4313.py \
    tests/test_awq_scale_search_memory.py tests/test_awq_scale_folding.py \
    tests/test_awq_empty_groups.py -q
48 passed

$ python3 -m pytest tests/test_rocm_default_env.py -q
11 passed, 4 skipped

Allocator probe (evidence only, not a committed test):

$ CUDA_VISIBLE_DEVICES=1 PYTORCH_ALLOC_CONF='max_split_size_mb:256,garbage_collection_threshold:0.7' \
    python3 alloc_probe.py
255MiB -> reserved=258.0 MiB
250MiB -> reserved=508.0 MiB
255MiB -> reserved=764.0 MiB
250MiB -> reserved=764.0 MiB
... segment_total=256.0 MiB, segment_allocated=0.0, block state=inactive, stream=0

Model Support / Module Plan

N/A - no new model types or registry entries.

Fixes #4313

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[BUG] AWQ quantization grows reserved VRAM by ~1 GiB per layer on ROCm (256 MiB segments never reused), starving other processes on the GPU

1 participant