Repository navigation
fix(FalconH1): give the attention layers a causal mask - #212
Merged
Merged
Conversation
The private createAttentionMask(h:cache:) in FalconH1.swift returned nil for every input, so a prompt ran attention without a causal mask. A later token changed the logits of earlier positions, and the pass with a cache differed from the pass without a cache. Remove the private function. The call now uses the causal mask of MLXLMCommon, as mlx-lm falcon_h1.py does with create_attention_mask. Add FalconH1CausalMaskTests: a causality test and a cached-decode test on a tiny model with random weights. Fixes #199 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
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 #199
Defect.
Libraries/MLXLLM/Models/FalconH1.swift:611-620(onmainat 0871943): the privatecreateAttentionMask(h:cache:)returns nil for one token (line 617) and nil for more tokens (line 619). The comment says the attention makes the causal mask when the mask is nil, butFalconH1Attentionpasses the nil mask toMLXFast.scaledDotProductAttention, which then applies no mask. A prompt runs attention without a causal mask: a later token changes the logits of earlier positions, and the pass with a cache differs from the pass without a cache.Reference. mlx-lm
mlx_lm/models/falcon_h1.py:426(ml-explore/mlx-lmat a9bd8af):attn_mask = create_attention_mask(h, cache[0][1]), a causal mask.Origin. The function is the same as at the fork base 7e2b710. Upstream ml-explore/mlx-swift-lm replaced it later (ml-explore#331), so upstream
maindoes not have the defect.Fix. Remove the private function. The existing call now resolves to
createAttentionMask(h:cache:) -> MLXArray?ofMLXLMCommon(KVCache.swift:243). That function gives a causal mask with the cache offset for more than one token, and nil for one token. The call site does not change.Test.
Tests/MLXLMTests/FalconH1CausalMaskTests.swift, a tiny FalconH1 model with seeded random weights (hidden size 32, 2 layers, no real weights):aLaterTokenDoesNotChangeEarlierLogits: a change to the token at position 6 does not change the logits of positions 0 to 5 (tolerance 1e-4), and does change the logits at position 6.cachedDecodeMatchesTheFullForwardPass: a prompt in chunks 5, 3 and three decode steps withnewCache(parameters:)gives the logits of the full pass, for batch 1 and batch 2.The two tests are copies of the FalconH1 cases of the recording tests in #185 (
TextModelForwardPassTests), without thewithKnownIssueblock. The helpers they need live only in #183 (Tests/MLXLMTests/Kernel/Support/SyntheticModel.swift), so the file has small private copies of them. They are private to the suite, so they do not collide with the #183 types.fork.yamlhas a new section "FalconH1 causal attention mask" for the two files.Merge note. After this PR and #185 are both on
main, remove the FalconH1knownIssuesentries (.causalityand.cache) from theModelCase"FalconH1" inTests/MLXLMTests/Kernel/LLM/TextModelForwardPassTests.swiftof #185. The pull request that merges second (this one or #185) must remove the namedknownIssuesentries in the same change. A known issue that is not intermittent and no longer fails is reported as a failure.Checklist
pre-commit run --all-filesto format my code / installed pre-commit prior to committing changes (ranswift formaton the changed Swift files)fork.yaml)🤖 Generated with Claude Code