Skip to content

feat: default the single-sequence CLI/bench prefill to chunked (2048) with a per-model opt-out policy #674

Closed

Description

@inureyes

Follow-up of #672.

Summary

The PR for issue #672 added opt-in cache-level chunked prefill to the single-sequence CLI/bench generator (MLXCEL_PREFILL_CHUNK, default 0 = single-pass). This issue proposes flipping the default to 2048 with a per-model opt-out policy hook, matching upstream mlx-lm/mlx-vlm behavior (DEFAULT_PREFILL_STEP_SIZE = 2048 with a chunked_prefill_policy model hook) and the mlxcel server (prefill_chunk_size, default 512).

Context

Measured motivation (GB10 CUDA, from the #672 validation, see benchmarks/cuda_gb10_gemma_longprompt_672_2026-07-06.csv): gemma-4-12b-it-4bit at 32768 prompt tokens drops from 56.8 GB MLX peak (single-pass, after the #672 attention fixes) to 16.6 GB (system peak 23 GB) with prefill speeding up 285 to 706 tok/s; gemma-4-31b-it-4bit completes 32768 tokens at 29.9 GB MLX peak / 38 GB system (single-pass projection is ~74 GB). Decode is unchanged (13.99 vs 14.03 tok/s at 8192 tokens over 64 decode tokens).

The mechanism: rotating sliding-window KV caches trim to their window between chunks instead of holding every prompt token for one pass, per-chunk transients stay chunk-sized, and clear_memory_cache() between chunks prevents cudaMallocAsync pool accumulation across per-chunk shape changes (measured 84 GB system peak without the release for a ~30 GB live set).

Proposed design

  1. Add a LanguageModel trait method such as supports_chunked_prefill() defaulting to true (mirror of mlx-vlm's chunked_prefill_policy), so model families that cannot handle multi-call prefill can opt out.
  2. Make the effective default chunk 2048 when MLXCEL_PREFILL_CHUNK is unset, keep the env override including 0 to force single-pass.
  3. Keep the Apple NA padded-prefill path's current priority decision explicit (today the chunk branch takes precedence over the M5 tile-padding branch when the env is set; decide and document which wins when chunking becomes the default).

Work items

  • Trait hook with default (supports_chunked_prefill() on LanguageModel)
  • Default flip in prefill_chunk_len() (src/lib/mlxcel-core/src/generate.rs)
  • Per-family opt-out audit for models with prompt-time one-shot state or non-standard multi-call prefill, at minimum: gemma3n (per_layer_inputs consumed by the first prefill, see the warmup note in src/bin/bench_decode.rs), recurrent gemma / SSM and linear-attention hybrids (qwen3_5 family), diffusion models (different generate path, likely unaffected), and VLM prefill-with-embeddings paths (currently not routed through the chunked branch and out of scope)
  • Docs update for the new default

Validation plan

  • For a representative set (llama-3.1-8b, qwen2.5-7b, qwen3-30b-a3b MoE, gemma2, gemma3, gemma-4-12b/31b, deepseek-v2-lite MLA, one VLM text path), assert greedy token identity between chunked and single-pass prefill at 8192 prompt tokens over at least 32 decode tokens, plus MLX peak memory reduction recorded per model.
  • Verify speculative/MTP entry points are untouched (they call forward directly, not the chunked branch).
  • Verify the bench profile (mlxcel-bench-decode) reports sensible prefill tok/s under the new default.

Acceptance criteria

  • Default chunked prefill enabled with parity validated on the representative set
  • Opt-out hook exercised by at least one model or a documented rationale that none needs it
  • Env override behavior (0 = single-pass) covered by a unit test
  • Docs mention the default and the env knob

Activity

  1. added
    type:enhancement
    priority:medium
    area:inference
    status:ready
    on Jul 6, 2026
  2. added
    status:done
    and removed
    status:ready
    on Jul 6, 2026
  3. self-assigned this
    on Aug 31, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

area:inferencepriority:mediumstatus:donetype:enhancement

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions