Skip to content

Generation-time quality controls: token masks, mid-word continuation, KV snapshot#8

Draft
FuJacob wants to merge 2 commits into
mainfrom
feat/generation-quality-controls
Draft

Generation-time quality controls: token masks, mid-word continuation, KV snapshot#8
FuJacob wants to merge 2 commits into
mainfrom
feat/generation-quality-controls

Conversation

@FuJacob
Copy link
Copy Markdown
Owner

@FuJacob FuJacob commented May 31, 2026

Summary

Adds generation-time quality controls so the app can stop emitting unwanted tokens at the source. All behavior is opt-in or backward compatible; existing single-sequence and multi-sequence paths are unchanged.

  • buildTokenMasks runs once per model load and classifies the whole vocabulary. Control, unknown, and unused tokens get a -inf logit bias (via llama_sampler_init_logit_bias in buildSampler) so they can never be sampled as visible text. EOG tokens are deliberately left sampleable so the existing stop check keeps working.
  • New single_line flag on SamplingConfig additionally masks line-break tokens.
  • setForceWordContinuation(seq, enabled) constrains the first (seed) token of the next generation to continue the current word (masks whitespace-leading tokens for that one token).
  • snapshotSize / snapshotSequence / restoreSequence wrap llama single-sequence KV state copy, serialized with the decoder thread (an enabler for future multi-candidate decoding; nothing calls them yet).
  • SampleResult.logprob: computeLogprob returns the chosen token's log-probability under the raw model distribution (<= 0), on both the batched sample path and the seed token. Lets the app suppress low-confidence completions.

Validation

swift build
# Build complete!

COTABBY_TEST_MODEL_PATH=<gemma-3-1b-it Q4_K_M> swift test
# Executed 20 tests, with 0 failures   (exit 0)

New tests: no-model guards for setForceWordContinuation and snapshotSize; model-gated tests for first-token word continuation, the snapshot/restore round-trip, and a finite logprob. Existing tests still pass, including testInterleavedMultiSequenceSampling (greedy determinism intact, so the always-on nonprintable mask does not perturb normal sampling). Without COTABBY_TEST_MODEL_PATH, model-gated tests skip and the suite is green, matching CI.

Notes

  • The nonprintable mask is always on (no flag) because it only removes tokens that should never be visible text, and it excludes EOG so natural stopping still works.
  • Fixed a latent end-to-end test bug: testEndToEndWithModel detokenized tokens[0], which for models that prepend BOS is a control token that renders to 0 bytes. It now detokenizes a content token.
  • SamplingConfig gained one trailing field (single_line); all in-repo constructors are updated. SampleResult gained logprob (read-only for callers).

…ion, KV snapshot

- buildTokenMasks classifies the vocab once per model load: control, unknown, and
  unused tokens get a -inf logit bias so they can never be sampled as visible text.
  EOG is deliberately left sampleable so the existing stop check still fires.
- single_line SamplingConfig flag additionally masks line-break tokens so
  single-line fields never receive a multi-line completion.
- setForceWordContinuation constrains the first (seed) token of a generation to
  continue the current word (masks whitespace-leading tokens) for mid-word carets.
- snapshotSize / snapshotSequence / restoreSequence wrap llama single-sequence KV
  state copy, serialized with the decoder thread.

Tests: new no-model and model-gated tests for the masks, mid-word continuation, and
snapshot/restore round-trip. Also fixes a latent end-to-end test bug where
detokenizing tokens[0] (BOS, a control token) returned 0 bytes.
computeLogprob returns the chosen token's log-probability under the raw model
distribution (<= 0). It is set on both the batched sample path and the seed token
sampled at decodePrompt, and returned with the seed via SampleResult.logprob. This
lets the app suppress low-confidence completions. Adds a model-gated test.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant