Skip to content

[c10] Don't build the contiguity predicate when the answer is computed concretely - #193220

Closed
anijain2305 wants to merge 23 commits into
gh/anijain2305/1309/basefrom
gh/anijain2305/1309/head
Closed

anijain2305 wants to merge 23 commits into
gh/anijain2305/1309/basefrom
gh/anijain2305/1309/head

Conversation

@anijain2305

@anijain2305 anijain2305 commented Aug 12, 2026 •

Copy link
Copy Markdown
Contributor

Stack from ghstack (oldest at bottom):

compute_contiguous asks three questions in order (see #157472): can contiguity
be proved without guarding, is every shape hinted so it can be computed
concretely, or must a symbolic predicate be handed back. But it ran the first
and third eagerly by calling _compute_contiguous_sym, which does both, and only
then checked whether the shapes were hinted. For a hinted tensor whose
contiguity is not provable guard-free, the predicate was built out of
per-dimension SymInt operations and then thrown away, because the concrete
computation below it is the answer.

Split the two phases apart in Contiguity.h - _contiguous_or_false_sym proves it
without guarding, _contiguous_expr_sym builds the predicate - and ask the
questions in cost order. _compute_contiguous_sym stays as a wrapper that does
both, so its behaviour is unchanged for any other caller.

This matters because each SymInt operation in those loops crosses into Python,
builds a sympy expression and runs it through ShapeEnv.replace, at a few
microseconds a time. With counters in SymbolicShapeMeta, one model's cold
compile with dynamic=True does 7126 contiguity derivations, and 3354 of them -
47% - take exactly the path where the predicate is discarded.

Time spent deriving contiguity, three runs each:

before 753ms 1059ms 747ms (mean 853ms)
after 554ms 548ms 545ms (mean 549ms)

-304ms, -36%, and with much less variance. That is below the ~1s run-to-run
noise of end-to-end compile time on this machine, so this is measured on the
derivation itself rather than on the wall clock; the counts of which path each
derivation takes are identical before and after (3772 concrete, 3354 hinted, 0
symbolic), which is what says the reordering did not move any answers.

Test plan:

python test/test_dynamic_shapes.py -k TestSymbolicContiguity
python test/test_dynamic_shapes.py
python test/test_proxy_tensor.py

TestSymbolicContiguity is new: 14 layouts (contiguous, permuted, channels-last,
size-1 dims, zero-size, mixed symbolic/concrete, 1d through 5d) crossed with
checks on the contiguity answer, the numel expression, and non-overlapping-and-
dense, plus one case asserting the guard-free path specializes nothing. It pins
the answers rather than the path, so it passes against both the old and the new
ordering - which is how it was used to develop this change. The other two suites
produce failure sets identical to a run with the change reverted (this host has
pre-existing aarch64 CPU codegen failures).

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

[ghstack-poisoned]
@pytorch-bot

pytorch-bot Bot commented Aug 12, 2026 •

Copy link
Copy Markdown

🔗 Helpful Links

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

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

❌ 1 Unclassified Failure

As of commit 9c8bac5 with merge base 6a0eb75 (image):

UNCLASSIFIED FAILURE - DrCI could not classify the following job because the workflow did not run on the merge base. The failure may be pre-existing on trunk or introduced by this PR:

  • Check mergeability of ghstack PR / ghstack-mergeability-check (gh) (this job did not run on the merge base, so DrCI cannot tell whether the failure is pre-existing)
    cherry-pick

    AI verdict: AI verdict: related

    The ghstack mergeability check fails because the commit's changes to test/test_dynamic_shapes.py conflict with the current state of trunk. This is a merge conflict directly caused by the PR's code changes being out of date with the base branch.

    Full reasoning on HUD →

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

@pytorch-bot

pytorch-bot Bot commented Aug 12, 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.

[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
anijain2305 added a commit that referenced this pull request Aug 14, 2026
…d concretely

compute_contiguous asks three questions in order (see #157472): can contiguity
be proved without guarding, is every shape hinted so it can be computed
concretely, or must a symbolic predicate be handed back. But it ran the first
and third eagerly by calling _compute_contiguous_sym, which does both, and only
then checked whether the shapes were hinted. For a hinted tensor whose
contiguity is not provable guard-free, the predicate was built out of
per-dimension SymInt operations and then thrown away, because the concrete
computation below it is the answer.

Split the two phases apart in Contiguity.h - _contiguous_or_false_sym proves it
without guarding, _contiguous_expr_sym builds the predicate - and ask the
questions in cost order. _compute_contiguous_sym stays as a wrapper that does
both, so its behaviour is unchanged for any other caller.

This matters because each SymInt operation in those loops crosses into Python,
builds a sympy expression and runs it through ShapeEnv.replace, at a few
microseconds a time. With counters in SymbolicShapeMeta, one model's cold
compile with dynamic=True does 7126 contiguity derivations, and 3354 of them -
47% - take exactly the path where the predicate is discarded.

Time spent deriving contiguity, three runs each:

  before   753ms   1059ms   747ms    (mean 853ms)
  after    554ms    548ms   545ms    (mean 549ms)

-304ms, -36%, and with much less variance. That is below the ~1s run-to-run
noise of end-to-end compile time on this machine, so this is measured on the
derivation itself rather than on the wall clock; the counts of which path each
derivation takes are identical before and after (3772 concrete, 3354 hinted, 0
symbolic), which is what says the reordering did not move any answers.

Test plan:

```
python test/test_dynamic_shapes.py -k TestSymbolicContiguity
python test/test_dynamic_shapes.py
python test/test_proxy_tensor.py
```

TestSymbolicContiguity is new: 14 layouts (contiguous, permuted, channels-last,
size-1 dims, zero-size, mixed symbolic/concrete, 1d through 5d) crossed with
checks on the contiguity answer, the numel expression, and non-overlapping-and-
dense, plus one case asserting the guard-free path specializes nothing. It pins
the answers rather than the path, so it passes against both the old and the new
ordering - which is how it was used to develop this change. The other two suites
produce failure sets identical to a run with the change reverted (this host has
pre-existing aarch64 CPU codegen failures).

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

ghstack-source-id: 3a73082
Pull-Request: #193220
[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
@anijain2305
anijain2305 requested a review from laithsakka August 16, 2026 00:03
[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
@github-actions

Copy link
Copy Markdown
Contributor

Looks like this PR hasn't been updated in a while so we're going to go ahead and mark this as Stale.
Feel free to remove the Stale label if you feel this was a mistake.
If you are unable to remove the Stale label please contact a maintainer in order to do so.
If you want the bot to never mark this PR stale again, add the no-stale label.
Stale pull requests will automatically be closed after 30 days of inactivity.

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant