Skip to content

fix(nemotronh): use a causal attention mask without a cache - #262

Merged
Gajesh2007 merged 3 commits into
mainfrom
fix/nemotronh-causal-mask
Oct 3, 2026
Merged

Gajesh2007 merged 3 commits into
mainfrom
fix/nemotronh-causal-mask

Conversation

@davidtai

@davidtai davidtai commented Oct 1, 2026 •

Copy link
Copy Markdown

Implementation

Related issue: #261. Related recording PR: #240 (already integrated on main).

NemotronHBackbone.callAsFunction passes the first attention layer's cache, or nil, to createAttentionMask(h:cache:). A no-cache multi-token prompt now receives a causal mask; a single token receives no mask. Models without attention still receive no attention mask. Existing CBv2/MTP entry points (cbv2Hidden / cbv2Forward) retain their separate control flow and unchanged semantics.

Merged current main 3799b1150699e5595ae4c100a36b1a07cef3c44f into the author branch. Resolved fork.yaml conflict by retaining both the Nemotron causal-mask topic and current main's DeepSeek pooled-cache / LFM2-MoE topics. Removed the resolved wrapper and matching predicate from Kernel/LLM/NemotronHRuntimeTests, preserving the causality expectation and all controls. Renamed the recording to attentionIsCausalWithAndWithoutACache and corrected its documentation/references. Author runtime, including its existing formatting, is otherwise unchanged.

Before

flowchart LR
  A["Serial prompt: callAsFunction"] --> B{"Cache supplied?"}
  B -->|No| C["attentionMask = none"]
  C --> D["Future tokens affect prefix logits"]
  B -->|Yes| E["createAttentionMask with cache"]
  classDef bad fill:#7f1d1d,color:#fff,stroke:#ef4444
  class D bad
Loading

After

flowchart LR
  A["Serial prompt: callAsFunction"] --> B["Select attention cache or nil"]
  B --> C["createAttentionMask(h:cache:)"]
  C --> D["Multi-token attention is causal"]
  D --> E["Later edits preserve prefix logits"]
  E --> F["Plain causal recording assertion"]
  G["CBv2/MTP requests"] --> H["Separate cbv2Hidden / cbv2Forward unchanged"]
  classDef good fill:#14532d,color:#fff,stroke:#22c55e
  class D,E,F good
Loading

Fresh Validation (2026-10-03)

Owned clone held-pr-prep-20261003/gen-cache-20261003-gaj, full test products built locally. mlx-swift/core/mlx-c tuple 0f4fe403 / 3fa8f25e / 02cf6f4d verified against the immutable reference Metal artifact before staging it only in this clone. GPU executions use the brief's Perl flock wrapper and held-pr-review-20261002/gpu.lock.

swift build --build-tests -j 4
swift test --skip-build --filter 'attentionIsCausalWithAndWithoutACache'
swift test --skip-build --filter 'NemotronHCausalMaskTests|NemotronHRuntimeTests|NemotronH35BackendParityTests/testTiny'
swift format lint Libraries/MLXLLM/Models/NemotronH.swift Tests/MLXLMTests/NemotronHCausalMaskTests.swift Tests/MLXLMTests/Kernel/LLM/NemotronHRuntimeTests.swift
git diff --check
  • Differential baseline: temporarily restored only the old mask closure, rebuilt, and ran the two causality cases (dedicated and cleaned recording). Both fail with prefix error 1.2534938 versus tolerance 0.0001. Restored exact author runtime and rebuilt before candidate validation.
  • Candidate: 16 Swift Testing tests in four suites pass, including complete NemotronHRuntimeTests and its sibling MTP-row suite, plus the dedicated regression. No stale causal known issue remains.
  • Existing CBv2 qualification controls: all four selected XCTest cases pass: float32/bfloat16 canonical backend logits/state repeat parity and float32/bfloat16 earlier/latest checkpoint suffix parity against contiguous storage. No new CBv2 implementation was introduced.
  • Evidence: lm262-baseline.log, lm262-candidate.log, and respective build logs in the owned clone. Lint emits only existing uppercase-local naming warnings.

Limits And Risk

This corrects generic no-cache Nemotron full-forward behavior. Canonical Nemotron3.5 Lightning serving goes through separate CBv2 paths, exercised here with existing tiny parity tests. An undersized cache list is still invalid input and may trap later in the existing layer loop; this PR does not add short-list support. Full hosted checkpoint/provider qualification, a full test-suite run, and the historical review's 90-cell matrix are not claimed as fresh execution. No dependency revisions, pins, deployment, or admission safeguards changed. Base remains main; no retarget dependency. Latest-head CI/review gates must be assessed separately.

Combined Integration Validation

On 2026-10-03, the current prepared heads of the twelve held PRs were composed
on a local-only validation branch based on main 3799b115, including the
signed-padding compatibility correction 96d5f976. No PR was merged.

  • Fresh swift build --build-tests -j 4 completed successfully.
  • Affected suites: 261 Swift Testing cases and 33 executed XCTest cases passed;
    five optional fixture-dependent XCTest cases skipped. Advanced Arrays/Mamba
    and nested signed-padding round trips passed in this composed build.
  • Bounded serving integration: 107 Swift Testing cases and 105 XCTest cases
    passed, with no failures or skips, covering paged backend, scheduler,
    checkpoint engine, SSM bounds, Nemotron storage/priming, OpenAI service and
    tool parser paths.
  • Both executions used swift test --skip-build --filter ... --no-parallel
    under the shared GPU mutex. This is focused integration validation, not a
    full-package, real-weight or provider qualification. Exact compatible MLX
    dependency tuple and immutable Metal artifact were verified before staging.
  • Combined git diff --check and python3 scripts/check_forkdiff.py passed.
    Existing assertions remain intact; this evidence does not waive required
    latest-head CI, review, or parent-first retarget/revalidation gates.

davidtai and others added 3 commits October 1, 2026 15:40
Without a cache, the attention mask of NemotronHModel was .none, so the
full forward pass was not causal. The model now builds the mask with or
without a cache, as mlx-lm nemotron_h.py does.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@Gajesh2007
Gajesh2007 merged commit 457cc91 into main Oct 3, 2026
6 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.

2 participants