Skip to content

serve: logit_bias is read, and plain steps and MTP passes draw from the same biased rows - #966

Merged
juliankang4 merged 1 commit into
midagedev:mainfrom
juliankang4:dgx-spark-q38-usab-logit-bias
Oct 29, 2026
Merged

juliankang4 merged 1 commit into
midagedev:mainfrom
juliankang4:dgx-spark-q38-usab-logit-bias

Conversation

@juliankang4

Copy link
Copy Markdown
Collaborator

logit_bias를 읽어서 일반 step과 MTP 검증 단계가 같은 bias가 더해진 행에서 토큰을 뽑게 했습니다.

What and why

logit_bias was a 400 on every route. This PR reads it on /completion, /v1/completions, /v1/chat/completions and /v1/responses (also /responses): the object form from token id to a number in -100..=100, added to that id's logit before every pick. It works for greedy, sampled and structured-output requests, with or without the MTP draft. Plain steps and MTP verification steps apply the same row transform, so a drafted server returns the same ids as an undrafted one. A structured-output request adds the bias before the schema mask, so the bias reorders the ids the schema allows and lets in no other. Under ignore_eos the stop ids stay at -inf. logprobs report the model's own row. Malformed input (a non-object, a key that is not a vocabulary id, a value out of range, one id named twice, in either spelling or as a repeated JSON key) is a 400 naming the field. generation_settings.logit_bias reports the applied bias.

Release notes: Added: logit_bias is accepted on /completion, /v1/completions, /v1/chat/completions and /v1/responses, with the same ids from the MTP draft as from a plain step.

Code shape

The review reads these first. A box left unticked needs one line of why.

  • No second copy. Logic two models (or two call sites) share has one common owner; I lifted it there instead of
    copying it (AGENTS.md "Common code first", docs/rust-quality.md R14). Where I found an existing copy, I
    named it below.
  • Model code holds only architecture facts. A model's own files carry only what its architecture forces
    (tensor shapes, the attention kind, the quant). A common owner gained no per-model branch, flag or trait method.
  • Dead code is gone. Nothing I made unused is left behind (functions, flags, gate clauses, docs).
  • No silent failure. Undefined input is a named error or panic, never a defined-looking output.
  • Comments state what is true now. No dates, issue numbers or how a bug was found (tools/check-comments.sh).
  • No relaxed gate. No band, tolerance or lint moved to make a check pass; a re-pin carries PIN(YYYY-MM-DD):
    and its reason.
  • cargo fmt and cargo clippy are clean on what I touched; the lint count did not go up.

Duplication found or removed (path:line), or "none": the existing banning helper in crates/serve/src/genloop.rs is generalized into Bias and biased, which the step path (Gen::answer) and the MTP path (Engine::advance_sampled) both use. No copy was added.

What I ran

Base 7163db37ff5c25f41870df1692bce5777536f22e. Model: Qwen3.8-Flash-Next-FP4-Local. Server: BLOOMERY_DRAFT={mtp,off} bloomery-serve -m <Qwen3.8-Flash-Next-FP4-Local folder> --parallel 1 --ctx-size 16896.

The serve GPU gate (the same test list as the gate-serve recipe, --include-ignored) on the card:

test result: ok. 391 passed; 0 failed   (lib)
test constrain::tests::a_bias_reorders_the_allowed_ids_and_lets_in_none ... ok
test hw_logit_bias_moves_the_picked_ids ... ok
gate-serve rc=0

Real bloomery-serve on /completion (prompt The capital of France is, 24 tokens, return_tokens; id 11751 is the first token " Paris", id 41674 is " banana"), each case run with BLOOMERY_DRAFT=mtp and BLOOMERY_DRAFT=off:

  • no logit_bias, {} or {"11751": 0}: ids [11751, 13, 561, ...], the same ids as no bias
  • {"11751": -100} (greedy, temperature 0.8 seeds 7 and 8): id 11751 absent from the returned ids
  • {"41674": 100} (greedy, seeds 7 and 8): every returned id is 41674
  • {"11751": -100, "41674": 4.5}: same ids with and without the draft
  • {"11751": 101}: 400 logit_bias["11751"] must be a number in -100..=100, not 101
CHECK PASS plain: mtp ids == off ids        (and empty, zero, ban_first, force_x, mixed, s7_*, s8_*: 15 cases)
CHECK PASS mtp ban_first: id 11751 absent
CHECK PASS mtp force_x: every id 41674
CHECK PASS mtp settings_bias [{'bias': -100.0, 'token': 11751}]
CHECK PASS mtp zero bias reported []
CHECK PASS mtp 101 refused: {'code': 400, 'message': 'logit_bias["11751"] must be a number in -100..=100, not 101', ...}

Structured output (/v1/chat/completions, response_format json_schema enum Paris or Lyon), same result with and without the draft:

