Skip to content

Studio: restore ROCm TunableOp state when the last diffusion pipeline unloads - #17340

Merged
danielhanchen merged 4 commits into
mainfrom
studio-tunableop-teardown-restore
Nov 10, 2026
Merged

danielhanchen merged 4 commits into
mainfrom
studio-tunableop-teardown-restore

Conversation

@danielhanchen

Copy link
Copy Markdown
Member

Summary

Unloading a diffusion pipeline on ROCm (gfx1151) left torch.cuda.tunable enabled for the rest of the process. Later GEMMs outside diffusion kept consulting the diffusion solution table. This change restores the TunableOp state once the last pipeline that enabled it unloads.

Root cause

#17299 added diffusion_rocm_tunableop.py. In the default lookup mode, _activate calls torch.cuda.tunable.enable(True) process-wide and install_for_pipe returns without registering anything that can be undone later. Image and video unload call restore_backend_flags, but snapshot_backend_flags does not capture TunableOp. The only uninstall callers are a speed=off load and an ineligible-arch load, and neither runs on unload. So after a normal load and unload, TunableOp stays enabled and active.

Fix

  • diffusion_rocm_tunableop: _activate records is_enabled() before enabling TunableOp. Each pipe that activates it gets an owner tag, set on the pipe so the second-expert and named-DiT views share it. release(pipe) drops that claim. The last release calls uninstall, which now restores the recorded state rather than forcing False.
  • diffusion_speed.release_rocm_tunableop(pipe): a best-effort helper. It does nothing if the module was never imported, which covers non-ROCm hosts.
  • The helper is called after restore_backend_flags in image _unload_locked, in the image pre-commit failure rollback and in video _teardown_state_locked.
  • Image and video pipelines can be loaded at the same time. Unloading one keeps TunableOp on while the other still holds a claim.
  • If the user sets PYTORCH_TUNABLEOP_* themselves, Studio never activates TunableOp or claims it, so their setting is left alone.

Flip test

studio/backend/tests/test_diffusion_rocm_tunableop_teardown.py uses a fake torch.cuda.tunable and runs on CPU only:

  • test_unload_restores_tunableop_only_after_the_last_pipeline
  • test_unload_restores_a_previously_enabled_state
  • test_user_owned_tunableop_is_left_alone
  • test_image_and_video_teardown_release_tunableop (AST check that both teardown methods call the release)

On origin/main (ec6d160), all 4 fail. A probe on main also shows install_for_pipe followed by restore_backend_flags (the current unload path) leaves enabled=True active=True. With the fix, all 4 pass.

Tests run

  • -o addopts= -m "not server and not native_mxc" over every test file that mentions tunableop, backend_flags, _unload_locked or _teardown_state_locked (28 files): 2813 passed, 79 skipped, 1 failed. The failure is test_diffusion_zimage_fused.py::test_model_unload_restores_the_process_global_patches. It also fails on origin/main, so it predates this change.
  • tests/test_refactor_guard.py: 12 passed.
  • The pre-push gate (stray files, AGPL header, ruff): pass.

Not proven

  • Not run on real ROCm hardware. The real torch.cuda.tunable state change is only exercised through a fake.
  • The video pre-commit rollback (_rollback_precommit_globals) does not have access to the pipe, so it does not release. A failed video load that already activated TunableOp still leaves it enabled until a later speed=off load or the next unload.
  • Other TunableOp settings (filename, max tuning duration and iterations) are not restored. They have no effect while TunableOp is disabled.

… unloads

Default lookup mode enabled torch.cuda.tunable process-wide and no unload path
undid it. Each pipeline that activates TunableOp now claims it; image and video
teardown (and the image pre-commit rollback) release the claim, and the last
release restores the state seen before activation. User-owned PYTORCH_TUNABLEOP_*
settings are never touched.
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 10, 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-10T04:46:43.062800Z 5f32097 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.

@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: 0d704ea040

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

Comment on lines +363 to +367
with _LOCK:
_STATE["owners"].discard(token)
last = _STATE["active"] and not _STATE["owners"]
if last:
uninstall(pipe)

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 Make the last-owner check and TunableOp restoration atomic

When an image unload overlaps a video load (or vice versa), another pipeline can activate and claim TunableOp after release() determines that it is the last owner but before uninstall() reacquires _LOCK. The pending uninstall then disables TunableOp and clears the new pipeline's claim, even though that pipeline successfully installed it. The image and video backends have separate lifecycle locks, so they do not prevent this interleaving. A controlled threaded reproduction leaves the newly installed pipeline with enabled=False, active=False, and no registered owners. Keep the last-owner decision and restoration in one critical section, and coordinate activation with owner registration under the same lock.

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 2e7bb37ecc: the lock is now reentrant, activation and the owner claim run in one critical section, and release() makes the last-owner decision and restores the TunableOp state inside the same one, so a concurrent load either claims before the check or activates after the restore. Added test_a_load_racing_the_last_release_keeps_its_claim (fails on the previous head) and test_install_waits_for_a_release_in_progress.

@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: d2a3f86078

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

): # activation and the claim are one step, so a concurrent last release cannot undo them
if not _activate(why, logger):
return False
_claim(pipe)

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 Release owner claims when a video load fails before publication

If a ROCm video load fails or is cancelled after the speed layer runs, this claim survives permanently: video.py explicitly deletes the unpublished pipe on cancellation, while _rollback_precommit_globals() restores backend flags without releasing TunableOp. Because owners retains a standalone token rather than a weak reference, collecting the failed pipeline cannot remove its claim. Subsequent successful image/video loads and unloads therefore never reach the last-owner restoration, leaving TunableOp enabled indefinitely. Release the unpublished pipeline's claim on video failure and cancellation, as the image rollback now does.

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 f355b83439: each claim registers a weakref.finalize on the underlying pipeline (views are unwrapped to the pipe they proxy), so a failed or cancelled load that drops its unpublished pipe ends the claim when the pipe is collected, and the last claim out restores the pre-activation TunableOp state. Explicit release() is unchanged and the finalizer is a no-op after it. Added test_a_failed_load_that_drops_its_pipe_releases_the_claim (fails on the previous head).

@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

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

Reviewed commit: 5f32097bdc

ℹ️ 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
danielhanchen merged commit e5aeca4 into main Nov 10, 2026
@danielhanchen
danielhanchen deleted the studio-tunableop-teardown-restore branch November 10, 2026 05:34
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