Repository navigation
Conversation
[ghstack-poisoned]
PR Status: Approved 🚀Please fix all CI failures and trigger merge by commenting "@pytorchbot merge". ✅ AUTOMATED REVIEW - OUTDATED (earlier commit) - Ready for human review:
Findings (3): Reviewed commit: 🔗 Helpful Links🧪 See artifacts and rendered test results at hud.pytorch.org/pr/204752
Note: Links to docs will display an error until the docs builds have been completed. ✅ No FailuresAs of commit d9278e2 with merge base f81a081 ( 🟡 GREEN LIGHT - OUTDATED (earlier commit) - PR requires human review:
reason: This comment was automatically generated by Dr. CI and updates every 15 minutes. |
[ghstack-poisoned]
fsdp::chunk_cat_mixed_dtype copies a reduce-scatter group with mixed gradient dtypes in one fused kernel: one launch copies the fp32 gradients and one launch casts the bf16 or fp16 gradients into the fp32 reduce-scatter input. The cast launch converted one element per thread per iteration with 2-byte loads and 4-byte stores, and the grid was sized for 16 output bytes per thread. It reached 1.7 TB/s on H100. The cast branch of copy_chunk_with_pad in torch/csrc/distributed/fsdp/ChunkCat.cu now uses the same head / aligned body / tail split as the copy path. The head makes dst 16-byte aligned. Each body iteration (unit) loads 8 source elements with the widest load the source alignment allows (16, 8, 4, or 2 bytes, through the read-only cache), converts them with the same c10 cast as before, and writes two 16-byte stores. Padding past the source is written as zeros in the same stores. cast_bytes_per_block sizes the cast grid: each thread takes one unit up to three waves of resident threads, two up to six waves, and so on, up to four. On one GB200, with 8 chunks of 16 or 128 bf16 inputs into fp32 (1 MB to 4 GB of output), a fixed one unit per thread is 11-17% slower than this at 64 MB and 46-53% slower from 1 GB up, and a fixed four units per thread is up to 17% slower at 16 MB. The heuristic is within 3.5% of the best fixed choice at every size. Only the FSDP kernel changes. Uniform groups go through _chunk_cat, whose ATen kernel has the same cast loop; changing it is left to a separate PR. The output is bitwise identical, including padding, all source and destination alignments, odd sizes, and both bf16 and fp16. The tests compare against _chunk_cat of the inputs cast to fp32 first, which runs the same-dtype copy kernel. test_numerics_cast_alignments covers all four load widths, every head length of the padded chunk, padded units and tails, an all-padding chunk, and chunks that span several blocks. test_numerics_cast_several_units_per_thread covers an input large enough for four units per thread on GPUs with up to 304 SMs. Kernel time from the profiler, FSDP2 copy-in, bf16 into fp32, old -> new: | | H100 | GB300 | GB200 | |---|---|---|---| | 285M elements, 64 chunks | 0.955 -> 0.841 ms (1.14x) | 0.709 -> 0.266 ms (2.66x) | 0.721 -> 0.260 ms (2.77x) | | 201M elements, 1 chunk | 0.710 -> 0.585 ms (1.21x) | 0.544 -> 0.180 ms (3.02x) | 0.552 -> 0.181 ms (3.05x) | The GB300 numbers come from a standalone build of the old and new kernels. Test Plan: ``` python -m pytest test/distributed/_composable/fsdp/test_fully_shard_comm.py -k "ChunkCatMixedDtype or NativeCollectiveCopy or PythonCustomOp" python -m pytest test/distributed/_composable/fsdp/test_fully_shard_comm.py test/distributed/_composable/fsdp/test_fully_shard_mixed_precision.py ``` On 2 GB200 GPUs, the first command passes 15 of 15 and the second passes 136 of 137. The one failure, test_fully_shard_alloc_from_pg, expects NCCL to register no buffer before allocation from the process group is enabled, but NCCL 2.30.7 registers a 2 MB buffer. This change does not touch that path. This change was written with an AI assistant (Claude). ghstack-source-id: e0fd5cd Pull-Request: #204752
[ghstack-poisoned]
|
@claude review this PR |
|
Claude encountered an error after 7s —— View job I'll analyze this and get back to you. |
|
@claude review this PR |
|
Claude encountered an error after 20s —— View job I'll analyze this and get back to you. |
|
@claude consdier A100, H100 and blackwell, amd/hip/rocm. whether this PR is risky and could break existing callsites |
|
Claude finished @weifengpy's task in 1m 37s —— View job I'd rate this PR low risk on A100, H100, Blackwell and ROCm. It adds no inline PTX or arch-specific code, the output is meant to be bitwise identical, and the only callsite is FSDP2's reduce-scatter copy-in for groups that mix bf16/fp16 with fp32. The one real gap is CI coverage: ROCm, A100 and Blackwell haven't run this kernel on this PR. DetailsWhat can reach the new code
Per-platform analysis
Correctness checks I traced (no bugs found)
Gaps and suggestions
Bottom line: I found no correctness bug or API change. The remaining risk is performance on ROCm and coverage of A100/ROCm that hasn't been run, and running the ciflow jobs would close that. |
[ghstack-poisoned]
fsdp::chunk_cat_mixed_dtype copies a reduce-scatter group with mixed gradient dtypes in one fused kernel: one launch copies the fp32 gradients and one launch casts the bf16 or fp16 gradients into the fp32 reduce-scatter input. The cast launch converted one element per thread per iteration with 2-byte loads and 4-byte stores, and the grid was sized for 16 output bytes per thread. It reached 1.7 TB/s on H100. The cast branch of copy_chunk_with_pad in torch/csrc/distributed/fsdp/ChunkCat.cu now uses the same head / aligned body / tail split as the copy path. The head makes dst 16-byte aligned. Each body iteration (unit) loads 8 source elements with the widest load the source alignment allows (16, 8, 4, or 2 bytes, through the read-only cache), converts them with the same c10 cast as before, and writes two 16-byte stores. Padding past the source is written as zeros in the same stores. cast_bytes_per_block sizes the cast grid: each thread takes one unit up to three waves of resident threads, two up to six waves, and so on, up to four. On one GB200, with 8 chunks of 16 or 128 bf16 inputs into fp32 (1 MB to 4 GB of output), a fixed one unit per thread is 11-17% slower than this at 64 MB and 46-53% slower from 1 GB up, and a fixed four units per thread is up to 17% slower at 16 MB. The heuristic is within 3.5% of the best fixed choice at every size. Only the FSDP kernel changes. Uniform groups go through _chunk_cat, whose ATen kernel has the same cast loop; changing it is left to a separate PR. The output is bitwise identical, including padding, all source and destination alignments, odd sizes, and both bf16 and fp16. The tests compare against _chunk_cat of the inputs cast to fp32 first, which runs the same-dtype copy kernel. test_numerics_cast_alignments covers all four load widths, every head length of the padded chunk, padded units and tails, an all-padding chunk, and chunks that span several blocks. test_numerics_cast_several_units_per_thread covers an input large enough for four units per thread on GPUs with up to 304 SMs. Kernel time from the profiler, FSDP2 copy-in, bf16 into fp32, old -> new: | | H100 | GB300 | GB200 | |---|---|---|---| | 285M elements, 64 chunks | 0.955 -> 0.841 ms (1.14x) | 0.709 -> 0.266 ms (2.66x) | 0.721 -> 0.260 ms (2.77x) | | 201M elements, 1 chunk | 0.710 -> 0.585 ms (1.21x) | 0.544 -> 0.180 ms (3.02x) | 0.552 -> 0.181 ms (3.05x) | The GB300 numbers come from a standalone build of the old and new kernels. Test Plan: ``` python -m pytest test/distributed/_composable/fsdp/test_fully_shard_comm.py -k "ChunkCatMixedDtype or NativeCollectiveCopy or PythonCustomOp" python -m pytest test/distributed/_composable/fsdp/test_fully_shard_comm.py test/distributed/_composable/fsdp/test_fully_shard_mixed_precision.py ``` On 2 GB200 GPUs, the first command passes 15 of 15 and the second passes 136 of 137. The one failure, test_fully_shard_alloc_from_pg, expects NCCL to register no buffer before allocation from the process group is enabled, but NCCL 2.30.7 registers a 2 MB buffer. This change does not touch that path. This change was written with an AI assistant (Claude). ghstack-source-id: 74ef3c6 Pull-Request: #204752
|
@claude review this PR |
|
Claude finished @fegin's task in 1m 15s —— View job I found no blocking issues in the vectorized cast path and recommend Approve. One stale comment needs fixing, but that is non-blocking. DetailsPR Review: #204752SummaryThe PR vectorizes the bf16/fp16 → fp32 cast branch of I traced these and found no correctness defect:
This is the same commit ( Code Quality
Performance
RecommendationApprove. Everything above is non-blocking. |
[ghstack-poisoned]
fsdp::chunk_cat_mixed_dtype copies a reduce-scatter group with mixed gradient dtypes in one fused kernel: one launch copies the fp32 gradients and one launch casts the bf16 or fp16 gradients into the fp32 reduce-scatter input. The cast launch converted one element per thread per iteration with 2-byte loads and 4-byte stores, and the grid was sized for 16 output bytes per thread. It reached 1.7 TB/s on H100. The cast branch of copy_chunk_with_pad in torch/csrc/distributed/fsdp/ChunkCat.cu now uses the same head / aligned body / tail split as the copy path. The head makes dst 16-byte aligned. Each body iteration (unit) loads 8 source elements with the widest load the source alignment allows (16, 8, 4, or 2 bytes, through the read-only cache), converts them with the same c10 cast as before, and writes two 16-byte stores. Padding past the source is written as zeros in the same stores. cast_bytes_per_block sizes the cast grid: each thread takes one unit up to three waves of resident threads, two up to six waves, and so on, up to four. On one GB200, with 8 chunks of 16 or 128 bf16 inputs into fp32 (1 MB to 4 GB of output), a fixed one unit per thread is 11-17% slower than this at 64 MB and 46-53% slower from 1 GB up, and a fixed four units per thread is up to 17% slower at 16 MB. The heuristic is within 3.5% of the best fixed choice at every size. Only the FSDP kernel changes. Uniform groups go through _chunk_cat, whose ATen kernel has the same cast loop; changing it is left to a separate PR. The output is bitwise identical, including padding, all source and destination alignments, odd sizes, and both bf16 and fp16. The tests compare against _chunk_cat of the inputs cast to fp32 first, which runs the same-dtype copy kernel. test_numerics_cast_alignments covers all four load widths, every head length of the padded chunk, padded units and tails, an all-padding chunk, and chunks that span several blocks. test_numerics_cast_several_units_per_thread covers an input large enough for four units per thread on GPUs with up to 304 SMs. Kernel time from the profiler, FSDP2 copy-in, bf16 into fp32, old -> new: | | H100 | GB300 | GB200 | |---|---|---|---| | 285M elements, 64 chunks | 0.955 -> 0.841 ms (1.14x) | 0.709 -> 0.266 ms (2.66x) | 0.721 -> 0.260 ms (2.77x) | | 201M elements, 1 chunk | 0.710 -> 0.585 ms (1.21x) | 0.544 -> 0.180 ms (3.02x) | 0.552 -> 0.181 ms (3.05x) | The GB300 numbers come from a standalone build of the old and new kernels. Test Plan: ``` python -m pytest test/distributed/_composable/fsdp/test_fully_shard_comm.py -k "ChunkCatMixedDtype or NativeCollectiveCopy or PythonCustomOp" python -m pytest test/distributed/_composable/fsdp/test_fully_shard_comm.py test/distributed/_composable/fsdp/test_fully_shard_mixed_precision.py ``` On 2 GB200 GPUs, the first command passes 15 of 15 and the second passes 136 of 137. The one failure, test_fully_shard_alloc_from_pg, expects NCCL to register no buffer before allocation from the process group is enabled, but NCCL 2.30.7 registers a 2 MB buffer. This change does not touch that path. This change was written with an AI assistant (Claude). ghstack-source-id: 7a20964 Pull-Request: #204752
There was a problem hiding this comment.
Automated review of 85f5a3b: nothing blocking; ready for human review.
The PR speeds up the bf16/fp16 fp32 cast in the fused fsdp::chunk_cat_mixed_dtype kernel by splitting each chunk into an alignment head, a vectorized 8-element-unit body and a tail, and by sizing the cast grid from the GPU's resident-thread count. Recommendation: Approve. Only non-blocking points remain, and adding ciflow/trunk before merge is strongly suggested.
⚪ 3 non-blocking findings
The new tests have not run on any CI GPU (non-blocking). The PR has no ciflow/* label, and no pull.yml job runs these tests on a GPU: the distributed config (pull.yml:103-110) runs only on CPU, so both @ onlyCUDA tests are skipped, and the CUDA 13.2, ROCm and XPU jobs only build. So far the kernel has run only on the author's GB200s. TestFullyShardChunkCatMixedDtype is a plain TestCase, so on GPU builds it runs through test_distributed_single_gpu in the default shards. Adding ciflow/trunk would run the tests on L4 (CUDA) and gfx950/MI350 (ROCm) before merge, which is also the cheapest direct answer to the maintainer's hardware question.
The grid heuristic has not been measured against the old kernel at small sizes or on A100/AMD (non-blocking). The cast launch always writes fp32, so the smallest cast block now covers 128 1 8 4 = 4096 output bytes, against the old BYTES_PER_BLOCK of 2048. Each chunk now gets ceil(chunk/4096) blocks instead of ceil(chunk/2048); e.g. 8 chunks of 64 KB go from 256 blocks to 128, below H100's 132 SMs. This is not necessarily a regression, since each thread does more vectorized work, but the heuristic comparison ran only on one GB200 from 1 MB up, and the old-vs-new table covers only large inputs on H100, GB300 and GB200. No small-size numbers and no A100 or MI-series numbers; on ROCm doLdg is a plain load. A small (16 MB) and a large old-vs-new comparison on A100 or H100 and on one MI-series GPU would settle this; a note next to the constants would also do.
Stale header comment (non-blocking), ChunkCat.cu:20-22. It still says the device code is "copied verbatim" from aten/src/ATen/native/cuda/TensorShape.cu apart from namespace qualification. That is no longer true: the cast branch of copy_chunk_with_pad (here, 275-313) has been rewritten, while TensorShape.cu:302-315 still has the scalar while loops, and CAST_VEC, CAST_MAX_UNITS_PER_THREAD, cast_bytes_per_block and cast_units_with_pad have no ATen counterpart. Someone re-syncing this file from ATen could silently undo the vectorization. Please say which parts are still verbatim and which now differ.
This review is AI-generated and advisory. Each new automated review replaces the previous one. Report a bad review
[ghstack-poisoned]
fsdp::chunk_cat_mixed_dtype copies a reduce-scatter group with mixed gradient dtypes in one fused kernel: one launch copies the fp32 gradients and one launch casts the bf16 or fp16 gradients into the fp32 reduce-scatter input. The cast launch converted one element per thread per iteration with 2-byte loads and 4-byte stores, and the grid was sized for 16 output bytes per thread. It reached 1.7 TB/s on H100. The cast branch of copy_chunk_with_pad in torch/csrc/distributed/fsdp/ChunkCat.cu now uses the same head / aligned body / tail split as the copy path. The head makes dst 16-byte aligned. Each body iteration (unit) loads 8 source elements with the widest load the source alignment allows (16, 8, 4, or 2 bytes, through the read-only cache), converts them with the same c10 cast as before, and writes two 16-byte stores. Padding past the source is written as zeros in the same stores. cast_bytes_per_block sizes the cast grid: each thread takes one unit up to three waves of resident threads, two up to six waves, and so on, up to four. On one GB200, with 8 chunks of 16 or 128 bf16 inputs into fp32 (1 MB to 4 GB of output), a fixed one unit per thread is 11-17% slower than this at 64 MB and 46-53% slower from 1 GB up, and a fixed four units per thread is up to 17% slower at 16 MB. The heuristic is within 3.5% of the best fixed choice at every size. Only the FSDP kernel changes. Uniform groups go through _chunk_cat, whose ATen kernel has the same cast loop; changing it is left to a separate PR. The output is bitwise identical, including padding, all source and destination alignments, odd sizes, and both bf16 and fp16. The tests compare against _chunk_cat of the inputs cast to fp32 first, which runs the same-dtype copy kernel. test_numerics_cast_alignments covers all four load widths, every head length of the padded chunk, padded units and tails, an all-padding chunk, and chunks that span several blocks. test_numerics_cast_several_units_per_thread covers an input large enough for four units per thread on GPUs with up to 304 SMs. Kernel time from the profiler, FSDP2 copy-in, bf16 into fp32, old -> new: | | H100 | GB300 | GB200 | |---|---|---|---| | 285M elements, 64 chunks | 0.955 -> 0.841 ms (1.14x) | 0.709 -> 0.266 ms (2.66x) | 0.721 -> 0.260 ms (2.77x) | | 201M elements, 1 chunk | 0.710 -> 0.585 ms (1.21x) | 0.544 -> 0.180 ms (3.02x) | 0.552 -> 0.181 ms (3.05x) | The GB300 numbers come from a standalone build of the old and new kernels. Test Plan: ``` python -m pytest test/distributed/_composable/fsdp/test_fully_shard_comm.py -k "ChunkCatMixedDtype or NativeCollectiveCopy or PythonCustomOp" python -m pytest test/distributed/_composable/fsdp/test_fully_shard_comm.py test/distributed/_composable/fsdp/test_fully_shard_mixed_precision.py ``` On 2 GB200 GPUs, the first command passes 15 of 15 and the second passes 136 of 137. The one failure, test_fully_shard_alloc_from_pg, expects NCCL to register no buffer before allocation from the process group is enabled, but NCCL 2.30.7 registers a 2 MB buffer. This change does not touch that path. This change was written with an AI assistant (Claude). ghstack-source-id: 632530f Pull-Request: #204752
|
@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 |
Stack from ghstack (oldest at bottom):
Summary (human-written)
The cast path of the fused FSDP2 chunk_cat kernel (
fsdp::chunk_cat_mixed_dtype) reads only 2 bytes at a time. This PR vectorizes the cast path to speed up the FSDP2 copy-in for gradient groups with mixed dtypes. ATen_chunk_catis not changed.Agent Summary
fsdp::chunk_cat_mixed_dtype copies a reduce-scatter group with mixed gradient dtypes in one fused kernel: one launch copies the fp32 gradients and one launch casts the bf16 or fp16 gradients into the fp32 reduce-scatter input. The cast launch converted one element per thread per iteration with 2-byte loads and 4-byte stores, and the grid was sized for 16 output bytes per thread. It reached 1.7 TB/s on H100.
The cast branch of copy_chunk_with_pad in torch/csrc/distributed/fsdp/ChunkCat.cu now uses the same head / aligned body / tail split as the copy path. The head makes dst 16-byte aligned. Each body iteration (unit) loads 8 source elements with the widest load the source alignment allows (16, 8, 4, or 2 bytes, through the read-only cache), converts them with the same c10 cast as before, and writes two 16-byte stores. Padding past the source is written as zeros in the same stores. cast_bytes_per_block sizes the cast grid: each thread takes one unit up to three waves of resident threads, two up to six waves, and so on, up to four. On one GB200, with 8 chunks of 16 or 128 bf16 inputs into fp32 (1 MB to 4 GB of output), a fixed one unit per thread is 11-17% slower than this at 64 MB and 46-53% slower from 1 GB up, and a fixed four units per thread is up to 17% slower at 16 MB. The heuristic is within 3.5% of the best fixed choice at every size.
Only the FSDP kernel changes. Uniform groups go through _chunk_cat, whose ATen kernel has the same cast loop; changing it is left to a separate PR.
The output is bitwise identical, including padding, all source and destination alignments, odd sizes, and both bf16 and fp16. The tests compare against _chunk_cat of the inputs cast to fp32 first, which runs the same-dtype copy kernel. test_numerics_cast_alignments covers all four load widths, every head length of the padded chunk, padded units and tails, an all-padding chunk, and chunks that span several blocks. test_numerics_cast_several_units_per_thread covers an input large enough for four units per thread on GPUs with up to 304 SMs.
Kernel time from the profiler, FSDP2 copy-in, bf16 into fp32, old -> new:
The GB300 numbers come from a standalone build of the old and new kernels.
Test Plan:
On 2 GB200 GPUs, the first command passes 15 of 15 and the second passes 136 of 137. The one failure, test_fully_shard_alloc_from_pg, expects NCCL to register no buffer before allocation from the process group is enabled, but NCCL 2.30.7 registers a 2 MB buffer. This change does not touch that path.
This change was written with an AI assistant (Claude).