Skip to content

sycl: make --fit respect --fit-target better - #27629

Merged
Titaniumtown merged 1 commit into
ggml-org:masterfrom
nicois:pr-sycl-fa-reserve
Aug 29, 2026
Merged

sycl: make --fit respect --fit-target better#27629
Titaniumtown merged 1 commit into
ggml-org:masterfrom
nicois:pr-sycl-fa-reserve

Conversation

@nicois

@nicois nicois commented Aug 23, 2026

Copy link
Copy Markdown
Contributor

Overview

Make --fit work properly on SYCL, so --fit-target 1 is viable

Additional information

There are two problems which make --fit not work as well as it should for SYCL devices:

  • it doesn't properly account for how much extra VRAM will be needed when the KV cache is fully populated
  • it is too pessimistic about how much VRAM is actually required for other reasons.

This means that SYNC users have to spend a long time adjusting ---fit-target to find a value large enough that it doesn't OOM when all the context is used.

This is a bigger problem for users who can fit all their KV cache onto their GPU, like me with a Arc b70 running unsloth's qwen 3.8. If only 12Gb of VRAM is available, most of the KV cache is in normal RAM anyway, so the under-estimate is less obvious.

With this PR applied, you should be able to run with --fit-target 1 and not have it OOM. The maximum context size might be a little lower than --fit calculated previously, but this time it's calculated correctly and won't crash mid-run.

To properly log memory allocations and deallocations I used a sibling PR, but as this is my first contribution I'll keep the other PR in draft until this is merged.

Requirements

  • I have read and agree with the contributing guidelines
  • AI usage disclosure: yes: I used Claude Opus for most of the heavy lifting here, and have reviewed what it's produced to the best of my ability - but I am still learning how this all works, so this certainly needs more competent review before it can be merged. I've tried to undo most of claude's excessive inline comments but have left a few here which I found helpful.

@nicois
nicois requested a review from a team as a code owner August 23, 2026 22:42
@github-actions github-actions Bot added ggml changes relating to the ggml tensor library for machine learning SYCL https://en.wikipedia.org/wiki/SYCL - GPU programming language labels Aug 23, 2026
@ggml-gh-bot

ggml-gh-bot Bot commented Aug 23, 2026

Copy link
Copy Markdown

Hi @nicois, thanks for your contribution!

Per our contribution guidelines, the automated PR checker found the following issue(s) that need your attention:

  • PR Template not respected: Please respect the template when creating a new pull request. Make sure to fill out all required sections.

Please note that maintainers reserve the right to make final decisions on PRs. If you believe there is a mistake, please comment below.

@ggml-gh-bot ggml-gh-bot Bot added the draft PR will be changed to draft by github-actions bot label Aug 23, 2026
@github-actions
github-actions Bot marked this pull request as draft August 23, 2026 22:47
@github-actions github-actions Bot removed the draft PR will be changed to draft by github-actions bot label Aug 23, 2026
@nicois
nicois force-pushed the pr-sycl-fa-reserve branch from 399aaf8 to 7b06245 Compare August 23, 2026 22:58
@github-actions github-actions Bot added the documentation Improvements or additions to documentation label Aug 23, 2026
@nicois
nicois marked this pull request as ready for review August 24, 2026 06:07
Comment thread docs/backend/SYCL.md Outdated
Comment thread ggml/src/ggml-sycl/fattn-common.hpp Outdated
Comment thread ggml/src/ggml-sycl/fattn-onednn.cpp Outdated
Comment thread ggml/src/ggml-sycl/fattn.cpp Outdated
Comment thread ggml/src/ggml-sycl/fattn.cpp Outdated
Comment thread ggml/src/ggml-sycl/fattn.cpp Outdated
Comment thread ggml/src/ggml-sycl/fattn.hpp
@NeoZhangJianyu

Copy link
Copy Markdown
Contributor

It's good job!
Small comments to make the code be graceful and easy to understand.

Thank you!

@nicois
nicois force-pushed the pr-sycl-fa-reserve branch 2 times, most recently from 7b425d3 to c5c243f Compare August 24, 2026 23:26
@nicois

nicois commented Aug 24, 2026

Copy link
Copy Markdown
Contributor Author

Thanks for that feedback. I believe I've incorporated all of it.

Comment thread ggml/src/ggml-sycl/fattn-onednn.hpp Outdated
Comment thread ggml/src/ggml-sycl/fattn.hpp Outdated
Comment thread ggml/src/ggml-sycl/ggml-sycl.cpp Outdated
Comment thread ggml/src/ggml-sycl/ggml-sycl.cpp
Comment thread ggml/src/ggml-sycl/fattn.cpp Outdated
Comment thread ggml/src/ggml-sycl/fattn-onednn.cpp
@Titaniumtown

Copy link
Copy Markdown
Contributor

@nicois thank you for your contribution! I like the changes and good thinking, I like the code in some areas too. Had some comments and attached those. Looking forward to seeing this get merged :)

@nicois

nicois commented Aug 25, 2026

Copy link
Copy Markdown
Contributor Author

Thanks for the further feedback. I'll definitely remove the verbosity and unslop what remains.

@Titaniumtown

Copy link
Copy Markdown
Contributor

@nicois No problem. I know it is very useful, and I think I tend to overuse sometimes too, but we (as-in people in general) have to think a bit about the code and how to describe it and not just rely on the AI comments. Human written comments tend to make more sense and have more character in my opinion.

@nicois
nicois force-pushed the pr-sycl-fa-reserve branch 3 times, most recently from d6d1c9a to 0a30dd4 Compare August 25, 2026 08:25

@arthw arthw left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

It's good job!

Thank you!

Comment thread ggml/src/ggml-sycl/fattn.cpp Outdated
Comment thread ggml/src/ggml-sycl/ggml-sycl.cpp
@Titaniumtown

Copy link
Copy Markdown
Contributor

@nicois Can you show it actually working? Like setting a fit-target before, and after your change, and showing vram usage and ctx size being different?

improve the --fit algorithm to take into account the actual peak
required VRAM for a given context size on a SYCL backend.

This includes both properly accounting for how much VRAM is required
when the allocated context is fully used (which makes the reported
context drop below what it did before, but stop it OOMing) as well
as preventing some overly-conservative calculations which meant too much
VRAM was being reserved.

Tested on a Arc b70 with unsloth's qwen3.8 (Q4_K_XL), able to get 262144 context,
fully usable, with q8_0 KV and MTP and 4k ubatch size using --fit-target 1
@nicois
nicois force-pushed the pr-sycl-fa-reserve branch from 0a30dd4 to 165bae3 Compare August 29, 2026 02:27
@nicois

nicois commented Aug 29, 2026

Copy link
Copy Markdown
Contributor Author

With this patch, on my Arc b70 (32Gb of VRAM), this is what I see.

