Sqrt
产品支持情况
功能说明
按元素做开方,计算公式如下:

函数原型
-
tensor前n个数据计算
template <typename T, const SqrtConfig& config = DEFAULT_SQRT_CONFIG> __aicore__ inline void Sqrt(const LocalTensor<T>& dst, const LocalTensor<T>& src, const int32_t& count) -
tensor高维切分计算
-
mask逐bit模式
template <typename T, bool isSetMask = true, const SqrtConfig& config = DEFAULT_SQRT_CONFIG> __aicore__ inline void Sqrt(const LocalTensor<T>& dst, const LocalTensor<T>& src, uint64_t mask[], const uint8_t repeatTime, const UnaryRepeatParams& repeatParams) -
mask连续模式
template <typename T, bool isSetMask = true, const SqrtConfig& config = DEFAULT_SQRT_CONFIG> __aicore__ inline void Sqrt(const LocalTensor<T>& dst, const LocalTensor<T>& src, uint64_t mask, const uint8_t repeatTime, const UnaryRepeatParams& repeatParams)
-
参数说明
表 1 模板参数说明
|
Atlas A2 训练系列产品/Atlas A2 推理系列产品,支持的数据类型为:half、float。 Atlas A3 训练系列产品/Atlas A3 推理系列产品,支持的数据类型为:half、float。 Ascend 950PR/Ascend 950DT,支持的数据类型为:half、float。 |
|
|
|
enum class SqrtAlgo {
INTRINSIC = 0,
FAST_INVERSE,
PRECISION_1ULP_FTZ_TRUE,
PRECISION_0ULP_FTZ_FALSE,
PRECISION_1ULP_FTZ_FALSE,
};
struct SqrtConfig {
SqrtAlgo algo = SqrtAlgo::INTRINSIC;
};
通过SqrtConfig结构体的参数algo来配置精度计算模式。
该参数的默认值DEFAULT_SQRT_CONFIG的取值如下: constexpr SqrtConfig DEFAULT_SQRT_CONFIG = { SqrtAlgo::INTRINSIC };
|
表 2 参数说明
|
类型为LocalTensor,支持的TPosition为VECIN/VECCALC/VECOUT。 |
||
|
类型为LocalTensor,支持的TPosition为VECIN/VECCALC/VECOUT。 |
||
|
||
重复迭代次数。矢量计算单元,每次读取连续的256Bytes数据进行计算,为完成对输入数据的处理,必须通过多次迭代(repeat)才能完成所有数据的读取与计算。repeatTime表示迭代的次数。 |
||
控制操作数地址步长的参数。UnaryRepeatParams类型,包含操作数相邻迭代间相同DataBlock的地址步长,操作数同一迭代内不同DataBlock的地址步长等参数。 |
返回值说明
无
约束说明
调用示例
本样例的srcLocal和dstLocal均为half类型。
更多样例可参考LINK。
-
tensor高维切分计算样例-mask连续模式
uint64_t mask = 256 / sizeof(half); // repeatTime = 4, 128 elements one repeat, 512 elements total // dstBlkStride, srcBlkStride = 1, no gap between blocks in one repeat // dstRepStride, srcRepStride = 8, no gap between repeats AscendC::Sqrt(dstLocal, srcLocal, mask, 4, { 1, 1, 8, 8 }); -
tensor高维切分计算样例-mask逐bit模式
uint64_t mask[2] = { UINT64_MAX, UINT64_MAX }; // repeatTime = 4, 128 elements one repeat, 512 elements total // dstBlkStride, srcBlkStride = 1, no gap between blocks in one repeat // dstRepStride, srcRepStride = 8, no gap between repeats AscendC::Sqrt(dstLocal, srcLocal, mask, 4, { 1, 1, 8, 8 }); -
tensor前n个数据计算样例
AscendC::Sqrt(dstLocal, srcLocal, 512); // Sqrt 0ulp static constexpr SqrtConfig config = { SqrtAlgo::FAST_INVERSE }; Sqrt<T, config>(dstLocal, srcLocal, 512); // Sqrt Subnormal static constexpr SqrtConfig config = { SqrtAlgo::PRECISION_0ULP_FTZ_FALSE }; Sqrt<T, config>(dstLocal, srcLocal, 512);
结果示例如下:
输入数据srcLocal:[1.0 2.0 3.0 4.0 ...]
输出数据dstLocal:[1.0 1.414 1.732 2.0...]