Skip to content

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

Draft
LeoBorcherding wants to merge 5 commits into
night-shift-tester/321-basefrom
night-shift-tester/321
Draft

LeoBorcherding wants to merge 5 commits into
night-shift-tester/321-basefrom
night-shift-tester/321

Conversation

@LeoBorcherding

Copy link
Copy Markdown
Owner

Mirror of unslothai#321 at ae1a7faafc, base 5c19a5a756 (base/upstream-5c19a5a75). A sandbox for the Codex review and fork CI; fixes land on the source branch and are synced here.

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.
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.
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.
@LeoBorcherding

Copy link
Copy Markdown
Owner Author

@codex review

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 30, 2026 •

Copy link
Copy Markdown

Codex Review Summary

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

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-10-30T13:41:45.428145Z 6074bd5 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 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: ae1a7faafc

ℹ️ 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
constexpr int64_t MiB = 1024*1024;

// a draft context follows the ubatch of the main context unless it was given its own
const bool extra_follows = extra != nullptr && extra->cparams->n_ubatch == cparams->n_ubatch;

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 Preserve an explicit draft ubatch cap

When --spec-draft-n-ubatch is explicitly set to the target's initial ubatch (for example, 512), this equality check treats the draft setting as implicit and raises it along with the target. That discards the user's draft VRAM cap; track whether the draft ubatch was omitted instead of inferring it from equality.

Useful? React with 👍 / 👎.

…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
Owner Author

Pushed 6074bd5f6 (also on the source branch of unslothai#321). The ubatch growth decided that the draft context follows the target by comparing their ubatch values, so --spec-draft-n-ubatch set to the target's starting value (512) was measured at the grown size during the fit, while the draft context is still created at 512. The caller now passes whether the draft has its own ubatch (params.speculative.draft.n_ubatch <= 0).

Before/after on an M5 (Metal), granite-3.1-1b-a400m Q4_K_M as target and draft, -c 4096 -lv 4, projected free memory at n_ubatch=2048:

arm before after draft context created at
no --spec-draft-n-ubatch 23257 MiB 23257 MiB 2048
--spec-draft-n-ubatch 512 23257 MiB (draft counted at 2048) 23389 MiB 512

The user's draft cap was never lost at runtime; the fit over-counted the draft's compute buffer by about 132 MiB here, which could reject a growth step that fits.

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. What shall we delve into next?

Reviewed commit: 6074bd5f6a

ℹ️ 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".

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.

1 participant