Skip to content

MoE grouped GEMM: share autotuners across identical cards; fix the TMA test on one-GPU hosts and macOS - #14490

Merged
danielhanchen merged 5 commits into
mainfrom
moe-grouped-gemm-r9-followup
Oct 21, 2026
Merged

danielhanchen merged 5 commits into
mainfrom
moe-grouped-gemm-r9-followup

Conversation

@danielhanchen

@danielhanchen danielhanchen commented Oct 21, 2026 •

Copy link
Copy Markdown
Member

Summary

Follow-up to #14358 from a post-merge regression review. Three problems, all fixed here.

What changes

  • Identical cards tune once again. MoE grouped GEMM: require sm90 on every visible card for TMA and launch on the operands' device #14358 gave every device index its own copy of each Triton autotuner with its own results cache, so a homogeneous multi-GPU host (device_map="auto" over identical cards) re-ran autotuning on every card. Each card still gets its own tuner object (Triton keeps per-run state such as nargs on it, and backward runs one autograd thread per device, so a shared object races), but copies on the same card model (name, compute capability, SM count) now share one results cache. Different models or MIG sizes stay separate, a single GPU still uses the kernel itself, and each copy follows a reassigned kernel.configs. Checked with the real Triton 3.6 autotuner: two concurrent cold-key calls on identical cards raise TypeError with one shared tuner and pass here.
  • test_launches_are_guarded_on_the_operands_device failed on any single-GPU CUDA host with CUDA error: invalid device ordinal: FakeTensorMode initialised a real cuda:1 context. The test now stubs torch._subclasses.fake_tensor.init_gpu_context, so no real context opens.
  • The same test file failed on Apple Silicon. unsloth_zoo installs a permissive triton stub on macOS arm64 + MLX, so deleting make_tensor_descriptor did not hide it. The module now skips when triton is that stub.

Not changed

One older visible card still turns TMA off process-wide (the flag is precomputed to avoid graph breaks, and the default autotune configs already disable TMA).

Tests

  • tests/test_grouped_gemm_mixed_tma_device.py: 18 passed; 2 fail against main's source.
  • On one real B200: main fails the guard test with invalid device ordinal, this branch passes all 18.

…ssigned configs; fix TMA test on one-GPU hosts and the macOS triton stub
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 21, 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 🔄 Running since 2026-10-21T17:00:20.379148Z 3c3a794 New commits
🔒 Security Review ⚠️ Failed 2026-10-21T11:07:03.885118Z
We couldn’t complete this review. Comment @codex security review to try again.
e1770a3 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.

@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Bravo.

Reviewed commit: e714a059a0

ℹ️ 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 added a commit that referenced this pull request Oct 21, 2026
## Problem

Five checks fail on main, so the `Repo tests (CPU, studio)`, `Repo tests
(CPU, rest)` and `workflow-trigger lint` shards are red on every open PR
(for example #14472, #14482, #14488, #14490 and #14494). All five
reproduce on main `4d7ebf5cea`.

| Test | Introduced by | Fault |
|---|---|---|
|
`tests/security/test_lint_risky_loaders.py::test_the_tree_matches_the_baseline`
| #14437 | Baseline. Two new dynamic-import sites: `import_module` in
`studio/backend/core/rag/embeddings.py`, and a third
`import_from_string` in `unsloth/models/sentence_transformer.py`
(`_require_word_tokenizer`). Both are safe, see below. |
|
`tests/studio/test_tauri_branding_contract.py::test_desktop_surfaces_do_not_restore_studio_branding`
| #14473 | Product. "Studio" became "Unsloth Studio" in the Markdown
import errors and the Tauri webview reject message. The desktop contract
allows "Unsloth" there, never "Unsloth Studio". "Unsloth" meets both
#14473's rule (no bare "Studio") and this one. |
|
`tests/studio/test_usage_examples_model_source_contract.py::test_auto_switch_section_sits_above_the_usage_examples`
| #14455 | Test. The redesign moved the usage examples above the monitor
link on purpose, and its own `api-settings-tab.test.ts` pins that. |
|
`tests/studio/test_usage_examples_model_source_contract.py::test_monitor_can_unload_the_loaded_model`
| #14455 | Test. Unloading moved from a standalone button on the page
into the model picker's eject (`ApiModelLoadControls`). The `/unload`
path itself is unchanged. |
|
`tests/studio/test_settings_compact_overflow_contract.py::test_api_monitor_entries_and_expanded_text_can_shrink`
| #14455 | Test. Entry rows are now rounded cards (no `border-b`), and
the prompt/reply box's `rounded-lg` became conditional, so the class
literals the test matched are gone. The overflow guards themselves are
still in the source. |

## Fix

- **Risky loaders:** two baseline entries with reasons.
- `embeddings.py` imports only the three `sentence_transformers.models`
paths written out in a literal tuple, to wrap their `import_from_string`
with the trust gate.
- `_require_word_tokenizer` imports only after its caller has refused
any `tokenizer_class` outside `sentence_transformers.*`, and only dotted
identifiers.
- **Branding:** "Unsloth Studio" to "Unsloth" in those seven Markdown
import errors and the Rust reject message.
`conversation-markdown-import.test.ts` now matches `/Unsloth Markdown/`.
- **#14455 contracts:** rewritten to guard what the redesign keeps:
- the auto-switch section still follows the monitor link, and the usage
examples come before both;
- the page passes `unloadActiveModel` to the load controls as
`onUnloadActive`, and eject is withheld while `busy` (`modelLoading ||
unloading`, #11223's rule);
  - the `/unload` call still reads the internal id from status;
  - entry rows keep `w-full min-w-0`;
  - the prompt/reply box keeps `max-h-72 overflow-auto break-words`;
  - the model id keeps `min-w-0 break-all`.

## Verification

- The four Python files: 78 passed (5 failed on main).
- `studio/frontend/tests/conversation-markdown-import.test.ts`: 20
passed.
- Sabotage, each run against the rewritten tests, each failing exactly
one test:
- removing `min-w-0` from the entry row, or `break-words` from the text
box: the overflow test fails;
- dropping `unloading` from `busy`, or passing `onEject` while busy: the
unload test fails;
- putting the auto-switch section above the monitor link: the order test
fails.
shimmyshimmer pushed a commit to shimmyshimmer/unsloth-staging-4 that referenced this pull request Oct 21, 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 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: a5d182d412

ℹ️ 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 tuner is None:
tuner = copy.copy(kernel)
tuner.cache = {}
tuner.cache = _MODEL_AUTOTUNE_CACHES.setdefault((id(kernel), _device_model(device)), {})

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 Serialize cold misses in the shared autotune cache

When identical devices encounter an unseen shape concurrently—as can happen in the per-device autograd threads mentioned here—each tuner can observe the shared cache key as absent before either inserts it. Both tuners then benchmark the complete configuration set, defeating the intended tune-once behavior, and the concurrent load can skew the timings and make the last writer's potentially suboptimal result win. The shared cache needs per-model/key single-flight synchronization rather than only a shared dictionary.

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 e671c69: copies on one card model now run under a per-model lock, so a second card that misses the same shape waits and then reads the first card's result. With the real Triton 3.6 autotuner and two concurrent identical cards: one shape benchmarks once (2 runs, was 4 on main), distinct shapes still tune once each, no errors. test_identical_cards_tune_a_cold_shape_once.

danielhanchen added a commit to danielhanchen/unsloth-staging-2 that referenced this pull request Oct 21, 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. Hooray!

Reviewed commit: e671c69a2a

ℹ️ 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 added a commit to danielhanchen/unsloth-staging-2 that referenced this pull request Oct 21, 2026
# Conflicts:
#	unsloth/kernels/moe/grouped_gemm/interface.py

[skip ci]
danielhanchen added a commit that referenced this pull request Oct 21, 2026
… again (#14544)

`pre-commit.ci` is red on every PR opened or pushed since #14490 merged
(#14510, #14515, #14543 so far), and it cannot push its autofix ("a
conflict occurred when applying fixes to the pr").

#14490 landed `_run_single_flight(*args, _run = run, _lock = lock,
**kwargs)` in `unsloth/kernels/moe/grouped_gemm/interface.py` on one
line, which is not what `ruff-format-with-kwargs` produces, and
pre-commit.ci runs the hook over all files. Running the hook over all
4525 tracked Python files on main changes only this one signature.

This PR is that formatter output and nothing else. It is a fixed point:
running the hook again leaves it unchanged.

## Testing

- `tests/test_formatter_fixed_point.py` on main: fails, naming
`unsloth/kernels/moe/grouped_gemm/interface.py`.
- Same test on this branch: 16 passed.
danielhanchen added a commit to danielhanchen/unsloth-staging-2 that referenced this pull request Oct 22, 2026
…ount once per card (unslothai#14605)

## Summary
Follow-up to unslothai#14490 from a post-merge review. unslothai#14490 wrapped every
grouped GEMM autotuner launch on a multi-GPU host in a per-GPU-model
lock, so identical cards would tune a new shape once. The lock was also
held on cache hits: every launch paid it (about 0.5 us more per call),
and a cold shape being tuned on one card blocked already-tuned launches
on an identical card until the benchmark finished.

## What changes
- **No lock.** Per-card tuner copies still share one results cache per
GPU model, so a shape tuned on one card is reused by every identical
card. A cached launch never waits on another card. Two identical cards
hitting the same cold shape at the same instant may both tune it, which
is what every multi-GPU host did before unslothai#14490.
- **SM count read once per card.** The forward, dX and dW launchers
called `torch.cuda.get_device_properties(...)` on every launch (three
per MoE layer per step); it is now cached per device index, since it
cannot change within a process.

Single-GPU hosts are unchanged (they never used the per-card copies).

## Tests
- `tests/test_grouped_gemm_mixed_tma_device.py`:
`test_identical_cards_launch_concurrently` (two cached launches on
identical cards must be inside `run` at once; main fails with
`BrokenBarrierError` because the lock serializes them) and
`test_sm_count_is_read_once_per_card` (fails on main). The shared-cache,
separate-model and configs-follow tests still pass.
- MoE CPU suites (11 files): main 225 passed / 48 skipped, this branch
226 / 48 (one new test), 0 failures on both.
- B200, base and head interleaved: forward, dX and dW with autotune off
and on give the identical output hash on every run.

---------

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
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