Skip to content

Sync upstream ml-explore/mlx-swift-lm main (48114ee) - #84

Merged
solderzzc merged 8 commits into
mainfrom
sync/upstream-48114ee-merge
Oct 17, 2026
Merged

solderzzc merged 8 commits into
mainfrom
sync/upstream-48114ee-merge

Conversation

@solderzzc

Copy link
Copy Markdown
Member

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

aleroot and others added 8 commits October 14, 2026 15:08
* 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]>
@solderzzc
solderzzc merged commit fac6b34 into main Oct 17, 2026
9 of 10 checks passed
@solderzzc
solderzzc deleted the sync/upstream-48114ee-merge branch November 4, 2026 05:33
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.

5 participants