ToFloat

Applicability

Product

Supported/Unsupported

Atlas A3 training products/Atlas A3 inference products

Atlas A2 training products/Atlas A2 inference products

Atlas 200I/500 A2 inference products

x

Atlas inference product's AI Core

x

Atlas inference product's Vector Core

x

Atlas training products

x

Function Usage

Converts the input data to the float type.

Prototype

  • Converts bfloat16_t to float.
    1
    __aicore__ inline float ToFloat(const bfloat16_t& bVal)
    

Parameters

Table 1 API parameters

Parameter

Input/Output

Meaning

bVal

Input

Scalar data to be converted.

Returns

Converted scalar data of the float type.

Restrictions

None

Example

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
void CalcFunc(bfloat16_t n)
{
	int dataLen = 32;
	AscendC::TPipe pipe;
	AscendC::TQue<AscendC::TPosition::VECIN, 1> inQueueSrcVecIn;
	AscendC::TQue<AscendC::TPosition::VECOUT, 1> inQueueDstVecIn;
	pipe.InitBuffer(inQueueDstVecIn, 1, dataLen * sizeof(bfloat16_t));
	pipe.InitBuffer(inQueueSrcVecIn, 1, dataLen * sizeof(float));
	AscendC::LocalTensor<bfloat16_t> dstLocal = inQueueDstVecIn.AllocTensor<bfloat16_t>();
	AscendC::LocalTensor<float> srcLocal = inQueueSrcVecIn.AllocTensor<float>();
	float t = AscendC::ToFloat(n);// Add scalars. bfloat16_t is not supported and needs to be converted to float first.
	PipeBarrier<PIPE_ALL>();
	AscendC::Duplicate(srcLocal, float(4.0f), dataLen);
	PipeBarrier<PIPE_ALL>();
	Adds(srcLocal, srcLocal, t, dataLen);
	PipeBarrier<PIPE_ALL>();
	// Output a tensor of the bfloat16_t type after the addition operation.
	Cast(dstLocal, srcLocal, AscendC::RoundMode::CAST_ROUND, dataLen);
	// ......
}