Skip to content

perf(inference): incremental per-sequence state caches for repetition/frequency/DRY sampling #328

Closed

Description

@inureyes

Summary

Maintain incremental per-sequence sampler state so repetition, frequency/presence, and DRY penalties, plus selected-token logprobs, stop rebuilding sorted sets, hashmaps, and full-vocab vectors every token in long generations.

Source: idea.md "6. Sampling Option State Caches" (P2, expected payoff low to medium, risk low-medium).

Current state

  • Repetition penalty copies, sorts, and deduplicates token history each token (src/lib/mlxcel-core/src/sampling.rs, around line 336).
  • Frequency/presence penalty rebuilds a token-count HashMap and allocates a full-vocabulary penalty vector each token (src/lib/mlxcel-core/src/sampling.rs, around line 366).
  • DRY penalty intentionally runs CPU-side sequential pattern matching and builds maps for the current window (src/lib/mlxcel-core/src/sampling.rs, around line 396).
  • compute_logprobs() applies full log-softmax plus extra gathers/evals when enabled (src/lib/mlxcel-core/src/sampling.rs:530).

Proposal

  • Maintain incremental per-sequence sampler state:
    • sorted/deduped seen-token set for repetition penalty,
    • token counts for frequency/presence penalty,
    • reusable sparse index/value buffers for touched tokens,
    • optional DRY window index cache for long outputs.
  • Add a selected-token-only logprob fast path when top_k == 0, IF the numerical semantics can remain OpenAI-compatible.

Validation

  • Microbench long generations with repetition/frequency/DRY enabled and disabled.
  • Server test with mixed option usage to ensure no baseline overhead when options are off.
  • Deterministic parity for penalty-adjusted greedy sampling.

Acceptance criteria

  • Per-sequence incremental state replaces per-token rebuilds for repetition and frequency/presence penalties.
  • Reusable sparse buffers avoid full-vocab allocation per token for touched tokens.
  • Optional DRY window index cache for long outputs, with behavior unchanged.
  • Selected-token-only logprob fast path for top_k == 0, only if OpenAI-compatible numerics are preserved; otherwise documented as not feasible.
  • NO added baseline overhead when penalties and logprobs are off, verified by the server test.
  • Deterministic parity for penalty-adjusted greedy sampling.

Risk and priority

idea.md priority P2; risk low-medium. Does not help the default no-penalty path; it reduces CPU and allocation overhead for OpenAI-compatible sampling features in long generations.

References

  • idea.md "6. Sampling Option State Caches".
  • Code: src/lib/mlxcel-core/src/sampling.rs (repetition/frequency/DRY penalties, compute_logprobs).

Activity

  1. added
    type:performance
    priority:low
    area:inference
    area:core
    status:in-progress
    status:review
    status:done
    and removed
    status:in-progress
    status:review
    on Jun 17, 2026
  2. added this to the 0.3 milestone on Jun 21, 2026
  3. self-assigned this
    on Jun 21, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

area:corearea:inferencepriority:lowstatus:donetype:performance

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions