Skip to content

Fix extra clone in torch.compile for stateless RNG APIs - #188495

Closed
jbschlosser wants to merge 4 commits into
gh/jbschlosser/277/basefrom
gh/jbschlosser/277/head
Closed

jbschlosser wants to merge 4 commits into
gh/jbschlosser/277/basefrom
gh/jbschlosser/277/head

Conversation

@jbschlosser

@jbschlosser jbschlosser commented Jun 29, 2026 •

Copy link
Copy Markdown
Contributor

Stack from ghstack (oldest at bottom):

Benchmark script (P2398638090) shows a performance issue with the new stateless RNG APIs within torch.compile. Tracked this down to the auto-generated functional variants of the new ops performing an extra clone. To address this, this PR:

  • Introduces explicit, non-autogenerated functional variants for the in-place ops that use empty_like(self) instead of self.clone() to indicate to functionalization that the data is completely overwritten and is not needed for correctness. This addresses the performance issue for the in-place ops, but still (briefly) results in a 2x allocation for an unused buffer alongside the output when the out-of-place APIs call the in-place ops.
  • Introduces new size-based overloads for the out-of-place APIs. Now they bypass the in-place ops entirely (preferring the size-based, out-of-place ops), avoiding the aforementioned 2x allocation.
  • Adds inplaceable_ops registry entries for the philox uniform / normal ops. These will be unneeded once we land Inductor lowerings or a more general fix (e.g. [inductor] Generalize reinplacing to functional ATen ops with an in-place variant #189080).

cc @voznesenskym @penguinwu @EikanWang @jgong5 @Guobing-Chen @XiaobingSuper @zhuhaozhe @blzheng @wenzhe-nrv @jiayisunx @ipiszy @kadeng @muchulee8 @amjames @chauhang @aakhundov @coconutruben @jataylo

[ghstack-poisoned]
@pytorch-bot

pytorch-bot Bot commented Jun 29, 2026 •

Copy link
Copy Markdown

🔗 Helpful Links

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

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

❌ 6 New Failures

As of commit fbb9e5f with merge base caf9663 (image):

NEW FAILURES - The following jobs have failed:

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

@jbschlosser jbschlosser added the topic: not user facing topic category label Jun 29, 2026
[ghstack-poisoned]
jbschlosser added a commit that referenced this pull request Jun 29, 2026
@jbschlosser
jbschlosser requested review from albanD, ngimel and zou3519 June 29, 2026 23:28

@zou3519 zou3519 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.

seems fine, discussed offline that there is likely a bug in Inductor that we can investigate separately.

[ghstack-poisoned]
jbschlosser added a commit that referenced this pull request Jul 6, 2026
[ghstack-poisoned]
jbschlosser added a commit that referenced this pull request Jul 6, 2026
@jbschlosser

Copy link
Copy Markdown
Contributor Author

Update: instead of adding out-of-place op variants, I want to make the ops inplaceable with manual allowlist entries until we can get a more general fix in (e.g. #189080).

@jbschlosser

Copy link
Copy Markdown
Contributor Author

@claude are the CI failures related?

@claude

claude Bot commented Jul 6, 2026 •

Copy link
Copy Markdown

Claude finished @jbschlosser's task in 1m 25s —— View job


Are the CI failures related?

  • Fetch CI status for this PR
  • Determine whether any failures are related to the changes

No — the only failure is unrelated infrastructure flakiness.

Details

The single failing workflow is torchtitan-test (run 28823537985). Both failing jobs — torchtitan_features_integration and torchtitan_models_integration — fail at the same spot: loading the HuggingFace c4_test dataset in the torchtitan trainer setup, before any training/compile step runs.

The root cause is a read-only cache mount, not the code under test:

File ".../datasets/builder.py", line 397, in __init__
    with FileLock(lock_path):
  ...
  File ".../filelock/_unix.py", line 47, in _acquire
    fd = os.open(self.lock_file, open_flags, self._context.mode)
OSError: [Errno 30] Read-only file system:
  '/mnt/hf_cache/datasets/_mnt_hf_cache_datasets_c4_test_default_0.0.0_d1c090699fbcc2ee.lock'

This OSError: [Errno 30] Read-only file system on /mnt/hf_cache/datasets/... occurs on every rank (437 occurrences across the two jobs). It's a dataset-cache mount permission problem on the runner, entirely unrelated to this PR, which only touches:

  • torch/_inductor/fx_passes/reinplace.py (adds inplaceable_ops entries for the philox uniform/normal ops)
  • test/test_stateless_rng.py

Neither of those files is exercised by the torchtitan dataset loading path that fails.

Everything else is green or still running: Lint, BC Lint, docs-build, Check mergeability all passed; pull and inductor are still in progress.

Recommendation: this failure is a runner-side flake. A rerun once the mount is healthy (or an unrelated infra fix) should clear it — no code change needed on this PR.

@jbschlosser

Copy link
Copy Markdown
Contributor Author

@pytorchbot merge -i

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

Copy link
Copy Markdown
Collaborator

@pytorchmergebot

Copy link
Copy Markdown
Collaborator

Merge failed

Reason: 1 jobs have failed, first few of them are: trunk / linux-jammy-aarch64-py3.10 / test-osdc (default, 5, 5, lf-l-arm64g3-16-62)

Details for Dev Infra team Raised by workflow job

@jbschlosser

Copy link
Copy Markdown
Contributor Author

@pytorchbot merge -i

@jbschlosser

Copy link
Copy Markdown
Contributor Author

@claude make sure all the CI failures are still infra-related

@claude

claude Bot commented Jul 6, 2026 •

Copy link
Copy Markdown

Claude finished @jbschlosser's task in 2m 41s —— View job


Are all the CI failures still infra-related?

  • Fetch current CI status
  • Inspect each failing job
  • Report whether failures are infra-related

Mostly yes, but not all — the inductor accuracy failures are a stale merge base (benign), and the rest are pure infra flakiness. None are caused by this PR. Two categories:

Details

I split the 6 reported failures into two root causes.

1. Inductor accuracy failures — stale merge base (benign, NOT infra, NOT this PR)

These are graph-break count assertions, not infra errors:

Job Model Observed Expected in PR checkout
inductor_timm (1,2) mobilenetv2_100 graph_breaks=7 0
inductor_timm (2,2) tf_efficientnet_b0 graph_breaks=6 0
inductor_torchbench (2,2) mnasnet1_0 / mobilenet_v2 / shufflenet_v2_x1_0 7 / 6 / 6 0 / 0 / 0

The expected counts were bumped on trunk. Comparing the CSVs:

# This PR's checkout (HEAD):
inductor_timm_training.csv:       mobilenetv2_100,pass,0    tf_efficientnet_b0,pass,0
inductor_torchbench_training.csv: mnasnet1_0,pass,0  mobilenet_v2,pass,0  shufflenet_v2_x1_0,pass,0

# origin/main:
inductor_timm_training.csv:       mobilenetv2_100,pass,7    tf_efficientnet_b0,pass,6
inductor_torchbench_training.csv: mnasnet1_0,pass,7  mobilenet_v2,pass,6  shufflenet_v2_x1_0,pass,6

The observed graph-break counts in CI (7/6/7/6/6) exactly match the values already on origin/main. Trunk raised these expectations after this PR's merge base was cut (base is from 2026-07-06 16:57 EDT; the CSV update is newer). So this is a stale-merge-base artifact, unrelated to the philox reinplace change — this PR only touches reinplace.py and test_stateless_rng.py, neither of which is exercised by these vision models. A rebase would clear it.

Note: this also settles the earlier Dr. CI "cannot establish causal mechanism" verdict for tf_efficientnet_b0 — the observed count matches trunk's expectation, so it's the same stale-CSV cause as the others.

2. Pure infra flakiness (NOT this PR)

  • torchtitan-test (both features_integration and models_integration): read-only filesystem on the HF dataset cache mount, failing before any compile step:
    OSError: [Errno 30] Read-only file system:
      '/mnt/hf_cache/datasets/_mnt_hf_cache_datasets_c4_test_default_0.0.0_...lock'
    
  • trunk / linux-jammy-aarch64-py3.10 (previously): sccache: error: couldn't connect to server — build-cache connectivity. This run is currently re-queued.

Bottom line

No failure is caused by this PR. The inductor ones are a stale merge base (would vanish on rebase); the torchtitan/aarch64 ones are runner-side infra flakiness. Ignoring/merging remains safe. Green/passing: Lint, BC Lint, TSan, docs-build, Check mergeability, all inductor-cpu tests; pull and inductor GPU still in progress.

• `gh/jbschlosser/277/head`

@jbschlosser jbschlosser added release notes: python_frontend python frontend release notes category topic: performance topic category and removed topic: not user facing topic category labels Jul 6, 2026
pytorchmergebot pushed a commit that referenced this pull request Jul 16, 2026
…kernels (#189735)

Original repro in P2415048693.

Found to affect the `_philox_uniform` / `_philox_normal` ops in practice, which currently utilize `FallbackKernel` and are re-inplaced as of #188495.

`FallbackKernel` previously did not record that the returned buffer aliases the input for re-inplaced ops. As a result, memory planning treated the input buffer as dead after the op and wrongly reused its storage while the aliasing output was still live, corrupting results.

This PR fixes this by recording the output <-> first-arg alias in `FallbackKernel` so memory planning keeps the buffer live.

Pull Request resolved: #189735
Approved by: https://github.com/eellison
ghstack dependencies: #189767
vishalgoyal316 pushed a commit to vishalgoyal316/pytorch that referenced this pull request Jul 16, 2026
Benchmark script (P2398638090) shows a performance issue with the new stateless RNG APIs within torch.compile. Tracked this down to the auto-generated functional variants of the new ops performing an extra clone. To address this, this PR:
* ~~Introduces explicit, non-autogenerated functional variants for the in-place ops that use `empty_like(self)` instead of `self.clone()` to indicate to functionalization that the data is completely overwritten and is not needed for correctness. This addresses the performance issue for the in-place ops, but still (briefly) results in a 2x allocation for an unused buffer alongside the output when the out-of-place APIs call the in-place ops.~~
* ~~Introduces new size-based overloads for the out-of-place APIs. Now they bypass the in-place ops entirely (preferring the size-based, out-of-place ops), avoiding the aforementioned 2x allocation.~~
* Adds `inplaceable_ops` registry entries for the philox uniform / normal ops. These will be unneeded once we land Inductor lowerings or a more general fix (e.g. pytorch#189080).

Pull Request resolved: pytorch#188495
Approved by: https://github.com/ngimel, https://github.com/zou3519
DamJanusz pushed a commit to DamJanusz/pytorch that referenced this pull request Jul 21, 2026
Benchmark script (P2398638090) shows a performance issue with the new stateless RNG APIs within torch.compile. Tracked this down to the auto-generated functional variants of the new ops performing an extra clone. To address this, this PR:
* ~~Introduces explicit, non-autogenerated functional variants for the in-place ops that use `empty_like(self)` instead of `self.clone()` to indicate to functionalization that the data is completely overwritten and is not needed for correctness. This addresses the performance issue for the in-place ops, but still (briefly) results in a 2x allocation for an unused buffer alongside the output when the out-of-place APIs call the in-place ops.~~
* ~~Introduces new size-based overloads for the out-of-place APIs. Now they bypass the in-place ops entirely (preferring the size-based, out-of-place ops), avoiding the aforementioned 2x allocation.~~
* Adds `inplaceable_ops` registry entries for the philox uniform / normal ops. These will be unneeded once we land Inductor lowerings or a more general fix (e.g. pytorch#189080).

Pull Request resolved: pytorch#188495
Approved by: https://github.com/ngimel, https://github.com/zou3519
DamJanusz pushed a commit to DamJanusz/pytorch that referenced this pull request Jul 21, 2026
…kernels (pytorch#189735)

Original repro in P2415048693.

Found to affect the `_philox_uniform` / `_philox_normal` ops in practice, which currently utilize `FallbackKernel` and are re-inplaced as of pytorch#188495.

`FallbackKernel` previously did not record that the returned buffer aliases the input for re-inplaced ops. As a result, memory planning treated the input buffer as dead after the op and wrongly reused its storage while the aliasing output was still live, corrupting results.

This PR fixes this by recording the output <-> first-arg alias in `FallbackKernel` so memory planning keeps the buffer live.

Pull Request resolved: pytorch#189735
Approved by: https://github.com/eellison
ghstack dependencies: pytorch#189767
youlituao pushed a commit to youlituao/pytorch that referenced this pull request Jul 28, 2026
…kernels (pytorch#189735)

Original repro in P2415048693.

Found to affect the `_philox_uniform` / `_philox_normal` ops in practice, which currently utilize `FallbackKernel` and are re-inplaced as of pytorch#188495.

`FallbackKernel` previously did not record that the returned buffer aliases the input for re-inplaced ops. As a result, memory planning treated the input buffer as dead after the op and wrongly reused its storage while the aliasing output was still live, corrupting results.

This PR fixes this by recording the output <-> first-arg alias in `FallbackKernel` so memory planning keeps the buffer live.

Pull Request resolved: pytorch#189735
Approved by: https://github.com/eellison
ghstack dependencies: pytorch#189767
aws-kingrj pushed a commit to amazon-contributing/upstream-to-pytorch that referenced this pull request Jul 29, 2026
Benchmark script (P2398638090) shows a performance issue with the new stateless RNG APIs within torch.compile. Tracked this down to the auto-generated functional variants of the new ops performing an extra clone. To address this, this PR:
* ~~Introduces explicit, non-autogenerated functional variants for the in-place ops that use `empty_like(self)` instead of `self.clone()` to indicate to functionalization that the data is completely overwritten and is not needed for correctness. This addresses the performance issue for the in-place ops, but still (briefly) results in a 2x allocation for an unused buffer alongside the output when the out-of-place APIs call the in-place ops.~~
* ~~Introduces new size-based overloads for the out-of-place APIs. Now they bypass the in-place ops entirely (preferring the size-based, out-of-place ops), avoiding the aforementioned 2x allocation.~~
* Adds `inplaceable_ops` registry entries for the philox uniform / normal ops. These will be unneeded once we land Inductor lowerings or a more general fix (e.g. pytorch#189080).

Pull Request resolved: pytorch#188495
Approved by: https://github.com/ngimel, https://github.com/zou3519
aws-kingrj pushed a commit to amazon-contributing/upstream-to-pytorch that referenced this pull request Jul 29, 2026
…kernels (pytorch#189735)

Original repro in P2415048693.

Found to affect the `_philox_uniform` / `_philox_normal` ops in practice, which currently utilize `FallbackKernel` and are re-inplaced as of pytorch#188495.

`FallbackKernel` previously did not record that the returned buffer aliases the input for re-inplaced ops. As a result, memory planning treated the input buffer as dead after the op and wrongly reused its storage while the aliasing output was still live, corrupting results.

This PR fixes this by recording the output <-> first-arg alias in `FallbackKernel` so memory planning keeps the buffer live.

Pull Request resolved: pytorch#189735
Approved by: https://github.com/eellison
ghstack dependencies: pytorch#189767
MelindaTan pushed a commit to MelindaTan/pytorch that referenced this pull request Jul 31, 2026
…kernels (pytorch#189735)

Original repro in P2415048693.

Found to affect the `_philox_uniform` / `_philox_normal` ops in practice, which currently utilize `FallbackKernel` and are re-inplaced as of pytorch#188495.

`FallbackKernel` previously did not record that the returned buffer aliases the input for re-inplaced ops. As a result, memory planning treated the input buffer as dead after the op and wrongly reused its storage while the aliasing output was still live, corrupting results.

This PR fixes this by recording the output <-> first-arg alias in `FallbackKernel` so memory planning keeps the buffer live.

Pull Request resolved: pytorch#189735
Approved by: https://github.com/eellison
ghstack dependencies: pytorch#189767
@github-actions
github-actions Bot deleted the gh/jbschlosser/277/head branch August 6, 2026 02:27
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: inductor release notes: python_frontend python frontend release notes category topic: performance topic category

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants