Repository navigation
[c10] Don't build the contiguity predicate when the answer is computed concretely - #193220
anijain2305 wants to merge 23 commits into
Conversation
[ghstack-poisoned]
🔗 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 FailureAs of commit 9c8bac5 with merge base 6a0eb75 ( 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:
This comment was automatically generated by Dr. CI and updates every 15 minutes. |
This PR needs a
|
[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
…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]
[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
|
Looks like this PR hasn't been updated in a while so we're going to go ahead and mark this as |
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:
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.