Skip to content

npu: key a score answer by level index, as upstream does (#653) - #776

Merged
bong-water-water-bong merged 3 commits into
mainfrom
1bit/npu-score-keys
Nov 1, 2026
Merged

bong-water-water-bong merged 3 commits into
mainfrom
1bit/npu-score-keys

Conversation

@bong-water-water-bong

@bong-water-water-bong bong-water-water-bong commented Nov 1, 2026 •

Copy link
Copy Markdown
Collaborator

What

A one-question answer object is keyed wrongly for score, found while writing the /v1/systemone route (#775) on top of this contract.

d3_runtime.normalize_question() renders a score as a choice whose option keys are the level indices, not the level texts:

return Question(kind, question, rendered,
                [str(i) for i in range(len(levels))],   # keys   -> "0", "1", "2"
                list(criteria))                          # descriptions (legend)

and product_answer() builds the answer from those keys:

"probabilities": dict(zip(question.keys, shares)),          # {"0": p0, "1": p1, "2": p2}
"legend": {key: _canonical(text) for key, text in zip(question.keys, question.descriptions)}

npu/systemone.h::answer_of() keyed both by the level text instead, so a client reading probabilities or legend saw {"cheap": …} where the d3 server it stands in for emits {"1": …}. score and confidence were already identical, and the prompt is untouched (it prints the answer code and the level text, neither of which was ever the key) — which is why the nine golden fixtures, which cover the prompt only, could not see it.

Change

  • npu/systemone.h: the score branch pushes "0".."n-1" as the option keys, with the level texts still what the prompt shows and the legend describes.
  • tests/systemone_test.cpp: ctest systemone_contract now asserts the keys are "0"/"1"/"2", that legend["1"] == "ok", and that neither map is keyed by the level text.

Verified

row result
ctest systemone_contract, systemone_route, decision_head, npu_sampling 4/4 PASS
tools/copyright.py --check rc 0
nine golden prompts (d3_format.py fixtures) still byte-identical — the prompt does not use the keys
/v1/systemone score row with #775 + this applied locally (Qwen3-0.6B, synthetic readout, host GEMM under npu-hold -l npufeat, 0 amdxdna errors) {"confidence":0.810,"legend":{"0":"bad","1":"ok","2":"great"},"probabilities":{"0":0.949,"1":0.0492,"2":0.00167},"score":0.0525,"type":"score"}

Independent of #775: this is the contract's answer shape, so it can land in either order.

That row is measured on a tree with both commits (this one cherry-picked onto #775's branch, then removed again — #775 stays a single commit and this PR is independent of it). Before this change the same request answered {"bad":…,"ok":…,"great":…}, which is the divergence this PR fixes.

d3_runtime's normalize_question() renders a score as a choice whose option keys are the level
INDICES ("0","1",...) and whose descriptions are the level texts, so product_answer() emits
`probabilities` and `legend` keyed that way. npu/systemone.h keyed both by the level text instead,
which is a different answer object for the same decision -- invisible until now because the nine
golden fixtures only cover the prompt, and because score's `score` and `confidence` values were
already identical.

The prompt is unchanged: it prints the answer code and the level text, neither of which was ever
the key. ctest systemone_contract now asserts that a score answer is keyed "0".."n-1" and that it
is not keyed by the level text.
@github-actions

github-actions Bot commented Nov 1, 2026 •

Copy link
Copy Markdown

PR Reviewer Guide 🔍

(Review updated until commit 06af0fa)

Here are some key observations to aid the review process:

🎫 Ticket compliance analysis 🔶

775 - Partially compliant

Compliant requirements:

  • POST /v1/systemone and GET /v1/systemone/models implemented
  • Decision config and model files supported
  • last_hidden() method added to Q4nxNpuForward
  • d3 contract compliance verified
  • Authentication with DECISION_API_KEY implemented
  • /health endpoint updated with d3 fields
  • Prompt formatting handled correctly
  • Tests added for systemone route and decision head
  • Documentation updated

Non-compliant requirements:

  • Real d3-lite backbone not implemented (as noted in PR description)
  • Vision support (images/videos) not implemented
  • State computed once per request optimization not implemented
  • noncausal_full_attention mode not implemented

Requires further human verification:

  • Verification of real d3-lite backbone functionality (not in this PR)
  • Testing with actual d3-lite pack (not in this PR)
⏱️ Estimated effort to review: 3 🔵🔵🔵⚪⚪
🧪 PR contains tests
🔒 No security concerns identified
⚡ Recommended focus areas for review

Incorrect score answer keying

The score answer was incorrectly keyed by level text instead of level index, which differs from upstream behavior. This caused the probabilities and legend in the answer to be keyed by level text rather than the numeric indices "0", "1", "2", etc. as expected by the d3 runtime. The fix ensures that the option keys match upstream's behavior where keys are the level indices.

// A score is rendered as a choice over its ordered levels, and upstream's option KEY is the
// level's index while the level text is only what the prompt shows: d3_runtime's
// normalize_question() builds `[str(i) for i in range(len(levels))]` as the keys, and
// product_answer() keys `probabilities` and `legend` by them. Keying by the text instead is a
// different answer object for the same decision.
for (size_t i = 0; i < out.texts.size(); i++) out.keys.push_back(std::to_string(i));

@bong-water-water-bong bong-water-water-bong left a comment

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

VERDICT: READY

Small, and I checked the claim against the reference implementation rather than against the PR's own quote of it.

Ran: tests/systemone_test.cpp at f3c9efa9 — systemone: ok, rc 0 (it now carries the two new assertions:
keys are "0"/"1"/"2", legend["1"] == "ok", and neither map is keyed by the level text).

Verified against d3_runtime.py itself (the pack's copy on this box, /tmp/d3meta/d3_runtime.py):

normalize_question(), score branch:
    return Question(kind, question, rendered,
                    [str(i) for i in range(len(levels))],   # keys
                    list(criteria))                          # descriptions
product_answer(), score branch:
    "probabilities": dict(zip(question.keys, shares)),
    "legend": {key: _canonical(text) for key, text in zip(question.keys, question.descriptions)},

So the keys really are the level indices and the legend really is keyed by them — the change matches upstream
exactly, and score/confidence are unchanged because upstream computes them from enumerate(shares), which the
C++ already did. The prompt side is untouched too: upstream renders criteria: {text: None for text in levels}, i.e.
texts only, which is what this tree already emitted — so the "nine golden fixtures could not see it" explanation is
right, they cover the prompt.

Nit, not a blocker: the new comment says "upstream's option KEY is the level's index while the level text is only what
the prompt shows", which is true of score but reads as a rule for all kinds — for choice, upstream's keys are the
caller's criteria keys (keys = list(criteria)), and noul uses options(rendered). One word ("for a score, …")
keeps a future reader from applying it to choice.

@bong-water-water-bong bong-water-water-bong added the tprs-ready TPRS: review verdict READY posted by the standing reviewer; merge gate satisfied label Nov 1, 2026

@bong-water-water-bong bong-water-water-bong left a comment

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

VERDICT: READY

Confirmation at 743404b3 after update-branch (main's #768 squash merged in; author commit f3c9efa unchanged). Re-ran
the suite rather than assuming the merge was inert:

$ g++-16 -std=c++26 -O1 -Inpu … tests/systemone_test.cpp && ./systemone_test
systemone: ok      rc 0

The keying claim still matches d3_runtime.py ([str(i) for i in range(len(levels))] in normalize_question, and
product_answer keying probabilities/legend by it). Nit unchanged: the new comment reads as a rule for every kind
but is true of score. tprs-ready stays.

@bong-water-water-bong bong-water-water-bong left a comment

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

VERDICT: READY

Confirmation at 06af0fa6 after update-branch: their own files are byte-identical to the head I verdicted — verified with
git diff --stat <previous head> 06af0fa6 -- <this PR's paths> → empty, the merge brought #768's and #775's squashes
(7f4e98a, e55096b) and nothing else. Content as reviewed (systemone), build green. tprs-ready stays.

@bong-water-water-bong
bong-water-water-bong merged commit 90c998a into main Nov 1, 2026
8 checks passed
@bong-water-water-bong
bong-water-water-bong deleted the 1bit/npu-score-keys branch November 1, 2026 18:27
@github-actions

github-actions Bot commented Nov 1, 2026

Copy link
Copy Markdown

Persistent review updated to latest commit 06af0fa

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

Labels

Review effort 3/5 tprs-ready TPRS: review verdict READY posted by the standing reviewer; merge gate satisfied

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant