Repository navigation
[fx] Skip replace() for a SymNode holding a boolean constant - #193159
anijain2305 wants to merge 13 commits into
Conversation
[ghstack-poisoned]
🔗 Helpful Links🧪 See artifacts and rendered test results at hud.pytorch.org/pr/193159
Note: Links to docs will display an error until the docs builds have been completed. ❌ 1 Cancelled Job, 1 Unclassified FailureAs of commit 137c39a with merge base 411c2b5 ( 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:
CANCELLED JOB - The following job was cancelled. Please retry:
This comment was automatically generated by Dr. CI and updates every 15 minutes. |
SymNode.expr already short-circuits ints and sympy Numbers, but sympy.true and sympy.false are neither, so a SymBool that has settled on a constant goes to ShapeEnv.replace on every access. Symbol replacement cannot change a constant, so the call is pure overhead: the version check, the lru lookup and hashing the expression. Extend the fast path with is_Boolean and is_Atom - the atom test keeps relationals, which do have symbols to replace, on the general path. On one inductor compile this drops ShapeEnv.replace from 86904 calls to 61078 (-30%). The wall-clock effect is below the noise floor of the model I measured, so this is about not doing provably useless work rather than a measured win. Test plan: ``` python test/test_dynamic_shapes.py python test/dynamo/test_misc.py ``` Both match a run with the change stashed. This PR was authored with the assistance of an AI coding agent. ghstack-source-id: eaed471 Pull-Request: #193159
[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
SymNode.expr already short-circuits ints and sympy Numbers, but sympy.true and sympy.false are neither, so a SymBool that has settled on a constant goes to ShapeEnv.replace on every access. Symbol replacement cannot change a constant, so the call is pure overhead: the version check, the lru lookup and hashing the expression. Extend the fast path with is_Boolean and is_Atom - the atom test keeps relationals, which do have symbols to replace, on the general path. On one inductor compile this drops ShapeEnv.replace from 86904 calls to 61078 (-30%). The wall-clock effect is below the noise floor of the model I measured, so this is about not doing provably useless work rather than a measured win. Test plan: ``` python test/test_dynamic_shapes.py python test/dynamo/test_misc.py ``` Both match a run with the change stashed. This PR was authored with the assistance of an AI coding agent. ghstack-source-id: 9dbf90b Pull-Request: #193159
[ghstack-poisoned]
[ghstack-poisoned]
| if ( | ||
| isinstance(self._expr, int) | ||
| or self._expr.is_Number # pyrefly: ignore[missing-attribute] | ||
| # sympy.true / sympy.false are not Numbers, and a SymBool that has |
There was a problem hiding this comment.
comment is long combine it with the one at line 214 to say this is fast path when we know replace will do nothing since replace is expenaive.
also keep the part that self._expr.is_Boolean and self._expr.is_Atom == sympy.true / sympy.false
| # symbols to replace, on the general path below. | ||
| or ( | ||
| self._expr.is_Boolean # pyrefly: ignore[missing-attribute] | ||
| and self._expr.is_Atom # pyrefly: ignore[missing-attribute] |
There was a problem hiding this comment.
nit :
is it easier to compare against sympy.true / sympy.false instead of
self._expr.is_Boolean # pyrefly: ignore[missing-attribute]
and self._expr.is_Atom ??
self._expr is _SYMPY_TRUE or self._expr is _SYMPY_FALSE
with
import sympy
_SYMPY_TRUE = sympy.true
_SYMPY_FALSE = sympy.false
outside
[ghstack-poisoned]
[ghstack-poisoned]
|
Starting merge as part of PR stack under #193161 |
…#193161) record_shapeenv_event wraps the mutating ShapeEnv methods, so it runs a few hundred thousand times per compile (366k in one model's cold compile). Event recording is only on under translation validation, but the wrapper paid for it on every call regardless: an import of symbolic_shapes, an isinstance assertion, a debug log, bumping the NEST counter, building the retlog closure, and a second call through it to log the return value. Check for "not recording, and the trace log is off" first and call the wrapped function directly. The isinstance check moves to the slow path; the decorator already verifies at decoration time that it is wrapping a method whose first argument is self, and the fast path would raise AttributeError immediately on anything without should_record_events. On one model's cold compile with dynamic=True, total compile time as the union of top-level chromium events: pair 1: baseline 48.12s changed 47.70s pair 2: baseline 47.76s changed 46.75s pair 3: baseline 47.57s changed 46.18s i.e. 47.82s -> 46.88s, -2.0%. Test plan: ``` python test/test_dynamic_shapes.py python test/dynamo/test_misc.py ``` Both give failure sets identical to a run with the change stashed (this host has pre-existing aarch64 CPU codegen failures). Recording itself is exercised by a ShapeEnv(should_record_events=True) that traces a few fake-tensor ops and replays its events; the recorded event names and the replayed ShapeEnv are unchanged. This PR was authored with the assistance of an AI coding agent. Pull Request resolved: #193161 Approved by: https://github.com/laithsakka ghstack dependencies: #193159
…lready knows (#193416) Every FunctionalTensor wraps an inner tensor by calling _make_wrapper_subclass with the inner tensor's sizes and strides. When the caller does not supply storage_size, that helper computes it with computeStorageNbytes, which walks the dimensions doing symbolic arithmetic - a sub, a mul and an add per dimension - to size a storage that exists only to track aliasing between subclasses; the data pointer is not even valid. The inner tensor already knows its storage size, so pass it. This is the same thing NestedTensor does (torch/nested/_internal/nested_tensor.py passes storage_size=values.untyped_storage().size()), and mirroring elem is if anything the more faithful value: the wrapper is meant to shadow elem, whereas the recomputed number describes only the extent the sizes and strides reach. On a 4d symbolic tensor the call goes from 144us to 21us. Over one model's cold compile with dynamic=True, across 4268 wrapper creations, time in FunctionalTensor.__new__ drops from 1.04s to 0.11s, and about 30k symbolic operations disappear from the compile. End to end, as the union of top-level chromium events, over interleaved pairs: pair 1: baseline 40.26s with storage_size 39.78s pair 2: baseline 40.05s with storage_size 38.16s pair 3: baseline 39.49s with storage_size 38.07s 39.93s -> 38.67s, -3.2%. Sparse inners keep the old path, as they already do for strides and storage_offset. Test plan: ``` python test/inductor/test_torchinductor_dynamic_shapes.py -k cuda python test/test_functionalization.py ``` Failure sets identical to a run with the change reverted (this aarch64 host has pre-existing failures). This PR was authored with the assistance of an AI coding agent. Pull Request resolved: #193416 Approved by: https://github.com/Skylion007, https://github.com/zou3519 ghstack dependencies: #193159, #193161
…#193159) SymNode.expr already short-circuits ints and sympy Numbers, but sympy.true and sympy.false are neither, so a SymBool that has settled on a constant goes to ShapeEnv.replace on every access. Symbol replacement cannot change a constant, so the call is pure overhead: the version check, the lru lookup and hashing the expression. Extend the fast path with is_Boolean and is_Atom - the atom test keeps relationals, which do have symbols to replace, on the general path. On one inductor compile this drops ShapeEnv.replace from 86904 calls to 61078 (-30%). The wall-clock effect is below the noise floor of the model I measured, so this is about not doing provably useless work rather than a measured win. Test plan: ``` python test/test_dynamic_shapes.py python test/dynamo/test_misc.py ``` Both match a run with the change stashed. This PR was authored with the assistance of an AI coding agent. Pull Request resolved: pytorch#193159 Approved by: https://github.com/laithsakka
…pytorch#193161) record_shapeenv_event wraps the mutating ShapeEnv methods, so it runs a few hundred thousand times per compile (366k in one model's cold compile). Event recording is only on under translation validation, but the wrapper paid for it on every call regardless: an import of symbolic_shapes, an isinstance assertion, a debug log, bumping the NEST counter, building the retlog closure, and a second call through it to log the return value. Check for "not recording, and the trace log is off" first and call the wrapped function directly. The isinstance check moves to the slow path; the decorator already verifies at decoration time that it is wrapping a method whose first argument is self, and the fast path would raise AttributeError immediately on anything without should_record_events. On one model's cold compile with dynamic=True, total compile time as the union of top-level chromium events: pair 1: baseline 48.12s changed 47.70s pair 2: baseline 47.76s changed 46.75s pair 3: baseline 47.57s changed 46.18s i.e. 47.82s -> 46.88s, -2.0%. Test plan: ``` python test/test_dynamic_shapes.py python test/dynamo/test_misc.py ``` Both give failure sets identical to a run with the change stashed (this host has pre-existing aarch64 CPU codegen failures). Recording itself is exercised by a ShapeEnv(should_record_events=True) that traces a few fake-tensor ops and replays its events; the recorded event names and the replayed ShapeEnv are unchanged. This PR was authored with the assistance of an AI coding agent. Pull Request resolved: pytorch#193161 Approved by: https://github.com/laithsakka ghstack dependencies: pytorch#193159
…lready knows (pytorch#193416) Every FunctionalTensor wraps an inner tensor by calling _make_wrapper_subclass with the inner tensor's sizes and strides. When the caller does not supply storage_size, that helper computes it with computeStorageNbytes, which walks the dimensions doing symbolic arithmetic - a sub, a mul and an add per dimension - to size a storage that exists only to track aliasing between subclasses; the data pointer is not even valid. The inner tensor already knows its storage size, so pass it. This is the same thing NestedTensor does (torch/nested/_internal/nested_tensor.py passes storage_size=values.untyped_storage().size()), and mirroring elem is if anything the more faithful value: the wrapper is meant to shadow elem, whereas the recomputed number describes only the extent the sizes and strides reach. On a 4d symbolic tensor the call goes from 144us to 21us. Over one model's cold compile with dynamic=True, across 4268 wrapper creations, time in FunctionalTensor.__new__ drops from 1.04s to 0.11s, and about 30k symbolic operations disappear from the compile. End to end, as the union of top-level chromium events, over interleaved pairs: pair 1: baseline 40.26s with storage_size 39.78s pair 2: baseline 40.05s with storage_size 38.16s pair 3: baseline 39.49s with storage_size 38.07s 39.93s -> 38.67s, -3.2%. Sparse inners keep the old path, as they already do for strides and storage_offset. Test plan: ``` python test/inductor/test_torchinductor_dynamic_shapes.py -k cuda python test/test_functionalization.py ``` Failure sets identical to a run with the change reverted (this aarch64 host has pre-existing failures). This PR was authored with the assistance of an AI coding agent. Pull Request resolved: pytorch#193416 Approved by: https://github.com/Skylion007, https://github.com/zou3519 ghstack dependencies: pytorch#193159, pytorch#193161
Stack from ghstack (oldest at bottom):
SymNode.expr already short-circuits ints and sympy Numbers, but sympy.true and
sympy.false are neither, so a SymBool that has settled on a constant goes to
ShapeEnv.replace on every access. Symbol replacement cannot change a constant,
so the call is pure overhead: the version check, the lru lookup and hashing the
expression. Extend the fast path with is_Boolean and is_Atom - the atom test
keeps relationals, which do have symbols to replace, on the general path.
On one inductor compile this drops ShapeEnv.replace from 86904 calls to 61078
(-30%). The wall-clock effect is below the noise floor of the model I measured,
so this is about not doing provably useless work rather than a measured win.
Test plan:
Both match a run with the change stashed.
This PR was authored with the assistance of an AI coding agent.