Repository navigation
[inductor] Stop recomputing per-node fusion inputs for every candidate pair - #194011
anijain2305 wants to merge 7 commits into
Conversation
[ghstack-poisoned]
🔗 Helpful Links🧪 See artifacts and rendered test results at hud.pytorch.org/pr/194011
Note: Links to docs will display an error until the docs builds have been completed. ✅ You can merge normally! (1 Unrelated Failure)As of commit c82ca36 with merge base 64b9efd ( BROKEN TRUNK - The following job failed but were present on the merge base:👉 Rebase onto the `viable/strict` branch to avoid these failures
This comment was automatically generated by Dr. CI and updates every 15 minutes. |
[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
|
From claude, I think there is only one minor issue ,this doesn't repro the historical issue: |
[ghstack-poisoned]
…e pair The fusion search asks O(pairs) questions, but three of the quantities it consults depend only on a single buffer or a single node, and each was recomputed from scratch on every pair: - can_fusion_increase_peak_memory rescanned every read of both nodes looking for single-user buffers, then built each buffer's reuse key, which runs compute_required_storage_length and sympy_str. - _score_fusion_memory_by_buffer_overlap summed read sizes over all deps of both nodes four separate times, hashing every Dep to reach the dep_size_hint cache. - score_fusion_memory materialized `reads | writes` for both nodes. Fusion keeps merging nodes into larger ones, so paying O(deps) per pair to rebuild that union is what turned the search quadratic in practice. Reuse keys are now memoized by buffer name on the Scheduler. Per-node read bytes are memoized on the node, alongside the other read_writes-derived caches and invalidated with them. The union is gone: the common-dep scan walks whichever node has fewer deps and tests membership against the other node's raw read and write sets, which preserves the de-duplication the union provided. Only the key is memoized, not whether the buffer qualifies. `users` shrinks while the fusion search runs - user-defined triton epilogue fusion drops one - so a cached "has exactly one user" would go stale mid-search and this heuristic would then reason from a buffer that is no longer reusable. That cannot produce wrong tensors, but it can accept or reject a fusion it should not, which changes the generated kernels. So eligibility is rechecked on every call and only the key, a pure function of the buffer's layout, is kept. Rechecking costs nothing measurable: 15.5s either way on the repro below. The key survives the other way a name can go stale. Finalizing a multi-template buffer swaps out the buffer a name refers to, but it copies the layout across, and the key is a function of layout, device, dtype and name. Keying the cache on the buffer object instead is not an option: an ir.MultiOutput is not hashable. Reuse keys are keyed by buffer name rather than by scheduler node for a performance reason too. Keying by node was tried first and was worse than doing nothing, because every newly fused node then rebuilds keys for all of its reads and the sharing across nodes that read the same buffer is lost. On the earlier attempts in this area: #127060 eagerly precomputed every node's dependency metadata and #127326 reverted it because sparse-overlap models regressed. This computes its per-node data lazily, only for the nodes a candidate pair actually names, so it is not that shape. BaseSchedulerNode crossed the docstring linter's grandfathered size limit, so it also gains the class docstring it was missing. Measured against the parent commit. The repro is three foreach calls over a list of N graph inputs - _foreach_norm(xs), then _foreach_div_(xs, norms) dividing in place by that per-tensor norm list, then a second in-place mutation of the same list - which is the shape of an internal optimizer graph that spends nearly all of its compile in the fusion search. Cold compile, autotuning off, median of three: parent this change N=128 18.5s 8.0s fused_nodes 12.1s 1.8s N=275 57.5s 16.0s fused_nodes 46.8s 5.1s The search is superlinear before and roughly linear after: 2.15x the inputs costs 3.1x the time on the parent and 2.0x with this change. Test Plan: ``` python -m pytest -q -p no:randomly test/inductor/test_perf.py \ test/inductor/test_memory.py test/inductor/test_inductor_scheduler.py \ test/inductor/test_fusion_regions.py test/inductor/test_combo_kernels.py \ test/inductor/test_foreach.py test/inductor/test_benchmark_fusion.py # 922 passed, 5 failed python -m pytest -q -p no:randomly test/inductor/test_torchinductor.py # 2532 passed, 55 failed python -m pytest -q -p no:randomly test/inductor/test_triton_kernels.py \ -k "epilogue or fusion" # 16 passed ``` None of those failures are from this change. The four TestOperatorReorderForPeakMemory ones fail identically with the change reverted. test_benchmark_on_non_zero_device passes on its own and passes when its whole file runs, so it is cross-file order dependence in that batch. For the 55, running the same 55 tests as a set gives 49 failed and 6 passed both with this change and on the parent, so the set is pre-existing and the difference in the full-file run is ordering. The triton_kernels epilogue and fusion tests are the ones that exercise the mutable-users path described above. This commit was authored with the assistance of an AI agent (Claude Code). ghstack-source-id: 93732d8 Pull-Request: #194011
|
@pytorchbot merge |
Merge startedYour 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 |
Merge failedReason: 1 jobs have failed, first few of them are: inductor / unit-test / linux-jammy-cpu-py3.12-gcc11-inductor-triton-cpu / test (inductor-triton-cpu, 1, 1, mt-l-x86iamx-32-128) Details for Dev Infra teamRaised by workflow job |
|
@pytorchbot merge |
Merge startedYour 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 |
…e pair (pytorch#194011) The fusion search asks O(pairs) questions, but three of the quantities it consults depend only on a single buffer or a single node, and each was recomputed from scratch on every pair: - can_fusion_increase_peak_memory rescanned every read of both nodes looking for single-user buffers, then built each buffer's reuse key, which runs compute_required_storage_length and sympy_str. - _score_fusion_memory_by_buffer_overlap summed read sizes over all deps of both nodes four separate times, hashing every Dep to reach the dep_size_hint cache. - score_fusion_memory materialized `reads | writes` for both nodes. Fusion keeps merging nodes into larger ones, so paying O(deps) per pair to rebuild that union is what turned the search quadratic in practice. Reuse keys are now memoized by buffer name on the Scheduler. Per-node read bytes are memoized on the node, alongside the other read_writes-derived caches and invalidated with them. The union is gone: the common-dep scan walks whichever node has fewer deps and tests membership against the other node's raw read and write sets, which preserves the de-duplication the union provided. Only the key is memoized, not whether the buffer qualifies. `users` shrinks while the fusion search runs - user-defined triton epilogue fusion drops one - so a cached "has exactly one user" would go stale mid-search and this heuristic would then reason from a buffer that is no longer reusable. That cannot produce wrong tensors, but it can accept or reject a fusion it should not, which changes the generated kernels. So eligibility is rechecked on every call and only the key, a pure function of the buffer's layout, is kept. Rechecking costs nothing measurable: 15.5s either way on the repro below. The key survives the other way a name can go stale. Finalizing a multi-template buffer swaps out the buffer a name refers to, but it copies the layout across, and the key is a function of layout, device, dtype and name. Keying the cache on the buffer object instead is not an option: an ir.MultiOutput is not hashable. Reuse keys are keyed by buffer name rather than by scheduler node for a performance reason too. Keying by node was tried first and was worse than doing nothing, because every newly fused node then rebuilds keys for all of its reads and the sharing across nodes that read the same buffer is lost. On the earlier attempts in this area: pytorch#127060 eagerly precomputed every node's dependency metadata and pytorch#127326 reverted it because sparse-overlap models regressed. This computes its per-node data lazily, only for the nodes a candidate pair actually names, so it is not that shape. BaseSchedulerNode crossed the docstring linter's grandfathered size limit, so it also gains the class docstring it was missing. Measured against the parent commit. The repro is three foreach calls over a list of N graph inputs - _foreach_norm(xs), then _foreach_div_(xs, norms) dividing in place by that per-tensor norm list, then a second in-place mutation of the same list - which is the shape of an internal optimizer graph that spends nearly all of its compile in the fusion search. Cold compile, autotuning off, median of three: parent this change N=128 18.5s 8.0s fused_nodes 12.1s 1.8s N=275 57.5s 16.0s fused_nodes 46.8s 5.1s The search is superlinear before and roughly linear after: 2.15x the inputs costs 3.1x the time on the parent and 2.0x with this change. Test Plan: ``` python -m pytest -q -p no:randomly test/inductor/test_perf.py \ test/inductor/test_memory.py test/inductor/test_inductor_scheduler.py \ test/inductor/test_fusion_regions.py test/inductor/test_combo_kernels.py \ test/inductor/test_foreach.py test/inductor/test_benchmark_fusion.py # 922 passed, 5 failed python -m pytest -q -p no:randomly test/inductor/test_torchinductor.py # 2532 passed, 55 failed python -m pytest -q -p no:randomly test/inductor/test_triton_kernels.py \ -k "epilogue or fusion" # 16 passed ``` None of those failures are from this change. The four TestOperatorReorderForPeakMemory ones fail identically with the change reverted. test_benchmark_on_non_zero_device passes on its own and passes when its whole file runs, so it is cross-file order dependence in that batch. For the 55, running the same 55 tests as a set gives 49 failed and 6 passed both with this change and on the parent, so the set is pre-existing and the difference in the full-file run is ordering. The triton_kernels epilogue and fusion tests are the ones that exercise the mutable-users path described above. This commit was authored with the assistance of an AI agent (Claude Code). Pull Request resolved: pytorch#194011 Approved by: https://github.com/mlazos
Stack from ghstack (oldest at bottom):
The fusion search asks O(pairs) questions, but three of the quantities it
consults depend only on a single buffer or a single node, and each was recomputed
from scratch on every pair:
single-user buffers, then built each buffer's reuse key, which runs
compute_required_storage_length and sympy_str.
nodes four separate times, hashing every Dep to reach the dep_size_hint cache.
reads | writesfor both nodes. Fusion keepsmerging nodes into larger ones, so paying O(deps) per pair to rebuild that
union is what turned the search quadratic in practice.
Reuse keys are now memoized by buffer name on the Scheduler. Per-node read bytes
are memoized on the node, alongside the other read_writes-derived caches and
invalidated with them. The union is gone: the common-dep scan walks whichever
node has fewer deps and tests membership against the other node's raw read and
write sets, which preserves the de-duplication the union provided.
Only the key is memoized, not whether the buffer qualifies.
usersshrinks whilethe fusion search runs - user-defined triton epilogue fusion drops one - so a
cached "has exactly one user" would go stale mid-search and this heuristic would
then reason from a buffer that is no longer reusable. That cannot produce wrong
tensors, but it can accept or reject a fusion it should not, which changes the
generated kernels. So eligibility is rechecked on every call and only the key, a
pure function of the buffer's layout, is kept. Rechecking costs nothing
measurable: 15.5s either way on the repro below.
The key survives the other way a name can go stale. Finalizing a multi-template
buffer swaps out the buffer a name refers to, but it copies the layout across,
and the key is a function of layout, device, dtype and name. Keying the cache on
the buffer object instead is not an option: an ir.MultiOutput is not hashable.
Reuse keys are keyed by buffer name rather than by scheduler node for a
performance reason too. Keying by node was tried first and was worse than doing
nothing, because every newly fused node then rebuilds keys for all of its reads
and the sharing across nodes that read the same buffer is lost.
On the earlier attempts in this area: #127060 eagerly precomputed every node's
dependency metadata and #127326 reverted it because sparse-overlap models
regressed. This computes its per-node data lazily, only for the nodes a candidate
pair actually names, so it is not that shape.
BaseSchedulerNode crossed the docstring linter's grandfathered size limit, so it
also gains the class docstring it was missing.
Measured against the parent commit. The repro is three foreach calls over a list
of N graph inputs - _foreach_norm(xs), then foreach_div(xs, norms) dividing in
place by that per-tensor norm list, then a second in-place mutation of the same
list - which is the shape of an internal optimizer graph that spends nearly all
of its compile in the fusion search. Cold compile, autotuning off, median of
three:
The search is superlinear before and roughly linear after: 2.15x the inputs costs
3.1x the time on the parent and 2.0x with this change.
Test Plan:
None of those failures are from this change. The four
TestOperatorReorderForPeakMemory ones fail identically with the change reverted.
test_benchmark_on_non_zero_device passes on its own and passes when its whole
file runs, so it is cross-file order dependence in that batch. For the 55,
running the same 55 tests as a set gives 49 failed and 6 passed both with this
change and on the parent, so the set is pre-existing and the difference in the
full-file run is ordering.
The triton_kernels epilogue and fusion tests are the ones that exercise the
mutable-users path described above.
This commit was authored with the assistance of an AI agent (Claude Code).
cc @voznesenskym @penguinwu @EikanWang @jgong5 @Guobing-Chen @XiaobingSuper @zhuhaozhe @blzheng @wenzhe-nrv @jiayisunx @ipiszy @kadeng @muchulee8 @amjames @chauhang @aakhundov @coconutruben @jataylo