Repository navigation
[Feature] AMD NPU: size the context to free RAM instead of a flat 8192 tokens - #17511
Conversation
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.
DFlash2 shares the 27B xclbin and fails the same way above 8192 (ROCm/FastFlowLM#1001), but Auto sized it up to 65536 on a big-RAM box.
Codex Review Summary: ✅ CompletedThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
|
Codex Review: Didn't find any major issues. Another round soon, please! Reviewed commit: ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
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". |
|
Codex Review: Didn't find any major issues. Bravo. Reviewed commit: ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
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.
|
Synced from the chat group (LeoBorcherding#856) in 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 |
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.
…caps Auto to the NPU window
…the RAM Windows lets the NPU map
|
@codex review |
1 similar comment
|
@codex review |
There was a problem hiding this comment.
💡 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) |
There was a problem hiding this comment.
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 👍 / 👎.
There was a problem hiding this comment.
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.
|
Codex Review: Didn't find any major issues. Nice work! Reviewed commit: ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
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". |

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_gbis 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.

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.