Generation-time quality controls: token masks, mid-word continuation, KV snapshot#8
Draft
FuJacob wants to merge 2 commits into
Draft
Generation-time quality controls: token masks, mid-word continuation, KV snapshot#8FuJacob wants to merge 2 commits into
FuJacob wants to merge 2 commits into
Conversation
…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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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.
buildTokenMasksruns once per model load and classifies the whole vocabulary. Control, unknown, and unused tokens get a-inflogit bias (viallama_sampler_init_logit_biasinbuildSampler) so they can never be sampled as visible text. EOG tokens are deliberately left sampleable so the existing stop check keeps working.single_lineflag onSamplingConfigadditionally 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/restoreSequencewrap llama single-sequence KV state copy, serialized with the decoder thread (an enabler for future multi-candidate decoding; nothing calls them yet).SampleResult.logprob:computeLogprobreturns 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
New tests: no-model guards for
setForceWordContinuationandsnapshotSize; model-gated tests for first-token word continuation, the snapshot/restore round-trip, and a finitelogprob. Existing tests still pass, includingtestInterleavedMultiSequenceSampling(greedy determinism intact, so the always-on nonprintable mask does not perturb normal sampling). WithoutCOTABBY_TEST_MODEL_PATH, model-gated tests skip and the suite is green, matching CI.Notes
testEndToEndWithModeldetokenizedtokens[0], which for models that prepend BOS is a control token that renders to 0 bytes. It now detokenizes a content token.SamplingConfiggained one trailing field (single_line); all in-repo constructors are updated.SampleResultgainedlogprob(read-only for callers).