feat: MTP per-request routing in BatchedEngine#223
Closed
Thump604 wants to merge 2 commits intowaybarrios:mainfrom
Closed
feat: MTP per-request routing in BatchedEngine#223Thump604 wants to merge 2 commits intowaybarrios:mainfrom
Thump604 wants to merge 2 commits intowaybarrios:mainfrom
Conversation
Port SimpleEngine's MLLM+MTP per-request routing to BatchedEngine. Text-only requests route to mlx_lm TextModel with MTP speculative decoding; media requests route to MLLM path. Uses text_model_from_vlm.py (already upstream from PR waybarrios#180) to build a zero-copy TextModel from VLM backbone weights. Routing decision is per-request based on message content via _has_media_content(). Changes: - Add mtp/prefill_step_size params to BatchedEngine.__init__ - Build TextModel in _start_mllm() when mtp=True - Route text-only to _stream_chat_text_model in chat()/stream_chat() - Add _chat_text_model/_stream_chat_text_model for mlx_lm generation - Add _has_media_content helper (mirrors SimpleEngine) - Add test_batched_mtp_routing.py (8 tests)
Removed manual make_prompt_cache + make_mtp_cache concatenation that caused AttributeError (keys=None) during generate_step. mlx_lm's stream_generate is MTP-aware and creates the correct cache internally.
Open
5 tasks
Collaborator
Author
|
Production evidence from M2 Ultra 128GB, Qwen3.5-122B-A10B-VLM-MTP-5bit, BatchedEngine: This PR enables MTP speculative decoding in BatchedEngine (continuous batching mode). Without it, MTP only works in SimpleEngine. The routing logic mirrors SimpleEngine: text-only requests go to TextModel with MTP, media requests go to MLLM path without MTP. Zero-copy weight sharing between paths. Tested with continuous_batching=true, mtp=true, mllm=true in production. MTP tokens accepted at ~60% rate, giving ~1.4x decode speedup on text-only requests while media requests work correctly through the MLLM path. |
7 tasks
Collaborator
Author
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
Port SimpleEngine's MLLM+MTP per-request routing to BatchedEngine for continuous batching.
Routing decision is per-request based on
_has_media_content(). Usestext_model_from_vlm.py(upstream from PR #180) to build a zero-copy TextModel from VLM backbone weights (~0 extra RAM).Changes (+226 lines in
batched.py, +107 lines tests)_has_media_content()helper +_MEDIA_TYPESconstant (mirrors SimpleEngine)mtpandprefill_step_sizeparams inBatchedEngine.__init___start_mllm()with Qwen3.5 eos_token fixchat()andstream_chat()_chat_text_model()/_stream_chat_text_model()for mlx_lm generation under lockstop()Design decisions
_text_generation_lockserializes Metal operations for text model path (same pattern as SimpleEngine's_generation_lock)build_text_modelreturns None and all requests go to MLLMTest plan
_has_media_content(text, image, video, audio, multi-turn, mixed)