ToFloat
Applicability
Product |
Supported/Unsupported |
|---|---|
√ |
|
√ |
|
x |
|
x |
|
x |
|
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
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); // ...... } |
Parent topic: Scalar Computation