fix(inference): keep long prompts whole in lattice chat - #1793
Merged
Merged
Conversation
`lattice chat` tokenized each line with the model's default 4096-token tokenizer cap, so on the CPU backend a prompt between 4097 tokens and the model's context window was generated from its first 4096 tokens, and the printed prompt count showed the shortened length. On the Metal backend an over-long prompt was rejected with a count taken from the already-shortened prompt, and with a zero decode budget it was shortened silently. The CPU backend now raises the tokenizer cap to the model's context window when it loads, as `lattice serve` does. Before generating, both backends count the prompt's full length and refuse a prompt longer than the context window with an error naming its real token count and the limit; the REPL reports the error and reads the next line.
E2E Parity ReportPASS: 3/4 gating prompts match; 1 known divergence (#535) excluded from the verdict
|
|
E2E Parity ReportPASS: all 4 prompts match within their respective match windows
|
print(fib
|
Q4 perplexity regression gate (#616)
|
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.
Fixes #1788.
What was wrong
lattice chattokenized each line with the model's default 4096-token tokenizer cap.Qwen35Model::max_context()ismin(max_position_embeddings, 8192), but generation re-tokenizes with the capped tokenizer and keepsinput_ids[..real_length]. A prompt of 4097..max_context tokens was generated from its first 4096 tokens, and the printed[N prompt tokens, …]showed 4096.MAX_CACHE_LEN). An over-long prompt was rejected by the context-budget check, but with a count taken from the already-shortened prompt. With a zero decode budget it was returned as a shortened, empty generation.Change
All runtime changes are in
src/bin/lattice/chat.rs:Backend::cpuraises the CPU model's tokenizer cap tomax_context()at load, usingQwen35Model::ensure_tokenizer_max_seq_len(added in fix(serve): keep long chat prompts whole on the CPU serving path #1787). This matcheslattice serve's CPU backend.generate_checkedcounts the prompt's full length (pre_truncation_lenfrom the tokenizer the backend generates with). Before calling generation, it refuses a prompt longer than the backend's context window withInvalidInput("prompt (N tokens) exceeds model context window (LIMIT)"). Both the CPU and Metal backends go through it.Backend::generate_chat_line. The REPL still passes the trimmed line, prints the error, and reads the next line.tests/data/pipeline_boundary_baseline.txtgains one row for the test module's use oftest_support::tiny_zero_model_with_context, the same fixturebin/lattice/main.rstests already use.Not changed:
src/bin/chat_metal.rsis a separate executable with the same 4096 tokenizer/cache pairing. Its prefix-cache path reports shortened counts the same way the Metal backend here did. It is left for its own change.Tests
New tests in
chat.rs, undercfg(all(test, feature = "test-utils")):cpu_chat_generation_keeps_long_promptsGenerateOutput.prompt_tokensfrom real CPU generation for 4097 and 8192 tokens, with context 8192 and initial cap 4096cpu_chat_refuses_full_count_and_accepts_next_linechat_guard_refuses_before_generation_with_metal_tokenizer_capmetal_chat_uses_checked_generation(metal-gpu)max_context()repl_uses_checked_generation_and_cpu_initializationBackend::cpuandgenerate_chat_line(trimmed, …)The two source-level tests guard wiring that the other tests can't reach. The REPL reads stdin, and the Metal fixtures that fit the flash-decode shape are private to the library's own tests. No real Metal load or generation runs in this PR's tests.
Each test was checked for mutation sensitivity on an Apple silicon Mac mini. Each fix line was removed in turn (then
touch), the named test run, and the line restored:cpu_chat_generation_keeps_long_promptschat_guard_refuses_before_generation_with_metal_tokenizer_cappre_truncation_len→real_length)chat_guard_refuses_before_generation_with_metal_tokenizer_capcpu_chat_refuses_full_count_and_accepts_next_linemetal_chat_uses_checked_generationrepl_uses_checked_generation_and_cpu_initializationGate (Apple silicon Mac mini, at this head's tree on base a243592)
cargo fmt --all -- --checkrc 0-D warnings,-p lattice-inference --all-targets, rc 0 with each feature set:test-utils;f16,metal-gpu,test-utils;--release f16,metal-gpu,bench-internals;f16,metal-gpu; default;mixturecargo test -p lattice-inference --bin lattice --features test-utils: 191 passedcargo test -p lattice-inference --bin lattice --features f16,metal-gpu,test-utils: 203 passedpipeline_boundary_contract: 14 passed at default features and atmetal-gpu,f16bench-compare disposition
No bench run: this is a structural waiver. The runtime change is confined to the
latticebinary's interactivechatsubcommand (src/bin/lattice/chat.rs), which is reached only throughCommand::Chatinbin/lattice/main.rs. I searched every declared target in every crate manifest: 25 bench targets and 14 bin targets incrates/inference, 5 benches and 2 bins incrates/embed, 1 bench each incrates/fannandcrates/transport, and 4 bins incrates/tune. None of them calls the chat module,generate_chat_lineorgenerate_checked. The other changed file is test baseline data. Manifests, lockfile, features, profiles and bench harnesses are unchanged. Residual risk: the added per-line tokenization for the length check is unmeasured. It runs once per interactive line, outside any timed region.Files changed: 2 · commits: 1