Sqrt

Applicability

Product

Supported

Atlas 350 Accelerator Card

Atlas A3 training product/Atlas A3 inference product

x

Atlas A2 training product/Atlas A2 inference product

x

Atlas 200I/500 A2 inference product

x

Atlas inference product AI Core

x

Atlas inference product Vector Core

x

Atlas training product

x

Function Usage

Applies the square root (Sqrt) operation to the input data srcReg based on the mask, and writes the result to dstReg. The formula is as follows:

Prototype

template <typename T = DefaultType, auto mode = MaskMergeMode::ZEROING, typename U>
__simd_callee__ inline void Sqrt(U& dstReg, U& srcReg, MaskReg& mask)

Parameters

Table 1 Parameters in the template

Parameter

Description

T

Operand data type.

For the Atlas 350 Accelerator Card, the supported data types are half and float.

mode

It can be set to the enumeration of MaskMergeMode or a pointer to a structure of SqrtSpecificMode.
  • MaskMergeMode: Set it to MERGING or ZEROING.
    • ZEROING: The elements that are not filtered by mask are set to zero in dstReg.
    • MERGING: This option is not supported currently.
  • SqrtSpecificMode is defined as follows:
    enum class SqrtAlgo {
        INTRINSIC = 0,
        FAST_INVERSE,
        PRECISION_1ULP_FTZ_TRUE,
        PRECISION_0ULP_FTZ_FALSE,
        PRECISION_1ULP_FTZ_FALSE,
    };
    struct SqrtSpecificMode {
        MaskMergeMode mrgMode = MaskMergeMode::ZEROING,
        bool precisionMode = false;
        SqrtAlgo algo = SqrtAlgo::INTRINSIC;
    };
    • mrgMode: Set it to MERGING or ZEROING.
    • precisionMode: is used to set the precision mode.

      When precisionMode is set to true, the Sqrt operation with higher precision is enabled, and the fast inverse algorithm is used to obtain the result. This algorithm is applicable when the input value is within the range [0, 85070596800837026223494223584045301760]. Within this range, the algorithm guarantees a maximum precision error of 0 ulp for the output. When the input value exceeds 85070596800837026223494223584045301760, the output is 0. Currently, this algorithm takes effect only for the float data type.

    • algo: used to configure the subnormal mode.
      • SqrtAlgo::INTRINSIC and SqrtAlgo::PRECISION_1ULP_FTZ_TRUE: The result is computed using a single instruction, with a maximum precision error of 1 ulp.
      • SqrtAlgo::FAST_INVERSE and SqrtAlgo::PRECISION_0ULP_FTZ_FALSE: The result is computed using a fast inverse algorithm. This algorithm is applicable when the input value is within the range [0, 85070596800837026223494223584045301760]. Within this range, the algorithm guarantees a maximum precision error of 0 ulp for the output. When the input value exceeds 85070596800837026223494223584045301760, the output is 0. Currently, this algorithm supports only the float data type and subnormal data computation in this mode.
      • SqrtAlgo::PRECISION_1ULP_FTZ_FALSE: Only subnormal data computation of the half type is supported. In this case, the maximum precision error is 1 ulp.

U

RegTensor type of the source and destination operands, for example, RegTensor<half>. It is automatically inferred by the compiler and does not need to be specified.

Table 2 Parameters

Parameter

Input/Output

Description

dstReg

Output

Destination operand.

The type is RegTensor.

srcReg

Input

Source operand.

The type is RegTensor.

The source operand must have the same data type as the destination operand.

mask

Input

Valid indication of the source operand element operation. For details, see MaskReg.

Returns

None

Restrictions

If the value of srcReg is not positive, unpredictable results may occur.

Examples

template<typename T>
__simd_vf__ inline void SqrtVF(__ubuf__ T* dstAddr, __ubuf__ T* srcAddr, uint32_t count, 
 uint32_t oneRepeatSize, uint16_t repeatTimes)
{
    AscendC::Reg::RegTensor<T> srcReg;
    AscendC::Reg::RegTensor<T> dstReg;
    AscendC::Reg::MaskReg mask;   
    // High-precision mode
    // static constexpr AscendC::Reg::SqrtSpecificMode mode = {MaskMergeMode::ZEROING, true};
    // Subnormal mode
    // static constexpr AscendC::Reg::SqrtSpecificMode mode = {MaskMergeMode::ZEROING, true, SqrtAlgo::PRECISION_0ULP_FTZ_FALSE};
    for (uint16_t i = 0; i < repeatTimes; i++) {
        mask = AscendC::Reg::UpdateMask<T>(count);
        AscendC::Reg::LoadAlign(srcReg, srcAddr + i * oneRepeatSize);
        AscendC::Reg::Sqrt(dstReg, srcReg, mask);
        // High-precision or subnormal mode
        // AscendC::Reg::Sqrt<T, &mode>(dstReg, srcReg, mask);
        AscendC::Reg::StoreAlign(dstAddr + i * oneRepeatSize, dstReg, mask);
    }
}