Skip to content

Add unsloth install-kernels for prebuilt xformers / causal_conv1d / mamba_ssm wheels - #12732

Merged
danielhanchen merged 5 commits into
mainfrom
feat/install-kernels
Oct 5, 2026
Merged

danielhanchen merged 5 commits into
mainfrom
feat/install-kernels

Conversation

@danielhanchen

Copy link
Copy Markdown
Member

Notebook install cells currently carry a hand-maintained torch to xformers map plus a cu130 wheel URL, and the Qwen3.5 / Qwen3.6 notebooks carry a 3 line causal_conv1d URL template. Studio already resolves these wheels exactly, so this exposes that resolver as one command:

!unsloth install-kernels xformers causal_conv1d

What it does

  • Probes the resident torch (version, CUDA, Python tag, ABI, platform) with wheel_utils.probe_torch_wheel_env.
  • xformers resolves through wheel_utils.xformers_wheel_url (exact torch patch x CUDA minor table, 0.0.35 above the stable ABI floor, honours UNSLOTH_PYTORCH_MIRROR).
  • causal_conv1d and mamba_ssm resolve through wheel_utils.direct_wheel_url, so torch 2.11 / 2.12 reuse the measured torch 2.10 wheels and torch 2.13 / 2.14 use the prebuilt-wheels-cu13 release, the same as Studio.
  • Wheel only. When nothing is published for this torch / CUDA / Python (CPU, ROCm, macOS, an unmeasured torch), it installs nothing, prints that the torch fallback is used, and exits 0. It never builds from source.
  • Installs use --no-deps, so torch is never touched. An install that already loads is left alone. A wheel whose extension fails to load after install is removed again, and the command exits 1.
  • --dry-run prints the resolved URL only.

Entry points

  • unsloth install-kernels ... dispatches in unsloth_cli/__init__.py before typer and the command modules are imported, because notebooks install unsloth with --no-deps and may not have typer.
  • python -m unsloth_cli install-kernels ... takes the same path.
  • A typer command of the same name is registered so unsloth --help lists it.
  • The causal-conv1d and mamba-ssm pins move from ssm_runtime.py to wheel_utils.py, so there is one source of truth. ssm_runtime re-imports them, so its existing lockstep tests are unchanged.

Testing

  • New studio/backend/tests/test_kernel_install.py: resolution matrix (torch 2.8.0+cu126, 2.9.0+cu128, 2.10.0+cu128, 2.11.0+cu130, 2.12.1+cu130, CPU, Windows, no torch), no install when nothing is published, --no-deps argv, idempotence, removal of a wheel that does not load, --dry-run, and the console entry running with typer made unimportable.
  • Existing test_ssm_runtime.py, test_wheel_utils_xformers.py, test_wheel_utils_url_exists.py, module entry point, typer floor and CI matrix tests pass.
  • Real installs, torch 2.11.0+cu130 / Python 3.13 (the current Colab stack): installs xformers-0.0.35 (cu130) and causal_conv1d-1.6.1+cu13torch2.10. A second run reports both already installed. On GPU, causal_conv1d_fn matches a torch conv1d + SiLU reference (max err 3.8e-3, fp16) and memory_efficient_attention matches SDPA (max err 2.0e-3).
  • pip install --no-deps of this branch into a venv without typer: the unsloth install-kernels console script runs, and --help works.
  • torch 2.11.0+cpu: both kernels print the fallback message, nothing is installed, exit 0.
  • torch 2.13.0+cu130 --dry-run: resolves xformers 0.0.35 cu130 plus the prebuilt-wheels-cu13 causal_conv1d 1.7.0 and mamba_ssm 2.3.2.post1 wheels.

A follow up in unslothai/notebooks will replace the xformers map and the causal_conv1d lines with this command once a release containing it is on PyPI.

…amba_ssm wheels

Notebook install cells currently carry a hand-maintained torch -> xformers
map plus a cu130 URL, and a 3 line causal_conv1d URL template. Studio already
resolves these wheels exactly (wheel_utils.xformers_wheel_url and
direct_wheel_url), so expose that resolver as a one-line command:

  !unsloth install-kernels xformers causal_conv1d

Wheel-only: when no wheel exists for this torch / CUDA / Python (CPU, ROCm,
macOS, an unmeasured torch) nothing is installed and the torch fallback is
kept. Installs are --no-deps so torch is never touched, an install that
already loads is left alone, and a wheel whose extension fails to load is
removed again.

The console script dispatches before typer and the command modules are
imported, because notebooks install unsloth with --no-deps. The causal-conv1d
and mamba-ssm pins move into wheel_utils so ssm_runtime and the new module
share one source of truth.
@danielhanchen danielhanchen self-assigned this Oct 5, 2026
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 5, 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 ✅ Completed 2026-10-05T11:10:32.227878Z ebb6514 Manual request
ℹ️ 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

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 🔄 Running since 2026-10-05T10:10:47.471551Z 57d6a91 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.

