Skip to content

fix(FalconH1): give the attention layers a causal mask - #212

Merged
Gajesh2007 merged 2 commits into
mainfrom
fix/falconh1-causal-mask
Oct 2, 2026
Merged

Gajesh2007 merged 2 commits into
mainfrom
fix/falconh1-causal-mask

Conversation

@davidtai

@davidtai davidtai commented Sep 30, 2026 •

Copy link
Copy Markdown

Proposed changes

Fixes #199

Defect. Libraries/MLXLLM/Models/FalconH1.swift:611-620 (on main at 0871943): the private createAttentionMask(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, but FalconH1Attention passes the nil mask to MLXFast.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-lm at 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 main does not have the defect.

Fix. Remove the private function. The existing call now resolves to createAttentionMask(h:cache:) -> MLXArray? of MLXLMCommon (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 with newCache(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 the withKnownIssue block. 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.yaml has a new section "FalconH1 causal attention mask" for the two files.

Merge note. After this PR and #185 are both on main, remove the FalconH1 knownIssues entries (.causality and .cache) from the ModelCase "FalconH1" in Tests/MLXLMTests/Kernel/LLM/TextModelForwardPassTests.swift of #185. The pull request that merges second (this one or #185) must remove the named knownIssues entries in the same change. A known issue that is not intermittent and no longer fails is reported as a failure.

Checklist

  • I have read the CONTRIBUTING document
  • I have run pre-commit run --all-files to format my code / installed pre-commit prior to committing changes (ran swift format on the changed Swift files)
  • I have added tests that prove my fix is effective or that my feature works
  • I have updated the necessary documentation (if needed) (fork.yaml)

🤖 Generated with Claude Code

davidtai and others added 2 commits September 30, 2026 18:33
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>
@Gajesh2007
Gajesh2007 merged commit 15f0577 into main Oct 2, 2026
4 of 5 checks passed
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.

FalconH1: createAttentionMask always returns nil, so a prompt is not causal

2 participants