[PHI] Fixed argsort big tensor bug #72712
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
PR Category
Operator Mechanism
PR Types
Bug fixes
Description
修复了大 Tensor 情况下的。argsort 算子出错。argsort 算子出错原因:
cub::DeviceSegmentedRadixSort目前不支持超过 INT_MAX 个元素的sort,所以对于大tensor,需要一个kernel call拆分机制,将一次 sort 拆分为多次。比如下面这个例子:由于 Tensor numel 为 2281701380 大于 INT_MAX,直接用
DeviceSegmentedRadixSort::SortPairs计算 temp buffer 大小将会出现溢出。目前的解决方案:每次参与 sort 的元素不超过 2^30 个(不选择 2^31 - 1 是为了避免过大的 temp buffer,显存压力大),按照 sorting 维度计算 batch,拆分为多个 cub kernel。比如上述例子将会被拆分为三次计算:关于
cub::DeviceSegmentedRadixSort的 API 支持范围,见:经过性能测试,此 kernel 目前平均运行时间约为 torch 的 1.25倍,部分较好的 shape 下(比如[5, 456340276] axis = -1)可达到将近50%的计算时间。
Pcard-89620