Repository navigation
fix(granitemoehybrid): use a causal attention mask without a cache - #222
Merged
Merged
Conversation
Without a cache, the attention mask of Granite MoE hybrid was .none, so the full pass was not causal. The model now builds the mask with or without a cache, as mlx-lm granitemoehybrid.py does. Fixes #198 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
3 of 4 tasks
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.
Proposed changes
Fixes #198
Defect.
Libraries/MLXLLM/Models/GraniteMoeHybrid.swift:464-470(main 0871943). The attention mask is built only when a cache is given. Without a cache the closure returns.none(line 468). The full pass is not causal, and it differs from the pass with a cache.Reference.
mlx_lm/models/granitemoehybrid.py:455-464(ml-explore/mlx-lm@a9bd8af) setscache = [None] * len(self.layers)and callscreate_attention_mask(hidden_states, cache[self.fa_idx])with or without a cache.Fix. The model passes the cache of the first attention layer, or nil, to
createAttentionMask(h:cache:). Without a cache that gives.causalfor a prompt and.nonefor one token, as in the reference.Test.
Tests/MLXLMTests/GraniteMoeHybridCausalMaskTests.swift, copied from the "GraniteMoeHybrid"ModelCaseof PR #185 without itswithKnownIssue. Tiny model with seeded random weights: a Mamba layer and an attention layer, 4 experts, 11 tokens.aLaterTokenDoesNotChangeEarlierLogits: a changed token at position 6 must not change the logits of positions 0-5. This proves the full pass is causal.cachedDecodeMatchesTheFullForwardPass: the pass in chunks 5, 3, 1, 1, 1 with a cache must match the full pass within 1e-4, for batch 1 and batch 2.The test file has private copies of a few helpers from the kernel test support of PR #183 (
Tests/MLXLMTests/Kernel/Support/SyntheticModel.swift): configuration decoding, seeded random weights, token rows, the logits call, and the causality and cache checks. They areprivateto the file, so they do not collide with #183 or with the other fix PRs.Merge note. After this PR and #185 both merge, remove the
knownIssuesof the "GraniteMoeHybrid" case inTests/MLXLMTests/Kernel/LLM/TextModelForwardPassTests.swift@cb1406a8:356-362. The pull request that merges second (this one or #185) must remove the namedknownIssuesin the same change. A known issue that is not intermittent and no longer fails is reported as a failure.Issues #197 and #198 have the same defect and the same fix in two files. They are separate PRs so that each one closes one issue.
Merge order (
fork.yaml). This PR has afork.yamlconflict with #252. After the first of these PRs merges, each other one needs a merge ofmainand a new Fork Tests run before it merges.Checklist
pre-commit run --all-filesto format my code / installed pre-commit prior to committing changes (I ranswift formaton the changed Swift files only.)fork.yaml)🤖 Generated with Claude Code