A uv-created venv has no pip, and pip keeps an installed same-version
build even when its extension does not load. Route through
wheel_utils.install_wheel (uv first, --system outside a venv, pip
fallback) with a new opt-in reinstall flag, and uninstall a wheel that
does not load with the same tool.
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Oct 5, 2026
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@danielhanchen

Copy link
Copy Markdown
Member Author

@codex security review

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

@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: e819cd8dca

ℹ️ 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 +106 to +108
if dry_run:
print(url)
return 0

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P1 Badge Redact mirror credentials before printing wheel URLs

When UNSLOTH_PYTORCH_MIRROR contains userinfo or a query token, xformers_wheel_url() deliberately preserves those credentials, so printing url exposes them in notebook output and logs; the same leak occurs in the install-failure and post-install messages below. wheel_utils.redact_url_credentials() already documents and implements the required redaction, so use it for every displayed URL while continuing to pass the original URL to the installer.

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.

Fixed in be9fbc2: every printed URL and the installer output tail go through redact_url_credentials.

Comment thread studio/backend/utils/kernel_install.py Outdated
Comment on lines +126 to +130
if not _loads(check, run):
# A wheel that imports but whose extension cannot load would be picked up and crash later.
_uninstall(distribution, run)
print(
f"Unsloth: {name} from {url} does not load with {torch_desc}; removed it, using the torch fallback."

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 Verify that the rejected wheel was actually uninstalled

If the uninstall subprocess fails—for example because uv is present but cannot modify the selected interpreter—its return code is discarded, yet the command reports that the incompatible wheel was removed. This leaves a wheel known not to load installed, so later imports can still fail or crash despite the fallback message; check the uninstall result and explicitly report that manual removal is required when it fails.

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.

Fixed in be9fbc2: the message only says removed when the uninstall returned 0, otherwise it names the pip uninstall command.

Comment thread studio/backend/utils/kernel_install.py Outdated
Comment on lines +38 to +39
"causal_conv1d": ("causal-conv1d", "import causal_conv1d"),
"mamba_ssm": ("mamba-ssm", "import mamba_ssm"),

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P1 Badge Probe selective_scan_cuda when validating mamba_ssm

When the mamba wheel lacks its CUDA extension or contains an ABI-incompatible one, import mamba_ssm can still succeed because that package treats selective_scan_cuda as optional, so this check reports the kernel as usable even though the command was meant to install that compiled fast path. The repository's own .github/scripts/prebuilt_wheels_forward.py explicitly notes that importing mamba_ssm alone proves nothing and separately imports selective_scan_cuda; make this validation do the same so a broken wheel is rejected rather than reported as installed.

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.

Fixed in be9fbc2: mamba_ssm is judged by importing selective_scan_cuda and causal_conv1d by causal_conv1d_cuda. Importing the packages was the wrong check in both directions: import causal_conv1d never loads the extension, and import mamba_ssm fails on einops under --no-deps.

Comment on lines +69 to +73

def _loads(check: str, run: Callable[..., subprocess.CompletedProcess]) -> bool:
result = run(
[sys.executable, "-c", check], stdout = subprocess.DEVNULL, stderr = subprocess.DEVNULL
)

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 Bound native-extension import probes with a timeout

When a mismatched native extension stalls during initialization rather than exiting, both the pre-install and post-install _loads calls wait forever because this subprocess has no timeout. The existing isolated probes in core/training/worker.py and install_python_stack.py use a 300-second bound specifically because native initialization can hang; apply the same timeout here and treat TimeoutExpired as a failed load so an interactive install command cannot remain stuck indefinitely.

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.

Not changing: the probe imports one installed extension once per command run, and a hang there would also hang the model load that follows. A timeout would only turn that into a different failure.

…e the -m fast path

- causal_conv1d / mamba_ssm are judged by loading causal_conv1d_cuda /
  selective_scan_cuda. import causal_conv1d never loads the extension, and
  import mamba_ssm fails on einops under --no-deps, which removed a good wheel.
- Printed URLs go through redact_url_credentials, since
  UNSLOTH_PYTORCH_MIRROR may carry credentials.
- A failed uninstall is no longer reported as removed.
- Under python -m, dispatch only when the module run is unsloth_cli, not a
  host package that happens to import it.
@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: be9fbc241f

ℹ️ 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".

url = resolve_wheel_url(name, env)
torch_desc = f"torch {env.get('torch_version')}" if env else "this environment"
# Only a 404 proves nothing is published; an unreachable check falls through to the install.
if url is None or exists(url) is False:

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P1 Badge Redact credentials in the availability-probe warning

When UNSLOTH_PYTORCH_MIRROR contains userinfo or a query token and the HEAD request encounters a non-404 HTTP error or network failure, this call reaches wheel_utils.url_exists(), whose warning interpolates the raw URL, so the credential still lands in stderr/logs before shown is computed. Fresh evidence beyond the prior print-site report is the raw-argument logging in url_exists()'s error path; keep using the original URL for the request but redact it in that diagnostic.

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.

Fixed in ebb6514: url_exists logs the redacted URL, and drops the error text when the URL carried credentials, since urllib echoes the userinfo as a bad port.

Comment thread unsloth_cli/__init__.py
Comment on lines +62 to +66
if (_is_entry_point or (_entry_base == "-m" and _runs_unsloth_cli)) and _sys.argv[1:2] == [
"install-kernels"
]:
from unsloth_cli._install_kernels import main as _install_kernels_main
_sys.exit(_install_kernels_main(_sys.argv[2:]))

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 Run the SSL keylog guard before fast-path dispatch

When SSLKEYLOGFILE points to an unwritable machine-wide path, this fast-path exit occurs before _drop_unwritable_ssl_keylog_file() runs. The subsequent url_exists() HTTPS check therefore cannot create an SSL context, and on systems without uv the pip download fails for the same reason, making the new command unusable in exactly the environment the existing entry-point guard handles. Invoke that guard before dispatching install-kernels while retaining plain-import behavior.

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.

Fixed in ebb6514: the fast path now runs drop_unwritable_ssl_keylog_file before dispatch.

…OGFILE first

url_exists logged the raw wheel URL, and urllib's error text can echo the
userinfo of a credentialed UNSLOTH_PYTORCH_MIRROR. The fast path also ran
before the CLI's SSLKEYLOGFILE guard, so an unwritable key log broke the
availability probe and the installers' HTTPS clients.
@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. Swish!

Reviewed commit: ebb6514896

ℹ️ 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
danielhanchen merged commit 9f5a62d into main Oct 5, 2026
@danielhanchen
danielhanchen deleted the feat/install-kernels branch October 5, 2026 12:39
Penguinjanator pushed a commit to Penguinjanator/notebooks that referenced this pull request Oct 7, 2026
* Install xformers / causal_conv1d with unsloth install-kernels

Replace the hand-written wheel picks in the Colab install cells with the
unsloth CLI, which resolves the prebuilt wheel for the installed torch and
CUDA (or installs nothing and keeps the torch fallback):

- xformers: drop the torch probe, the version map, the CUDA 13 URL line
  and {xformers}; run `!unsloth install-kernels xformers` after unsloth
  is installed (default, Gemma 4, DiffusionGemma, QAT and the
  hand-maintained notebooks).
- Qwen3.5 / Qwen3.6: replace the causal_conv1d URL template, which 404s
  on Colab's torch 2.11, and the fla uninstall with
  `!unsloth install-kernels causal_conv1d`. unsloth_zoo's vendored fla
  already wins over an older pip install.
- Qwen3.8: drop the fla uninstall and the uv install it needed. The
  Kaggle GRPO notebook's pinned causal_conv1d URL becomes the same line.
- AMD cells are unchanged: the composer maps the new line back to the fla
  uninstall and the causal_conv1d source build.
- molab: strip the line before marimo conversion, so it no longer leaves
  an empty `import subprocess` cell behind.

Needs an unsloth release that ships install-kernels (unslothai/unsloth#12732).

* Install every kernel with bare unsloth install-kernels

`unsloth install-kernels` with no names installs xformers, flash-attn,
causal_conv1d and mamba_ssm, each skipped when no wheel matches the torch
(unslothai/unsloth#12744), so every Colab cell now runs the bare form.

- Granite 4.0 / Nemotron Nano: keep the resident torch instead of pinning
  torch 2.7.1 for mamba_ssm 2.2.5 wheels, and drop the --no-build-isolation
  install that took ~30 minutes on Blackwell. Wheels exist for every torch
  these cells see (2.7, 2.10 cp312, 2.11, 2.13).
- Falcon H1, LFM2 and Granite 4.0 350M templates and the hand-maintained
  Falcon H1 / LFM2 notebooks: replace the pinned or git-main kernel builds
  with named install-kernels lines.
- AMD notebooks are unchanged: ROCm has no wheels, so the composer maps
  each kernel line back to the line it read before, and
  install-kernels counts as install code when it collects install cells.
- molab: the kernel install becomes its own runtime cell, so molab gets
  wheels for its torch (still pinned to 2.7.1 for the Mamba hybrids)
  instead of the old pins in the PEP 723 header.

* Install EmbeddingGemma 2 kernels with unsloth install-kernels
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