CHECK PASS mtp schema plain: {"answer": "Paris"}
CHECK PASS mtp schema force_x: {"answer": "Paris"}      (+100 on an id outside the schema; the mask wins)
CHECK PASS mtp schema ban_paris: {"answer": "Lyon"}     (-100 on the "Paris" token 57590)

/v1/completions and /v1/chat/completions with {"41674": 100} give "banana banana ..." and match between the two servers. Banning the first token on those two routes, by id (MTP server):

CHECK PASS /completion ban: id 11751 absent
CHECK PASS /v1/completions ban text == /completion ban text
chat plain 200 [57590, 248046] "Paris"
chat ban 57590 200 [11751, 248046] "Paris"
CHECK PASS /v1/chat/completions ban: id 57590 absent from the reply's ids
ALL PASS

Request-parse gate and real server on the card (head 3f08d5ba0a1a2a014c89bf57171a996482d83c56, MTP draft, same model and server command), covering a repeated logit_bias key on every route and n=2 with a ban:

bash tools/gate.sh -p bloomery-serve --test penalty -- --include-ignored hw_bad_penalties_are_named_400s
test hw_bad_penalties_are_named_400s ... ok
test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured; 5 filtered out
bloomery-serve 0.3.10 (commit 3f08d5ba0a1a2a014c89bf57171a996482d83c56)
CHECK PASS /completion ban: id 11751 absent
CHECK PASS /v1/completions n=2 with logit_bias: both choices keep the ban
CHECK PASS /v1/chat/completions ban: id 57590 absent from the reply's ids
CHECK PASS /completion duplicate logit_bias key -> 400
CHECK PASS /v1/completions duplicate logit_bias key -> 400
CHECK PASS /v1/chat/completions duplicate logit_bias key -> 400
CHECK PASS /v1/responses duplicate logit_bias key -> 400
CHECK PASS /responses duplicate logit_bias key -> 400
ALL PASS

Hardware (if the change is for a machine)

DGX Spark, NVIDIA GB10, driver 595.84.

AI help

AI 코딩 에이전트가 코드를 작성한 후 설명을 썼고, 테스트를 마쳤습니다. diff 리뷰와 PR 초안 리뷰는 제가 직접 했습니다.

…he same biased rows

OpenAI's logit_bias object (token id to a number in -100..=100) was a 400.
It is now added to each row before every pick by one owner, genloop's Bias:
the step's draw and an MTP pass's verified-row draw run the same transform,
so a drafted run takes the plain run's ids. ignore_eos's stop ids join it
as -inf. A greedy request reads its row only where the bias can move the
argmax. A structured-output request adds the bias before the schema's mask,
so the bias reorders the allowed ids and lets in no other; logprobs read the
row before the bias. Other forms (llama-server's array of pairs, false, a
token's text), ids past the vocabulary, values out of range and one id
named twice are 400s naming the field. generation_settings reports the
bias as llama-server does.
@github-actions github-actions Bot added documentation Improvements or additions to documentation serve The HTTP server: crates/serve labels Oct 29, 2026

@github-actions github-actions Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Verdict: CLEAN: No must-fix finding remains after validation.

Non-blocking

⚠ Complete gate evidence: crates/serve/tests/penalty.rs:200. The PR reports passing runs but no FAIL-first output. Record failures with the bias addition and duplicate-key guard removed, followed by the passing gate command. Include the nvidia-smi card and driver output to complete the card evidence.

Optimization headroom

  • E. Less work: crates/serve/src/genloop.rs:1964. Structured requests with a nonzero bias and no logprobs can modify their owned logits buffer directly. This avoids one full row copy, 4 × n_vocab bytes per decision token [derived]; requests with logprobs still need the original row.

Checked

Duplicates: plain draws, draft draws and schema choices use the shared Bias transform. Dryer reported zero pairs.
Complexity: crapper flagged no changed function.
Silent failure: invalid bias ids and values produce named 400s. Bias ids are checked against the tokenizer vocabulary that sizes the logits row.
Tests: static mutation inspection confirms assertions for the bias addition, signed-zero normalization, schema transform, sampler wrapping, and range, vocabulary and duplicate-key checks. Mutations were not executed.
Gates: no tolerance or threshold moved; no existing test was removed.
Adversarial checks: out-of-vocabulary ids and duplicate ids with alternate decimal spellings are rejected. Bias precedes schema selection and stop bans.
Escape catalog: C1–C4 do not match the changed code. No ledger lessons are in force.

Notes

Review was static. No build or gate was run here. GPU evidence is incomplete because the PR omits the nvidia-smi output.

🔨 Shingler, bloomery's review bot: approves a clean pull request and requests changes on a must-fix (how it works)

@juliankang4

Copy link
Copy Markdown
Collaborator Author

The review is done and the contribution check passed, so I'm merging this.

@juliankang4
juliankang4 merged commit 686e8cc into midagedev:main Oct 29, 2026
7 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

documentation Improvements or additions to documentation serve The HTTP server: crates/serve

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant