Skip to content

Studio ROCm: Windows wide-head SDPA guard and TunableOp tune hooks compile without a graph break - #18122

Merged
danielhanchen merged 4 commits into
mainfrom
studio-rocm-compile-fixes
Nov 17, 2026
Merged

danielhanchen merged 4 commits into
mainfrom
studio-rocm-compile-fixes

Conversation

@danielhanchen

Copy link
Copy Markdown
Member

Both changes remove a Python lock from code that a whole-model torch.compile traces. On main each one makes a fullgraph compile fail with Unsupported context manager, and a default compile drop the call to eager.

  1. Windows ROCm wide-head SDPA guard (Windows ROCm: run attention heads wider than 256 on SDPA math (Gemma 4 NaN) #17976). studio/backend/core/_rocm_windows_sdpa.py pins the math backend under a threading.Lock. Studio installs its guard before unsloth's, and unsloth's compile-safe guard (unsloth/import_fixes.py, _sdpa_math_only under allow_in_graph) sees the shared marker and keeps Studio's wrapper, so a compiled Gemma 4 global layer (head dim 512) on Windows ROCm reaches the lock. The pin now runs in an allow_in_graph helper keyed by a string token, as in unsloth.import_fixes.
  2. ROCm TunableOp tune hooks (Studio ROCm: use the measured fastest GEMM solution table for diffusion denoisers on gfx1151 #17299). With UNSLOTH_DIFFUSION_ROCM_TUNABLEOP=tune, forward hooks on the denoiser root hold an RLock and flip the process-wide tuning switch. The max tier compiles an SDXL U-Net whole (Module.compile, fullgraph), which traces those hooks and fails at the first forward, with no eager fallback. The hooks now return early under torch.compiler.is_compiling(), so compiled calls only read the tuning table and tuning stays with eager calls.

GPU results on gfx1151 (Radeon 8060S), base (main 62d66cb) vs this branch, same job and runner per fix

fix runner check base this branch
wide-head guard Windows 11, torch 2.11.0+rocm7.14.0 bf16 head-512 causal attention, fullgraph=True with eager / aot_eager / inductor all three fail: Unsupported context manager ... lock all three compile, no break reasons
wide-head guard same compiled vs guarded eager output n/a rel L2 0.0 / 0.0 / 2.6e-05; eager vs fp32 reference 0.00158 in both arms; no fused attention kernel ran; backend flags restored
tune hooks Linux, torch 2.11.0+rocm7.13.0 small bf16 GEMM module, hooks installed in tune mode, fullgraph=True with eager / aot_eager / inductor all three fail: Unsupported context manager ... RLock all three compile, rel L2 0.0 vs eager
tune hooks same fullgraph=False 3 unsupported, 23 of 26 frames compiled (module call dropped to eager) 0 unsupported, 3 of 3 frames
tune hooks same eager tuning still runs tuning switch on then off, results file written tuning switch on then off, TunableOp results 25 to 27, results file written (27 rows)

Tests

  • studio/backend/tests/test_rocm_windows_wide_head_sdpa.py: test_guarded_wide_head_compiles_fullgraph[aot_eager|inductor] compiles the real Studio wrapper with fullgraph=True; fails on main. The inductor case skips on Windows when MSVC (cl) is not on PATH, since Inductor's CPU backend needs it.
  • studio/backend/tests/test_diffusion_rocm_tunableop.py: new compile test; fails on main.
  • CPU (torch 2.14.1): base 2 failed / 9 passed and 1 failed / 19 passed; this branch 31 passed across both files.
  • gfx1151 runners: Windows suite base 9 passed, this branch 10 passed and the inductor case failed with Compiler: cl is not found (the skip above was added after that run); Linux TunableOp suite base 19 passed, this branch 20 passed.

Not covered

  • The wide-head guard ran on Windows only and the TunableOp hooks on Linux only; discrete RDNA / CDNA cards and multi-GPU hosts were not tested.
  • The full SDXL max-tier U-Net compile with tune mode was not run end to end; the check used a small module with the same hooks.

#17976 moved Studio's guard to a lock-held flag pin, which Dynamo cannot trace (Unsupported context manager
under fullgraph=True). Studio installs its guard before unsloth's, and unsloth's fix sees the shared marker
and keeps Studio's wrapper, so a compiled Gemma 4 global layer on Windows ROCm hit the lock. The pin now
runs in an allow_in_graph helper keyed by a string token, as in unsloth.import_fixes. The new test compiles
the real Studio wrapper with fullgraph=True and fails on main.
… is missing

Inductor's CPU backend needs cl on Windows; gfx1151 Windows runners have none, so the inductor
parametrization failed there for a toolchain reason. The aot_eager case still checks the guard traces.
UNSLOTH_DIFFUSION_ROCM_TUNABLEOP=tune installs forward hooks on the denoiser root. The max tier compiles an
SDXL U-Net whole (Module.compile, fullgraph), which traces those hooks; their RLock and process-global
tuning switch raise Unsupported context manager at the first forward, and the whole-module U-Net compile
has no eager fallback guard. The hooks now return early while compiling, so a compiled call only reads the
table and tuning stays with eager calls.
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 17, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-17T10:18:08.975755Z a561c19 Manual request
ℹ️ 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.

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Hooray!

Reviewed commit: a561c19cbd

ℹ️ 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 Nov 17, 2026
…17659) (#18125)

`tests/test_source_read_encoding.py::test_checked_in_file_reads_name_an_encoding`
fails on main in the CPU `rest-2` shard since #17659 (seen on the
staging run of #18122):

```
E   AssertionError: 1 file reads in the test trees touch a checked-in file with the platform default encoding, ...
E     + 'tests/studio/test_keep_awake_initialization.py:10: read_text()'
```

## Cause

`tests/studio/test_keep_awake_initialization.py` (added in #17659) reads
`studio/src-tauri/src/keep_awake.rs` with `read_text()`. Without an
encoding that uses the locale codepage on Windows (cp1252, cp932, ...),
so the test breaks there as soon as the Rust file gains a non-ASCII
byte. The lint is right; the new test missed it.

## Fix

`read_text(encoding = "utf-8")`.

## Verification

- main: the lint fails with the line above.
- this branch: `tests/test_source_read_encoding.py` 16 passed.
@danielhanchen
danielhanchen merged commit 74a17da into main Nov 17, 2026
@danielhanchen
danielhanchen deleted the studio-rocm-compile-fixes branch November 17, 2026 11:24
danielhanchen added a commit that referenced this pull request Nov 17, 2026
… of a 1e-6 atol (intermittent m-r failure) (#18153)

`studio/backend/tests/test_rocm_windows_wide_head_sdpa.py::test_guarded_wide_head_compiles_fullgraph[inductor]`
(added in #18122) fails intermittently in the Studio `m-r` shard on
unrelated PRs (seen on the staging run of #18075):

```
tests/test_rocm_windows_wide_head_sdpa.py:154: in test_guarded_wide_head_compiles_fullgraph
    assert torch.allclose(got, want, atol = 1e-6)
E   assert False
```

## Cause

The test compares the guarded SDPA call in eager (math backend) with the
same call compiled `fullgraph` by Inductor. Inductor lowers the math
SDPA to its own CPU kernel, so the two agree to fp32 rounding, not bit
for bit. On this host the gap on a 512-wide head is already up to 8.3e-7
(seeds 0 to 3), right at the fixed `atol = 1e-6`, and the inputs were
unseeded, so a draw on a different CPU (CI runners vary) can land just
over it. The guard is fine; the bound was too tight for two different
kernels.

## Fix

- Seeded inputs (`torch.Generator().manual_seed(0)`), so a failure is
reproducible.
- `torch.testing.assert_close(got, want)` with the fp32 defaults (atol
1e-5, rtol 1.3e-6) and a comment saying why it is not bitwise.

## Verification

- `tests/test_rocm_windows_wide_head_sdpa.py`: 11 passed, twice.
- Sabotage: comparing against a non-causal eager result makes both
`compiles_fullgraph` cases fail, so the check still catches a wrong
answer.
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