Skip to content

fix(granitemoehybrid): use a causal attention mask without a cache - #222

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

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

Conversation

@davidtai

@davidtai davidtai commented Oct 1, 2026 •

Copy link
Copy Markdown

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) sets cache = [None] * len(self.layers) and calls create_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 .causal for a prompt and .none for one token, as in the reference.

Test. Tests/MLXLMTests/GraniteMoeHybridCausalMaskTests.swift, copied from the "GraniteMoeHybrid" ModelCase of PR #185 without its withKnownIssue. 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 are private to 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 knownIssues of the "GraniteMoeHybrid" case in Tests/MLXLMTests/Kernel/LLM/TextModelForwardPassTests.swift@cb1406a8:356-362. The pull request that merges second (this one or #185) must remove the named knownIssues in 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 a fork.yaml conflict with #252. After the first of these PRs merges, each other one needs a merge of main and a new Fork Tests run before it merges.

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 (I ran swift format on the changed Swift files only.)
  • 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

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>
@Gajesh2007
Gajesh2007 merged commit c1fd7d5 into main Oct 2, 2026
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.

GraniteMoeHybrid: attention mask is .none without a cache, so the full pass is not causal

2 participants