将输入数据转换为float类型。
1 | __aicore__ inline float ToFloat(const bfloat16_t& bVal) |
参数名称 |
输入/输出 |
含义 |
---|---|---|
bVal |
输入 |
待转换的标量数据。 |
无
转换后的float类型标量数据。
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);// 对标量进行加法,不支持bfloat16_t,需要先转换成float PipeBarrier<PIPE_ALL>(); AscendC::Duplicate(srcLocal, float(4.0f), dataLen); PipeBarrier<PIPE_ALL>(); Adds(srcLocal, srcLocal, t, dataLen); PipeBarrier<PIPE_ALL>(); // 做加法运算后,输出bfloat16_t类型tensor Cast(dstLocal, srcLocal, AscendC::RoundMode::CAST_ROUND, dataLen); // …… } |