Skip to content

[Bug] AMD NPU: non-streaming chat completions return no usage - #18577

Merged
danielhanchen merged 1 commit into
unslothai:mainfrom
LeoBorcherding:fix/npu-nonstream-usage
Nov 20, 2026
Merged

danielhanchen merged 1 commit into
unslothai:mainfrom
LeoBorcherding:fix/npu-nonstream-usage

Conversation

@LeoBorcherding

Copy link
Copy Markdown
Collaborator

Non-streaming /v1/chat/completions on an AMD NPU model never returns usage, so the OpenAI SDK reads usage=None. Streaming with stream_options.include_usage does return it.

Cause. _npu_chat_completions always streams from FastFlowLM and collects the stream for a JSON caller. FastFlowLM sends usage in its own empty-choices chunk, and _proxy_to_external_provider drops that chunk unless the payload opts in (_is_openai_usage_only_sse). The non-streaming payload never opts in, so _NpuStreamRelay never sees usage and the collected body has none.

Fix. When the caller didn't ask for a stream, set stream_options.include_usage on the upstream copy of the payload. Streaming requests are unchanged.

Test. test_non_streaming_reply_keeps_a_separate_usage_chunk makes the fake FastFlowLM send usage the way the real one does: a separate chunk, only on include_usage. It fails on main with KeyError: 'usage' and passes here. The existing fake put usage inline on the finish chunk, which the proxy never drops, so the existing test passed on main. All 657 NPU backend tests pass (2 skipped).

Before and after on a Strix Halo NPU

Both legs in one job, run 44657898339, each a fresh install.sh --local in its own home, unsloth run --model LFM3-1.7B-NPU2 --context-length 4096, then the same requests.

request main 3942942 this PR 7fa1666
stream: false 200, no usage key 200, usage with prompt 17, completion 200, total 217, plus FastFlowLM's KV fields
stream: false, enable_tools: false 200, no usage key 200, usage present
OpenAI SDK chat.completions.create usage=None CompletionUsage(completion_tokens=109, prompt_tokens=17, total_tokens=126, ...)
stream: true with include_usage usage chunk usage chunk (unchanged)
stream: true, no stream_options no usage no usage (unchanged)

The same usage=None shows on the v0.1.920-beta release install (tag 2f2490c) with Qwen3.6-35B-A3B-NPU2 in runs 44644638040 and 44654204267.

machine what ran measured at
AMD DevLab Strix Halo, Ryzen AI MAX+ 395 (NPU Strix Halo, aie2p 6x8), Ubuntu 24.04.5, kernel 7.0.0-43-generic, amdxdna 2.25.260102.56 (dkms), NPU firmware 1.1.2.65, XRT 2.25.37, FastFlowLM v1.0.12, Lemonade 2026.44.1 before/after above 7fa1666 vs 3942942

Not run: Windows (the route is OS-independent, but nobody ran the Windows NPU path), Strix Point / Krackan / Gorgon Point NPUs, and FastFlowLM 1.0.13 from #18553. The change is in Unsloth Studio's route only, so it doesn't depend on the NPU part.

@danielhanchen

Copy link
Copy Markdown
Member

@codex review

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 20, 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-20T14:27:21.233315Z 7fa1666 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

Copy link
Copy Markdown

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

Reviewed commit: 7fa16664c9

ℹ️ 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
danielhanchen merged commit 966a004 into unslothai:main Nov 20, 2026
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.

2 participants