Skip to content

[inductor] Cache optimization_hint across lowering - #192817

Closed
anijain2305 wants to merge 7 commits into
gh/anijain2305/1297/basefrom
gh/anijain2305/1297/head
Closed

anijain2305 wants to merge 7 commits into
gh/anijain2305/1297/basefrom
gh/anijain2305/1297/head

Conversation

@anijain2305

@anijain2305 anijain2305 commented Aug 10, 2026 •

Copy link
Copy Markdown
Contributor

Stack from ghstack (oldest at bottom):

SizeVarAllocator.optimization_hint is asked for the same expression over and
over while lowering: on FairChem UMA, 75784 calls over 388 distinct
expressions, with one expression accounting for 18917 of them. Each miss runs
sympy substitution plus the unbacked-hint heuristics, none of which sympy
caches internally.

Route it through the existing _lru_cache helper, which already backs
stride_vars and simplify_with_ranges and drops the cache whenever replacements
change.

Measured on UMA, interleaved runs: cold compile 55.32s -> 54.36s.

Test Plan:

python test/inductor/test_torchinductor.py
python test/inductor/test_torchinductor_dynamic_shapes.py

Failure sets unchanged. The cache was also checked by computing both the cached
and uncached value on every call for a full compile: 43906 comparisons, 0
mismatches.

I also tried caching SizeVarAllocator.simplify, which looked more promising
still (149938 calls over 1396 distinct expressions) but measured slower: it is
sympy.expand plus xreplace, and sympy already memoizes expand, so an lru_cache
only adds hashing of large expressions on top. Not included.

This PR was authored with the assistance of an AI coding agent.

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 Aug 10, 2026 •

Copy link
Copy Markdown

🔗 Helpful Links

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

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

✅ No Failures

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

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

[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
@anijain2305
anijain2305 requested a review from mlazos August 11, 2026 04:26
[ghstack-poisoned]
[ghstack-poisoned]
@anijain2305 anijain2305 added ciflow/trunk Trigger trunk jobs on your pull request topic: not user facing topic category labels Aug 11, 2026
[ghstack-poisoned]
@eellison
eellison requested a review from laithsakka August 11, 2026 19:22
@anijain2305

Copy link
Copy Markdown
Contributor Author

@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

pytorchmergebot pushed a commit that referenced this pull request Aug 11, 2026
…ch cache hit (#192821)

When the fake tensor dispatch cache hits for a view op, the output is synthesized
by allocating a meta tensor with empty_strided and then calling set_ on it to
point at the input's storage. set_ overwrites the size, stride and storage that
empty_strided just established, so on symbolic shapes we pay twice for the part
that dominates both calls: deriving the storage size and the contiguity flags
from symbolic sizes and strides, which calls back into Python for every SymInt
add, mul and comparison.

Allocate the placeholder with an empty size and stride when the op is a view, and
let set_ install the real ones.

Measured on a FairChem UMA compile, where 7288 of 16551 cache hits are views:

  empty_strided in the cache hit path   192us/call, 3.19s total
  set_ in the cache hit path            283us/call, 2.06s total

Interleaved cold compile runs: 53.93s -> 52.43s.

Test Plan:

```
python test/test_fake_tensor.py
```

Failure set identical to baseline (4 pre-existing cudnn RNN errors). Also
exercised the view synthesis path end to end under both dynamic=True and
dynamic=False with view/permute/T/slice/expand/as_strided plus backward:

```
python -c "... torch.compile(f, dynamic=dyn) over view, permute, .T, x[1:], expand, as_strided ..."
```

This PR was authored with the assistance of an AI coding agent.
Pull Request resolved: #192821
Approved by: https://github.com/zou3519
ghstack dependencies: #192817
ankitbhrdwj pushed a commit to ankitbhrdwj/pytorch that referenced this pull request Aug 12, 2026
SizeVarAllocator.optimization_hint is asked for the same expression over and
over while lowering: on FairChem UMA, 75784 calls over 388 distinct
expressions, with one expression accounting for 18917 of them. Each miss runs
sympy substitution plus the unbacked-hint heuristics, none of which sympy
caches internally.

Route it through the existing _lru_cache helper, which already backs
stride_vars and simplify_with_ranges and drops the cache whenever replacements
change.

Measured on UMA, interleaved runs: cold compile 55.32s -> 54.36s.

Test Plan:

```
python test/inductor/test_torchinductor.py
python test/inductor/test_torchinductor_dynamic_shapes.py
```

Failure sets unchanged. The cache was also checked by computing both the cached
and uncached value on every call for a full compile: 43906 comparisons, 0
mismatches.

I also tried caching SizeVarAllocator.simplify, which looked more promising
still (149938 calls over 1396 distinct expressions) but measured slower: it is
sympy.expand plus xreplace, and sympy already memoizes expand, so an lru_cache
only adds hashing of large expressions on top. Not included.

This PR was authored with the assistance of an AI coding agent.

Pull Request resolved: pytorch#192817
Approved by: https://github.com/eellison
ankitbhrdwj pushed a commit to ankitbhrdwj/pytorch that referenced this pull request Aug 12, 2026
…ch cache hit (pytorch#192821)

When the fake tensor dispatch cache hits for a view op, the output is synthesized
by allocating a meta tensor with empty_strided and then calling set_ on it to
point at the input's storage. set_ overwrites the size, stride and storage that
empty_strided just established, so on symbolic shapes we pay twice for the part
that dominates both calls: deriving the storage size and the contiguity flags
from symbolic sizes and strides, which calls back into Python for every SymInt
add, mul and comparison.

Allocate the placeholder with an empty size and stride when the op is a view, and
let set_ install the real ones.

Measured on a FairChem UMA compile, where 7288 of 16551 cache hits are views:

  empty_strided in the cache hit path   192us/call, 3.19s total
  set_ in the cache hit path            283us/call, 2.06s total

Interleaved cold compile runs: 53.93s -> 52.43s.

Test Plan:

```
python test/test_fake_tensor.py
```

Failure set identical to baseline (4 pre-existing cudnn RNN errors). Also
exercised the view synthesis path end to end under both dynamic=True and
dynamic=False with view/permute/T/slice/expand/as_strided plus backward:

```
python -c "... torch.compile(f, dynamic=dyn) over view, permute, .T, x[1:], expand, as_strided ..."
```

This PR was authored with the assistance of an AI coding agent.
Pull Request resolved: pytorch#192821
Approved by: https://github.com/zou3519
ghstack dependencies: pytorch#192817
gavinwang269 pushed a commit to gavinwang269/pytorch that referenced this pull request Aug 13, 2026
SizeVarAllocator.optimization_hint is asked for the same expression over and
over while lowering: on FairChem UMA, 75784 calls over 388 distinct
expressions, with one expression accounting for 18917 of them. Each miss runs
sympy substitution plus the unbacked-hint heuristics, none of which sympy
caches internally.

Route it through the existing _lru_cache helper, which already backs
stride_vars and simplify_with_ranges and drops the cache whenever replacements
change.

Measured on UMA, interleaved runs: cold compile 55.32s -> 54.36s.

Test Plan:

```
python test/inductor/test_torchinductor.py
python test/inductor/test_torchinductor_dynamic_shapes.py
```

Failure sets unchanged. The cache was also checked by computing both the cached
and uncached value on every call for a full compile: 43906 comparisons, 0
mismatches.

I also tried caching SizeVarAllocator.simplify, which looked more promising
still (149938 calls over 1396 distinct expressions) but measured slower: it is
sympy.expand plus xreplace, and sympy already memoizes expand, so an lru_cache
only adds hashing of large expressions on top. Not included.

This PR was authored with the assistance of an AI coding agent.

Pull Request resolved: pytorch#192817
Approved by: https://github.com/eellison
gavinwang269 pushed a commit to gavinwang269/pytorch that referenced this pull request Aug 13, 2026
…ch cache hit (pytorch#192821)

When the fake tensor dispatch cache hits for a view op, the output is synthesized
by allocating a meta tensor with empty_strided and then calling set_ on it to
point at the input's storage. set_ overwrites the size, stride and storage that
empty_strided just established, so on symbolic shapes we pay twice for the part
that dominates both calls: deriving the storage size and the contiguity flags
from symbolic sizes and strides, which calls back into Python for every SymInt
add, mul and comparison.

Allocate the placeholder with an empty size and stride when the op is a view, and
let set_ install the real ones.

Measured on a FairChem UMA compile, where 7288 of 16551 cache hits are views:

  empty_strided in the cache hit path   192us/call, 3.19s total
  set_ in the cache hit path            283us/call, 2.06s total

Interleaved cold compile runs: 53.93s -> 52.43s.

Test Plan:

```
python test/test_fake_tensor.py
```

Failure set identical to baseline (4 pre-existing cudnn RNN errors). Also
exercised the view synthesis path end to end under both dynamic=True and
dynamic=False with view/permute/T/slice/expand/as_strided plus backward:

```
python -c "... torch.compile(f, dynamic=dyn) over view, permute, .T, x[1:], expand, as_strided ..."
```

This PR was authored with the assistance of an AI coding agent.
Pull Request resolved: pytorch#192821
Approved by: https://github.com/zou3519
ghstack dependencies: pytorch#192817
@github-actions
github-actions Bot deleted the gh/anijain2305/1297/head branch November 3, 2026 01:49
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 topic: not user facing topic category

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants