Skip to content

llama: fix early stop in params_fit if ctx is set - #18070

Merged
JohannesGaessler merged 1 commit into
ggml-org:masterfrom
JohannesGaessler:llama-fp-fix-ctx
Dec 16, 2025
Merged

JohannesGaessler merged 1 commit into
ggml-org:masterfrom
JohannesGaessler:llama-fp-fix-ctx

Conversation

@JohannesGaessler

Copy link
Copy Markdown
Contributor

Should at least partially fix #18066 .

The early return on context reduction is done too often, in particular it should never be done if the user is explicitly setting a context size.

For a single GPU we can always safely return if we can fit the model via context reduction. However, for multiple GPUs however this is unsafe so we should always go on to fit.

@pwilkin

pwilkin commented Dec 15, 2025

Copy link
Copy Markdown
Member

Also I don't know if this is relevant, but I've had a bug with the autofit of "barely fitting", i.e. the model did fit upon loading, but would crash with "CUDA out of memory error" upon processing the first or second prompt.

@verygreen

Copy link
Copy Markdown

This seems to work for me when I specify the context manually:

$ ./build/bin/llama-fit-params -m /usr/local/ai/models/GLM4.6/UD-Q4_K_XL/GLM-4.6-UD-Q4_K_XL-00001-of-00005.gguf  -c 131072
ggml_cuda_init: GGML_CUDA_FORCE_MMQ:    no
ggml_cuda_init: GGML_CUDA_FORCE_CUBLAS: no
ggml_cuda_init: found 5 CUDA devices:
  Device 0: NVIDIA GeForce RTX 4090, compute capability 8.9, VMM: yes
  Device 1: NVIDIA GeForce RTX 4090, compute capability 8.9, VMM: yes
  Device 2: NVIDIA GeForce RTX 4090, compute capability 8.9, VMM: yes
  Device 3: NVIDIA RTX PRO 6000 Blackwell Workstation Edition, compute capability 12.0, VMM: yes
  Device 4: NVIDIA RTX PRO 6000 Blackwell Workstation Edition, compute capability 12.0, VMM: yes
build: 7417 (be95a9cb9) with GNU 15.2.1 for Linux x86_64
llama_params_fit_impl: projected memory use with initial parameters [MiB]:
llama_params_fit_impl:   - CUDA0 (NVIDIA GeForce RTX 4090)                          :  24082 total,  18485 used,   5201 surplus
llama_params_fit_impl:   - CUDA1 (NVIDIA GeForce RTX 4090)                          :  24082 total,  21324 used,   2363 surplus
llama_params_fit_impl:   - CUDA2 (NVIDIA GeForce RTX 4090)                          :  24082 total,  24255 used,    568 deficit
llama_params_fit_impl:   - CUDA3 (NVIDIA RTX PRO 6000 Blackwell Workstation Edition):  97250 total,  90678 used,   6009 surplus
llama_params_fit_impl:   - CUDA4 (NVIDIA RTX PRO 6000 Blackwell Workstation Edition):  97250 total,  85725 used,  10963 surplus
llama_params_fit_impl: projected to use 240470 MiB of device memory vs. 266748 MiB of free device memory
llama_params_fit_impl: with only dense weights in device memory there is a total surplus of 200251 MiB
llama_params_fit_impl: filling dense-only layers back-to-front:
llama_params_fit_impl:   - CUDA4 (NVIDIA RTX PRO 6000 Blackwell Workstation Edition): 93 layers,      0 MiB used,  96688 MiB free
llama_params_fit_impl:   - CUDA3 (NVIDIA RTX PRO 6000 Blackwell Workstation Edition):  0 layers,      0 MiB used,  96688 MiB free
llama_params_fit_impl:   - CUDA2 (NVIDIA GeForce RTX 4090)                          :  0 layers,      0 MiB used,  23687 MiB free
llama_params_fit_impl:   - CUDA1 (NVIDIA GeForce RTX 4090)                          :  0 layers,      0 MiB used,  23687 MiB free
llama_params_fit_impl:   - CUDA0 (NVIDIA GeForce RTX 4090)                          :  0 layers,   1765 MiB used,  21922 MiB free
llama_params_fit_impl: converting dense-only layers to full layers and filling them front-to-back with overflow to next device/system memory:
llama_params_fit_impl:   - CUDA0 (NVIDIA GeForce RTX 4090)                          : 10 layers ( 0 overflowing),  22172 MiB used,   1515 MiB free
llama_params_fit_impl:   - CUDA1 (NVIDIA GeForce RTX 4090)                          :  9 layers ( 1 overflowing),  22588 MiB used,   1098 MiB free
llama_params_fit_impl:   - CUDA2 (NVIDIA GeForce RTX 4090)                          :  8 layers ( 1 overflowing),  22334 MiB used,   1352 MiB free
llama_params_fit_impl:   - CUDA3 (NVIDIA RTX PRO 6000 Blackwell Workstation Edition): 36 layers ( 1 overflowing),  95239 MiB used,   1449 MiB free
llama_params_fit_impl:   - CUDA4 (NVIDIA RTX PRO 6000 Blackwell Workstation Edition): 30 layers ( 0 overflowing),  79208 MiB used,  17480 MiB free
llama_params_fit: successfully fit params to free device memory
llama_params_fit: fitting params to free memory took 13.79 seconds
Printing fitted CLI arguments to stdout...
-c 131072 -ngl 94 -ts 10,9,8,36,31 -ot blk\.18\.ffn_(gate|down).*=CUDA2,blk\.26\.ffn_down.*=CUDA3,blk\.62\.ffn_(gate|down).*=CUDA4

(would it be too much to ask to include single quotes around the arguments so one can just copy-paste them witohut any need to hunt and insert the quotes manually?)

Without -c specified the detected suggested reduced context size still does not avoid the cpu hit seemingly:

$ ./build/bin/llama-fit-params -m /usr/local/ai/models/GLM4.6/UD-Q4_K_XL/GLM-4.6-UD-Q4_K_XL-00001-of-00005.gguf 
ggml_cuda_init: GGML_CUDA_FORCE_MMQ:    no
ggml_cuda_init: GGML_CUDA_FORCE_CUBLAS: no
ggml_cuda_init: found 5 CUDA devices:
  Device 0: NVIDIA GeForce RTX 4090, compute capability 8.9, VMM: yes
  Device 1: NVIDIA GeForce RTX 4090, compute capability 8.9, VMM: yes
  Device 2: NVIDIA GeForce RTX 4090, compute capability 8.9, VMM: yes
  Device 3: NVIDIA RTX PRO 6000 Blackwell Workstation Edition, compute capability 12.0, VMM: yes
  Device 4: NVIDIA RTX PRO 6000 Blackwell Workstation Edition, compute capability 12.0, VMM: yes
build: 7417 (be95a9cb9) with GNU 15.2.1 for Linux x86_64
llama_params_fit_impl: projected memory use with initial parameters [MiB]:
llama_params_fit_impl:   - CUDA0 (NVIDIA GeForce RTX 4090)                          :  24082 total,  21215 used,   2471 surplus
llama_params_fit_impl:   - CUDA1 (NVIDIA GeForce RTX 4090)                          :  24082 total,  23634 used,     53 surplus
llama_params_fit_impl:   - CUDA2 (NVIDIA GeForce RTX 4090)                          :  24082 total,  26845 used,   3158 deficit
llama_params_fit_impl:   - CUDA3 (NVIDIA RTX PRO 6000 Blackwell Workstation Edition):  97250 total, 100268 used,   3580 deficit
llama_params_fit_impl:   - CUDA4 (NVIDIA RTX PRO 6000 Blackwell Workstation Edition):  97250 total,  94730 used,   1958 surplus
llama_params_fit_impl: projected to use 266695 MiB of device memory vs. 266748 MiB of free device memory
llama_params_fit_impl: cannot fulfill margin of 1024 MiB on all devices, need to use 7375 MiB less in total
llama_params_fit_impl: context size reduced from 202752 to 182230 -> need 7375 MiB less memory in total
llama_params_fit_impl: entire model should be fit across devices by reducing context
llama_params_fit_impl: with only dense weights in device memory there is a total surplus of 181701 MiB
llama_params_fit_impl: filling dense-only layers back-to-front:
llama_params_fit_impl:   - CUDA4 (NVIDIA RTX PRO 6000 Blackwell Workstation Edition): 93 layers,      0 MiB used,  96688 MiB free
llama_params_fit_impl:   - CUDA3 (NVIDIA RTX PRO 6000 Blackwell Workstation Edition):  0 layers,      0 MiB used,  96688 MiB free
llama_params_fit_impl:   - CUDA2 (NVIDIA GeForce RTX 4090)                          :  0 layers,      0 MiB used,  23687 MiB free
llama_params_fit_impl:   - CUDA1 (NVIDIA GeForce RTX 4090)                          :  0 layers,      0 MiB used,  23687 MiB free
llama_params_fit_impl:   - CUDA0 (NVIDIA GeForce RTX 4090)                          :  0 layers,   2115 MiB used,  21572 MiB free
llama_params_fit_impl: converting dense-only layers to full layers and filling them front-to-back with overflow to next device/system memory:
llama_params_fit_impl:   - CUDA0 (NVIDIA GeForce RTX 4090)                          : 10 layers ( 1 overflowing),  22129 MiB used,   1558 MiB free
llama_params_fit_impl:   - CUDA1 (NVIDIA GeForce RTX 4090)                          :  7 layers ( 0 overflowing),  22198 MiB used,   1489 MiB free
llama_params_fit_impl:   - CUDA2 (NVIDIA GeForce RTX 4090)                          :  8 layers ( 1 overflowing),  22445 MiB used,   1242 MiB free
llama_params_fit_impl:   - CUDA3 (NVIDIA RTX PRO 6000 Blackwell Workstation Edition): 33 layers ( 0 overflowing),  95505 MiB used,   1183 MiB free
llama_params_fit_impl:   - CUDA4 (NVIDIA RTX PRO 6000 Blackwell Workstation Edition): 35 layers ( 2 overflowing),  95642 MiB used,   1045 MiB free
llama_params_fit: successfully fit params to free device memory
llama_params_fit: fitting params to free memory took 14.57 seconds
Printing fitted CLI arguments to stdout...
-c 182230 -ngl 94 -ts 10,7,8,33,36 -ot blk\.9\.ffn_(up|gate|down).*=CUDA1,blk\.24\.ffn_down.*=CUDA3,blk\.91\.ffn_(up|gate|down).*=CPU,blk\.92\.ffn_(up|down|gate)_(ch|)exps=CPU

But it's a massive improvement none-the-less. Thank you.

@JohannesGaessler

Copy link
Copy Markdown
Contributor Author

@pwilkin what could be happening is that the CUDA pool buffers are growing beyond the margin you're setting.

@pwilkin

pwilkin commented Dec 15, 2025

Copy link
Copy Markdown
Member

@JohannesGaessler the thing is, I wasn't setting any buffers. I didn't realize the change was already merged, so I just ran the server without any extra params and I got this behavior. So just indicating that the default settings might be a bit too aggressive.

@verygreen

Copy link
Copy Markdown

I tried on a Devstral model and ugh, I don't think this output makes sense?

$ ./build/bin/llama-fit-params -m /usr/local/ai/models/Devstral/UD-Q8_K_XL/Devstral-2-123B-Instruct-2512-UD-Q8_K_XL-00001-of-00003.gguf 
ggml_cuda_init: GGML_CUDA_FORCE_MMQ:    no
ggml_cuda_init: GGML_CUDA_FORCE_CUBLAS: no
ggml_cuda_init: found 5 CUDA devices:
  Device 0: NVIDIA GeForce RTX 4090, compute capability 8.9, VMM: yes
  Device 1: NVIDIA GeForce RTX 4090, compute capability 8.9, VMM: yes
  Device 2: NVIDIA GeForce RTX 4090, compute capability 8.9, VMM: yes
  Device 3: NVIDIA RTX PRO 6000 Blackwell Workstation Edition, compute capability 12.0, VMM: yes
  Device 4: NVIDIA RTX PRO 6000 Blackwell Workstation Edition, compute capability 12.0, VMM: yes
build: 7417 (be95a9cb9) with GNU 15.2.1 for Linux x86_64
llama_params_fit_impl: projected memory use with initial parameters [MiB]:
llama_params_fit_impl:   - CUDA0 (NVIDIA GeForce RTX 4090)                          :  24082 total,  23640 used,     47 surplus
llama_params_fit_impl:   - CUDA1 (NVIDIA GeForce RTX 4090)                          :  24082 total,  19974 used,   3712 surplus
llama_params_fit_impl:   - CUDA2 (NVIDIA GeForce RTX 4090)                          :  24082 total,  19974 used,   3712 surplus
llama_params_fit_impl:   - CUDA3 (NVIDIA RTX PRO 6000 Blackwell Workstation Edition):  97250 total,  80920 used,  15767 surplus
llama_params_fit_impl:   - CUDA4 (NVIDIA RTX PRO 6000 Blackwell Workstation Edition):  97250 total,  84573 used,  12115 surplus
llama_params_fit_impl: projected to use 229084 MiB of device memory vs. 266748 MiB of free device memory
llama_params_fit_impl: filling dense layers back-to-front:
llama_params_fit_impl:   - CUDA4 (NVIDIA RTX PRO 6000 Blackwell Workstation Edition): 36 layers,  93690 MiB used,   2998 MiB free
llama_params_fit_impl:   - CUDA3 (NVIDIA RTX PRO 6000 Blackwell Workstation Edition): 39 layers,  95547 MiB used,   1140 MiB free
llama_params_fit_impl:   - CUDA2 (NVIDIA GeForce RTX 4090)                          : 4294967227 layers,  20555 MiB used,   3132 MiB free
llama_params_fit_impl:   - CUDA1 (NVIDIA GeForce RTX 4090)                          : 362388906 layers,      0 MiB used,  23687 MiB free
llama_params_fit_impl:   - CUDA0 (NVIDIA GeForce RTX 4090)                          :  0 layers,   3352 MiB used,  20335 MiB free
llama_params_fit: successfully fit params to free device memory
llama_params_fit: fitting params to free memory took 22.80 seconds
Printing fitted CLI arguments to stdout...
-c 0 -ngl 362388912 -ts 0,3.62389e+08,4.29497e+09,39,36

if I try to use this command line, it's accepted, but the load fails:

$ ./build/bin/llama-cli -m /usr/local/ai/models/Devstral/UD-Q8_K_XL/Devstral-2-123B-Instruct-2512-UD-Q8_K_XL-00001-of-00003.gguf -c 0 -ngl 362388912 -ts 0,3.62389e+08,4.29497e+09,39,36
ggml_cuda_init: GGML_CUDA_FORCE_MMQ:    no
ggml_cuda_init: GGML_CUDA_FORCE_CUBLAS: no
ggml_cuda_init: found 5 CUDA devices:
  Device 0: NVIDIA GeForce RTX 4090, compute capability 8.9, VMM: yes
  Device 1: NVIDIA GeForce RTX 4090, compute capability 8.9, VMM: yes
  Device 2: NVIDIA GeForce RTX 4090, compute capability 8.9, VMM: yes
  Device 3: NVIDIA RTX PRO 6000 Blackwell Workstation Edition, compute capability 12.0, VMM: yes
  Device 4: NVIDIA RTX PRO 6000 Blackwell Workstation Edition, compute capability 12.0, VMM: yes

Loading model... /ggml_backend_cuda_buffer_type_alloc_buffer: allocating 123049.64 MiB on device 2: cudaMalloc failed: out of memory
alloc_tensor_range: failed to allocate CUDA2 buffer of size 129026899968
|llama_model_load: error loading model: unable to allocate CUDA2 buffer
llama_model_load_from_file_impl: failed to load model
common_init_from_params: failed to load model '/usr/local/ai/models/Devstral/UD-Q8_K_XL/Devstral-2-123B-Instruct-2512-UD-Q8_K_XL-00001-of-00003.gguf'
srv    load_model: failed to load model, '/usr/local/ai/models/Devstral/UD-Q8_K_XL/Devstral-2-123B-Instruct-2512-UD-Q8_K_XL-00001-of-00003.gguf'
 
Failed to load the model

@Beinsezii

Copy link
Copy Markdown
Contributor

Also I don't know if this is relevant, but I've had a bug with the autofit of "barely fitting", i.e. the model did fit upon loading, but would crash with "CUDA out of memory error" upon processing the first or second prompt.

@pwilkin what could be happening is that the CUDA pool buffers are growing beyond the margin you're setting.

I also noticed the ability to meet a --fit-target can vary by model and backend. Using -fitt 2048 on vulkan, Mistral Small 3.2 works perfectly @ 21.9GiB read from /sys/class/drm/card1/device/mem_info_vram_used but for Gemma 3 it allocates 23.4GiB. Mistral 3.2 on ROCm 7 is up to 22.5GiB before sending a single prompt, 22.9GiB after sending a prompt and acquiring the extra cublas buffer.

@JohannesGaessler

Copy link
Copy Markdown
Contributor Author

@verygreen please open a new issue if you find a new bug. That makes keeping track of them much easier from my end.

@JohannesGaessler JohannesGaessler changed the title llama: fix context handling in params_fit llama: fix early stop in params_fit if ctx is set Dec 16, 2025
@verygreen

verygreen commented Dec 16, 2025 •

Copy link
Copy Markdown

Dense models like desvstral suggested weird parameters is not a separate new issue, I don't get it without this PR (that was not yet merged) (without the PR I get same "this model fits, do nothing" messaging like in #18066)

@JohannesGaessler

Copy link
Copy Markdown
Contributor Author

@verygreen your output contains these lines:

llama_params_fit_impl: filling dense layers back-to-front:
llama_params_fit_impl:   - CUDA4 (NVIDIA RTX PRO 6000 Blackwell Workstation Edition): 36 layers,  93690 MiB used,   2998 MiB free
llama_params_fit_impl:   - CUDA3 (NVIDIA RTX PRO 6000 Blackwell Workstation Edition): 39 layers,  95547 MiB used,   1140 MiB free
llama_params_fit_impl:   - CUDA2 (NVIDIA GeForce RTX 4090)                          : 4294967227 layers,  20555 MiB used,   3132 MiB free
llama_params_fit_impl:   - CUDA1 (NVIDIA GeForce RTX 4090)                          : 362388906 layers,      0 MiB used,  23687 MiB free
llama_params_fit_impl:   - CUDA0 (NVIDIA GeForce RTX 4090)                          :  0 layers,   3352 MiB used,  20335 MiB free

What is happening here is that due to a bug there is a numerical underflow for the number of layers per GPU, which is why it's trying to assign over 4 billion layers to one of them. This is a bug separate to issues with context size. Please open an issue and post the console output you get, both with and without --verbose.

@JohannesGaessler
JohannesGaessler merged commit ec98e20 into ggml-org:master Dec 16, 2025
71 checks passed
Seunghhon pushed a commit to Seunghhon/llama.cpp that referenced this pull request Apr 26, 2026
my-other-github-account pushed a commit to my-other-github-account/llama.cpp that referenced this pull request May 15, 2026
fukuro-kun pushed a commit to fukuro-kun/fukuro-llama-cpp-turboquant that referenced this pull request Jul 5, 2026
MrLordCat referenced this pull request in MrLordCat/llama.cpp-rdna-lab Jul 16, 2026
zommiommy pushed a commit to zommiommy/llama.cpp that referenced this pull request Aug 18, 2026
frostyautumnleaf pushed a commit to frostyautumnleaf/llama.cpp that referenced this pull request Oct 5, 2026
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.

Misc. bug: llama-fit-params works unreliably for models that "fit" on GPUs (or so it thinks)

5 participants