Skip to content

serve: print the [prefix] line on the refusal branch too, where it has never once fired (engine#2895) - #2898

Closed
bong-water-water-bong wants to merge 1 commit into
mainfrom
fix2895-prefix-refusal-line
Closed

bong-water-water-bong wants to merge 1 commit into
mainfrom
fix2895-prefix-refusal-line

Conversation

@bong-water-water-bong

Copy link
Copy Markdown
Collaborator

Fixes engine#2895 — the [prefix] line that explains a cache refusal has never printed on the branch that refuses.

The defect, in two line numbers

504:  stats.prefix = npu::PrefixDecision{-1, 0, "cache_prompt: false"};   <- reason recorded, nothing printed
505:  }                                                                    <- closes
…
525:  std::fprintf(stderr, "[prefix] slot=%d cached=%d of %zu reason=%s\n", …)   <- the ONLY print, inside the
                                                                                branch that requires cache_prompt

So a serve log with the cache declined had no [prefix] line at all — indistinguishable from the prefix path not being reached, which is the pair of states that line exists to separate (#650).

And the comment directly above the refusal claimed the line below "says so rather than staying silent." It did not. A comment that promises a diagnostic is worse than no comment, because the next reader trusts it instead of checking — which is how this survived #2889 carrying a CHANGES on it.

The change

Three lines, no control-flow change: the print is duplicated into the refusal branch with the same format and the same arguments.

Both prints sit inside an e.prefix && e.prefix->enabled() guard, so exactly one fires per request and neither fires when the prefix path is off:

501:  if (!cache_prompt && e.prefix && e.prefix->enabled()) {
508:      std::fprintf(… "[prefix] slot=…" …)      <- new
511:  if (cache_prompt && e.prefix && e.prefix->enabled()) {
530:      std::fprintf(… "[prefix] slot=…" …)      <- existing

I considered hoisting to a single print after both branches and did not. That restructures the block, and a C++ refactor I cannot build here is worth less than an addition whose guard is visible in two lines.

What I verified, and what I did not

Verified by reading: exactly two [prefix] slot= prints, each inside its own enabled() guard; the file's braces balance (375 / 375); tools/copyright.py --check passes.

Not built locally. This tree needs the full ONEBIT_HRX configuration and I am not spending the fleet's queue on a three-line addition. CI is the build — and if CI disagrees with my reading, the reading is what is wrong.

…s never once fired (engine#2895)

The [prefix] slot= line sits inside the branch that requires cache_prompt, so the refusal branch above it recorded stats.prefix = {-1, 0, "cache_prompt: false"} and emitted nothing. A serve log with the cache declined therefore had no [prefix] line at all -- indistinguishable from the prefix path not being reached, which is the pair of states that line exists to separate (#650).

The in-code comment above the refusal claimed the line below 'says so rather than staying silent'. It did not. A comment that promises a diagnostic is worse than no comment, because the next reader trusts it instead of checking -- which is how this survived #2889 with a CHANGES on it.

Three lines, no control-flow change: the print is duplicated into the refusal branch with the same format and the same arguments, and both prints sit inside an e.prefix && e.prefix->enabled() guard, so exactly one fires per request and neither fires when the prefix path is off. I considered hoisting to a single print after both branches and did not: that restructures the block, and a C++ refactor I cannot build here is worth less than an addition whose guard is visible in two lines.

NOT BUILT locally -- this tree needs the full ONEBIT_HRX configuration and I am not spending the fleet's queue on it. CI is the build. What I did verify: exactly two [prefix] slot= prints, each inside its own enabled() guard, and the file's braces balance.
@bong-water-water-bong

Copy link
Copy Markdown
Collaborator Author

VERDICT at 1d8296: CHANGES — superseded by #2899, which makes the same change and adds an invariant to tools/ci/serve_routes_check.py: "every recorded prefix decision must be printed", asserted over the source (stats.prefix = npu::PrefixDecision{ needs log_prefix( near it). I measured that the invariant fails on main — 2 recorded decisions, 0 log_prefix( calls — which is what makes it an arm rather than a description. The two-line difference between it and #2899 is precisely the guard.

Land #2899 and close this with a pointer; the behaviour is identical and the coverage is not.

@bong-water-water-bong

Copy link
Copy Markdown
Collaborator Author

Superseded by #2899 — same issue (engine#2895), better shape

#2899 fixes this by introducing one log_prefix lambda that both branches call:

const auto log_prefix = [&ids](const npu::PrefixDecision& d) {
    std::fprintf(stderr, "[prefix] slot=%d cached=%d of %zu reason=%s\n", …);
};
… log_prefix(stats.prefix);   // accept branch
… log_prefix(stats.prefix);   // refusal branch

and adds a check arm that fails when a recorded decision has no log within 12 lines (mutation-verified: 2 prefix decision(s) recorded and never surfaced, rc 1).

The difference that matters is the third path: with one function, a new decision site has to call it; with an extra fprintf inside the refusal branch, the next site copies the print and the two drift — which is exactly how the refusal ended up silent while the accept branch logged. Both PRs also touch tools/ci/serve_routes_check.py, so whichever lands second conflicts there.

Suggestion: gh pr close 2898 --delete-branch, and #2899 carries the fix.

@bong-water-water-bong

Copy link
Copy Markdown
Collaborator Author

VERDICT at 31d7744: CHANGES — superseded by #2899. Correcting my own previous verdict line on this PR, which named 1d8296: that sha is not this PR's head and was not read from anywhere. The head has not moved — I mistyped the address, which would have left this PR effectively unverdicted while looking verdicted to me.

The substance is unchanged and short:

PR diff arm
this one app/forward_serve.cpp +7/-2 none
#2899 +9/-2 + tools/ci/serve_routes_check.py +31/-2 a source invariant
#2901 +24/-21 none

Same fix in all three. #2899 adds an invariant to the routes checker — every recorded prefix decision must be printed, asserted by requiring log_prefix( near each stats.prefix = npu::PrefixDecision{ — and I measured that the invariant fails on main (2 recorded decisions, 0 log_prefix( calls, no log call within ±3 lines of either). That is what makes it an arm rather than a description.

So: land #2899, close this with a pointer. The behaviour here is identical; the coverage is not.

@github-actions

Copy link
Copy Markdown

PR Reviewer Guide 🔍

Here are some key observations to aid the review process:

🎫 Ticket compliance analysis 🔶

650 - Partially compliant

Compliant requirements:

  • Implementation of prefix cache functionality for NPU inference engine
  • Support for configurable prefix cache parameters (--prefix-slots, --prefix-cache-mb, --prefix-min-gain)
  • Proper reporting of cached tokens and prefix cache decisions in logs
  • Handling of system-prompt+tools snapshot as separate cache entry

Non-compliant requirements:

  • The PR only addresses the logging issue (engine#2895) and does not implement the full prefix cache functionality described in the ticket

Requires further human verification:

  • The actual prefix cache implementation needs to be verified against hardware performance metrics
  • Full integration testing with the NPU inference engine is required
⏱️ Estimated effort to review: 2 🔵🔵⚪⚪⚪
🧪 No relevant tests
🔒 No security concerns identified
⚡ Recommended focus areas for review

Missing prefix cache log entry

The PR fixes a logging issue where the [prefix] line was not printed when cache lookup failed. Previously, when cache_prompt was false, the reason was recorded in stats.prefix but no log line was emitted, making it indistinguishable from the prefix path not being reached at all. This could lead to confusion when debugging prefix cache behavior.

if (!cache_prompt && e.prefix && e.prefix->enabled()) {
    // A refusal to consult the cache is a decision, and it has to be PRINTED here: the [prefix]
    // line further down sits inside the branch that requires `cache_prompt`, so before this
    // print the refusal recorded a reason and emitted nothing -- a log with no [prefix] line at
    // all, which is indistinguishable from the prefix path not being reached. That is precisely
    // the pair of states the line exists to separate (#650, engine#2895).
    stats.prefix = npu::PrefixDecision{-1, 0, "cache_prompt: false"};
    std::fprintf(stderr, "[prefix] slot=%d cached=%d of %zu reason=%s\n", stats.prefix.slot,
                 stats.prefix.cached_tokens, ids.size(), stats.prefix.reason.c_str());
}

@bong-water-water-bong

Copy link
Copy Markdown
Collaborator Author

main has already fixed this, and better than I did.

My commit duplicated the fprintf onto the refusal branch — the option I explicitly considered and declined as 'a C++ refactor I cannot build here'. Main's version hoists it into a log_prefix lambda and calls it at 512 (the refusal) and 533 (the consulted case):

504:  const auto log_prefix = [&ids](const npu::PrefixDecision& d) {
512:      log_prefix(stats.prefix);          <- refusal branch, the one that never printed
533:      log_prefix(stats.prefix);          <- consulted branch

One definition, two call sites, no duplicated format string — which is what I said I would not attempt without a build, and the right answer regardless. engine#2895 is fixed on main; this PR is superseded and the branch is deleted.

The lesson costs me nothing to write: the cheaper option was cheaper because I could not verify it, not because it was better. A lane that can build should always prefer the hoist.

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.

1 participant