Skip to content

[dynamo] Compile before reseeding in test_random_float_draws_inductor - #202212

Closed
ezyang wants to merge 1 commit into
pytorch:mainfrom
ezyang:export-D127783734
Closed

ezyang wants to merge 1 commit into
pytorch:mainfrom
ezyang:export-D127783734

Conversation

@ezyang

@ezyang ezyang commented Oct 27, 2026 •

Copy link
Copy Markdown
Contributor

Summary:
test_random_float_draws_inductor_float32 is flaky internally (T294923386). On failure the third output, x * random.random(), equals x * 0.7580, the second draw after random.seed(0), instead of x * 0.8444, the first. So something drew from the global RNG once between the reseed and the runtime draw. The test reseeds before torch.compile, and compilation is lazy, so any global RNG use during the first call's compile (internal module initializations and similar) shifts the draw.

Fix it the way test_multiple_consecutive_random_calls_before_graph in the same file does: compile with a warm-up call, then reseed. The random.Random argument is guarded by type only, so the second call reuses the compiled code.

This change was authored by an AI agent (Oncall Operator, TestX batch triage for pt2_stack_for_internal).

Test Plan:

buck test fbcode//mode/opt fbcode//caffe2/test/dynamo:test_dynamo -- --exact 'caffe2/test/dynamo:test_dynamo - test_unspec.py::UnspecTests::test_random_float_draws_inductor_float32' --stress-runs 20

Fixes T294923386

Differential Revision: D127783734

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

Summary:
`test_random_float_draws_inductor_float32` is flaky internally (T294923386). On failure the third output, `x * random.random()`, equals `x * 0.7580`, the second draw after `random.seed(0)`, instead of `x * 0.8444`, the first. So something drew from the global RNG once between the reseed and the runtime draw. The test reseeds before `torch.compile`, and compilation is lazy, so any global RNG use during the first call's compile (internal module initializations and similar) shifts the draw.

Fix it the way `test_multiple_consecutive_random_calls_before_graph` in the same file does: compile with a warm-up call, then reseed. The `random.Random` argument is guarded by type only, so the second call reuses the compiled code.

This change was authored by an AI agent (Oncall Operator, TestX batch triage for pt2_stack_for_internal).

Test Plan:
```
buck test fbcode//mode/opt fbcode//caffe2/test/dynamo:test_dynamo -- --exact 'caffe2/test/dynamo:test_dynamo - test_unspec.py::UnspecTests::test_random_float_draws_inductor_float32' --stress-runs 20
```

Fixes T294923386

Differential Revision: D127783734
@pytorch-bot

pytorch-bot Bot commented Oct 27, 2026 •

Copy link
Copy Markdown

PR Status: Approved 🚀

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

✅ AUTOMATED REVIEW - Ready for human review:

The PR fixes the internal flake in `test_random_float_draws_inductor` by making
a warm-up compiled call before `random.seed(0)`, the same pattern
`test_multiple_consecutive_random_calls_before_graph` uses. The fix is sound,
but the new comment names the wrong cause. Recommendation: Approve. Fix the
comment wording before landing.

Findings (2):

[minor] test/dynamo/test_unspec.py:261
The comment blames the wrong source of the extra draw. Compilation itself cannot
be the cause. Tracing and Inductor compilation run inside the Dynamo callback,
which runs under `PreserveGlobalState`
(`torch/csrc/dynamo/eval_frame_cpp.cpp:227-253`, used at `:630`). That saves
Python's `random` state before compiling and restores it afterwards, and it also
undoes the trace-time `example_value` draw in `call_random_fn`. So the extra
draw has to happen outside that guard: in the `torch.compile(...)` construction
(e.g. lazy imports) or in the Python `eval_frame` wrapper. The original test
reseeded before `torch.compile(...)`. The first-call tests at lines 820-855
reseed after it and do not appear to flake, which points at construction. Reword
the comment to say what actually happens: one-time global-RNG use outside the
compile itself, during `torch.compile` construction or the first-call wrapper.
Alternatively, reuse the comment already used for this pattern at lines 161-162
/ 212-213 / 420-421.

