Skip to content

fix(npu): default 32k context on NPU profile, resolving the 4096↔32768 mismatch - #1747

Merged
itomek-amd merged 4 commits into
mainfrom
autofix/issue-1745
Jun 26, 2026
Merged

itomek-amd merged 4 commits into
mainfrom
autofix/issue-1745

Conversation

@github-actions

@github-actions github-actions Bot commented Jun 18, 2026 •

Copy link
Copy Markdown
Contributor

Before: gaia init --profile npu reported a 4096-token context window, but at runtime GAIA loaded the FLM chat model at ctx_size=32768 — a config/runtime mismatch (#1676) that left the init report lying about the window actually in use. After: the NPU profile defaults to 32768 everywhere, matching GPU/CPU, so what init reports is what the loader requests.

The fix flips the three pinned-4096 sites — the gemma-4-e2b model requirement, the NPU DeviceConfig, and the npu init profile — to 32768, and updates the comments/tests that documented the old 4096 NPU window.

Closes #1745

Validated on NPU hardware — Ryzen AI MAX+ 395 (cold start)

The risk flagged in the issue (the #1655 lesson) was that flipping the window blind could OOM/fail an FLM load on a cold NPU. It does not — verified end-to-end against Lemonade serving the FLM build on a Ryzen AI MAX+ 395:

  • Cold load at 32k succeeds. POST /api/v1/load {model_name: gemma4-it-e2b-FLM, ctx_size: 32768} → {"status":"success"}, HTTP 200, 11.4 s, no OOM. Health then reports device: npu, recipe_options.ctx_size: 32768 — the 32k window is really allocated, not silently clamped to 4096.
  • Serves at 32k. A chat completion echoes a planted passphrase verbatim (finish_reason: stop), at 22.85 tok/s decode — in line with the documented ~24 tok/s NPU figure. (KV occupancy 0.137 % at 45 tokens ⇒ total ≈ 32,775 ≈ 32768, independently corroborating the window.)
  • gaia init --profile npu reports 32768. Run from this branch on the box, it prints ✓ Context size verified: 32768 tokens and gemma4-it-e2b-FLM - OK (ctx: 32768) (exit 0) — the old 4096 is gone.
  • External confirmation. @soulafein83 confirmed a 32k FLM load on a Ryzen AI 7 350 / 16 GB.

Test plan

  • python -m pytest on the 4 affected unit files — 69 passed
  • python util/lint.py — black + flake8 clean (E501 ignored by repo config); CI Run Code Quality Checks green
  • gaia init --profile npu reports 32768 (not 4096) — verified on a Ryzen AI MAX+ 395
  • FLM chat model (gemma4-it-e2b-FLM) loads at ctx_size=32768 on a Ryzen AI APU from a cold NPU start — verified, no OOM
  • External confirmation from @soulafein83
CI: two red jobs are a pre-existing, unrelated infra failure

Test Lemonade Embeddings API and Test RAG fail on nomic-embed-text-v2-moe-GGUF: llama-server failed to start (HTTP 500) — the embedding model's server, which this PR does not touch. The same failure hits unrelated branch fix/lemonade-version-ci-trigger at the same time and reproduces on both Lemonade 10.7.0 and 10.8.1. The previously-failing Lemonade Server Smoke Test now passes after main's 10.8.1 bump (#1869) merged in. All unit, lint, CLI, and Agent-SDK integration jobs are green.

…8 mismatch

The NPU profile pinned a 4096-token context at three sites while the
runtime load path requested 32768, so `gaia init --profile npu` reported
4096 but GAIA loaded the model at 32k — a config/runtime mismatch (#1676).
NPU now defaults to 32768 to match GPU/CPU. The E2B/FLM build is confirmed
loading at 32k on a Ryzen AI 7 350 / 16 GB; cold-NPU validation pending.

Closes #1745
@kovtcharov-amd

Copy link
Copy Markdown
Collaborator

@claude perform a deep code review of this PR, make sure this is documented.

@github-actions

Copy link
Copy Markdown
Contributor Author

Summary

This is a clean, well-scoped fix that aligns the NPU profile's context window to 32768 everywhere, so gaia init --profile npu stops reporting a window (4096) that differs from what the loader requests. All three pinned-4096 sites — the model requirement, the DeviceConfig, and the init profile — are flipped together, with every NPU-specific test and code comment updated to match. The single most important thing: the code change is correct and internally consistent, but the actual gating risk (does a cold NPU actually load FLM at 32k without OOM?) is a hardware fact this review can't verify — the author has already flagged it and it must be ticked off before merge.

The change correctly avoids touching the generic ModelRequirement / AgentProfile dataclass defaults (min_ctx_size: int = 4096 at lemonade_client.py:168,181), which are not NPU-specific. Scope is tidy: only the NPU paths and their tests move.

Issues Found

🟡 Cold-NPU load is the real merge gate, not the Python tests — The unit tests only confirm the constants moved together; they can't prove FLM serves a 32k window on a cold NPU. This is exactly the failure mode the original test_e2b_min_ctx_size_matches_npu_window docstring warned about ("asserting a larger minimum than the hardware serves would be a faked requirement the model can't satisfy"). The PR relies on one confirmation (Ryzen AI 7 350 / 16 GB), but lower-memory NPUs could OOM on a cold load. No code change needed — but the test-plan checkbox "FLM loads at ctx_size=32768 on a Ryzen AI APU from a cold NPU start" must be genuinely exercised (not against a warm cache, per the #1655 lesson) before this merges. Worth confirming whether the runtime loader was already requesting 32768 on all paths (init used min_context_size=4096, but resolve_device_model returned 4096 too — so the UI device-dropdown path is genuinely being raised here, not just realigned).

🟢 Eval note (informational) — This touches a model ctx-load path, which normally triggers the "run agent evals" rule. It's not actionable here: the committed baselines are the GPU GGUF gemma-4-e4b build, and the FLM NPU model can't be evaluated in CI. No action needed beyond the hardware check above.

Strengths

Verdict

Approve with suggestions — The code is correct, scope-clean, and well-tested at the unit level. There are no blocking code defects. The one true gate is the cold-NPU hardware validation the author already flagged; that checkbox must be ticked (against a cold start, not a warm cache) before merge.

@github-actions

Copy link
Copy Markdown
Contributor Author

Deep review: the code is correct and complete, the change is self-documenting in code/tests, but there's no user-facing doc — and that's the one gap worth closing before merge. I traced the mismatch end-to-end and it confirms the PR's premise (with a bonus finding below). The only blocking gate remains the cold-NPU hardware load the author and the first review already flagged.

Code correctness — ✅ consistent and complete

All paths that encode the NPU window now agree at 32768:

  • MODELS["gemma-4-e2b"].min_ctx_size (lemonade_client.py:239)
  • NPU DeviceConfig.ctx_size → feeds resolve_device_model / the UI dropdown (registry.py:279, _chat_helpers.py:521)
  • npu init min_context_size (init_command.py:108)

I grepped for stragglers: the remaining 4096s are correct to leave — the generic ModelRequirement/AgentProfile dataclass defaults (lemonade_client.py:168,181), the legacy Qwen entry (:261), and unrelated stream buffers. Nothing half-landed.

Bonus finding — this is more than a cosmetic report fix

The runtime 32768 wasn't arbitrary: the chat preflight _maybe_load_expected_model (_chat_helpers.py:1030) reloads any model whose active_ctx < DEFAULT_CONTEXT_SIZE (32768) and reloads it at 32768. With FLM pinned to 4096, every chat turn saw ctx=4096 < 32768, judged FLM "wrong ctx," and forced a reload — so the old 4096 pin was actively feeding the FLM↔GGUF churn in #1676, not just lying in the init report. Raising the pin makes FLM load at 32768 first time, so the preflight stops flagging it. Worth calling out in the PR body — it strengthens the case and ties the change back to the parent loop bug.

Scope note — 🟢

This Closes #1745 (the ctx mismatch) but correctly does not close #1676 (the full FLM↔GGUF eviction loop, which also needs the embeddings/Vulkan thrash addressed). The title/description are accurate; just keep #1676 open.

Documentation — 🟡 the actual gap

The change is well-documented in code comments and tests, but no doc file is touched and docs/guides/npu.mdx never mentions a context window at all. Two concrete, low-cost additions would close the "make sure this is documented" ask:

  1. A one-line note in the Device Selection table or a short "Context window" line in docs/guides/npu.mdx stating the NPU profile now serves 32k (matching GPU/CPU), so gaia init --profile npu reporting 32768 is expected.
  2. A release-note line under the next version in docs/releases/, framed as user impact: before — init reported 4096 while the loader requested 32k; after — they agree at 32k.

Merge gate — 🔴 unchanged

The cold-NPU 32k FLM load is the real gate, and it's now a louder one: min_ctx_size=32768 is a hard floor, so on a lower-memory NPU that can't serve a 32k KV cache the load will fail loudly rather than silently clamp. That's the right behavior, but it means the single 16 GB / Ryzen AI 7 350 confirmation isn't sufficient on its own — reproduce on a cold start (no warm cache, per the #1655 lesson) before merging.

Net: approve the code, please add the two doc lines, and hold merge on the cold-NPU check.

@github-actions github-actions Bot added llm LLM backend changes tests Test changes performance Performance-critical changes agents labels Jun 18, 2026
@itomek itomek self-assigned this Jun 26, 2026
@itomek
itomek marked this pull request as draft June 26, 2026 15:45
@itomek
itomek marked this pull request as ready for review June 26, 2026 16:10
@itomek-amd
itomek-amd merged commit a0e3e72 into main Jun 26, 2026
62 of 65 checks passed
@itomek-amd
itomek-amd deleted the autofix/issue-1745 branch June 26, 2026 16:11
@github-actions

Copy link
Copy Markdown
Contributor Author

Verdict: Approve

This flips the NPU/FLM context window from 4096 to 32768 across the three places it was pinned (the model requirement, the device config, and the npu init profile), so gaia init --profile npu now reports the same 32k window the loader actually requests — closing the config↔runtime mismatch from #1745. The change is consistent across all three sites, the tests track it, and it's validated cold on real NPU hardware (32k load succeeds, no OOM, serves a planted passphrase at ~23 tok/s), which directly addresses the #1655 cold-start lesson. Safe to merge.

One leftover: a comment elsewhere in the codebase still claims "NPU's FLM build runs at 4K," which now contradicts this change — worth a one-line fix so the doc trail stays consistent. Non-blocking.

🔍 Technical details

🟢 Minor

Stale "runs at 4K" comment missed by the sweep (src/gaia/agents/chat/agent.py:79)

The PR updated the comments at the three flipped sites but this one still documents the old window, now contradicting the new 32768 default:

    # The NPU FLM build defaults to 32K ctx (matching GPU/CPU); a device config can still override it.

That's the only lingering 4K/4096-for-NPU reference I found — the 4096 hits in lemonade_client.py:2732, lemonade_manager.py, and agent.py:3146 are about Lemonade's own fallback default, not the FLM NPU window, so they correctly stay. No doc-site (docs/) references the old NPU 4096 figure either.

Strengths

  • All three pinned sites flipped together (registry.py DeviceConfig, init_command.py profile, lemonade_client.py min_ctx_size) with matching test updates — no half-applied state that would re-introduce the mismatch.
  • Cold-start hardware validation, not just a warm-cache check — exactly the gap CLAUDE.md calls out from gaia init --profile npu fails with status 400 (missing user. prefix for model name) #1655/[Bug]: 1.4MB file indexed no response to queries in the UI #1030. The load-at-32k-with-no-OOM evidence is what makes this safe rather than a blind flip.
  • The 4 K floor assertion was kept, not deleted (test_e2b_min_ctx_size_matches_npu_window now asserts >= 4096 with an explanation) — the test still guards the real constraint instead of just chasing the new number.

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

Labels

agents llm LLM backend changes performance Performance-critical changes tests Test changes

Projects

None yet

Development

Successfully merging this pull request may close these issues.

fix(npu): default 32k context on NPU profile + resolve 4096↔32768 runtime mismatch

3 participants