Skip to content

fix(fla): disable intracard CP for single-process launches - #10850

Closed
Souravrajvi0 wants to merge 3 commits into
unslothai:mainfrom
Souravrajvi0:feat/fla-dispatch-env-f148
Closed

Souravrajvi0 wants to merge 3 commits into
unslothai:mainfrom
Souravrajvi0:feat/fla-dispatch-env-f148

Conversation

@Souravrajvi0

@Souravrajvi0 Souravrajvi0 commented Sep 13, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Single-process Unsloth training (Qwen3.5 / GatedDeltaNet / FLA) should not route through the intra-card context-parallel FLA backend. This PR sets FLA_INTRACARD_CP=0 before FLA is imported when the launcher advertises one data-parallel rank.

Does not set FLA_DISABLE_BACKEND_DISPATCH, so TileLang backend dispatch on sm90+ Hopper stays enabled (per review on #10850).

World size

Uses torch-free unsloth/launcher_world_size.py (same rules as Studio dataset_bounds.world_size_from_env): WORLD_SIZE, MPI vars (OMPI_COMM_WORLD_SIZE, PMI_SIZE, MV2_COMM_WORLD_SIZE, …), and MLX rank files. Invalid values (e.g. auto) fall back to 1 without breaking import unsloth.

Test plan

  • pytest tests/test_launcher_world_size.py

Notes

Unrelated Studio llama-server logging changes were removed from this branch; they can land in a separate PR.

@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: 51fc013acc

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

Comment thread unsloth/__init__.py Outdated
Comment on lines +16 to +17
if int(os.environ.get("WORLD_SIZE", "1") or 1) <= 1:
os.environ.setdefault("FLA_DISABLE_BACKEND_DISPATCH", "1")

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 Preserve dispatch for MPI-launched multi-rank jobs

When a multi-rank job is launched through Open MPI, Intel/MPICH, or MVAPICH, the rank count is exposed through variables such as OMPI_COMM_WORLD_SIZE, PMI_SIZE, or MV2_COMM_WORLD_SIZE, while WORLD_SIZE remains unset; the repository explicitly documents these launch modes in studio/backend/core/training/dataset_bounds.py:30-38. Every rank therefore takes this branch and disables FLA backend dispatch, preventing an FLA context-parallel operation from selecting its distributed backend and causing it to run through the non-CP path. Determine single-process status using all supported launcher variables rather than WORLD_SIZE alone.

Useful? React with 👍 / 👎.

Comment thread unsloth/__init__.py Outdated

# fla backend dispatch walks registered backends on every hot kernel call; the CP
# backend rejects single-process training. Set before fla is imported (Qwen3.5 / Next).
if int(os.environ.get("WORLD_SIZE", "1") or 1) <= 1:

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 Parse WORLD_SIZE without aborting package import

If WORLD_SIZE contains a non-integer value such as auto or another stale launcher setting, this top-level int() raises ValueError before any of Unsloth's import setup runs, so even workflows that do not use FLA can no longer import the package. Other world-size handling in the repository treats unusable values as single-process defaults; this import-time check should likewise fall back safely instead of making an optional optimization fatal.

Useful? React with 👍 / 👎.

@danielhanchen

Copy link
Copy Markdown
Member

Setting FLA_DISABLE_BACKEND_DISPATCH process-wide in unsloth/init.py short-circuits the whole dispatch decorator, including the TileLang backend fla turns on by default on sm90+ as a hardware workaround, so could you scope it to the context-parallel backend and confirm a single-GPU Hopper run is unchanged? The branch also carries the Studio llama-server logging and debug log source changes, which are unrelated to the title and would be much easier to review as their own PR.

@Souravrajvi0

Copy link
Copy Markdown
Contributor Author

@danielhanchen Addressed the review on the latest push:

Scope: Removed FLA_DISABLE_BACKEND_DISPATCH (and dropped the unrelated Studio llama-server logging commits from this branch — those can go in a separate PR). Single-process launches now only setdefault("FLA_INTRACARD_CP", "0") before FLA import, so TileLang backend dispatch on sm90+ is unchanged.

World size: Uses new torch-free unsloth/launcher_world_size.py (same rules as dataset_bounds.world_size_from_env: OMPI_COMM_WORLD_SIZE, PMI_SIZE, MV2_COMM_WORLD_SIZE, etc.; invalid values like auto fall back to 1 without breaking import unsloth).

Tests: tests/test_launcher_world_size.py

Happy to run a single-GPU Hopper sanity check if you want a specific model/script pinned in CI.

@Souravrajvi0
Souravrajvi0 force-pushed the feat/fla-dispatch-env-f148 branch from 943156b to a40d3dd Compare September 13, 2026 15:23
@Souravrajvi0 Souravrajvi0 changed the title fix(fla): disable backend dispatch for single-process training fix(fla): disable intracard CP for single-process launches Sep 13, 2026
@danielhanchen

Copy link
Copy Markdown
Member

Thanks for narrowing this to FLA_INTRACARD_CP and dropping the Studio logging commits, that addresses my earlier point; note that unsloth_zoo already sets FLA_INTRACARD_CP=0 and forces IntraCardCPBackend.is_available to False around the vendored fla injection in temporary_patches/fla_vendor.py, so the remaining gap is a separately installed newer fla. Could you confirm that case is what you hit, and if so whether an 81 line launcher world size module with MLX rank file parsing is needed here rather than a plain single process check?

Drop FLA_DISABLE_BACKEND_DISPATCH so TileLang backend dispatch stays
enabled on Hopper. Use launcher_world_size_from_env (MPI + torchrun vars,
safe int parsing) and set FLA_INTRACARD_CP=0 before FLA import when the
advertised rank count is 1.

Removes unrelated Studio llama-server logging commits from this branch.
@Souravrajvi0

Copy link
Copy Markdown
Contributor Author

@danielhanchen — Yes: the failure mode was a separately installed flash-linear-attention (not the unsloth_zoo vendored copy). temporary_patches/fla_vendor.py only forces FLA_INTRACARD_CP=0 around the injected module; if FLA is already on sys.path from pip, import-time backend selection runs before that patch.

World size vs plain single-process: WORLD_SIZE alone is not enough — MPI / MPICH / MVAPICH jobs often expose OMPI_COMM_WORLD_SIZE, PMI_SIZE, MV2_COMM_WORLD_SIZE while WORLD_SIZE stays unset (dataset_bounds.py documents the same set). Using only WORLD_SIZE==1 would disable intracard CP on every rank of a multi-rank MPI launch.

That said, the 81-line MLX rank-file parser was more than this import hook needs. Latest push drops rank-file parsing and keeps the shared env-var max (~40 lines), with a note that FLA training is torch/GPU and multi-rank launchers should already export one of those vars.

Still only setdefault("FLA_INTRACARD_CP", "0") when world_size_from_env() <= 1 — TileLang dispatch unchanged on single-GPU Hopper.

@Souravrajvi0
Souravrajvi0 force-pushed the feat/fla-dispatch-env-f148 branch from dfffa31 to bb06b8e Compare September 17, 2026 04:29
@danielhanchen danielhanchen self-assigned this Oct 1, 2026
@danielhanchen

Copy link
Copy Markdown
Member

Thank you for the PR and for narrowing it after the earlier review. After checking against upstream flash-linear-attention and unsloth-zoo, I am going to close this one, since FLA_INTRACARD_CP=0 is already the effective state for every fla that Unsloth can load:

  1. IntraCardCPBackend has default_enable = False upstream since fla-org/flash-linear-attention@8d6f019 ("Disable intracard cp by default"), which is in every release from v0.5.0 through current main (0.6.0). With the variable unset, IntraCardCPBackend.is_enabled() and can_use() both return False, so setdefault("FLA_INTRACARD_CP", "0") does not change behaviour.
  2. Even when enabled, the backend's verifier only accepts calls under torch.inference_mode() with varlen cu_seqlens, so it is never selected during training.
  3. Intra-card CP is a single-GPU prefill optimisation (it splits long sequences across SMs on one card). It is not related to the number of launcher processes, so gating it on world size does not match what the flag controls.
  4. unsloth-zoo only defers to a separately installed fla when it is newer than the vendored 0.5.1, and those versions already have the backend off by default. Older installs (the short window around v0.4.2 where it defaulted on) are replaced by the vendored tree, which sets FLA_INTRACARD_CP=0 and marks the backend unavailable.

If you do have a reproduction (fla version, script, and the failure or wrong output you saw), please open an issue with it and we can take another look.

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.

2 participants