Repository navigation
Sync upstream ml-explore/mlx-swift-lm main (48114ee) - #84
Merged
Merged
Conversation
* Fix exact prefix rewinds in rotating KV caches * Fix Gemma3n cross-layer KV sharing
* Drop the attention mask from text-only VLM prompts FastVLM, Gemma 3, Gemma 4 (both processors), Mistral 3 and SmolVLM paired every prompt with an all-ones attention mask, including prompts with no media. `ChatSession` reads any mask as a reason not to resume a cache (`carriesAttentionMask`), so on these models every text turn and every tool follow-up prefilled the whole conversation again. A text-only prompt is one unpadded sequence, so the mask carries nothing, and none of these models reads it without media. Their text-only prompts now carry no mask, as Qwen2-VL, Qwen3-VL, GLM-OCR, LFM2-VL and Muse-Glimmer already do. Prompts with media are unchanged. Measured on gemma-4-12B-it-4bit (Gemma4UnifiedProcessor), one tool call and two follow-up turns: the passes after the first reused 175, 226 and 267 cached positions instead of 0, and their prompt time fell from 3.1-3.9 s to about 1.0 s.
* Omit empty text from message content parts
`contentParts(for:layout:)` always appended a text part, so an assistant
tool call without text became `[{"type": "text", "text": ""}]`. Chat
templates test `message.content` for truth: the empty string a text
model receives is false, but a list holding an empty part is true.
Qwen3-VL's template then renders the call as `\n\n<tool_call>` instead
of the `\n<tool_call>` the model generated. The cache no longer matches
the prompt, and a rewind is refused because the model carries M-RoPE
state, so every tool follow-up pass prefilled the whole conversation.
Empty text now gives no part. An empty list is false, as the empty
string is, so the re-rendered prompt extends the cache again.
Checked against the chat templates of every registered model that uses
content parts (Qwen2-VL, Qwen2.5-VL, Qwen3-VL, Qwen3.5, Qwen3.8,
Muse-Glimmer, Gemma 3, Gemma 4, LFM2-VL, Ministral 3, FastVLM, GLM-OCR):
only the Qwen3-VL template changes output, and it now renders the same
as with string content.
* Limit empty text omission to assistant tool calls
* Support for scripted models (tests) - add TranscriptKVCache (tests) - add ability to extend KVCache hierarchy
Reject projections with global scales during fusion preparation. The current fused projection and checkpoint view constructors do not retain those scalars, so keep the original modules and parameters.
transformers 5 writes the LFM2 MLP width as intermediate_size, keeps rope_theta under rope_parameters, and can untie the output head with a top-level tie_word_embeddings or tie_embedding. LFM2VL now reads all three. An untied model gets an lm_head, and sanitize maps a top-level lm_head into the language model. With this, LiquidAI/d1-3B loads and chats as an LFM2-VL model.
Brings in ml-explore#622 (exact prefix rewinds in rotating KV caches), ml-explore#619 (streaming detokenizers), ml-explore#698, ml-explore#699, ml-explore#700, ml-explore#701, ml-explore#705, ml-explore#697 and ml-explore#695. Conflicts: AttentionUtils.swift keeps TurboKV telemetry next to upstream's SharedAttentionKVCache; KVCache.swift takes upstream's serialization and rotating-cache view/trim while keeping the fork's TurboQuant state, public cache fields and temporallyOrdered. Follow-ups for upstream behavior the fork had changed: the Qwen2VL audio placeholder no longer indexes content.count - 1 when a tool-call message has no text part, and Gemma4MessageGenerator uses content parts for every non-system role. Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
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.
Real merge of upstream main (10 commits since c53d7d4, including ml-explore#622 rotating KV cache prefix rewinds and ml-explore#619 streaming detokenizers).
Conflicts were in AttentionUtils.swift and KVCache.swift. The fork's TurboKV telemetry and TurboQuant state are kept next to upstream's SharedAttentionKVCache, serialization and rotating-cache view/trim.
Two fork behaviors broke against upstream's new tests and are fixed here: the Qwen2VL audio placeholder crashed (index -1) for a tool-call message with no text part, and Gemma4MessageGenerator returned a plain string instead of content parts for non-system roles.
Local: 1268 tests, 0 failures.
🤖 Generated with Claude Code