Fix batch generation and adopt mlx-lm batch improvements#911
Open
Fix batch generation and adopt mlx-lm batch improvements#911
Conversation
- Add `tokens`, `samplers`, and `logits_processors` fields to Batch class with proper filter/extend support - BatchGenerator.insert() now accepts per-sequence samplers and logits_processors for fine-grained control (e.g. mixed temperature) - _step() applies per-sequence logits processors and samplers during generation, falling back to shared sampler when not provided - Add missing `nbytes` and `empty()` to SlidingWindowCache and StaticKVCache to satisfy _BaseCache interface from mlx-lm Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
…oken handling - Introduced a new `_right_pad_prompts` function for right padding of prompts. - Integrated `SequenceStateMachine` to manage stop token detection, allowing for multi-token sequences. - Updated `Batch` class to support state machine states, ensuring proper handling during filtering and merging. - Modified `BatchGenerator` to utilize the state machine for improved stop detection logic. - Ensured backward compatibility with legacy stopping criteria while enhancing functionality.
…offset Three bugs that caused garbage output when batch_size > 1: 1. Vision tower flattened all batch image tokens into [1, total, dim], losing the batch dimension. Now preserves [B, tokens_per_image, dim]. 2. masked_scatter flattened all batches causing cross-batch index contamination via modulo wrapping. Now processes per-batch for B>1. 3. BatchRotatingKVCache.offset is a mutable mx.array that gets modified by update_and_fetch. The attention code captured this reference before the update but it mutated, causing queries to get wrong RoPE positions. Fixed by snapshotting the offset. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
CI uses released mlx-lm which doesn't have these yet. Gracefully falls back to legacy stopping_criteria when unavailable. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
BatchGenerator.insert()now accepts per-sequencesamplersandlogits_processorslists, enabling mixed-temperature/top-p serving in batch mode. Falls back to shared sampler when not provided — fully backward compatible.Batchtracks generated tokens per sequence in the newtokensfield.nbytesproperty andempty()method toSlidingWindowCacheandStaticKVCacheto satisfy_BaseCacheabstract interface from mlx-lm, preventing breakage on future mlx-lm updates.Inspired by ml-explore/mlx-lm#1072.
Test plan
batch_generate()with Qwen2.5-VL-3B produces correct outputgenerate()unaffectedSlidingWindowCacheandStaticKVCachepassempty()andnbyteschecks🤖 Generated with Claude Code