Repository navigation
Conversation
🔗 Helpful Links🧪 See artifacts and rendered test results at hud.pytorch.org/pr/182986
Note: Links to docs will display an error until the docs builds have been completed. ✅ No FailuresAs of commit e58930b with merge base 0893b64 ( This comment was automatically generated by Dr. CI and updates every 15 minutes. |
|
@njriasan has exported this pull request. If you are a Meta employee, you can view the originating Diff in D100024904. |
| } | ||
|
|
||
| static bool use_inner_tree_reduction() { | ||
| static const bool disabled = (std::getenv("PYTORCH_SUM_LEGACY") != nullptr); |
There was a problem hiding this comment.
This will default to False after CI runs.
cbe84a5 to
472f445
Compare
…ytorch#182986) Summary: Adds support for an inner_tree sum reduction algorithm with multiple launcher strategies. An inner tree reduction is a tree reduction where we reduce values based on their index with all bits match except for i at stage i. For example to reduce 4 elements it will be: ``` result | ┌─────────┴──────────┐ │ │ ┌───┴───┐ ┌───┴───┐ │ │ │ │ x[0] x[1] x[2] x[3] ``` This pattern continues/composes for increasing powers of 2 until reaching the final result. By rewriting the reduction algorithm in this way, we should be able to maintain strong performance but have a reduction algorithm that is much more robust to subtle layout differences or warp count changes, better enabling bitwise equivalence between eager and compile. In addition, our tree is not fully optimal for large sizes. From experimentation once we exceed 8192 on all data types the perform drops off due to increased register usage/spilling to maintain our tree. This then extends the eager algorithm to do a *persistent reduction* based on kMaxTreeThreshold and then does a linear sum. Any user written or inductor generated kernel can match this pattern and maintain bitwise equivalence. This then creates 4 kernel launch patterns: - inner_tree_reduction_kernel: Main kernel to reduce on a whole tree. - inner_tree_reduction_looped_kernel: Persistent reduction for when N is slightly greater than 8192 so the launch overhead of additional kernels would be too large. - inner_tree_reduction_multirow_kernel: Variation of inner_tree_reduction_kernel that processes multiple rows as once. Used for small N values. - two kernel reduction: Persistent reduction using two separate kernels, 1 for each tree of 8192 and one to sum the results. This is helpful because we can assign warps per tree size, including the final reduction and for large N values we don't need to round up to multiple of 8192 (we round the last result to its nearest power of 2). In the future I expect future optimizations to be largely launch grid oriented and still consist of the same core "tree-reduction" algorithm. Note: There are still performance cases that need investigating. Test Plan: Correctness: `buck test fbcode//caffe2/test:test_reductions -- TestTritonStyleSum` Correctness compared to Triton inner-tree: `buck run fbcode//scripts/njriasan/bitwise_equiv:sum_kernel_benchmark -- accuracy` Peformance testing: Command: `CUDA_VISIBLE_DEVICES=5 TRITON_REDUCE_STRATEGY=OLD_EAGER buck run fbcode//scripts/njriasan/bitwise_equiv:sum_kernel_benchmark -- benchmark` and `CUDA_VISIBLE_DEVICES=5 TRITON_REDUCE_STRATEGY=EAGER buck run fbcode//scripts/njriasan/bitwise_equiv:sum_kernel_benchmark -- benchmark` Results on a H100 server CSV of results: https://gist.github.com/njriasan/569d01e408032b80f94ae6de84ea4cb1 Claude Analysis: https://gist.github.com/njriasan/bb5e4f2194be1c9ae2e3b90577b1f912 Based on these insights the most important shape to optimize next is the M=1 case, probably needing num_splits. High-level graph without nuance about how important each combination is or overall time. {F1989532624} Differential Revision: D100024904
472f445 to
a7f7150
Compare
|
@claude explain those CI failures, are they related? |
|
Claude finished @jansel's task in 1m 43s —— View job CI Failure Analysis
TL;DR: Two of the three failures are caused by this PR. The ONNX failure is unrelated. 1. Lint failure (
|
a7f7150 to
1252388
Compare
…ytorch#182986) Summary: Adds the triton-style inner tree reduction kernel for sum in the caffe2/ATen CUDA backend, including partitioned launch variants, tests, and the output/input offset handling from D104676317 so TensorIterator output strides and strided outer input rows are preserved. Test Plan: arc lint -a fbcode/caffe2/aten/src/ATen/native/cuda/ReduceSumProdKernel.cu xplat/caffe2/aten/src/ATen/native/cuda/ReduceSumProdKernel.cu fbcode/caffe2/test/test_reductions.py xplat/caffe2/test/test_reductions.py buck2 test fbcode//caffe2/test:torch_cuda_cuda -- TestInnerTreeSum Differential Revision: D100024904
|
@claude explain those CI failures, are they related? |
|
Claude finished @jansel's task in 1m 31s —— View job CI Failure Analysis
Yes, both failures are caused by this PR. They are independent issues (one lint, one compile error) but both stem from code added in this commit. 1. ROCm/HIP Build Failure —
|
| Failure | Related? | Severity |
|---|---|---|
ROCm/HIP constexpr error |
Yes — caused by this PR | Build-breaking on ROCm |
Lint torch/csrc/cuda/Module.cpp |
Yes — caused by this PR | Lint warning |
1252388 to
8fe1983
Compare
…ytorch#182986) Summary: Adds the triton-style inner tree reduction kernel for sum in the caffe2/ATen CUDA backend, including partitioned launch variants, tests, and the output/input offset handling from D104676317 so TensorIterator output strides and strided outer input rows are preserved. Test Plan: arc lint -a fbcode/caffe2/aten/src/ATen/native/cuda/ReduceSumProdKernel.cu xplat/caffe2/aten/src/ATen/native/cuda/ReduceSumProdKernel.cu fbcode/caffe2/test/test_reductions.py xplat/caffe2/test/test_reductions.py buck2 test fbcode//caffe2/test:torch_cuda_cuda -- TestInnerTreeSum Differential Revision: D100024904
…ytorch#182986) Summary: Adds the triton-style inner tree reduction kernel for sum in the caffe2/ATen CUDA backend, including partitioned launch variants, tests, and the output/input offset handling from D104676317 so TensorIterator output strides and strided outer input rows are preserved. Test Plan: arc lint -a fbcode/caffe2/aten/src/ATen/native/cuda/ReduceSumProdKernel.cu xplat/caffe2/aten/src/ATen/native/cuda/ReduceSumProdKernel.cu fbcode/caffe2/test/test_reductions.py xplat/caffe2/test/test_reductions.py buck2 test fbcode//caffe2/test:torch_cuda_cuda -- TestInnerTreeSum Differential Revision: D100024904
8fe1983 to
04e14aa
Compare
|
@eellison @jansel @ngimel This is moved to CuteDSL and working now. The only remaining feedback to apply should be the decision to reduce "Compile-cache pressure". Other than the startup cost of hitting a new path with an initial compilation, can this have side-effects on other kernels (e.g. a max cache side) or will this be isolated behind the flag. I will turn the flag off by default now that I have confirmed CI is working. |
|
@claude inspect the cuteDSL kernel code in this PR and suggest areas where existing cuteDSL abstractions and techniques could be used - pay special attention to vectorized loads / stores |
|
I would pay special attention to the host-side overhead of this override, especially with the 2-kernel versions of the code - it can easily make DSL code noticeably slower end-to-end even if the underlying kernel is faster. I do also have a prototype implementation of all reduction operators in aten, with work in progress on pointwise -- it might be worth comparing notes to see if there's some common ground / kernels we can use. |
|
Claude finished @slayton58's task in 2m 57s —— View job CuTeDSL abstraction review — focus on vectorized loads/storesI read both kernel modules ( The kernels are a careful, deliberately literal port of the CUDA originals, and the bitwise-equivalence constraint legitimately forces some hand-rolling (see "what should stay hand-rolled" below). But the load path is fully scalar today and is the one place where existing abstractions would help without touching reduction order. 🔴 The biggest gap: loads are scalar, not vectorized
for i in cutlass.range_constexpr(vec_size):
off = base + Int32(i)
v = zero
if off < Int32(N):
v = acc(mIn[row, off]) # one scalar LDG per element
vals.append(v)The reduced dim is contiguous (the predicate enforces
Why this is bitwise-safe: vectorizing the load changes only the memory transaction width — the register values handed to the reduction tree are identical to the scalar path. The accumulation DAG ( 🟠 Per-element bounds checks → vector predicationThe Important alignment caveat: a 128-bit load requires each row's base to be 16-byte aligned. The inner stride is 1, but the outer row stride ( 🟡 Two-kernel accumulate: the partials read is also scalar
s = mPartials[base]
for b in cutlass.range(1, num_batches): # runtime loop
s = s + mPartials[base + b]The partials for a row are contiguous ( ✅ What should stay hand-rolled (don't "fix" these)
NetThe reduction/shuffle hand-rolling is justified by the bitwise goal and should stay. The clear, bitwise-neutral win is vectorizing the gmem load in |
|
claude suggestions look reasonable to me - I don't remember seeing benchmarks, but aten's reductions are pretty well optimized and you'll need heavy vectorization to be competitive |
6bdf433 to
fd91224
Compare
…ytorch#182986) Summary: Adds the triton-style inner tree reduction kernel for sum in the caffe2/ATen CUDA backend, including partitioned launch variants, tests, and the output/input offset handling from D104676317 so TensorIterator output strides and strided outer input rows are preserved. Test Plan: arc lint -a fbcode/caffe2/aten/src/ATen/native/cuda/ReduceSumProdKernel.cu xplat/caffe2/aten/src/ATen/native/cuda/ReduceSumProdKernel.cu fbcode/caffe2/test/test_reductions.py xplat/caffe2/test/test_reductions.py buck2 test fbcode//caffe2/test:torch_cuda_cuda -- TestInnerTreeSum Differential Revision: D100024904
fd91224 to
04d64ef
Compare
…ytorch#182986) Summary: Adds the triton-style inner tree reduction kernel for sum in the caffe2/ATen CUDA backend, including partitioned launch variants, tests, and the output/input offset handling from D104676317 so TensorIterator output strides and strided outer input rows are preserved. Test Plan: arc lint -a fbcode/caffe2/aten/src/ATen/native/cuda/ReduceSumProdKernel.cu xplat/caffe2/aten/src/ATen/native/cuda/ReduceSumProdKernel.cu fbcode/caffe2/test/test_reductions.py xplat/caffe2/test/test_reductions.py buck2 test fbcode//caffe2/test:torch_cuda_cuda -- TestInnerTreeSum Differential Revision: D100024904
04d64ef to
9dd1544
Compare
…ytorch#182986) Summary: Adds the triton-style inner tree reduction kernel for sum in the caffe2/ATen CUDA backend, including partitioned launch variants, tests, and the output/input offset handling from D104676317 so TensorIterator output strides and strided outer input rows are preserved. Test Plan: arc lint -a fbcode/caffe2/aten/src/ATen/native/cuda/ReduceSumProdKernel.cu xplat/caffe2/aten/src/ATen/native/cuda/ReduceSumProdKernel.cu fbcode/caffe2/test/test_reductions.py xplat/caffe2/test/test_reductions.py buck2 test fbcode//caffe2/test:torch_cuda_cuda -- TestInnerTreeSum Differential Revision: D100024904
9dd1544 to
bec7d6d
Compare
…ytorch#182986) Summary: Adds the triton-style inner tree reduction kernel for sum in the caffe2/ATen CUDA backend, including partitioned launch variants, tests, and the output/input offset handling from D104676317 so TensorIterator output strides and strided outer input rows are preserved. Test Plan: arc lint -a fbcode/caffe2/aten/src/ATen/native/cuda/ReduceSumProdKernel.cu xplat/caffe2/aten/src/ATen/native/cuda/ReduceSumProdKernel.cu fbcode/caffe2/test/test_reductions.py xplat/caffe2/test/test_reductions.py buck2 test fbcode//caffe2/test:torch_cuda_cuda -- TestInnerTreeSum Differential Revision: D100024904
|
@pytorchbot merge |
Merge failedReason: This PR has internal changes and must be landed via Phabricator! Please try reimporting/rexporting the PR! Details for Dev Infra teamRaised by workflow job |
|
@pytorchbot merge |
1 similar comment
|
@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 |
|
The merge job was canceled or timed out. This most often happen if two merge requests were issued for the same PR, or if merge job was waiting for more than 6 hours for tests to finish. In later case, please do not hesitate to reissue the merge command |
|
@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 |


Summary:
Adds the triton-style inner tree reduction kernel for sum in the caffe2/ATen CUDA backend, including partitioned launch variants, tests, and the output/input offset handling from D104676317 so TensorIterator output strides and strided outer input rows are preserved.
Test Plan:
arc lint -a fbcode/caffe2/aten/src/ATen/native/cuda/ReduceSumProdKernel.cu xplat/caffe2/aten/src/ATen/native/cuda/ReduceSumProdKernel.cu fbcode/caffe2/test/test_reductions.py xplat/caffe2/test/test_reductions.py
buck2 test fbcode//caffe2/test:torch_cuda_cuda -- TestInnerTreeSum
Differential Revision: D100024904