llama: let the backend pick n_ubatch, and default to 2048 on RDNA3.5 - #77
Draft
roberteg16 wants to merge 1 commit into
Draft
llama: let the backend pick n_ubatch, and default to 2048 on RDNA3.5#77roberteg16 wants to merge 1 commit into
roberteg16 wants to merge 1 commit into
Conversation
The 512-token default physical batch predates these iGPUs. On gfx1151 a 512-token ubatch leaves the matrix cores short of work and re-reads the weights more times than necessary, so prompt processing gives up throughput on some architectures while gaining it on others. Rather than hardcode a number, add a way for a backend to state a preference. Adds LLAMA_N_UBATCH_AUTO as the new llama_context_default_params() value for n_ubatch. When set, llama_context queries each device for an optional "ggml_backend_get_optimal_ubatch" proc address; devices with no preference return 0 and are ignored, and if several disagree the smallest preference wins. Anything other than LLAMA_N_UBATCH_AUTO is passed through untouched, so an explicit -ub 512 still means 512. The CUDA/HIP backend returns 2048 for RDNA3.5 and 0 elsewhere, so no other architecture changes behaviour. llama-bench now reports the resolved value via llama_n_ubatch() instead of the requested one, since the request may be "auto". Measured on gfx1151 (Radeon 8060S), Q4_K_M, pp2048, -r 30 on an idle GPU. This is not a uniform win and the default is chosen on the strength of the MoE cases: GLM-4.7-Flash 1013 -> 1132 +11.7% gemma-4-26B-A4B-it 1527 -> 1668 +9.3% Qwen2.5-7B-Instruct 1541 -> 1588 +3.1% Qwen2.5-0.5B-Instruct 15606 -> 16021 +2.7% gemma-2b-it 4317 -> 4384 +1.6% gemma-3-12b-it 857 -> 866 +1.0% Qwen2.5-3B-Instruct 3218 -> 3230 +0.4% Llama-3.1-8B-Instruct 1459 -> 1422 -2.5% Qwen3.6-27B 386 -> 379 -1.8% Qwen3-8B 1433 -> 1389 -3.0% Qwen3-4B 2447 -> 2353 -3.9% Qwen3-1.7B 5410 -> 5125 -5.3% Qwen3.5-0.8B 9206 -> 8097 -12.1% Across 20 model/repo combinations: 5 improve by more than 1%, 4 are flat, and 11 regress by more than 1%. Median -1.8%, mean -0.8%. The large wins are concentrated in MoE models and the largest regressions in small dense models, where a 2048-token ubatch is a substantial fraction of the whole prompt.
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.
Summary
The 512-token default physical batch predates these iGPUs. On gfx1151 a 512-token ubatch leaves the matrix cores short of work and re-reads the weights more times than necessary. Rather than hardcode a new number, this adds a way for a backend to state a preference.
LLAMA_N_UBATCH_AUTO, the newllama_context_default_params()value forn_ubatch. When set,llama_contextqueries each device for an optionalggml_backend_get_optimal_ubatchproc address. Devices with no preference return 0 and are ignored; if several disagree the smallest preference wins.LLAMA_N_UBATCH_AUTOis passed through untouched, so an explicit-ub 512still means 512.llama-benchnow reports the resolved value viallama_n_ubatch()rather than the requested one, since the request may be "auto".Measured impact
gfx1151 (Radeon 8060S), Q4_K_M,
-ngl 999 -d 0 -p 2048 -n 0. Both delta columns are against the same reference: today's default of-ub 512withGGML_CUDA_GDN_CHUNKEDunset.-ub 2048— what this PR does on its own.-ub 2048+ CUDA: fused chunked gated_delta_net kernel (RDNA3.5) #54 — this PR together with the fused gated-delta-net kernel from CUDA: fused chunked gated_delta_net kernel (RDNA3.5) #54 enabled (GGML_CUDA_GDN_CHUNKED=1).ᵈ marks rows where both columns are the same run: #54's kernel only applies to gated-delta-net models.
*marks rows still >=3% relative stddev after re-measurement at-r 30.-ub 512)-ub 2048-ub 2048+ #54-ub 2048-ub 2048+ #54Only the four gated-delta-net rows differ between the two columns:
-ub 2048-ub 2048+ #54GDN's sequential scan gets slower as the ubatch grows, because the scan's serial length grows
with
n_tokens— so a larger ubatch is actively wrong for those models today. #54's chunkedkernel removes that dependency, which is what turns three of those four rows positive.
Caveat on the second column
Merging #54 is not by itself sufficient. Its kernel is opt-in (
GGML_CUDA_GDN_CHUNKED=1; unset means off, seeggml_cuda_gdn_chunked_supported), so the+ #54column describes a configuration, not the merge alone. The two changes are complementary and arguably should land together, or #54 should default its kernel on for RDNA3.5.Qwen3.5-0.8B stays negative either way. At ~9k tok/s a 2048-token ubatch is most of the prompt, so there is little left to overlap.
Test plan
n_ubatch = 2048on gfx1151; explicit-ub 512/-ub 1024/-ub 256are preservedn_ctxclamping still applies (-p 512yieldsn_ubatch = 512)