Repository navigation
Conversation
thrust::cuda::par synchronizes the stream after every algorithm call. None of these call sites need it: results are consumed by later work on the same stream, or read on the host after an explicit stream-ordered copy plus Wait/cudaStreamSynchronize, or come from algorithms whose mandatory sync is kept by par_nosync (reduce, equal, unique_by_key, reduce_by_key, remove, min_element, ...). Two places relied on the implicit sync for correctness on non-blocking streams, and are fixed here: - unique_kernel.cu: inv_loc[0] was written from the host through a thrust::device_ptr, which runs on the legacy default stream. Use thrust::fill on the kernel's stream instead. - sparse mask/sparse_utils kernels: GetBatchSizes and ConvertCsrCrowsToCooRows were launched without a stream. Launch them on dev_ctx.stream(). Co-Authored-By: Claude Code <noreply@anthropic.com>
Paddle-Bot Review Board (review完成)
可维护性建议(总分 89.7 / 100,只给建议,不影响评审结论)
各维度得分(仅评本 PR 改动,点击展开)
Powered by Nyanpasu claude with Opus 5.5 默认推理级别, please check the suggestions carefully. |
| op); | ||
| #else | ||
| thrust::transform(thrust::cuda::par.on(dev_ctx.stream()), | ||
| thrust::transform(thrust::cuda::par_nosync.on(dev_ctx.stream()), |
There was a problem hiding this comment.
thrust::cuda::par_nosync 从 Thrust 1.16 才提供,但当前构建仍允许 CUDA 11.7/11.8,这些配置下会编译失败。
cmake/configure.cmake:116的最低要求仍是 CUDA >= 11.7;setup.py/python/setup.py.in仍有"11.8"的依赖表,tools/dockerfile/ubuntu20_dev.sh默认ref_CUDA_MAJOR=11.8。cmake/third_party.cmake:484-493只在 CUDA >= 12.0 且WITH_SHARED_PHI时引入 CCCL;CUDA 11.7/11.8 只替换了 CUB(cmake/external/cub.cmake:27-29),Thrust 仍是 toolkit 自带的 1.15.x,其中没有par_nosync。- PR 描述里也提到 CUDA 11.x 不支持,但代码里没有版本保护,本 PR 改到的 47 处都会受影响。
建议二选一:
- 在公共头里统一按版本选择,各调用点改用这个宏,例如:
注意:回退到
#if THRUST_VERSION >= 101600 #define PADDLE_THRUST_CUDA_PAR thrust::cuda::par_nosync #else #define PADDLE_THRUST_CUDA_PAR thrust::cuda::par #endif
par后,unique_kernel.cu和稀疏 kernel 里补上的 stream 顺序修复仍然有效。 - 明确放弃 CUDA 11.x,同步把
configure.cmake的最低版本提到 12.0,并清理 11.8 相关的打包配置。
| ConvertCsrCrowsToCooRows<IntT><<<config.block_per_grid, | ||
| config.thread_per_block.x, | ||
| 0, | ||
| dev_ctx.stream()>>>( |
There was a problem hiding this comment.
这里给 3D 路径补上了
dev_ctx.stream(),但同文件 2D 路径 MaskCsr2DGPUKernel 的同一个 kernel(mask_kernel.cu:191-193)仍在 legacy default stream 上启动:
ConvertCsrCrowsToCooRows<IntT>
<<<config.block_per_grid, config.thread_per_block.x>>>(...);它后面的 GpuMemcpyAsync 和 MaskKernel(第 206 行)都在 dev_ctx.stream() 上,并且会读取这个 kernel 写出的 coo_rows_data。当 dev_ctx.stream() 是用户创建的 cudaStreamNonBlocking stream 时,这两者之间没有顺序保证,就是 PR 描述里要修的那类竞争。建议按本处的写法补成 <<<config.block_per_grid, config.thread_per_block.x, 0, dev_ctx.stream()>>>。
| inv_loc_data_dev[0] = 0; // without device_ptr, segmentation fault | ||
| // Set inv_loc[0] = 0 on dev_ctx.stream() so it is ordered after the | ||
| // adjacent_difference above, which no longer synchronizes the stream. | ||
| thrust::fill(exec_policy, inv_loc_data_ptr, inv_loc_data_ptr + 1, 0); |
There was a problem hiding this comment.
💡 可维护性建议(测试策略,不阻塞合入):这个 PR 修复了两处依赖隐式同步的 stream 顺序问题(这里的 inv_loc[0],以及稀疏 mask / csr->coo 的 kernel 启动),但没有配套测试。现有 test/legacy_test/test_unique.py 和 test_sparse_mask_as_op.py 都只在默认 stream 上运行,旧代码在默认 stream 下同样能通过,以后有人回退这些修改也测不出来。建议参照 test/legacy_test/test_cuda_stream_event.py,在 paddle.device.cuda.stream_guard(paddle.device.cuda.Stream()) 下补一个用例:用较大输入调用 paddle.unique(..., return_inverse=True) 和 3D CSR 的 mask_as / to_sparse_coo,并与 numpy 结果对比。
- Add PADDLE_THRUST_CUDA_PAR in paddle/phi/backends/gpu/gpu_thrust_policy.h. It is thrust::cuda::par_nosync for Thrust >= 1.16 and falls back to thrust::cuda::par otherwise, so CUDA 11.7/11.8 builds (toolkit Thrust 1.15) keep compiling. Use it at all call sites. - Launch ConvertCsrCrowsToCooRows on dev_ctx.stream() in MaskCsr2DGPUKernel too. - Add test_thrust_nosync_stream.py, which runs unique (return_inverse), CSR mask_as (2D/3D) and 3D CSR->COO on a user-created non-blocking stream and compares against numpy. Co-Authored-By: Claude Code <noreply@anthropic.com>
PR Category
Performance Optimization
PR Types
Performance
Description
Replace
thrust::cuda::parwiththrust::cuda::par_nosyncunderpaddle/(47 call sites in 23 files), through a new macroPADDLE_THRUST_CUDA_PARdefined inpaddle/phi/backends/gpu/gpu_thrust_policy.h.thrust::cuda::parsynchronizes the stream after every algorithm call;par_nosyncskips that optional sync. HIP paths (thrust::hip::par) are unchanged.Why removing the sync is safe
reduce,equal,unique/unique_by_key,reduce_by_key,remove/copy_if,min_element, ...) still perform their mandatory synchronization underpar_nosync.transform,fill,sort,scan,lower_bound,sequence, ...), the results are either consumed only by later work on the same stream, or read on the host after a stream-ordered D2H copy followed byWait()/cudaStreamSynchronize.ThrustAllocatorandDenseTensor), or released withcudaFree, which synchronizes.Two places that relied on the implicit sync are fixed
Paddle's default stream is blocking, but user-created streams are
cudaStreamNonBlocking, so work on the legacy default stream is not ordered with them:gpu/unique_kernel.cu:inv_loc[0] = 0was written from the host throughthrust::device_ptr, which runs on the legacy default stream and could race with the precedingadjacent_difference. It is now written withthrust::fillon the kernel's stream.sparse/gpu/sparse_utils_kernel.cu,sparse/gpu/mask_kernel.cu(both the 2D and 3D paths):GetBatchSizesandConvertCsrCrowsToCooRowswere launched without a stream around athrust::exclusive_scan. They now launch ondev_ctx.stream().CUDA 11.x compatibility
par_nosyncrequires Thrust >= 1.16.PADDLE_THRUST_CUDA_PARisthrust::cuda::par_nosyncwhenTHRUST_VERSION >= 101600and falls back tothrust::cuda::parotherwise, so builds with the CUDA 11.x toolkit Thrust (1.15) keep compiling with the old behavior.Testing
-DWITH_GPU=ON -DWITH_SHARED_PHI=ON -DCUDA_ARCH_BIN=100on CUDA 13.2 (aarch64) succeeds;paddle.utils.run_check()passes on a single GPU.test/legacy_test/test_thrust_nosync_stream.py: runspaddle.unique(withreturn_inverse, flat andaxis=0), CSRpaddle.sparse.mask_as(2D/3D) and 3D CSR->COO on a user-created non-blocking stream and compares with numpy.是否引起精度变化
否
🤖 Generated with Claude Code