Repository navigation
Experimenting with performance boosts on Windows (Non-WSL) using a modified clr: llama.cpp + Qwen3.8-27b + 9060XT 8GB #8728
Replies: 2 comments
Appendix A: Building clr standalone using the Python package rocm-sdk and a git-cloned TheRockNormally, I build everything by cloning TheRock and targeting hip-clr+build after running the CMake configuration. However, since it has to build LLVM first, the initial build takes a long time. To speed things up, I tried building clr standalone using the Python package version of rocm-sdk (I personally love the Python package because it makes switching between different ROCm environments really easy.). While I used the clr's README.md as a guide, using the Python package instead of the standard tarball archive method meant I encountered a few extra hurdles. Here’s how to navigate them. PreparationFirst, verify that TheRock is at the same version as the python package rocm-sdk (and git checkout if necessary), then follow the instructions in TheRock's README.md (windows-11-vs-2022) to complete the build process up to the cmake configuration step (cmake -B build -GNinja . -DTHEROCK_AMDGPU_FAMILIES=gfxXXXXX). Required Fixes1. Fix .../clr/hipamd/src/hiprtc/CMakeLists.txtIn the lines starting with "find_program", change ${ROCM_PATH}/llvm/bin to ${ROCM_PATH}/lib/llvm/bin. (There are two spots to fix.) 2. Enable kpackOn therock10.0, I found that kpack is necessary if you want to use PyTorch. 3. Fetch missing dependencies from TheRockAfter running the initial CMake configuration, go to your TheRock build directory and run: Since these don't require LLVM, they build very quickly. 4. Set Environment Variables and CMake FlagsSet your ROCM_PATH by activating your Python virtual environment and running: Set HIP_COMMON_DIR: Set HIPCC_BIN_DIR: Finally, add these extra flags to your CMake configuration command to point to the libraries you just built: (If necessary, make "-DAMD_COMPUTE_WIN" an absolute path as well.) 5. The "Missing Something?" MysteryOne thing to note: my resulting amdhip64_7.dll was slightly smaller than the original one... so something might be missing. If you dig deep into [Your_TheRock_Dir]/build/core/clr/_init.cmake, you might find the answer. But honestly, it works fine for me, so I’m not too worried about it. SummaryAfter completing these fixes and following the build instructions in clr's README.md, you should successfully complete the build process. If all else fails, just stick to the standard hip-clr+build target and save yourself the headache. Appendix B: Building TheRock on WindowsA few more heads-up for those attempting to build TheRock on Windows. 1. Watch out for "Ninja Ninjas" hiding in your PATHIt’s not just the ninja.exe you installed via winget. There are others lurking around—inside Strawberry Perl, or even within the python virtual environment inside the pytorch build directory. The real headache is that these different instances often have slightly different versions. When you build pytorch, they're present in the python virtual environment inside the pytorch directory. To avoid trouble, make sure your PATH is configured so that the latest WinGet version takes precedence. For example: or I actually ran into a build failure with aotriton-enabled PyTorch because I failed to set this correctly. 2. Multi-byte character issuesI previously encountered a build failure in TheRock caused by multi-byte character issues related to tar in CMake. This should be resolved if you use the CMake version released in January 2026. (Looking back, it’s possible that my previous Ninja issue was also caused by using a version that didn't handle multi-byte characters properly.) 3. A note on ccacheBased on recent issues, it seems like ccache might be behaving better now, but I haven't tried it yet. |
|
In llama-server v0.7.0, even when using large models that exceed VRAM capacity with "-ngl 99", warnings appear but the operation works as intended. |
Uh oh!
There was an error while loading. Please reload this page.
Environment
GPU: Radeon RX 9060 XT 8GB, CPU: Ryzen 7600, RAM: 48GB
OS/Software: Windows 11 (Build 26200.9457) / therock10.0 / llama.cpp v0.4.1 / Adrenalin 26.8.1 (26.10.41.01)
Summary
When running the Qwen3.8-27b (UD-Q3_K_XL quantization) via llama.cpp, the performance was stuck at around 3–4 tok/s. However, by modifying clr to enable memory allocation to the Shared GPU Memory, I was able to boost the speed to over 10 tok/s.
Motivation
I wanted to use Qwen3.8-27b, but with 8GB of VRAM, the performance was disappointing (only 3–4 tok/s). I noticed that the CPU load was extremely high, while the GPU was only being utilized at about 10–20%. I suspected that llama.cpp was offloading the portions of the model that didn't fit in VRAM to the CPU. I thought that if I could find a way to make the GPU access data in system memory directly as well, the speed would improve significantly.
Trial and Error & Technical Observations
1. hipMalloc and Memory Management on Windows
According to the ihipMalloc function in clr/hipamd/src/hip_memory.cpp, it appears that on Windows, memory allocation is possible up to the maximum amount of system RAM (the value returned by ullTotalPhys via the GlobalMemoryStatusEx function), in addition to the dedicated VRAM. I previously monitored the memory allocation during VAE processing in a certain image generation program and observed it momentarily spiking to nearly 11GB (exceeding the 8GB VRAM limit). Furthermore, when running multiple llama-server instances, they initially allocate to the dedicated GPU memory, but once that capacity is reached, they begin to utilize the Shared GPU Memory.
Memory allocation in Windows seems to be both flexible and somewhat ambiguous. This is likely because WDDM (Windows Display Driver Model) manages resource distribution to allow multiple applications to share the GPU. While HIP's VMM feature is not available on Windows, memory appears to be managed in a way that feels similar to virtual memory management by default (likely involving WDDM), though this lacks fine-grained control.
(Note: When usage exceeds the dedicated VRAM capacity, driver stability may be an issue. It is highly recommended to use at least Adrenalin version 26.1.1 or later. It seems that the page-in/page-out mechanisms between VRAM and system RAM have changed since the 2026 updates(?) )
2. llama.cpp memory allocation logic
In llama.cpp/ggml/src/ggml-cuda/ggml-cuda.cu, the function ggml_backend_cuda_device_get_memory calls cudaMemGetInfo (which is replaced by hipMemGetInfo in this environment) to retrieve the available memory. It appears that the system uses this value to allocate memory in a way that attempts to fit everything within the reported VRAM limits.
I also observed that when running llama-server with a model larger than the VRAM capacity while setting the environment variable REMOTE_ALLOC=1, the system switches from dedicated GPU memory to Shared GPU Memory. However, the allocation was still capped at the 8GB VRAM limit; while it functioned correctly (albeit slowly, due to relying on system RAM), the reported usage didn't expand beyond the physical VRAM size. This happens because hipMemGetInfo calls globalFreeMemory in clr/rocclr/device/pal/paldevice.cpp to calculate available space. This function calculates free memory by subtracting the memory already allocated by clr from the total VRAM capacity. Even with REMOTE_ALLOC=1, it continues to subtract from the total VRAM, which explains this behavior.
This led me to a key realization: "What if I could tweak the value returned by this function?"
Implementation Strategy
Based on these observations, my strategy was to add the available Shared GPU Memory capacity to the reported VRAM free memory. This effectively tricks llama.cpp into perceiving the combined capacity (Dedicated + Shared) as if it were one large pool of VRAM.
However, since allowing the full allocation of Shared GPU Memory could potentially cause system instability or other issues, I implemented a safeguard. I added an option to set an upper limit for the additional Shared GPU Memory via an environment variable. (Setting this value to 0 will revert the behavior to the original state.)
The environment variable is: WIN_PAL_MEMFREE_ADD_SHARE_GPU_MEM_LIMIT
Implementation
Below are the git diff snippets for the two modified files.
Results & Observations
Configuration
I used the following settings for llama-server:
--temp 0.7 --top-p 0.8 --top-k 20 --min-p 0.0 --presence-penalty 0.5 --frequency-penalty 1.0 --spec-type draft-mtp -np 1 --spec-draft-device ROCm0 -c 32768
I utilized speculative decoding with MTP. I experimented with various values for -b, -ub, and --spec-draft-n-max, but found that the default settings performed best (it seems --fit is now enabled by default).
The Shared GPU Memory limit was set to 16GB via the following environment variable:
WIN_PAL_MEMFREE_ADD_SHARE_GPU_MEM_LIMIT=16384
Performance & Hardware Metrics
As mentioned in the summary, the performance exceeded 10 tok/s (at a context length of approximately 2k). In this state, the CPU load was low (around 10%), while the GPU load reached approximately 95%.
According to HWiNFO, the DRAM read speed was around 40 GB/s and the write speed was in the 4 GB/s range. This performance is likely enabled by the combination of DDR5 RAM and a PCIe 5.0 x16 interface. (For comparison, my benchmark for HIP-Basic/bandwidth in rocm-examples showed approximately 55 GB/s for host-to-device transfer using "pinned" memory with a 256MB payload.) While this is significantly slower than dedicated VRAM bandwidth, the GPU utilization remains remarkably high, which I suspect is due to the efficiency of Flash-Attention.
I also observed that utilizing Shared GPU memory tends to slightly increase the overall system RAM usage.
Additional Observations
Gemma4-26b-a4b-it:
I also tested this model with MTP and Q4_K_XL quantization. The performance trends were somewhat complex and harder to generalize. Since the active parameters already fit within the VRAM, the speed was not particularly slow even without the modification. However, I did confirm the trend that increasing the Shared GPU memory allocation results in higher GPU load and lower CPU load.
Long Context Test (Qwen3.8-27b):
I tested Qwen3.8-27b with a higher quantization (Q4_K_S) and a longer context of 64k (-c 65536), using --cache-type-v q8_0 --cache-type-k q8_0.
Initially, the speed was in the 9 tok/s range, but it dropped to approximately 6.7 tok/s as the context length reached around 32k. Interestingly, after adjusting the --presence-penalty to 1.5 and continuing the session, the speed recovered to the 8 tok/s range.
Reflections & Final Thoughts
My main takeaway from this experiment is that in a Windows + llama.cpp environment, utilizing Shared GPU Memory can provide a significant performance boost when running dense models that exceed the dedicated VRAM capacity. This effectively mitigates the massive performance drop that occurs when the workload is forced onto the CPU.
I suspect this effect might be more pronounced on lower-end Radeon cards, where the ratio of compute power to bus bandwidth is relatively small. Conversely, on higher-end cards like the 9070XT—which features high compute capability and a large gap between VRAM bandwidth and bus bandwidth—the impact might be less significant. However, even with a 16GB VRAM card (such as an RX 9060 XT 16GB), it is definitely worth experimenting with this method if your model overflows by 2–3GB.
Please note, however, that this approach may not be necessary for software that already implements aggressive prefetching from system RAM to VRAM.
On a more personal note, since the generation speed was relatively low, I had a unique and fascinating opportunity to observe the model's "reasoning" process in real-time. Watching it output tokens like "Hmm..." or "Wait..." as it processed thoughts was a very interesting experience.
Since the modifications are quite minimal, I highly encourage anyone capable of building the project to give it a try!
All reactions