Skip to content

[tunableop] dedicated compile-time benchmark worker and mode flag - #201767

Open
ruddhic wants to merge 64 commits into
gh/ruddhic/2/basefrom
gh/ruddhic/2/head
Open

ruddhic wants to merge 64 commits into
gh/ruddhic/2/basefrom
gh/ruddhic/2/head

Conversation

@ruddhic

@ruddhic ruddhic commented Oct 22, 2026 •

Copy link
Copy Markdown

Stack from ghstack (oldest at bottom):

Infrastructure shared by the two compile-time TunableOp passes that follow in this
stack. Nothing calls it yet.

cuda.autotune_tunableop_mode is a single tri-state config rather than a pair of
booleans
because the passes are mutually exclusive: "tune" builds the results file, "reuse"
consumes it. An enum makes that structural instead of an invariant every call site
has to re-assert. The names follow the vocabulary already used in
TuningResultsManager::Lookup, which distinguishes the reuse path from an actual
tuning pass. It is set by TORCHINDUCTOR_AUTOTUNE_TUNABLEOP_MODE, and
cuda.autotune_tunableop_timeout (TORCHINDUCTOR_AUTOTUNE_TUNABLEOP_TIMEOUT,
default 900s) bounds the wait per GEMM, since hipBLASLt tuning can hang. Both
share the prefix of the existing cuda.autotune_tunableop_dynamic_dims_wildcard.

Both passes benchmark GEMMs in a worker process rather than in the compiling
process, because hipBLASLt tuning can abort the process with an uncatchable fault
or hang indefinitely. Neither is survivable inline.

The worker is a dedicated TuningProcess, not Inductor's shared
TuningProcessPool. TunableOp's enable/tuning flags are process-global, so leaving
them set in a worker that also benchmarks triton candidates would tune unrelated
GEMMs, distort their timings and append their signatures to the results file.

It is also one worker per compile rather than one per GEMM. Process startup
dominates this work otherwise, and TuningProcess launches a bare interpreter over
a pipe instead of re-running the parent binary's bootstrap the way a
multiprocessing spawn does for every launch. A hang leaves the worker killed by
get() and a crash leaves it closed; either way the next submission restarts it,
and the restarted process re-reads the results file, so solutions recorded before
the fault survive.

submit reports faults as an Outcome status rather than raising, since a compile
has to continue even when a GEMM cannot be benchmarked.
@exported-using-ghexport

Differential Revision: D116493592

Differential Revision: D116493592

[ghstack-poisoned]
@pytorch-bot

pytorch-bot Bot commented Oct 22, 2026 •

Copy link
Copy Markdown

PR Status: Approved 🚀

Please fix all CI failures and trigger merge by commenting "@pytorchbot merge".

✅ AUTOMATED REVIEW - OUTDATED (earlier commit) - Ready for human review:

This PR adds the `cuda.autotune_tunableop_mode` and
`cuda.autotune_tunableop_timeout` configs and a dedicated, long-lived
`TuningProcess` worker (`torch/_inductor/tunableop.py`) that compile-time
TunableOp passes will use to benchmark GEMMs. Recommendation: Approve. Every
remaining point is non-blocking, and none of the earlier blocking findings is
still present.

Findings (7):

[minor] torch/_inductor/config.py:3071
Non-blocking: invalid timeout values make every GEMM fail and restart the
worker. `TORCHINDUCTOR_AUTOTUNE_TUNABLEOP_TIMEOUT` is parsed with a bare
`float()`, and `submit` passes it straight to `TuningProcess.get`
`selector.select(timeout)`. With `inf`, `EpollSelector.select` raises
`OverflowError` from `math.ceil(timeout * 1e3)` (NaN raises `ValueError`).
`get()` then kills the child, and `submit` returns "error" from the `except
Exception` branch at `tunableop.py:503`. With `0` or a negative value the wait
becomes a non-blocking poll, so every GEMM comes back "timeout". In each case
every GEMM pays a spawn plus a kill, and only the first fault of each kind
reaches WARNING. Fix: reject values that are not positive and finite, or map
`inf`/`<= 0` to `None` for an unbounded wait.

[minor] torch/_inductor/tunableop.py:258
Non-blocking: `_kill_and_reap` logs a spurious ERROR on every faulting GEMM.
When the job raised, or a generic exception or `BaseException` came through,
`worker.running` is still True. `process.kill()` sends SIGKILL. Then
`worker.kill()` checks `alive()`, sees `poll()` still returning None because the
child hasn't died yet, logs "Sending SIGKILL to autotune subprocess" at ERROR,
and signals again. Fix: call `process.kill()` directly only when `not
worker.running`, or follow it with `worker.close()` instead of `worker.kill()`.

[minor] test/inductor/test_tunableop_worker.py:719
Non-blocking: the real-worker teardown never runs.
`TunableOpRealWorkerTest.setUp` (719-721) and
`test_real_job_tunes_and_appends_to_shared_file` (865-867) register
`addCleanup(tunableop.shutdown_worker)` before the `setattr(tunableop,
"_worker"/"_worker_key", None)` cleanups. Cleanups run in reverse order, so
`_worker` is already None when `shutdown_worker` runs, and `_shutdown_locked`
does nothing. Each test ends on an "ok" submission, so a live child is left
behind with no sentinel and no bounded wait. It exits only after GC closes its
pipes, with a "subprocess is still running" ResourceWarning. In the GPU test the
child keeps its CUDA context past the end of the test. Fix: drop the two
`setattr` cleanups (`_shutdown_locked` already resets both globals), or register
`shutdown_worker` last.

[minor] test/inductor/test_tunableop_worker.py:839
Non-blocking: the round-trip test can pass without testing the conversion.
`TunableOpSettingsRoundTripTest` (839-845) sets the rotating buffer to 4 MB. On
a GPU whose L2 is exactly 4 MiB (e.g. T4), `_rotating_buffer_mb` returns -1
("use L2"). Setting -1 reads back as L2, which is also 4 MiB, so the assertion
passes and the bytesMB path never runs. A broken conversion would pass there
too. Fix: add `self.assertEqual(stamped, 4)`, or pick a size different from the
device's L2.

(3 more not shown, see the review run)

Reviewed commit: 61b524e (NOT the current head aff7b6b)

Review run

🐛 Report a bad review

🔗 Helpful Links

🧪 See artifacts and rendered test results at hud.pytorch.org/pr/201767

Note: Links to docs will display an error until the docs builds have been completed.

⏳ 3 Pending, 1 Unrelated Failure

As of commit aff7b6b with merge base 836c141 (image):

FLAKY - The following job failed but was likely due to flakiness present on trunk:

This comment was automatically generated by Dr. CI and updates every 15 minutes.

@linux-foundation-easycla

linux-foundation-easycla Bot commented Oct 22, 2026 •

Copy link
Copy Markdown

@pytorch-bot

pytorch-bot Bot commented Oct 22, 2026

Copy link
Copy Markdown

This PR needs a release notes: label

If your changes are user facing and intended to be a part of release notes, please use a label starting with release notes:.

If not, please add the topic: not user facing label.

To add a label, you can comment to pytorchbot, for example
@pytorchbot label "topic: not user facing"

For more information, see
https://github.com/pytorch/pytorch/wiki/PyTorch-AutoLabel-Bot#why-categorize-for-release-notes-and-how-does-it-work.

github-actions[bot]

This comment was marked as outdated.

[ghstack-poisoned]
ruddhic added a commit that referenced this pull request Oct 22, 2026
Pull Request resolved: #201767

Infrastructure shared by the two compile-time TunableOp passes that follow in this
stack. Nothing calls it yet.

`aten_tunableop_mode` is a single tri-state config rather than a pair of booleans
because the passes are mutually exclusive: "tune" builds the results file, "reuse"
consumes it. An enum makes that structural instead of an invariant every call site
has to re-assert. The names follow the vocabulary already used in
`TuningResultsManager::Lookup`, which distinguishes the reuse path from an actual
tuning pass.

Both passes benchmark GEMMs in a worker process rather than in the compiling
process, because hipBLASLt tuning can abort the process with an uncatchable fault
or hang indefinitely. Neither is survivable inline.

The worker is a dedicated `TuningProcess`, not Inductor's shared
`TuningProcessPool`. TunableOp's enable/tuning flags are process-global, so leaving
them set in a worker that also benchmarks triton candidates would tune unrelated
GEMMs, distort their timings and append their signatures to the results file.

It is also one worker per compile rather than one per GEMM. Process startup
dominates this work otherwise, and `TuningProcess` launches a bare interpreter over
a pipe instead of re-running the parent binary's bootstrap the way a
multiprocessing spawn does for every launch. A hang leaves the worker killed by
`get()` and a crash leaves it closed; either way the next submission restarts it,
and the restarted process re-reads the results file, so solutions recorded before
the fault survive.

`submit` reports faults as an `Outcome` status rather than raising, since a compile
has to continue even when a GEMM cannot be benchmarked.
ghstack-source-id: 454857423
@exported-using-ghexport

Differential Revision: [D116493592](https://our.internmc.facebook.com/intern/diff/D116493592/)
github-actions[bot]

This comment was marked as outdated.

[ghstack-poisoned]
github-actions[bot]

This comment was marked as outdated.

[ghstack-poisoned]
github-actions[bot]

This comment was marked as outdated.

[ghstack-poisoned]
github-actions[bot]

This comment was marked as outdated.

[ghstack-poisoned]
github-actions[bot]

This comment was marked as outdated.

[ghstack-poisoned]
github-actions[bot]

This comment was marked as outdated.

[ghstack-poisoned]
github-actions[bot]

This comment was marked as outdated.

[ghstack-poisoned]
github-actions[bot]

This comment was marked as outdated.

@github-actions
github-actions Bot dismissed stale reviews from themself October 30, 2026 16:22

Withdrawn: the automated review of d702e05 found nothing blocking.

[ghstack-poisoned]
github-actions[bot]

This comment was marked as outdated.

[ghstack-poisoned]
github-actions[bot]

This comment was marked as outdated.

[ghstack-poisoned]
github-actions[bot]

This comment was marked as outdated.

[ghstack-poisoned]
github-actions[bot]

This comment was marked as outdated.

[ghstack-poisoned]

@github-actions github-actions Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Automated review of 61b524e: nothing blocking; ready for human review.

This PR adds the cuda.autotune_tunableop_mode and cuda.autotune_tunableop_timeout configs and a dedicated, long-lived TuningProcess worker (torch/_inductor/tunableop.py) that compile-time TunableOp passes will use to benchmark GEMMs. Recommendation: Approve. Every remaining point is non-blocking, and none of the earlier blocking findings is still present.

⚪ 7 non-blocking findings

autotune_tunableop_timeout: float = float(

Non-blocking: invalid timeout values make every GEMM fail and restart the worker. TORCHINDUCTOR_AUTOTUNE_TUNABLEOP_TIMEOUT is parsed with a bare float(), and submit passes it straight to TuningProcess.get selector.select(timeout). With inf, EpollSelector.select raises OverflowError from math.ceil(timeout * 1e3) (NaN raises ValueError). get() then kills the child, and submit returns "error" from the except Exception branch at tunableop.py:503. With 0 or a negative value the wait becomes a non-blocking poll, so every GEMM comes back "timeout". In each case every GEMM pays a spawn plus a kill, and only the first fault of each kind reaches WARNING. Fix: reject values that are not positive and finite, or map inf/&lt;= 0 to None for an unbounded wait.


def _kill_and_reap(worker: TuningProcess) -> None:

Non-blocking: _kill_and_reap logs a spurious ERROR on every faulting GEMM. When the job raised, or a generic exception or BaseException came through, worker.running is still True. process.kill() sends SIGKILL. Then worker.kill() checks alive(), sees poll() still returning None because the child hasn't died yet, logs "Sending SIGKILL to autotune subprocess" at ERROR, and signals again. Fix: call process.kill() directly only when not worker.running, or follow it with worker.close() instead of worker.kill().


self.addCleanup(tunableop.shutdown_worker)

Non-blocking: the real-worker teardown never runs. TunableOpRealWorkerTest.setUp (719-721) and test_real_job_tunes_and_appends_to_shared_file (865-867) register addCleanup(tunableop.shutdown_worker) before the setattr(tunableop, "_worker"/"_worker_key", None) cleanups. Cleanups run in reverse order, so _worker is already None when shutdown_worker runs, and _shutdown_locked does nothing. Each test ends on an "ok" submission, so a live child is left behind with no sentinel and no bounded wait. It exits only after GC closes its pipes, with a "subprocess is still running" ResourceWarning. In the GPU test the child keeps its CUDA context past the end of the test. Fix: drop the two setattr cleanups (_shutdown_locked already resets both globals), or register shutdown_worker last.


tunable.set_rotating_buffer_size(4) # MB

Non-blocking: the round-trip test can pass without testing the conversion. TunableOpSettingsRoundTripTest (839-845) sets the rotating buffer to 4 MB. On a GPU whose L2 is exactly 4 MiB (e.g. T4), _rotating_buffer_mb returns -1 ("use L2"). Setting -1 reads back as L2, which is also 4 MiB, so the assertion passes and the bytesMB path never runs. A broken conversion would pass there too. Fix: add self.assertEqual(stamped, 4), or pick a size different from the device's L2.


def _submit(self, device=0, filename="t0.csv", job=None):

Non-blocking: the GENERIC classes (TunableOpWorkerTest, BenchmarkJobCallTest, TunableopModeValidationTest, TunableOpRealWorkerTest) break test/REVIEW.md Rule 3 ("No accelerator APIs: torch.cuda.*"). Besides the mock.patch("torch.cuda.") targets and the torch.device("cuda", N) metadata, some calls reach the real API. _submit/_submit_with_fault (94-99, 294-310) patch only current_device and get_filename, so snapshot_settings calls the real torch.cuda.tunable getters and torch.cuda.get_device_properties. On a GPU host, get_rotating_buffer_size goes through getCurrentDeviceProperties() in C++ and creates a CUDA context in the test process. test_valid_modes_do_not_warn (695-702) calls the real torch.cuda.is_available(), so on a GPU host its assertNoLogs depends on the PYTORCH_TUNABLEOP_* environment variables. Fix: patch snapshot_settings and is_available in the generic classes, or reclassify the classes that need real torch.cuda as CUDA.


def test_a_failure_before_the_worker_is_also_reported(self):

Non-blocking, carried over from the last review: duplicate test. test_a_failure_before_the_worker_is_also_reported (337) is a subset of test_setup_failure_does_not_restart_worker (377). Both patch current_device to raise RuntimeError("no context") and assert "error", and 377 also checks that the existing worker survives. Fix: move the assertIsNone(outcome.time) into 377 and delete 337.


autotune_tunableop_timeout: float = float(

Non-blocking: the timeout config has no unit in its name. cuda.autotune_tunableop_timeout doesn't follow the _seconds suffix of the neighbouring autotune timeouts (max_autotune_subproc_result_timeout_seconds, precompilation_timeout_seconds). Renaming it and its env var costs nothing now but would be a BC change later.

This review is AI-generated and advisory. Each new automated review replaces the previous one. Report a bad review

@pytorch-bot pytorch-bot Bot added ready for review Automated review passed, ready for a final review by a maintainer. and removed in progress PR is being worked on by the author, until the automated review accepts it labels Nov 6, 2026
[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
@mlazos
mlazos self-requested a review December 1, 2026 19:37
@pytorch-bot pytorch-bot Bot added the ciflow/trunk Trigger trunk jobs on your pull request label Dec 2, 2026

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ciflow/inductor ciflow/torchtitan Run TorchTitan integration tests ciflow/trunk Trigger trunk jobs on your pull request meta-exported module: inductor ready for review Automated review passed, ready for a final review by a maintainer.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants