Skip to content

Contract the mamba2 chunk scan with einsum instead of broadcast-then-sum - #48978

Merged
tarekziade merged 7 commits into
mainfrom
tarek/mamba2-chunk-scan-einsum
Sep 21, 2026
Merged

tarekziade merged 7 commits into
mainfrom
tarek/mamba2-chunk-scan-einsum

Conversation

@tarekziade

@tarekziade tarekziade commented Sep 21, 2026

Copy link
Copy Markdown
Collaborator

CPU CI GPU run-slow

What does this PR do?

The fallback mamba2_chunk_scan built each contraction as a broadcast product followed by a sum, materialising the un-summed tensor first. G's is (batch, chunks, chunk, chunk, heads, state) in float32 : 4 GiB per sequence at bamba-9B's shapes, so BambaModelIntegrationTest OOMs asking for 8 GiB on a 22.3 GiB runner for a 10-token generate, with 18.2 GiB of that card holding the model setUpClass loaded. Largest intermediate drops 4GiB -> 0.03 GiB.

@tarekziade

Copy link
Copy Markdown
Collaborator Author

run-slow: bamba, falcon_h1, granitemoehybrid, mamba2, nemotron_h, zamba2

The fallback `mamba2_chunk_scan` built each contraction as a broadcast product
followed by a sum, materialising the un-summed tensor first. G's is
(batch, chunks, chunk, chunk, heads, state) in float32 — 4 GiB per sequence at
bamba-9B's shapes, so BambaModelIntegrationTest OOMs asking for 8 GiB on a
22.3 GiB runner for a 10-token generate, with 18.2 GiB of that card holding the
model setUpClass loaded. Largest intermediate drops 4.000 GiB -> 0.031 GiB.
@HuggingFaceDocBuilderDev

Copy link
Copy Markdown

The docs for this PR live here. All of your documentation changes will be reflected on that endpoint. The docs are available until 30 days after the last update.

@tarekziade
tarekziade force-pushed the tarek/mamba2-chunk-scan-einsum branch from a4d08a1 to b078515 Compare September 21, 2026 08:31
@tarekziade

Copy link
Copy Markdown
Collaborator Author

run-slow: bamba, falcon_h1, granitemoehybrid, mamba2, nemotron_h, zamba2

@tarekziade

Copy link
Copy Markdown
Collaborator Author

run-slow: bamba, falcon_h1, granitemoehybrid, mamba2, nemotron_h, zamba2

@tarekziade

Copy link
Copy Markdown
Collaborator Author

@vasqu should EXPECTED_TEXT_A10 for test_falcon_h1_hard be re-recorded here? I have not touched it, since that overwrites an existing device key rather than adding one, and note this is the mamba_ssm-absent fallback, which already does not match the fused kernel byte for byte

@tarekziade
tarekziade requested a review from vasqu September 21, 2026 11:23
@tarekziade

Copy link
Copy Markdown
Collaborator Author

The bamba fairures are post-OOM, not this patch.

@molbap molbap left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

cc @Rocketknight1 who I think made also a long investigation on the matter of einsum vs matmuls in many cases (here it might remove some intermediate instances that consume too much memory but we have to be sure it's worth losing the readbility)

@vasqu vasqu left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just small comments but overall just 1 thing to keep in mind -> explain the shapes a bit more so non einsum enthusiasts can also understand

(unsure about that one contiguous call which might be unnecessary?)

Comment thread src/transformers/models/bamba/modeling_bamba.py
output = Y_diag + Y_off
# Add output of intra-chunk and inter-chunk terms (diagonal and off-diagonal blocks).
# `contiguous` because einsum may return a permuted view and callers `.view()` this.
output = (Y_diag + Y_off).contiguous()

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

dont understand why we use the contigous here because reshape below may handle non contigous as well no?

@tarekziade tarekziade Sep 21, 2026

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're right about the reshape, but he breakage is one level up.
FalconH1Mixer calls .view(batch_size, seq_len, -1) on this function's return value, and without the contiguous()

that breaks 12 falcons tests. (that call is cheap btw)

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lets shorten it to the original comment tho, the contigous is usually self-explanatory

Comment thread src/transformers/models/bamba/modeling_bamba.py Outdated
@tarekziade

Copy link
Copy Markdown
Collaborator Author

run-slow: bamba, falcon_h1

The einsum contractions reassociate a 128-term reduction, which is bit-exact
in float64 but shifts the last bits in float32, and greedy decoding turns that
into a different continuation from token ~100 on. Text captured from the A10G
runners on this PR; single-gpu and multi-gpu produced it byte-identically.
@github-actions

Copy link
Copy Markdown
Contributor

[For maintainers] Suggested jobs to run (before merge)

run-slow: bamba, falcon_h1, granitemoehybrid, mamba2, nemotron_h, zamba2

@tarekziade

Copy link
Copy Markdown
Collaborator Author

run-slow: bamba, falcon_h1, granitemoehybrid, mamba2, nemotron_h, zamba2

@github-actions

Copy link
Copy Markdown
Contributor

Nvidia CI

Workflow Run ⚙️

This comment contains run-slow, running the specified jobs on Nvidia:

models: ["models/bamba", "models/falcon_h1", "models/granitemoehybrid", "models/mamba2", "models/nemotron_h", "models/zamba2"]
quantizations: []

@github-actions

Copy link
Copy Markdown
Contributor

AMD CI

Workflow Run ⚙️

This comment contains run-slow, running the specified jobs on AMD:

models: ["models/bamba", "models/falcon_h1", "models/granitemoehybrid", "models/mamba2", "models/nemotron_h", "models/zamba2"]

@github-actions

Copy link
Copy Markdown
Contributor

CI recap

Dashboard: View test results in Grafana
Latest run: 35613317497:1
Result: success | Jobs: 3 | Tests: 227 | Failures: 0 | Duration: 8m 28s

@github-actions

Copy link
Copy Markdown
Contributor

CI Results (AMD)

Workflow Run ⚙️

Commit Info

Context Commit Description
RUN 1f61a830 workflow commit (merge commit)
PR ee032b90 branch commit (from PR)
main d53e5876 base commit (on main)

Model CI Report

2 new failed tests from this PR 😭

  • bamba:
    tests/models/bamba/test_modeling_bamba.py::BambaModelIntegrationTest::test_simple_batched_generate_with_padding (❌ ⟹ ❌)

  • falcon_h1:
    tests/models/falcon_h1/test_modeling_falcon_h1.py::FalconH1ModelIntegrationTest::test_falcon_h1_hard (❌ ⟹ ❌)

@github-actions

Copy link
Copy Markdown
Contributor

CI Results (Nvidia)

Workflow Run ⚙️

Commit Info

Context Commit Description
RUN 1f61a830 workflow commit (merge commit)
PR ee032b90 branch commit (from PR)
main d53e5876 base commit (on main)

Model CI Report

2 new failed tests from this PR 😭

  • bamba:
    tests/models/bamba/test_modeling_bamba.py::BambaModelIntegrationTest::test_simple_batched_generate_with_padding (❌ ⟹ ❌)
    tests/models/bamba/test_modeling_bamba.py::BambaModelIntegrationTest::test_simple_generate (❌ ⟹ ❌)

@tarekziade
tarekziade added this pull request to the merge queue Sep 21, 2026
Merged via the queue into main with commit a52f659 Sep 21, 2026
49 of 51 checks passed
@tarekziade
tarekziade deleted the tarek/mamba2-chunk-scan-einsum branch September 21, 2026 15:18
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.

4 participants