okftype: reference.api description: "在 L0C Buffer 搬出并启用随路 Nz 转 ND 或 DN 格式时,配置矩阵数量、源相邻 Nz 矩阵偏移与目的相邻 ND 或 DN 矩阵偏移,仅 Ascend 950PR&950DT 支持。" tag:
- roles: [data_movement_configuration] dependencies:
- path: docs/zh/api/SIMD-API/c_api/cube_datamove/cube_compute_store/asc_copy_l0c2gm_arch_3510.md kind: configures condition: "搬出接口 enable_nz2nd 或 enable_nz2dn 为 true 时必须先调用本接口"
- path: docs/zh/api/SIMD-API/c_api/cube_datamove/cube_compute_store/asc_copy_l0c2l1_arch_3510.md kind: configures condition: "搬出接口 enable_nz2nd 或 enable_nz2dn 为 true 时必须先调用本接口"
- path: docs/zh/api/SIMD-API/c_api/cube_datamove/cube_compute_store/asc_copy_l0c2ub.md kind: configures condition: "搬出接口 enable_nz2nd 或 enable_nz2dn 为 true 时必须先调用本接口"
- path: docs/zh/api/SIMD-API/c_api/cube_datamove/cube_store_aux_config/asc_set_l0c_copy_channel_para.md kind: related condition: "Nz2DN 场景还需由该接口设置源 Nz 分形行间隔"
- path: docs/zh/api/SIMD-API/c_api/cube_datamove/cube_store_key_features/NZ2DN.md kind: related
- path: docs/zh/api/SIMD-API/c_api/cube_datamove/cube_store_key_features/NZ2ND.md kind: related version: ">=9.2.0" version_changes:
- since: "9.2.0" kind: renamed scope: "page" scope_basis: kind_default note: "由 asc_set_l0c2gm_nz2nd(9.0.0 提供,参数为拼装的 config)改为 asc_set_l0c_copy_nz_para(matrix_num, src_nz_matrix_stride, dst_matrix_stride),参数个数、类型与单位随之调整(源步长单位 64 字节、目的步长单位为元素);旧名保留为 deprecated_interface 页面。"
asc_set_l0c_copy_nz_para
产品支持情况
- Ascend 950PR&950DT系列产品:支持
- Atlas A3系列产品:不支持
- Atlas A2系列产品:不支持
- Atlas 200I/500 A2推理产品:不支持
- Atlas推理系列产品AI Core:不支持
- Atlas推理系列产品Vector Core:不支持
- Atlas训练系列产品:不支持
功能说明
头文件路径为:"c_api/cube_datamove/cube_datamove.h"。
本接口用于在L0C Buffer搬出接口asc_copy_l0c2gm、asc_copy_l0c2l1、asc_copy_l0c2ub启用随路Nz转ND/DN格式功能时,配置格式转换所需的相关参数。
本接口为矩阵搬出相关配置接口,仅在AIC上生效。
矩阵数量、源和目的矩阵间隔的图示见NZ2ND和NZ2DN;Nz2DN场景还需配合asc_set_l0c_copy_channel_para设置源Nz分形行间隔。
函数原型
C
__aicore__ inline void asc_set_l0c_copy_nz_para(uint16_t matrix_num,
uint16_t src_nz_matrix_stride,
uint32_t dst_matrix_stride)
参数说明
表1 参数说明
| 参数名 | 输入/输出 | 描述 |
|---|---|---|
| matrix_num | 输入 | ND/DN矩阵的数量,取值范围为[1, 65535]。 |
| src_nz_matrix_stride | 输入 | 源相邻Nz矩阵之间的偏移(相邻Nz矩阵头与头的间隔)。当matrix_num配置为1时,src_nz_matrix_stride不生效,配置为0即可。取值范围为[0, 65535],单位为64字节(16 × sizeof(dtype),dtype为L0C Buffer上数据的类型,支持int32_t和float)。 |
| dst_matrix_stride | 输入 | 目的相邻ND/DN矩阵之间的偏移(相邻ND/DN矩阵头与头的间隔)。当matrix_num配置为1时,dst_matrix_stride不生效,配置为0即可。取值范围为[0, 2^32-1],单位为元素。 |
返回值说明
无
流水类型
PIPE_S
约束说明
- 本接口非AIC调用直接返回。
- 调用L0C Buffer搬出接口asc_copy_l0c2gm、asc_copy_l0c2l1、asc_copy_l0c2ub并且需要进行随路Nz转ND/DN格式之前,即搬出接口内参数
enable_nz2nd与enable_nz2dn其一设置为true时,必须先调用本接口配置格式转换参数。
调用示例
将代码保存为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 <cstdint>
#include <iostream>
#include <vector>
#include "c_api/asc_simd.h"
#include "acl/acl.h"
namespace {
constexpr uint32_t DIM = 16, ELEMENTS = DIM * DIM;
constexpr uint16_t HALF_ONE = 0x3c00;
__global__ __cube__ void asc_set_l0c_copy_nz_para_kernel(
__gm__ uint16_t* a, __gm__ uint16_t* b, __gm__ float* output)
{
asc_init();
__cbuf__ half a_l1[ELEMENTS], b_l1[ELEMENTS];
__ca__ half a_l0[ELEMENTS];
__cb__ half b_l0[ELEMENTS];
__cc__ float c_l0[ELEMENTS];
asc_set_gm2l1_nz_para(1, 1, 16, 0);
asc_copy_gm2l1_nd2nz(a_l1, reinterpret_cast<__gm__ half*>(a), DIM * sizeof(half), 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, reinterpret_cast<__gm__ half*>(b), DIM * sizeof(half), 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, 1, 1, 1);
asc_copy_l12l0b_transpose(b_l0, b_l1, 0, 0, 1, 1, 1, 1);
asc_sync_notify(PIPE_MTE1, PIPE_M, EVENT_ID0);
asc_sync_wait(PIPE_MTE1, PIPE_M, EVENT_ID0);
asc_mmad(c_l0, a_l0, b_l0, DIM, DIM, DIM, asc_unit_flag_mode::DISABLE, true, false, true);
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(output, c_l0, DIM, DIM, DIM, DIM, asc_store_l2_cache_mode::NORMAL_FIRST_VICTIM,
asc_unit_flag_mode::DISABLE, QuantMode_t::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<uint16_t> a(ELEMENTS), b(ELEMENTS);
std::vector<float> output(ELEMENTS), golden(ELEMENTS);
for (uint32_t i = 0; i < DIM; ++i) {
a[i * DIM + i] = HALF_ONE;
b[i * DIM + i] = HALF_ONE;
golden[i * DIM + i] = 1.0f;
}
aclInit(nullptr);
aclrtSetDevice(0);
uint16_t *a_device = nullptr, *b_device = nullptr;
float* output_device = nullptr;
aclrtMalloc(reinterpret_cast<void**>(&a_device), a.size() * sizeof(uint16_t), ACL_MEM_MALLOC_HUGE_FIRST);
aclrtMalloc(reinterpret_cast<void**>(&b_device), b.size() * sizeof(uint16_t), ACL_MEM_MALLOC_HUGE_FIRST);
aclrtMalloc(reinterpret_cast<void**>(&output_device), output.size() * sizeof(float), ACL_MEM_MALLOC_HUGE_FIRST);
aclrtMemcpy(a_device, a.size() * sizeof(uint16_t), a.data(), a.size() * sizeof(uint16_t), ACL_MEMCPY_HOST_TO_DEVICE);
aclrtMemcpy(b_device, b.size() * sizeof(uint16_t), b.data(), b.size() * sizeof(uint16_t), ACL_MEMCPY_HOST_TO_DEVICE);
asc_set_l0c_copy_nz_para_kernel<<<1, 0>>>(a_device, b_device, output_device);
aclrtSynchronizeDevice();
aclrtMemcpy(output.data(), output.size() * sizeof(float), output_device, output.size() * sizeof(float),
ACL_MEMCPY_DEVICE_TO_HOST);
print_row("ND output row 0", output);
print_row("Golden row 0", golden);
const bool passed = output == golden;
std::cout << (passed ? "[Success] asc_set_l0c_copy_nz_para produced row-major ND output."
: "[Failed] asc_set_l0c_copy_nz_para result mismatch.") << std::endl;
aclrtFree(a_device); aclrtFree(b_device); aclrtFree(output_device);
aclrtResetDevice(0);
aclFinalize();
return passed ? 0 : 1;
}