Skip to content

serve: the [prefix] line prints on the refusal branch too (engine#2895) - #2901

Closed
bong-water-water-bong wants to merge 1 commit into
mainfrom
cl-game/2895-prefix-line
Closed

bong-water-water-bong wants to merge 1 commit into
mainfrom
cl-game/2895-prefix-line

Conversation

@bong-water-water-bong

Copy link
Copy Markdown
Collaborator

Draft: engine is at 4 non-draft PRs against the cap of 3 (my #2889 merged an hour ago; this is its follow-up). Flips to ready when a slot frees.

The defect (from a CHANGES verdict that got buried on #2889)

#2889 recorded a refusal as a decision and put the one [prefix] print inside the other branch:

if (!cache_prompt && e.prefix && e.prefix->enabled()) {
    stats.prefix = npu::PrefixDecision{-1, 0, "cache_prompt: false"};   // recorded …
}                                                                      // … and the if closes
if (cache_prompt && e.prefix && e.prefix->enabled()) {
    …
    std::fprintf(stderr, "[prefix] slot=%d …");                        // the only print, unreachable on refusal
}

So the decision that most needs explaining was the only one that printed nothing, and a refused request's log looked exactly like a request that never touched the cache. My own PR body claimed the opposite — "the [prefix] line says you asked for no slot rather than looking like a miss" — which was false, and the reviewer who raised CHANGES on that commit was right.

The fix is the shape, not just the line

One decision per request, then one print after the decision, outside the branch that makes it:

if (e.prefix && e.prefix->enabled()) {
    if (!cache_prompt) { stats.prefix = {-1, 0, "cache_prompt: false"}; }
    else               { …lookup, restore… }
    std::fprintf(stderr, "[prefix] slot=%d cached=%d of %zu reason=%s\n", …);
}

Three decisions, one exit: cache_prompt: false, slot has no usable state, or the lookup's own. A fourth added later cannot skip the line without deleting the print it sits next to.

Verification

  • app/forward_serve.cpp compiles clean under ONEBIT_NPU_RUNTIME_OWN; pre-pr-checks 13 green / 2 skipped.
  • Not yet done, and stated so: the runtime arm — a serve with the prefix cache on, one request with cache_prompt: false, checking that the line appears with reason=cache_prompt: false. That needs a device ticket, and I would rather run it than assert it. The binary for it is building now.

…5, my #2889)

#2889 recorded a refusal as a decision -- `stats.prefix = {-1, 0, "cache_prompt: false"}` -- and put the one
`[prefix] slot=` print inside the OTHER branch, the one that requires `cache_prompt` true. So the decision
that most needs explaining was the only one that printed nothing, and the log of a refused request looked
exactly like a request that never touched the cache. My own PR body claimed the opposite ("the [prefix] line
says *you asked for no slot*"); that claim was wrong, and the CHANGES verdict raised at the same commit was
right about it.

The shape is the fix as much as the line is: one decision per request, then ONE print after the decision,
outside the branch that makes it. Before, two `if`s could each own a decision and only one of them could
print; now there is a single exit and no way to add a decision that skips the line without seeing it.

`cache_prompt` false is not a special case in the output: it is the same line, with the reason saying which
of the three it was -- "cache_prompt: false", "slot has no usable state", or the lookup's own.

Compiles clean under ONEBIT_NPU_RUNTIME_OWN; pre-pr 13 green, 2 skipped. The runtime arm -- that a request
with `cache_prompt: false` logs the line -- needs a serve with the prefix cache on, which is the next step
rather than this commit.
@bong-water-water-bong

Copy link
Copy Markdown
Collaborator Author

VERDICT at 38c725f: 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. It is also a +24/-21 rewrite of the same twenty lines, so it is the largest diff of the three for the same behaviour.

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

Duplicate of #2899, which is the same unification (one log_prefix lambda on both the refusal and accept branches) plus the check that keeps it: serve_routes_check.py now fails when a stats.prefix = npu::PrefixDecision{…} has no log_prefix(/[prefix] slot= within 12 lines. I verified that check three ways on #2899 — rc 1 on main's tree (line 504 records a decision and nothing logs it), rc 0 on its own tree (every recorded prefix decision is logged, 2 site(s)), and rc 1 on a mutant that replaces one call with a comment (naming line 511) — and 77 when it cannot find any recorded decision at all, so a refactor cannot silently disable it.

Recommend closing this draft with a pointer so the branch can be deleted; #2899 carries the same code and the row that stops a third decision path from going silent.

@bong-water-water-bong

Copy link
Copy Markdown
Collaborator Author

Correcting my own verification plan in this PR body: the runtime arm cannot be taken on this box, and the reason is measurable.

I wrote "the binary for it is building now" about the runtime check — a request with cache_prompt: false logging reason=cache_prompt: false. That build finished, and the two binaries I produced for a before/after came out byte-identical:

8dddcec51e8dcfaaa23b197cf49c1c2d  1bit-post2889
8dddcec51e8dcfaaa23b197cf49c1c2d  1bit-pre2887    ← same file, one built from main and one from main with #2887+#2889 reverted

That is not a coincidence, it is the answer: app/forward_serve.cpp is not in that binary at all. It is a target_sources(onebit PRIVATE …) inside if(ONEBIT_NPU) (CMakeLists.txt:2527, :2650), and this configure had ONEBIT_NPU off — as every configure on this host must, because the NPU-enabled build needs -DONEBIT_NPU_PRIVATE=<add-on checkout> and there is no /opt/xdna here (the one lane tree that has an NPU cache also has ONEBIT_NPU:BOOL=OFF). Reverting a file that is not compiled changes nothing, and the hashes say so.

So the honest status of the verification on this PR:

  • compile + static: done (ONEBIT_NPU_RUNTIME_OWN syntax check over the exact file, pre-pr-checks 13 green / 2 skipped);
  • runtime: needs an NPU-enabled build, i.e. a host or lane that has the private add-on. The command is one line once that exists:
1bit serve -m <model> --device npu --prefix-slots 8      # then a /completion with "cache_prompt": false
# expected in stderr: [prefix] slot=-1 cached=0 of N reason=cache_prompt: false

I would rather record the arm as owed on that host than claim it. The defect itself is not in doubt — the print sat inside the if (cache_prompt && …) branch, which is a property of the file, not of a run.

@github-actions

Copy link
Copy Markdown

PR Reviewer Guide 🔍

Here are some key observations to aid the review process:

🎫 Ticket compliance analysis 🔶

2895 - Partially compliant

Compliant requirements:

  • The [prefix] line now prints on the refusal branch
  • The print statement is moved outside the conditional branches to ensure it runs for all decision paths
  • The fix ensures that all three decision paths are logged correctly

Non-compliant requirements:

  • None

Requires further human verification:

  • None
⏱️ Estimated effort to review: 2 🔵🔵⚪⚪⚪
🧪 No relevant tests
🔒 No security concerns identified
⚡ Recommended focus areas for review

Control Flow Fix

The PR correctly restructures the control flow to ensure that the [prefix] log line is printed for all decision paths, including the refusal case. This addresses the issue where the refusal reason was recorded but never printed, making it indistinguishable from a cache miss in logs.

if (e.prefix && e.prefix->enabled()) {
    if (!cache_prompt) {
        // A refusal to consult the cache is a decision, and the single line below prints it like any other:
        // "no slot" and "you asked for no slot" must not read the same in a log (#650).
        stats.prefix = npu::PrefixDecision{-1, 0, "cache_prompt: false"};
    } else {
        const npu::PrefixDecision d = e.prefix->lookup(cache_ids);
        stats.prefix = d;
        // One token short of both the slot and the prompt: the snapshot stops one token short (see the save
        // below), and the last token is run again for the logits the answer is drawn from.
        const int want = npu::resume_point(d, ids.size());
        // The forward clears itself only when a batch starts at 0 (step_batch), and a resumed
        // prefill starts above 0, so the previous request's state has to go first.
        if (d.slot >= 0 && want > 0) {
            e.fwd->reset();
            if (e.fwd->restore_state(e.prefix_state[size_t(d.slot)], want)) {
                resume = want;
                stats.prefix.cached_tokens = resume;
            } else {
                e.fwd->reset();
                stats.prefix = npu::PrefixDecision{-1, 0, "slot has no usable state"};
            }
        }
    }
    // One line per decision, as FLM logs it, so the behaviour is checkable without a debugger. Deliberately
    // outside the consult branch: the print used to live inside it, so the one decision that most needs
    // explaining -- the refusal -- was the one that printed nothing (engine#2895).
    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

Closing this as superseded by #2899 — a draft opened before it appeared, on the same issue, and the other one is the better fix.

What decided it is the part mine does not have: #2899 extends serve_routes_check.py with a static arm that every stats.prefix = npu::PrefixDecision{…} has a logging call within twelve lines, rc 1 naming the line otherwise, and three self-test arms for the recognisers. So the defect class — a decision recorded and never surfaced — is guarded, not just fixed. Mine moved the print out of the branch and stopped there; a fourth decision path added next month would silently reintroduce exactly this bug and my version would still be green.

Second, smaller: the log_prefix lambda makes "one decision, one line" a single call site rather than one print position. Both are structural, theirs states the invariant more plainly.

What mine had and #2899 does not: nothing load-bearing. My hoisted print fires once per request when the cache is on; theirs fires once per decision taken. For the two paths that exist they are the same output line, and the checker is what makes it stay true.

One correction to carry, since I raised it in this branch's description: the runtime arm cannot be taken on this box. app/forward_serve.cpp is only compiled under if(ONEBIT_NPU) (CMakeLists.txt:2527, :2650), and NPU-enabled builds need -DONEBIT_NPU_PRIVATE=<add-on checkout> — there is no /opt/xdna here, and even the one lane tree with an NPU cache has ONEBIT_NPU:BOOL=OFF. I found that out by building two binaries with and without the fix and getting byte-identical md5s; the identical hash was the answer, not a nuisance. So #2899's static arm is not a substitute for the runtime check — it is the only check available on this host, which is a good reason to have it.

Branch deleted. #2895 stays fixed on #2899.

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