源操作数内每个element做右移,右移的位数由输入参数scalar决定。
对数据类型uint16_t/uint32_t的源操作数做逻辑右移,对数据类型int16_t/int32_t的源操作数做算术右移。
逻辑右移为去掉最低位,最高位填充为0。
算术右移为去掉最低位,最高位复制符号位。
例:数据类型uint16_t,二进制数 1010101010101010,逻辑右移一位结果为 0101010101010101;
数据类型int16_t,二进制数 1010101010101010,算术右移一位结果为 1101010101010101;
数据类型int16_t,二进制数 1010101010101010,算术右移三位结果为 1111010101010101。
1 2 |
template <typename T, bool isSetMask = true> __aicore__ inline void ShiftRight(const LocalTensor<T>& dstLocal, const LocalTensor<T>& srcLocal, const T& scalarValue, const int32_t& calCount) |
1 2 |
template <typename T, bool isSetMask = true> __aicore__ inline void ShiftRight(const LocalTensor<T>& dstLocal, const LocalTensor<T>& srcLocal, const T& scalarValue, uint64_t mask[], const uint8_t repeatTimes, const UnaryRepeatParams& repeatParams,bool roundEn = false) |
1 2 |
template <typename T, bool isSetMask = true> __aicore__ inline void ShiftRight(const LocalTensor<T>& dstLocal, const LocalTensor<T>& srcLocal, const T& scalarValue, uint64_t mask, const uint8_t repeatTimes, const UnaryRepeatParams& repeatParams,bool roundEn = false) |
dstLocal和srcLocal使用TensorTrait类型时,其数据类型TensorTrait和scalarValue的数据类型(对应TensorTrait中的LiteType类型)不一致。因此新增模板类型U表示scalarValue的数据类型,并通过std::enable_if检查T中萃取出的LiteType和U是否完全一致,一致则接口通过编译,否则编译失败。接口原型定义如下:
1 2 |
template <typename T, typename U, bool isSetMask = true, typename std::enable_if<IsSameType<PrimT<T>, U>::value, bool>::type = true> __aicore__ inline void ShiftRight(const LocalTensor<T>& dstLocal, const LocalTensor<T>& srcLocal, const U& scalarValue, const int32_t& calCount) |
1 2 |
template <typename T, typename U, bool isSetMask = true, typename std::enable_if<IsSameType<PrimT<T>, U>::value, bool>::type = true> __aicore__ inline void ShiftRight(const LocalTensor<T>& dstLocal, const LocalTensor<T>& srcLocal, const U& scalarValue, uint64_t mask[], const uint8_t repeatTimes, const UnaryRepeatParams& repeatParams, bool roundEn) |
1 2 |
template <typename T, typename U, bool isSetMask = true, typename std::enable_if<IsSameType<PrimT<T>, U>::value, bool>::type = true> __aicore__ inline void ShiftRight(const LocalTensor<T>& dstLocal, const LocalTensor<T>& srcLocal, const U& scalarValue, uint64_t mask, const uint8_t repeatTimes, const UnaryRepeatParams& repeatParams, bool roundEn) |
参数名 |
描述 |
---|---|
T |
操作数数据类型。 |
U |
scalarValue数据类型。 |
isSetMask |
是否在接口内部设置mask模式和mask值。
|
参数名称 |
类型 |
说明 |
---|---|---|
dstLocal |
输出 |
目的操作数。 类型为LocalTensor,支持的TPosition为VECIN/VECCALC/VECOUT。 LocalTensor的起始地址需要32字节对齐。 Atlas A2训练系列产品/Atlas 800I A2推理产品,支持的数据类型为:Tensor(uint16/int16/uint32/int32) Atlas 200I/500 A2推理产品,支持的数据类型为:Tensor(uint16/int16/uint32/int32) |
srcLocal |
输入 |
源操作数。 类型为LocalTensor,支持的TPosition为VECIN/VECCALC/VECOUT。 LocalTensor的起始地址需要32字节对齐。 数据类型需要与目的操作数保持一致。 Atlas A2训练系列产品/Atlas 800I A2推理产品,支持的数据类型为:Tensor(uint16/int16/uint32/int32) Atlas 200I/500 A2推理产品,支持的数据类型为:Tensor(uint16/int16/uint32/int32) |
scalarValue |
输入 |
源操作数,数据类型需要与目的操作数Tensor中的元素数据类型保持一致。 当src为uint16_t/int16_t类型时,scalar取值范围:[0, 16]。 当src为uint32_t/int32_t类型时,scalar取值范围:[0, 32]。 Atlas A2训练系列产品/Atlas 800I A2推理产品,支持的数据类型为:uint16/int16/uint32/int32 Atlas 200I/500 A2推理产品,支持的数据类型为:uint16/int16/uint32/int32 |
calCount |
输入 |
输入数据元素个数。 参数取值范围和操作数的数据类型有关,数据类型不同,能够处理的元素个数最大值不同。 矢量计算单元,每个迭代读取连续256 Bytes数据进行计算,通过多次迭代完成所有数据的读取与计算。所以当操作数为16位时,calCount∈[1,128*255],255表示迭代次数的最大值,128表示每次迭代内能够处理128个16位数据;当操作数为32位时,calCount∈[1,64*255],64表示每次迭代内能够处理64个32位数据。 |
mask |
输入 |
mask用于控制每次迭代内参与计算的元素。
|
repeatTimes |
输入 |
重复迭代次数。 矢量计算单元,每次读取连续的256 Bytes数据进行计算,为完成对输入数据的处理,必须通过多次迭代(repeat)才能完成所有数据的读取与计算。repeatTimes表示迭代的次数。 关于该参数的具体描述请参考通用参数说明。 |
repeatParams |
输入 |
元素操作控制结构信息,具体请参考UnaryRepeatParams。 |
roundEn |
输入 |
舍入功能使能开关,支持数据类型:bool,true为使能,false为不使能。仅当src为int16_t/int32_t类型时使能有效。 例:使能舍入功能,src数据类型为int16_t,将src算数右移5位,如果src_ele二进制数中的第5位为1,则dst_ele值为对src_ele算术右移5后加1。 src_ele = 17 = 0b0000000000010001 第五位为1 dst_ele = arithmetic_righ_shift(src_ele, 5) + 1 = 0b0000000000000000 + 1 = 0b0000000000000001 Atlas 200I/500 A2推理产品,不支持使能舍入功能,仅支持传入false。 |
无
Atlas A2训练系列产品/Atlas 800I A2推理产品
Atlas 200I/500 A2推理产品
1 2 3 4 5 6 |
uint64_t mask = 128; int16_t scalar = 2; // repeatTimes = 4, 单次迭代处理128个数,计算512个数需要迭代4次 // dstBlkStride, srcBlkStride = 1, 每个迭代内src0参与计算的数据地址间隔为1个datablock,表示单次迭代内数据连续读取和写入 // dstRepStride, srcRepStride = 8, 相邻迭代间的地址间隔为8个datablock,表示相邻迭代间数据连续读取和写入 AscendC::ShiftRight(dstLocal, srcLocal, scalar, mask, 4, { 1, 1, 8, 8 }, false); |
1 2 3 4 5 6 |
uint64_t mask[2] = { UINT64_MAX, UINT64_MAX }; int16_t scalar = 2; // repeatTimes = 4, 单次迭代处理128个数,计算512个数需要迭代4次 // dstBlkStride, srcBlkStride = 1, 每个迭代内src0参与计算的数据地址间隔为1个datablock,表示单次迭代内数据连续读取和写入 // dstRepStride, srcRepStride = 8, 相邻迭代间的地址间隔为8个datablock,表示相邻迭代间数据连续读取和写入 AscendC::ShiftRight(dstLocal, srcLocal, scalar, mask, 4, {1, 1, 8, 8}, false); |
1 2 |
int16_t scalar = 2; AscendC::ShiftRight(dstLocal, srcLocal, scalar, 512); |
输入数据(src0Local): [1 2 3 ... 512] 输入数据 scalar = 2 输出数据(dstLocal): [0 0 0 1 1 1 1 ... 128]