Skip to content

npu: forward-serve has no sampling controls and no structured output #625

Closed

Description

@bong-water-water-bong

1bit forward-serve serves the NPU route with greedy decoding only and no output constraint. The
request parser takes max_completion_tokens/max_tokens and nothing else, and the sampler is a plain
argmax:

// app/forward_serve.cpp:134
const int max_tokens = body.value("max_completion_tokens", body.value("max_tokens", 256));
// app/forward_serve.cpp:167
auto argmax = [](const std::vector<float>& v) {
    int a = 0; for (size_t i = 1; i < v.size(); i++) if (v[i] > v[a]) a = (int)i; return a; };

grep -ril "gbnf\|grammar" app npu tools returns nothing, so there is no constrained decoding on this
route either.

Why this matters now

The reference NPU stack shipped enforced structured output on 2026-10-22 (FastFlowLM v1.0.10,
PR #790): constrained decoding through
xgrammar, driven by the standard OpenAI fields —
response_format: {"type":"json_object"|"json_schema"}, tool_choice: none|auto|required|{function},
parallel_tool_calls — enforced while generating, with 400 for a malformed request instead of a
silently ignored field. So "ask the NPU a question and get schema-valid JSON back" works there and
cannot be done on our route at all.

Concrete consequences here:

  • no structured extraction demo on the NPU (a JSON row is the most convincing thing to show);
  • no temperature/top-p, so no reproducibility story beyond greedy;
  • callers with an OpenAI-shaped client see the fields accepted-and-ignored, which is the failure mode
    FLM's release explicitly calls out.

Proposed scope (smallest useful step)

  1. Parse response_format (json_object, json_schema) plus temperature and top_p in
    app/forward_serve.cpp, and reject what is not implemented with a 400 rather than ignoring it.
  2. Mask logits before the argmax (equivalently before any truncation — filtering after truncation can
    leave zero candidates) and advance the grammar state after the sampled token.
  3. Two traps to design in from the start, both seen in the FLM PR's review:
    • all tokens masked → softmax/NaN: detect "no finite logit survives" and fall back
      deterministically, and do not advance the grammar with a token outside it;
    • per-request state leaking: the grammar/constraint state is owned per request, so it must be
      reset on every exit path (this tree already had exactly this bug class with the GDN
      conv_state_/rec_state_ in reset(), fixed in npu: prefill a GDN model in one chunk instead of one token at a time (#587) #624's fallout).

Out of scope for the first cut: GBNF/grammar passthrough (FLM calls theirs "coming later" too), lazy
grammars, and tool-call parsing — a JSON-schema path with the two traps handled is the useful subset.

Not a duplicate

#619 tracks the census/watch side of FastFlowLM capabilities and #574 seals kernel manifests; this is
the engine-side sampling and constraint surface on our own NPU route.

Activity

  1. bong-water-water-bong commented on Oct 28, 2026

    @bong-water-water-bong
    CollaboratorAuthor

    Verified against main (8d2b25d), cheaply, no device work:

    • Sampling controls really are absent: app/forward_serve.cpp is 382 lines, the request has exactly one knob — const int max_tokens = body.value("max_completion_tokens", body.value("max_tokens", 256)); at app/forward_serve.cpp:134. Everything downstream is greedy: auto argmax = [](const std::vector<float>& v) at app/forward_serve.cpp:163, called once per token at app/forward_serve.cpp:191. No temperature, top_p, top_k, seed, stop handling anywhere in the file.
    • Structured output really is absent: grep -rn "gbnf\|grammar" app/ npu/ tools/ returns nothing, so there is no grammar path on the NPU route and nothing to hook a response_format into.
    • The issue's line number has moved (167 -> 163 for the argmax lambda), everything else is accurate.

    Classification: actionable now — this is pure host-side C++ behind the HTTP surface, it needs no GPU or NPU to land, and it can be tested on the CPU route. The one caveat: sampling is per-request state, and the file is a single-shot forward loop, so the fix should carry the #618/#624-class per-request-state lesson rather than adding globals. Suggested split: temperature/top_k/top_p/seed + stop first (host-side, testable without the device), grammar/response_format second (needs a real GBNF parser in the engine, or a decision to defer to the HRX route).

  2. bong-water-water-bong commented on Oct 30, 2026

    @bong-water-water-bong
    CollaboratorAuthor

    Closed by #635 (merged 2026-10-30, main 38e29a2): sampling controls (temperature/top_p/top_k/seed/stop) and a GBNF/JSON output constraint on the NPU forward-serve route.

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancement

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions