Skip to content

[release/3.4] fix: use ThrustAllocator in argsort 1D path to avoid implicit cudaStr…#78733

Open
risemeup1111 wants to merge 1 commit intoPaddlePaddle:release/3.4from
risemeup1111:cherry-pick/78726/release/3.4
Open

[release/3.4] fix: use ThrustAllocator in argsort 1D path to avoid implicit cudaStr…#78733
risemeup1111 wants to merge 1 commit intoPaddlePaddle:release/3.4from
risemeup1111:cherry-pick/78726/release/3.4

Conversation

@risemeup1111
Copy link
Copy Markdown

PR Category

Operator Mechanism

PR Types

Performance

Description

修复argsort算子在1D case下的低效malloc路径,使其调用框架内存管理机制,避免裸malloc带来的额外同步

pcard-91067

是否引起精度变化


Cherry-pick of #78726 (authored by @A-nnonymous) to release/3.4.

devPR:#78726

PaddlePaddle#78726)

* fix: use ThrustAllocator in argsort 1D path to avoid implicit cudaStreamSynchronize

The 1D argsort path uses thrust::sort_by_key / thrust::stable_sort_by_key
with the default execution policy (thrust::cuda::par.on(stream)), which
causes thrust to allocate temporary workspace via cudaMalloc/cudaFree.
These are synchronous CUDA API calls that implicitly trigger
cudaStreamSynchronize, draining all pending GPU work on the stream.

This creates a false data dependency: if any prior kernels (e.g. backward
weight gradient GEMMs) are still executing on the same stream, argsort
blocks until they complete — adding ~2ms of unnecessary stall per call.

The fix passes phi::memory_utils::ThrustAllocator to the thrust execution
policy, routing temporary allocations through Paddle's caching allocator
(which is async and does not synchronize). This is consistent with other
Paddle kernels that use thrust (e.g. unique_kernel.cu, shuffle_batch_kernel.cu).

nsys evidence (500K int32 argsort after a 4096x4096 matmul):
  Before: 3x cudaStreamSynchronize + 1x cudaMalloc + 1x cudaFree per call
          wall time = 2.5ms (steady state)
  After:  0x cudaStreamSynchronize, 0x cudaMalloc (caching allocator hit)
          wall time = 0.07ms (expected, matching the 2D CUB path)

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>

* polish

---------

Co-authored-by: Claude Opus 4.6 <noreply@anthropic.com>
@paddle-bot
Copy link
Copy Markdown

paddle-bot Bot commented Apr 20, 2026

你的PR提交成功,感谢你对开源项目的贡献!
请关注后续CI自动化测试结果,详情请参考Paddle-CI手册
Your PR has been submitted. Thanks for your contribution!
Please wait for the result of CI firstly. See Paddle CI Manual for details.

@paddle-bot paddle-bot Bot added the contributor External developers label Apr 20, 2026
@A-nnonymous
Copy link
Copy Markdown
Contributor

/re-run all-failed

2 similar comments
@A-nnonymous
Copy link
Copy Markdown
Contributor

/re-run all-failed

@A-nnonymous
Copy link
Copy Markdown
Contributor

/re-run all-failed

Copy link
Copy Markdown
Contributor

@wanghuancoder wanghuancoder left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants