Repository navigation
Qwen3 embedder: honor attentionMask instead of silently ignoring it - #418
Merged
davidkoski merged 1 commit intoJul 13, 2026
Merged
Conversation
Qwen3Model.callAsFunction accepts an attentionMask parameter and discarded it: the backbone ran with a plain causal mask, so padding tokens participated as attention keys for any position after them. With left padding (the Hugging Face default for Qwen3-Embedding) every real token attends to the pads that precede it, corrupting the hidden states that last-token pooling reads. Fold the [B, L] key-padding mask into the causal mask (causal AND key-not-padding, broadcast to [B, 1, L, L]) whenever a caller provides one. Right-padded causal batches are unaffected by construction — pads there sit after the real tokens, which causality already hides. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
spokvulcan
added a commit
to spokvulcan/mlx-swift-lm
that referenced
this pull request
Jul 12, 2026
The embedding model accepted an attentionMask parameter and discarded it — the backbone ran causal-only, so padding tokens participated as attention keys for any position after them. Fold the [B, L] key-padding mask into the causal mask when a caller provides one. Right-padded causal batches are unchanged by construction. Upstreamed as ml-explore#418; part of the Tesseract EOS attractor fix (spokvulcan/tesseract#332). Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
spokvulcan
added a commit
to spokvulcan/tesseract
that referenced
this pull request
Jul 12, 2026
… attractor (#332) Three faults compounding: the embedder padded every batch to >=16 tokens with EOS, the vendored Qwen3Model ignored the attention mask it was handed, and pooling ran maskless under the model's last-token strategy — so every text under the floor pooled a pad position, and all short memories collapsed onto one content-free direction (pairwise cosine 0.959 for the owner's sub-40-char memories). A short recall query landed on the same attractor and returned confidently-ranked noise. The embed path is reference-faithful now: each sequence pools its own trailing EOS under a length-based mask (a token-value mask would erase that very EOS), the pad floor is gone, and queries carry the model card's instruct prefix while documents stay bare — measured worth 1-2pp MRR. Vectors are artifacts of a scheme, and the store now knows which one it holds: an embedding_scheme stamp (schema v2 meta table) is reconciled at prewarm — a mismatch wipes every vector plus the cue affinities learned from ranking them, re-embeds everything, and stamps only on completion so an interrupted pass re-runs. This is also the repair path for vectors that never got written. recall now searches what its description always claimed: beliefs plus the raw episodic record, so a fact told this morning — an episode sleep has not yet distilled — is findable, with episode hits dated and quoted. The vendored Qwen3Model honors attentionMask (key-padding folded into the causal mask; upstreamed as ml-explore/mlx-swift-lm#418). For this app's right-padded batches it changes nothing — the pooling mask was the load-bearing repair. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
davidkoski
approved these changes
Jul 13, 2026
davidkoski
left a comment
Member
There was a problem hiding this comment.
Looks good to me. Thank you.
dkgv
pushed a commit
to dkgv/mlx-swift-lm
that referenced
this pull request
Jul 30, 2026
…l-explore#418) Qwen3Model.callAsFunction accepts an attentionMask parameter and discarded it: the backbone ran with a plain causal mask, so padding tokens participated as attention keys for any position after them. With left padding (the Hugging Face default for Qwen3-Embedding) every real token attends to the pads that precede it, corrupting the hidden states that last-token pooling reads. Fold the [B, L] key-padding mask into the causal mask (causal AND key-not-padding, broadcast to [B, 1, L, L]) whenever a caller provides one. Right-padded causal batches are unaffected by construction — pads there sit after the real tokens, which causality already hides.
davidtai
referenced
this pull request
in Layr-Labs/mlx-swift-lm
Oct 23, 2026
… eps - mlx-swift 9bd73fc7 (#418, in the pinned mlx-swift) runs a recursive `update(parameters:)` through an internal method, so a nested module's override runs only when the update starts at that module. An update that started at a decoder layer or a model no longer invalidated the fused GDN input projection, and compiled segments kept the stale projection. New `ParameterUpdateObserving` (MLXLMCommon): the modules that can start an update forward the updated keys to the GDN layers below them, in MLXVLM and MLXLLM Qwen 3.5. Fixes the three Qwen35FusedGDNProjectionTests VLM cases. - Qwen35DirectExpertReductionTests: the reference is the fork block's own established path (its router finalizer, flattened rows, weightedExpertSum); the bitwise check is unchanged. - Keep the fork's Qwen 3.5 GDN q/k-norm eps (rmsNorm eps 1e-6, as mlx-lm) over upstream c408452 (ml-explore#633); adapt GatedDeltaQKNormTests.testQwen35 to it (David, 2026-10-23: fork numerics win). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
3 of 5 tasks
div0-space
pushed a commit
to LibraxisAI/mlx-swift-lm
that referenced
this pull request
Nov 29, 2026
O5 on the phase-D cumulative SDK: ml-explore#407 merged onto ml-explore#416 + ml-explore#417 (opt-in Gemma reopened thoughts)
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.
What
Qwen3Model.callAsFunction(MLXEmbedders) accepts anattentionMaskparameter and silently discards it — the backbone always runs with a plain causal mask, so padding tokens participate as attention keys for any position that comes after them.Why it matters
Qwen3-Embedding is a last-token-pooling model, and its Hugging Face reference tokenizes with
padding_side='left'. Under left padding, every real token attends to the pads that precede it, corrupting the hidden state at the exact position pooling reads. Callers who pass a mask today reasonably believe they are protected; the parameter is an API promise the implementation doesn't keep.We found this in production: combined with maskless pooling on our side, short texts embedded to a shared content-free direction ("I love cats." vs "He is from Europe." at cosine 0.96), and semantic recall over a personal memory store returned confidently-ranked noise.
Fix
When a caller provides a
[B, L]mask, fold it into the causal mask —causal ∧ key-not-padding, broadcast to[B, 1, L, L]— via the existingcreateCausalMaskhelper. No mask provided → behavior unchanged.Right-padded causal batches are unaffected by construction (pads sit after the real tokens, which causality already hides) — verified: our embedding regression suite produces identical vectors before and after this change for right-padded batches.
swift build --target MLXEmbedderspasses.