Skip to content

[Feature] AMD NPU: size the context to free RAM instead of a flat 8192 tokens - #17511

Merged
LeoBorcherding merged 22 commits into
unslothai:mainfrom
LeoBorcherding:studio/npu-auto-context
Nov 13, 2026
Merged

LeoBorcherding merged 22 commits into
unslothai:mainfrom
LeoBorcherding:studio/npu-auto-context

Conversation

@LeoBorcherding

@LeoBorcherding LeoBorcherding commented Nov 11, 2026 •

Copy link
Copy Markdown
Collaborator

NPU models always loaded at 8192 tokens. Auto now sizes the context to free RAM, like GGUF's host-RAM Auto: free RAM less 2 GiB and the weights, priced per token from FastFlowLM's bf16 KV on full-attention layers plus its default prefix cache. Largest multiple of 1024 that fits, capped at 65536 or the model limit, floor 8192. Pins still win, with a warning above the estimate. A reload releases the old model before sampling RAM.

Engine caps below the checkpoint limit: Qwen3.8 27B MTP and DFlash2 at 8192 (ROCm/FastFlowLM#1001), Qwen3 0.6B to 8B and LFM2.5 1.2B at 32768 (FastFlowLM 1.0.12 model cards).

On a 31.8 GiB Strix Halo with 23.9 GiB free: qwen3.5-9b 65536, qwen3.6-moe-35b-a3b 8192, qwen3.8-mtp-27b 8192.

Follow-up, not here: cap Auto at the memory window the NPU can map (about half of RAM on Windows), once #17531's npu_memory_window_gb is in.

Tested on Windows 11, Ryzen 9 9950X, no NPU: backend and frontend tests only, no NPU load.

Screenshots

Spoofed NPU (fake Lemonade/FastFlowLM, no NPU). Run settings now says Context Length "Auto" instead of a flat 8192.
17511

Also checked by building Unsloth Studio from this PR's merge base (left) and head (right), on the same spoofed AMD NPU:

Chat > run settings of qwen3-0.6b-FLM: Context Length defaults to Auto, which fits up to FastFlowLM's 32k card limit, instead of a fixed 8192. On a real XDNA2 NPU the Auto load went from 8192 to 32768 and a 40960 pin was clamped to 32768.

Before and after

Auto now picks the largest context whose bf16 KV cache (full-attention
layers only, read from FastFlowLM's config.json) plus FastFlowLM's prefix
cache fits the RAM left after the weights and a 2 GiB headroom, up to
65536 or the model's limit, never below 8192. A missing config.json keeps
8192. Pins still win; one above the estimate is logged and flagged in the
run settings.
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 12, 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-11-13T18:08:51.337061Z 74d0fce 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.

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Another round soon, please!

Reviewed commit: 6f7b5d380e

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

@chatgpt-codex-connector

Copy link
Copy Markdown

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

Reviewed commit: 9dcf0af692

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

…e default unknown label

Carried from the phase 1 branch (6ba12ba): the NPU picker passes "Auto" as unknownLabel, so the MLX dash has to stay the default.
@LeoBorcherding

Copy link
Copy Markdown
Collaborator Author

Synced from the chat group (LeoBorcherding#856) in 469652b, on top of 9dcf0af: one contract assertion (the unknown-window label still defaults to a dash). The rest of the group's Auto sizing was already here or is superseded by 3cb7016/6f7b5d3.

One limit is still open: the group also capped Auto at the memory window the NPU can map (half of RAM on Windows). It needs #17531's npu_memory_window_gb, so it lands as budget = min(budget, window) in plan_context once both are in. Codex was clean on the fork mirror at this head.

@Imagineer99

Copy link
Copy Markdown
Member

Synced from the chat group (LeoBorcherding#856) in 469652b, on top of 9dcf0af: one contract assertion (the unknown-window label still defaults to a dash). The rest of the group's Auto sizing was already here or is superseded by 3cb7016/6f7b5d3.

One limit is still open: the group also capped Auto at the memory window the NPU can map (half of RAM on Windows). It needs #17531's npu_memory_window_gb, so it lands as budget = min(budget, window) in plan_context once both are in. Codex was clean on the fork mirror at this head.

Thanks, Leo. The Windows follow-up makes sense. One small thing: the dash-default assertion was already there before 469652b, so it now appears twice. We can drop one copy.

…n map

On Windows the NPU maps about half of system RAM. Auto budgeted the KV cache from free host RAM
alone, so a Windows laptop with spare RAM could get a context that fails at load. The budget is
now the smaller of free RAM and the NPU window less the weights and load margin. The window
helpers live in npu_memory.py, shared with the catalog's fit warning.
@danielhanchen danielhanchen self-assigned this Nov 13, 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: 74d0fce916

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

budget = (available - HOST_RAM_HEADROOM_MIB) * _MIB - weights
window = _npu_window_bytes(weights)
if window is not None:
budget = min(budget, window)

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 Keep host-only prefixes out of the NPU mapping budget

When the Windows NPU mapping window is the binding limit, this min feeds it to _largest_fitting, whose _cost includes the host-only prefix cache. This charges CPU snapshots against the device-mapping allowance: with the existing 48 GiB/Qwen3.6 layout and 40 GiB free, Auto selects 13,312 tokens although 65,536 needs 1.25 GiB of mapped KV (under the 2.4 GiB allowance) and 5.25 GiB including prefixes (under the 17.4 GiB host budget). Check active mapped KV against the NPU window and active-plus-prefix memory against free host RAM separately; otherwise Auto and the fit warning substantially understate usable context.

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.

Not changing this: charging the prefix snapshots against the Windows window only makes Auto pick a smaller context, which still loads, while under-charging fails the load. FastFlowLM 1.0.12 does not document where those snapshots live relative to the NPU mapping, so the conservative bound stays.

danielhanchen pushed a commit to danielhanchen/unsloth-staging-2 that referenced this pull request Nov 13, 2026
@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Nice work!

Reviewed commit: 74d0fce916

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

@LeoBorcherding

Copy link
Copy Markdown
Collaborator Author
image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants