Skip to content

common : --fit grows the ubatch of MoE models into spare VRAM, for up to 2.5x faster prompt processing - #321

Open
LeoBorcherding wants to merge 23 commits into
unslothai:base/upstream-a105b1924from
LeoBorcherding:fix/moe-ubatch-default
Open

LeoBorcherding wants to merge 23 commits into
unslothai:base/upstream-a105b1924from
LeoBorcherding:fix/moe-ubatch-default

Conversation

@LeoBorcherding

@LeoBorcherding LeoBorcherding commented Oct 23, 2026 •

Copy link
Copy Markdown

What this does

llama.cpp reads a prompt in chunks called ubatches. The default chunk is 512 tokens. For MoE models (Qwen3-30B-A3B, DeepSeek, Qwen3.8-Flash-Next and so on), every chunk has to run through all the experts' weights once. So a 4,600-token prompt means 10 trips through every expert. When the experts live in system RAM, each trip also copies them to the GPU again.

Bigger chunks mean fewer trips, but each chunk needs more memory while it runs. So this PR lets --fit decide:

  1. --fit places the weights and the context exactly as it does today, at 512.
  2. If there's memory left over, it doubles the chunk (512, 1024, 2048, up to n_batch, max 4096) as long as every device still keeps the fit's margin free and host RAM keeps 1 GiB free.
  3. It never gives up context or moves weights to RAM to make room. If there's no spare memory, nothing changes.

The log says when it kicks in: MoE model, n_ubatch = 2048 fits in free memory (set -ub or -fitu off to override).

When it stays at 512

The growth only runs where it is a measured win and its memory can be budgeted. Everything else keeps today's behaviour byte for byte:

  • Dense models (GGUF expert_count = 0).
  • Sliding-window models (gpt-oss, Gemma and similar). Their cache grows with the ubatch and every decode step reads it, so a bigger chunk trades decode speed for prompt speed. This is detected by measuring whether the context memory grows, not from file metadata.
  • CPU-only runs. The gain is fewer expert passes and uploads on a GPU.
  • Hosts where free RAM can't be read (macOS today), since the host buffers the growth adds could not be budgeted.
  • LoRA adapters (--lora) or a multimodal projector on the CPU (--no-mmproj-offload), which load into the memory the fit leaves.
  • llama-mtmd-cli, llama-mtmd-debug and llama-tts, which load the projector after the fit without reserving its memory.
  • An explicit ubatch: -ub / LLAMA_ARG_UBATCH, or a preset or tool that sets its own chunk size.
  • -fit off, or -fitu off (--fit-ubatch off, LLAMA_ARG_FIT_UBATCH=off) to turn off just the growth.
  • A draft model on any device the main model doesn't use, including the same GPU reached through a second backend (for example CUDA and Vulkan), since its memory is not checked there. A draft or MTP model sharing the main devices is measured together with it; an explicit --spec-draft-n-ubatch is kept.

Memory checks

Each doubling re-measures memory with the fit's own probe and is kept only if all of these hold:

  • Every device keeps its --fit-target margin free.
  • Host RAM keeps 1 GiB free after the host compute buffers, the context, repacked CPU weights (charged once when an MTP head shares them), and each model's weights unless that model is memory-mapped. On Linux the reading is MemAvailable, capped by any cgroup v1 or v2 memory limit up to the container root.
  • On integrated GPUs and shared-memory systems (APUs, DGX Spark, OpenCL on Adreno / Intel, Hexagon HTP, OpenVINO NPU / iGPU), including a draft's own device, host buffers count against the GPU's budget and the GPU's own buffers count against host RAM, since both come from the same memory.
  • A server waking from sleep refits from 512 rather than from the grown value.
  • The server router's memory estimate keeps its minimum at a 512 chunk (a separate refit), so admission and eviction never use the grown figure.

How this ships

The unslothai prebuilt is an upstream release tag plus the pins listed in scripts/unsloth/pr-set.json, merged in order. This PR is one pin: its diff is exactly one change on top of upstream b13004 (the PR's base branch, base/upstream-a105b1924), 12 files under common/, tests/, tools/mtmd/, tools/tts/ and the tool READMEs. It adds no fork-only code beyond that. Once upstream carries an equivalent change, the pin is dropped.

The change leaves the signature of the fit's memory probe (common_get_device_memory_data_impl) untouched: the CPU extra buffers it needs (repacked weights) are collected through a scoped collector set by the caller. Other pins that extend that probe, such as the MoE cache sizing in #251, therefore merge with this one without a conflict. Checked by running the nightly's merge (git diff3, then the additive add/add resolver) of this head on top of upstream b13007 plus the current pin set including #251: common/fit.cpp merges cleanly, common/common.cpp resolves as pure add/add, and every file this PR touches compiles in the merged tree.

Results

Prompt speed with and without the change

Prompt processing, same machine, with and without the change. These numbers were measured on an earlier version of this PR. Since then, sliding-window models (the gpt-oss rows) keep 512 and are listed separately below; the other rows are unaffected by the later scope limits.

machine model ubatch picked without with
Radeon AI PRO R9700, Windows 11, ROCm 7.1 Qwen3.8-Flash-Next IQ1_M, experts in RAM (-cmoe) 2048 248 tok/s 616 tok/s (2.5x)
L4 (23 GB), Linux, CUDA 13.0 Qwen3-30B-A3B Q3_K_M 2048 2,136 tok/s 2,971 tok/s (1.4x)
L4 Qwen3-30B-A3B Q4_K_M, card nearly full 512 (no room) 2,125 tok/s 2,123 tok/s, same context
L4 Qwen3-30B-A3B Q6_K, --fit spills experts 512 (no room) 723 tok/s 724 tok/s, same placement
L4 Qwen3-8B Q4_K_M (dense, control) 512 2,711 tok/s 2,693 tok/s (noise)
A100 40 GB, Linux, CUDA 13.0 Qwen3.8-Flash-Next IQ1_M, experts in RAM (-cmoe) 2048 107 tok/s 271 tok/s (2.5x)
A100 40 GB Qwen3-30B-A3B Q4_K_M, experts in RAM (-cmoe) 2048 164 tok/s 449 tok/s (2.7x)
A100 40 GB Qwen3-30B-A3B Q4_K_M, fully in VRAM 2048 3,560 tok/s 5,767 tok/s (1.6x)
RTX PRO 6000 Blackwell 96 GB, Linux, CUDA 13.0 Qwen3.8-Flash-Next IQ1_M, experts in RAM (-cmoe) 2048 556 tok/s 1,320 tok/s (2.4x)
RTX PRO 6000 Qwen3-30B-A3B Q4_K_M, experts in RAM (-cmoe) 2048 886 tok/s 2,318 tok/s (2.6x)
RTX PRO 6000 Qwen3-30B-A3B Q4_K_M, fully in VRAM 2048 9,366 tok/s 14,050 tok/s (1.5x)
A100 / RTX PRO 6000 Qwen3-8B dense (control) 512 4,494 / 12,586 tok/s 4,492 / 12,619 tok/s (same)
Radeon AI PRO R9700, Windows 11, ROCm 7.1 Qwen4-35B-A3B UD-Q4_K_XL, fully in VRAM (-ub set by hand, details) 2048 3,261 tok/s 3,848 tok/s (1.18x)
Radeon AI PRO R9700, Windows 11, ROCm 7.1 Qwen3.8-27B UD-Q4_K_M (dense, control) 512 392.5 tok/s 392.7 tok/s (same)
RX 5700 XT 8 GB, Windows 11, Vulkan Qwen3-30B-A3B Q4_K_M, experts in RAM (-cmoe) 2048 26.9 tok/s 42.9 tok/s (1.6x)
RX 5700 XT Qwen3-30B-A3B Q4_K_M, --fit spills experts 1024 (no room for 2048) 32.2 tok/s 40.2 tok/s (1.25x)
RX 6500 XT 4 GB, Windows 11, Vulkan Qwen3-30B-A3B Q4_K_M, experts in RAM (-cmoe) 2048 7.6 tok/s 45.0 tok/s (5.9x), mostly escaping a slow path this card hits at ubatch 512; -ub 1024 by hand does the same

Sliding-window models, measured before the scope limit and now left at 512 (use -ub 2048 by hand for these numbers): gpt-oss-20b on L4 2,751 to 3,441 tok/s, on A100 4,489 to 5,693 tok/s, on RTX PRO 6000 16,657 to 18,794 tok/s.

Generation speed doesn't change. What changes is how long you wait for the first word (4,565 prompt tokens divided by the prompt speeds above):

setup wait without wait with saved
NVIDIA A100, Flash-Next, experts in RAM 42.7 s 16.8 s 25.9 s
AMD R9700, Flash-Next, experts in RAM 18.4 s 7.4 s 11.0 s
NVIDIA RTX PRO 6000, Flash-Next, experts in RAM 8.2 s 3.5 s 4.8 s
NVIDIA A100, Qwen3-30B-A3B in VRAM 1.3 s 0.8 s 0.5 s
AMD R9700, Qwen3.8-27B (dense, not MoE) 11.6 s 11.6 s none, left alone

Accuracy

Perplexity on wikitext-2 (Qwen3.8-Flash-Next, experts in RAM): 5.365 at ubatch 512 and 5.370 at 2048, error bars ±0.08. This is the same tiny numeric difference you get today by passing -ub 2048 yourself. When the growth does not apply, the output matches the base exactly.

Where to look in the code

Tradeoffs

  • When the card is full or the model spills, there's no speedup. It stays at 512 rather than give up context or VRAM. -ub 2048 still works if you want the speed anyway.
  • Startup takes a moment longer for MoE models with spare memory. Each doubling re-measures memory, 0.25 to 0.5 s per step.

How it was tested

  • Prompt speed: warm prompts, two servers per build alternating, ~485, ~583 and ~4.2k-4.6k-token prompts, base vs this PR on the same base.
  • test-arg-parser covers -ub and -fitu; CPU build and tests pass at the current head; the pin set merges cleanly with this PR on b13004 / b13005.
  • The cgroup limit walk was checked against fake v1, v2, hybrid and unlimited cgroup trees.
  • Base vs head on one CUDA GPU (A100 40 GB, static builds of upstream b13004 and b13004 + this PR, 3 long prompts each, greedy):
case base head
gpt-oss-20b (sliding window) ubatch 512 512, same output, buffers and placement
gpt-oss-20b --swa-full 512 2048, output identical to base with -ub 2048
Qwen3-0.6B (dense) 512 512, same output and buffers
Qwen3-30B-A3B -cmoe 512 2048, output identical to base with -ub 2048; -fitu off and -ub 512 give base's 512 output
Qwen3-30B-A3B, --fit placement 512 2048, same weight placement and context, output identical to base with -ub 2048
Qwen3-30B-A3B Q4_K_M -cmoe 512 2048, output identical to base with -ub 2048; same with --no-repack
  • Repeated at the current head 37671861c on an L4 (static builds of the PR base and base + this PR, a check that only the head binary has --fit-ubatch): gpt-oss-20b and Qwen3-0.6B unchanged (512, same output and buffers); Qwen3-30B-A3B UD-Q2_K_XL with -cmoe and with --fit placement grow to 2048 with the same weight placement and output identical to base with -ub 2048; -fitu off and -ub 512 reproduce base.
  • Not covered by a test: Windows host RAM path end to end, the shared-memory GPU, Hexagon and OpenVINO paths, the MTP repacked-weight charge, the router estimate, the sleep / refit path, multi-GPU, Metal.

A MoE ubatch reads the weights of nearly every expert, and uploads them when
the experts are in host memory, so prompt processing pays one pass over all
experts per ubatch. Without -ub, MoE models now use the logical batch (up to
4096) instead of 512. -ub still overrides it.

With a draft or MTP model the larger default applies only when the tensor
overrides keep the experts in host memory. The draft context follows the main
context's ubatch, so both compute buffers grow, and with the whole model on the
GPU that can overrun VRAM.
@LeoBorcherding

Copy link
Copy Markdown
Author

Initial version (34765b3): fixed MoE default

Recording the first approach and its numbers here, because the body will change if the --fit-aware version being tested replaces it.

Approach: MoE models (GGUF expert_count > 0) with no -ub default n_ubatch to n_batch (2048, capped at 4096) instead of 512. This is set before --fit runs, so fit plans for the bigger compute buffer. An explicit -ub wins, and a draft/MTP model with the experts on the GPU stays at 512.

Prompt speed

Time per request

Accuracy

Results:

  • Prompt processing is 2.4x faster on an R9700 with experts in RAM, 2.3x on an L4 when --fit spills experts, and 1.2x for MoEs that fit in VRAM.
  • Dense models are unchanged, and so is generation speed.
  • Perplexity is 5.365 vs 5.370 (±0.08).

Why it's being reworked: a fixed default ignores how much VRAM is free. On a nearly full card:

  • --fit paid for the bigger buffer by cutting context (39,424 → 35,584 on Qwen3-30B-A3B Q4_K_M, L4).
  • It also spilled more experts, which made short prompts ~9% slower.
  • With -fit off right at the VRAM limit, the model could fail to load.

The next version lets --fit grow the ubatch only into spare VRAM.

Replaces the fixed MoE default from the previous commit. Without -ub, --fit now places the weights and the context at the default ubatch as before, then doubles n_ubatch towards n_batch (up to 4096) while the larger compute buffers still fit in the memory left over on every device. The fit never gives up context or device weights for a bigger ubatch, and a draft or MTP context that follows the main ubatch is measured with it. -ub still overrides it, and -fit off keeps the default.
@LeoBorcherding LeoBorcherding changed the title common : MoE models default to a 2048-token ubatch, for up to 2.4x faster prompt processing common : --fit grows the ubatch of MoE models into spare VRAM, for up to 2.5x faster prompt processing Oct 24, 2026
The growth stays on by default. -fitub off (or LLAMA_ARG_FIT_UBATCH=off) keeps the ubatch at its default while --fit still places the weights and the context, so the behaviour can be switched off without pinning -ub 512.
-fitub becomes -fitu (as -fitp, -fitt, -fitc), the option moves next to the other --fit options, and the log line names it as the way to turn the growth off.
@danielhanchen danielhanchen self-assigned this Oct 25, 2026
@LeoBorcherding

Copy link
Copy Markdown
Author

@danielhanchen Reopening this because it looks like it fits with your open MoE PRs instead of overlapping them. If you don't think we need it, feel free to close it.

What it does: MoE models still process prompts in chunks of 512 tokens unless you pass -ub by hand. When --fit finishes with memory to spare, this PR raises the chunk to 1024 or 2048, but only while it still fits inside the fit's margin. It never cuts context or moves weights, so on a full card it does nothing. -ub or -fitu off turns it off.

How it fits with your PRs. I merged this PR into each head and measured prompt speed with experts in RAM (-cmoe), at about 4.2k tokens:

merged into R9700 32 GB (ROCm) A100 40 GB L4 24 GB
#264 (CUDA MoE prompt kernels) 379 → 1014 (2.68x) 166 → 456 (2.74x) 164 → 437 (2.66x)
#268 (prefetch + MoE cache) 761 → 1960 (2.58x) 2566 → 5046 (1.97x) 1335 → 2383 (1.79x)
#251 (--moe-cache-mib auto) 793 → 1780 (2.24x) 400 → 1042 (2.61x) 390 → 950 (2.44x)

With the whole model in VRAM, the gain is smaller but still there: about 1.2x on the R9700 and 1.46x to 1.62x on the A100. On the L4 it stays at 512, because there's no room left over. Dense models are never touched.

Correctness: greedy outputs are byte-identical with and without this PR on every pair, card and config, and answer speed doesn't change. Running each PR with -ub 2048 by hand gives the same speed wherever this PR grew the ubatch, so the gain comes entirely from the chunk size.

The merges were local. Both had small conflicts, in common_init_result (#268) and in the new common_fit_apply() (#251), and I'm happy to rebase onto whichever of these lands first.

…ubatch

The growth inferred that the draft follows the target from equal ubatch
values, so --spec-draft-n-ubatch set to the target's ubatch was measured
at the grown size while the draft context is created at the user's value.
Pass whether the draft follows from the caller instead.
@LeoBorcherding

Copy link
Copy Markdown
Author

Codex review on a mirror of 6074bd5f6 is clean. CI is next.

@LeoBorcherding

Copy link
Copy Markdown
Author

AMD, model fully in VRAM, which the table above didn't cover yet. Radeon AI PRO R9700 32 GB, Windows 11, ROCm 7.1, prebuilt b12966-mix-e49c75f, Qwen4-35B-A3B UD-Q4_K_XL, -ngl all, --fit off, -ub set by hand to what this PR picks:

ubatch on the R9700

ubatch Prompt, 8K tokens (tok/s) Decode (tok/s)
512 (today) 3,261 108
2048 (this PR's pick) 3,848 (1.18x) 107
4096 3,739 (1.15x) 105

Mean of 2 launches per row, alternating order. Decode output identical across all launches. Same result as the CUDA fully-in-VRAM rows: 2048 is the sweet spot, and 4096 adds nothing.

Upstream moved the fit call into common_fit_apply and takes model paths as
vectors (get_paths, hp_nsk). The ubatch growth is re-applied on that API:
common_fit_apply keeps the fit status and grows the ubatch after a
successful fit, and the extra model is measured by its own helper so that
common_params_fit_impl stays as upstream has it.
…ed and host memory

- Sliding-window models keep their ubatch unless --swa-full: the SWA cache
  holds n_swa + n_ubatch cells and every decode step attends over all of
  them (gemma-4 and gpt-oss decode slower at 2048).
- Only the default ubatch grows: presets and tools that set n_ubatch
  themselves (gpt-oss presets, 2048) keep it, as -ub does.
- CPU-only runs keep their ubatch and skip the extra probe.
- The growth stops when the host buffers it projects (the pinned compute
  buffer grows with the ubatch) would leave less than 1 GiB of available
  system memory (MemAvailable on Linux, ullAvailPhys on Windows).
- test-arg-parser covers -ub at the default value and -fitu.
@danielhanchen
danielhanchen changed the base branch from base/upstream-5c19a5a75 to base/upstream-a105b1924 December 5, 2026 14:08
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Dec 5, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-12-06T00:41:08.214981Z 47574cd Manual request
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

chatgpt-codex-connector[bot]

This comment was marked as resolved.

…ices and mmap in the ubatch growth

- Keep the ubatch when the main model's context memory grows with it: that is
  a sliding-window cache, whatever the GGUF metadata says (Llama 4 without the
  sliding_window key still uses an 8192 chunked window), and --swa-full is
  covered because its cache does not grow. One reference probe at the fitted
  ubatch, skipped without devices.
- Stop when a draft that follows the main ubatch also uses a device the main
  model does not: that device's margin is not checked.
- Count resident weights in the host check unless the loader will mmap them
  (mmap supported, and for AUTO every device supports mmap).
- One sentence per comment line.
chatgpt-codex-connector[bot]

This comment was marked as resolved.

…fault ubatch

- An integrated GPU (CUDA UMA such as DGX Spark, APUs) reports free system
  memory, so the host buffers the growth projects are subtracted from its
  budget as well.
- A refit (a server waking from sleep reloads from params) starts again at
  the default ubatch and grows into what is free then, instead of fitting
  weights and context around the ubatch grown before.
@unslothai unslothai deleted a comment from danielhanchen Dec 5, 2026
@unslothai unslothai deleted a comment from danielhanchen Dec 5, 2026
@unslothai unslothai deleted a comment from danielhanchen Dec 5, 2026
@unslothai unslothai deleted a comment from danielhanchen Dec 5, 2026
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Dec 5, 2026
@unslothai unslothai deleted a comment from danielhanchen Dec 5, 2026
@unslothai unslothai deleted a comment from danielhanchen Dec 5, 2026
@unslothai unslothai deleted a comment from danielhanchen Dec 5, 2026
@unslothai unslothai deleted a comment from danielhanchen Dec 5, 2026
@unslothai unslothai deleted a comment from danielhanchen Dec 5, 2026
@unslothai unslothai deleted a comment from danielhanchen Dec 5, 2026
chatgpt-codex-connector[bot]

This comment was marked as resolved.

@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Dec 5, 2026
@danielhanchen

Copy link
Copy Markdown
Member

@codex review

1 similar comment
@danielhanchen

Copy link
Copy Markdown
Member

@codex review

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 5624905858

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread common/common.cpp
Comment on lines +1308 to +1309
if (!params.lora_adapters.empty() || (!params.mmproj.path.empty() && !params.mmproj_use_gpu)) {
return; // adapters and a CPU projector load into the memory the fit left, which the growth would take

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge Budget the TTS projector before allowing ubatch growth

For non-SWA MoE TTS backbones such as Drama3 or ZONOS3, a GPU projector still passes this guard. Its memory has not been reserved. The TTS loader creates the text context before loading the projector, without adding its allocation to fit_params_target. Growth can therefore consume VRAM needed by a projector larger than the remaining fit margin, turning a configuration that worked at ubatch 512 into an OOM during mtmd_init_from_file. Disable growth for this caller or reserve its projector before fitting. Written with GPT-6.1 Sol

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Fixed in 3767186: llama-tts keeps the default ubatch, like llama-mtmd-cli and llama-mtmd-debug, since its projector loads after the fit.

Comment thread common/fit.cpp Outdated
std::vector<llama_device_memory_data> ret(devs.size() + 1);
ret.back().mb = measured.back().mb;
// host RAM always taken, whatever the main model's mmap: CPU extra buffers, and weights the draft copies
ret.back().mb.context += cpu_extra;

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge Exclude shared MTP weights from the extra host-memory charge

With MTP and CPU-repacked experts, this adds shared model weights to the extra context charge. The later shares_model cleanup clears only mb.model, not mb.context. Since cpu_extra includes model allocations and MTP reuses the target model, the host check charges the same repacked weights once for the target and again for the MTP context. When available RAM covers one copy but not two, this incorrectly rejects every ubatch increase despite sufficient memory. Keep CPU model bytes separate from context/compute bytes and omit the shared model portion. Written with GPT-6.1 Sol

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Fixed in 3767186: an MTP head that shares the target model is no longer charged for the CPU repacked weight bytes; its own CPU context and compute buffers still count.

@danielhanchen

Copy link
Copy Markdown
Member

@codex review

1 similar comment
@danielhanchen

Copy link
Copy Markdown
Member

@codex review

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 37671861cb

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread common/fit.cpp Outdated
Comment on lines +1143 to +1144
std::vector<llama_device_memory_data> dmds = common_get_device_memory_data_impl(
paths_model, mparams, cparams, devs, hp_ngl, hp_nsk, hp_nct, hp_nex, log_level, &cpu_extra);

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge Budget CUDA MoE scratch before accepting ubatch growth

For CUDA FP16/BF16 MoEs, this estimate omits the scratch buffers used by the cuBLAS fallback. Those buffers grow with the ubatch but are not included in the scheduler's memory report. For a 4096-token ubatch with 16 active experts and 4096-wide inputs/outputs, src1_sorted and dst_sorted alone require 1.5 GiB, versus 192 MiB at 512 tokens. The growth loop can therefore accept 4096 with the default 1 GiB margin remaining and then encounter a CUDA OOM when processing a full ubatch, even though the 512-token baseline fits. Budget this batch-dependent scratch before accepting growth. Written with GPT-6.1 Sol

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Declining: by default the growth stops at n_batch (2048), where this scratch is about 0.75 GiB and stays inside the 1 GiB margin the fit keeps for allocations it cannot see; 4096 needs an explicit larger -b on an FP16/BF16 MoE that sits right at the margin, the same exposure a user has today passing -ub 4096.

@danielhanchen

Copy link
Copy Markdown
Member

@codex review

1 similar comment
@danielhanchen

Copy link
Copy Markdown
Member

@codex review

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 37671861cb

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread common/fit.cpp
// An OpenVINO dGPU is counted too, which only makes its check stricter.
static bool common_fit_dev_shares_host_memory(ggml_backend_dev_t dev) {
const std::string reg = ggml_backend_reg_name(ggml_backend_dev_backend_reg(dev));
return ggml_backend_dev_type(dev) == GGML_BACKEND_DEVICE_TYPE_IGPU || reg == "OpenCL" || reg == "HTP" || reg == "OPENVINO";

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge Include managed CUDA allocations in the host-memory budget

On Linux with GGML_CUDA_ENABLE_UNIFIED_MEMORY=1 on a discrete CUDA GPU, this helper still returns false. That mode allocates with cudaMallocManaged and reports available host RAM as device headroom, so the device and host checks can independently spend the same RAM while managed buffers spill beyond VRAM. A mocked-budget run of the actual growth loop with 8 GiB host headroom and 2 GiB VRAM accepts ubatch 2048 even though combined host demand rises from 6.75 GiB at 512 to 9.25 GiB; automatic growth can therefore exhaust host memory for a configuration that fits at the default. Account for managed-memory spill in the host budget, or skip growth for this mode. Written with GPT-6.1 Sol

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Declining: GGML_CUDA_ENABLE_UNIFIED_MEMORY is an opt-in mode where the user already chooses to oversubscribe VRAM into pageable host memory (managed allocations page rather than fail), so it is outside the default path this growth budgets.

@danielhanchen

Copy link
Copy Markdown
Member

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Chef's kiss.

Reviewed commit: 37671861cb

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@danielhanchen

Copy link
Copy Markdown
Member

@codex review

1 similar comment
@danielhanchen

Copy link
Copy Markdown
Member

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. 🚀

Reviewed commit: 47574cd254

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

This branch has not been deployed

No deployments
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.

3 participants