Repository navigation
Studio: restore ROCm TunableOp state when the last diffusion pipeline unloads - #17340
Conversation
… 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.
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. |
[skip ci]
|
@codex review |
There was a problem hiding this comment.
💡 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".
| with _LOCK: | ||
| _STATE["owners"].discard(token) | ||
| last = _STATE["active"] and not _STATE["owners"] | ||
| if last: | ||
| uninstall(pipe) |
There was a problem hiding this comment.
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 👍 / 👎.
There was a problem hiding this comment.
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.
|
@codex review |
There was a problem hiding this comment.
💡 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) |
There was a problem hiding this comment.
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 👍 / 👎.
There was a problem hiding this comment.
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).
…ts pipeline [skip ci]
|
@codex review |
|
Codex Review: Didn't find any major issues. Delightful! 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". |
Summary
Unloading a diffusion pipeline on ROCm (gfx1151) left
torch.cuda.tunableenabled 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,_activatecallstorch.cuda.tunable.enable(True)process-wide andinstall_for_pipereturns without registering anything that can be undone later. Image and video unload callrestore_backend_flags, butsnapshot_backend_flagsdoes not capture TunableOp. The onlyuninstallcallers are aspeed=offload 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:_activaterecordsis_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 callsuninstall, which now restores the recorded state rather than forcingFalse.diffusion_speed.release_rocm_tunableop(pipe): a best-effort helper. It does nothing if the module was never imported, which covers non-ROCm hosts.restore_backend_flagsin image_unload_locked, in the image pre-commit failure rollback and in video_teardown_state_locked.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.pyuses a faketorch.cuda.tunableand runs on CPU only:test_unload_restores_tunableop_only_after_the_last_pipelinetest_unload_restores_a_previously_enabled_statetest_user_owned_tunableop_is_left_alonetest_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_pipefollowed byrestore_backend_flags(the current unload path) leavesenabled=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_lockedor_teardown_state_locked(28 files): 2813 passed, 79 skipped, 1 failed. The failure istest_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.Not proven
torch.cuda.tunablestate change is only exercised through a fake._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 laterspeed=offload or the next unload.