Cast (Multiple Types to float)

Applicability

Product

Supported

Atlas 350 Accelerator Card

Atlas A3 training product/Atlas A3 inference product

Atlas A2 training product/Atlas A2 inference product

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

Converts the input data to the float type.

Prototype

  • Converts bfloat16_t to float.
    1
    __aicore__ inline float Cast(const bfloat16_t& bVal)
    
  • Converts multiple data types to float.
    1
    2
    3
    4
    5
    6
    7
    template <typename T, typename U = float,
              typename = Std::enable_if_t<
              (Std::is_same<T, bfloat16_t>::value || Std::is_same<T, hifloat8_t>::value ||
               Std::is_same<T, fp8_e5m2_t>::value || Std::is_same<T, fp8_e4m3fn_t>::value ||
               Std::is_same<T, fp4x2_e1m2_t>::value || Std::is_same<T, fp4x2_e2m1_t>::value), 
              void>>
    __aicore__ constexpr inline U Cast(T bVal)
    

Parameters

Table 1 Template parameters

Parameter

Description

T

Operand data type.

For the Atlas 350 Accelerator Card, the supported data types are fp4x2_e2m1_t, fp4x2_e1m2_t, hifloat8_t, fp8_e5m2_t, fp8_e4m3fn_t, and bfloat16_t.

To meet the 1-byte data size requirement, the fp4x2_e1m2_t and fp4x2_e2m1_t types are constructed by concatenating two 4-bit scalar data elements. During conversion, the lower 4 bits of the fp4x2_e1m2_t and fp4x2_e2m1_t scalar data are converted, that is, bits [0:3].

U

Data type of the return value. Only float is supported.

Table 2 API parameters

Parameter

Input/Output

Meaning

bVal

Input

Scalar data to be converted.

Returns

Converted scalar data of the float type.

Constraints

None

Example

1
2
3
4
// Convert the first element of srcLocal to the float type using Cast and assign the result to fValue.
float fValue = AscendC::Cast(srcLocal.GetValue(0));
// Fill the scalar fValue into the first 32 positions of dstLocal.
AscendC::Duplicate(dstLocal, fValue, 32);

Result example:

1
2
3
4
Input (srcLocal):
[21.3750, 74.0000, 57.2500,  ...,  2.4062, 72.5000, 67.5000]
Output (dstLocal):
[21.375 21.375 21.375 ... 21.375 21.375 21.375]
1
2
3
4
5
6
// The following is an example of the input type fp4x2_e1m2_t:
float fValue = AscendC::Cast<T, float>(srcLocal.GetValue(0));
AscendC::Duplicate(dstLocal, fValue, bufferSize);

// Input (srcLocal) (binary expression): [0b10001111 0b10001111 0b10001111 ...]
// Output (dstLocal): [-1.75 -1.75 -1.75 ...]