[inductor] remove redundant view/permute earlier - #170649
Conversation
[ghstack-poisoned]
🔗 Helpful Links🧪 See artifacts and rendered test results at hud.pytorch.org/pr/170649
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 b7a971d with merge base 2708634 ( 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. |
eellison
left a comment
There was a problem hiding this comment.
Should we apply these canonicalization patterns to the patterns generated from register_replacement, as well ?
| score.size(0), score.size(1), score.size(2), score.size(3) | ||
| ) | ||
| return viewd_score2.float().softmax(dim=-1).type_as(query).matmul(value), key, value | ||
| return score.float().softmax(dim=-1).type_as(query).matmul(value), key, value |
There was a problem hiding this comment.
ideally we wouldn't need to change these
I think both should work. But the change to the attn pattern makes them simpler. So I would still prefer changing those patterns. On the other hand, we can apply those canonicalization patterns in the trace function generating patterns as the same time. But I have 2 concerns
I'm open to more discussions. |
2 benefits 1. remove these redundancy earlier so that AutoChunker does not need to handle redundant view pairs 2. some attention patterns add redudant views in the pattern on purpose for matching against the compiled graph. By removing these redundancy in the compiled graph earlier, we can simplify those patterns a bit by not injecting redundant views. test by the existing tests in test_fused_attention.py cc voznesenskym penguinwu EikanWang jgong5 Guobing-Chen XiaobingSuper zhuhaozhe blzheng wenzhe-nrv jiayisunx ipiszy kadeng muchulee8 amjames chauhang aakhundov coconutruben jataylo [ghstack-poisoned]
eellison
left a comment
There was a problem hiding this comment.
Since this runs prior to all post grad, and joint graph passes, can we apply this to user patterns as well ? this would just be added as a pass to register_replacement.
2 benefits 1. remove these redundancy earlier so that AutoChunker does not need to handle redundant view pairs 2. some attention patterns add redudant views in the pattern on purpose for matching against the compiled graph. By removing these redundancy in the compiled graph earlier, we can simplify those patterns a bit by not injecting redundant views. test by the existing tests in test_fused_attention.py cc voznesenskym penguinwu EikanWang jgong5 Guobing-Chen XiaobingSuper zhuhaozhe blzheng wenzhe-nrv jiayisunx ipiszy kadeng muchulee8 amjames chauhang aakhundov coconutruben jataylo [ghstack-poisoned]
|
Starting merge as part of PR stack under #136702 |
2 benefits 1. remove these redundancy earlier so that AutoChunker does not need to handle redundant view pairs 2. some attention patterns add redudant views in the pattern on purpose for matching against the compiled graph. By removing these redundancy in the compiled graph earlier, we can simplify those patterns a bit by not injecting redundant views. test by the existing tests in test_fused_attention.py cc voznesenskym penguinwu EikanWang jgong5 Guobing-Chen XiaobingSuper zhuhaozhe blzheng wenzhe-nrv jiayisunx ipiszy kadeng muchulee8 amjames chauhang aakhundov coconutruben jataylo [ghstack-poisoned]
|
Starting merge as part of PR stack under #136702 |
The AutoChunker defines the following chunking metadata and propagate them thru the subgraphs that we chunk 1. scale_by: The AutoChunker is only enabled if there is a single scalar tangent. To decouple the dependency on tangent for the bwd subgraph so we can compute them in fwd, we pretend the tangent is 1 first and record it in 'scale_by' . This metadata get propagated and when we cancel the chunking effect in the end of bwd subgraph, we apply the scaling. 2. chunk_dim: record which dimension of the tensor get chunked 3. need_sum: if true, the original Tensor is the sum (rather than concat) of each chunked tensors. One important implementation detail is, we need put chunked subgraph in a HOP (use invoke_subgraph here). Otherwise Inductor fuse across these subgraphs and results in no peak memory saving. Here are some early benchmarking result on GPT2. - 64 chunks: - final 19 iters avg: 242.550ms - peak memory consumption: 12603 MiB - 32 chunks: - final 19 iters avg: 206.180ms - peak memory consumption: 12880 MiB - 16 chunks - final 19 iters avg: 196.997ms - peak memory consumption: 13267 MiB - 8 chunks - final 19 iters avg: 194.924ms - peak memory consumption: 14049 MiB With 64 chunks, our peak memory is smaller than llm.c's 13.4GB. I also tried the AutoChunker on PT2 OSS benchmarks to verify the numerical. By default our accuracy test picks a very small batch size. This makes AutoChunker get skipped. I force batch_size to be 16 for BertForMaskedLM to trigger the AutoChunker and verified the numerical correctness. Pull Request resolved: #136702 Approved by: https://github.com/jansel ghstack dependencies: #170649
|
@pytorchbot revert -m 'Sorry for reverting your change but this seems to break vLLM CI' -c nosignal |
|
@pytorchbot successfully started a revert job. Check the current status here. |
This reverts commit 8e09f22. Reverted #170649 on behalf of https://github.com/huydhn due to Sorry for reverting your change but this seems to break vLLM CI ([comment](#170649 (comment)))
|
@shunting314 your PR has been successfully reverted. |
Applying these to user patterns (in fwd_only) cause duplicate patterns registered from vllm. If we really want to do this, we'd need to change vllm first. Repro for reference: vllm pinned commit: 7c73ceb5812ace65e0d1b6ada3622b8b9f0400c0 |
2 benefits 1. remove these redundancy earlier so that AutoChunker does not need to handle redundant view pairs 2. some attention patterns add redudant views in the pattern on purpose for matching against the compiled graph. By removing these redundancy in the compiled graph earlier, we can simplify those patterns a bit by not injecting redundant views. test by the existing tests in test_fused_attention.py cc voznesenskym penguinwu EikanWang jgong5 Guobing-Chen XiaobingSuper zhuhaozhe blzheng wenzhe-nrv jiayisunx ipiszy kadeng muchulee8 amjames chauhang aakhundov coconutruben jataylo [ghstack-poisoned]
|
Starting merge as part of PR stack under #136702 |
The AutoChunker defines the following chunking metadata and propagate them thru the subgraphs that we chunk 1. scale_by: The AutoChunker is only enabled if there is a single scalar tangent. To decouple the dependency on tangent for the bwd subgraph so we can compute them in fwd, we pretend the tangent is 1 first and record it in 'scale_by' . This metadata get propagated and when we cancel the chunking effect in the end of bwd subgraph, we apply the scaling. 2. chunk_dim: record which dimension of the tensor get chunked 3. need_sum: if true, the original Tensor is the sum (rather than concat) of each chunked tensors. One important implementation detail is, we need put chunked subgraph in a HOP (use invoke_subgraph here). Otherwise Inductor fuse across these subgraphs and results in no peak memory saving. Here are some early benchmarking result on GPT2. - 64 chunks: - final 19 iters avg: 242.550ms - peak memory consumption: 12603 MiB - 32 chunks: - final 19 iters avg: 206.180ms - peak memory consumption: 12880 MiB - 16 chunks - final 19 iters avg: 196.997ms - peak memory consumption: 13267 MiB - 8 chunks - final 19 iters avg: 194.924ms - peak memory consumption: 14049 MiB With 64 chunks, our peak memory is smaller than llm.c's 13.4GB. I also tried the AutoChunker on PT2 OSS benchmarks to verify the numerical. By default our accuracy test picks a very small batch size. This makes AutoChunker get skipped. I force batch_size to be 16 for BertForMaskedLM to trigger the AutoChunker and verified the numerical correctness. Pull Request resolved: #136702 Approved by: https://github.com/jansel ghstack dependencies: #170649
2 benefits 1. remove these redundancy earlier so that AutoChunker does not need to handle redundant view pairs 2. some attention patterns add redudant views in the pattern on purpose for matching against the compiled graph. By removing these redundancy in the compiled graph earlier, we can simplify those patterns a bit by not injecting redundant views. test by the existing tests in test_fused_attention.py Pull Request resolved: pytorch#170649 Approved by: https://github.com/eellison, https://github.com/jansel
The AutoChunker defines the following chunking metadata and propagate them thru the subgraphs that we chunk 1. scale_by: The AutoChunker is only enabled if there is a single scalar tangent. To decouple the dependency on tangent for the bwd subgraph so we can compute them in fwd, we pretend the tangent is 1 first and record it in 'scale_by' . This metadata get propagated and when we cancel the chunking effect in the end of bwd subgraph, we apply the scaling. 2. chunk_dim: record which dimension of the tensor get chunked 3. need_sum: if true, the original Tensor is the sum (rather than concat) of each chunked tensors. One important implementation detail is, we need put chunked subgraph in a HOP (use invoke_subgraph here). Otherwise Inductor fuse across these subgraphs and results in no peak memory saving. Here are some early benchmarking result on GPT2. - 64 chunks: - final 19 iters avg: 242.550ms - peak memory consumption: 12603 MiB - 32 chunks: - final 19 iters avg: 206.180ms - peak memory consumption: 12880 MiB - 16 chunks - final 19 iters avg: 196.997ms - peak memory consumption: 13267 MiB - 8 chunks - final 19 iters avg: 194.924ms - peak memory consumption: 14049 MiB With 64 chunks, our peak memory is smaller than llm.c's 13.4GB. I also tried the AutoChunker on PT2 OSS benchmarks to verify the numerical. By default our accuracy test picks a very small batch size. This makes AutoChunker get skipped. I force batch_size to be 16 for BertForMaskedLM to trigger the AutoChunker and verified the numerical correctness. Pull Request resolved: pytorch#136702 Approved by: https://github.com/jansel ghstack dependencies: pytorch#170649
)" This reverts commit 8e09f22. Reverted pytorch#170649 on behalf of https://github.com/huydhn due to Sorry for reverting your change but this seems to break vLLM CI ([comment](pytorch#170649 (comment)))
2 benefits 1. remove these redundancy earlier so that AutoChunker does not need to handle redundant view pairs 2. some attention patterns add redudant views in the pattern on purpose for matching against the compiled graph. By removing these redundancy in the compiled graph earlier, we can simplify those patterns a bit by not injecting redundant views. test by the existing tests in test_fused_attention.py Pull Request resolved: pytorch#170649 Approved by: https://github.com/eellison, https://github.com/jansel
The AutoChunker defines the following chunking metadata and propagate them thru the subgraphs that we chunk 1. scale_by: The AutoChunker is only enabled if there is a single scalar tangent. To decouple the dependency on tangent for the bwd subgraph so we can compute them in fwd, we pretend the tangent is 1 first and record it in 'scale_by' . This metadata get propagated and when we cancel the chunking effect in the end of bwd subgraph, we apply the scaling. 2. chunk_dim: record which dimension of the tensor get chunked 3. need_sum: if true, the original Tensor is the sum (rather than concat) of each chunked tensors. One important implementation detail is, we need put chunked subgraph in a HOP (use invoke_subgraph here). Otherwise Inductor fuse across these subgraphs and results in no peak memory saving. Here are some early benchmarking result on GPT2. - 64 chunks: - final 19 iters avg: 242.550ms - peak memory consumption: 12603 MiB - 32 chunks: - final 19 iters avg: 206.180ms - peak memory consumption: 12880 MiB - 16 chunks - final 19 iters avg: 196.997ms - peak memory consumption: 13267 MiB - 8 chunks - final 19 iters avg: 194.924ms - peak memory consumption: 14049 MiB With 64 chunks, our peak memory is smaller than llm.c's 13.4GB. I also tried the AutoChunker on PT2 OSS benchmarks to verify the numerical. By default our accuracy test picks a very small batch size. This makes AutoChunker get skipped. I force batch_size to be 16 for BertForMaskedLM to trigger the AutoChunker and verified the numerical correctness. Pull Request resolved: pytorch#136702 Approved by: https://github.com/jansel ghstack dependencies: pytorch#170649
ghstack-source-id: 6c50026 Pull Request resolved: pytorch/pytorch#170649
Stack from ghstack (oldest at bottom):
2 benefits
test by the existing tests in test_fused_attention.py
cc @voznesenskym @penguinwu @EikanWang @jgong5 @Guobing-Chen @XiaobingSuper @zhuhaozhe @blzheng @wenzhe-nrv @jiayisunx @ipiszy @kadeng @muchulee8 @amjames @chauhang @aakhundov @coconutruben @jataylo