Skip to content

studio: skip flash-attn install on Blackwell GPUs (sm_100+) - #5420

Merged
rolandtannous merged 8 commits into
mainfrom
fix/blackwell-flashattention
May 14, 2026
Merged

rolandtannous merged 8 commits into
mainfrom
fix/blackwell-flashattention

Conversation

@rolandtannous

@rolandtannous rolandtannous commented May 14, 2026 •

Copy link
Copy Markdown
Contributor

Summary

  • Add shared has_blackwell_gpu() helper in studio/backend/utils/wheel_utils.py that detects compute capability >= 10.0 (sm_100, sm_120, sm_121, ...) via nvidia-smi --query-gpu=compute_cap. Works on Linux and Windows.
  • Gate studio/install_python_stack._ensure_flash_attn on the helper before the existing OS/no-torch short-circuit, so Blackwell users (including Blackwell-on-Windows) see an explicit warning instead of a silent skip.
  • Gate studio/backend/core/training/worker._ensure_flash_attn_for_long_context on the same helper so the runtime install path also skips on Blackwell, with a status message to the event queue.

Why: Dao-AILab does not publish prebuilt flash-attn wheels for Blackwell architectures, and the older-arch wheels fail to load on those GPUs. Attempting the install just produces noisy warnings (install-time) or fails at training start and chat with CUDA_ASSERT errors

Non-Blackwell Windows behavior is unchanged — the pre-existing IS_WINDOWS short-circuit still applies.

Test plan

  • ~/.unsloth/studio/unsloth_studio/bin/python -m pytest tests/python/test_flash_attn_install_python_stack.py -v — covers has_blackwell_gpu() (sm_100/sm_120/sm_121 → True, sm_89/sm_90 → False, mixed GPUs, missing nvidia-smi, failures, timeouts, malformed output) and install-path skip on Blackwell (Linux + Windows).
  • ~/.unsloth/studio/unsloth_studio/bin/python -m pytest studio/backend/tests/test_training_worker_flash_attn.py -v — covers runtime path skip on Blackwell with status message.
  • On a Blackwell Linux box, run ./install.sh --local and confirm log line "Skipping flash-attn: Blackwell GPU detected (sm_100+); no compatible prebuilt wheel" and that no flash-attn install is attempted.

Roland Tannous added 2 commits May 14, 2026 15:46
Dao-AILab does not publish prebuilt flash-attn wheels for sm_100, sm_120,
or sm_121, and the older-arch wheels fail to load on Blackwell. Add a
shared has_blackwell_gpu() helper and gate both the install-time
(install_python_stack._ensure_flash_attn) and runtime
(worker._ensure_flash_attn_for_long_context) paths on it. Detection uses
nvidia-smi --query-gpu=compute_cap, which works on Linux and Windows.
prefers_prebuilt_wheel and falls_back_to_pypi exercise the install
paths that the Blackwell guard now short-circuits. Make them explicit
about non-Blackwell so they pass on real Blackwell hosts.

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Code Review

This pull request implements a detection mechanism for Blackwell GPUs (compute capability 10.0+) to skip flash-attention installation, as compatible prebuilt wheels are not yet available. The changes include a new utility function has_blackwell_gpu using nvidia-smi, integration into the training worker and installation scripts, and comprehensive unit tests. Feedback suggests caching the GPU check result for efficiency and reordering the check in the installation script to avoid redundant warnings when Torch is disabled.

Comment thread studio/backend/utils/wheel_utils.py Resolved
Comment thread studio/install_python_stack.py Resolved
Roland Tannous and others added 3 commits May 14, 2026 16:31
- Wrap has_blackwell_gpu in functools.lru_cache so repeated calls in a
  single process avoid redundant nvidia-smi spawns. Tests clear the
  cache via setup_method/teardown_method.
- In _ensure_flash_attn, run the NO_TORCH short-circuit before the
  Blackwell check so GGUF-only users (who never install torch anyway)
  do not see a Blackwell warning. Blackwell check still runs above the
  IS_WINDOWS / IS_MACOS gates so Blackwell-on-Windows users still see
  the explicit reason rather than a silent OS skip.
@rolandtannous

Copy link
Copy Markdown
Contributor Author

tests:
Screenshot 2026-05-14 at 3 59 52 PM
Screenshot 2026-05-14 at 4 20 51 PM
Screenshot 2026-05-14 at 4 19 24 PM
Screenshot 2026-05-14 at 4 00 12 PM
Screenshot 2026-05-14 at 4 23 30 PM

end to end test test on blackwell:

flash_attention_end_to_end_test_blackwell.mp4

Roland Tannous and others added 3 commits May 14, 2026 16:50
test_mlx_training_worker_config loads worker.py against a hand-rolled
utils.wheel_utils stub. Adding has_blackwell_gpu to the stub symbol
list so worker's import line resolves.
@rolandtannous

Copy link
Copy Markdown
Contributor Author

already passed CI/CD . merging.tests shared above.

@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: 47189b6900

ℹ️ 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 +43 to +44
result = subprocess.run(
[exe, "--query-gpu=compute_cap", "--format=csv,noheader"],

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 Respect selected GPUs when checking Blackwell

On mixed-GPU hosts where resolved_gpu_ids selects only non-Blackwell cards, this still queries every GPU that nvidia-smi knows about and returns true if any unselected Blackwell card exists. run_training_process calls apply_gpu_ids(...) before the flash-attn check, and NVIDIA documents CUDA_VISIBLE_DEVICES as filtering CUDA applications while integer IDs are the system ordinals reported by nvidia-smi, so this subprocess scan is not limited to the CUDA-visible training devices. The result is that long-context jobs pinned to Ampere/Hopper GPUs skip a compatible flash-attn install just because another Blackwell GPU is present in the machine.

Useful? React with 👍 / 👎.

danielhanchen added a commit that referenced this pull request Aug 12, 2026
A prebuilt wheel can install with exit code 0 and then fail to load, so the exit code
on its own is not proof the install is usable. Both Blackwell incidents were that
shape: in #5420 the older-arch wheels installed and raised on import, and the arch
gate added to work around it became the bug in #6961 once Dao-AILab started shipping
sm_100 wheels.

Verify the import after a zero exit code in the two paths that did not, and treat a
failure as not installed. ssm_runtime._install_kernel already did exactly this, for
the same reason, so this is that check applied to the setup installer and the
long-context training worker.

worker._is_importable() catches any exception rather than only ImportError: an
arch/ABI mismatch surfaces as OSError or RuntimeError ("undefined symbol"), which the
old pre-check would have let escape mid-training.

Also drop has_blackwell_gpu() and its two call sites. It has returned False
unconditionally since 03cbe21, so both blocks were already unreachable and removing
them changes no behaviour. An arch gate encodes a snapshot of what upstream publishes
and goes stale silently in both directions, whereas the import check catches a wheel
that will not load whatever the cause.

Checked on an 8x B200 host (compute_cap 10.0): the wheel this resolver builds for
cu13/torch2.10/cp313 carries sm_100 and sm_120 cubins, installs through
install_wheel(), and runs, matching SDPA to 0.0078 max abs difference on a
2x4096x16x128 bf16 causal forward.
danielhanchen added a commit that referenced this pull request Aug 12, 2026
* Studio: verify the flash-attn import after installing it

A prebuilt wheel can install with exit code 0 and then fail to load, so the exit code
on its own is not proof the install is usable. Both Blackwell incidents were that
shape: in #5420 the older-arch wheels installed and raised on import, and the arch
gate added to work around it became the bug in #6961 once Dao-AILab started shipping
sm_100 wheels.

Verify the import after a zero exit code in the two paths that did not, and treat a
failure as not installed. ssm_runtime._install_kernel already did exactly this, for
the same reason, so this is that check applied to the setup installer and the
long-context training worker.

worker._is_importable() catches any exception rather than only ImportError: an
arch/ABI mismatch surfaces as OSError or RuntimeError ("undefined symbol"), which the
old pre-check would have let escape mid-training.

Also drop has_blackwell_gpu() and its two call sites. It has returned False
unconditionally since 03cbe21, so both blocks were already unreachable and removing
them changes no behaviour. An arch gate encodes a snapshot of what upstream publishes
and goes stale silently in both directions, whereas the import check catches a wheel
that will not load whatever the cause.

Checked on an 8x B200 host (compute_cap 10.0): the wheel this resolver builds for
cu13/torch2.10/cp313 carries sm_100 and sm_120 cubins, installs through
install_wheel(), and runs, matching SDPA to 0.0078 max abs difference on a
2x4096x16x128 bf16 causal forward.

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* Tighten the comments added by this PR (no behaviour change)

* Stub utils.native_tls in the MLX worker config test

worker.py imports six utils.* submodules and calls activate_native_tls() at import
time; _load_worker_module stubbed five of them. The file therefore loaded only when
another test had already imported the real utils.native_tls, so it passed in a full
run and failed on its own with "No module named 'utils.native_tls'".

Reproduced on upstream main at aaf9948, so this predates this branch: running the
file alone, or in any narrow selection, errors during collection. Cross-platform CI
runs the PR's own test files as a narrow selection, which is how it surfaced.

* Replace a rejected wheel on the fallback, and verify that install too

Rejecting a wheel that installed but would not import left the broken distribution in
site-packages, and the fallback installed over it rather than replacing it. pip reports
it as already satisfied and uv audits it as no change, both exiting 0, and that exit
code reached an unconditional return True. Long-context training then continued with an
unusable extension, which is the failure the wheel check was meant to stop.

Measured against a real flash_attn 2.8.1 install:
  pip install --no-build-isolation --no-deps flash-attn
    -> "Requirement already satisfied: flash-attn ... (2.8.1)", exit 0
  uv pip install --no-build-isolation --no-deps flash-attn
    -> "Audited 1 package", "Would make no changes", exit 0

So pass --force-reinstall (pip) / --reinstall (uv) on the fallback when a wheel was
rejected, and verify the import afterwards instead of trusting rc=0. The flag is gated on
the rejection so the ordinary path keeps installing over nothing and does not rebuild.

test_runtime_flash_attn_falls_back_to_pypi mocked an install that exits 0 without ever
making the module importable, which under the post-install check is a failed install, not
a successful one. It now flips the import stub when the install runs.

* Uninstall a rejected wheel instead of reinstalling over it, and probe out of process

Two problems with the previous commit.

--force-reinstall was the wrong tool. pip documents it as "Reinstall all packages even
if they are already up-to-date" and uv's --reinstall as "Reinstall all packages": both
scope to the whole resolved transaction, not the named one. flash-attn depends on torch,
so on the plain fallback path (which carries no --no-deps) that could reinstall or
downgrade the torch the worker is currently running on. Measured with a stand-in whose
metadata resolves, since flash-attn's sdist cannot build metadata here:

  pip install --dry-run requests                    -> 0 packages
  pip install --force-reinstall --dry-run requests  -> 5 packages
                                                       (requests + its whole closure)

It also fails outright on that path: --force-reinstall makes pip rebuild flash-attn from
sdist, and the plain branch passes no --no-build-isolation, so the build dies on
"No module named 'torch'". This file already pairs --force-reinstall with --no-deps
elsewhere for the same reason.

So remove the rejected distribution and leave the fallback command untouched. Nothing is
reported as already satisfied afterwards, the transaction stays exactly as wide as it was
before, and no rebuild flags change.

Second, the post-install probe now runs in a child. A wheel built for the wrong arch can
abort or segfault inside the extension's initialiser rather than raising, and except
Exception cannot catch that: it would kill the worker and take the fallback with it. The
child turns it into a return code, negative for a fatal signal. install_python_stack
already probes this way; the worker now matches it. The cheap in-process check stays for
"is it already installed", which costs no spawn on every training start and imports the
module the worker wants imported anyway.

The module name is passed as argv rather than formatted into the -c body.

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* Bound the installer's flash-attn import probe

The worker bounds this same untrusted import at 300s and handles TimeoutExpired; the
installer probe had no timeout, so a native extension that hangs in its initialiser
rather than failing would leave setup waiting forever and never reach the warning. The
asymmetry arrived with the post-install probe added earlier in this branch, which is
exactly the call that hands an unvalidated wheel to the import machinery.

Bound it the same way and treat a hang, or a probe that cannot be spawned, as an
unusable wheel. The timeout is a named constant on both sides so the two paths cannot
drift apart again silently.

* Tighten the comments added in review (no behaviour change)

* Remove a rejected flash-attn wheel in the installer too

The setup path warned and continued while leaving the unusable distribution in
site-packages, so "Continuing without flash-attn" was not true. unsloth/models/_utils.py
gates on _package_available("flash_attn"), which reads metadata rather than importing,
and then does an in-process "from flash_attn.flash_attn_interface import ...". A wheel
that aborts during native initialisation therefore takes the training process down, and
that is exactly the wheel this branch has just rejected.

Confirmed with a package that calls os.abort() at import: importing it in process kills
the interpreter with SIGABRT, while the out-of-process probe returns rc -6 and survives.
Uninstalling a properly installed distribution removes it cleanly.

The worker path already uninstalls before its fallback; the installer now matches.

* Uninstall with the mode uv was installed with, and stop claiming a failed removal

Two problems in the cleanup added in the previous commit.

It hard-coded "uv pip uninstall --python". _bootstrap_uv sets UV_NEEDS_SYSTEM exactly
when the --python probe FAILED and --system succeeded, so on those hosts the cleanup used
the one mode already known not to work there: the uninstall fails, the unusable wheel
stays in site-packages, and unsloth/models/_utils.py still finds it by metadata and
imports it in process. Mirror the install mode instead. uv documents --system as "Use the
system Python to uninstall packages".

It also printed "removed it" unconditionally, so a failed removal produced two
contradicting warnings in the same run: "Could not remove the unusable flash-attn
install" followed by "...; removed it". The helper now returns whether the package is
actually gone, and the caller says which happened. A wheel still installed is not the
same state as never having installed one, so the failure case says so and points at the
manual uninstall.

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* Route every rejected install through one discard path

The PyPI fallback returned False without removing the distribution it had just rejected,
so the unusable extension stayed where unsloth/models/_utils.py finds it: that gate reads
package METADATA and then imports the native module in process, which is exactly the
import the isolated probe could not survive. Two ways in: a failed uninstall leaves the
wheel "already satisfied" so the fallback no-ops, or the source build itself produces an
incompatible extension.

This is the third round on the same defect, in a third place, so fix the shape rather
than the instance. _reject_install is now the single discard path: it uninstalls, and it
reports which state we actually ended in. _uninstall_package returns whether the
distribution is gone rather than only logging.

The pre-fallback uninstall stays a plain call: a failure there is not fatal, because the
fallback then no-ops on "already satisfied", the probe rejects it, and _reject_install
reports the real state.

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* Enforce the discard invariant in one place instead of at each return

The timeout and install-failure exits returned without discarding the rejected
distribution, so a wheel that had already failed its import check stayed where
unsloth/models/_utils.py finds it: that gate reads METADATA and only then imports the
native module in process. The comment added last round claimed the fallback path would
clean up on its own, and that is only true when the fallback exits 0.

This is the fourth defect of the same shape, each one an exit somebody did not think to
clean up, so enforce the invariant structurally rather than adding a fourth call.
_install_package_wheel_first now keeps the two "touch nothing" guards (already importable,
offline) and delegates the rest to _attempt_package_install, discarding whatever is left
in a finally. Any unsuccessful exit, including ones added later, is covered.

The discard is state-based so it is safe to run everywhere: _distribution_present reads
metadata via importlib.metadata without importing, so it never loads an extension that
would abort, and _reject_install no-ops when there is nothing installed.

Verified on the B200 that a working flash-attn 2.8.1 is still untouched: three consecutive
runs return True with zero install or uninstall subprocesses, and it stays importable.

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* State the installed-but-broken state in the rejection tests

Two rejection tests asserted on the discard status message without pinning
_distribution_present, so after the discard became state-based they only passed where
flash-attn happened to be installed. That was true in the venv I had been running them in
and false on a clean runner, where the discard correctly no-ops and no status is sent.
Cross-platform CI caught it on ubuntu-latest; the same two tests fail in a fresh venv here
and pass with the state pinned.

No production change: the invariant holds either way. The tests were reading the machine
rather than the code.

* Uninstall from the interpreter install_wheel actually installed into

install_wheel always targets sys.executable: its uv command passes --python in addition
to --system, and its pip fallback runs that interpreter directly. The cleanup passed
--system INSTEAD of --python, so on a UV_NEEDS_SYSTEM host it uninstalled from the system
Python while the wheel sat in the venv, and setup then reported the wheel removed when it
was still there for the metadata gate to import.

Pass both, so the removal mirrors the install rather than half of it. The earlier test
asserting the --system-only command is replaced, since it pinned the broken command.

The worker path already targets --python sys.executable on both sides and needs no change.

* Tighten the review comments (no behaviour change)

---------

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
@danielhanchen danielhanchen mentioned this pull request Sep 21, 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.

1 participant