Skip to content

Fix MLX min_new_tokens for EOS and stop tokens - #42155

Open
Manfredss wants to merge 1 commit into
sgl-project:mainfrom
Manfredss:fix_42153
Open

Manfredss wants to merge 1 commit into
sgl-project:mainfrom
Manfredss:fix_42153

Conversation

@Manfredss

@Manfredss Manfredss commented Oct 1, 2026 •

Copy link
Copy Markdown
Contributor

Motivation

On MLX, min_new_tokens=4 can still stop after one EOS or requested stop token. Fixes #42153.

Modifications

Prevent EOS and requested stop tokens from being selected until the minimum is reached, including in greedy decoding. Add regression tests for decoding and cache reuse.

Accuracy Tests

On Apple M4 with mlx-community/Qwen3-0.6B-4bit, both cases stopped after 1 token on unmodified main. With this fix, forced EOS stopped at token 5 and an explicit stop-token request generated 8 tokens (the configured maximum). The min_new_tokens=0 controls still stop after 1 token. Related MLX tests: 79 passed, 19 subtests passed.

Speed Tests and Profiling

128-token greedy decode, 5 measured requests: 0.559 s median on main, 0.563 s with the fix. Output token IDs matched.

Checklist

  • Pre-commit passed (Rust-only hooks skipped; no Rust files changed).
  • Added and ran regression tests.
  • No documentation change needed for this existing option.
  • Included accuracy and speed results.
  • Followed SGLang code style.

CI States

Latest PR Test (Base): ❌ Run #36942503174
Latest PR Test (Extra): ❌ Run #36942502890
Latest PR Test (AMD ROCm 10): ❌ Run #36942503134

@Yihmyqz

Yihmyqz commented Nov 28, 2026

Copy link
Copy Markdown

I tested this on an M2 MacBook Air with 8 GB of unified memory, using mlx-community/Qwen3-0.6B-4bit on macOS 27.2.1 (MLX 0.32.2, mlx-lm 0.31.3). I compared base 5423a4d885 with PR commit f7dbde7074, using greedy decoding and max_new_tokens=8.

Request min_new_tokens Base PR
Forced EOS 0 1 1
Forced EOS 4 1 5
Explicit stop token 0 1 1
Explicit stop token 4 1 Metal out of memory

I reproduced both early stops on the base commit. With the PR, the forced-EOS case matched your M4 result: four tokens were generated before EOS appeared as the fifth.

For the explicit-stop case, I used the prompt The capital of France is and stop_token_ids=[12095], the first token from greedy decoding. With min_new_tokens=4, the PR run hit kIOGPUCommandBufferCallbackErrorOutOfMemory in async_chained_decode_mlx on two attempts, including one with a 256-token KV pool and one concurrent request. I couldn't verify the final token count for that case on this machine.

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug][MLX] min_new_tokens allows early EOS and stop-token termination

2 participants