Skip to content

Studio: Mamba-2 hybrids use the extension-free mamba-ssm wheel when unsloth-zoo runs their conv on Triton - #14160

Merged
danielhanchen merged 42 commits into
mainfrom
mamba2-pure-wheel
Oct 20, 2026
Merged

danielhanchen merged 42 commits into
mainfrom
mamba2-pure-wheel

Conversation

@danielhanchen

Copy link
Copy Markdown
Member

Stacked on #13971. Pairs with unslothai/unsloth-zoo#1865 and the extension-free mamba-ssm wheel added in #14142.

Mamba-2 hybrids (Nemotron-H, Falcon-H1, Granite-4.0-H, Bamba, Zamba2, Mamba2) install a 224 KB mamba-ssm wheel without its CUDA extension instead of the 230 to 594 MB mamba-ssm + causal-conv1d CUDA wheels, when unsloth-zoo can run their causal conv on Triton.

Why

On transformers 5.x these models only use mamba-ssm's Triton ops, never selective_scan_cuda. With unsloth-zoo#1865 the causal conv runs on Triton too. On a Colab T4 (torch 2.10, Falcon-H1-0.5B fp16 LoRA, three interleaved rounds) that route matches the CUDA wheels: train step +1.4% (inside the CUDA arm's own spread), decode +1.7% / +1.9%, prefill +0.7%, identical peak memory.

Changes

  • ssm_runtime.py / training worker / export: the pure wheel is used, and causal-conv1d skipped, only when the config resolves to one of those six model types, no remote code is needed, torch is a CUDA build with Triton, transformers is 5.15 or newer, and the installed unsloth-zoo source carries the Triton drop-ins. UNSLOTH_MAMBA_CUDA_KERNELS=1 keeps the CUDA wheels.
  • No source build on that route. If the pure wheel is unusable (definite 404, install failure, import failure), the load falls back to today's CUDA route (causal-conv1d, then CUDA mamba-ssm with the existing resolver and source build policy). The failure memo is keyed per wheel source, so a failed pure attempt never blocks the CUDA wheel, and both routes take the per-env lock one after the other.
  • wheel_utils.mamba_ssm_pure_wheel_url().

Tests

test_ssm_mamba2_triton.py (24 tests; the fallback ones exercise the real _install_kernel with lock, memo and cleanup, only network and pip faked). 509 passed across the clef, export, kernel_install, SSM and training worker suites.

Not covered: ROCm and macOS stay on the existing route by design; Windows triton-windows untested on a GPU; Mamba-1 models and transformers older than 5.15 keep the CUDA wheels.

…rovision SSM kernels in export

- Request causal-conv1d on demand again for Qwen3.5/3.6/Qwen3-Next in training, inference and Clef
- mamba-ssm stays required only for nemotron_h, falcon_h1 and granitemoehybrid; newer SSM families continue on the torch path
- Resolve local adapter bases and the adapter base pin before deciding kernels
- Export worker provisions SSM kernels before importing Unsloth
- Inference re-reads modeling sources without importing transformers and respawns the worker once after a fresh install
- Setup removes causal-conv1d / mamba-ssm builds that no longer import after a torch change
- Skip flash-attn downloads below sm80 at setup and for long-context training
- Stale kernel removal needs proof: torch imports, then the extension import completes with an
  ImportError / OSError; a broken torch, a signal, a hang or an unrelated exit keeps the kernel
- unsloth install-kernels --without-gpu (or UNSLOTH_INSTALL_KERNELS_WITHOUT_GPU=1) provisions
  GPU-gated kernels in a GPU-less image build; skipped kernels are listed, and a named kernel
  that was skipped exits non-zero
- Inference respawn never spawns over a worker that survived shutdown
- SSM restart and Xet fallback each get their own one-use retry, at most three spawns, with an
  accurate stall message
- Restart only for imported modeling modules, including the selected remote-code model's
- Export skips SSM provisioning on macOS, MLX and CPU
- UV_OFFLINE counts as offline for on-demand kernel installs
- Long-context flash-attn: no probe when already importable, reuse the worker's torch, never
  apply the sm80 floor on ROCm
… matching

- install-kernels checks an already-working extension before the wheel, GPU and offline skips
  (a ROCm source build or a GPU-hidden builder's install is success); skipped kernels are listed
  and exit 0 as before, --strict opts into exit 2
- install-kernels installs nothing offline (UV_OFFLINE, HF_HUB_OFFLINE, TRANSFORMERS_OFFLINE), and
  install_wheel never falls back from an offline uv to pip
- Restart detection takes the implementation modules from the config's auto_map (any module
  name) and matches transformers_modules.<checkpoint namespace> exactly, both spellings
- Export decides the CPU / MLX / macOS bypass before the connectivity window
… up, lock

- A wheel whose HEAD check is unanswered is still downloaded and verified (as setup did); only a
  real download / import failure falls back, in inference, export and training
- A failed install is not retried in the same process (keyed by kernel, torch and toolchain), so
  a load's preflight and _handle_load share one attempt; configs are read once per process
- Optional mamba-ssm families (mamba, mamba2, falcon_mamba, jamba, zamba, zamba2, bamba) get a
  prebuilt wheel only, never a source build, and are skipped below setup's sm80 / sm75 + Triton
  3.4 floor; Nemotron-H, Falcon-H1 and Granite-4.0-H keep the source fallback and no floor
- CUDA source builds get the 1800 s deadline ROCm had, and a timeout stops the whole build tree
- A failed runtime install removes the distribution whose extension does not import
- One cross-process lock per kernel and environment covers precheck, install, verify and cleanup,
  rechecks after acquiring, reports while waiting and gives up after a bound
- Only a CUDA or ROCm torch provisions kernels; export provisions only for a CUDA / ROCm backend
…source builds

- The Unsloth prebuilt resolver answers only for (platform, torch, interpreter) cells the release
  carries: torch 2.13 / 2.14 are cp313 on every platform, so linux_x86_64 cp310-cp312 and
  win_arm64 cp311 / cp312 no longer get a URL that 404s; no upstream URL for torch 2.13+ either
- No source build of the pinned causal-conv1d 1.6.1 / mamba-ssm 2.3.1 on torch 2.13+ (C++17
  sources against C++20 torch headers): the prebuilt wheel or the torch path, in inference,
  export and training
- Training stops with the inference error when Nemotron-H, Falcon-H1 or Granite-4.0-H cannot get
  mamba-ssm on a CUDA / ROCm torch, including Windows without triton-windows
- The MLX training-worker test stubs the wheel_utils names the worker now imports
The pinned upstream releases (causal-conv1d v1.6.1.post4, mamba-ssm v2.3.1) publish CUDA 11 / 12
/ 13 Linux wheels for a fixed set of torch minors and cp310-cp313 only; CUDA 13 before torch 2.9,
aarch64 before 2.7 and abiFALSE after 2.6 were URLs that 404 in front of the source fallback.
The cells are now a table read from the two asset lists, and a test ties every URL the resolver
can emit to it.
Nemotron-H, Falcon-H1 and Granite-4.0-H keep the source fallback, but a failed install or a
Windows torch without triton-windows no longer stops the run: their native transformers path can
run without mamba-ssm, so training reports it and continues, as it did before.
Nemotron-H, Falcon-H1 and Granite-4.0-H still try mamba-ssm first (source build included), but a
failed install now refuses the chat or export load only when that load imports code that cannot
run without it: remote modeling code (auto_map, under trust_remote_code or with no native class)
that imports mamba_ssm unconditionally, or a model type the installed transformers has no native
implementation of. Their native transformers models run without it, so otherwise the load goes
ahead on the torch path with a warning, as before #13861 (a Nemotron-H checkpoint on Windows
ROCm was refused).
…nel builds

- Training's eager causal-conv1d install and its fast-path hook share the runtime's failed-attempt
  memo (same key), so a failed CUDA / HIP build is never repeated in one worker
- Only successful config reads are cached; a miss is read again next time
- A kernel a peer installed while this worker waited counts as new here, so inference refreshes
  or restarts its already-imported modeling modules
- The install lock lives beside the environment (else one per environment in the temp folder),
  and every mutating entry point takes it: Studio workers, setup's stale-kernel removal and
  unsloth install-kernels. A lock that cannot be opened or taken skips the install instead of
  racing unguarded; optional accelerations wait at most 120 s for a peer, required ones 2400 s
- Force-terminating a training run first stops the processes its worker started, including a
  source build in its own session
- A runtime install whose installer cannot even start still removes a broken copy and is
  remembered as failed
- Tests never take the environment's real install locks
…amba / Zamba / Bamba

_ensure_mamba_ssm defaulted essential to True, so a caller that left it out (the ROCm hardware
probe on gfx1151) compiled mamba-ssm from source for a Mamba2 model for 60 s. Unspecified now
means the name decides: only Nemotron-H, Falcon-H1 and Granite-4.0-H may build it.
…n CUDA build deadline

- Export worker: run the malware and remote-code consent gates (transformers-free,
  no load-subdir scan) before provisioning SSM kernels, at startup and on each
  "load" command, so a blocked checkpoint never triggers a kernel build.
  _handle_load keeps its authoritative re-check through the shared helper.
- Export offline window: mark an HF offline set only because the Hub was
  unreachable; package_installs_offline() and package_installs_offline_env()
  then honour only UV_OFFLINE, so required kernels can still come from GitHub /
  PyPI. An operator-set HF offline still blocks installs.
- install_python_stack: re-probe a stale SSM extension while holding the kernel
  install lock and remove it only if it still fails the same way.
- unsloth install-kernels --dry-run prints the wheel URL even offline.
- ssm_runtime: a model that cannot load without mamba-ssm now fails clearly on a
  CPU / XPU torch instead of falling through to a torch path that cannot work.
- CUDA source builds get their own 7200 s deadline (UNSLOTH_SOURCE_BUILD_TIMEOUT
  overrides); HIP keeps 1800 s; KERNEL_LOCK_WAIT covers the longer of the two.
… wheel is unusable

A Mamba-2 load that cannot get the py3-none-any mamba_ssm wheel (not published,
refused, or not importable after install) now takes the CUDA-wheel route,
causal-conv1d then mamba-ssm, instead of continuing without either. The pure
attempt is remembered on its own failure key, so it does not block the CUDA
wheel and a later load in the process goes straight to it; both routes still
run under the same per-environment install lock.
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 18, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ⚠️ Failed 2026-10-20T13:11:38.854442Z 7e4f303 New commits
🔒 Security Review ⚠️ Failed 2026-10-18T21:38:11.356958Z 52762b0 PR opened
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

chatgpt-codex-connector[bot]

This comment was marked as resolved.

…emote code counts only when loaded

A load on the CUDA route (Mamba-1 families, remote code, UNSLOTH_MAMBA_CUDA_KERNELS=1)
accepted any importable mamba_ssm, so the extension-free wheel a Mamba-2 load left behind
was kept and transformers took mamba_ssm's fast path into selective_scan_fn, which raises
without selective_scan_cuda. That route now requires the compiled scan, reinstalls over the
extension-free copy under the same lock and memo, drops the stale mamba_ssm modules so the
next import loads the new copy, and removes the extension-free copy if the replacement fails.
The Triton route still accepts an installed CUDA mamba_ssm.

The Triton gate excluded every config with an AutoModel auto_map entry. It now excludes only
loads that run the remote implementation: trust_remote_code on, or no native class. Training
passes its trust_remote_code into the resolution.
…te already failed

A CUDA-route failure is remembered for the process; if a later Mamba-2 load put
the extension-free wheel back, a Mamba-1 load would return early and keep it,
then fail in selective_scan_fn. Remove it under the install lock instead.
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

1 similar comment
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 8c84e0556f

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment on lines +124 to +130
# mamba_ssm with no compiled extension (MAMBA_KEEP_CUDA_BUILD unset), one py3-none-any wheel for every
# torch, interpreter and platform. Its Triton kernels are all the Mamba-2 mixers call; unsloth_zoo
# serves their causal conv (prefill, fused training path, decode) on Triton, so with a zoo that has
# those drop-ins the Mamba-2 hybrids need neither CUDA wheel. Built and signed by
# prebuilt-cuda-wheels.yml on the same tag.
MAMBA_SSM_PURE_VERSION = "2.3.2.post1"
MAMBA_SSM_PURE_WHEEL = f"mamba_ssm-{MAMBA_SSM_PURE_VERSION}-py3-none-any.whl"

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge Add the pure wheel to the release workflow

The new URL names mamba_ssm-2.3.2.post1-py3-none-any.whl, but a repo-wide search finds no build or publish step that creates it: .github/workflows/prebuilt-cuda-wheels.yml only publishes the matrix generated from .github/scripts/prebuilt_wheels.py, whose mamba spec always sets MAMBA_KEEP_CUDA_BUILD=TRUE and requires selective_scan_cuda. Therefore a normal run of the workflow cited here cannot upload the extension-free asset, so every pure-wheel probe returns 404 and silently falls back to the two CUDA wheels. Add this universal wheel to the planned, smoke-tested, signed, and published artifacts.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

The build lives in #14142 (commit 9413cb8), which this PR pairs with: a CPU-only job builds mamba_ssm-2.3.2.post1-py3-none-any.whl from the pinned commit without the extension, gates it (exact filename, no compiled files, py3-none-any metadata, --no-deps install and import on Ubuntu and Windows), and it joins the existing sign and publish steps. Until that republish lands, the probe here gets a 404 and the load takes the CUDA route, which is the intended fallback.

…l reporting for the mamba-ssm routes

- remote_code / remote_code_used read every auto_map in the config, sub-configs included, as
  the remote-code consent gate does: the loader runs a composite model's text_config.auto_map.
- The Triton route follows setup's mamba Triton GPU floor (_mamba_ssm_floor_reason); below it
  the load keeps today's route.
- Offline, the CUDA route still drops an extension-free copy (removal is local), in the runtime
  and in training; training also drops it on a remembered failure.
- Training's source-build fallback uninstalls the extension-free copy before building, since
  pip / uv would call it satisfied, and accepts the build only with selective_scan_cuda.
- A removal is reported in the newly installed list ("removed mamba_ssm"), so provision
  refreshes transformers' gates (dropping cached kernel modules too) and the inference worker
  runs its stale-module / restart check as for an install.
chatgpt-codex-connector[bot]

This comment was marked as resolved.

@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Oct 18, 2026
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Oct 18, 2026
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Oct 18, 2026
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Oct 18, 2026
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

1 similar comment
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Another round soon, please!

Reviewed commit: ab9c599044

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@danielhanchen

Copy link
Copy Markdown
Member Author

The Source lint failure (No new risky loader call sites step) comes from this PR's base, not its diff. This PR is based on studio-ssm-on-demand-followups (#13971), and that branch at 7c90c5bd adds a trust_remote_code load at studio/backend/core/export/worker.py:330 that the risky-loader baseline does not list. export/worker.py is not in this PR's diff, and tests/security/test_lint_risky_loaders.py passes on main.

The fix belongs on #13971 (details in my comment on #14148). Either add that call site to the baseline with the consent gate it sits behind, or route the load through the consent-checked helper. This PR clears once its base carries that.

Base automatically changed from studio-ssm-on-demand-followups to main October 19, 2026 11:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant