okftype: reference.api description: "关闭Mmad计算的HF32模式,使L0A Buffer与L0B Buffer中的float数据在参与矩阵乘加前不做舍入处理,恢复FP32直算;须在asc_mmad执行前调用,与asc_enable_hf32互斥,仅AIC生效。" tag:
- roles: [configuration] dependencies:
- path: docs/zh/api/SIMD-API/c_api/cube_compute/mmad_compute/asc_mmad.md kind: constrained_by condition: "须在矩阵乘加指令执行前调用"
- path: docs/zh/api/SIMD-API/c_api/cube_compute/mmad_compute_aux_config/asc_enable_hf32.md kind: related condition: "作用相反,二者不同时生效,开启HF32需改用对方接口"
- path: docs/zh/api/SIMD-API/c_api/cube_compute/mmad_compute_key_features/HF32.md kind: related condition: "HF32格式、舍入模式与精度影响说明" version: ">=9.2.0" version_changes:
- since: "9.2.0" kind: renamed scope: "page" scope_basis: kind_default note: "以 asc_disable_hf32 取代 9.2.0 废弃的 asc_set_fp32_mode:两者实现同为清除控制寄存器第46位(HF32_MODE_BIT),签名均为无参无返回,语义不变;依据为 impl 对比与 ASC_DEPRECATED(9.2.0, ..., asc_disable_hf32)。"
asc_disable_hf32
产品支持情况
- Ascend 950PR&950DT系列产品:支持
- Atlas A3系列产品:支持
- Atlas A2系列产品:支持
- Atlas 200I/500 A2推理产品:不支持
- Atlas推理系列产品AI Core:不支持
- Atlas推理系列产品Vector Core:不支持
- Atlas训练系列产品:不支持
功能说明
头文件路径为:"c_api/cube_compute/cube_compute.h"。
本接口用于设置Mmad计算关闭HF32模式,其作用与asc_enable_hf32相反,两个接口不同时生效。关闭HF32模式后,L0A Buffer与L0B Buffer中的float数据在参与Mmad计算之前不做舍入处理。
本接口为矩阵计算相关配置接口,仅在AIC上生效。
HF32格式、舍入模式及对计算精度的影响参见HF32特性说明。
函数原型
C
__aicore__ inline void asc_disable_hf32()
参数说明
无
返回值说明
无
流水类型
PIPE_S
约束说明
- 本接口非AIC调用直接返回。
- 本接口需在矩阵乘加指令(asc_mmad)执行前调用,以此来确保模式配置在矩阵乘加计算过程中生效。
- 与asc_enable_hf32作用相反,二者不同时生效。
调用示例
将代码保存为example.asc后,可通过bisheng命令编译运行,其中--npu-arch参数需根据实际产品型号指定对应的NPU架构,具体产品与NPU架构的映射关系请参考__NPU_ARCH__。
以Ascend 950PR&950DT系列产品(对应NPU架构为dav-3510)为例,编译运行命令如下:
Bash
bisheng example.asc -o main --npu-arch=dav-3510 && ./main
以下调用示例代码仅Ascend 950PR&950DT系列产品支持。
C++
#include <cmath>
#include <cstdint>
#include <iostream>
#include <vector>
#include "c_api/asc_simd.h"
#include "acl/acl.h"
namespace {
constexpr uint32_t DIM = 16, ELEMENTS = DIM * DIM;
__global__ __cube__ void asc_disable_hf32_kernel(
__gm__ float* a, __gm__ float* b, __gm__ float* hf32_output, __gm__ float* fp32_output)
{
asc_init();
__cbuf__ float a_l1[ELEMENTS], b_l1[ELEMENTS];
__ca__ float a_l0[ELEMENTS];
__cb__ float b_l0[ELEMENTS];
__cc__ float hf32_l0[ELEMENTS], fp32_l0[ELEMENTS];
asc_set_gm2l1_nz_para(1, 1, 16, 0);
asc_copy_gm2l1_nd2nz(a_l1, a, DIM * sizeof(float), asc_load_l2_cache_mode::NORMAL_FIRST_VICTIM,
DIM, DIM, 0, false);
asc_set_gm2l1_nz_para(1, 1, 16, 0);
asc_copy_gm2l1_nd2nz(b_l1, b, DIM * sizeof(float), asc_load_l2_cache_mode::NORMAL_FIRST_VICTIM,
DIM, DIM, 0, false);
asc_sync_notify(PIPE_MTE2, PIPE_MTE1, EVENT_ID0);
asc_sync_wait(PIPE_MTE2, PIPE_MTE1, EVENT_ID0);
asc_copy_l12l0a(a_l0, a_l1, 0, 0, 1, 2, 1, 1);
asc_copy_l12l0b_transpose(b_l0, b_l1, 0, 0, 1, 2, 1, 1);
asc_sync_notify(PIPE_MTE1, PIPE_M, EVENT_ID0);
asc_sync_wait(PIPE_MTE1, PIPE_M, EVENT_ID0);
asc_enable_hf32();
asc_set_hf32_round_mode(asc_hf32_round_mode::NEAREST_EVEN);
asc_mmad(hf32_l0, a_l0, b_l0, DIM, DIM, DIM, asc_unit_flag_mode::DISABLE, true, false, true);
asc_sync_pipe(PIPE_M);
asc_disable_hf32();
asc_mmad(fp32_l0, a_l0, b_l0, DIM, DIM, DIM, asc_unit_flag_mode::DISABLE, true, false, true);
asc_sync_pipe(PIPE_M);
asc_sync_notify(PIPE_M, PIPE_FIX, EVENT_ID0);
asc_sync_wait(PIPE_M, PIPE_FIX, EVENT_ID0);
asc_set_l0c_copy_nz_para(1, 0, 0);
asc_copy_l0c2gm(hf32_output, hf32_l0, DIM, DIM, DIM, DIM,
asc_store_l2_cache_mode::NORMAL_FIRST_VICTIM, asc_unit_flag_mode::DISABLE, asc_quant_mode::NoQuant,
asc_relu_pre_mode::NONE, false, true, false, false);
asc_set_l0c_copy_nz_para(1, 0, 0);
asc_copy_l0c2gm(fp32_output, fp32_l0, DIM, DIM, DIM, DIM,
asc_store_l2_cache_mode::NORMAL_FIRST_VICTIM, asc_unit_flag_mode::DISABLE, asc_quant_mode::NoQuant,
asc_relu_pre_mode::NONE, false, true, false, false);
asc_sync();
}
void print_row(const char* label, const std::vector<float>& data)
{
std::cout << label << ':';
for (uint32_t i = 0; i < 8; ++i) std::cout << ' ' << data[i];
std::cout << " ..." << std::endl;
}
} // namespace
int main()
{
std::vector<float> a(ELEMENTS), b(ELEMENTS), hf32(ELEMENTS), fp32(ELEMENTS), golden(ELEMENTS);
for (uint32_t i = 0; i < DIM; ++i) {
a[i * DIM + i] = golden[i * DIM + i] = 1.0003f;
b[i * DIM + i] = 1.0f;
}
aclInit(nullptr);
aclrtSetDevice(0);
float *a_device = nullptr, *b_device = nullptr, *hf32_device = nullptr, *fp32_device = nullptr;
aclrtMalloc(reinterpret_cast<void**>(&a_device), ELEMENTS * sizeof(float), ACL_MEM_MALLOC_HUGE_FIRST);
aclrtMalloc(reinterpret_cast<void**>(&b_device), ELEMENTS * sizeof(float), ACL_MEM_MALLOC_HUGE_FIRST);
aclrtMalloc(reinterpret_cast<void**>(&hf32_device), ELEMENTS * sizeof(float), ACL_MEM_MALLOC_HUGE_FIRST);
aclrtMalloc(reinterpret_cast<void**>(&fp32_device), ELEMENTS * sizeof(float), ACL_MEM_MALLOC_HUGE_FIRST);
aclrtMemcpy(a_device, ELEMENTS * sizeof(float), a.data(), ELEMENTS * sizeof(float), ACL_MEMCPY_HOST_TO_DEVICE);
aclrtMemcpy(b_device, ELEMENTS * sizeof(float), b.data(), ELEMENTS * sizeof(float), ACL_MEMCPY_HOST_TO_DEVICE);
asc_disable_hf32_kernel<<<1, 0>>>(a_device, b_device, hf32_device, fp32_device);
aclrtSynchronizeDevice();
aclrtMemcpy(hf32.data(), ELEMENTS * sizeof(float), hf32_device, ELEMENTS * sizeof(float), ACL_MEMCPY_DEVICE_TO_HOST);
aclrtMemcpy(fp32.data(), ELEMENTS * sizeof(float), fp32_device, ELEMENTS * sizeof(float), ACL_MEMCPY_DEVICE_TO_HOST);
print_row("Input A row 0", a);
print_row("HF32 output row 0", hf32);
print_row("Restored FP32 output row 0", fp32);
print_row("FP32 golden row 0", golden);
const bool passed = fp32 == golden && std::fabs(hf32[0] - fp32[0]) > 1e-5f;
std::cout << (passed ? "[Success] asc_disable_hf32 restores full-precision MMAD."
: "[Failed] asc_disable_hf32 result mismatch.") << std::endl;
aclrtFree(a_device); aclrtFree(b_device); aclrtFree(hf32_device); aclrtFree(fp32_device);
aclrtResetDevice(0);
aclFinalize();
return passed ? 0 : 1;
}