Repository navigation
Studio flashattn - #5435
Studio flashattn#5435Datta0 wants to merge 42 commits into
Conversation
Signed-off-by: Datta Nimmaturi <[email protected]>
There was a problem hiding this comment.
Code Review
This pull request refactors the installation logic for optional training kernels—including flash-attn, causal-conv1d, mamba-ssm, and the newly added flash-linear-attention—into a centralized install_optional_kernel utility driven by a KernelPackageSpec dataclass. Additionally, the sequence length threshold for triggering flash-attn installation has been reduced from 32,768 to 16,384. Feedback was provided to enhance the robustness of the PyPI installation path within the new utility by implementing a fallback from uv to pip upon failure, ensuring consistency with other installation methods in the codebase.
| pypi_cmd = _pypi_install_command( | ||
| spec, | ||
| python_executable = python_executable, | ||
| use_uv = use_uv, | ||
| uv_needs_system = uv_needs_system, | ||
| is_hip = is_hip, | ||
| ) | ||
|
|
||
| run_kwargs: dict[str, object] = { | ||
| "stdout": subprocess.PIPE, | ||
| "stderr": subprocess.STDOUT, | ||
| "text": True, | ||
| "env": child_env_without_native_path_secret(), | ||
| } | ||
| if is_hip: | ||
| run_kwargs["timeout"] = 1800 | ||
|
|
||
| try: | ||
| result = run(pypi_cmd, **run_kwargs) |
There was a problem hiding this comment.
The PyPI installation logic in install_optional_kernel currently lacks a fallback mechanism if uv is requested but fails (e.g., due to environment-specific issues or missing flags). This is inconsistent with the install_wheel function (line 249), which robustly tries uv first and then falls back to pip.
To maintain separation of concerns and avoid coupling with external installer scripts, this logic should be implemented directly within the utility. I suggest refactoring this section to attempt the installation with uv (if available) and then fall back to pip on failure, ensuring that the training process isn't interrupted by uv-specific issues.
run_kwargs: dict[str, object] = {
"stdout": subprocess.PIPE,
"stderr": subprocess.STDOUT,
"text": True,
"env": child_env_without_native_path_secret(),
}
if is_hip:
run_kwargs["timeout"] = 1800
# Try uv first if requested and available, then fall back to pip
if use_uv and shutil.which("uv"):
pypi_cmd = _pypi_install_command(
spec,
python_executable = python_executable,
use_uv = True,
uv_needs_system = uv_needs_system,
is_hip = is_hip,
)
try:
result = run(pypi_cmd, **run_kwargs)
if result.returncode == 0:
_logger.info("Installed %s from PyPI using uv", spec.display_name)
return True
_logger.warning("uv failed to install %s from PyPI; falling back to pip", spec.display_name)
except subprocess.TimeoutExpired:
raise
except Exception as exc:
_logger.warning("uv encountered an error: %s; falling back to pip", exc)
pypi_cmd = _pypi_install_command(
spec,
python_executable = python_executable,
use_uv = False,
uv_needs_system = uv_needs_system,
is_hip = is_hip,
)
try:
result = run(pypi_cmd, **run_kwargs)References
- Avoid coupling modules by importing installer script logic into backend utilities. If a utility needs similar functionality, implement it within the utility itself to maintain separation of concerns.
|
CC says gemini's concern is legitimate. If we want pip to be a uv fallback on failure we should address. |
|
Warning You have reached your daily quota limit. Please wait up to 24 hours and I will start processing your requests again! |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 5ef59a8a59
ℹ️ 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".
| if spec in (FLASH_ATTN_SPEC, FLASH_LINEAR_ATTN_SPEC) and is_blackwell(): | ||
| _send_status( | ||
| event_queue, | ||
| f"Skipping {spec.display_name} install: Blackwell GPU detected (sm_100+); no compatible prebuilt wheel", | ||
| ) | ||
| continue |
There was a problem hiding this comment.
Don't skip PyPI-only FLA on Blackwell
When a Blackwell GPU is present and the selected model needs the FLA path (for example Qwen3-Next/Qwen3.5), this guard skips FLASH_LINEAR_ATTN_SPEC before install_optional_kernel can run. Unlike FLASH_ATTN_SPEC, the flash-linear-attention spec is PyPI-only (build_wheel_url returns None), so the “no compatible prebuilt wheel” rationale does not apply; this leaves those models without the newly required flash-linear-attention package specifically on Blackwell machines.
Useful? React with 👍 / 👎.
…tall_optional_kernel Adds three targeted tests for install_optional_kernel branches that were not previously asserted on the new shared helper: * ROCm with no hipcc on PATH returns False without invoking run and surfaces a hipcc status message. * ROCm with hipcc and a resolved gcc install dir injects --gcc-install-dir into HIPCC_COMPILE_FLAGS_APPEND on the subprocess env and applies the 1800s HIP source build timeout. * Direct wheel install scrubs UNSLOTH_NATIVE_PATH_LEASE_SECRET from the subprocess env so the native path lease secret never leaks into pip or uv.
The production flash-linear-attention install path lives in _ensure_flash_linear_attention_unconditional in studio/backend/core/training/worker.py and does not go through install_optional_kernel, so FLASH_LINEAR_ATTN_SPEC was only ever referenced by the wheel_utils unit tests. Removes the dead export and re-points the affected tests at an in-test synthetic plain-PyPI spec via the new _plain_pypi_spec helper. Test coverage of the plain-PyPI install branches in install_optional_kernel is unchanged.
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: adb78393b6
ℹ️ 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".
| try: | ||
| __import__(import_name) | ||
| except ImportError: |
There was a problem hiding this comment.
Treat broken installed kernels as not importable
When setup runs with a stale or partially broken flash_attn package whose top-level import raises something other than ImportError (for example an OSError/RuntimeError from ABI or CUDA library initialization), this helper lets the exception propagate before it can check/install the compatible prebuilt wheel. The previous setup path ran python -c 'import flash_attn' and treated any non-zero result as missing, so these environments now abort installation instead of repairing flash-attn.
Useful? React with 👍 / 👎.
Addresses several findings from a 20-reviewer pass over PR unslothai#5435: * wheel_utils._package_is_importable now treats any import failure as "missing", not just ImportError. An ABI-broken native package such as flash_attn after a torch bump raises OSError or RuntimeError on import and would otherwise crash the helper before the wheel install or fallback path could run. Logs a warning when the broad branch fires so users still see why the package is being re-installed. * install_optional_kernel now tails the installer stdout on a wheel install failure when no PyPI fallback is allowed (setup-time flow). Previously stdout was only sent to _logger.warning so setup users did not see the resolver, network, cache, or ABI error that caused the wheel install to fail. * install_python_stack run() pip_install_try() and the USE_UV branch of pip_install() now scrub UNSLOTH_STUDIO_NATIVE_PATH_LEASE_SECRET from the child env via child_env_without_native_path_secret(). The optional-kernel subprocesses were already sanitized in the original PR; this extends the same hygiene to the generic install subprocesses in the same setup pipeline. * _ensure_flash_attn() now reads the install_optional_kernel boolean return value and emits "Continuing without flash-attn" when the wheel install fails. The previous flow lost that final status line. * Adds a regression test for the broadened import-failure branch.
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: cde405fcf2
ℹ️ 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".
| if result.returncode == 0 and any( | ||
| line.strip() for line in result.stdout.splitlines() | ||
| ): | ||
| return True |
There was a problem hiding this comment.
Respect the process GPU mask before returning true
In environments where CUDA_VISIBLE_DEVICES is set to an empty mask or to non-NVIDIA/ROCm devices, nvidia-smi --query-gpu=name can still list the host's physical NVIDIA GPUs even though none are visible to this worker process. Because this returns True before the torch visibility fallback runs, the new flash-attn/FLA gates can try to install NVIDIA-only kernels for CPU-masked or AMD-selected runs instead of skipping them as intended by the docstring and callers.
Useful? React with 👍 / 👎.
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: df4f898278
ℹ️ 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".
| uv_needs_system = uv_needs_system, | ||
| run = run, | ||
| ): | ||
| if result.returncode == 0: |
There was a problem hiding this comment.
Recheck the kernel after a successful reinstall
When _package_is_importable() returned false because an already-installed native extension is broken, treating any installer exit code 0 as success can leave the environment unrepaired: the direct-wheel commands omit a reinstall flag, and python -m pip install --help describes --force-reinstall as “Reinstall all packages even if they are already up-to-date,” so a same-version installed wheel can be skipped while this returns True without verifying that import flash_attn now works. This affects stale/partially broken flash-attn installs at the same wheel version, causing setup/runtime to stop trying even though the package remains unusable.
Useful? React with 👍 / 👎.
# Conflicts: # studio/backend/tests/test_training_worker_flash_attn.py # studio/backend/utils/wheel_utils.py # studio/install_python_stack.py # tests/python/test_flash_attn_install_python_stack.py
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: b1f7c724c4
ℹ️ About Codex in GitHub
Codex has been enabled to automatically 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 👍.
When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".
| if has_blackwell_gpu(): | ||
| _send_status( | ||
| event_queue, | ||
| "Skipping flash-linear-attention install: Blackwell GPU detected", | ||
| ) | ||
| return False |
There was a problem hiding this comment.
Respect the selected GPU before skipping FLA
When a mixed-GPU host has any Blackwell card but the worker is pinned via resolved_gpu_ids/CUDA_VISIBLE_DEVICES to a non-Blackwell NVIDIA GPU, this new gate still skips FLA because has_blackwell_gpu() queries nvidia-smi for all physical GPUs and does not filter by the process mask. That makes compatible runs on the selected non-Blackwell GPU miss the FLA fast path before the installer or version check can run.
Useful? React with 👍 / 👎.
|
Main has moved on since this branch: |
|
Closing this one. Checked the diff against current What is already on main:
What is obsolete:
One real gap survives and is worth its own PR. The runtime threshold change (32768 to 16384, and We are picking these up, so no action needed on your side. Thanks for the work on this. |
For anything prefer wheels over pypi