feat(vulkan): Add native Vulkan support for TurboQuant KV Cache (turbo3/turbo4)#158
Open
Yvi71 wants to merge 4 commits into
Open
Conversation
The legacy memory pool (ggml_cuda_pool_leg) retains peak-sized allocations permanently. For quantized KV flash attention, the f16 dequant temp buffers (K_f16, V_f16) stay allocated in the pool after use, consuming more VRAM than the KV compression saves. This causes quantized KV (q8_0, q4_0) to OOM before f16 at equivalent context lengths on HIP/ROCm where VMM is unavailable. Root cause: ggml_cuda_pool_leg::free() stores buffers in buffer_pool[] for reuse and never calls cudaFree. On CUDA with VMM the OS can reclaim unused virtual memory. On HIP without VMM (all consumer RDNA 3/4 GPUs), the pool permanently consumes peak VRAM. Fix: on HIP, allocate f16 temp buffers with cudaMalloc and free with cudaFree (via RAII wrapper) instead of the pool. Memory is released after the FA kernel completes via cudaStreamSynchronize. Trade-off: one cudaStreamSynchronize per FA call (~5% overhead at 32K). Impact: CUDA/Metal unaffected (#ifdef GGML_USE_HIP only). Confirmed: gfx1100 (RX 7900 XT), gfx1201 (RX 9070 XT) Fixes: ggml-org#22107
hip: bypass memory pool for FA f16 temp buffers
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This PR adds native Vulkan support for the TurboQuant KV Cache quantization formats (
turbo3andturbo4) on AMD RDNA 2 and other Vulkan-compatible hardware.Currently, TurboQuant is fully implemented for Metal (macOS) and HIP/CUDA backends, but Vulkan support was missing, resulting in assertion failures and missing shader pipelines when attempting to run on Vulkan.
Changes:
ggml-backend.cpp: RelaxedGGML_ASSERTchecks for async get/set operations on tensor views. Since the KV Cache copying and shifting operations often use views (without directtensor->data), this prevents hard assertion crashes when moving the quantized cache.ggml-vulkan.cpp: Registered theGGML_TYPE_TURBO3_0andGGML_TYPE_TURBO4_0types in the Vulkan device capability checks and loaded their set-row pipelines.vulkan-shaders-gen.cpp: Addedturbo3_0andturbo4_0to the offline shader compilation loop, allowingcopy_to_quant.compto correctly generate SPIR-V shaders (set_rows_turbo3_0andset_rows_turbo4_0).Testing: