Repository navigation
Studio diffusion: HiDream experts run only on their routed tokens - #18793
Conversation
…ouped GEMMs, no host sync) on bf16 CUDA denoisers
|
@codex review |
Codex Review Summary: ✅ CompletedThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
|
Codex Review: Didn't find any major issues. More of your lovely PRs please. Reviewed commit: ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
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". |
…m runs on the denoiser's card [skip ci]
|
@codex review |
|
Codex Review: Didn't find any major issues. Breezy! Reviewed commit: ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
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". |
…DMD diffusers skip, risky loader lint) (#18824) ## Summary Four checks are red on `main` itself, so every open PR currently inherits them in the Backend CI shards and the risky loader lint. Each was reproduced on a clean `main` checkout before fixing. | Failing check | Introduced by | Which side was wrong | Fix | |---|---|---|---| | `tests/test_diffusion_rocm_tag_detect.py::test_no_diffusion_or_video_gate_reads_version_hip_directly` | #18793 (`diffusion_regional_compile.py`) and #18792 (`diffusion_vae_strips.py`) | Code. Both new gates read `torch.version.hip` directly, which the ROCm tag guard exists to forbid (a CUDA wheel with a stray `hip` attribute, or a ROCm wheel reporting it differently, takes the wrong branch). | Route both through the shared `is_rocm_torch(torch)` helper from `rocm_bf16`. Behaviour on CUDA and ROCm is unchanged. | | `tests/test_h3_quantised_offload.py::test_a_streamed_torchao_render_runs_under_no_grad_and_the_rest_keep_inference_mode` | #18780 added `draft_ctx` to the denoise `with` | Test. The invariant (the denoise loop enters `grad_ctx` together with its progress context) still holds; the test pinned one exact spelling of the `with` line. | Parse the `with` statements and assert `grad_ctx` and `progress_ctx()` are entered together, regardless of what other contexts join them. Verified it still fails when `grad_ctx` is dropped from the statement. | | `tests/test_video_dmd.py::test_dmd_scheduler_reseeds_per_render`, `::test_renoise_stream_is_independent_of_the_initial_latent_noise` | #18779 | Test. `make_dmd_scheduler` builds on a diffusers scheduler, but these two tests only skipped on missing torch, so they error with `No module named 'diffusers'` in the shard that runs without diffusers. | `pytest.importorskip("diffusers")`, matching the existing scheduler test in the same file. | | `scripts/lint_risky_loaders.py` (`diffusion_ddp.py:440 [dynamic-import] __import__`) | #18771 | Code. The rank bootstrap imported two fixed modules through `__import__` in a loop. | Import `utils.native_tls` and `utils.happy_eyeballs` by name, each in its own `try`, so a failure in one still does not stop the rank or skip the other. No baseline entry needed. | ## Testing - `tests/test_diffusion_rocm_tag_detect.py`, `tests/test_h3_quantised_offload.py`, `tests/test_video_dmd.py`, `tests/test_diffusion_ddp.py`, `tests/test_diffusion_training.py`, the native TLS and happy eyeballs suites, and the regional compile and VAE strip suites: all pass. - `tests/test_video_dmd.py` with diffusers blocked from import: 26 passed, 7 skipped (previously 2 errors). - `python scripts/lint_risky_loaders.py`: all call sites recorded. - Sabotage: restoring either original `torch.version.hip` read makes the ROCm tag guard fail; restoring the `__import__` loop makes the risky loader lint fail.
HiDream's MoE layers route each token to 2 of 4 experts. Studio's traceable replacement for diffusers'
moe_infer(_moe_infer_dense, which avoids thebincount().cpu()host sync that broke every block's graph) runs every expert on every token and masks the result. So half of the routed-expert GEMMs do no useful work.Change
diffusion_regional_compile._moe_infer_densetakes a grouped path first when the denoiser is bf16 on an sm80+ NVIDIA card and every expert is a stock bias-free SwiGLU of plainnn.Linear/nn.Parameter, with no hooks.torch._grouped_mmcalls (gate, up, down) run each expert only on its own rows.bincount, so there is no graph break. On sm90 / sm100 the grouped GEMM is one fused kernel with no host sync. On other cards torch 2.9+ runs its per-group fallback, which copies the offsets to the host per call; that path is not CUDA-graph capturable, and the denoiser graph's warm-up then runs eager (HiDream's graph did not capture on main either).torch._grouped_mmon the denoiser's card at install and keeps the dense experts if it raises (torch 2.8 ships the op but raises off sm90)._grouped_mmkeep the dense path.UNSLOTH_DIFFUSION_HIDREAM_GROUPED_MOE=0turns it off.Results
One layer at HiDream-I1 dims (dim 2560, expert hidden 6912, 4 experts, top 2, 4224 tokens, random bf16 weights). Inductor,
fullgraph=True, 0 graph breaks on both arms, median of 5 interleaved rounds:Accuracy is rel L2 1.9-2.6e-3 against dense. That is GEMM reduction order; the released diffusers loop is 4.5e-3 from dense.
Tests
tests/test_diffusion_hidream_grouped_moe.py(7, CPU,torch._grouped_mmCPU kernel):tests/test_diffusion_regional_compile.pyis unchanged and passes.Not proven here
Real render
HiDream-I1-Fast, 1024x1024, 16 steps, RTX PRO 6000, torch 2.14.1, bf16 resident and compiled, interleaved in one process: 0.372-0.380 -> 0.288-0.289 s/step, 6.08-6.13 -> 4.68-4.69 s/image (1.30x); the grouped path engaged on 48 of 48 MoE layers; peak 57.63 -> 57.60 GiB. A separate merge-base run on another VM gave 0.372-0.374 s/step, matching the dense arm. HiDream renders are not reproducible run to run here even at a fixed seed (same-arm LPIPS 0.34-0.57), so image metrics cannot separate the arms; grouped vs dense measured 0.40 / 0.57 with matching composition, and the per-layer relative L2 is 1.4e-3.