Skip to content

Conversation

@cafeTechne
Copy link

@cafeTechne cafeTechne commented Nov 25, 2025

Dynamic n_gpu_layers Heuristic for Low-VRAM GPUs

Summary

This PR implements a dynamic n_gpu_layers calculation based on available VRAM to enable optimal GPU offloading on low-VRAM devices like the AMD RX 6500 XT.

Motivation

The primary motivation for this PR is to enable use of llama.cpp on low-VRAM GPUs such as the AMD RX 6500 XT, which is particularly compelling due to its low power consumption and affordability. Many users, including myself, cannot justify purchasing a higher-end GPU, yet still want meaningful acceleration from Vulkan offloading.

Instead of requiring users to manually tune n_gpu_layers, this PR automates the process to prevent OOM crashes while maximizing acceleration.

The design also comports with the expectations outlined in the llama.cpp CONTRIBUTING.md guidelines:

  • The feature is self-contained and maintains codebase minimalism.
  • It adds functionality without modifying core operators.
  • It uses clear naming conventions and avoids architectural complexity.
  • It provides documentation, benchmarks, and reasoning consistent with contributor requirements.

Changes

Core Implementation

Dynamic Heuristic (common/common.cpp):

  • Queries GGUF metadata for model size and layer count
  • Calculates optimal n_gpu_layers based on available VRAM
  • Reserves 800MB overhead for KV cache and compute buffers
  • Triggered when n_gpu_layers = -1 (default)
  • Generalizes across architectures (Gemma, Llama, Qwen, etc.)

VRAM Query API (ggml-vulkan.cpp):

  • Added ggml_backend_vk_get_device_memory() to query available VRAM
  • Exposes device memory info to heuristic layer

Documentation & Testing

  • Added docs/windows_vulkan_low_vram.md
  • Benchmark scripts for validation
  • Inline comments explaining heuristic logic

Performance (llama-bench)

Hardware: AMD RX 6500 XT (4GB VRAM)
Model: Gemma 2B Q4_K_M (1.59 GiB)

Performance Summary

Metric CPU-only GPU Heuristic Improvement
Prompt processing (pp512) 497 t/s 1231 t/s +147%
Token generation (tg128) 19.4 t/s 60.4 t/s +212%
Layers offloaded 0/27 26/27 Auto-optimized

Multi-Model Results

Model Size Layers Offloaded Performance
Gemma 2B 1.6GB 26/27 (96%) 2.5–3.1× faster
Llama 3.2 3B 1.9GB 28/29 (97%) ~2× faster
Llama 2 7B 3.9GB 21/33 (64%) 1.6× faster

Key Insight: The heuristic maximizes offloading for small models while preventing OOM on larger models.

Testing

  • llama-bench: Verified 2.5-3.1x speedup on Gemma 2B
  • Multi-model: Tested on Gemma 2B, Llama 2 7B, Llama 2 13B
  • OOM Prevention: Larger models gracefully degrade (no crashes)
  • Platform: Windows 11, AMD RX 6500 XT
  • Cross-platform: Linux/macOS testing pending (code is platform-agnostic, so I don't anticipate there being any issues here)

Compliance

  • ✅ No third-party dependencies
  • ✅ Follows naming conventions (snake_case, longest prefix)
  • ✅ No ggml operators modified
  • ✅ Trailing whitespace cleaned
  • clang-format run

Maintainer

Requesting review from @0cc4m (Vulkan backend maintainer per CODEOWNERS).
Willing to maintain long-term if accepted as collaborator.
and hope to extend this method to whisper and ggml for the same motivations!

@github-actions github-actions bot added documentation Improvements or additions to documentation testing Everything test related Vulkan Issues specific to the Vulkan backend examples ggml changes relating to the ggml tensor library for machine learning labels Nov 25, 2025
@cafeTechne cafeTechne requested a review from 0cc4m as a code owner November 25, 2025 21:43
Implements a dynamic VRAM allocation heuristic that automatically calculates
the optimal number of GPU layers to offload based on available VRAM.

Changes:
- Added ggml_backend_vk_get_device_info and ggml_backend_vk_get_default_gpu_layers to ggml-vulkan.cpp
- Added dynamic heuristic to common_model_params_to_llama in common.cpp
- Added llama-vk-device-info tool for inspecting Vulkan devices
- Added documentation in docs/vulkan_low_vram.md

Tested on AMD RX 6500 XT with 4GB VRAM, achieving 2.5-3.1x speedup.
@cafeTechne cafeTechne force-pushed the vulkan-dynamic-vram-heuristic branch from 5c8ad2b to 03fe95d Compare November 27, 2025 18:42
Implements a dynamic VRAM allocation heuristic that automatically calculates
the optimal number of GPU layers to offload based on available VRAM.

Changes:
- Added ggml_backend_vk_get_device_info and ggml_backend_vk_get_default_gpu_layers to ggml-vulkan.cpp
- Added dynamic heuristic to common_model_params_to_llama in common.cpp
- Added llama-vk-device-info tool for inspecting Vulkan devices
- Added documentation in docs/vulkan_low_vram.md

Tested on AMD RX 6500 XT with 4GB VRAM, achieving 2.5-3.1x speedup.
@cafeTechne cafeTechne force-pushed the vulkan-dynamic-vram-heuristic branch from 2273c93 to e8bf9ed Compare November 28, 2025 01:36
if (params.n_gpu_layers != -1) {
mparams.n_gpu_layers = params.n_gpu_layers;
}
#ifdef GGML_USE_VULKAN
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Backend-specific code has been long removed from outside the GGML library, you have to use the backend interface to access them in a generic way. You can already do that for total and free VRAM.

Have you looked into #16653 to see if that already covers your use case? That proposes a backend-agnostic way to automate setting parameters.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for covering the first part, but you have not answered the questions yet. Have you tried that PR?

Replaces Vulkan-specific calls with ggml_backend_dev_memory to be backend-agnostic. Reverts changes to ggml-vulkan.cpp/h and removes vk_device_info example to comply with reviewer feedback.
@cafeTechne cafeTechne requested a review from 0cc4m November 29, 2025 17:08
@cafeTechne
Copy link
Author

cafeTechne commented Dec 13, 2025 via email

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

Labels

documentation Improvements or additions to documentation examples ggml changes relating to the ggml tensor library for machine learning testing Everything test related Vulkan Issues specific to the Vulkan backend

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants