|
产品 |
是否支持 |
|---|---|
|
Ascend 950PR/Ascend 950DT |
√ |
|
|
√ |
|
|
√ |
|
|
x |
|
|
√ |
|
|
x |
|
|
x |
排序函数,按照数值大小进行降序排序。排序后的数据按照如下排布方式进行保存:
Ascend 950PR/Ascend 950DT采用方式一。
1
|
[x1, y1, x2, y2, score, label, reserved_0, reserved_1] |
对于数据类型half,每一个Region Proposal占16Bytes,Byte[15:12]是无效数据,Byte[11:0]包含6个half类型的元素,其中Byte[11:10]定义为label,Byte[9:8]定义为score,Byte[7:6]定义为y2,Byte[5:4]定义为x2,Byte[3:2]定义为y1,Byte[1:0]定义为x1。
如下图所示,总共包含16个Region Proposals。

对于数据类型float,每一个Region Proposal占32Bytes,Byte[31:24]是无效数据,Byte[23:0]包含6个float类型的元素,其中Byte[23:20]定义为label,Byte[19:16]定义为score,Byte[15:12]定义为y2,Byte[11:8]定义为x2,Byte[7:4]定义为y1,Byte[3:0]定义为x1。
如下图所示,总共包含16个Region Proposals。

仅在Ascend 950PR/Ascend 950DT上支持。
根据模板参数SortConfig,按其中指定的排序算法,对输入数据排序,排序结果可以指定升序或降序排序。
当函数原型带有输出索引dstIndexTensor参数,需要输出排序结果数据分别对应的索引;若输入带有索引srcIndexTensor参数,则输出索引即为原输入的索引,若输入不带有索引,则对输入数据从0开始生成所需排序数量的索引,最终输出索引即为对应输入数据的索引。如下两幅图,分别为输入带有索引和输入不带索引的数据排序示意图。
1 2 |
template <typename T, bool isFullSort> __aicore__ inline void Sort(const LocalTensor<T>& dst, const LocalTensor<T>& concat, const LocalTensor<uint32_t>& index, LocalTensor<T>& tmp, const int32_t repeatTime) |
仅在Ascend 950PR/Ascend 950DT上支持。
1 2 |
template <typename T, bool isReuseSource = false, const SortConfig& config = DEFAULT_SORT_CONFIG> __aicore__ inline void Sort(LocalTensor<T>& dstTensor, const LocalTensor<T>& srcTensor, const uint32_t calCount) |
1 2 |
template <typename T, bool isReuseSource = false, const SortConfig& config = DEFAULT_SORT_CONFIG> __aicore__ inline void Sort(LocalTensor<T>& dstTensor, LocalTensor<uint32_t>& dstIndexTensor, const LocalTensor<T>& srcTensor, const uint32_t calCount) |
1 2 |
template <typename T, typename U, bool isReuseSource = false, const SortConfig& config = DEFAULT_SORT_CONFIG> __aicore__ inline void Sort(const LocalTensor<T>& dstTensor, const LocalTensor<U>& dstIndexTensor, const LocalTensor<T>& srcTensor, const LocalTensor<U>& srcIndexTensor, const uint32_t calCount) |
1 2 |
template <typename T, bool isReuseSource = false, const SortConfig& config = DEFAULT_SORT_CONFIG> __aicore__ inline void Sort(LocalTensor<T>& dstTensor, const LocalTensor<T>& srcTensor, const LocalTensor<uint8_t>& sharedTmpBuffer, const uint32_t calCount) |
1 2 |
template <typename T, bool isReuseSource = false, const SortConfig& config = DEFAULT_SORT_CONFIG> __aicore__ inline void Sort(LocalTensor<T>& dstTensor, LocalTensor<uint32_t>& dstIndexTensor, const LocalTensor<T>& srcTensor, const LocalTensor<uint8_t>& sharedTmpBuffer, const uint32_t calCount) |
1 2 |
template <typename T, typename U, bool isReuseSource = false, const SortConfig& config = DEFAULT_SORT_CONFIG> __aicore__ inline void Sort(LocalTensor<T>& dstTensor, LocalTensor<U>& dstIndexTensor, const LocalTensor<T>& srcTensor, const LocalTensor<U>& srcIndexTensor, const LocalTensor<uint8_t>& sharedTmpBuffer, const uint32_t calCount) |
由于该接口的内部实现中涉及复杂的数学计算,需要额外的临时空间来存储计算过程中的中间变量。临时空间支持开发者通过sharedTmpBuffer入参传入和接口框架申请两种方式。
通过sharedTmpBuffer传入的情况,开发者需要为tensor申请空间;接口框架申请的方式,开发者需要预留临时空间。临时空间大小BufferSize的获取方式如下:通过GetSortMaxMinTmpSize中提供的接口获取需要预留空间范围的大小。
|
参数名 |
含义 |
|---|---|
|
T |
操作数的数据类型。支持的数据类型为:half、float。 |
|
isFullSort |
是否开启全排序模式。全排序模式指将全部输入降序排序,非全排序模式下,排序方式请参考表2中的repeatTime说明。 |
|
参数名称 |
输入/输出 |
含义 |
|---|---|---|
|
dst |
输出 |
目的操作数,shape为[2n]。 类型为LocalTensor,支持的TPosition为VECIN/VECCALC/VECOUT。 LocalTensor的起始地址需要32字节对齐。 |
|
concat |
输入 |
源操作数,即接口功能说明中的score,shape为[n]。 类型为LocalTensor,支持的TPosition为VECIN/VECCALC/VECOUT。 LocalTensor的起始地址需要32字节对齐。 此源操作数的数据类型需要与目的操作数保持一致。 |
|
index |
输入 |
源操作数,shape为[n]。 类型为LocalTensor,支持的TPosition为VECIN/VECCALC/VECOUT。 LocalTensor的起始地址需要32字节对齐。 此源操作数固定为uint32_t数据类型。 |
|
tmp |
输入 |
临时空间。接口内部复杂计算时用于存储中间变量,由开发者提供,临时空间大小BufferSize的获取方式请参考GetSortTmpSize。数据类型与源操作数保持一致。 类型为LocalTensor,支持的TPosition为VECIN/VECCALC/VECOUT。 LocalTensor的起始地址需要32字节对齐。 |
|
repeatTime |
输入 |
重复迭代次数,int32_t类型。
|
|
接口 |
功能 |
||||||
|---|---|---|---|---|---|---|---|
|
T |
操作数srcTensor和dstTensor的数据类型。RADIX_SORT排序算法支持的数据类型为:uint8_t、int8_t、uint16_t、int16_t、uint32_t、int32_t、half、bfloat16_t、float、uint64_t、int64_t,MERGE_SORT排序算法支持的数据类型为:half、float。 |
||||||
|
U |
操作数srcIndexTensor和dstIndexTensor的数据类型。RADIX_SORT排序算法支持的数据类型为:uint32_t、int32_t、uint64_t、int64_t,MERGE_SORT排序算法支持的数据类型为:uint32_t。 |
||||||
|
isReuseSource |
可选参数。是否可以复用输入的Tensor空间。 |
||||||
|
config |
可选参数。Sort接口的相应配置:选择的排序算法,排序结果的升降序。数据类型SortConfig,定义如下。
其中,排序算法的数据类型SortType取值如下。
Sort提供了两种不同的排序算法,MERGE_SORT归并排序算法和RADIX_SORT基排序算法。两种算法在执行速度、时间复杂度和算法稳定性上表现不同。
config的默认值DEFAULT_SORT_CONFIG取值如下,使用基排序RADIX_SORT,对排序结果升序排序。
|
|
参数名称 |
输入/输出 |
含义 |
|---|---|---|
|
dstTensor |
输出 |
值目的操作数,shape为[n]。MERGE_SORT算法下输出数据的每个元素需要按8Byte申请空间。 类型为LocalTensor,支持的TPosition为VECIN/VECCALC/VECOUT。 LocalTensor的起始地址需要32字节对齐。 |
|
dstIndexTensor |
输出 |
索引目的操作数,shape为[n]。当输入不带srcIndexTensor时,只支持uint32_t类型。 类型为LocalTensor,支持的TPosition为VECIN/VECCALC/VECOUT。 LocalTensor的起始地址需要32字节对齐。 |
|
srcTensor |
输入 |
值源操作数,shape为[n]。 类型为LocalTensor,支持的TPosition为VECIN/VECCALC/VECOUT。 LocalTensor的起始地址需要32字节对齐。 此源操作数的数据类型需要与值目的操作数保持一致。 |
|
srcIndexTensor |
输入 |
索引源操作数,shape为[n]。 类型为LocalTensor,支持的TPosition为VECIN/VECCALC/VECOUT。 LocalTensor的起始地址需要32字节对齐。 此源操作数的数据类型需要与索引目的操作数保持一致。 |
|
sharedTmpBuffer |
输入 |
临时空间。接口内部复杂计算时用于存储中间变量,由开发者提供,临时空间大小BufferSize的获取方式请参考GetSortMaxMinTmpSize。数据类型为uint8_t。 类型为LocalTensor,支持的TPosition为VECIN/VECCALC/VECOUT。 LocalTensor的起始地址需要32字节对齐。 |
|
calCount |
输入 |
需要进行排序的数据元素个数。uint32_t类型。 |
无
算子样例工程请通过sort样例链接获取。
Ascend 950PR/Ascend 950DT
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
// sortedLocal:排序结果 // concatLocal:待排序数据的预处理结果 // indexLocal:索引数据 // sortTmpLocal:临时空间 // 待排序元素数量 uint32_t m_elementCount = 128; // 迭代次数,每次迭代完成32个元素的排序 uint32_t m_sortRepeatTimes = m_elementCount / 32; uint32_t m_extractRepeatTimes = m_elementCount / 32; // 预处理 AscendC::Concat(concatLocal, valueLocal, concatTmpLocal, m_concatRepeatTimes); // 执行排序 AscendC::Sort<T, isFullSort>(sortedLocal, concatLocal, indexLocal, sortTmpLocal, m_sortRepeatTimes); AscendC::Extract(dstValueLocal, dstIndexLocal, sortedLocal, m_extractRepeatTimes); |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
示例结果 输入数据(srcValueGm): 128个half类型数据 [31 30 29 ... 2 1 0 63 62 61 ... 34 33 32 95 94 93 ... 66 65 64 127 126 125 ... 98 97 96] 输入数据(srcIndexGm): [31 30 29 ... 2 1 0 63 62 61 ... 34 33 32 95 94 93 ... 66 65 64 127 126 125 ... 98 97 96] 输出数据(dstValueGm): [127 126 125 ... 2 1 0] 输出数据(dstIndexGm): [127 126 125 ... 2 1 0] |
1 2 3 4 5 6 |
uint32_t m_elementCount = 64; uint32_t m_sortRepeatTimes = m_elementCount / 16; uint32_t m_extractRepeatTimes = m_elementCount / 16; AscendC::Concat(concatLocal, valueLocal, concatTmpLocal, m_concatRepeatTimes); AscendC::Sort<T, isFullSort>(sortedLocal, concatLocal, indexLocal, sortTmpLocal, m_sortRepeatTimes); AscendC::Extract(dstValueLocal, dstIndexLocal, sortedLocal, m_extractRepeatTimes); |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
示例结果 输入数据(srcValueGm): 64个half类型数据 [15 14 13 ... 2 1 0 31 30 29 ... 18 17 16 47 46 45 ... 34 33 32 63 62 61 ... 50 49 48] 输入数据(srcIndexGm): [15 14 13 ... 2 1 0 31 30 29 ... 18 17 16 47 46 45 ... 34 33 32 63 62 61 ... 50 49 48] 输出数据(dstValueGm): [63 62 61 ... 2 1 0] 输出数据(dstIndexGm): [63 62 61 ... 2 1 0] |
Ascend 950PR/Ascend 950DT
1 2 |
static constexpr AscendC::SortConfig config = {AscendC::SortType::RADIX_SORT, false}; Sort<T, false, config>(dstLocal, dstIndexLocal, srcLocal, 1024); |
1 2 3 4 5 6 7 8 9 |
示例结果 输入数据(srcGm): 1024个half类型数据 [1023 1022 ... 2 1 0] 输入数据(srcIndexGm): 1024个uint32_t类型数据 [0 1 2 ... 1022 1023] 输出数据(dstGm): [0 1 2 ... 1022 1023] 输出数据(dstIndexGm): [1023 1022 ... 2 1 0] |