Skip to content

fix(inference): evaluate embedding prefill logits before after_prefill - #2489

Merged
inureyes merged 1 commit into
lablup:mainfrom
hoyajigi:fix/issue-2177-embedding-prefill-eval-order
Oct 20, 2026
Merged

inureyes merged 1 commit into
lablup:mainfrom
hoyajigi:fix/issue-2177-embedding-prefill-eval-order

Conversation

@hoyajigi

Copy link
Copy Markdown
Member

Summary

Fixes what is left of the two decode-path defects in #2177 after epic #2166 retired CxxGenerator (#2225), in the code that replaced it:

  1. Engine::prefill now always evaluates an embedding (VLM) prefill's logits before running LanguageModel::after_prefill, instead of relying on every caller to set PrefillStep::eval. The hook may replace weight arrays the lazy prefill graph still references.
  2. The remaining comments and docs that still describe --no-batch as bypassing the scheduler, or a "classic generator" speculative path, now match the unified engine.

Related issues

Closes #2177

Type of change

  • fix — bug fix

Test plan

  • New src/lib/mlxcel-core/src/engine/after_prefill_tests.rs (stub model that records every forward and after_prefill call): embedding_prefill_evaluates_its_logits_even_with_eval_off fails on main (verified by reverting prefill.rs) and passes with the fix; plus text prefill keeps the eval deferred and skips the hook, both session embedding entry points (plain and --profile) run the hook exactly once between the prefill forward and the first decode forward with identical tokens, and text generation never runs the hook
  • cargo test --profile test-fast -p mlxcel-core --lib filters after_prefill (4), engine:: (68), session (13), generate (52): all pass on both the CPU backend and Metal
  • cargo clippy -p mlxcel-core --lib --tests -- -D warnings and cargo clippy -p mlxcel --lib --tests -- -D warnings (Metal build): clean
  • cargo fmt --all -- --check
  • Validated with a real checkpoint on Metal (Apple M3 Pro): mlx-community/Qwen2-VL-2B-Instruct-4bit, mlxcel generate --image <img> --temp 0 -n 64, with and without --profile, fixed binary vs a baseline with prefill.rs from main: identical output text on two images in all four runs
  • make verify, CUDA gate, server image path end to end: not run (the server's embedding prefill already passed eval: true, so its behavior does not change)

Notes for reviewers

  • The only behavioral line is let eval = if step.eval || step.embeddings.is_some(). Today all three embedding callers (DirectEngine::prefill_embeddings and the scheduler's run_embedding_prefill) already pass eval: true and no family overrides after_prefill, so output does not change; this moves the ordering guarantee into the one function that calls the hook.
  • Text prefill is unchanged: the terminal piece's eval still waits for the first sample.
  • Asserting eval-before-hook order directly would need a new FFI query for "is this array evaluated"; the tests instead observe that the eval happened and the call order, and the order is fixed structurally in Engine::prefill.
  • Comment-only fixes: docs/CONTINUOUS_BATCHING.md (--no-batch runs the same scheduler at max_batch_size=1), ServerConfig::no_batch, ModelProvider::new_with_server_config, and speculative_dispatch.rs (now names run_generation_mode, drops stale CLI line numbers). // Used by: docs updated on Engine::prefill, PrefillStep::eval and LanguageModel::after_prefill.
  • Not changed: the user-facing error string in src/server/model_provider.rs still says --no-batch bypasses the batch scheduler; the issue scoped this part to comments, so it is left for a follow-up.

Checklist

  • PR title uses a Conventional Commits prefix
  • One logical change per PR
  • Updated docs/
  • Updated // Used by: ... comments on shared functions modified
  • No secrets, credentials, or .env files committed

Two defects found while mapping the CLI and server decode paths (lablup#2177).
Epic lablup#2166 retired CxxGenerator after the issue was filed, so both are
fixed against the engine that replaced it.

1. after_prefill ordering. LanguageModel::after_prefill may replace
   weight arrays the lazy prefill graph still references, so the prefill
   logits must be evaluated before it runs. Engine::prefill, now the only
   caller, ran the hook after an eval only when the caller set
   PrefillStep::eval; every embedding caller passes true today, but the
   order lived at each call site. Engine::prefill now always evaluates an
   embedding piece's logits before the hook, whatever `eval` says, and
   reports that eval in PrefillOutcome::eval. Text pieces are unchanged:
   the terminal piece's eval stays with the first sample. No family
   overrides the hook, so output does not change.

2. Comments that misdescribe the CLI/server split. Most sites the issue
   lists were rewritten or removed by the epic. The remaining ones are
   corrected: --no-batch runs the same BatchScheduler at
   max_batch_size=1 (docs/CONTINUOUS_BATCHING.md, ServerConfig::no_batch,
   ModelProvider::new_with_server_config), the Classic speculative arm
   declines to plain decode and drafts nothing (speculative_dispatch.rs),
   and the CLI dispatch reference names run_generation_mode instead of
   stale line numbers. Comment-only.

Tests in engine/after_prefill_tests.rs use a recording stub model that
overrides after_prefill: an embedding prefill with eval off still
evaluates (its failing logits surface in PrefillOutcome::eval; this test
fails on main), a text prefill neither forces the eval nor runs the hook,
and both session embedding variants (generate_streaming_with_embeddings
and generate_with_stats_and_embeddings) run the hook exactly once,
between the prefill forward and the first decode forward.

Closes lablup#2177
@inureyes

Copy link
Copy Markdown
Member

Implementation hardening

Reviewed PR #2489 against issue #2177, including the replacement of CxxGenerator by the shared engine. The engine owns embedding-logit evaluation before the after-prefill hook, and both session entry points use that path. No additional code fix is required.

  • Correctness: inspected the common prefill path, direct/session embedding callers, scheduler embedding caller, deferred text evaluation, eval-error reporting and the recording-model regression tests.
  • Security: no new externally controlled inputs, unsafe operations or permission boundary changes were introduced.
  • Performance/reliability: embedding evaluation is forced only where the hook requires it; deferred text evaluation remains unchanged. Existing embedding callers already request evaluation, so their synchronization count does not increase.
  • Validation: CARGO_TARGET_DIR=/home/inureyes/Development/backend.ai/mlxcel/target cargo test --profile test-fast --features cuda -p mlxcel-core --lib engine:: -- --test-threads=1 passed on NVIDIA GB10: 68 tests, including all four new after-prefill regressions. Existing PR CI checks are passing.
  • Scope note: the pre-existing user-facing no-batch diagnostic still says it bypasses the scheduler, as explicitly disclosed in the PR; the issue requested comment corrections. The author's Metal VLM checkpoint evidence was reviewed; this pass independently validated the shared engine on CUDA.

@inureyes
inureyes merged commit 44aab7d into lablup:main Oct 20, 2026
27 checks passed
@inureyes inureyes added status:done Completed type:bug Bug fixes, error corrections, or issue resolutions priority:low Low priority area:inference Generation, sampling, decoding (incl. speculative, DRY) area:cli Command-line interface / CLI flags labels Oct 20, 2026
@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:cli Command-line interface / CLI flags area:inference Generation, sampling, decoding (incl. speculative, DRY) 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.

fix: small decode-path defects found while mapping CLI vs server decode

2 participants