Make qwen3 last_token_pool compile-safe (fixes eval-during-compile failure) - #70
Open
alantmiller wants to merge 1 commit into
Open
Make qwen3 last_token_pool compile-safe (fixes eval-during-compile failure)#70alantmiller wants to merge 1 commit into
alantmiller wants to merge 1 commit into
Conversation
…nch) A Python `if` on the lazy left-padding comparison forces an eval, which raises "[eval] Attempting to eval an array during function transformations like compile or vmap is not allowed" the first time the function runs under mx.compile with a traced attention_mask. Servers that wrap the model in mx.compile (e.g. oMLX) silently fall back to the eager path. Replace the branch with pure array ops: the last valid token index is max(position where mask != 0), which handles left padding, right padding, and no padding uniformly, and also fixes a crash on float attention masks (the old sum-1 path produced float gather indices). Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Jsssr7vdMR7E8CpBpwb1uT
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.
Problem
last_token_poolinmlx_embeddings/models/qwen3.pydecides between left- and right-padding handling with a Python branch on a lazy comparison:The
ifforces an eval of a traced array, so the first real call undermx.compileraises:Any server that wraps the model in
mx.compile(oMLX does, with an eager fallback) silently loses the compiled path for Qwen3 embedding models. Found in production while running a large Qwen3-Embedding-8B backfill behind oMLX — its log showedcompiled embedding path failed ... falling back to eager generate()on every load.Fix
Compute each sequence's last valid token index with pure array ops —
max(position where mask != 0)— which handles left padding, right padding, and no padding uniformly, with no data-dependent Python branch. Behavior is identical to the old code for integer masks in all three regimes (verified numerically). It also fixes a latent crash: with a float attention mask the old right-padding path produced float gather indices (ValueError: [gather] Got indices with invalid dtype).Tests
Added
test_qwen3_last_token_pool(correctness across padding regimes) andtest_qwen3_last_token_pool_compile_safe(regression: the function must survivemx.compilewith a traced mask). The compile-safety test fails with an error against the previous implementation and passes with this change; verified on Apple Silicon, MLX from a current oMLX bundle.🤖 Generated with Claude Code
https://claude.ai/code/session_01Jsssr7vdMR7E8CpBpwb1uT