First scenario (omitting args which don't affect memory usage, such as temperature etc.) is

llama-server --no-mmproj-auto --spec-draft-n-max 3 --spec-type draft-mtp --batch-size 8192 --cache-type-k q8_0 --cache-type-v q8_0 --flash-attn on --fit on --fit-target 1 --hf-repo unsloth/Qwen3.8-27B-GGUF:Q8_0 --ubatch-size 1024

which yields

srv    load_model: initializing, n_slots = 1, n_ctx_slot = 98560, kv_unified = 'false'

Using the unsloth/Qwen3.8-27B-GGUF:UD-Q4_K_XL model, I can increase --ubatch-size 2048 and still get maximal context:

srv    load_model: initializing, n_slots = 1, n_ctx_slot = 262144, kv_unified = 'false'

.. and this context is fully usable; I have filled it and not OOMed.

Using the unpatched version, with the Q8 model and 1024 ubatch size, llama-server reports

Aug 29 08:42:12 rainbow llama-b70-server[601154]: [57657] level_zero backend failed with error: 39 (UR_RESULT_ERROR_OUT_OF_DEVICE_MEMORY)Exception caught at file:/app/ggml/src/ggml-sycl/ggml-sycl.cpp, line:5428
Aug 29 08:42:12 rainbow llama-b70-server[601154]: [57657] Error OP MUL_MAT

with --fit-target 1.

With Q4 and 2048 ubatch it reports

srv    load_model: initializing, n_slots = 1, n_ctx_slot = 262144, kv_unified = 'false'

same as with the patch, but then crashes at around X tokens:

level_zero backend failed with error: 39 (UR_RESULT_ERROR_OUT_OF_DEVICE_MEMORY)

@Titaniumtown

Copy link
Copy Markdown
Contributor

Fantastic! Thank you <3

@Titaniumtown
Titaniumtown merged commit cc83d7b into ggml-org:master Aug 29, 2026
25 of 29 checks passed
nicois added a commit to nicois/llama.cpp that referenced this pull request Aug 30, 2026
Applies the approach merged in ggml-org#27629 to MUL_MAT's dequant/conversion scratch, which is
still allocated outside any ggml_backend_buffer and so invisible to --fit.

NOT PR-ready: the f32 and bf16 paths are uncovered. Motivation is measured -- a 27B Q8_0
at --fit-target 1 aborts during warmup on a 32656 MiB B70 with
UR_RESULT_ERROR_OUT_OF_DEVICE_MEMORY and "Error OP MUL_MAT".
nicois added a commit to nicois/llama.cpp that referenced this pull request Aug 30, 2026
Local working notes, not upstream material. Records the path to ggml-org#27595 and ggml-org#27629,
including the theories that were measured and ruled out first: the weight-reorder
scratch, the vision encoder, and async-allocation retention.
fewtarius pushed a commit to fewtarius/CachyLLama that referenced this pull request Sep 5, 2026
improve the --fit algorithm to take into account the actual peak
required VRAM for a given context size on a SYCL backend.

This includes both properly accounting for how much VRAM is required
when the allocated context is fully used (which makes the reported
context drop below what it did before, but stop it OOMing) as well
as preventing some overly-conservative calculations which meant too much
VRAM was being reserved.

Tested on a Arc b70 with unsloth's qwen3.8 (Q4_K_XL), able to get 262144 context,
fully usable, with q8_0 KV and MTP and 4k ubatch size using --fit-target 1
thecodacus pushed a commit to thecodacus/llama.cpp that referenced this pull request Sep 7, 2026
improve the --fit algorithm to take into account the actual peak
required VRAM for a given context size on a SYCL backend.

This includes both properly accounting for how much VRAM is required
when the allocated context is fully used (which makes the reported
context drop below what it did before, but stop it OOMing) as well
as preventing some overly-conservative calculations which meant too much
VRAM was being reserved.

Tested on a Arc b70 with unsloth's qwen3.8 (Q4_K_XL), able to get 262144 context,
fully usable, with q8_0 KV and MTP and 4k ubatch size using --fit-target 1
nicois added a commit to nicois/llama.cpp that referenced this pull request Sep 10, 2026
Applies the approach merged in ggml-org#27629 to MUL_MAT's dequant/conversion scratch, which is
still allocated outside any ggml_backend_buffer and so invisible to --fit.

NOT PR-ready: the f32 and bf16 paths are uncovered. Motivation is measured -- a 27B Q8_0
at --fit-target 1 aborts during warmup on a 32656 MiB B70 with
UR_RESULT_ERROR_OUT_OF_DEVICE_MEMORY and "Error OP MUL_MAT".
nicois added a commit to nicois/llama.cpp that referenced this pull request Sep 10, 2026
Local working notes, not upstream material. Records the path to ggml-org#27595 and ggml-org#27629,
including the theories that were measured and ruled out first: the weight-reorder
scratch, the vision encoder, and async-allocation retention.
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 ggml changes relating to the ggml tensor library for machine learning SYCL https://en.wikipedia.org/wiki/SYCL - GPU programming language

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants