asc_float2half
产品支持情况
- Ascend 950PR/Ascend 950DT:支持
- Atlas A3 训练系列产品/Atlas A3 推理系列产品:不支持
- Atlas A2 训练系列产品/Atlas A2 推理系列产品:不支持
- Atlas 200I/500 A2 推理产品:不支持
- Atlas 推理系列产品AI Core:不支持
- Atlas 推理系列产品Vector Core:不支持
- Atlas 训练系列产品:不支持
功能说明
将float类型的标量转换为half类型并返回,舍入模式固定为ODD(最近邻奇数舍入)。
关于舍入模式和饱和/非饱和模式的详细说明,请参见舍入模式与饱和模式。
函数原型
C
__aicore__ inline half asc_float2half_ro(float value)
参数说明
表1 参数说明
| 参数名 | 输入/输出 | 描述 |
|---|---|---|
| value | 输入 | 源操作数(标量),数据类型为float。 |
返回值说明
返回输入数据value类型转换成half的结果。
约束说明
- 饱和/非饱和模式可通过asc_set_saturation_flag接口配置。
nan、inf和超出half表示范围的输入值的处理方式,请参见舍入模式与饱和模式。- 本接口运行在标量流水(
PIPE_S)上,同一流水内的数据依赖由指令执行顺序保证,无需额外同步。
调用示例
将代码保存为example.asc后,可通过bisheng命令编译运行,其中--npu-arch参数需根据实际产品型号指定对应的NPU架构,具体产品与NPU架构的映射关系请参考__NPU_ARCH__。
以Ascend 950PR/Ascend 950DT产品(对应NPU架构为dav-3510)为例,编译运行命令如下:
Bash
bisheng example.asc -o main --npu-arch=dav-3510 && ./main
C++
#include <cstdint>
#include <iostream>
#include <vector>
#include "c_api/asc_simd.h"
#include "acl/acl.h"
namespace {
constexpr uint32_t ELEMENTS = 1;
constexpr uint32_t BYTES = ELEMENTS * sizeof(half);
constexpr float INPUT_VALUE = 1.5f;
__global__ __vector__ void asc_float2half_kernel(__gm__ half* output)
{
asc_init();
output[0] = asc_float2half_ro(INPUT_VALUE);
}
} // namespace
int main()
{
std::vector<half> output(ELEMENTS, half(0.0f));
const std::vector<half> golden(ELEMENTS, half(INPUT_VALUE));
aclInit(nullptr);
aclrtSetDevice(0);
half* output_device = nullptr;
aclrtMalloc(reinterpret_cast<void**>(&output_device), BYTES, ACL_MEM_MALLOC_HUGE_FIRST);
asc_float2half_kernel<<<1, 0>>>(output_device);
aclrtSynchronizeDevice();
aclrtMemcpy(output.data(), BYTES, output_device, BYTES, ACL_MEMCPY_DEVICE_TO_HOST);
const bool passed = output == golden;
std::cout << (passed ? "[Success] asc_float2half_ro passed."
: "[Failed] asc_float2half_ro failed.")
<< std::endl;
aclrtFree(output_device);
aclrtResetDevice(0);
aclFinalize();
return passed ? 0 : 1;
}