Skip to content

fix(metal): keep Mirostat mu across decode steps - #1816

Open
chaitanya0305 wants to merge 1 commit into
dphnAI:mainfrom
chaitanya0305:fix/metal-mirostat-mu-persistence
Open

chaitanya0305 wants to merge 1 commit into
dphnAI:mainfrom
chaitanya0305:fix/metal-mirostat-mu-persistence

Conversation

@chaitanya0305

Copy link
Copy Markdown

On the Metal backend, Mirostat v2 never adapts: mu resets to 2 * tau on every generated token, so the target-surprise feedback loop does not run. Mirostat requests on Apple Silicon effectively sample with a fixed truncation instead.

Repro (CPU, no model needed)

Five decode steps through SamplingBatch + sample_from_logits for a request with mirostat_mode=2, mirostat_tau=2.0, mirostat_eta=0.5, spying on the mu that _fetch_mirostat_args hands to the sampler:

main:  mu seen by each decode step: [[4.0], [4.0], [4.0], [4.0], [4.0]]

With this change, mu carries over and is updated from the previous step's value.

Root cause

Each Metal decode step builds a new SamplingBatch, and SamplingBatch.make_sampling_metadata() passed

persistent_data={index: {} for index in range(len(self.sampling_params_list))}

The Mirostat op reads miro_mu from persistent_data and writes the updated value back there. That dict is thrown away with the batch, so the next step starts from the default again.

Fix

  • RequestState gets a sampler_state dict that lives as long as the request.
  • SamplingBatch takes an optional persistent_data list (one dict per row) and passes those same dicts into SamplingMetadata. Without it, the behaviour is unchanged. A length mismatch raises.
  • The three decode call sites pass the request's sampler_state: sample_decode_tokens (paged path), _batched_decode and _sequential_decode.

The prefill token is still sampled from a fresh state (initial mu = 2 * tau), as before. The running state starts with the first decode step.

Tests

tests/metal/test_sampling_batch_mirostat.py (skipped when mlx is not installed):

  • mu changes across four decode steps of one request and does not restart at 2 * tau;
  • two requests in one batch keep separate state;
  • a persistent_data length mismatch raises.

All 3 tests fail on main and pass with the fix. ruff check and ruff format (v0.17.2, as pinned in pre-commit) are clean.

Related but separate: #1674 fixes the mixed-batch sampler skip in _execute_samplers_in_order. This PR does not touch that code.

Each Metal decode step builds a fresh SamplingBatch, and
make_sampling_metadata() handed the sampler a new empty persistent_data
dict every time. Mirostat v2 stores its running mu there, so mu was
reset to 2 * tau on every token and the target-surprise feedback loop
never ran.

Keep the per-request sampler state on RequestState and pass it into the
batch from both decode paths (batched and sequential) and from
sample_decode_tokens().

Signed-off-by: Chaitanya Deshpande <dcchaitanyadeshpande@gmail.com>

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant