ReduceMin
产品支持情况
| 产品 | 是否支持 |
|---|---|
| Ascend 950PR/Ascend 950DT | √ |
| Atlas A3 训练系列产品/Atlas A3 推理系列产品 | √ |
| Atlas A2 训练系列产品/Atlas A2 推理系列产品 | √ |
| Atlas 200I/500 A2 推理产品 | √ |
| Atlas 推理系列产品 AI Core | √ |
| Atlas 推理系列产品 Vector Core | x |
| Atlas 训练系列产品 | √ |
| Kirin X90 | √ |
| Kirin 9030 | √ |
功能说明
头文件路径为:"basic_api/kernel_operator_vec_reduce_intf.h"。
ReduceMin接口用于从所有输入数据中找出最小值和最小值索引。
ReduceMin计算过程如下图所示:首先,在每个repeat迭代中计算得到最小值和repeat内部索引,这些中间结果暂存于sharedTmpBuffer工作区中;然后,在中间结果的基础上继续按repeat迭代得到最终的最小值和最小值索引。需要注意的是,每次repeat迭代获取的最小值索引是repeat内部索引,返回最终结果时,需要根据迭代位置和repeat内部索引推导全量数据的最小值索引。

函数原型
-
tensor前n个数据计算:
template <typename T> __aicore__ inline void ReduceMin(const LocalTensor<T>& dst, const LocalTensor<T>& src, const LocalTensor<T>& sharedTmpBuffer, const int32_t count, bool calIndex = 0) -
tensor高维切分计算:
-
mask逐bit模式:template <typename T> __aicore__ inline void ReduceMin(const LocalTensor<T>& dst, const LocalTensor<T>& src, const LocalTensor<T>& sharedTmpBuffer, const uint64_t mask[], const int32_t repeatTime, const int32_t srcRepStride, bool calIndex = 0) -
mask连续模式:template <typename T> __aicore__ inline void ReduceMin(const LocalTensor<T>& dst, const LocalTensor<T>& src, const LocalTensor<T>& sharedTmpBuffer, const int32_t mask, const int32_t repeatTime, const int32_t srcRepStride, bool calIndex = 0)
-
参数说明
表 1 模板参数说明
| 参数名 | 描述 |
|---|---|
| T | 操作数数据类型。 |
表 2 参数说明
| 参数名称 | 输入/输出 | 含义 |
|---|---|---|
| dst | 输出 | 目的操作数。 类型为LocalTensor,支持的TPosition为VECIN、VECCALC、VECOUT(存储位置为Unified Buffer)。 |
| src | 输入 | 源操作数。 类型为LocalTensor,支持的TPosition为VECIN、VECCALC、VECOUT(存储位置为Unified Buffer)。 |
| sharedTmpBuffer | 输入 | 指令执行期间存储中间结果,用于内部计算所需操作空间,需特别注意空间大小。详情请参考关键特性说明。 类型为LocalTensor,支持的TPosition为VECIN、VECCALC、VECOUT(存储位置为Unified Buffer)。 |
| count | 输入 | 参与计算的元素个数。关于该参数的具体说明请参考连续计算。最大处理的数据量不能超过UB大小限制。 |
| mask/mask[] | 输入 | mask用于控制每次迭代内参与计算的源操作数。详细设置参考掩码概述。 |
| repeatTime | 输入 | 迭代次数。关于该参数的具体描述请参考高维切分。注:与高维切分中不同的是,repeatTime可以支持更大的取值范围,保证不超过int32_t的最大值即可。 |
| srcRepStride | 输入 | 源操作数相邻迭代间的地址步长,即源操作数每次迭代跳过的DataBlock数目。取值范围为[0, 216−12^{16}-1]。 |
| calIndex | 输入 | 指定是否获取最小值的索引,bool类型,默认值为false,取值:• true:同时获取最小值和最小值索引。• false:不获取索引,只获取最小值。 |
注:以上高维切分相关参数mask,repeatTime,srcRepStride请参考高维切分中的介绍。
数据类型
支持的数据类型如下:
- Ascend 950PR/Ascend 950DT,支持int16_t、uint16_t、half、int32_t、uint32_t、float、int64_t、uint64_t。
- Atlas A3 训练系列产品/Atlas A3 推理系列产品,支持half、float。
- Atlas A2 训练系列产品/Atlas A2 推理系列产品,支持half、float。
- Atlas 200I/500 A2 推理产品,支持half、float。
- Atlas 推理系列产品 AI Core,支持half、float。
- Atlas 训练系列产品,支持half。
- Kirin X90,支持half、float。
- Kirin 9030,支持half、float。
返回值说明
无
约束说明
- 源操作数及
sharedTmpBuffer的地址对齐约束请参考通用地址对齐约束,起始地址需要32字节对齐;目的操作数的起始地址对齐约束请参考ReduceRepeat-表3。 - 操作数地址重叠约束请参考通用地址重叠约束。
- 需要使用
sharedTmpBuffer的情况下,支持dst与sharedTmpBuffer地址重叠(通常情况下dst比sharedTmpBuffer所需的空间要小),此时sharedTmpBuffer必须满足所需空间要求,详情请参考关键特性说明。
- 针对如下型号,当
mask=0或repeatTime=0时,不会执行归约操作,不会对目的操作数进行写入,该接口将被视为NOP(空操作)。- Atlas A3 训练系列产品/Atlas A3 推理系列产品
- Atlas A2 训练系列产品/Atlas A2 推理系列产品
srcRepStride取值范围为[0, 216−12^{16}-1],需要结合UB的实际大小避免出现越界。- 如果存在多个最小值,该指令会将最小索引写入目的操作数。
dst结果存储顺序为最小值,最小值索引,若不需要索引,只会存储最小值。- 当输入类型是
half的时候,只支持获取最大不超过65535(uint16_t能表示的最大值)的索引值。 - 索引按操作数的数据类型存储,读取索引需要将类型转换到整型。请参考ReduceRepeat关键特性说明。
- 针对Ascend 950PR/Ascend 950DT,
int64_t/uint64_t数据类型仅支持tensor前n个数据计算接口。 - 对于Ascend 950PR/Ascend 950DT,因接口内部算法实现不同,无需使用
sharedTmpBuffer,可以直接传入src或者任意大小的sharedTmpBuffer。
- 针对如下型号,需要使用
sharedTmpBuffer。- Atlas A3 训练系列产品/Atlas A3 推理系列产品
- Atlas A2 训练系列产品/Atlas A2 推理系列产品
- Atlas 200I/500 A2 推理产品
- Atlas 推理系列产品 AI Core
- Atlas 训练系列产品
- Kirin X90
- Kirin 9030
关键特性说明
-
索引值需要强制类型转换,详情请参考
ReduceRepeat关键特性说明。 -
sharedTmpBuffer最小所需空间设置:与ReduceMax类似,详情请参考ReduceMax关键特性说明。
调用示例
更多样例请参考ReduceMin样例。
-
tensor高维切分计算样例-
mask连续模式:// dstLocal,srcLocal和sharedTmpBuffer均为half类型,srcLocal的计算数据量为8320,并且连续排布,需要索引值,使用tensor高维切分计算接口,设定repeatTime为65,mask为全部元素参与计算 int32_t mask = 128; AscendC::ReduceMin<half>(dstLocal, srcLocal, sharedTmpBuffer, mask, 65, 8, true); -
tensor高维切分计算样例-
mask逐bit模式:// dstLocal,srcLocal和sharedTmpBuffer均为half类型,srcLocal的计算数据量为8320,并且连续排布,需要索引值,使用tensor高维切分计算接口,设定repeatTime为65,mask为全部元素参与计算 uint64_t mask[2] = { 0xFFFFFFFFFFFFFFFF, 0xFFFFFFFFFFFFFFFF }; AscendC::ReduceMin<half>(dstLocal, srcLocal, sharedTmpBuffer, mask, 65, 8, true); -
tensor前n个数据计算样例:
// dstLocal,srcLocal和sharedTmpBuffer均为half类型,srcLocal的计算数据量为8320,并且连续排布,需要索引值,使用tensor前n个数据计算接口 AscendC::ReduceMin<half>(dstLocal, srcLocal, sharedTmpBuffer, 8320, true); -
#include "kernel_operator.h" int srcDataSize = 512; int dstDataSize = 512; int mask = 128; int repStride = 8; int repeat = srcDataSize / mask; // 初始化srcLocal 、dstLocal 、sharedTmpBuffer AscendC::LocalTensor<half> srcLocal = inQueueSrc.DeQue<half>(); AscendC::LocalTensor<half> dstLocal = outQueueDst.AllocTensor<half>(); AscendC::LocalTensor<half> sharedTmpBuffer = workQueue.AllocTensor<half>(); // mask为128一次计算128个元素,4次repeat计算完512个数,calIndex为true,获取最小值的索引 AscendC::ReduceMin<half>(dstLocal, srcLocal, sharedTmpBuffer, mask, repeat, repStride, true); // 释放Tensor outQueueDst.EnQue<half>(dstLocal); inQueueSrc.FreeTensor(srcLocal); workQueue.FreeTensor(sharedTmpBuffer);示例结果如下:
输入数据src_gm:
[0.769 0.8584 0.1082 0.2715 0.1759 0.7646 0.6406 0.2944 0.4255 0.927 0.8022 0.04507 0.9688 0.919 0.3008 0.7144 0.3206 0.6753 0.8276 ... 0.304 ]
输出数据dst_gm:
[0.01034, 2.104e-05] // 2.104e-05需要使用reinterpret_cast方法转换得到索引值353
-
tensor前n个数据计算接口完整调用示例:
#include "kernel_operator.h" int srcDataSize = 288; // 初始化srcLocal 、dstLocal 、sharedTmpBuffer AscendC::LocalTensor<half> srcLocal = inQueueSrc.DeQue<half>(); AscendC::LocalTensor<half> dstLocal = outQueueDst.AllocTensor<half>(); AscendC::LocalTensor<half> sharedTmpBuffer = workQueue.AllocTensor<half>(); // level2接口计算前288个数,calIndex为true,获取最小值的索引 AscendC::ReduceMin<half>(dstLocal, srcLocal, sharedTmpBuffer, srcDataSize, true); // 释放Tensor outQueueDst.EnQue<half>(dstLocal); inQueueSrc.FreeTensor(srcLocal); workQueue.FreeTensor(sharedTmpBuffer);示例结果如下:
输入数据src_gm:
[0.556 0.5225 0.3623 0.214 0.556 0.0643 0.769 0.594 0.261 0.3652 0.911 0.924 0.386 0.3696 0.2296 0.5957 0.1709 0.79 0.8516 ... 0.2242 ]
输出数据dst_gm:
[0.00393, 4.3e-06] // 4.3e-06需要使用reinterpret_cast方法转换得到索引值72