Skip to content

fix(speculative): refuse model-owned families in the classic walk - #2535

Merged
inureyes merged 3 commits into
mainfrom
fix/issue-2495-classic-spec-refuse-model-owned
Oct 22, 2026
Merged

inureyes merged 3 commits into
mainfrom
fix/issue-2495-classic-spec-refuse-model-owned

Conversation

@inureyes

@inureyes inureyes commented Oct 22, 2026 •

Copy link
Copy Markdown
Member

Summary

The classic --draft-model walk drops a rejected draft block by trimming the external KV caches of both models. On a family whose sequence state lives elsewhere (Qwen3-Next's recurrent GDN layers, model-owned caches) the trim rolls nothing back, so later rounds condition on stale positions, and the can_trim_prompt_cache asserts pass trivially on the placeholder caches. This takes fix option 1 from the issue: refuse such models instead of trying to rewind them, since GDN state cannot be rewound exactly by a trim.

  • SpeculativeGenerator::generate resets the generator, then checks the target and the draft with prompt_lookup_unsupported_reason (the rule Engine::verify_rounds_unsupported already applies) before any prefill or forward, and now returns Result<_, SpeculativeGenerateError>. The error has TargetUnsupported and DraftUnsupported variants carrying the reason string and is re-exported from the mlxcel crate next to SpeculativeGenerator. Resetting first means a reused generator does not keep the tokens of an earlier successful call after a refused one.
  • mlxcel generate --draft-model refuses an unsupported target before it loads the draft checkpoint, and maps a refusal from generate (which also covers the draft) through the same error text. The message points at plain generation without --draft-model; it does not suggest --prompt-lookup or the engine drafters, because they refuse the same models. The explicit --draft-kind error that recommends the classic path now says it refuses such targets.
  • Behavior change: the shared rule also refuses families that opt out of padded prefill (Gemma 4, Llama 4 per the rule's doc comment), so those models now get an error on the classic path where they previously ran.
  • Library callers of generate must handle the Result (the type is already deprecated, refactor: run mlxcel generate on the engine and retire CxxGenerator #2176). The existing can_trim_prompt_cache and empty-prompt asserts are unchanged.

Test plan

  • cargo test --profile test-fast -p mlxcel-core --lib speculative::: 178 passed. generate_refuses_a_model_owned_target_before_any_forward and generate_refuses_a_model_owned_draft_before_any_forward loop over all three reasons of the rule (no batched decode, a model-owned layout on a batchable model, no padded prefill), assert the error names the refused model with the rule's reason, and assert neither mock's forward ran. refused_call_clears_tokens_from_a_previous_success runs a dense call and then a refused call on one generator. The distribution tests now expect the Ok, so a refused dense mock cannot pass silently.
  • Revert check: with both gate calls replaced by None, speculative::tests gives 15 passed, 3 failed (the three tests above); restored afterwards. Each refusal loop stops at its first failing state kind, so only that case was observed failing.
  • cargo test --profile test-fast -p mlxcel --bin mlxcel commands::generate: 101 passed, 1 ignored.
  • cargo clippy --profile test-fast -p mlxcel-core --lib --tests -- -D warnings and cargo clippy --profile test-fast -p mlxcel --lib --bin mlxcel --tests -- -D warnings: clean.
  • cargo fmt --all --check

Unverified: no real Qwen3-Next (or other model-owned checkpoint) run through mlxcel generate --draft-model; the refusal is exercised through mock models at the SpeculativeGenerator::generate entry point, and the CLI's early target check is covered by compilation and code reading only. The LoadedModel wrapper used by the CLI delegates supports_batching, sequence_state_layout, and supports_padded_prefill to the model (read, not executed).

Closes #2495

The classic draft-model walk drops a rejected draft block by trimming the external KV caches of both models. A family that keeps its sequence state elsewhere (Qwen3-Next's recurrent layers, model-owned caches, families that opt out of padded prefill) keeps the rejected tokens, so later rounds condition on stale positions, and the can_trim_prompt_cache asserts pass trivially on the placeholder caches.

SpeculativeGenerator::generate now checks the target and the draft with prompt_lookup_unsupported_reason, the rule Engine::verify_rounds_unsupported already applies, before any state change or forward pass, and returns a new SpeculativeGenerateError naming the refused model and the reason. The generate CLI maps the error into a message that points at plain generation. Tests cover a refused target and a refused draft for both reason branches, asserting no forward ran.

Closes #2495
@inureyes inureyes added status:review Under review type:bug Bug fixes, error corrections, or issue resolutions priority:low Low priority area:core mlxcel-core: MLX FFI, primitives, KV cache, layers labels Oct 22, 2026
@inureyes

Copy link
Copy Markdown
Member Author

Implementation Review Summary

Intent

Refuse model-owned / recurrent families (target or draft) in the classic SpeculativeGenerator::generate walk with an explicit error instead of silently corrupting state after a rejected round (#2495, fix option 1).

Findings Addressed

None needed: no CRITICAL or HIGH findings.

Remaining Items

  • The refusal fires after the draft model is loaded and after the "running the classic SpeculativeGenerator path" lines print, so a refused target still pays the draft load. A target-only pre-check before load_model(draft_model_path) would fail faster (LOW, UX only; the gate in generate stays the source of truth).
  • The refusal tests pin the supports_batching() == false and model-owned-layout branches of the shared rule but not the supports_padded_prefill() == false branch, which is the Gemma 4 / Llama 4 behavior change; a third StateKind would pin it (LOW, test coverage).
  • The explicit --draft-kind dflash error still advises omitting --draft-kind to use the classic path, which now refuses model-owned targets (LOW, wording).

Verification

  • All stated requirements implemented (gate on both models via prompt_lookup_unsupported_reason, before reset(), typed SpeculativeGenerateError naming target or draft, # Errors doc, asserts kept)
  • No placeholder/mock code remaining
  • Integrated into project code flow (run_generation_mode propagates with ? before the acceptance summary; LoadedModel delegates all three flags)
  • Project conventions followed
  • Existing modules reused where applicable (same rule as Engine::verify_rounds_unsupported)
  • No unintended structural changes (Gemma 4 / Llama 4 refusal on the classic path is the documented intended change)
  • Tests pass: speculative:: 177 passed; commands::generate 101 passed; cargo check -p mlxcel --lib --tests, clippy -D warnings on -p mlxcel-core and -p mlxcel (--lib --tests), and cargo fmt --check clean. Re-ran the revert check independently: with the gate removed both refusal tests fail with Ok([2, 2, 2, 2, 2, 2, 2, 2]).

The classic --draft-model path now checks the target against the shared rollback rule before printing "Loading draft model" and loading the checkpoint, so a refused target no longer pays for a full draft load. The in-generate gate stays the authority and also covers the draft. The CLI error text is shared by both sites.

SpeculativeGenerator::generate now resets its own caches and counters before the gate, so a reused generator does not keep a previous successful call's tokens after a refused one. The explicit --draft-kind error notes that the classic path refuses targets whose state a cache trim cannot roll back.

Tests add a padded-prefill opt-out state (the Gemma 4 / Llama 4 shape) to both refusal tests and a reuse test for the stale-token case. With the in-generate gate removed, the refusal tests fail.

Validation: cargo test --profile test-fast -p mlxcel-core --lib speculative:: (178 passed), cargo test --profile test-fast -p mlxcel --bin mlxcel commands::generate (101 passed), clippy -D warnings clean on both packages.

Refs #2495
@inureyes inureyes added status:done Completed and removed status:review Under review labels Oct 22, 2026
@inureyes
inureyes merged commit 165ffd4 into main Oct 22, 2026
54 of 55 checks passed
@inureyes
inureyes deleted the fix/issue-2495-classic-spec-refuse-model-owned branch October 22, 2026 10:23
@inureyes inureyes added this to the 0.8 milestone Nov 30, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area:core mlxcel-core: MLX FFI, primitives, KV cache, layers priority:low Low priority status:done Completed type:bug Bug fixes, error corrections, or issue resolutions

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Classic speculative walk cannot rewind model-owned caches after a rejected round

1 participant