[info] test/dynamo/test_unspec.py:263
Possible narrower fix (low confidence, needs an internal stress run): moving
`random.seed(0)` to just after `torch.compile(...)` may be enough on its own.
That would keep first-compiled-call coverage for the Inductor path, which the
warm-up call now throws away.

Reviewed commit: 04c6e60

Review run

🔗 Helpful Links

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

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

⏳ No Failures, 4 Pending

As of commit 04c6e60 with merge base a1f5fd0 (image):
💚 Looks good so far! There are no failures yet. 💚

🟢 GREEN LIGHT - PR approved to be merged without human review:

  • Triviality
    • Single-cause bugfix: flaky test_random_float_draws_inductor reseeded before lazy compile, so compile-time global RNG draws shifted random.random()
    • Fix adds a warm-up opt_fn(x, random.Random(0)) before random.seed(0), mirroring test_multiple_consecutive_random_calls_before_graph in the same file
  • Test strength
    • Assertion and expected are unchanged; compiled output is still compared to the eager result under the same seed
    • random.Random args get TYPE_MATCH guards in torch/_dynamo/variables/builder.py, so the asserted call reuses the compiled code
  • Scope
    • One hunk in test/dynamo/test_unspec.py; no production, fixture, or runner file touched

reason: clean
Reviewed commit: 04c6e60

Inference job

🐛 Report a wrong verdict

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

@linux-foundation-easycla

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

Copy link
Copy Markdown

CLA Signed
The committers listed above are authorized under a signed CLA.

@meta-codesync

meta-codesync Bot commented Oct 27, 2026

Copy link
Copy Markdown

@ezyang has exported this pull request. If you are a Meta employee, you can view the originating Diff in D127783734.

@pytorchbot pytorchbot added the in progress PR is being worked on by the author, until the automated review accepts it label Oct 27, 2026
@pytorchgreenlight pytorchgreenlight Bot added the GL/approved Approved by GreenLight label Oct 27, 2026
@pytorch-bot pytorch-bot Bot added the ciflow/trunk Trigger trunk jobs on your pull request label Oct 27, 2026

@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 04c6e60: nothing blocking; ready for human review.

The PR fixes the internal flake in test_random_float_draws_inductor by making a warm-up compiled call before random.seed(0), the same pattern test_multiple_consecutive_random_calls_before_graph uses. The fix is sound, but the new comment names the wrong cause. Recommendation: Approve. Fix the comment wording before landing.

⚪ 2 non-blocking findings

# Compiling can draw from the global RNG (e.g. internal module

The comment blames the wrong source of the extra draw. Compilation itself cannot be the cause. Tracing and Inductor compilation run inside the Dynamo callback, which runs under PreserveGlobalState (torch/csrc/dynamo/eval_frame_cpp.cpp:227-253, used at :630). That saves Python's random state before compiling and restores it afterwards, and it also undoes the trace-time example_value draw in call_random_fn. So the extra draw has to happen outside that guard: in the torch.compile(...) construction (e.g. lazy imports) or in the Python eval_frame wrapper. The original test reseeded before torch.compile(...). The first-call tests at lines 820-855 reseed after it and do not appear to flake, which points at construction. Reword the comment to say what actually happens: one-time global-RNG use outside the compile itself, during torch.compile construction or the first-call wrapper. Alternatively, reuse the comment already used for this pattern at lines 161-162 / 212-213 / 420-421.


opt_fn(x, random.Random(0))

Possible narrower fix (low confidence, needs an internal stress run): moving random.seed(0) to just after torch.compile(...) may be enough on its own. That would keep first-compiled-call coverage for the Inductor path, which the warm-up call now throws away.

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

@ezyang

ezyang commented Oct 27, 2026

Copy link
Copy Markdown
Contributor Author

Superseded by #202224 (same change, resubmitted with the correct commit author email so the CLA check passes).

@ezyang ezyang closed this Oct 27, 2026
@jarias-lfx

Copy link
Copy Markdown

/easycla

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

Labels

ciflow/inductor ciflow/trunk Trigger trunk jobs on your pull request GL/approved Approved by GreenLight in progress PR is being worked on by the author, until the automated review accepts it meta-exported module: dynamo topic: not user facing topic category

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants