Skip to content

[dynamo] Preserve Inductor deterministic config during tracing - #198572

Closed
AnkitNakhawa wants to merge 1 commit into
pytorch:mainfrom
AnkitNakhawa:fix-inductor-deterministic-state
Closed

AnkitNakhawa wants to merge 1 commit into
pytorch:mainfrom
AnkitNakhawa:fix-inductor-deterministic-state

Conversation

@AnkitNakhawa

@AnkitNakhawa AnkitNakhawa commented Sep 25, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #198563

Dynamo restores the deterministic-algorithms global state after tracing by calling the public torch.use_deterministic_algorithms API. That API also updates torch._inductor.config.deterministic, so the cleanup unintentionally overwrites an independently configured Inductor value, including TORCHINDUCTOR_DETERMINISTIC=1.

Restore the C++ global flag directly during Dynamo cleanup. This retains the saved mode and warn_only state without changing the separate Inductor config.

The regression test uses the eager backend and CPU tensors, so it does not require CUDA.

Test plan:

  • python test/dynamo/test_misc.py MiscTests.test_deterministic_algorithms_mutated MiscTests.test_inductor_deterministic_preserved MiscTests.test_deterministic_algorithms_warn_only_mutated MiscTests.test_deterministic_algorithms_warn_only_compile_time_value
  • repository RUFF and PYFMT adapters on the changed files
  • exact TORCHINDUCTOR_DETERMINISTIC=1 reproduction against the 2026-09-24 CPU nightly

cc @voznesenskym @penguinwu @EikanWang @jgong5 @Guobing-Chen @XiaobingSuper @zhuhaozhe @blzheng @wenzhe-nrv @jiayisunx @kadeng @chauhang @amjames @jataylo @azahed98

@pytorch-bot

pytorch-bot Bot commented Sep 25, 2026 •

Copy link
Copy Markdown

PR Status: Approved 🚀

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

🔗 Helpful Links

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

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

✅ No Failures

As of commit a64cc92 with merge base 3fd0a85 (image):
💚 Looks good so far! There are no failures yet. 💚

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

@pytorch-bot pytorch-bot Bot added ciflow/inductor ciflow/torchtitan Run TorchTitan integration tests module: dynamo labels Sep 25, 2026
@pytorch-bot

pytorch-bot Bot commented Sep 25, 2026 •

Copy link
Copy Markdown

Workflows were awaiting approval. CI has now been triggered for the ciflow labels on this PR.

@pytorch-bot

pytorch-bot Bot commented Sep 25, 2026

Copy link
Copy Markdown

The following ciflow label(s) have been added but CI has not been triggered yet because the workflows are awaiting approval:

  • ciflow/inductor
  • ciflow/torchtitan

Once a maintainer approves the workflows (scroll to the bottom of the PR page), the corresponding CI jobs will be triggered automatically. Please ping one of the reviewers if you do not have access to approve and run workflows.

@soulitzer soulitzer added the triaged This issue has been looked at a team member, and triaged and prioritized into an appropriate module label Sep 25, 2026
@jansel

jansel commented Sep 28, 2026

Copy link
Copy Markdown
Contributor

@claude review these changes

@claude

claude Bot commented Sep 28, 2026 •

Copy link
Copy Markdown

Claude finished @jansel's task in 57s —— View job


The fix is correct and I'd approve it; my only request is a small cleanup in the new test.

PR Review: #198572

Summary

Dynamo's preserve_global_state cleanup now restores the deterministic flag with torch._C._set_deterministic_algorithms instead of the public torch.use_deterministic_algorithms. That stops tracing from overwriting torch._inductor.config.deterministic (for example, a value set by TORCHINDUCTOR_DETERMINISTIC=1). This matches how Dynamo already handles a user's torch.use_deterministic_algorithms call inside a traced region (torch/_dynamo/variables/torch.py:1484-1488), which also sets only the C++ flag. Nothing else under torch/_dynamo, torch/_inductor or torch/_functorch calls the public API as part of save/restore. The only problem I found is a style issue in the test.

Testing

  • test/dynamo/test_misc.py:12416-12426: the test saves and restores torch._inductor.config.deterministic by hand in finally, even though the with torch._inductor.config.patch(deterministic=True) block already restores it on exit. The repo guidance is to use config.patch instead of manual save/restore. Suggested change: decorate the test with @torch._inductor.config.patch(deterministic=True) and drop both prior_inductor_deterministic and the last finally line. The use_deterministic_algorithms save/restore should stay, because it matches the neighbouring tests. Unlike the manual restore, the decorator also restores the config if an assert fails.
  • The test compiles once and checks the state after cleanup, which is enough to catch the regression. Before this fix, the assert assertTrue(torch._inductor.config.deterministic) fails because cleanup resets the config to False.

Recommendation

Approve, after the small test cleanup above. The one-line core change is correct and consistent with the existing handler for traced use_deterministic_algorithms calls.

I did not run the tests locally; this review is based on reading the code.

@seongyun1104

seongyun1104 commented Oct 28, 2026 •

Copy link
Copy Markdown

I also checked the aot_compile path on CPU. In this case, the first graph is affected: lowering happens after Dynamo restores global state, so TORCHINDUCTOR_DETERMINISTIC=1 alone does not keep Inductor's deterministic setting enabled through lowering.

Applying this PR's restoration change at 2cdc76a8a2 locally to torch 2.13.0 fixes that path too.

With TORCHINDUCTOR_DETERMINISTIC=1, recording config.deterministic on entry to GraphLowering.__init__, and using a fresh process for each path:

Version torch.compile: lowering / after call aot_compile: lowering / after call
torch 2.13.0 [True] / False [False] / False
torch 2.13.0 + this PR's restoration change [True] / True [True] / True

This checks configuration propagation with a small CPU function only. I did not test GPU execution or loading a saved AOT artifact.

Minimal reproduction for the AOT path

Run in a fresh process:

TORCHINDUCTOR_DETERMINISTIC=1 python repro.py
import torch
import torch._inductor.config as config
import torch._inductor.graph as graph

seen = []
original_init = graph.GraphLowering.__init__


def record_init(self, *args, **kwargs):
    seen.append(config.deterministic)
    original_init(self, *args, **kwargs)


graph.GraphLowering.__init__ = record_init


def f(x):
    return (
        x * torch.rsqrt(x.pow(2).mean(-1, keepdim=True) + 1e-6)
    ).sum()


torch.compile(f, fullgraph=True).aot_compile(
    ((torch.randn(8, 64),), {})
)
print(seen, config.deterministic)

Before the change:

[False] False

With the change applied:

[True] True

@williamwen42

Copy link
Copy Markdown
Member

@pytorchbot merge

@pytorch-bot pytorch-bot Bot added the ciflow/trunk Trigger trunk jobs on your pull request label Oct 30, 2026
@pytorchmergebot

Copy link
Copy Markdown
Collaborator

Merge failed

Reason: 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.

Details for Dev Infra team

Raised by workflow job

@williamwen42

Copy link
Copy Markdown
Member

@pytorchbot rebase

@pytorchmergebot

Copy link
Copy Markdown
Collaborator

@pytorchbot started a rebase job onto refs/remotes/origin/viable/strict. Check the current status here

@pytorchmergebot

Copy link
Copy Markdown
Collaborator

Successfully rebased fix-inductor-deterministic-state onto refs/remotes/origin/viable/strict, please pull locally before adding more changes (for example, via git checkout fix-inductor-deterministic-state && git pull --rebase)

@pytorchmergebot
pytorchmergebot force-pushed the fix-inductor-deterministic-state branch from 2cdc76a to a64cc92 Compare October 30, 2026 18:52
@williamwen42

Copy link
Copy Markdown
Member

@pytorchbot merge

@pytorchmergebot

Copy link
Copy Markdown
Collaborator

Merge started

Your change will be merged once all checks pass (ETA 0-4 Hours).

Learn more about merging in the wiki.

Questions? Feedback? Please reach out to the PyTorch DevX Team

Advanced Debugging
Check the merge workflow status
here

dominikjastrzebskix pushed a commit to dominikjastrzebskix/pytorch that referenced this pull request Nov 9, 2026
…ch#198572)

Fixes pytorch#198563

Dynamo restores the deterministic-algorithms global state after tracing by calling the public `torch.use_deterministic_algorithms` API. That API also updates `torch._inductor.config.deterministic`, so the cleanup unintentionally overwrites an independently configured Inductor value, including `TORCHINDUCTOR_DETERMINISTIC=1`.

Restore the C++ global flag directly during Dynamo cleanup. This retains the saved `mode` and `warn_only` state without changing the separate Inductor config.

The regression test uses the eager backend and CPU tensors, so it does not require CUDA.

Test plan:

- `python test/dynamo/test_misc.py MiscTests.test_deterministic_algorithms_mutated MiscTests.test_inductor_deterministic_preserved MiscTests.test_deterministic_algorithms_warn_only_mutated MiscTests.test_deterministic_algorithms_warn_only_compile_time_value`
- repository RUFF and PYFMT adapters on the changed files
- exact `TORCHINDUCTOR_DETERMINISTIC=1` reproduction against the 2026-09-24 CPU nightly

Pull Request resolved: pytorch#198572
Approved by: https://github.com/williamwen42
pytorchmergebot pushed a commit that referenced this pull request Nov 25, 2026
…204320)

**Context**

#198572 already changed Dynamo's `preserve_global_state` to restore the deterministic-algorithms state with `torch._C._set_deterministic_algorithms` instead of `torch.use_deterministic_algorithms`, so deterministic mode set through Inductor alone (`TORCHINDUCTOR_DETERMINISTIC=1` / `config.deterministic = True`) is no longer turned off after the first compiled frame. This PR builds on that.

**Change**

- `preserve_global_state` also saves `torch._inductor.config.deterministic` before compiling a frame and restores it afterwards, so a compile can no longer leave Inductor's deterministic flag changed for later frames (e.g. through a `torch.use_deterministic_algorithms` call made during compilation).
- `torch.set_deterministic_debug_mode` now goes through `torch.use_deterministic_algorithms`, so it sets Inductor's deterministic flag the same way `use_deterministic_algorithms` does (#165950). Behavior change: `"warn"`/`"error"` (1/2) turn on Inductor's deterministic mode, which skips on-device benchmarking and can be slower, and `"default"` (0) turns it off, including when it was set with `TORCHINDUCTOR_DETERMINISTIC=1`.

**Test plan**

- `test_compile_does_not_leak_deterministic_mode` (`test/inductor/test_deterministic.py`): a change to Inductor's flag during compilation does not persist past the frame.
- `test_deterministic_flag_survives_compile` and the Dynamo tests: deterministic mode set through Inductor alone stays on across frames and in the backward compile.
- `test_set_deterministic_debug_mode_sets_inductor_flag`: each debug mode sets Inductor's flag to match.
- `test/dynamo/test_misc.py -k "deterministic or global_state"`.

This change was authored with an AI assistant (Claude Code), with the change and tests reviewed in the session.

Pull Request resolved: #204320
Approved by: https://github.com/drisspg
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 Merged module: dynamo open source topic: not user facing topic category triaged This issue has been looked at a team member, and triaged and prioritized into an appropriate module

Projects

None yet

7 participants