Merged
增加data_copy_l0c2ub、data_copy_l0c2l1用例 #5346
yhf0922Add on Aug 26
增加data_copy_l0c2ub、data_copy_l0c2l1用例 #5346
Merged
Merged fromyhf0922/asc-devkitcube_example_docintocann/asc-devkitmaster
Total: 19 files changed+2144-29
| @@ -9,8 +9,9 @@ | |||
| 9 | | 目录名称 | 功能描述 | 支持的产品 | | 9 | | 目录名称 | 功能描述 | 支持的产品 | |
| 10 | | --- | --- | --- | | 10 | | --- | --- | --- | |
| 11 | | [data_copy_gm2l1](./data_copy_gm2l1) | 本样例展示将输入矩阵数据从GM(Global Memory)搬运到L1(L1 Buffer)的过程,包含格式转换、矩阵乘计算以及通过Fixpipe进行的量化结果搬出。 | Ascend 950PR/Ascend 950DT | | 11 | | [data_copy_gm2l1](./data_copy_gm2l1) | 本样例展示将输入矩阵数据从GM(Global Memory)搬运到L1(L1 Buffer)的过程,包含格式转换、矩阵乘计算以及通过Fixpipe进行的量化结果搬出。 | Ascend 950PR/Ascend 950DT | |
| 12 | -| [data_copy_l0c2gm](./data_copy_l0c2gm) | 本样例展示通过Fixpipe将L0C(L0C Buffer)中的矩阵乘计算结果搬运到GM(Global Memory),并在搬运过程中完成标量和Vector量化、ReLU激活以及Nz格式到ND格式的转换。 | Ascend 950PR/Ascend 950DT | | 12 | +| [data_copy_l0c2gm](./data_copy_l0c2gm) | 本样例展示通过asc_copy_l0c2gm将L0C(L0C Buffer)中的矩阵乘计算结果搬运到GM(Global Memory),并在搬运过程中完成标量和Vector量化、ReLU激活以及Nz格式到ND格式的转换。 | Ascend 950PR/Ascend 950DT | |
| 13 | -| [data_copy_ub2l1](./data_copy_ub2l1) | 本样例在原有Nz输入和ND输入两种UB(Unified Buffer)到L1(L1 Buffer)矩阵乘场景基础上使用非对齐shape,并新增GM->UB->L1->UB->GM双向通路场景。 | Ascend 950PR/Ascend 950DT | | 13 | +| [data_copy_l0c2l1](./data_copy_l0c2l1) | 本样例展示通过asc_copy_l0c2l1将L0C(L0C Buffer)中的矩阵乘计算结果搬运到L1(L1 Buffer),并在搬运过程中完成标量和Vector量化、ReLU激活以及Nz格式到ND格式的转换。 | Ascend 950PR/Ascend 950DT | |
| 14 | +| [data_copy_l0c2ub](./data_copy_l0c2ub) | 本样例展示通过asc_copy_l0c2ub将L0C(L0C Buffer)中的矩阵乘计算结果搬运到UB(Unified Buffer),并在搬运过程中完成标量和Vector量化、ReLU激活以及Nz格式到ND格式的转换。 | Ascend 950PR/Ascend 950DT | | ||
| 14 | | [reg_load_gather](./reg_load_gather) | 本样例演示离散数据搬入的过程,包含按元素索引的`asc_gather`和按数据块(DataBlock)索引的`asc_gather_datablock`两种场景。 | Ascend 950PR/Ascend 950DT | | 15 | | [reg_load_gather](./reg_load_gather) | 本样例演示离散数据搬入的过程,包含按元素索引的`asc_gather`和按数据块(DataBlock)索引的`asc_gather_datablock`两种场景。 | Ascend 950PR/Ascend 950DT | |
| 15 | | [reg_load_store_align](./reg_load_store_align) | 本样例展示使用Reg矢量计算接口实现UB(Unified Buffer)与矢量数据寄存器之间的连续和非连续的对齐数据搬运。 | Ascend 950PR/Ascend 950DT | | 16 | | [reg_load_store_align](./reg_load_store_align) | 本样例展示使用Reg矢量计算接口实现UB(Unified Buffer)与矢量数据寄存器之间的连续和非连续的对齐数据搬运。 | Ascend 950PR/Ascend 950DT | |
| 16 | | [reg_load_store_mask](./reg_load_store_mask) | 本样例展示使用Reg矢量计算接口实现UB(Unified Buffer)与掩码寄存器之间的加载和存储,以及基于掩码寄存器进行数据选择的操作。 | Ascend 950PR/Ascend 950DT | | 17 | | [reg_load_store_mask](./reg_load_store_mask) | 本样例展示使用Reg矢量计算接口实现UB(Unified Buffer)与掩码寄存器之间的加载和存储,以及基于掩码寄存器进行数据选择的操作。 | Ascend 950PR/Ascend 950DT | |
| @@ -9,8 +9,9 @@ This directory contains Ascend C C API examples for multiple data-movement inter | |||
| 9 | | Directory Name | Function Description | Supported Products | | 9 | | Directory Name | Function Description | Supported Products | |
| 10 | | --- | --- | --- | | 10 | | --- | --- | --- | |
| 11 | | [data_copy_gm2l1](./data_copy_gm2l1) | This example demonstrates moving input matrix data from GM (Global Memory) to L1 (L1 Buffer), including format conversion, matrix multiplication, and Fixpipe quantized-result copy-out. | Ascend 950PR/Ascend 950DT | | 11 | | [data_copy_gm2l1](./data_copy_gm2l1) | This example demonstrates moving input matrix data from GM (Global Memory) to L1 (L1 Buffer), including format conversion, matrix multiplication, and Fixpipe quantized-result copy-out. | Ascend 950PR/Ascend 950DT | |
| 12 | -| [data_copy_l0c2gm](./data_copy_l0c2gm) | This example demonstrates moving matrix multiplication results from L0C Buffer to GM (Global Memory) through Fixpipe, while performing scalar and Vector quantization, ReLU activation, and Nz-to-ND conversion during the transfer. | Ascend 950PR/Ascend 950DT | | 12 | +| [data_copy_l0c2gm](./data_copy_l0c2gm) | This example demonstrates using `asc_copy_l0c2gm` to move matrix multiplication results from L0C (L0C Buffer) to GM (Global Memory), while performing scalar and Vector quantization, ReLU activation, and Nz-to-ND conversion during the transfer. | Ascend 950PR/Ascend 950DT | |
| 13 | -| [data_copy_ub2l1](./data_copy_ub2l1) | This example keeps the original Nz-input and ND-input UB (Unified Buffer) to L1 (L1 Buffer) matrix multiplication scenarios with a non-aligned shape, and adds a GM->UB->L1->UB->GM bidirectional-path scenario. | Ascend 950PR/Ascend 950DT | | 13 | +| [data_copy_l0c2l1](./data_copy_l0c2l1) | This example demonstrates using `asc_copy_l0c2l1` to move matrix multiplication results from L0C (L0C Buffer) to L1 (L1 Buffer), while performing scalar and Vector quantization, ReLU activation, and Nz-to-ND conversion during the transfer. | Ascend 950PR/Ascend 950DT | |
| 14 | +| [data_copy_l0c2ub](./data_copy_l0c2ub) | This example demonstrates using `asc_copy_l0c2ub` to move matrix multiplication results from L0C (L0C Buffer) to UB (Unified Buffer), while performing scalar and Vector quantization, ReLU activation, and Nz-to-ND conversion during the transfer. | Ascend 950PR/Ascend 950DT | | ||
| 14 | | [reg_load_gather](./reg_load_gather) | This example demonstrates non-contiguous data loads, including element-indexed `asc_gather` and DataBlock-indexed `asc_gather_datablock` scenarios. | Ascend 950PR/Ascend 950DT | | 15 | | [reg_load_gather](./reg_load_gather) | This example demonstrates non-contiguous data loads, including element-indexed `asc_gather` and DataBlock-indexed `asc_gather_datablock` scenarios. | Ascend 950PR/Ascend 950DT | |
| 15 | | [reg_load_store_align](./reg_load_store_align) | This example demonstrates contiguous and non-contiguous aligned data movement between UB (Unified Buffer) and vector registers with Reg vector computation interfaces. | Ascend 950PR/Ascend 950DT | | 16 | | [reg_load_store_align](./reg_load_store_align) | This example demonstrates contiguous and non-contiguous aligned data movement between UB (Unified Buffer) and vector registers with Reg vector computation interfaces. | Ascend 950PR/Ascend 950DT | |
| 16 | | [reg_load_store_mask](./reg_load_store_mask) | This example demonstrates mask-register load and store between UB (Unified Buffer) and mask registers with Reg vector computation interfaces, as well as mask-register-based data selection. | Ascend 950PR/Ascend 950DT | | 17 | | [reg_load_store_mask](./reg_load_store_mask) | This example demonstrates mask-register load and store between UB (Unified Buffer) and mask registers with Reg vector computation interfaces, as well as mask-register-based data selection. | Ascend 950PR/Ascend 950DT | |
| @@ -10,7 +10,7 @@ | |||
| 10 | 10 | ||
| 11 | | 产品 | CANN软件版本 | | 11 | | 产品 | CANN软件版本 | |
| 12 | |------|-------------| | 12 | |------|-------------| |
| 13 | -| Ascend 950PR/Ascend 950DT | >= CANN 9.1.0 | | 13 | +| Ascend 950PR/Ascend 950DT | >= CANN 9.2.0 | |
| 14 | 14 | ||
| 15 | ## 目录结构介绍 | 15 | ## 目录结构介绍 |
| 16 | 16 | ||
| @@ -79,7 +79,7 @@ data_copy_l0c2gm | |||
| 79 | - 输出:C `[128, 256]`,Nz格式`int8_t` | 79 | - 输出:C `[128, 256]`,Nz格式`int8_t` |
| 80 | - 实现:使用`VREQ8`模式并启用ReLU | 80 | - 实现:使用`VREQ8`模式并启用ReLU |
| 81 | 81 | ||
| 82 | -对于场景2、4和6,量化参数先通过`asc_copy_gm2l1`从GM搬运到L1 Buffer,再通过`asc_copy_l12fb`搬运到Fixpipe Buffer,最后调用`asc_set_l0c2gm_config`配置Vector量化参数地址。`asc_set_l0c2gm_config`的参数地址以128B为单位,因此参数文件需按128B向上对齐,不足部分补0。本样例的`[256] uint64_t`参数共2048B,已满足要求。量化参数从L1 Buffer搬运至Fixpipe Buffer时,需要搬运2048B数据,数据搬运单位为64B,对应`asc_copy_l12fb`的`len_burst`配置为32。量化参数从L1 Buffer搬运至Fixpipe Buffer后,通过`asc_sync_pipe(PIPE_FIX)`确保参数和配置生效,随后通过Fixpipe将L0C Buffer中的计算结果搬运到GM(Global Memory)。 | 82 | +对于场景2、4和6,量化参数先通过`asc_copy_gm2l1`从GM搬运到L1 Buffer,再通过`asc_copy_l12fb`搬运到Fixpipe Buffer,最后调用`asc_set_l0c_copy_config`配置Vector量化参数地址。`asc_set_l0c_copy_config`的参数地址以128B为单位,因此参数文件需按128B向上对齐,不足部分补0。本样例的`[256] uint64_t`参数共2048B,已满足要求。量化参数从L1 Buffer搬运至Fixpipe Buffer时,需要搬运2048B数据,数据搬运单位为64B,对应`asc_copy_l12fb`的`len_burst`配置为32。量化参数从L1 Buffer搬运至Fixpipe Buffer后,通过`asc_sync_pipe(PIPE_FIX)`确保参数和配置生效,随后通过Fixpipe将L0C Buffer中的计算结果搬运到GM(Global Memory)。 |
| 83 | 83 | ||
| 84 | ## 编译运行 | 84 | ## 编译运行 |
| 85 | 85 | ||
| @@ -10,7 +10,7 @@ This example applies to Ascend 950PR/Ascend 950DT (`dav-3510`) and can run in NP | |||
| 10 | 10 | ||
| 11 | | Product | CANN version | | 11 | | Product | CANN version | |
| 12 | |------|-------------| | 12 | |------|-------------| |
| 13 | -| Ascend 950PR/Ascend 950DT | >= CANN 9.1.0 | | 13 | +| Ascend 950PR/Ascend 950DT | >= CANN 9.2.0 | |
| 14 | 14 | ||
| 15 | ## Directory Structure | 15 | ## Directory Structure |
| 16 | 16 | ||
| @@ -79,7 +79,7 @@ The device-side flow is as follows. Each K chunk follows `GM→L1 Buffer (MTE2) | |||
| 79 | - Output: C `[128, 256]` in Nz `int8_t` | 79 | - Output: C `[128, 256]` in Nz `int8_t` |
| 80 | - Implementation: Use `VREQ8` and enable ReLU | 80 | - Implementation: Use `VREQ8` and enable ReLU |
| 81 | 81 | ||
| 82 | -For scenarios 2, 4, and 6, the parameters are moved from GM to L1 Buffer with `asc_copy_gm2l1`, then from L1 Buffer to Fixpipe Buffer with `asc_copy_l12fb`. `asc_set_l0c2gm_config` configures the vector-quantization parameter address. Its parameter address is expressed in 128B units, so the parameter file must be rounded up to 128B and any padding must be zero-filled. The `[256] uint64_t` parameters in this example occupy 2048B and already meet this requirement. Moving these parameters from L1 Buffer to Fixpipe Buffer transfers 2048B of data in 64B data blocks, so `len_burst` in `asc_copy_l12fb` is set to 32. `asc_sync_pipe(PIPE_FIX)` completes the L1 Buffer-to-Fixpipe Buffer movement and parameter configuration before Fixpipe moves the L0C Buffer result to GM. | 82 | +For scenarios 2, 4, and 6, the parameters are moved from GM to L1 Buffer with `asc_copy_gm2l1`, then from L1 Buffer to Fixpipe Buffer with `asc_copy_l12fb`. `asc_set_l0c_copy_config` configures the vector-quantization parameter address. Its parameter address is expressed in 128B units, so the parameter file must be rounded up to 128B and any padding must be zero-filled. The `[256] uint64_t` parameters in this example occupy 2048B and already meet this requirement. Moving these parameters from L1 Buffer to Fixpipe Buffer transfers 2048B of data in 64B data blocks, so `len_burst` in `asc_copy_l12fb` is set to 32. `asc_sync_pipe(PIPE_FIX)` completes the L1 Buffer-to-Fixpipe Buffer movement and parameter configuration before Fixpipe moves the L0C Buffer result to GM. |
| 83 | 83 | ||
| 84 | ## Build and Run | 84 | ## Build and Run |
| 85 | 85 | ||
| @@ -79,16 +79,10 @@ constexpr uint16_t A_MTE2_NZ_C0_STRIDE = | |||
| 79 | static_cast<uint16_t>(IS_INT8_INPUT ? ceil_align(M, INT8_C0_ELEMENTS) : ceil_align(M, HALF_C0_ELEMENTS)); | 79 | static_cast<uint16_t>(IS_INT8_INPUT ? ceil_align(M, INT8_C0_ELEMENTS) : ceil_align(M, HALF_C0_ELEMENTS)); |
| 80 | constexpr uint16_t B_MTE2_NZ_C0_STRIDE = static_cast<uint16_t>( | 80 | constexpr uint16_t B_MTE2_NZ_C0_STRIDE = static_cast<uint16_t>( |
| 81 | IS_INT8_INPUT ? ceil_align(K_CHUNK, INT8_C0_ELEMENTS) : ceil_align(K_CHUNK, HALF_C0_ELEMENTS)); | 81 | IS_INT8_INPUT ? ceil_align(K_CHUNK, INT8_C0_ELEMENTS) : ceil_align(K_CHUNK, HALF_C0_ELEMENTS)); |
| 82 | -constexpr uint64_t A_MTE2_NZ_PARA = (static_cast<uint64_t>(MTE2_NZ_LOOP4_DST_STRIDE) << 48) | | 82 | +constexpr float SCALAR_QUANT_SCALE = 2.0F; |
| 83 | - (static_cast<uint64_t>(A_MTE2_NZ_C0_STRIDE) << 32) | | 83 | +constexpr uint16_t SCALAR_QUANT_OFFSET = 0; |
| 84 | - (static_cast<uint64_t>(MTE2_NZ_N_STRIDE) << 16) | MTE2_NZ_MATRIX_NUM; | 84 | +constexpr bool SCALAR_QUANT_HALF_IS_SIGNED = false; |
| 85 | -constexpr uint64_t B_MTE2_NZ_PARA = (static_cast<uint64_t>(MTE2_NZ_LOOP4_DST_STRIDE) << 48) | | 85 | +constexpr bool SCALAR_QUANT_INT8_IS_SIGNED = true; |
| 86 | - (static_cast<uint64_t>(B_MTE2_NZ_C0_STRIDE) << 32) | | ||
| 87 | - (static_cast<uint64_t>(MTE2_NZ_N_STRIDE) << 16) | MTE2_NZ_MATRIX_NUM; | ||
| 88 | - | ||
| 89 | -// The low 32 bits are the IEEE-754 representation of 2.0f. Bit 46 selects signed int8 output. | ||
| 90 | -constexpr uint64_t SCALAR_QUANT_HALF = 0x40000000ULL; | ||
| 91 | -constexpr uint64_t SCALAR_QUANT_INT8 = SCALAR_QUANT_HALF | (1ULL << 46); | ||
| 92 | // For Nz output, loop_dst_stride is measured in output elements. The API | 86 | // For Nz output, loop_dst_stride is measured in output elements. The API |
| 93 | // converts its 32B-block dstStride to this unit before issuing Fixpipe. | 87 | // converts its 32B-block dstStride to this unit before issuing Fixpipe. |
| 94 | constexpr uint32_t FIXPIPE_NZ_DST_STRIDE_HALF = M * C0_BYTES / sizeof(half); | 88 | constexpr uint32_t FIXPIPE_NZ_DST_STRIDE_HALF = M * C0_BYTES / sizeof(half); |
| @@ -112,11 +106,11 @@ __global__ __cube__ void data_copy_l0c2gm( | |||
| 112 | __cc__ int32_t* c_l0 = reinterpret_cast<__cc__ int32_t*>(c_l0_workspace); | 106 | __cc__ int32_t* c_l0 = reinterpret_cast<__cc__ int32_t*>(c_l0_workspace); |
| 113 | 107 | ||
| 114 | for (uint32_t k_index = 0; k_index < K_ROUND; ++k_index) { | 108 | for (uint32_t k_index = 0; k_index < K_ROUND; ++k_index) { |
| 115 | - asc_set_gm2l1_nz_para(A_MTE2_NZ_PARA); | 109 | + asc_set_gm2l1_nz_para(MTE2_NZ_MATRIX_NUM, MTE2_NZ_N_STRIDE, A_MTE2_NZ_C0_STRIDE, MTE2_NZ_LOOP4_DST_STRIDE); |
| 116 | asc_copy_gm2l1_nd2nz( | 110 | asc_copy_gm2l1_nd2nz( |
| 117 | a_l1, reinterpret_cast<__gm__ int8_t*>(a) + k_index * A_SINGLE_SIZE, K_CHUNK * sizeof(int8_t), | 111 | a_l1, reinterpret_cast<__gm__ int8_t*>(a) + k_index * A_SINGLE_SIZE, K_CHUNK * sizeof(int8_t), |
| 118 | L2_CACHE_NORMAL, M, K_CHUNK, 0, false); | 112 | L2_CACHE_NORMAL, M, K_CHUNK, 0, false); |
| 119 | - asc_set_gm2l1_nz_para(B_MTE2_NZ_PARA); | 113 | + asc_set_gm2l1_nz_para(MTE2_NZ_MATRIX_NUM, MTE2_NZ_N_STRIDE, B_MTE2_NZ_C0_STRIDE, MTE2_NZ_LOOP4_DST_STRIDE); |
| 120 | asc_copy_gm2l1_nd2nz( | 114 | asc_copy_gm2l1_nd2nz( |
| 121 | b_l1, reinterpret_cast<__gm__ int8_t*>(b) + k_index * B_SINGLE_SIZE, N * sizeof(int8_t), | 115 | b_l1, reinterpret_cast<__gm__ int8_t*>(b) + k_index * B_SINGLE_SIZE, N * sizeof(int8_t), |
| 122 | L2_CACHE_NORMAL, K_CHUNK, N, 0, false); | 116 | L2_CACHE_NORMAL, K_CHUNK, N, 0, false); |
| @@ -159,18 +153,18 @@ __global__ __cube__ void data_copy_l0c2gm( | |||
| 159 | QUANT_TRANSFER_BYTES / FIXPIPE_L1_TO_FB_BURST_BYTES, 0, 0); | 153 | QUANT_TRANSFER_BYTES / FIXPIPE_L1_TO_FB_BURST_BYTES, 0, 0); |
| 160 | const uint64_t quant_fb_addr = | 154 | const uint64_t quant_fb_addr = |
| 161 | static_cast<uint64_t>(reinterpret_cast<uintptr_t>(quant_fb)) / FIXPIPE_QUANT_ADDR_GRANULARITY; | 155 | static_cast<uint64_t>(reinterpret_cast<uintptr_t>(quant_fb)) / FIXPIPE_QUANT_ADDR_GRANULARITY; |
| 162 | - asc_set_l0c2gm_config(0, quant_fb_addr, false); | 156 | + asc_set_l0c_copy_config(0, quant_fb_addr, false); |
| 163 | asc_sync_pipe(PIPE_FIX); | 157 | asc_sync_pipe(PIPE_FIX); |
| 164 | } | 158 | } |
| 165 | 159 | ||
| 166 | asc_sync_notify(PIPE_M, PIPE_FIX, EVENT_ID0); | 160 | asc_sync_notify(PIPE_M, PIPE_FIX, EVENT_ID0); |
| 167 | asc_sync_wait(PIPE_M, PIPE_FIX, EVENT_ID0); | 161 | asc_sync_wait(PIPE_M, PIPE_FIX, EVENT_ID0); |
| 168 | if constexpr (IS_NZ2ND_OUTPUT) { | 162 | if constexpr (IS_NZ2ND_OUTPUT) { |
| 169 | - asc_set_l0c2gm_nz2nd(1, 0, 0); | 163 | + asc_set_l0c_copy_nz_para(1, 0, 0); |
| 170 | } | 164 | } |
| 171 | 165 | ||
| 172 | if constexpr (SCENARIO_NUM == 1) { | 166 | if constexpr (SCENARIO_NUM == 1) { |
| 173 | - asc_set_l0c_copy_prequant(SCALAR_QUANT_HALF); | 167 | + asc_set_l0c_copy_prequant(SCALAR_QUANT_SCALE, SCALAR_QUANT_OFFSET, SCALAR_QUANT_HALF_IS_SIGNED); |
| 174 | asc_copy_l0c2gm( | 168 | asc_copy_l0c2gm( |
| 175 | reinterpret_cast<__gm__ half*>(c), c_l0, N, M, N, ceil_align(M, CUBE_BLOCK), L0C2GM_L2_CACHE_NORMAL, | 169 | reinterpret_cast<__gm__ half*>(c), c_l0, N, M, N, ceil_align(M, CUBE_BLOCK), L0C2GM_L2_CACHE_NORMAL, |
| 176 | L0C2GM_UNIT_FLAG_DISABLE, QuantMode_t::DEQF16, L0C2GM_RELU_MODE, false, true, false, false); | 170 | L0C2GM_UNIT_FLAG_DISABLE, QuantMode_t::DEQF16, L0C2GM_RELU_MODE, false, true, false, false); |
| @@ -180,7 +174,7 @@ __global__ __cube__ void data_copy_l0c2gm( | |||
| 180 | ceil_align(M, CUBE_BLOCK), L0C2GM_L2_CACHE_NORMAL, L0C2GM_UNIT_FLAG_DISABLE, QuantMode_t::VDEQF16, | 174 | ceil_align(M, CUBE_BLOCK), L0C2GM_L2_CACHE_NORMAL, L0C2GM_UNIT_FLAG_DISABLE, QuantMode_t::VDEQF16, |
| 181 | L0C2GM_RELU_MODE, false, false, false, false); | 175 | L0C2GM_RELU_MODE, false, false, false, false); |
| 182 | } else if constexpr (SCENARIO_NUM == 5) { | 176 | } else if constexpr (SCENARIO_NUM == 5) { |
| 183 | - asc_set_l0c_copy_prequant(SCALAR_QUANT_INT8); | 177 | + asc_set_l0c_copy_prequant(SCALAR_QUANT_SCALE, SCALAR_QUANT_OFFSET, SCALAR_QUANT_INT8_IS_SIGNED); |
| 184 | asc_copy_l0c2gm( | 178 | asc_copy_l0c2gm( |
| 185 | reinterpret_cast<__gm__ int8_t*>(c), c_l0, N, M, N, ceil_align(M, CUBE_BLOCK), L0C2GM_L2_CACHE_NORMAL, | 179 | reinterpret_cast<__gm__ int8_t*>(c), c_l0, N, M, N, ceil_align(M, CUBE_BLOCK), L0C2GM_L2_CACHE_NORMAL, |
| 186 | L0C2GM_UNIT_FLAG_DISABLE, QuantMode_t::REQ8, L0C2GM_RELU_MODE, false, true, false, false); | 180 | L0C2GM_UNIT_FLAG_DISABLE, QuantMode_t::REQ8, L0C2GM_RELU_MODE, false, true, false, false); |
| @@ -198,11 +192,11 @@ __global__ __cube__ void data_copy_l0c2gm( | |||
| 198 | __cc__ float* c_l0 = reinterpret_cast<__cc__ float*>(c_l0_workspace); | 192 | __cc__ float* c_l0 = reinterpret_cast<__cc__ float*>(c_l0_workspace); |
| 199 | 193 | ||
| 200 | for (uint32_t k_index = 0; k_index < K_ROUND; ++k_index) { | 194 | for (uint32_t k_index = 0; k_index < K_ROUND; ++k_index) { |
| 201 | - asc_set_gm2l1_nz_para(A_MTE2_NZ_PARA); | 195 | + asc_set_gm2l1_nz_para(MTE2_NZ_MATRIX_NUM, MTE2_NZ_N_STRIDE, A_MTE2_NZ_C0_STRIDE, MTE2_NZ_LOOP4_DST_STRIDE); |
| 202 | asc_copy_gm2l1_nd2nz( | 196 | asc_copy_gm2l1_nd2nz( |
| 203 | a_l1, reinterpret_cast<__gm__ half*>(a) + k_index * A_SINGLE_SIZE, K_CHUNK * sizeof(half), | 197 | a_l1, reinterpret_cast<__gm__ half*>(a) + k_index * A_SINGLE_SIZE, K_CHUNK * sizeof(half), |
| 204 | L2_CACHE_NORMAL, M, K_CHUNK, 0, false); | 198 | L2_CACHE_NORMAL, M, K_CHUNK, 0, false); |
| 205 | - asc_set_gm2l1_nz_para(B_MTE2_NZ_PARA); | 199 | + asc_set_gm2l1_nz_para(MTE2_NZ_MATRIX_NUM, MTE2_NZ_N_STRIDE, B_MTE2_NZ_C0_STRIDE, MTE2_NZ_LOOP4_DST_STRIDE); |
| 206 | asc_copy_gm2l1_nd2nz( | 200 | asc_copy_gm2l1_nd2nz( |
| 207 | b_l1, reinterpret_cast<__gm__ half*>(b) + k_index * B_SINGLE_SIZE, N * sizeof(half), L2_CACHE_NORMAL, | 201 | b_l1, reinterpret_cast<__gm__ half*>(b) + k_index * B_SINGLE_SIZE, N * sizeof(half), L2_CACHE_NORMAL, |
| 208 | K_CHUNK, N, 0, false); | 202 | K_CHUNK, N, 0, false); |
| @@ -245,18 +239,18 @@ __global__ __cube__ void data_copy_l0c2gm( | |||
| 245 | QUANT_TRANSFER_BYTES / FIXPIPE_L1_TO_FB_BURST_BYTES, 0, 0); | 239 | QUANT_TRANSFER_BYTES / FIXPIPE_L1_TO_FB_BURST_BYTES, 0, 0); |
| 246 | const uint64_t quant_fb_addr = | 240 | const uint64_t quant_fb_addr = |
| 247 | static_cast<uint64_t>(reinterpret_cast<uintptr_t>(quant_fb)) / FIXPIPE_QUANT_ADDR_GRANULARITY; | 241 | static_cast<uint64_t>(reinterpret_cast<uintptr_t>(quant_fb)) / FIXPIPE_QUANT_ADDR_GRANULARITY; |
| 248 | - asc_set_l0c2gm_config(0, quant_fb_addr, false); | 242 | + asc_set_l0c_copy_config(0, quant_fb_addr, false); |
| 249 | asc_sync_pipe(PIPE_FIX); | 243 | asc_sync_pipe(PIPE_FIX); |
| 250 | } | 244 | } |
| 251 | 245 | ||
| 252 | asc_sync_notify(PIPE_M, PIPE_FIX, EVENT_ID0); | 246 | asc_sync_notify(PIPE_M, PIPE_FIX, EVENT_ID0); |
| 253 | asc_sync_wait(PIPE_M, PIPE_FIX, EVENT_ID0); | 247 | asc_sync_wait(PIPE_M, PIPE_FIX, EVENT_ID0); |
| 254 | if constexpr (IS_NZ2ND_OUTPUT) { | 248 | if constexpr (IS_NZ2ND_OUTPUT) { |
| 255 | - asc_set_l0c2gm_nz2nd(1, 0, 0); | 249 | + asc_set_l0c_copy_nz_para(1, 0, 0); |
| 256 | } | 250 | } |
| 257 | 251 | ||
| 258 | if constexpr (SCENARIO_NUM == 3) { | 252 | if constexpr (SCENARIO_NUM == 3) { |
| 259 | - asc_set_l0c_copy_prequant(SCALAR_QUANT_INT8); | 253 | + asc_set_l0c_copy_prequant(SCALAR_QUANT_SCALE, SCALAR_QUANT_OFFSET, SCALAR_QUANT_INT8_IS_SIGNED); |
| 260 | asc_copy_l0c2gm( | 254 | asc_copy_l0c2gm( |
| 261 | reinterpret_cast<__gm__ int8_t*>(c), c_l0, ceil_align(N, CUBE_BLOCK), M, FIXPIPE_NZ_DST_STRIDE_INT8, | 255 | reinterpret_cast<__gm__ int8_t*>(c), c_l0, ceil_align(N, CUBE_BLOCK), M, FIXPIPE_NZ_DST_STRIDE_INT8, |
| 262 | ceil_align(M, CUBE_BLOCK), L0C2GM_L2_CACHE_NORMAL, L0C2GM_UNIT_FLAG_DISABLE, QuantMode_t::QF322B8_PRE, | 256 | ceil_align(M, CUBE_BLOCK), L0C2GM_L2_CACHE_NORMAL, L0C2GM_UNIT_FLAG_DISABLE, QuantMode_t::QF322B8_PRE, |
| @@ -0,0 +1,38 @@ | |||
| 1 | +# ---------------------------------------------------------------------------------------------------------- | ||
| 2 | +# Copyright (c) 2026 Huawei Technologies Co., Ltd. | ||
| 3 | +# This program is free software, you can redistribute it and/or modify it under the terms and conditions of | ||
| 4 | +# CANN Open Software License Agreement Version 2.0 (the "License"). | ||
| 5 | +# Please refer to the License for details. You may not use this file except in compliance with the License. | ||
| 6 | +# THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED, | ||
| 7 | +# INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE. | ||
| 8 | +# See LICENSE in the root of the software repository for the full text of the License. | ||
| 9 | +# ---------------------------------------------------------------------------------------------------------- | ||
| 10 | + | ||
| 11 | +cmake_minimum_required(VERSION 3.16) | ||
| 12 | + | ||
| 13 | +set(CMAKE_ASC_RUN_MODE "npu" CACHE STRING "Run mode: npu, sim") | ||
| 14 | +set(CMAKE_ASC_ARCHITECTURES "dav-3510" CACHE STRING "NPU architecture: dav-3510") | ||
| 15 | + | ||
| 16 | +find_package(ASC REQUIRED) | ||
| 17 | + | ||
| 18 | +project(c_api_data_copy_l0c2l1 LANGUAGES ASC CXX) | ||
| 19 | + | ||
| 20 | +if(NOT CMAKE_ASC_ARCHITECTURES STREQUAL "dav-3510") | ||
| 21 | + message(FATAL_ERROR "This C API example only supports CMAKE_ASC_ARCHITECTURES=dav-3510.") | ||
| 22 | +endif() | ||
| 23 | + | ||
| 24 | +if(NOT CMAKE_ASC_RUN_MODE MATCHES "^(npu|sim)$") | ||
| 25 | + message(FATAL_ERROR "CMAKE_ASC_RUN_MODE must be npu or sim, but got ${CMAKE_ASC_RUN_MODE}.") | ||
| 26 | +endif() | ||
| 27 | + | ||
| 28 | +set(SCENARIO_NUM "1" CACHE STRING "Scene number (1-6)") | ||
| 29 | +if(NOT SCENARIO_NUM MATCHES "^[1-6]$") | ||
| 30 | + message(FATAL_ERROR "SCENARIO_NUM must be 1, 2, 3, 4, 5, or 6, but got ${SCENARIO_NUM}") | ||
| 31 | +endif() | ||
| 32 | + | ||
| 33 | +add_executable(demo data_copy_l0c2l1.asc) | ||
| 34 | + | ||
| 35 | +add_definitions(-DSCENARIO_NUM=${SCENARIO_NUM}) | ||
| 36 | +target_compile_options(demo PRIVATE | ||
| 37 | + $<$<COMPILE_LANGUAGE:ASC>:--npu-arch=${CMAKE_ASC_ARCHITECTURES}> | ||
| 38 | +) | ||
| @@ -0,0 +1,131 @@ | |||
| 1 | +# data_copy_l0c2l1样例 | ||
| 2 | + | ||
| 3 | +## 概述 | ||
| 4 | + | ||
| 5 | +本样例演示如何使用Ascend C C API,通过Fixpipe将矩阵乘计算结果从L0C Buffer搬运到L1(L1 Buffer),并在搬运过程中完成标量、Vector量化、ReLU激活和NZ2ND格式转换。输入矩阵A和B均为ND格式,先从GM搬运到L1 Buffer并转换为Nz格式,再经L0A Buffer和L0B Buffer完成两次K轴分块矩阵乘。为便于Host侧校验,样例随后将L1中的输出数据经UB搬运回GM。 | ||
| 6 | + | ||
| 7 | +本样例适用于Ascend 950PR/Ascend 950DT(`dav-3510`),可在NPU运行模式或NPU仿真模式下执行,不提供CPU域调试模式。 | ||
| 8 | + | ||
| 9 | +## 本样例支持的产品及CANN软件版本 | ||
| 10 | + | ||
| 11 | +| 产品 | CANN软件版本 | | ||
| 12 | +|------|-------------| | ||
| 13 | +| Ascend 950PR/Ascend 950DT | >= CANN 9.2.0 | | ||
| 14 | + | ||
| 15 | +## 目录结构介绍 | ||
| 16 | + | ||
| 17 | +``` | ||
| 18 | +data_copy_l0c2l1 | ||
| 19 | +├── scripts | ||
| 20 | +│ ├── gen_data.py // 输入数据和真值数据生成脚本 | ||
| 21 | +│ └── verify_result.py // 输出数据与真值数据精度校验脚本 | ||
| 22 | +├── data_utils.h // 数据读入写出函数 | ||
| 23 | +├── CMakeLists.txt // CMake编译文件 | ||
| 24 | +├── data_copy_l0c2l1.asc // C API样例实现及调用代码 | ||
| 25 | +├── README.md // 中文样例说明文档 | ||
| 26 | +└── README_en.md // 英文样例说明文档 | ||
| 27 | +``` | ||
| 28 | + | ||
| 29 | +## 场景详细说明 | ||
| 30 | + | ||
| 31 | +通过编译参数`SCENARIO_NUM`选择场景。所有场景的矩阵乘规格均为`[M, K, N] = [128, 128, 256]`,其中K轴拆分为两个`[128, 64]`分块,内核函数名为`data_copy_l0c2l1`。 | ||
| 32 | + | ||
| 33 | +| SCENARIO_NUM | A/B类型 | L0C类型 | 输出类型与格式 | 量化模式 | ReLU | NZ2ND | | ||
| 34 | +|---|---|---|---|---|---|---| | ||
| 35 | +| 1 | int8_t | int32_t | half,ND | 标量`DEQF16` | 是 | 是 | | ||
| 36 | +| 2 | int8_t | int32_t | half,Nz | Vector `VDEQF16` | 否 | 否 | | ||
| 37 | +| 3 | half | float | int8_t,Nz | 标量`QF322B8_PRE` | 否 | 否 | | ||
| 38 | +| 4 | half | float | int8_t,ND | Vector `VQF322B8_PRE` | 是 | 是 | | ||
| 39 | +| 5 | int8_t | int32_t | int8_t,ND | 标量`REQ8` | 否 | 是 | | ||
| 40 | +| 6 | int8_t | int32_t | int8_t,Nz | Vector `VREQ8` | 是 | 否 | | ||
| 41 | + | ||
| 42 | +`SCENARIO_NUM`由CMake作为编译期宏传入,内核通过`if constexpr`选择对应场景。切换场景后,需要重新编译。 | ||
| 43 | + | ||
| 44 | +设备侧数据流如下:每个K轴分块按照`GM→L1 Buffer(MTE2)→L0A Buffer/L0B Buffer(MTE1)→Mmad(M)`执行;`MTE1→MTE2`和`M→MTE1`仅用于保护下一轮分块对L1 Buffer、L0A Buffer和L0B Buffer的复用。最终建立`M→FIX→MTE1`依赖,使Fixpipe在Mmad完成后将L0C Buffer中的计算结果搬运到L1,再通过`asc_copy_l12ub`将L1结果异步搬运到subblock0的UB;调用`asc_sync_block_arrive`前使用`asc_sync_pipe(PIPE_MTE1)`确保搬运完成。核函数结束时调用`asc_sync_pipe(PIPE_ALL)`,确保全部流水完成。 | ||
| 45 | + | ||
| 46 | +**场景1:int8输入、标量反量化并输出ND half** | ||
| 47 | + | ||
| 48 | +- 输入:A `[128, 128]`、B `[128, 256]`,均为ND格式`int8_t` | ||
| 49 | +- 输出:C `[128, 256]`,ND格式`half` | ||
| 50 | +- 实现:调用`asc_set_l0c_copy_prequant`配置标量反量化参数,使用`DEQF16`模式;启用ReLU和NZ2ND | ||
| 51 | + | ||
| 52 | +**场景2:int8输入、Vector反量化并输出Nz half** | ||
| 53 | + | ||
| 54 | +- 输入:A `[128, 128]`、B `[128, 256]`,均为ND格式`int8_t`;量化参数`[256] uint64_t` | ||
| 55 | +- 输出:C `[128, 256]`,Nz格式`half` | ||
| 56 | +- 实现:使用`VDEQF16`模式,每列使用一个Vector量化参数 | ||
| 57 | + | ||
| 58 | +**场景3:half输入、标量量化并输出Nz int8_t** | ||
| 59 | + | ||
| 60 | +- 输入:A `[128, 128]`、B `[128, 256]`,均为ND格式`half` | ||
| 61 | +- 输出:C `[128, 256]`,Nz格式`int8_t` | ||
| 62 | +- 实现:调用`asc_set_l0c_copy_prequant`配置标量量化参数,使用`QF322B8_PRE`模式 | ||
| 63 | + | ||
| 64 | +**场景4:half输入、Vector量化并输出ND int8_t** | ||
| 65 | + | ||
| 66 | +- 输入:A `[128, 128]`、B `[128, 256]`,均为ND格式`half`;量化参数`[256] uint64_t` | ||
| 67 | +- 输出:C `[128, 256]`,ND格式`int8_t` | ||
| 68 | +- 实现:使用`VQF322B8_PRE`模式;启用ReLU和NZ2ND | ||
| 69 | + | ||
| 70 | +**场景5:int8输入、标量量化并输出ND int8_t** | ||
| 71 | + | ||
| 72 | +- 输入:A `[128, 128]`、B `[128, 256]`,均为ND格式`int8_t` | ||
| 73 | +- 输出:C `[128, 256]`,ND格式`int8_t` | ||
| 74 | +- 实现:调用`asc_set_l0c_copy_prequant`配置标量量化参数,使用`REQ8`模式并启用NZ2ND | ||
| 75 | + | ||
| 76 | +**场景6:int8输入、Vector量化并输出Nz int8_t** | ||
| 77 | + | ||
| 78 | +- 输入:A `[128, 128]`、B `[128, 256]`,均为ND格式`int8_t`;量化参数`[256] uint64_t` | ||
| 79 | +- 输出:C `[128, 256]`,Nz格式`int8_t` | ||
| 80 | +- 实现:使用`VREQ8`模式并启用ReLU | ||
| 81 | + | ||
| 82 | +对于场景2、4和6,量化参数先通过`asc_copy_gm2l1`从GM搬运到L1 Buffer,再通过`asc_copy_l12fb`搬运到Fixpipe Buffer,最后调用`asc_set_l0c_copy_config`配置Vector量化参数地址。`asc_set_l0c_copy_config`的参数地址以128B为单位,因此参数文件需按128B向上对齐,不足部分补0。本样例的`[256] uint64_t`参数共2048B,已满足要求。量化参数从L1 Buffer搬运至Fixpipe Buffer时,需要搬运2048B数据,数据搬运单位为64B,对应`asc_copy_l12fb`的`len_burst`配置为32。量化参数从L1 Buffer搬运至Fixpipe Buffer后,通过`asc_sync_pipe(PIPE_FIX)`确保参数和配置生效,随后通过Fixpipe将L0C Buffer中的计算结果搬运到L1。 | ||
| 83 | + | ||
| 84 | +## 编译运行 | ||
| 85 | + | ||
| 86 | +在本样例根目录执行以下步骤。 | ||
| 87 | + | ||
| 88 | +- 配置环境变量 | ||
| 89 | + | ||
| 90 | + 请根据CANN开发套件包的[安装方式](../../../../../docs/zh/quick_start.md#prepare&install)配置环境变量。 | ||
| 91 | + | ||
| 92 | + ```bash | ||
| 93 | + source ${install_path}/cann/set_env.sh | ||
| 94 | + ``` | ||
| 95 | + | ||
| 96 | + > **说明:** `${install_path}`为CANN包安装目录;root用户默认为`/usr/local/Ascend`,非root用户默认为`${HOME}/Ascend`。 | ||
| 97 | + | ||
| 98 | +- NPU运行 | ||
| 99 | + | ||
| 100 | + ```bash | ||
| 101 | + SCENARIO_NUM=1 | ||
| 102 | + mkdir -p build && cd build; # 创建并进入build目录 | ||
| 103 | + cmake -DSCENARIO_NUM=$SCENARIO_NUM -DCMAKE_ASC_ARCHITECTURES=dav-3510 ..;make -j; # 编译工程,默认NPU模式 | ||
| 104 | + python3 ../scripts/gen_data.py -scenarioNum=$SCENARIO_NUM # 生成测试输入数据和真值数据 | ||
| 105 | + ./demo # 执行编译生成的可执行程序,执行样例 | ||
| 106 | + python3 ../scripts/verify_result.py -scenarioNum=$SCENARIO_NUM output/output.bin ./output/golden.bin # 验证输出结果是否正确 | ||
| 107 | + ``` | ||
| 108 | + | ||
| 109 | +- NPU仿真 | ||
| 110 | + | ||
| 111 | + ```bash | ||
| 112 | + cmake -DSCENARIO_NUM=$SCENARIO_NUM -DCMAKE_ASC_RUN_MODE=sim -DCMAKE_ASC_ARCHITECTURES=dav-3510 ..;make -j; # NPU仿真模式 | ||
| 113 | + ``` | ||
| 114 | + | ||
| 115 | + 切换运行模式前需清理CMake缓存,例如在`build`目录执行`rm CMakeCache.txt`后重新配置。 | ||
| 116 | + | ||
| 117 | +- 编译选项说明 | ||
| 118 | + | ||
| 119 | + | 选项 | 可选值 | 说明 | | ||
| 120 | + |------|--------|------| | ||
| 121 | + | `CMAKE_ASC_RUN_MODE` | `npu`(默认)、`sim` | 运行模式:NPU运行、NPU仿真 | | ||
| 122 | + | `CMAKE_ASC_ARCHITECTURES` | `dav-3510` | NPU架构,对应Ascend 950PR/Ascend 950DT | | ||
| 123 | + | `SCENARIO_NUM` | 1-6 | 场景编号 | | ||
| 124 | + | ||
| 125 | +- 执行结果 | ||
| 126 | + | ||
| 127 | + 输出与真值比较通过时,打印如下信息: | ||
| 128 | + | ||
| 129 | + ```bash | ||
| 130 | + test pass! | ||
| 131 | + ``` | ||
| @@ -0,0 +1,131 @@ | |||
| 1 | +# data_copy_l0c2l1 Example | ||
| 2 | + | ||
| 3 | +## Overview | ||
| 4 | + | ||
| 5 | +This example uses the Ascend C C API to move matrix-multiplication results from L0C Buffer to L1 (L1 Buffer) through Fixpipe. It performs scalar or vector quantization, ReLU activation, and NZ2ND conversion during the movement. The ND-format input matrices A and B are converted to Nz while moved from GM to L1 Buffer, then processed through L0A Buffer and L0B Buffer by two K-axis matrix-multiplication chunks. For host-side verification, the example then copies the L1 output back to GM through UB. | ||
| 6 | + | ||
| 7 | +This example applies to Ascend 950PR/Ascend 950DT (`dav-3510`) and can run in NPU execution or NPU simulation mode. CPU debug mode is not provided. | ||
| 8 | + | ||
| 9 | +## Supported Products and CANN Versions | ||
| 10 | + | ||
| 11 | +| Product | CANN version | | ||
| 12 | +|------|-------------| | ||
| 13 | +| Ascend 950PR/Ascend 950DT | >= CANN 9.2.0 | | ||
| 14 | + | ||
| 15 | +## Directory Structure | ||
| 16 | + | ||
| 17 | +``` | ||
| 18 | +data_copy_l0c2l1 | ||
| 19 | +├── scripts | ||
| 20 | +│ ├── gen_data.py // Generates input and golden data | ||
| 21 | +│ └── verify_result.py // Verifies output against golden data | ||
| 22 | +├── data_utils.h // Data input/output helpers | ||
| 23 | +├── CMakeLists.txt // CMake build file | ||
| 24 | +├── data_copy_l0c2l1.asc // C API implementation and launcher | ||
| 25 | +├── README.md // Chinese example documentation | ||
| 26 | +└── README_en.md // English example documentation | ||
| 27 | +``` | ||
| 28 | + | ||
| 29 | +## Detailed Scenario Description | ||
| 30 | + | ||
| 31 | +Use the `SCENARIO_NUM` build parameter to select a scenario. Every scenario uses the matrix-multiplication shape `[M, K, N] = [128, 128, 256]`; the K axis is split into two `[128, 64]` chunks. The kernel is named `data_copy_l0c2l1`. | ||
| 32 | + | ||
| 33 | +| SCENARIO_NUM | A/B type | L0C type | Output type and format | Quantization mode | ReLU | NZ2ND | | ||
| 34 | +|---|---|---|---|---|---|---| | ||
| 35 | +| 1 | int8_t | int32_t | half, ND | Scalar `DEQF16` | Yes | Yes | | ||
| 36 | +| 2 | int8_t | int32_t | half, Nz | Vector `VDEQF16` | No | No | | ||
| 37 | +| 3 | half | float | int8_t, Nz | Scalar `QF322B8_PRE` | No | No | | ||
| 38 | +| 4 | half | float | int8_t, ND | Vector `VQF322B8_PRE` | Yes | Yes | | ||
| 39 | +| 5 | int8_t | int32_t | int8_t, ND | Scalar `REQ8` | No | Yes | | ||
| 40 | +| 6 | int8_t | int32_t | int8_t, Nz | Vector `VREQ8` | Yes | No | | ||
| 41 | + | ||
| 42 | +`SCENARIO_NUM` is passed by CMake as a compile-time macro. The kernel selects the corresponding scenario with `if constexpr`. After changing the scenario, recompile the project. | ||
| 43 | + | ||
| 44 | +The device-side flow is as follows. Each K chunk follows `GM→L1 Buffer (MTE2)→L0A Buffer/L0B Buffer (MTE1)→Mmad (M)`. `MTE1→MTE2` and `M→MTE1` protect L1 Buffer, L0A Buffer, and L0B Buffer reuse for the next chunk. The final `M→FIX→MTE1` dependency moves the L0C result to L1 and then uses `asc_copy_l12ub` to asynchronously move it to subblock0 UB; `asc_sync_pipe(PIPE_MTE1)` completes the transfer before `asc_sync_block_arrive` notifies the AIVs. `asc_sync_pipe(PIPE_ALL)` is called at the end of the kernel to ensure that all pipelines complete. | ||
| 45 | + | ||
| 46 | +**Scenario 1: int8 input, scalar dequantization, and ND half output** | ||
| 47 | + | ||
| 48 | +- Input: A `[128, 128]` and B `[128, 256]`, both ND `int8_t` | ||
| 49 | +- Output: C `[128, 256]` in ND `half` | ||
| 50 | +- Implementation: Configure scalar dequantization with `asc_set_l0c_copy_prequant`, use `DEQF16`, and enable ReLU and NZ2ND | ||
| 51 | + | ||
| 52 | +**Scenario 2: int8 input, vector dequantization, and Nz half output** | ||
| 53 | + | ||
| 54 | +- Input: A `[128, 128]` and B `[128, 256]`, both ND `int8_t`; `[256] uint64_t` quantization parameters | ||
| 55 | +- Output: C `[128, 256]` in Nz `half` | ||
| 56 | +- Implementation: Use `VDEQF16`; each output column uses one vector quantization parameter | ||
| 57 | + | ||
| 58 | +**Scenario 3: half input, scalar quantization, and Nz int8_t output** | ||
| 59 | + | ||
| 60 | +- Input: A `[128, 128]` and B `[128, 256]`, both ND `half` | ||
| 61 | +- Output: C `[128, 256]` in Nz `int8_t` | ||
| 62 | +- Implementation: Configure scalar quantization with `asc_set_l0c_copy_prequant` and use `QF322B8_PRE` | ||
| 63 | + | ||
| 64 | +**Scenario 4: half input, vector quantization, and ND int8_t output** | ||
| 65 | + | ||
| 66 | +- Input: A `[128, 128]` and B `[128, 256]`, both ND `half`; `[256] uint64_t` quantization parameters | ||
| 67 | +- Output: C `[128, 256]` in ND `int8_t` | ||
| 68 | +- Implementation: Use `VQF322B8_PRE`, and enable ReLU and NZ2ND | ||
| 69 | + | ||
| 70 | +**Scenario 5: int8 input, scalar quantization, and ND int8_t output** | ||
| 71 | + | ||
| 72 | +- Input: A `[128, 128]` and B `[128, 256]`, both ND `int8_t` | ||
| 73 | +- Output: C `[128, 256]` in ND `int8_t` | ||
| 74 | +- Implementation: Configure scalar quantization with `asc_set_l0c_copy_prequant`, use `REQ8`, and enable NZ2ND | ||
| 75 | + | ||
| 76 | +**Scenario 6: int8 input, vector quantization, and Nz int8_t output** | ||
| 77 | + | ||
| 78 | +- Input: A `[128, 128]` and B `[128, 256]`, both ND `int8_t`; `[256] uint64_t` quantization parameters | ||
| 79 | +- Output: C `[128, 256]` in Nz `int8_t` | ||
| 80 | +- Implementation: Use `VREQ8` and enable ReLU | ||
| 81 | + | ||
| 82 | +For scenarios 2, 4, and 6, the parameters are moved from GM to L1 Buffer with `asc_copy_gm2l1`, then from L1 Buffer to Fixpipe Buffer with `asc_copy_l12fb`. `asc_set_l0c_copy_config` configures the vector-quantization parameter address. Its parameter address is expressed in 128B units, so the parameter file must be rounded up to 128B and any padding must be zero-filled. The `[256] uint64_t` parameters in this example occupy 2048B and already meet this requirement. Moving these parameters from L1 Buffer to Fixpipe Buffer transfers 2048B of data in 64B data blocks, so `len_burst` in `asc_copy_l12fb` is set to 32. `asc_sync_pipe(PIPE_FIX)` completes the L1 Buffer-to-Fixpipe Buffer movement and parameter configuration before Fixpipe moves the L0C Buffer result to L1. | ||
| 83 | + | ||
| 84 | +## Build and Run | ||
| 85 | + | ||
| 86 | +Run the following steps in the example root directory. | ||
| 87 | + | ||
| 88 | +- Configure environment variables | ||
| 89 | + | ||
| 90 | + Configure the environment according to the CANN development kit [installation instructions](../../../../../docs/en/quick_start.md#prepare&install). | ||
| 91 | + | ||
| 92 | + ```bash | ||
| 93 | + source ${install_path}/cann/set_env.sh | ||
| 94 | + ``` | ||
| 95 | + | ||
| 96 | + > **Note:** `${install_path}` is the CANN package installation directory. The default is `/usr/local/Ascend` for the root user and `${HOME}/Ascend` for non-root users. | ||
| 97 | + | ||
| 98 | +- NPU execution | ||
| 99 | + | ||
| 100 | + ```bash | ||
| 101 | + SCENARIO_NUM=1 | ||
| 102 | + mkdir -p build && cd build; # Create and enter the build directory | ||
| 103 | + cmake -DSCENARIO_NUM=$SCENARIO_NUM -DCMAKE_ASC_ARCHITECTURES=dav-3510 ..;make -j; # Build the project; NPU mode by default | ||
| 104 | + python3 ../scripts/gen_data.py -scenarioNum=$SCENARIO_NUM # Generate test input and golden data | ||
| 105 | + ./demo # Run the example executable | ||
| 106 | + python3 ../scripts/verify_result.py -scenarioNum=$SCENARIO_NUM output/output.bin ./output/golden.bin # Verify the result | ||
| 107 | + ``` | ||
| 108 | + | ||
| 109 | +- NPU simulation | ||
| 110 | + | ||
| 111 | + ```bash | ||
| 112 | + cmake -DSCENARIO_NUM=$SCENARIO_NUM -DCMAKE_ASC_RUN_MODE=sim -DCMAKE_ASC_ARCHITECTURES=dav-3510 ..;make -j; # NPU simulation mode | ||
| 113 | + ``` | ||
| 114 | + | ||
| 115 | + Clear the CMake cache before switching run modes. For example, run `rm CMakeCache.txt` in the `build` directory before reconfiguring. | ||
| 116 | + | ||
| 117 | +- Build options | ||
| 118 | + | ||
| 119 | + | Option | Values | Description | | ||
| 120 | + |------|--------|------| | ||
| 121 | + | `CMAKE_ASC_RUN_MODE` | `npu` (default), `sim` | Run mode: NPU execution or NPU simulation | | ||
| 122 | + | `CMAKE_ASC_ARCHITECTURES` | `dav-3510` | NPU architecture for Ascend 950PR/Ascend 950DT | | ||
| 123 | + | `SCENARIO_NUM` | 1-6 | Scenario number | | ||
| 124 | + | ||
| 125 | +- Expected result | ||
| 126 | + | ||
| 127 | + Successful output-to-golden verification prints: | ||
| 128 | + | ||
| 129 | + ```bash | ||
| 130 | + test pass! | ||
| 131 | + ``` | ||
| @@ -0,0 +1,373 @@ | |||
| 1 | +/** | ||
| 2 | + * Copyright (c) 2026 Huawei Technologies Co., Ltd. | ||
| 3 | + * This program is free software, you can redistribute it and/or modify it under the terms and conditions of | ||
| 4 | + * CANN Open Software License Agreement Version 2.0 (the "License"). | ||
| 5 | + * Please refer to the License for details. You may not use this file except in compliance with the License. | ||
| 6 | + * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED, | ||
| 7 | + * INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE. | ||
| 8 | + * See LICENSE in the root of the software repository for the full text of the License. | ||
| 9 | + */ | ||
| 10 | + | ||
| 11 | +/* ! | ||
| 12 | + * \file data_copy_l0c2l1.asc | ||
| 13 | + * \brief Demonstrates L0C-to-L1 matrix movement with inline quantization using the Ascend C C API. | ||
| 14 | + */ | ||
| 15 | + | ||
| 16 | +#include <cstddef> | ||
| 17 | +#include <cstdint> | ||
| 18 | +#include <cstdio> | ||
| 19 | + | ||
| 20 | +#include "acl/acl.h" | ||
| 21 | +#include "c_api/asc_simd.h" | ||
| 22 | +#include "data_utils.h" | ||
| 23 | + | ||
| 24 | +constexpr uint32_t CUBE_BLOCK = 16; | ||
| 25 | +constexpr uint32_t C0_BYTES = 32; | ||
| 26 | +constexpr uint32_t K_ROUND = 2; | ||
| 27 | +constexpr uint32_t FIXPIPE_QUANT_ADDR_GRANULARITY = 128; | ||
| 28 | +constexpr uint32_t FIXPIPE_L1_TO_FB_BURST_BYTES = 64; | ||
| 29 | + | ||
| 30 | +constexpr uint32_t M = 128; | ||
| 31 | +constexpr uint32_t K = 128; | ||
| 32 | +constexpr uint32_t N = 256; | ||
| 33 | +constexpr uint32_t K_CHUNK = K / K_ROUND; | ||
| 34 | + | ||
| 35 | +__aicore__ constexpr uint32_t ceil_division(uint32_t numerator, uint32_t denominator) | ||
| 36 | +{ | ||
| 37 | + return (numerator + denominator - 1) / denominator; | ||
| 38 | +} | ||
| 39 | + | ||
| 40 | +__aicore__ constexpr uint32_t ceil_align(uint32_t numerator, uint32_t denominator) | ||
| 41 | +{ | ||
| 42 | + return ceil_division(numerator, denominator) * denominator; | ||
| 43 | +} | ||
| 44 | + | ||
| 45 | +constexpr bool IS_INT8_INPUT = SCENARIO_NUM == 1 || SCENARIO_NUM == 2 || SCENARIO_NUM == 5 || SCENARIO_NUM == 6; | ||
| 46 | +constexpr bool IS_VECTOR_QUANT = SCENARIO_NUM == 2 || SCENARIO_NUM == 4 || SCENARIO_NUM == 6; | ||
| 47 | +constexpr bool IS_NZ2ND_OUTPUT = SCENARIO_NUM == 1 || SCENARIO_NUM == 4 || SCENARIO_NUM == 5; | ||
| 48 | +constexpr bool IS_RELU_ENABLED = SCENARIO_NUM == 1 || SCENARIO_NUM == 4 || SCENARIO_NUM == 6; | ||
| 49 | + | ||
| 50 | +constexpr uint32_t INT8_C0_ELEMENTS = C0_BYTES / sizeof(int8_t); | ||
| 51 | +constexpr uint32_t HALF_C0_ELEMENTS = C0_BYTES / sizeof(half); | ||
| 52 | +constexpr uint32_t A_SINGLE_SIZE = M * K_CHUNK; | ||
| 53 | +constexpr uint32_t B_SINGLE_SIZE = K_CHUNK * N; | ||
| 54 | +constexpr uint32_t A_L1_MAX_BYTES = ceil_align(M, CUBE_BLOCK) * ceil_align(K_CHUNK, HALF_C0_ELEMENTS) * sizeof(half); | ||
| 55 | +constexpr uint32_t B_L1_MAX_BYTES = | ||
| 56 | + ceil_align(K_CHUNK, HALF_C0_ELEMENTS) * ceil_align(N, HALF_C0_ELEMENTS) * sizeof(half); | ||
| 57 | +constexpr uint32_t C_L0_MAX_BYTES = ceil_align(M, CUBE_BLOCK) * ceil_align(N, CUBE_BLOCK) * sizeof(float); | ||
| 58 | +constexpr uint32_t QUANT_TRANSFER_BYTES = ceil_align(N * sizeof(uint64_t), FIXPIPE_QUANT_ADDR_GRANULARITY); | ||
| 59 | + | ||
| 60 | +// Keep A1, B1, and C1 in separate regions of the C API local-memory workspace. | ||
| 61 | +constexpr uint32_t A1_OFFSET_BYTES = 0; | ||
| 62 | +constexpr uint32_t B1_OFFSET_BYTES = 64 * 1024; | ||
| 63 | +constexpr uint32_t C1_OFFSET_BYTES = 128 * 1024; | ||
| 64 | +constexpr uint32_t QUANT_OFFSET_BYTES = 192 * 1024; | ||
| 65 | +constexpr uint32_t CBUF_WORKSPACE_BYTES = QUANT_OFFSET_BYTES + QUANT_TRANSFER_BYTES; | ||
| 66 | + | ||
| 67 | +constexpr uint8_t L2_CACHE_NORMAL = 0; | ||
| 68 | +constexpr uint8_t UNIT_FLAG_DISABLE = 0; | ||
| 69 | +constexpr asc_unit_flag_mode L0C2L1_UNIT_FLAG_DISABLE = asc_unit_flag_mode::DISABLE; | ||
| 70 | +constexpr asc_relu_pre_mode L0C2L1_RELU_MODE = IS_RELU_ENABLED ? asc_relu_pre_mode::NORMAL : asc_relu_pre_mode::NONE; | ||
| 71 | +constexpr uint16_t MTE2_NZ_MATRIX_NUM = 1; | ||
| 72 | +constexpr uint16_t MTE2_NZ_N_STRIDE = 1; | ||
| 73 | +constexpr uint16_t MTE2_NZ_LOOP4_DST_STRIDE = 0; | ||
| 74 | +constexpr uint16_t A_MTE2_NZ_C0_STRIDE = | ||
| 75 | + static_cast<uint16_t>(IS_INT8_INPUT ? ceil_align(M, INT8_C0_ELEMENTS) : ceil_align(M, HALF_C0_ELEMENTS)); | ||
| 76 | +constexpr uint16_t B_MTE2_NZ_C0_STRIDE = static_cast<uint16_t>( | ||
| 77 | + IS_INT8_INPUT ? ceil_align(K_CHUNK, INT8_C0_ELEMENTS) : ceil_align(K_CHUNK, HALF_C0_ELEMENTS)); | ||
| 78 | +constexpr float SCALAR_QUANT_SCALE = 2.0F; | ||
| 79 | +constexpr uint16_t SCALAR_QUANT_OFFSET = 0; | ||
| 80 | +constexpr bool SCALAR_QUANT_HALF_IS_SIGNED = false; | ||
| 81 | +constexpr bool SCALAR_QUANT_INT8_IS_SIGNED = true; | ||
| 82 | +// For Nz output, loop_dst_stride is measured in output elements. The API | ||
| 83 | +// converts its 32B-block dstStride to this unit before issuing Fixpipe. | ||
| 84 | +constexpr uint32_t FIXPIPE_NZ_DST_STRIDE_HALF = M * C0_BYTES / sizeof(half); | ||
| 85 | +constexpr uint32_t FIXPIPE_NZ_DST_STRIDE_INT8 = M * C0_BYTES / sizeof(int8_t); | ||
| 86 | +constexpr uint32_t OUTPUT_ELEMENT_BYTES = (SCENARIO_NUM == 1 || SCENARIO_NUM == 2) ? sizeof(half) : sizeof(int8_t); | ||
| 87 | +constexpr uint32_t OUTPUT_BYTES = M * N * OUTPUT_ELEMENT_BYTES; | ||
| 88 | +constexpr uint32_t OUTPUT_UB_BYTES = M * N * sizeof(half); | ||
| 89 | +constexpr uint16_t OUTPUT_L12UB_BURST_COUNT = 1; | ||
| 90 | +constexpr uint16_t OUTPUT_L12UB_BURST_BLOCKS = OUTPUT_BYTES / C0_BYTES; | ||
| 91 | +constexpr int64_t OUTPUT_READY_FLAG_ID = 11; | ||
| 92 | + | ||
| 93 | +__global__ __mix__(1, 2) void data_copy_l0c2l1( | ||
| 94 | + __gm__ uint8_t* a, __gm__ uint8_t* b, __gm__ void* c, __gm__ uint64_t* quant_pre) | ||
| 95 | +{ | ||
| 96 | + asc_init(); | ||
| 97 | + | ||
| 98 | + __cbuf__ uint8_t cbuf_workspace[CBUF_WORKSPACE_BYTES]; | ||
| 99 | + __ca__ uint8_t a_l0_workspace[A_L1_MAX_BYTES]; | ||
| 100 | + __cb__ uint8_t b_l0_workspace[B_L1_MAX_BYTES]; | ||
| 101 | + __cc__ uint8_t c_l0_workspace[C_L0_MAX_BYTES]; | ||
| 102 | + __ubuf__ uint8_t* output_ub = reinterpret_cast<__ubuf__ uint8_t*>(0); | ||
| 103 | + | ||
| 104 | + if ASC_IS_AIC { | ||
| 105 | + if constexpr (IS_INT8_INPUT) { | ||
| 106 | + __cbuf__ int8_t* a_l1 = reinterpret_cast<__cbuf__ int8_t*>(cbuf_workspace + A1_OFFSET_BYTES); | ||
| 107 | + __cbuf__ int8_t* b_l1 = reinterpret_cast<__cbuf__ int8_t*>(cbuf_workspace + B1_OFFSET_BYTES); | ||
| 108 | + __ca__ int8_t* a_l0 = reinterpret_cast<__ca__ int8_t*>(a_l0_workspace); | ||
| 109 | + __cb__ int8_t* b_l0 = reinterpret_cast<__cb__ int8_t*>(b_l0_workspace); | ||
| 110 | + __cc__ int32_t* c_l0 = reinterpret_cast<__cc__ int32_t*>(c_l0_workspace); | ||
| 111 | + | ||
| 112 | + for (uint32_t k_index = 0; k_index < K_ROUND; ++k_index) { | ||
| 113 | + asc_set_gm2l1_nz_para( | ||
| 114 | + MTE2_NZ_MATRIX_NUM, MTE2_NZ_N_STRIDE, A_MTE2_NZ_C0_STRIDE, MTE2_NZ_LOOP4_DST_STRIDE); | ||
| 115 | + asc_copy_gm2l1_nd2nz( | ||
| 116 | + a_l1, reinterpret_cast<__gm__ int8_t*>(a) + k_index * A_SINGLE_SIZE, K_CHUNK * sizeof(int8_t), | ||
| 117 | + L2_CACHE_NORMAL, M, K_CHUNK, 0, false); | ||
| 118 | + asc_set_gm2l1_nz_para( | ||
| 119 | + MTE2_NZ_MATRIX_NUM, MTE2_NZ_N_STRIDE, B_MTE2_NZ_C0_STRIDE, MTE2_NZ_LOOP4_DST_STRIDE); | ||
| 120 | + asc_copy_gm2l1_nd2nz( | ||
| 121 | + b_l1, reinterpret_cast<__gm__ int8_t*>(b) + k_index * B_SINGLE_SIZE, N * sizeof(int8_t), | ||
| 122 | + L2_CACHE_NORMAL, K_CHUNK, N, 0, false); | ||
| 123 | + | ||
| 124 | + asc_sync_notify(PIPE_MTE2, PIPE_MTE1, EVENT_ID0); | ||
| 125 | + asc_sync_wait(PIPE_MTE2, PIPE_MTE1, EVENT_ID0); | ||
| 126 | + asc_copy_l12l0a( | ||
| 127 | + a_l0, a_l1, 0, 0, ceil_division(M, CUBE_BLOCK), ceil_division(K_CHUNK, INT8_C0_ELEMENTS), | ||
| 128 | + ceil_division(M, CUBE_BLOCK), ceil_division(M, CUBE_BLOCK)); | ||
| 129 | + // Nz -> Zn. asc_copy_l12l0b_trans uses the 3510 instruction stride directly. | ||
| 130 | + const uint32_t dst_offset = ceil_division(N, INT8_C0_ELEMENTS) * 512 * 2; | ||
| 131 | + const uint32_t src_offset = 512 * 2; | ||
| 132 | + for (uint32_t i = 0; i < ceil_division(K_CHUNK, INT8_C0_ELEMENTS); ++i) { | ||
| 133 | + asc_copy_l12l0b_trans( | ||
| 134 | + b_l0 + i * dst_offset, b_l1 + i * src_offset, static_cast<uint16_t>(0), | ||
| 135 | + static_cast<uint8_t>(ceil_division(N, INT8_C0_ELEMENTS)), | ||
| 136 | + static_cast<uint16_t>(ceil_division(K_CHUNK, INT8_C0_ELEMENTS) * 2), static_cast<uint16_t>(1), | ||
| 137 | + static_cast<uint16_t>(0), static_cast<uint16_t>(0)); | ||
| 138 | + } | ||
| 139 | + asc_sync_notify(PIPE_MTE1, PIPE_M, EVENT_ID0); | ||
| 140 | + asc_sync_wait(PIPE_MTE1, PIPE_M, EVENT_ID0); | ||
| 141 | + asc_sync_notify(PIPE_MTE1, PIPE_MTE2, EVENT_ID0); | ||
| 142 | + asc_sync_wait(PIPE_MTE1, PIPE_MTE2, EVENT_ID0); | ||
| 143 | + asc_mmad( | ||
| 144 | + c_l0, a_l0, b_l0, M, K_CHUNK, ceil_align(N, CUBE_BLOCK), UNIT_FLAG_DISABLE, false, false, | ||
| 145 | + k_index == 0); | ||
| 146 | + asc_sync_notify(PIPE_M, PIPE_MTE1, EVENT_ID0); | ||
| 147 | + asc_sync_wait(PIPE_M, PIPE_MTE1, EVENT_ID0); | ||
| 148 | + } | ||
| 149 | + | ||
| 150 | + if constexpr (IS_VECTOR_QUANT) { | ||
| 151 | + __cbuf__ uint64_t* quant_l1 = reinterpret_cast<__cbuf__ uint64_t*>(cbuf_workspace + QUANT_OFFSET_BYTES); | ||
| 152 | + __fbuf__ uint64_t quant_fb[QUANT_TRANSFER_BYTES / sizeof(uint64_t)]; | ||
| 153 | + asc_copy_gm2l1( | ||
| 154 | + reinterpret_cast<__cbuf__ void*>(quant_l1), reinterpret_cast<__gm__ void*>(quant_pre), 1, | ||
| 155 | + QUANT_TRANSFER_BYTES / C0_BYTES, 0, 0, 0); | ||
| 156 | + asc_sync_notify(PIPE_MTE2, PIPE_FIX, EVENT_ID0); | ||
| 157 | + asc_sync_wait(PIPE_MTE2, PIPE_FIX, EVENT_ID0); | ||
| 158 | + asc_copy_l12fb( | ||
| 159 | + reinterpret_cast<__fbuf__ void*>(quant_fb), reinterpret_cast<__cbuf__ void*>(quant_l1), 1, | ||
| 160 | + QUANT_TRANSFER_BYTES / FIXPIPE_L1_TO_FB_BURST_BYTES, 0, 0); | ||
| 161 | + const uint64_t quant_fb_addr = | ||
| 162 | + static_cast<uint64_t>(reinterpret_cast<uintptr_t>(quant_fb)) / FIXPIPE_QUANT_ADDR_GRANULARITY; | ||
| 163 | + asc_set_l0c_copy_config(0, quant_fb_addr, false); | ||
| 164 | + asc_sync_pipe(PIPE_FIX); | ||
| 165 | + } | ||
| 166 | + | ||
| 167 | + asc_sync_notify(PIPE_M, PIPE_FIX, EVENT_ID0); | ||
| 168 | + asc_sync_wait(PIPE_M, PIPE_FIX, EVENT_ID0); | ||
| 169 | + if constexpr (IS_NZ2ND_OUTPUT) { | ||
| 170 | + asc_set_l0c_copy_nz_para(1, 0, 0); | ||
| 171 | + } | ||
| 172 | + | ||
| 173 | + if constexpr (SCENARIO_NUM == 1) { | ||
| 174 | + asc_set_l0c_copy_prequant(SCALAR_QUANT_SCALE, SCALAR_QUANT_OFFSET, SCALAR_QUANT_HALF_IS_SIGNED); | ||
| 175 | + asc_copy_l0c2l1( | ||
| 176 | + reinterpret_cast<__cbuf__ half*>(cbuf_workspace + A1_OFFSET_BYTES), c_l0, N, M, N, | ||
| 177 | + ceil_align(M, CUBE_BLOCK), L0C2L1_UNIT_FLAG_DISABLE, QuantMode_t::DEQF16, L0C2L1_RELU_MODE, false, | ||
| 178 | + true, false, false); | ||
| 179 | + } else if constexpr (SCENARIO_NUM == 2) { | ||
| 180 | + asc_copy_l0c2l1( | ||
| 181 | + reinterpret_cast<__cbuf__ half*>(cbuf_workspace + A1_OFFSET_BYTES), c_l0, ceil_align(N, CUBE_BLOCK), | ||
使用新的接口定义 全文排查整改 ![]() ![]() | |||
| 182 | + M, FIXPIPE_NZ_DST_STRIDE_HALF, ceil_align(M, CUBE_BLOCK), L0C2L1_UNIT_FLAG_DISABLE, | ||
| 183 | + QuantMode_t::VDEQF16, L0C2L1_RELU_MODE, false, false, false, false); | ||
| 184 | + } else if constexpr (SCENARIO_NUM == 5) { | ||
| 185 | + asc_set_l0c_copy_prequant(SCALAR_QUANT_SCALE, SCALAR_QUANT_OFFSET, SCALAR_QUANT_INT8_IS_SIGNED); | ||
| 186 | + asc_copy_l0c2l1( | ||
| 187 | + reinterpret_cast<__cbuf__ int8_t*>(cbuf_workspace + A1_OFFSET_BYTES), c_l0, N, M, N, | ||
| 188 | + ceil_align(M, CUBE_BLOCK), L0C2L1_UNIT_FLAG_DISABLE, QuantMode_t::REQ8, L0C2L1_RELU_MODE, false, | ||
| 189 | + true, false, false); | ||
| 190 | + } else { | ||
| 191 | + asc_copy_l0c2l1( | ||
| 192 | + reinterpret_cast<__cbuf__ int8_t*>(cbuf_workspace + A1_OFFSET_BYTES), c_l0, | ||
| 193 | + ceil_align(N, CUBE_BLOCK), M, FIXPIPE_NZ_DST_STRIDE_INT8, ceil_align(M, CUBE_BLOCK), | ||
| 194 | + L0C2L1_UNIT_FLAG_DISABLE, QuantMode_t::VREQ8, L0C2L1_RELU_MODE, false, false, false, false); | ||
| 195 | + } | ||
| 196 | + } else { | ||
| 197 | + __cbuf__ half* a_l1 = reinterpret_cast<__cbuf__ half*>(cbuf_workspace + A1_OFFSET_BYTES); | ||
| 198 | + __cbuf__ half* b_l1 = reinterpret_cast<__cbuf__ half*>(cbuf_workspace + B1_OFFSET_BYTES); | ||
| 199 | + __ca__ half* a_l0 = reinterpret_cast<__ca__ half*>(a_l0_workspace); | ||
| 200 | + __cb__ half* b_l0 = reinterpret_cast<__cb__ half*>(b_l0_workspace); | ||
| 201 | + __cc__ float* c_l0 = reinterpret_cast<__cc__ float*>(c_l0_workspace); | ||
| 202 | + | ||
| 203 | + for (uint32_t k_index = 0; k_index < K_ROUND; ++k_index) { | ||
| 204 | + asc_set_gm2l1_nz_para( | ||
| 205 | + MTE2_NZ_MATRIX_NUM, MTE2_NZ_N_STRIDE, A_MTE2_NZ_C0_STRIDE, MTE2_NZ_LOOP4_DST_STRIDE); | ||
| 206 | + asc_copy_gm2l1_nd2nz( | ||
| 207 | + a_l1, reinterpret_cast<__gm__ half*>(a) + k_index * A_SINGLE_SIZE, K_CHUNK * sizeof(half), | ||
| 208 | + L2_CACHE_NORMAL, M, K_CHUNK, 0, false); | ||
| 209 | + asc_set_gm2l1_nz_para( | ||
| 210 | + MTE2_NZ_MATRIX_NUM, MTE2_NZ_N_STRIDE, B_MTE2_NZ_C0_STRIDE, MTE2_NZ_LOOP4_DST_STRIDE); | ||
| 211 | + asc_copy_gm2l1_nd2nz( | ||
| 212 | + b_l1, reinterpret_cast<__gm__ half*>(b) + k_index * B_SINGLE_SIZE, N * sizeof(half), | ||
| 213 | + L2_CACHE_NORMAL, K_CHUNK, N, 0, false); | ||
| 214 | + | ||
| 215 | + asc_sync_notify(PIPE_MTE2, PIPE_MTE1, EVENT_ID0); | ||
| 216 | + asc_sync_wait(PIPE_MTE2, PIPE_MTE1, EVENT_ID0); | ||
| 217 | + asc_copy_l12l0a( | ||
| 218 | + a_l0, a_l1, 0, 0, ceil_division(M, CUBE_BLOCK), ceil_division(K_CHUNK, HALF_C0_ELEMENTS), | ||
| 219 | + ceil_division(M, CUBE_BLOCK), ceil_division(M, CUBE_BLOCK)); | ||
| 220 | + // Nz -> Zn. half uses one 256-element cube block per transpose iteration. | ||
| 221 | + const uint32_t dst_offset = ceil_division(N, HALF_C0_ELEMENTS) * 256; | ||
| 222 | + const uint32_t src_offset = 256; | ||
| 223 | + for (uint32_t i = 0; i < ceil_division(K_CHUNK, HALF_C0_ELEMENTS); ++i) { | ||
| 224 | + asc_copy_l12l0b_trans( | ||
| 225 | + b_l0 + i * dst_offset, b_l1 + i * src_offset, static_cast<uint16_t>(0), | ||
| 226 | + static_cast<uint8_t>(ceil_division(N, HALF_C0_ELEMENTS)), | ||
| 227 | + static_cast<uint16_t>(ceil_division(K_CHUNK, HALF_C0_ELEMENTS)), static_cast<uint16_t>(0), | ||
| 228 | + static_cast<uint16_t>(0), static_cast<uint16_t>(0)); | ||
| 229 | + } | ||
| 230 | + asc_sync_notify(PIPE_MTE1, PIPE_M, EVENT_ID0); | ||
| 231 | + asc_sync_wait(PIPE_MTE1, PIPE_M, EVENT_ID0); | ||
| 232 | + asc_sync_notify(PIPE_MTE1, PIPE_MTE2, EVENT_ID0); | ||
| 233 | + asc_sync_wait(PIPE_MTE1, PIPE_MTE2, EVENT_ID0); | ||
| 234 | + asc_mmad( | ||
| 235 | + c_l0, a_l0, b_l0, M, K_CHUNK, ceil_align(N, CUBE_BLOCK), UNIT_FLAG_DISABLE, false, false, | ||
| 236 | + k_index == 0); | ||
| 237 | + asc_sync_notify(PIPE_M, PIPE_MTE1, EVENT_ID0); | ||
| 238 | + asc_sync_wait(PIPE_M, PIPE_MTE1, EVENT_ID0); | ||
| 239 | + } | ||
| 240 | + | ||
| 241 | + if constexpr (IS_VECTOR_QUANT) { | ||
| 242 | + __cbuf__ uint64_t* quant_l1 = reinterpret_cast<__cbuf__ uint64_t*>(cbuf_workspace + QUANT_OFFSET_BYTES); | ||
| 243 | + __fbuf__ uint64_t quant_fb[QUANT_TRANSFER_BYTES / sizeof(uint64_t)]; | ||
| 244 | + asc_copy_gm2l1( | ||
| 245 | + reinterpret_cast<__cbuf__ void*>(quant_l1), reinterpret_cast<__gm__ void*>(quant_pre), 1, | ||
| 246 | + QUANT_TRANSFER_BYTES / C0_BYTES, 0, 0, 0); | ||
| 247 | + asc_sync_notify(PIPE_MTE2, PIPE_FIX, EVENT_ID0); | ||
| 248 | + asc_sync_wait(PIPE_MTE2, PIPE_FIX, EVENT_ID0); | ||
| 249 | + asc_copy_l12fb( | ||
| 250 | + reinterpret_cast<__fbuf__ void*>(quant_fb), reinterpret_cast<__cbuf__ void*>(quant_l1), 1, | ||
| 251 | + QUANT_TRANSFER_BYTES / FIXPIPE_L1_TO_FB_BURST_BYTES, 0, 0); | ||
| 252 | + const uint64_t quant_fb_addr = | ||
| 253 | + static_cast<uint64_t>(reinterpret_cast<uintptr_t>(quant_fb)) / FIXPIPE_QUANT_ADDR_GRANULARITY; | ||
| 254 | + asc_set_l0c_copy_config(0, quant_fb_addr, false); | ||
| 255 | + asc_sync_pipe(PIPE_FIX); | ||
| 256 | + } | ||
| 257 | + | ||
| 258 | + asc_sync_notify(PIPE_M, PIPE_FIX, EVENT_ID0); | ||
| 259 | + asc_sync_wait(PIPE_M, PIPE_FIX, EVENT_ID0); | ||
| 260 | + if constexpr (IS_NZ2ND_OUTPUT) { | ||
| 261 | + asc_set_l0c_copy_nz_para(1, 0, 0); | ||
| 262 | + } | ||
| 263 | + | ||
| 264 | + if constexpr (SCENARIO_NUM == 3) { | ||
| 265 | + asc_set_l0c_copy_prequant(SCALAR_QUANT_SCALE, SCALAR_QUANT_OFFSET, SCALAR_QUANT_INT8_IS_SIGNED); | ||
| 266 | + asc_copy_l0c2l1( | ||
| 267 | + reinterpret_cast<__cbuf__ int8_t*>(cbuf_workspace + A1_OFFSET_BYTES), c_l0, | ||
| 268 | + ceil_align(N, CUBE_BLOCK), M, FIXPIPE_NZ_DST_STRIDE_INT8, ceil_align(M, CUBE_BLOCK), | ||
| 269 | + L0C2L1_UNIT_FLAG_DISABLE, QuantMode_t::QF322B8_PRE, L0C2L1_RELU_MODE, false, false, false, false); | ||
| 270 | + } else { | ||
| 271 | + asc_copy_l0c2l1( | ||
| 272 | + reinterpret_cast<__cbuf__ int8_t*>(cbuf_workspace + A1_OFFSET_BYTES), c_l0, N, M, N, | ||
| 273 | + ceil_align(M, CUBE_BLOCK), L0C2L1_UNIT_FLAG_DISABLE, QuantMode_t::VQF322B8_PRE, L0C2L1_RELU_MODE, | ||
| 274 | + false, true, false, false); | ||
| 275 | + } | ||
| 276 | + } | ||
| 277 | + asc_sync_notify(PIPE_FIX, PIPE_MTE1, EVENT_ID0); | ||
| 278 | + asc_sync_wait(PIPE_FIX, PIPE_MTE1, EVENT_ID0); | ||
| 279 | + asc_copy_l12ub( | ||
| 280 | + reinterpret_cast<__ubuf__ void*>(output_ub), | ||
| 281 | + reinterpret_cast<__cbuf__ void*>(cbuf_workspace + A1_OFFSET_BYTES), 0, OUTPUT_L12UB_BURST_COUNT, | ||
| 282 | + OUTPUT_L12UB_BURST_BLOCKS, 0, 0); | ||
| 283 | + asc_sync_pipe(PIPE_MTE1); | ||
| 284 | + asc_sync_block_arrive(PIPE_MTE1, OUTPUT_READY_FLAG_ID); | ||
| 285 | + } | ||
| 286 | + if ASC_IS_AIV { | ||
| 287 | + asc_sync_block_wait(PIPE_MTE3, OUTPUT_READY_FLAG_ID); | ||
| 288 | + if (asc_get_sub_block_id() != 0) { | ||
| 289 | + return; | ||
| 290 | + } | ||
| 291 | + asc_copy_ub2gm_align( | ||
| 292 | + reinterpret_cast<__gm__ uint8_t*>(c), reinterpret_cast<__ubuf__ uint8_t*>(output_ub), OUTPUT_BYTES); | ||
| 293 | + } | ||
| 294 | + asc_sync_pipe(PIPE_ALL); | ||
| 295 | +} | ||
| 296 | + | ||
| 297 | +#define CHECK_ACL(expr) \ | ||
| 298 | + do { \ | ||
| 299 | + auto __ret = (expr); \ | ||
| 300 | + int32_t __code = static_cast<int32_t>(__ret); \ | ||
| 301 | + if (__code != 0) { \ | ||
| 302 | + fprintf(stderr, "[ERROR] %s failed at %s:%d, ret=%d\n", #expr, __FILE__, __LINE__, __code); \ | ||
| 303 | + } \ | ||
| 304 | + } while (0) | ||
| 305 | + | ||
| 306 | +int32_t main(int32_t argc, char* argv[]) | ||
| 307 | +{ | ||
| 308 | + (void)argc; | ||
| 309 | + (void)argv; | ||
| 310 | + | ||
| 311 | + constexpr size_t input_element_size = IS_INT8_INPUT ? sizeof(int8_t) : sizeof(half); | ||
| 312 | + constexpr size_t output_element_size = (SCENARIO_NUM == 1 || SCENARIO_NUM == 2) ? sizeof(half) : sizeof(int8_t); | ||
| 313 | + constexpr size_t a_file_size = M * K * input_element_size; | ||
| 314 | + constexpr size_t b_file_size = K * N * input_element_size; | ||
| 315 | + constexpr size_t c_file_size = M * N * output_element_size; | ||
| 316 | + constexpr int32_t device_id = 0; | ||
| 317 | + | ||
| 318 | + uint8_t* a_host = nullptr; | ||
| 319 | + uint8_t* b_host = nullptr; | ||
| 320 | + uint8_t* c_host = nullptr; | ||
| 321 | + uint8_t* a_device = nullptr; | ||
| 322 | + uint8_t* b_device = nullptr; | ||
| 323 | + void* c_device = nullptr; | ||
| 324 | + uint8_t* quant_host = nullptr; | ||
| 325 | + uint64_t* quant_device = nullptr; | ||
| 326 | + aclrtStream stream = nullptr; | ||
| 327 | + | ||
| 328 | + CHECK_ACL(aclInit(nullptr)); | ||
| 329 | + CHECK_ACL(aclrtSetDevice(device_id)); | ||
| 330 | + CHECK_ACL(aclrtCreateStream(&stream)); | ||
| 331 | + | ||
| 332 | + CHECK_ACL(aclrtMallocHost(reinterpret_cast<void**>(&a_host), a_file_size)); | ||
| 333 | + CHECK_ACL(aclrtMalloc(reinterpret_cast<void**>(&a_device), a_file_size, ACL_MEM_MALLOC_HUGE_FIRST)); | ||
| 334 | + ReadFile("./input/x1_gm.bin", a_host, a_file_size); | ||
🟡 Medium Priority 变更行 → 受影响行为 → 失败模式 → 建议修复。 data_utils.h 中的 ReadFile/WriteFile 显式设计为返回 bool,并有完整的失败路径(文件不存在 stat 失败、S_ISREG 校验、大小不匹配、读不足、写失败等),但 data_copy_l0c2l1.asc 的 main() 中 4 处调用(333 行 ReadFile x1、338 行 ReadFile x2、348 行 ReadFile quant_pre、356 行 WriteFile output)全部丢弃返回值。 失败模式:当 建议:对 333/338/348/356 行的 ReadFile/WriteFile 检查返回的 bool,失败时打印并终止(return 1),避免在输入文件缺失或写入失败时仍使用未初始化缓冲继续计算。 ![]() ![]() Incorrect? | |||
| 335 | + CHECK_ACL(aclrtMemcpy(a_device, a_file_size, a_host, a_file_size, ACL_MEMCPY_HOST_TO_DEVICE)); | ||
| 336 | + | ||
| 337 | + CHECK_ACL(aclrtMallocHost(reinterpret_cast<void**>(&b_host), b_file_size)); | ||
| 338 | + CHECK_ACL(aclrtMalloc(reinterpret_cast<void**>(&b_device), b_file_size, ACL_MEM_MALLOC_HUGE_FIRST)); | ||
| 339 | + ReadFile("./input/x2_gm.bin", b_host, b_file_size); | ||
| 340 | + CHECK_ACL(aclrtMemcpy(b_device, b_file_size, b_host, b_file_size, ACL_MEMCPY_HOST_TO_DEVICE)); | ||
| 341 | + | ||
| 342 | + CHECK_ACL(aclrtMallocHost(reinterpret_cast<void**>(&c_host), c_file_size)); | ||
| 343 | + CHECK_ACL(aclrtMalloc(&c_device, c_file_size, ACL_MEM_MALLOC_HUGE_FIRST)); | ||
| 344 | + | ||
| 345 | + if constexpr (IS_VECTOR_QUANT) { | ||
| 346 | + CHECK_ACL(aclrtMallocHost(reinterpret_cast<void**>(&quant_host), QUANT_TRANSFER_BYTES)); | ||
| 347 | + CHECK_ACL( | ||
| 348 | + aclrtMalloc(reinterpret_cast<void**>(&quant_device), QUANT_TRANSFER_BYTES, ACL_MEM_MALLOC_HUGE_FIRST)); | ||
| 349 | + ReadFile("./input/quant_pre.bin", quant_host, QUANT_TRANSFER_BYTES); | ||
| 350 | + CHECK_ACL(aclrtMemcpy( | ||
| 351 | + quant_device, QUANT_TRANSFER_BYTES, quant_host, QUANT_TRANSFER_BYTES, ACL_MEMCPY_HOST_TO_DEVICE)); | ||
| 352 | + } | ||
| 353 | + | ||
| 354 | + data_copy_l0c2l1<<<1, 0, stream>>>(a_device, b_device, c_device, quant_device); | ||
| 355 | + CHECK_ACL(aclrtSynchronizeStream(stream)); | ||
| 356 | + CHECK_ACL(aclrtMemcpy(c_host, c_file_size, c_device, c_file_size, ACL_MEMCPY_DEVICE_TO_HOST)); | ||
| 357 | + WriteFile("./output/output.bin", c_host, c_file_size); | ||
| 358 | + | ||
| 359 | + if constexpr (IS_VECTOR_QUANT) { | ||
| 360 | + CHECK_ACL(aclrtFree(quant_device)); | ||
| 361 | + CHECK_ACL(aclrtFreeHost(quant_host)); | ||
| 362 | + } | ||
| 363 | + CHECK_ACL(aclrtFree(c_device)); | ||
| 364 | + CHECK_ACL(aclrtFreeHost(c_host)); | ||
| 365 | + CHECK_ACL(aclrtFree(b_device)); | ||
| 366 | + CHECK_ACL(aclrtFreeHost(b_host)); | ||
| 367 | + CHECK_ACL(aclrtFree(a_device)); | ||
| 368 | + CHECK_ACL(aclrtFreeHost(a_host)); | ||
| 369 | + CHECK_ACL(aclrtDestroyStream(stream)); | ||
| 370 | + CHECK_ACL(aclrtResetDevice(device_id)); | ||
| 371 | + CHECK_ACL(aclFinalize()); | ||
| 372 | + return 0; | ||
| 373 | +} | ||
| @@ -0,0 +1,119 @@ | |||
| 1 | +/** | ||
| 2 | + * Copyright (c) 2026 Huawei Technologies Co., Ltd. | ||
| 3 | + * This program is free software, you can redistribute it and/or modify it under the terms and conditions of | ||
| 4 | + * CANN Open Software License Agreement Version 2.0 (the "License"). | ||
| 5 | + * Please refer to the License for details. You may not use this file except in compliance with the License. | ||
| 6 | + * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED, | ||
| 7 | + * INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE. | ||
| 8 | + * See LICENSE in the root of the software repository for the full text of the License. | ||
| 9 | + */ | ||
| 10 | + | ||
| 11 | +/* ! | ||
| 12 | + * \file data_utils.h | ||
| 13 | + * \brief File read/write helpers for the host program. | ||
| 14 | + */ | ||
| 15 | + | ||
| 16 | +#ifndef DATA_UTILS_H | ||
| 17 | +#define DATA_UTILS_H | ||
| 18 | + | ||
| 19 | +#include <fcntl.h> | ||
| 20 | +#include <sys/stat.h> | ||
| 21 | +#include <unistd.h> | ||
| 22 | + | ||
| 23 | +#include <cerrno> | ||
| 24 | +#include <cstdint> | ||
| 25 | +#include <cstdio> | ||
| 26 | +#include <fstream> | ||
| 27 | +#include <limits> | ||
| 28 | + | ||
| 29 | +#define ERROR_LOG(fmt, args...) fprintf(stdout, "[ERROR] " fmt "\n", ##args) | ||
| 30 | + | ||
| 31 | +bool ReadFile(const std::string& file_path, void* buffer, size_t expected_size) | ||
| 32 | +{ | ||
| 33 | + if (buffer == nullptr || expected_size == 0) { | ||
| 34 | + ERROR_LOG("Read file failed. buffer is nullptr or expected size is 0"); | ||
| 35 | + return false; | ||
| 36 | + } | ||
| 37 | + | ||
| 38 | + struct stat stat_buffer; | ||
| 39 | + if (stat(file_path.data(), &stat_buffer) == -1) { | ||
| 40 | + ERROR_LOG("failed to get file"); | ||
| 41 | + return false; | ||
| 42 | + } | ||
| 43 | + if (S_ISREG(stat_buffer.st_mode) == 0) { | ||
| 44 | + ERROR_LOG("%s is not a file, please enter a file", file_path.c_str()); | ||
| 45 | + return false; | ||
| 46 | + } | ||
| 47 | + if (stat_buffer.st_size <= 0 || | ||
| 48 | + static_cast<uintmax_t>(stat_buffer.st_size) != static_cast<uintmax_t>(expected_size)) { | ||
| 49 | + ERROR_LOG( | ||
| 50 | + "file size does not match expected size. expected = %zu, actual = %lld", expected_size, | ||
| 51 | + static_cast<long long>(stat_buffer.st_size)); | ||
| 52 | + return false; | ||
| 53 | + } | ||
| 54 | + if (expected_size > static_cast<size_t>(std::numeric_limits<std::streamsize>::max())) { | ||
| 55 | + ERROR_LOG("file size exceeds stream size limit. size = %zu", expected_size); | ||
| 56 | + return false; | ||
| 57 | + } | ||
| 58 | + | ||
| 59 | + std::ifstream file(file_path, std::ios::binary); | ||
| 60 | + if (!file.is_open()) { | ||
| 61 | + ERROR_LOG("Open file failed. path = %s", file_path.c_str()); | ||
| 62 | + return false; | ||
| 63 | + } | ||
| 64 | + file.read(static_cast<char*>(buffer), static_cast<std::streamsize>(expected_size)); | ||
| 65 | + if (file.gcount() != static_cast<std::streamsize>(expected_size)) { | ||
| 66 | + ERROR_LOG( | ||
| 67 | + "Read file failed. expected = %zu, actual = %lld", expected_size, static_cast<long long>(file.gcount())); | ||
| 68 | + file.close(); | ||
| 69 | + return false; | ||
| 70 | + } | ||
| 71 | + file.close(); | ||
| 72 | + if (file.fail()) { | ||
| 73 | + ERROR_LOG("Close file failed. path = %s", file_path.c_str()); | ||
| 74 | + return false; | ||
| 75 | + } | ||
| 76 | + return true; | ||
| 77 | +} | ||
| 78 | + | ||
| 79 | +bool WriteFile(const std::string& file_path, const void* buffer, size_t size) | ||
| 80 | +{ | ||
| 81 | + if (buffer == nullptr) { | ||
| 82 | + ERROR_LOG("Write file failed. buffer is nullptr"); | ||
| 83 | + return false; | ||
| 84 | + } | ||
| 85 | + | ||
| 86 | + int fd = open(file_path.c_str(), O_WRONLY | O_CREAT | O_TRUNC, S_IRUSR | S_IWRITE); | ||
| 87 | + if (fd < 0) { | ||
| 88 | + ERROR_LOG("Open file failed. path = %s", file_path.c_str()); | ||
| 89 | + return false; | ||
| 90 | + } | ||
| 91 | + | ||
| 92 | + size_t written_size = 0; | ||
| 93 | + const char* src = static_cast<const char*>(buffer); | ||
| 94 | + while (written_size < size) { | ||
| 95 | + ssize_t write_size = write(fd, src + written_size, size - written_size); | ||
| 96 | + if (write_size < 0) { | ||
| 97 | + if (errno == EINTR) { | ||
| 98 | + continue; | ||
| 99 | + } | ||
| 100 | + ERROR_LOG("Write file failed. path = %s", file_path.c_str()); | ||
| 101 | + (void)close(fd); | ||
| 102 | + return false; | ||
| 103 | + } | ||
| 104 | + if (write_size == 0) { | ||
| 105 | + ERROR_LOG("Write file failed. no data was written"); | ||
| 106 | + (void)close(fd); | ||
| 107 | + return false; | ||
| 108 | + } | ||
| 109 | + written_size += static_cast<size_t>(write_size); | ||
| 110 | + } | ||
| 111 | + | ||
| 112 | + if (close(fd) != 0) { | ||
| 113 | + ERROR_LOG("Close file failed. path = %s", file_path.c_str()); | ||
| 114 | + return false; | ||
| 115 | + } | ||
| 116 | + return true; | ||
| 117 | +} | ||
| 118 | + | ||
| 119 | +#endif // DATA_UTILS_H | ||
| @@ -0,0 +1,190 @@ | |||
| 1 | +#!/usr/bin/env python3 | ||
| 2 | +# -*- coding: utf-8 -*- | ||
| 3 | +# ---------------------------------------------------------------------------------------------------------- | ||
| 4 | +# Copyright (c) 2026 Huawei Technologies Co., Ltd. | ||
| 5 | +# This program is free software, you can redistribute it and/or modify it under the terms and conditions of | ||
| 6 | +# CANN Open Software License Agreement Version 2.0 (the "License"). | ||
| 7 | +# Please refer to the License for details. You may not use this file except in compliance with the License. | ||
| 8 | +# THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED, | ||
| 9 | +# INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE. | ||
| 10 | +# See LICENSE in the root of the software repository for the full text of the License. | ||
| 11 | +# ---------------------------------------------------------------------------------------------------------- | ||
| 12 | + | ||
| 13 | +import argparse | ||
| 14 | +import os | ||
| 15 | +import struct | ||
| 16 | + | ||
| 17 | +import numpy as np | ||
| 18 | + | ||
| 19 | + | ||
| 20 | +M = 128 | ||
| 21 | +K = 128 | ||
| 22 | +N = 256 | ||
| 23 | +K_ROUND = 2 | ||
| 24 | +VECTOR_QUANT_SCENARIOS = (2, 4, 6) | ||
| 25 | +NZ_OUTPUT_SCENARIOS = (2, 3, 6) | ||
| 26 | +RELU_SCENARIOS = (1, 4, 6) | ||
| 27 | + | ||
| 28 | + | ||
Outdated 两个空行,ruff_format可能会失败 ![]() ![]() | |||
| 29 | +def decode_quant_pre(quant_pre): | ||
| 30 | + quant_pre = int(quant_pre) | ||
| 31 | + alpha_bits = (quant_pre >> 13) & 0xFFFFF | ||
| 32 | + sign_bit = (alpha_bits >> 18) & 0x1 | ||
| 33 | + exponent = (alpha_bits >> 10) & 0xFF | ||
| 34 | + mantissa = alpha_bits & 0x3FF | ||
| 35 | + alpha = (-1) ** sign_bit * (1 + mantissa / 1024) * (2 ** (exponent - 127)) | ||
| 36 | + offset = (quant_pre >> 37) & 0x1FF | ||
| 37 | + sign = (quant_pre >> 46) & 0x1 | ||
| 38 | + shift = (quant_pre >> 32) & 0xF | ||
| 39 | + mode_control = (quant_pre >> 36) & 0x1 | ||
| 40 | + return alpha, offset, sign, shift, mode_control | ||
| 41 | + | ||
| 42 | + | ||
| 43 | +def decode_relu_pre(relu_pre): | ||
| 44 | + alpha_bits = (int(relu_pre) >> 13) & 0xFFFFF | ||
| 45 | + sign_bit = (alpha_bits >> 18) & 0x1 | ||
| 46 | + exponent = (alpha_bits >> 10) & 0xFF | ||
| 47 | + mantissa = alpha_bits & 0x3FF | ||
| 48 | + return (-1) ** sign_bit * (1 + mantissa / 1024) * (2 ** (exponent - 127)) | ||
| 49 | + | ||
| 50 | + | ||
| 51 | +def saturate(value, min_value, max_value, dtype): | ||
| 52 | + return np.round(np.clip(value, min_value, max_value)).astype(dtype) | ||
| 53 | + | ||
| 54 | + | ||
| 55 | +def snap_near_half_integer(value, epsilon=1e-5): | ||
| 56 | + absolute_value = abs(value) | ||
| 57 | + fractional_part = absolute_value - np.floor(absolute_value) | ||
| 58 | + if 0.5 - epsilon < fractional_part < 0.5: | ||
| 59 | + return np.sign(value) * (np.floor(absolute_value) + np.float32(0.5)) | ||
| 60 | + return value | ||
| 61 | + | ||
| 62 | + | ||
| 63 | +def apply_deqf16(value, quant_pre, relu_pre): | ||
| 64 | + alpha, offset, _, shift, mode_control = decode_quant_pre(quant_pre) | ||
| 65 | + if mode_control: | ||
| 66 | + value = saturate( | ||
| 67 | + value >> shift, | ||
| 68 | + np.finfo(np.float16).min, | ||
| 69 | + np.finfo(np.float16).max, | ||
| 70 | + np.float16, | ||
| 71 | + ) | ||
| 72 | + value = np.float32(value) | ||
| 73 | + value *= alpha if value >= 0 else decode_relu_pre(relu_pre) | ||
| 74 | + value = ( | ||
| 75 | + saturate( | ||
| 76 | + value, | ||
| 77 | + np.finfo(np.float16).min, | ||
| 78 | + np.finfo(np.float16).max, | ||
| 79 | + np.float16, | ||
| 80 | + ) | ||
| 81 | + + offset | ||
| 82 | + ) | ||
| 83 | + return saturate( | ||
| 84 | + value, | ||
| 85 | + np.finfo(np.float16).min, | ||
| 86 | + np.finfo(np.float16).max, | ||
| 87 | + np.float16, | ||
| 88 | + ) | ||
| 89 | + | ||
| 90 | + | ||
| 91 | +def apply_qf322b8(value, quant_pre, relu_pre): | ||
| 92 | + alpha, offset, sign, shift, mode_control = decode_quant_pre(quant_pre) | ||
| 93 | + if mode_control: | ||
| 94 | + value = saturate(value / (2**shift), -32768, 32767, np.int16) | ||
| 95 | + value = np.float32(value) | ||
| 96 | + value *= alpha if value >= 0 else decode_relu_pre(relu_pre) | ||
| 97 | + value = snap_near_half_integer(value) | ||
| 98 | + value = saturate(value, -256, 255, np.int16) + offset | ||
| 99 | + return ( | ||
| 100 | + saturate(value, -128, 127, np.int8) | ||
| 101 | + if sign | ||
| 102 | + else saturate(value, 0, 255, np.uint8) | ||
| 103 | + ) | ||
| 104 | + | ||
| 105 | + | ||
| 106 | +def apply_req8(value, quant_pre, relu_pre): | ||
| 107 | + alpha, offset, sign, shift, mode_control = decode_quant_pre(quant_pre) | ||
| 108 | + if mode_control: | ||
| 109 | + value = saturate(value >> shift, -32768, 32767, np.int16) | ||
| 110 | + value = np.float32(value) | ||
| 111 | + value *= alpha if value >= 0 else decode_relu_pre(relu_pre) | ||
| 112 | + value = snap_near_half_integer(value) | ||
| 113 | + value = saturate(value, -256, 255, np.int16) + offset | ||
| 114 | + return ( | ||
| 115 | + saturate(value, -128, 127, np.int8) | ||
| 116 | + if sign | ||
| 117 | + else saturate(value, 0, 255, np.uint8) | ||
| 118 | + ) | ||
| 119 | + | ||
| 120 | + | ||
| 121 | +def make_quant_params(): | ||
| 122 | + alpha_bits = struct.unpack("!I", struct.pack("!f", 2.0))[0] | ||
| 123 | + return np.full(N, np.uint64(alpha_bits | (1 << 46)), dtype=np.uint64) | ||
| 124 | + | ||
| 125 | + | ||
| 126 | +def apply_prequant(matrix, scenario_num, quant_params): | ||
| 127 | + relu_alpha = 0.0 if scenario_num in RELU_SCENARIOS else 2.0 | ||
| 128 | + relu_pre = struct.unpack("!I", struct.pack("!f", relu_alpha))[0] | ||
| 129 | + result_dtype = np.float16 if scenario_num in (1, 2) else np.int8 | ||
| 130 | + result = np.zeros((M, N), dtype=result_dtype) | ||
| 131 | + for row in range(M): | ||
| 132 | + for column in range(N): | ||
| 133 | + if scenario_num in (1, 2): | ||
| 134 | + result[row, column] = apply_deqf16( | ||
| 135 | + matrix[row, column], quant_params[column], relu_pre | ||
| 136 | + ) | ||
| 137 | + elif scenario_num in (3, 4): | ||
| 138 | + result[row, column] = apply_qf322b8( | ||
| 139 | + matrix[row, column], quant_params[column], relu_pre | ||
| 140 | + ) | ||
| 141 | + else: | ||
| 142 | + result[row, column] = apply_req8( | ||
| 143 | + matrix[row, column], quant_params[column], relu_pre | ||
| 144 | + ) | ||
| 145 | + return result | ||
| 146 | + | ||
| 147 | + | ||
| 148 | +def gen_golden_data(scenario_num): | ||
| 149 | + np.random.seed(9) | ||
| 150 | + if scenario_num in (1, 2, 5, 6): | ||
| 151 | + input_dtype = np.int8 | ||
| 152 | + mm_dtype = np.int32 | ||
| 153 | + output_dtype = np.float16 if scenario_num in (1, 2) else np.int8 | ||
| 154 | + x1 = np.random.randint(-3, 3, (M, K)).astype(input_dtype) | ||
| 155 | + x2 = np.random.randint(-3, 3, (K, N)).astype(input_dtype) | ||
| 156 | + else: | ||
| 157 | + input_dtype = np.float16 | ||
| 158 | + mm_dtype = np.float32 | ||
| 159 | + output_dtype = np.int8 | ||
| 160 | + x1 = np.random.uniform(-3.0, 3.0, (M, K)).astype(input_dtype) | ||
| 161 | + x2 = np.random.uniform(-3.0, 3.0, (K, N)).astype(input_dtype) | ||
| 162 | + | ||
| 163 | + golden = np.matmul(x1.astype(mm_dtype), x2.astype(mm_dtype)).astype(mm_dtype) | ||
| 164 | + quant_params = make_quant_params() | ||
| 165 | + golden = apply_prequant(golden, scenario_num, quant_params) | ||
| 166 | + | ||
| 167 | + os.makedirs("input", exist_ok=True) | ||
| 168 | + os.makedirs("output", exist_ok=True) | ||
| 169 | + if scenario_num in VECTOR_QUANT_SCENARIOS: | ||
| 170 | + quant_params.tofile("./input/quant_pre.bin") | ||
| 171 | + | ||
| 172 | + if scenario_num in NZ_OUTPUT_SCENARIOS: | ||
| 173 | + block_columns = 16 if output_dtype == np.float16 else 32 | ||
| 174 | + golden = golden.reshape( | ||
| 175 | + M // 16, 16, N // block_columns, block_columns | ||
| 176 | + ).transpose(2, 0, 1, 3) | ||
| 177 | + | ||
| 178 | + # The kernel consumes A as two consecutive [M, K / K_ROUND] chunks. | ||
| 179 | + x1.reshape(M, K_ROUND, K // K_ROUND).transpose(1, 0, 2).astype(input_dtype).tofile( | ||
| 180 | + "./input/x1_gm.bin" | ||
| 181 | + ) | ||
| 182 | + x2.astype(input_dtype).tofile("./input/x2_gm.bin") | ||
| 183 | + golden.astype(output_dtype).tofile("./output/golden.bin") | ||
| 184 | + | ||
| 185 | + | ||
| 186 | +if __name__ == "__main__": | ||
| 187 | + parser = argparse.ArgumentParser() | ||
| 188 | + parser.add_argument("-scenarioNum", type=int, default=1, choices=range(1, 7)) | ||
| 189 | + args = parser.parse_args() | ||
| 190 | + gen_golden_data(args.scenarioNum) | ||
| @@ -0,0 +1,57 @@ | |||
| 1 | +#!/usr/bin/env python3 | ||
| 2 | +# -*- coding: utf-8 -*- | ||
| 3 | +# ---------------------------------------------------------------------------------------------------------- | ||
| 4 | +# Copyright (c) 2026 Huawei Technologies Co., Ltd. | ||
| 5 | +# This program is free software, you can redistribute it and/or modify it under the terms and conditions of | ||
| 6 | +# CANN Open Software License Agreement Version 2.0 (the "License"). | ||
| 7 | +# Please refer to the License for details. You may not use this file except in compliance with the License. | ||
| 8 | +# THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED, | ||
| 9 | +# INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE. | ||
| 10 | +# See LICENSE in the root of the software repository for the full text of the License. | ||
| 11 | +# ---------------------------------------------------------------------------------------------------------- | ||
| 12 | + | ||
| 13 | +import argparse | ||
| 14 | +import sys | ||
| 15 | + | ||
| 16 | +import numpy as np | ||
| 17 | + | ||
| 18 | + | ||
| 19 | +RELATIVE_TOL = 1e-6 | ||
| 20 | +ABSOLUTE_TOL = 1e-9 | ||
| 21 | +ERROR_TOL = 1e-4 | ||
| 22 | + | ||
| 23 | + | ||
| 24 | +def verify_result(scenario_num, output_path, golden_path): | ||
| 25 | + output_dtype = np.float16 if scenario_num in (1, 2) else np.int8 | ||
| 26 | + output = np.fromfile(output_path, dtype=output_dtype).reshape(-1) | ||
| 27 | + golden = np.fromfile(golden_path, dtype=output_dtype).reshape(-1) | ||
| 28 | + if output.size != golden.size: | ||
| 29 | + print( | ||
| 30 | + "element count mismatch: expected %d, actual %d" | ||
| 31 | + % (golden.size, output.size) | ||
| 32 | + ) | ||
| 33 | + return False | ||
| 34 | + | ||
| 35 | + match = np.isclose( | ||
| 36 | + output, golden, rtol=RELATIVE_TOL, atol=ABSOLUTE_TOL, equal_nan=True | ||
| 37 | + ) | ||
| 38 | + mismatch_indices = np.where(~match)[0] | ||
| 39 | + for index in mismatch_indices[:101]: | ||
| 40 | + print( | ||
| 41 | + "data index: %06d, expected: %-.9f, actual: %-.9f" | ||
| 42 | + % (index, golden[index], output[index]) | ||
| 43 | + ) | ||
| 44 | + error_ratio = float(mismatch_indices.size) / golden.size | ||
| 45 | + return error_ratio <= ERROR_TOL | ||
| 46 | + | ||
| 47 | + | ||
| 48 | +if __name__ == "__main__": | ||
| 49 | + parser = argparse.ArgumentParser() | ||
| 50 | + parser.add_argument("-scenarioNum", type=int, default=1, choices=range(1, 7)) | ||
| 51 | + parser.add_argument("output", type=str) | ||
| 52 | + parser.add_argument("golden", type=str) | ||
| 53 | + args = parser.parse_args() | ||
| 54 | + if not verify_result(args.scenarioNum, args.output, args.golden): | ||
| 55 | + print("[ERROR] result error") | ||
| 56 | + sys.exit(1) | ||
| 57 | + print("test pass!") | ||
| @@ -0,0 +1,38 @@ | |||
| 1 | +# ---------------------------------------------------------------------------------------------------------- | ||
| 2 | +# Copyright (c) 2026 Huawei Technologies Co., Ltd. | ||
| 3 | +# This program is free software, you can redistribute it and/or modify it under the terms and conditions of | ||
| 4 | +# CANN Open Software License Agreement Version 2.0 (the "License"). | ||
| 5 | +# Please refer to the License for details. You may not use this file except in compliance with the License. | ||
| 6 | +# THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED, | ||
| 7 | +# INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE. | ||
| 8 | +# See LICENSE in the root of the software repository for the full text of the License. | ||
| 9 | +# ---------------------------------------------------------------------------------------------------------- | ||
| 10 | + | ||
| 11 | +cmake_minimum_required(VERSION 3.16) | ||
| 12 | + | ||
| 13 | +set(CMAKE_ASC_RUN_MODE "npu" CACHE STRING "Run mode: npu, sim") | ||
| 14 | +set(CMAKE_ASC_ARCHITECTURES "dav-3510" CACHE STRING "NPU architecture: dav-3510") | ||
| 15 | + | ||
| 16 | +find_package(ASC REQUIRED) | ||
| 17 | + | ||
| 18 | +project(c_api_data_copy_l0c2ub LANGUAGES ASC CXX) | ||
| 19 | + | ||
| 20 | +if(NOT CMAKE_ASC_ARCHITECTURES STREQUAL "dav-3510") | ||
| 21 | + message(FATAL_ERROR "This C API example only supports CMAKE_ASC_ARCHITECTURES=dav-3510.") | ||
| 22 | +endif() | ||
| 23 | + | ||
| 24 | +if(NOT CMAKE_ASC_RUN_MODE MATCHES "^(npu|sim)$") | ||
| 25 | + message(FATAL_ERROR "CMAKE_ASC_RUN_MODE must be npu or sim, but got ${CMAKE_ASC_RUN_MODE}.") | ||
| 26 | +endif() | ||
| 27 | + | ||
| 28 | +set(SCENARIO_NUM "1" CACHE STRING "Scene number (1-7)") | ||
| 29 | +if(NOT SCENARIO_NUM MATCHES "^[1-7]$") | ||
| 30 | + message(FATAL_ERROR "SCENARIO_NUM must be 1, 2, 3, 4, 5, 6, or 7, but got ${SCENARIO_NUM}") | ||
| 31 | +endif() | ||
| 32 | + | ||
| 33 | +add_executable(demo data_copy_l0c2ub.asc) | ||
| 34 | + | ||
| 35 | +add_definitions(-DSCENARIO_NUM=${SCENARIO_NUM}) | ||
| 36 | +target_compile_options(demo PRIVATE | ||
| 37 | + $<$<COMPILE_LANGUAGE:ASC>:--npu-arch=${CMAKE_ASC_ARCHITECTURES}> | ||
| 38 | +) | ||
| @@ -0,0 +1,140 @@ | |||
| 1 | +# data_copy_l0c2ub样例 | ||
| 2 | + | ||
| 3 | +## 概述 | ||
| 4 | + | ||
| 5 | +本样例演示如何使用Ascend C C API,通过Fixpipe将矩阵乘计算结果从L0C Buffer搬运到UB(Unified Buffer),并在搬运过程中完成标量、Vector量化、ReLU激活和NZ2ND格式转换。输入矩阵A和B均为ND格式,先从GM搬运到L1 Buffer并转换为Nz格式,再经L0A Buffer和L0B Buffer完成两次K轴分块矩阵乘。为便于Host侧校验,样例随后将UB中的输出数据搬运回GM。 | ||
| 6 | + | ||
| 7 | +本样例适用于Ascend 950PR/Ascend 950DT(`dav-3510`),可在NPU运行模式或NPU仿真模式下执行,不提供CPU域调试模式。 | ||
| 8 | + | ||
| 9 | +## 本样例支持的产品及CANN软件版本 | ||
| 10 | + | ||
| 11 | +| 产品 | CANN软件版本 | | ||
| 12 | +|------|-------------| | ||
| 13 | +| Ascend 950PR/Ascend 950DT | >= CANN 9.2.0 | | ||
| 14 | + | ||
| 15 | +## 目录结构介绍 | ||
| 16 | + | ||
| 17 | +``` | ||
| 18 | +data_copy_l0c2ub | ||
| 19 | +├── scripts | ||
| 20 | +│ ├── gen_data.py // 输入数据和真值数据生成脚本 | ||
| 21 | +│ └── verify_result.py // 输出数据与真值数据精度校验脚本 | ||
| 22 | +├── data_utils.h // 数据读入写出函数 | ||
| 23 | +├── CMakeLists.txt // CMake编译文件 | ||
| 24 | +├── data_copy_l0c2ub.asc // C API样例实现及调用代码 | ||
| 25 | +├── README.md // 中文样例说明文档 | ||
| 26 | +└── README_en.md // 英文样例说明文档 | ||
| 27 | +``` | ||
| 28 | + | ||
| 29 | +## 场景详细说明 | ||
| 30 | + | ||
| 31 | +通过编译参数`SCENARIO_NUM`选择场景。所有场景的矩阵乘规格均为`[M, K, N] = [128, 128, 256]`,其中K轴拆分为两个`[128, 64]`分块,内核函数名为`data_copy_l0c2ub`。 | ||
| 32 | + | ||
| 33 | +| SCENARIO_NUM | A/B类型 | L0C类型 | 输出类型与格式 | 量化模式 | ReLU | NZ2ND | | ||
| 34 | +|---|---|---|---|---|---|---| | ||
| 35 | +| 1 | int8_t | int32_t | half,ND | 标量`DEQF16` | 是 | 是 | | ||
| 36 | +| 2 | int8_t | int32_t | half,Nz | Vector `VDEQF16` | 否 | 否 | | ||
| 37 | +| 3 | half | float | int8_t,Nz | 标量`QF322B8_PRE` | 否 | 否 | | ||
| 38 | +| 4 | half | float | int8_t,ND | Vector `VQF322B8_PRE` | 是 | 是 | | ||
| 39 | +| 5 | int8_t | int32_t | int8_t,ND | 标量`REQ8` | 否 | 是 | | ||
| 40 | +| 6 | int8_t | int32_t | int8_t,Nz | Vector `VREQ8` | 是 | 否 | | ||
| 41 | +| 7 | half | float | float,ND | `NoQuant` | 否 | 是,M维双目标拆分 | | ||
| 42 | + | ||
| 43 | +`SCENARIO_NUM`由CMake作为编译期宏传入,内核通过`if constexpr`选择对应场景。切换场景后,需要重新编译。 | ||
| 44 | + | ||
| 45 | +设备侧数据流如下:每个K轴分块按照`GM→L1 Buffer(MTE2)→L0A Buffer/L0B Buffer(MTE1)→Mmad(M)`执行;`MTE1→MTE2`和`M→MTE1`仅用于保护下一轮分块对L1 Buffer、L0A Buffer和L0B Buffer的复用。最终建立`M→FIX`依赖,使Fixpipe在Mmad完成后通过`asc_copy_l0c2ub`将L0C Buffer中的计算结果搬运到UB。场景1至6为单目标模式,结果写入subblock0的UB并由subblock0回写GM;场景7使用`DUAL_DST_SPLIT_M`模式,将L0C结果按M维均分并分别写入两个AIV的UB,两个AIV等待数据就绪后各自写回一半GM结果。核函数结束时调用`asc_sync_pipe(PIPE_ALL)`,确保全部流水完成。 | ||
| 46 | + | ||
| 47 | +**场景1:int8输入、标量反量化并输出ND half** | ||
| 48 | + | ||
| 49 | +- 输入:A `[128, 128]`、B `[128, 256]`,均为ND格式`int8_t` | ||
| 50 | +- 输出:C `[128, 256]`,ND格式`half` | ||
| 51 | +- 实现:调用`asc_set_l0c_copy_prequant`配置标量反量化参数,使用`DEQF16`模式;启用ReLU和NZ2ND | ||
| 52 | + | ||
| 53 | +**场景2:int8输入、Vector反量化并输出Nz half** | ||
| 54 | + | ||
| 55 | +- 输入:A `[128, 128]`、B `[128, 256]`,均为ND格式`int8_t`;量化参数`[256] uint64_t` | ||
| 56 | +- 输出:C `[128, 256]`,Nz格式`half` | ||
| 57 | +- 实现:使用`VDEQF16`模式,每列使用一个Vector量化参数 | ||
| 58 | + | ||
| 59 | +**场景3:half输入、标量量化并输出Nz int8_t** | ||
| 60 | + | ||
| 61 | +- 输入:A `[128, 128]`、B `[128, 256]`,均为ND格式`half` | ||
| 62 | +- 输出:C `[128, 256]`,Nz格式`int8_t` | ||
| 63 | +- 实现:调用`asc_set_l0c_copy_prequant`配置标量量化参数,使用`QF322B8_PRE`模式 | ||
| 64 | + | ||
| 65 | +**场景4:half输入、Vector量化并输出ND int8_t** | ||
| 66 | + | ||
| 67 | +- 输入:A `[128, 128]`、B `[128, 256]`,均为ND格式`half`;量化参数`[256] uint64_t` | ||
| 68 | +- 输出:C `[128, 256]`,ND格式`int8_t` | ||
| 69 | +- 实现:使用`VQF322B8_PRE`模式;启用ReLU和NZ2ND | ||
| 70 | + | ||
| 71 | +**场景5:int8输入、标量量化并输出ND int8_t** | ||
| 72 | + | ||
| 73 | +- 输入:A `[128, 128]`、B `[128, 256]`,均为ND格式`int8_t` | ||
| 74 | +- 输出:C `[128, 256]`,ND格式`int8_t` | ||
| 75 | +- 实现:调用`asc_set_l0c_copy_prequant`配置标量量化参数,使用`REQ8`模式并启用NZ2ND | ||
| 76 | + | ||
| 77 | +**场景6:int8输入、Vector量化并输出Nz int8_t** | ||
| 78 | + | ||
| 79 | +- 输入:A `[128, 128]`、B `[128, 256]`,均为ND格式`int8_t`;量化参数`[256] uint64_t` | ||
| 80 | +- 输出:C `[128, 256]`,Nz格式`int8_t` | ||
| 81 | +- 实现:使用`VREQ8`模式并启用ReLU | ||
| 82 | + | ||
| 83 | +**场景7:half输入、L0C到UB双目标M维拆分并输出ND float** | ||
| 84 | + | ||
| 85 | +- 输入:A `[128, 128]`、B `[128, 256]`,均为ND格式`half` | ||
| 86 | +- 输出:C `[128, 256]`,ND格式`float` | ||
| 87 | +- 实现:使用`asc_copy_l0c2ub`的`DUAL_DST_SPLIT_M`和`NoQuant`模式,将结果按M维均分至两个AIV的UB,再由两个AIV分别写回对应的GM分片 | ||
| 88 | + | ||
| 89 | +对于场景2、4和6,量化参数先通过`asc_copy_gm2l1`从GM搬运到L1 Buffer,再通过`asc_copy_l12fb`搬运到Fixpipe Buffer,最后调用`asc_set_l0c_copy_config`配置Vector量化参数地址。`asc_set_l0c_copy_config`的参数地址以128B为单位,因此参数文件需按128B向上对齐,不足部分补0。本样例的`[256] uint64_t`参数共2048B,已满足要求。量化参数从L1 Buffer搬运至Fixpipe Buffer时,需要搬运2048B数据,数据搬运单位为64B,对应`asc_copy_l12fb`的`len_burst`配置为32。量化参数从L1 Buffer搬运至Fixpipe Buffer后,通过`asc_sync_pipe(PIPE_FIX)`确保参数和配置生效,随后通过Fixpipe将L0C Buffer中的计算结果搬运到UB。 | ||
| 90 | + | ||
| 91 | +## 编译运行 | ||
| 92 | + | ||
| 93 | +在本样例根目录执行以下步骤。 | ||
| 94 | + | ||
| 95 | +- 配置环境变量 | ||
| 96 | + | ||
| 97 | + 请根据CANN开发套件包的[安装方式](../../../../../docs/zh/quick_start.md#prepare&install)配置环境变量。 | ||
| 98 | + | ||
| 99 | + ```bash | ||
| 100 | + source ${install_path}/cann/set_env.sh | ||
| 101 | + ``` | ||
| 102 | + | ||
| 103 | + > **说明:** `${install_path}`为CANN包安装目录;root用户默认为`/usr/local/Ascend`,非root用户默认为`${HOME}/Ascend`。 | ||
| 104 | + | ||
| 105 | +- NPU运行 | ||
| 106 | + | ||
| 107 | + ```bash | ||
| 108 | + SCENARIO_NUM=1 | ||
| 109 | + mkdir -p build && cd build; # 创建并进入build目录 | ||
| 110 | + cmake -DSCENARIO_NUM=$SCENARIO_NUM -DCMAKE_ASC_ARCHITECTURES=dav-3510 ..;make -j; # 编译工程,默认NPU模式 | ||
| 111 | + python3 ../scripts/gen_data.py -scenarioNum=$SCENARIO_NUM # 生成测试输入数据和真值数据 | ||
| 112 | + ./demo # 执行编译生成的可执行程序,执行样例 | ||
| 113 | + python3 ../scripts/verify_result.py -scenarioNum=$SCENARIO_NUM output/output.bin ./output/golden.bin # 验证输出结果是否正确 | ||
| 114 | + ``` | ||
| 115 | + | ||
| 116 | + > **说明:** `gen_data.py`和`verify_result.py`必须传入相同的`SCENARIO_NUM`。若校验命令不传`-scenarioNum`,脚本默认按场景1解析输出;场景7输出为`float32`,会被误按`float16`读取并导致校验失败。 | ||
| 117 | + | ||
| 118 | +- NPU仿真 | ||
| 119 | + | ||
| 120 | + ```bash | ||
| 121 | + cmake -DSCENARIO_NUM=$SCENARIO_NUM -DCMAKE_ASC_RUN_MODE=sim -DCMAKE_ASC_ARCHITECTURES=dav-3510 ..;make -j; # NPU仿真模式 | ||
| 122 | + ``` | ||
| 123 | + | ||
| 124 | + 切换运行模式前需清理CMake缓存,例如在`build`目录执行`rm CMakeCache.txt`后重新配置。 | ||
| 125 | + | ||
| 126 | +- 编译选项说明 | ||
| 127 | + | ||
| 128 | + | 选项 | 可选值 | 说明 | | ||
| 129 | + |------|--------|------| | ||
| 130 | + | `CMAKE_ASC_RUN_MODE` | `npu`(默认)、`sim` | 运行模式:NPU运行、NPU仿真 | | ||
| 131 | + | `CMAKE_ASC_ARCHITECTURES` | `dav-3510` | NPU架构,对应Ascend 950PR/Ascend 950DT | | ||
| 132 | + | `SCENARIO_NUM` | 1-7 | 场景编号 | | ||
| 133 | + | ||
| 134 | +- 执行结果 | ||
| 135 | + | ||
| 136 | + 输出与真值比较通过时,打印如下信息: | ||
| 137 | + | ||
| 138 | + ```bash | ||
| 139 | + test pass! | ||
| 140 | + ``` | ||
| @@ -0,0 +1,140 @@ | |||
| 1 | +# data_copy_l0c2ub Example | ||
| 2 | + | ||
| 3 | +## Overview | ||
| 4 | + | ||
| 5 | +This example uses the Ascend C C API to move matrix-multiplication results from L0C Buffer to UB (Unified Buffer) through Fixpipe. It performs scalar or vector quantization, ReLU activation, and NZ2ND conversion during the movement. The ND-format input matrices A and B are converted to Nz while moved from GM to L1 Buffer, then processed through L0A Buffer and L0B Buffer by two K-axis matrix-multiplication chunks. For host-side verification, the example then copies the UB output back to GM. | ||
| 6 | + | ||
| 7 | +This example applies to Ascend 950PR/Ascend 950DT (`dav-3510`) and can run in NPU execution or NPU simulation mode. CPU debug mode is not provided. | ||
| 8 | + | ||
| 9 | +## Supported Products and CANN Versions | ||
| 10 | + | ||
| 11 | +| Product | CANN version | | ||
| 12 | +|------|-------------| | ||
| 13 | +| Ascend 950PR/Ascend 950DT | >= CANN 9.2.0 | | ||
| 14 | + | ||
| 15 | +## Directory Structure | ||
| 16 | + | ||
| 17 | +``` | ||
| 18 | +data_copy_l0c2ub | ||
| 19 | +├── scripts | ||
| 20 | +│ ├── gen_data.py // Generates input and golden data | ||
| 21 | +│ └── verify_result.py // Verifies output against golden data | ||
| 22 | +├── data_utils.h // Data input/output helpers | ||
| 23 | +├── CMakeLists.txt // CMake build file | ||
| 24 | +├── data_copy_l0c2ub.asc // C API implementation and launcher | ||
| 25 | +├── README.md // Chinese example documentation | ||
| 26 | +└── README_en.md // English example documentation | ||
| 27 | +``` | ||
| 28 | + | ||
| 29 | +## Detailed Scenario Description | ||
| 30 | + | ||
| 31 | +Use the `SCENARIO_NUM` build parameter to select a scenario. Every scenario uses the matrix-multiplication shape `[M, K, N] = [128, 128, 256]`; the K axis is split into two `[128, 64]` chunks. The kernel is named `data_copy_l0c2ub`. | ||
| 32 | + | ||
| 33 | +| SCENARIO_NUM | A/B type | L0C type | Output type and format | Quantization mode | ReLU | NZ2ND | | ||
| 34 | +|---|---|---|---|---|---|---| | ||
| 35 | +| 1 | int8_t | int32_t | half, ND | Scalar `DEQF16` | Yes | Yes | | ||
| 36 | +| 2 | int8_t | int32_t | half, Nz | Vector `VDEQF16` | No | No | | ||
| 37 | +| 3 | half | float | int8_t, Nz | Scalar `QF322B8_PRE` | No | No | | ||
| 38 | +| 4 | half | float | int8_t, ND | Vector `VQF322B8_PRE` | Yes | Yes | | ||
| 39 | +| 5 | int8_t | int32_t | int8_t, ND | Scalar `REQ8` | No | Yes | | ||
| 40 | +| 6 | int8_t | int32_t | int8_t, Nz | Vector `VREQ8` | Yes | No | | ||
| 41 | +| 7 | half | float | float, ND | `NoQuant` | No | Yes, M-axis dual-destination split | | ||
| 42 | + | ||
| 43 | +`SCENARIO_NUM` is passed by CMake as a compile-time macro. The kernel selects the corresponding scenario with `if constexpr`. After changing the scenario, recompile the project. | ||
| 44 | + | ||
| 45 | +The device-side flow is as follows. Each K chunk follows `GM→L1 Buffer (MTE2)→L0A Buffer/L0B Buffer (MTE1)→Mmad (M)`. `MTE1→MTE2` and `M→MTE1` protect L1 Buffer, L0A Buffer, and L0B Buffer reuse for the next chunk. The final `M→FIX` dependency ensures that Fixpipe uses `asc_copy_l0c2ub` to move the L0C Buffer result to UB after Mmad completes. Scenarios 1 through 6 use single-destination mode, write the result to subblock0 UB, and let subblock0 write it back to GM. Scenario 7 uses `DUAL_DST_SPLIT_M` to split the L0C result along M and write each half to one AIV UB; both AIVs wait for the result and write their own GM slice. `asc_sync_pipe(PIPE_ALL)` is called at the end of the kernel to ensure that all pipelines complete. | ||
| 46 | + | ||
| 47 | +**Scenario 1: int8 input, scalar dequantization, and ND half output** | ||
| 48 | + | ||
| 49 | +- Input: A `[128, 128]` and B `[128, 256]`, both ND `int8_t` | ||
| 50 | +- Output: C `[128, 256]` in ND `half` | ||
| 51 | +- Implementation: Configure scalar dequantization with `asc_set_l0c_copy_prequant`, use `DEQF16`, and enable ReLU and NZ2ND | ||
| 52 | + | ||
| 53 | +**Scenario 2: int8 input, vector dequantization, and Nz half output** | ||
| 54 | + | ||
| 55 | +- Input: A `[128, 128]` and B `[128, 256]`, both ND `int8_t`; `[256] uint64_t` quantization parameters | ||
| 56 | +- Output: C `[128, 256]` in Nz `half` | ||
| 57 | +- Implementation: Use `VDEQF16`; each output column uses one vector quantization parameter | ||
| 58 | + | ||
| 59 | +**Scenario 3: half input, scalar quantization, and Nz int8_t output** | ||
| 60 | + | ||
| 61 | +- Input: A `[128, 128]` and B `[128, 256]`, both ND `half` | ||
| 62 | +- Output: C `[128, 256]` in Nz `int8_t` | ||
| 63 | +- Implementation: Configure scalar quantization with `asc_set_l0c_copy_prequant` and use `QF322B8_PRE` | ||
| 64 | + | ||
| 65 | +**Scenario 4: half input, vector quantization, and ND int8_t output** | ||
| 66 | + | ||
| 67 | +- Input: A `[128, 128]` and B `[128, 256]`, both ND `half`; `[256] uint64_t` quantization parameters | ||
| 68 | +- Output: C `[128, 256]` in ND `int8_t` | ||
| 69 | +- Implementation: Use `VQF322B8_PRE`, and enable ReLU and NZ2ND | ||
| 70 | + | ||
| 71 | +**Scenario 5: int8 input, scalar quantization, and ND int8_t output** | ||
| 72 | + | ||
| 73 | +- Input: A `[128, 128]` and B `[128, 256]`, both ND `int8_t` | ||
| 74 | +- Output: C `[128, 256]` in ND `int8_t` | ||
| 75 | +- Implementation: Configure scalar quantization with `asc_set_l0c_copy_prequant`, use `REQ8`, and enable NZ2ND | ||
| 76 | + | ||
| 77 | +**Scenario 6: int8 input, vector quantization, and Nz int8_t output** | ||
| 78 | + | ||
| 79 | +- Input: A `[128, 128]` and B `[128, 256]`, both ND `int8_t`; `[256] uint64_t` quantization parameters | ||
| 80 | +- Output: C `[128, 256]` in Nz `int8_t` | ||
| 81 | +- Implementation: Use `VREQ8` and enable ReLU | ||
| 82 | + | ||
| 83 | +**Scenario 7: half input, L0C-to-UB dual-destination M split, and ND float output** | ||
| 84 | + | ||
| 85 | +- Input: A `[128, 128]` and B `[128, 256]`, both ND `half` | ||
| 86 | +- Output: C `[128, 256]` in ND `float` | ||
| 87 | +- Implementation: Use `asc_copy_l0c2ub` with `DUAL_DST_SPLIT_M` and `NoQuant` to split the result across the two AIV UBs, then let each AIV write back its GM slice | ||
| 88 | + | ||
| 89 | +For scenarios 2, 4, and 6, the parameters are moved from GM to L1 Buffer with `asc_copy_gm2l1`, then from L1 Buffer to Fixpipe Buffer with `asc_copy_l12fb`. `asc_set_l0c_copy_config` configures the vector-quantization parameter address. Its parameter address is expressed in 128B units, so the parameter file must be rounded up to 128B and any padding must be zero-filled. The `[256] uint64_t` parameters in this example occupy 2048B and already meet this requirement. Moving these parameters from L1 Buffer to Fixpipe Buffer transfers 2048B of data in 64B data blocks, so `len_burst` in `asc_copy_l12fb` is set to 32. `asc_sync_pipe(PIPE_FIX)` completes the L1 Buffer-to-Fixpipe Buffer movement and parameter configuration before Fixpipe moves the L0C Buffer result to UB. | ||
| 90 | + | ||
| 91 | +## Build and Run | ||
| 92 | + | ||
| 93 | +Run the following steps in the example root directory. | ||
| 94 | + | ||
| 95 | +- Configure environment variables | ||
| 96 | + | ||
| 97 | + Configure the environment according to the CANN development kit [installation instructions](../../../../../docs/en/quick_start.md#prepare&install). | ||
| 98 | + | ||
| 99 | + ```bash | ||
| 100 | + source ${install_path}/cann/set_env.sh | ||
| 101 | + ``` | ||
| 102 | + | ||
| 103 | + > **Note:** `${install_path}` is the CANN package installation directory. The default is `/usr/local/Ascend` for the root user and `${HOME}/Ascend` for non-root users. | ||
| 104 | + | ||
| 105 | +- NPU execution | ||
| 106 | + | ||
| 107 | + ```bash | ||
| 108 | + SCENARIO_NUM=1 | ||
| 109 | + mkdir -p build && cd build; # Create and enter the build directory | ||
| 110 | + cmake -DSCENARIO_NUM=$SCENARIO_NUM -DCMAKE_ASC_ARCHITECTURES=dav-3510 ..;make -j; # Build the project; NPU mode by default | ||
| 111 | + python3 ../scripts/gen_data.py -scenarioNum=$SCENARIO_NUM # Generate test input and golden data | ||
| 112 | + ./demo # Run the example executable | ||
| 113 | + python3 ../scripts/verify_result.py -scenarioNum=$SCENARIO_NUM output/output.bin ./output/golden.bin # Verify the result | ||
| 114 | + ``` | ||
| 115 | + | ||
| 116 | + > **Note:** `gen_data.py` and `verify_result.py` must use the same `SCENARIO_NUM`. If `-scenarioNum` is omitted from the verification command, the script uses scenario 1 by default. Scenario 7 writes `float32` output, which would be read as `float16` and cause verification to fail. | ||
| 117 | + | ||
| 118 | +- NPU simulation | ||
| 119 | + | ||
| 120 | + ```bash | ||
| 121 | + cmake -DSCENARIO_NUM=$SCENARIO_NUM -DCMAKE_ASC_RUN_MODE=sim -DCMAKE_ASC_ARCHITECTURES=dav-3510 ..;make -j; # NPU simulation mode | ||
| 122 | + ``` | ||
| 123 | + | ||
| 124 | + Clear the CMake cache before switching run modes. For example, run `rm CMakeCache.txt` in the `build` directory before reconfiguring. | ||
| 125 | + | ||
| 126 | +- Build options | ||
| 127 | + | ||
| 128 | + | Option | Values | Description | | ||
| 129 | + |------|--------|------| | ||
| 130 | + | `CMAKE_ASC_RUN_MODE` | `npu` (default), `sim` | Run mode: NPU execution or NPU simulation | | ||
| 131 | + | `CMAKE_ASC_ARCHITECTURES` | `dav-3510` | NPU architecture for Ascend 950PR/Ascend 950DT | | ||
| 132 | + | `SCENARIO_NUM` | 1-7 | Scenario number | | ||
| 133 | + | ||
| 134 | +- Expected result | ||
| 135 | + | ||
| 136 | + Successful output-to-golden verification prints: | ||
| 137 | + | ||
| 138 | + ```bash | ||
| 139 | + test pass! | ||
| 140 | + ``` | ||
| @@ -0,0 +1,382 @@ | |||
| 1 | +/** | ||
| 2 | + * Copyright (c) 2026 Huawei Technologies Co., Ltd. | ||
| 3 | + * This program is free software, you can redistribute it and/or modify it under the terms and conditions of | ||
| 4 | + * CANN Open Software License Agreement Version 2.0 (the "License"). | ||
| 5 | + * Please refer to the License for details. You may not use this file except in compliance with the License. | ||
| 6 | + * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED, | ||
| 7 | + * INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE. | ||
| 8 | + * See LICENSE in the root of the software repository for the full text of the License. | ||
| 9 | + */ | ||
| 10 | + | ||
| 11 | +/* ! | ||
| 12 | + * \file data_copy_l0c2ub.asc | ||
| 13 | + * \brief Demonstrates L0C-to-UB matrix movement with inline quantization using the Ascend C C API. | ||
| 14 | + */ | ||
| 15 | + | ||
| 16 | +#include <cstddef> | ||
| 17 | +#include <cstdint> | ||
| 18 | +#include <cstdio> | ||
| 19 | + | ||
| 20 | +#include "acl/acl.h" | ||
| 21 | +#include "c_api/asc_simd.h" | ||
| 22 | +#include "data_utils.h" | ||
| 23 | + | ||
| 24 | +constexpr uint32_t CUBE_BLOCK = 16; | ||
| 25 | +constexpr uint32_t C0_BYTES = 32; | ||
| 26 | +constexpr uint32_t K_ROUND = 2; | ||
| 27 | +constexpr uint32_t FIXPIPE_QUANT_ADDR_GRANULARITY = 128; | ||
| 28 | +constexpr uint32_t FIXPIPE_L1_TO_FB_BURST_BYTES = 64; | ||
| 29 | + | ||
| 30 | +constexpr uint32_t M = 128; | ||
| 31 | +constexpr uint32_t K = 128; | ||
| 32 | +constexpr uint32_t N = 256; | ||
| 33 | +constexpr uint32_t K_CHUNK = K / K_ROUND; | ||
| 34 | + | ||
| 35 | +__aicore__ constexpr uint32_t ceil_division(uint32_t numerator, uint32_t denominator) | ||
用例有对商用包的版本有要求吗?可以看看ceil_div的接口在对应版本上是否可以替换? ![]() ![]() | |||
| 36 | +{ | ||
| 37 | + return (numerator + denominator - 1) / denominator; | ||
| 38 | +} | ||
| 39 | + | ||
| 40 | +__aicore__ constexpr uint32_t ceil_align(uint32_t numerator, uint32_t denominator) | ||
| 41 | +{ | ||
| 42 | + return ceil_division(numerator, denominator) * denominator; | ||
| 43 | +} | ||
| 44 | + | ||
| 45 | +constexpr bool IS_DUAL_DST_SCENARIO = SCENARIO_NUM == 7; | ||
| 46 | +constexpr bool IS_INT8_INPUT = SCENARIO_NUM == 1 || SCENARIO_NUM == 2 || SCENARIO_NUM == 5 || SCENARIO_NUM == 6; | ||
| 47 | +constexpr bool IS_VECTOR_QUANT = SCENARIO_NUM == 2 || SCENARIO_NUM == 4 || SCENARIO_NUM == 6; | ||
| 48 | +constexpr bool IS_NZ2ND_OUTPUT = SCENARIO_NUM == 1 || SCENARIO_NUM == 4 || SCENARIO_NUM == 5 || IS_DUAL_DST_SCENARIO; | ||
| 49 | +constexpr bool IS_RELU_ENABLED = SCENARIO_NUM == 1 || SCENARIO_NUM == 4 || SCENARIO_NUM == 6; | ||
| 50 | + | ||
| 51 | +constexpr uint32_t INT8_C0_ELEMENTS = C0_BYTES / sizeof(int8_t); | ||
| 52 | +constexpr uint32_t HALF_C0_ELEMENTS = C0_BYTES / sizeof(half); | ||
| 53 | +constexpr uint32_t A_SINGLE_SIZE = M * K_CHUNK; | ||
| 54 | +constexpr uint32_t B_SINGLE_SIZE = K_CHUNK * N; | ||
| 55 | +constexpr uint32_t A_L1_MAX_BYTES = ceil_align(M, CUBE_BLOCK) * ceil_align(K_CHUNK, HALF_C0_ELEMENTS) * sizeof(half); | ||
| 56 | +constexpr uint32_t B_L1_MAX_BYTES = | ||
| 57 | + ceil_align(K_CHUNK, HALF_C0_ELEMENTS) * ceil_align(N, HALF_C0_ELEMENTS) * sizeof(half); | ||
| 58 | +constexpr uint32_t C_L0_MAX_BYTES = ceil_align(M, CUBE_BLOCK) * ceil_align(N, CUBE_BLOCK) * sizeof(float); | ||
| 59 | +constexpr uint32_t QUANT_TRANSFER_BYTES = ceil_align(N * sizeof(uint64_t), FIXPIPE_QUANT_ADDR_GRANULARITY); | ||
| 60 | + | ||
| 61 | +// Keep A1, B1, and C1 in separate regions of the C API local-memory workspace. | ||
| 62 | +constexpr uint32_t A1_OFFSET_BYTES = 0; | ||
| 63 | +constexpr uint32_t B1_OFFSET_BYTES = 64 * 1024; | ||
| 64 | +constexpr uint32_t C1_OFFSET_BYTES = 128 * 1024; | ||
| 65 | +constexpr uint32_t CBUF_WORKSPACE_BYTES = C1_OFFSET_BYTES + QUANT_TRANSFER_BYTES; | ||
| 66 | + | ||
| 67 | +constexpr uint8_t L2_CACHE_NORMAL = 0; | ||
| 68 | +constexpr uint8_t UNIT_FLAG_DISABLE = 0; | ||
| 69 | +constexpr uint16_t MTE2_NZ_MATRIX_NUM = 1; | ||
| 70 | +constexpr uint16_t MTE2_NZ_N_STRIDE = 1; | ||
| 71 | +constexpr uint16_t MTE2_NZ_LOOP4_DST_STRIDE = 0; | ||
| 72 | +constexpr uint16_t A_MTE2_NZ_C0_STRIDE = | ||
| 73 | + static_cast<uint16_t>(IS_INT8_INPUT ? ceil_align(M, INT8_C0_ELEMENTS) : ceil_align(M, HALF_C0_ELEMENTS)); | ||
| 74 | +constexpr uint16_t B_MTE2_NZ_C0_STRIDE = static_cast<uint16_t>( | ||
| 75 | + IS_INT8_INPUT ? ceil_align(K_CHUNK, INT8_C0_ELEMENTS) : ceil_align(K_CHUNK, HALF_C0_ELEMENTS)); | ||
| 76 | +constexpr float SCALAR_QUANT_SCALE = 2.0F; | ||
| 77 | +constexpr uint16_t SCALAR_QUANT_OFFSET = 0; | ||
| 78 | +constexpr bool SCALAR_QUANT_HALF_IS_SIGNED = false; | ||
| 79 | +constexpr bool SCALAR_QUANT_INT8_IS_SIGNED = true; | ||
| 80 | +// For Nz output, loop_dst_stride is measured in output elements. The API | ||
| 81 | +// converts its 32B-block dstStride to this unit before issuing Fixpipe. | ||
| 82 | +constexpr uint32_t FIXPIPE_NZ_DST_STRIDE_HALF = M * C0_BYTES / sizeof(half); | ||
| 83 | +constexpr uint32_t FIXPIPE_NZ_DST_STRIDE_INT8 = M * C0_BYTES / sizeof(int8_t); | ||
| 84 | +constexpr uint32_t OUTPUT_ELEMENT_BYTES = | ||
| 85 | + IS_DUAL_DST_SCENARIO ? sizeof(float) : ((SCENARIO_NUM == 1 || SCENARIO_NUM == 2) ? sizeof(half) : sizeof(int8_t)); | ||
| 86 | +constexpr uint32_t OUTPUT_BYTES = M * N * OUTPUT_ELEMENT_BYTES; | ||
| 87 | +constexpr int8_t L0C2UB_SUB_BLOCK0 = 0; | ||
| 88 | +constexpr asc_dual_dst_mode L0C2UB_DUAL_DST_DISABLE = asc_dual_dst_mode::DUAL_DST_DISABLE; | ||
| 89 | +constexpr asc_dual_dst_mode L0C2UB_DUAL_DST_SPLIT_M = asc_dual_dst_mode::DUAL_DST_SPLIT_M; | ||
| 90 | +constexpr asc_unit_flag_mode L0C2UB_UNIT_FLAG_DISABLE = asc_unit_flag_mode::DISABLE; | ||
| 91 | +constexpr asc_relu_pre_mode L0C2UB_RELU_MODE = IS_RELU_ENABLED ? asc_relu_pre_mode::NORMAL : asc_relu_pre_mode::NONE; | ||
| 92 | +constexpr int64_t OUTPUT_READY_FLAG_ID = 10; | ||
| 93 | + | ||
| 94 | +__global__ __mix__(1, 2) void data_copy_l0c2ub( | ||
| 95 | + __gm__ uint8_t* a, __gm__ uint8_t* b, __gm__ void* c, __gm__ uint64_t* quant_pre) | ||
| 96 | +{ | ||
| 97 | + asc_init(); | ||
| 98 | + | ||
| 99 | + __cbuf__ uint8_t cbuf_workspace[CBUF_WORKSPACE_BYTES]; | ||
| 100 | + __ca__ uint8_t a_l0_workspace[A_L1_MAX_BYTES]; | ||
| 101 | + __cb__ uint8_t b_l0_workspace[B_L1_MAX_BYTES]; | ||
| 102 | + __cc__ uint8_t c_l0_workspace[C_L0_MAX_BYTES]; | ||
| 103 | + __ubuf__ uint8_t* output_ub = reinterpret_cast<__ubuf__ uint8_t*>(0); | ||
| 104 | + | ||
| 105 | + if ASC_IS_AIC { | ||
| 106 | + if constexpr (IS_INT8_INPUT) { | ||
| 107 | + __cbuf__ int8_t* a_l1 = reinterpret_cast<__cbuf__ int8_t*>(cbuf_workspace + A1_OFFSET_BYTES); | ||
| 108 | + __cbuf__ int8_t* b_l1 = reinterpret_cast<__cbuf__ int8_t*>(cbuf_workspace + B1_OFFSET_BYTES); | ||
| 109 | + __ca__ int8_t* a_l0 = reinterpret_cast<__ca__ int8_t*>(a_l0_workspace); | ||
| 110 | + __cb__ int8_t* b_l0 = reinterpret_cast<__cb__ int8_t*>(b_l0_workspace); | ||
| 111 | + __cc__ int32_t* c_l0 = reinterpret_cast<__cc__ int32_t*>(c_l0_workspace); | ||
| 112 | + | ||
| 113 | + for (uint32_t k_index = 0; k_index < K_ROUND; ++k_index) { | ||
| 114 | + asc_set_gm2l1_nz_para( | ||
| 115 | + MTE2_NZ_MATRIX_NUM, MTE2_NZ_N_STRIDE, A_MTE2_NZ_C0_STRIDE, MTE2_NZ_LOOP4_DST_STRIDE); | ||
| 116 | + asc_copy_gm2l1_nd2nz( | ||
| 117 | + a_l1, reinterpret_cast<__gm__ int8_t*>(a) + k_index * A_SINGLE_SIZE, K_CHUNK * sizeof(int8_t), | ||
| 118 | + L2_CACHE_NORMAL, M, K_CHUNK, 0, false); | ||
| 119 | + asc_set_gm2l1_nz_para( | ||
| 120 | + MTE2_NZ_MATRIX_NUM, MTE2_NZ_N_STRIDE, B_MTE2_NZ_C0_STRIDE, MTE2_NZ_LOOP4_DST_STRIDE); | ||
| 121 | + asc_copy_gm2l1_nd2nz( | ||
| 122 | + b_l1, reinterpret_cast<__gm__ int8_t*>(b) + k_index * B_SINGLE_SIZE, N * sizeof(int8_t), | ||
| 123 | + L2_CACHE_NORMAL, K_CHUNK, N, 0, false); | ||
| 124 | + | ||
| 125 | + asc_sync_notify(PIPE_MTE2, PIPE_MTE1, EVENT_ID0); | ||
| 126 | + asc_sync_wait(PIPE_MTE2, PIPE_MTE1, EVENT_ID0); | ||
| 127 | + asc_copy_l12l0a( | ||
| 128 | + a_l0, a_l1, 0, 0, ceil_division(M, CUBE_BLOCK), ceil_division(K_CHUNK, INT8_C0_ELEMENTS), | ||
| 129 | + ceil_division(M, CUBE_BLOCK), ceil_division(M, CUBE_BLOCK)); | ||
| 130 | + // Nz -> Zn. asc_copy_l12l0b_trans uses the 3510 instruction stride directly. | ||
| 131 | + const uint32_t dst_offset = ceil_division(N, INT8_C0_ELEMENTS) * 512 * 2; | ||
| 132 | + const uint32_t src_offset = 512 * 2; | ||
| 133 | + for (uint32_t i = 0; i < ceil_division(K_CHUNK, INT8_C0_ELEMENTS); ++i) { | ||
| 134 | + asc_copy_l12l0b_trans( | ||
| 135 | + b_l0 + i * dst_offset, b_l1 + i * src_offset, static_cast<uint16_t>(0), | ||
| 136 | + static_cast<uint8_t>(ceil_division(N, INT8_C0_ELEMENTS)), | ||
| 137 | + static_cast<uint16_t>(ceil_division(K_CHUNK, INT8_C0_ELEMENTS) * 2), static_cast<uint16_t>(1), | ||
| 138 | + static_cast<uint16_t>(0), static_cast<uint16_t>(0)); | ||
| 139 | + } | ||
| 140 | + asc_sync_notify(PIPE_MTE1, PIPE_M, EVENT_ID0); | ||
| 141 | + asc_sync_wait(PIPE_MTE1, PIPE_M, EVENT_ID0); | ||
| 142 | + asc_sync_notify(PIPE_MTE1, PIPE_MTE2, EVENT_ID0); | ||
| 143 | + asc_sync_wait(PIPE_MTE1, PIPE_MTE2, EVENT_ID0); | ||
| 144 | + asc_mmad( | ||
| 145 | + c_l0, a_l0, b_l0, M, K_CHUNK, ceil_align(N, CUBE_BLOCK), UNIT_FLAG_DISABLE, false, false, | ||
| 146 | + k_index == 0); | ||
| 147 | + asc_sync_notify(PIPE_M, PIPE_MTE1, EVENT_ID0); | ||
| 148 | + asc_sync_wait(PIPE_M, PIPE_MTE1, EVENT_ID0); | ||
| 149 | + } | ||
| 150 | + | ||
| 151 | + if constexpr (IS_VECTOR_QUANT) { | ||
| 152 | + __cbuf__ uint64_t* quant_l1 = reinterpret_cast<__cbuf__ uint64_t*>(cbuf_workspace + C1_OFFSET_BYTES); | ||
| 153 | + __fbuf__ uint64_t quant_fb[QUANT_TRANSFER_BYTES / sizeof(uint64_t)]; | ||
| 154 | + asc_copy_gm2l1( | ||
| 155 | + reinterpret_cast<__cbuf__ void*>(quant_l1), reinterpret_cast<__gm__ void*>(quant_pre), 1, | ||
| 156 | + QUANT_TRANSFER_BYTES / C0_BYTES, 0, 0, 0); | ||
| 157 | + asc_sync_notify(PIPE_MTE2, PIPE_FIX, EVENT_ID0); | ||
| 158 | + asc_sync_wait(PIPE_MTE2, PIPE_FIX, EVENT_ID0); | ||
| 159 | + asc_copy_l12fb( | ||
| 160 | + reinterpret_cast<__fbuf__ void*>(quant_fb), reinterpret_cast<__cbuf__ void*>(quant_l1), 1, | ||
| 161 | + QUANT_TRANSFER_BYTES / FIXPIPE_L1_TO_FB_BURST_BYTES, 0, 0); | ||
| 162 | + const uint64_t quant_fb_addr = | ||
| 163 | + static_cast<uint64_t>(reinterpret_cast<uintptr_t>(quant_fb)) / FIXPIPE_QUANT_ADDR_GRANULARITY; | ||
| 164 | + asc_set_l0c_copy_config(0, quant_fb_addr, false); | ||
| 165 | + asc_sync_pipe(PIPE_FIX); | ||
| 166 | + } | ||
| 167 | + | ||
| 168 | + asc_sync_notify(PIPE_M, PIPE_FIX, EVENT_ID0); | ||
| 169 | + asc_sync_wait(PIPE_M, PIPE_FIX, EVENT_ID0); | ||
| 170 | + if constexpr (IS_NZ2ND_OUTPUT) { | ||
| 171 | + asc_set_l0c_copy_nz_para(1, 0, 0); | ||
| 172 | + } | ||
| 173 | + | ||
| 174 | + if constexpr (SCENARIO_NUM == 1) { | ||
| 175 | + asc_set_l0c_copy_prequant(SCALAR_QUANT_SCALE, SCALAR_QUANT_OFFSET, SCALAR_QUANT_HALF_IS_SIGNED); | ||
| 176 | + asc_copy_l0c2ub( | ||
| 177 | + reinterpret_cast<__ubuf__ half*>(output_ub), c_l0, N, M, N, ceil_align(M, CUBE_BLOCK), | ||
| 178 | + L0C2UB_SUB_BLOCK0, L0C2UB_DUAL_DST_DISABLE, L0C2UB_UNIT_FLAG_DISABLE, QuantMode_t::DEQF16, | ||
| 179 | + L0C2UB_RELU_MODE, false, true, false, false); | ||
| 180 | + } else if constexpr (SCENARIO_NUM == 2) { | ||
| 181 | + asc_copy_l0c2ub( | ||
| 182 | + reinterpret_cast<__ubuf__ half*>(output_ub), c_l0, ceil_align(N, CUBE_BLOCK), M, | ||
| 183 | + FIXPIPE_NZ_DST_STRIDE_HALF, ceil_align(M, CUBE_BLOCK), L0C2UB_SUB_BLOCK0, L0C2UB_DUAL_DST_DISABLE, | ||
| 184 | + L0C2UB_UNIT_FLAG_DISABLE, QuantMode_t::VDEQF16, L0C2UB_RELU_MODE, false, false, false, false); | ||
| 185 | + } else if constexpr (SCENARIO_NUM == 5) { | ||
| 186 | + asc_set_l0c_copy_prequant(SCALAR_QUANT_SCALE, SCALAR_QUANT_OFFSET, SCALAR_QUANT_INT8_IS_SIGNED); | ||
| 187 | + asc_copy_l0c2ub( | ||
| 188 | + reinterpret_cast<__ubuf__ int8_t*>(output_ub), c_l0, N, M, N, ceil_align(M, CUBE_BLOCK), | ||
| 189 | + L0C2UB_SUB_BLOCK0, L0C2UB_DUAL_DST_DISABLE, L0C2UB_UNIT_FLAG_DISABLE, QuantMode_t::REQ8, | ||
| 190 | + L0C2UB_RELU_MODE, false, true, false, false); | ||
| 191 | + } else { | ||
| 192 | + asc_copy_l0c2ub( | ||
| 193 | + reinterpret_cast<__ubuf__ int8_t*>(output_ub), c_l0, ceil_align(N, CUBE_BLOCK), M, | ||
| 194 | + FIXPIPE_NZ_DST_STRIDE_INT8, ceil_align(M, CUBE_BLOCK), L0C2UB_SUB_BLOCK0, L0C2UB_DUAL_DST_DISABLE, | ||
| 195 | + L0C2UB_UNIT_FLAG_DISABLE, QuantMode_t::VREQ8, L0C2UB_RELU_MODE, false, false, false, false); | ||
| 196 | + } | ||
| 197 | + } else { | ||
| 198 | + __cbuf__ half* a_l1 = reinterpret_cast<__cbuf__ half*>(cbuf_workspace + A1_OFFSET_BYTES); | ||
| 199 | + __cbuf__ half* b_l1 = reinterpret_cast<__cbuf__ half*>(cbuf_workspace + B1_OFFSET_BYTES); | ||
| 200 | + __ca__ half* a_l0 = reinterpret_cast<__ca__ half*>(a_l0_workspace); | ||
| 201 | + __cb__ half* b_l0 = reinterpret_cast<__cb__ half*>(b_l0_workspace); | ||
| 202 | + __cc__ float* c_l0 = reinterpret_cast<__cc__ float*>(c_l0_workspace); | ||
| 203 | + | ||
| 204 | + for (uint32_t k_index = 0; k_index < K_ROUND; ++k_index) { | ||
| 205 | + asc_set_gm2l1_nz_para( | ||
| 206 | + MTE2_NZ_MATRIX_NUM, MTE2_NZ_N_STRIDE, A_MTE2_NZ_C0_STRIDE, MTE2_NZ_LOOP4_DST_STRIDE); | ||
| 207 | + asc_copy_gm2l1_nd2nz( | ||
| 208 | + a_l1, reinterpret_cast<__gm__ half*>(a) + k_index * A_SINGLE_SIZE, K_CHUNK * sizeof(half), | ||
| 209 | + L2_CACHE_NORMAL, M, K_CHUNK, 0, false); | ||
| 210 | + asc_set_gm2l1_nz_para( | ||
| 211 | + MTE2_NZ_MATRIX_NUM, MTE2_NZ_N_STRIDE, B_MTE2_NZ_C0_STRIDE, MTE2_NZ_LOOP4_DST_STRIDE); | ||
| 212 | + asc_copy_gm2l1_nd2nz( | ||
| 213 | + b_l1, reinterpret_cast<__gm__ half*>(b) + k_index * B_SINGLE_SIZE, N * sizeof(half), | ||
| 214 | + L2_CACHE_NORMAL, K_CHUNK, N, 0, false); | ||
| 215 | + | ||
| 216 | + asc_sync_notify(PIPE_MTE2, PIPE_MTE1, EVENT_ID0); | ||
| 217 | + asc_sync_wait(PIPE_MTE2, PIPE_MTE1, EVENT_ID0); | ||
| 218 | + asc_copy_l12l0a( | ||
| 219 | + a_l0, a_l1, 0, 0, ceil_division(M, CUBE_BLOCK), ceil_division(K_CHUNK, HALF_C0_ELEMENTS), | ||
| 220 | + ceil_division(M, CUBE_BLOCK), ceil_division(M, CUBE_BLOCK)); | ||
| 221 | + // Nz -> Zn. half uses one 256-element cube block per transpose iteration. | ||
| 222 | + const uint32_t dst_offset = ceil_division(N, HALF_C0_ELEMENTS) * 256; | ||
| 223 | + const uint32_t src_offset = 256; | ||
| 224 | + for (uint32_t i = 0; i < ceil_division(K_CHUNK, HALF_C0_ELEMENTS); ++i) { | ||
| 225 | + asc_copy_l12l0b_trans( | ||
| 226 | + b_l0 + i * dst_offset, b_l1 + i * src_offset, static_cast<uint16_t>(0), | ||
| 227 | + static_cast<uint8_t>(ceil_division(N, HALF_C0_ELEMENTS)), | ||
| 228 | + static_cast<uint16_t>(ceil_division(K_CHUNK, HALF_C0_ELEMENTS)), static_cast<uint16_t>(0), | ||
| 229 | + static_cast<uint16_t>(0), static_cast<uint16_t>(0)); | ||
| 230 | + } | ||
| 231 | + asc_sync_notify(PIPE_MTE1, PIPE_M, EVENT_ID0); | ||
| 232 | + asc_sync_wait(PIPE_MTE1, PIPE_M, EVENT_ID0); | ||
| 233 | + asc_sync_notify(PIPE_MTE1, PIPE_MTE2, EVENT_ID0); | ||
| 234 | + asc_sync_wait(PIPE_MTE1, PIPE_MTE2, EVENT_ID0); | ||
| 235 | + asc_mmad( | ||
| 236 | + c_l0, a_l0, b_l0, M, K_CHUNK, ceil_align(N, CUBE_BLOCK), UNIT_FLAG_DISABLE, false, false, | ||
| 237 | + k_index == 0); | ||
| 238 | + asc_sync_notify(PIPE_M, PIPE_MTE1, EVENT_ID0); | ||
| 239 | + asc_sync_wait(PIPE_M, PIPE_MTE1, EVENT_ID0); | ||
| 240 | + } | ||
| 241 | + | ||
| 242 | + if constexpr (IS_VECTOR_QUANT) { | ||
| 243 | + __cbuf__ uint64_t* quant_l1 = reinterpret_cast<__cbuf__ uint64_t*>(cbuf_workspace + C1_OFFSET_BYTES); | ||
| 244 | + __fbuf__ uint64_t quant_fb[QUANT_TRANSFER_BYTES / sizeof(uint64_t)]; | ||
| 245 | + asc_copy_gm2l1( | ||
| 246 | + reinterpret_cast<__cbuf__ void*>(quant_l1), reinterpret_cast<__gm__ void*>(quant_pre), 1, | ||
| 247 | + QUANT_TRANSFER_BYTES / C0_BYTES, 0, 0, 0); | ||
| 248 | + asc_sync_notify(PIPE_MTE2, PIPE_FIX, EVENT_ID0); | ||
| 249 | + asc_sync_wait(PIPE_MTE2, PIPE_FIX, EVENT_ID0); | ||
| 250 | + asc_copy_l12fb( | ||
| 251 | + reinterpret_cast<__fbuf__ void*>(quant_fb), reinterpret_cast<__cbuf__ void*>(quant_l1), 1, | ||
| 252 | + QUANT_TRANSFER_BYTES / FIXPIPE_L1_TO_FB_BURST_BYTES, 0, 0); | ||
| 253 | + const uint64_t quant_fb_addr = | ||
| 254 | + static_cast<uint64_t>(reinterpret_cast<uintptr_t>(quant_fb)) / FIXPIPE_QUANT_ADDR_GRANULARITY; | ||
| 255 | + asc_set_l0c_copy_config(0, quant_fb_addr, false); | ||
| 256 | + asc_sync_pipe(PIPE_FIX); | ||
| 257 | + } | ||
| 258 | + | ||
| 259 | + asc_sync_notify(PIPE_M, PIPE_FIX, EVENT_ID0); | ||
| 260 | + asc_sync_wait(PIPE_M, PIPE_FIX, EVENT_ID0); | ||
| 261 | + if constexpr (IS_NZ2ND_OUTPUT) { | ||
| 262 | + asc_set_l0c_copy_nz_para(1, 0, 0); | ||
| 263 | + } | ||
| 264 | + | ||
| 265 | + if constexpr (IS_DUAL_DST_SCENARIO) { | ||
| 266 | + asc_copy_l0c2ub( | ||
| 267 | + reinterpret_cast<__ubuf__ float*>(output_ub), c_l0, N, M, N, ceil_align(M, CUBE_BLOCK), | ||
| 268 | + L0C2UB_SUB_BLOCK0, L0C2UB_DUAL_DST_SPLIT_M, L0C2UB_UNIT_FLAG_DISABLE, QuantMode_t::NoQuant, | ||
| 269 | + L0C2UB_RELU_MODE, false, true, false, false); | ||
| 270 | + } else if constexpr (SCENARIO_NUM == 3) { | ||
| 271 | + asc_set_l0c_copy_prequant(SCALAR_QUANT_SCALE, SCALAR_QUANT_OFFSET, SCALAR_QUANT_INT8_IS_SIGNED); | ||
| 272 | + asc_copy_l0c2ub( | ||
| 273 | + reinterpret_cast<__ubuf__ int8_t*>(output_ub), c_l0, ceil_align(N, CUBE_BLOCK), M, | ||
| 274 | + FIXPIPE_NZ_DST_STRIDE_INT8, ceil_align(M, CUBE_BLOCK), L0C2UB_SUB_BLOCK0, L0C2UB_DUAL_DST_DISABLE, | ||
| 275 | + L0C2UB_UNIT_FLAG_DISABLE, QuantMode_t::QF322B8_PRE, L0C2UB_RELU_MODE, false, false, false, false); | ||
| 276 | + } else { | ||
| 277 | + asc_copy_l0c2ub( | ||
| 278 | + reinterpret_cast<__ubuf__ int8_t*>(output_ub), c_l0, N, M, N, ceil_align(M, CUBE_BLOCK), | ||
| 279 | + L0C2UB_SUB_BLOCK0, L0C2UB_DUAL_DST_DISABLE, L0C2UB_UNIT_FLAG_DISABLE, QuantMode_t::VQF322B8_PRE, | ||
| 280 | + L0C2UB_RELU_MODE, false, true, false, false); | ||
| 281 | + } | ||
| 282 | + } | ||
| 283 | + asc_sync_block_arrive(PIPE_FIX, OUTPUT_READY_FLAG_ID); | ||
| 284 | + } | ||
| 285 | + if ASC_IS_AIV { | ||
| 286 | + asc_sync_block_wait(PIPE_MTE3, OUTPUT_READY_FLAG_ID); | ||
| 287 | + if constexpr (IS_DUAL_DST_SCENARIO) { | ||
| 288 | + constexpr uint32_t DUAL_DST_OUTPUT_BYTES = OUTPUT_BYTES / 2; | ||
| 289 | + const uint32_t sub_block_id = static_cast<uint32_t>(asc_get_sub_block_id()); | ||
| 290 | + asc_copy_ub2gm_align( | ||
| 291 | + reinterpret_cast<__gm__ uint8_t*>(c) + sub_block_id * DUAL_DST_OUTPUT_BYTES, | ||
| 292 | + reinterpret_cast<__ubuf__ uint8_t*>(output_ub), DUAL_DST_OUTPUT_BYTES); | ||
| 293 | + } else { | ||
| 294 | + if (asc_get_sub_block_id() != 0) { | ||
| 295 | + return; | ||
| 296 | + } | ||
| 297 | + asc_copy_ub2gm_align( | ||
| 298 | + reinterpret_cast<__gm__ uint8_t*>(c), reinterpret_cast<__ubuf__ uint8_t*>(output_ub), OUTPUT_BYTES); | ||
| 299 | + } | ||
| 300 | + } | ||
| 301 | + asc_sync_pipe(PIPE_ALL); | ||
| 302 | +} | ||
| 303 | + | ||
| 304 | +#define CHECK_ACL(expr) \ | ||
| 305 | + do { \ | ||
| 306 | + auto __ret = (expr); \ | ||
| 307 | + int32_t __code = static_cast<int32_t>(__ret); \ | ||
| 308 | + if (__code != 0) { \ | ||
| 309 | + fprintf(stderr, "[ERROR] %s failed at %s:%d, ret=%d\n", #expr, __FILE__, __LINE__, __code); \ | ||
| 310 | + } \ | ||
| 311 | + } while (0) | ||
| 312 | + | ||
| 313 | +int32_t main(int32_t argc, char* argv[]) | ||
| 314 | +{ | ||
| 315 | + (void)argc; | ||
| 316 | + (void)argv; | ||
| 317 | + | ||
| 318 | + constexpr size_t input_element_size = IS_INT8_INPUT ? sizeof(int8_t) : sizeof(half); | ||
| 319 | + constexpr size_t output_element_size = | ||
| 320 | + IS_DUAL_DST_SCENARIO ? sizeof(float) : | ||
| 321 | + ((SCENARIO_NUM == 1 || SCENARIO_NUM == 2) ? sizeof(half) : sizeof(int8_t)); | ||
| 322 | + constexpr size_t a_file_size = M * K * input_element_size; | ||
| 323 | + constexpr size_t b_file_size = K * N * input_element_size; | ||
| 324 | + constexpr size_t c_file_size = M * N * output_element_size; | ||
| 325 | + constexpr int32_t device_id = 0; | ||
| 326 | + | ||
| 327 | + uint8_t* a_host = nullptr; | ||
| 328 | + uint8_t* b_host = nullptr; | ||
| 329 | + uint8_t* c_host = nullptr; | ||
| 330 | + uint8_t* a_device = nullptr; | ||
| 331 | + uint8_t* b_device = nullptr; | ||
| 332 | + void* c_device = nullptr; | ||
| 333 | + uint8_t* quant_host = nullptr; | ||
| 334 | + uint64_t* quant_device = nullptr; | ||
| 335 | + aclrtStream stream = nullptr; | ||
| 336 | + | ||
| 337 | + CHECK_ACL(aclInit(nullptr)); | ||
| 338 | + CHECK_ACL(aclrtSetDevice(device_id)); | ||
| 339 | + CHECK_ACL(aclrtCreateStream(&stream)); | ||
| 340 | + | ||
| 341 | + CHECK_ACL(aclrtMallocHost(reinterpret_cast<void**>(&a_host), a_file_size)); | ||
| 342 | + CHECK_ACL(aclrtMalloc(reinterpret_cast<void**>(&a_device), a_file_size, ACL_MEM_MALLOC_HUGE_FIRST)); | ||
| 343 | + ReadFile("./input/x1_gm.bin", a_host, a_file_size); | ||
| 344 | + CHECK_ACL(aclrtMemcpy(a_device, a_file_size, a_host, a_file_size, ACL_MEMCPY_HOST_TO_DEVICE)); | ||
| 345 | + | ||
| 346 | + CHECK_ACL(aclrtMallocHost(reinterpret_cast<void**>(&b_host), b_file_size)); | ||
| 347 | + CHECK_ACL(aclrtMalloc(reinterpret_cast<void**>(&b_device), b_file_size, ACL_MEM_MALLOC_HUGE_FIRST)); | ||
| 348 | + ReadFile("./input/x2_gm.bin", b_host, b_file_size); | ||
| 349 | + CHECK_ACL(aclrtMemcpy(b_device, b_file_size, b_host, b_file_size, ACL_MEMCPY_HOST_TO_DEVICE)); | ||
| 350 | + | ||
| 351 | + CHECK_ACL(aclrtMallocHost(reinterpret_cast<void**>(&c_host), c_file_size)); | ||
| 352 | + CHECK_ACL(aclrtMalloc(&c_device, c_file_size, ACL_MEM_MALLOC_HUGE_FIRST)); | ||
| 353 | + | ||
| 354 | + if constexpr (IS_VECTOR_QUANT) { | ||
| 355 | + CHECK_ACL(aclrtMallocHost(reinterpret_cast<void**>(&quant_host), QUANT_TRANSFER_BYTES)); | ||
| 356 | + CHECK_ACL( | ||
| 357 | + aclrtMalloc(reinterpret_cast<void**>(&quant_device), QUANT_TRANSFER_BYTES, ACL_MEM_MALLOC_HUGE_FIRST)); | ||
| 358 | + ReadFile("./input/quant_pre.bin", quant_host, QUANT_TRANSFER_BYTES); | ||
| 359 | + CHECK_ACL(aclrtMemcpy( | ||
| 360 | + quant_device, QUANT_TRANSFER_BYTES, quant_host, QUANT_TRANSFER_BYTES, ACL_MEMCPY_HOST_TO_DEVICE)); | ||
| 361 | + } | ||
| 362 | + | ||
| 363 | + data_copy_l0c2ub<<<1, 0, stream>>>(a_device, b_device, c_device, quant_device); | ||
| 364 | + CHECK_ACL(aclrtSynchronizeStream(stream)); | ||
| 365 | + CHECK_ACL(aclrtMemcpy(c_host, c_file_size, c_device, c_file_size, ACL_MEMCPY_DEVICE_TO_HOST)); | ||
| 366 | + WriteFile("./output/output.bin", c_host, c_file_size); | ||
| 367 | + | ||
| 368 | + if constexpr (IS_VECTOR_QUANT) { | ||
| 369 | + CHECK_ACL(aclrtFree(quant_device)); | ||
| 370 | + CHECK_ACL(aclrtFreeHost(quant_host)); | ||
| 371 | + } | ||
| 372 | + CHECK_ACL(aclrtFree(c_device)); | ||
| 373 | + CHECK_ACL(aclrtFreeHost(c_host)); | ||
| 374 | + CHECK_ACL(aclrtFree(b_device)); | ||
| 375 | + CHECK_ACL(aclrtFreeHost(b_host)); | ||
| 376 | + CHECK_ACL(aclrtFree(a_device)); | ||
| 377 | + CHECK_ACL(aclrtFreeHost(a_host)); | ||
| 378 | + CHECK_ACL(aclrtDestroyStream(stream)); | ||
| 379 | + CHECK_ACL(aclrtResetDevice(device_id)); | ||
| 380 | + CHECK_ACL(aclFinalize()); | ||
| 381 | + return 0; | ||
| 382 | +} | ||
| @@ -0,0 +1,119 @@ | |||
| 1 | +/** | ||
| 2 | + * Copyright (c) 2026 Huawei Technologies Co., Ltd. | ||
| 3 | + * This program is free software, you can redistribute it and/or modify it under the terms and conditions of | ||
| 4 | + * CANN Open Software License Agreement Version 2.0 (the "License"). | ||
| 5 | + * Please refer to the License for details. You may not use this file except in compliance with the License. | ||
| 6 | + * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED, | ||
| 7 | + * INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE. | ||
| 8 | + * See LICENSE in the root of the software repository for the full text of the License. | ||
| 9 | + */ | ||
| 10 | + | ||
| 11 | +/* ! | ||
| 12 | + * \file data_utils.h | ||
| 13 | + * \brief File read/write helpers for the host program. | ||
| 14 | + */ | ||
| 15 | + | ||
| 16 | +#ifndef DATA_UTILS_H | ||
| 17 | +#define DATA_UTILS_H | ||
| 18 | + | ||
| 19 | +#include <fcntl.h> | ||
| 20 | +#include <sys/stat.h> | ||
| 21 | +#include <unistd.h> | ||
| 22 | + | ||
| 23 | +#include <cerrno> | ||
| 24 | +#include <cstdint> | ||
| 25 | +#include <cstdio> | ||
| 26 | +#include <fstream> | ||
| 27 | +#include <limits> | ||
| 28 | + | ||
| 29 | +#define ERROR_LOG(fmt, args...) fprintf(stdout, "[ERROR] " fmt "\n", ##args) | ||
| 30 | + | ||
| 31 | +bool ReadFile(const std::string& file_path, void* buffer, size_t expected_size) | ||
| 32 | +{ | ||
| 33 | + if (buffer == nullptr || expected_size == 0) { | ||
| 34 | + ERROR_LOG("Read file failed. buffer is nullptr or expected size is 0"); | ||
| 35 | + return false; | ||
| 36 | + } | ||
| 37 | + | ||
| 38 | + struct stat stat_buffer; | ||
| 39 | + if (stat(file_path.data(), &stat_buffer) == -1) { | ||
| 40 | + ERROR_LOG("failed to get file"); | ||
| 41 | + return false; | ||
| 42 | + } | ||
| 43 | + if (S_ISREG(stat_buffer.st_mode) == 0) { | ||
| 44 | + ERROR_LOG("%s is not a file, please enter a file", file_path.c_str()); | ||
| 45 | + return false; | ||
| 46 | + } | ||
| 47 | + if (stat_buffer.st_size <= 0 || | ||
| 48 | + static_cast<uintmax_t>(stat_buffer.st_size) != static_cast<uintmax_t>(expected_size)) { | ||
| 49 | + ERROR_LOG( | ||
| 50 | + "file size does not match expected size. expected = %zu, actual = %lld", expected_size, | ||
| 51 | + static_cast<long long>(stat_buffer.st_size)); | ||
| 52 | + return false; | ||
| 53 | + } | ||
| 54 | + if (expected_size > static_cast<size_t>(std::numeric_limits<std::streamsize>::max())) { | ||
| 55 | + ERROR_LOG("file size exceeds stream size limit. size = %zu", expected_size); | ||
| 56 | + return false; | ||
| 57 | + } | ||
| 58 | + | ||
| 59 | + std::ifstream file(file_path, std::ios::binary); | ||
| 60 | + if (!file.is_open()) { | ||
| 61 | + ERROR_LOG("Open file failed. path = %s", file_path.c_str()); | ||
| 62 | + return false; | ||
| 63 | + } | ||
| 64 | + file.read(static_cast<char*>(buffer), static_cast<std::streamsize>(expected_size)); | ||
| 65 | + if (file.gcount() != static_cast<std::streamsize>(expected_size)) { | ||
| 66 | + ERROR_LOG( | ||
| 67 | + "Read file failed. expected = %zu, actual = %lld", expected_size, static_cast<long long>(file.gcount())); | ||
| 68 | + file.close(); | ||
| 69 | + return false; | ||
| 70 | + } | ||
| 71 | + file.close(); | ||
| 72 | + if (file.fail()) { | ||
| 73 | + ERROR_LOG("Close file failed. path = %s", file_path.c_str()); | ||
| 74 | + return false; | ||
| 75 | + } | ||
| 76 | + return true; | ||
| 77 | +} | ||
| 78 | + | ||
| 79 | +bool WriteFile(const std::string& file_path, const void* buffer, size_t size) | ||
| 80 | +{ | ||
| 81 | + if (buffer == nullptr) { | ||
| 82 | + ERROR_LOG("Write file failed. buffer is nullptr"); | ||
| 83 | + return false; | ||
| 84 | + } | ||
| 85 | + | ||
| 86 | + int fd = open(file_path.c_str(), O_WRONLY | O_CREAT | O_TRUNC, S_IRUSR | S_IWRITE); | ||
| 87 | + if (fd < 0) { | ||
| 88 | + ERROR_LOG("Open file failed. path = %s", file_path.c_str()); | ||
| 89 | + return false; | ||
| 90 | + } | ||
| 91 | + | ||
| 92 | + size_t written_size = 0; | ||
| 93 | + const char* src = static_cast<const char*>(buffer); | ||
| 94 | + while (written_size < size) { | ||
| 95 | + ssize_t write_size = write(fd, src + written_size, size - written_size); | ||
| 96 | + if (write_size < 0) { | ||
| 97 | + if (errno == EINTR) { | ||
| 98 | + continue; | ||
| 99 | + } | ||
| 100 | + ERROR_LOG("Write file failed. path = %s", file_path.c_str()); | ||
| 101 | + (void)close(fd); | ||
| 102 | + return false; | ||
| 103 | + } | ||
| 104 | + if (write_size == 0) { | ||
| 105 | + ERROR_LOG("Write file failed. no data was written"); | ||
| 106 | + (void)close(fd); | ||
| 107 | + return false; | ||
| 108 | + } | ||
| 109 | + written_size += static_cast<size_t>(write_size); | ||
| 110 | + } | ||
| 111 | + | ||
| 112 | + if (close(fd) != 0) { | ||
| 113 | + ERROR_LOG("Close file failed. path = %s", file_path.c_str()); | ||
| 114 | + return false; | ||
| 115 | + } | ||
| 116 | + return true; | ||
| 117 | +} | ||
| 118 | + | ||
| 119 | +#endif // DATA_UTILS_H | ||
| @@ -0,0 +1,191 @@ | |||
| 1 | +#!/usr/bin/env python3 | ||
| 2 | +# -*- coding: utf-8 -*- | ||
| 3 | +# ---------------------------------------------------------------------------------------------------------- | ||
| 4 | +# Copyright (c) 2026 Huawei Technologies Co., Ltd. | ||
| 5 | +# This program is free software, you can redistribute it and/or modify it under the terms and conditions of | ||
| 6 | +# CANN Open Software License Agreement Version 2.0 (the "License"). | ||
| 7 | +# Please refer to the License for details. You may not use this file except in compliance with the License. | ||
| 8 | +# THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED, | ||
| 9 | +# INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE. | ||
| 10 | +# See LICENSE in the root of the software repository for the full text of the License. | ||
| 11 | +# ---------------------------------------------------------------------------------------------------------- | ||
| 12 | + | ||
| 13 | +import argparse | ||
| 14 | +import os | ||
| 15 | +import struct | ||
| 16 | + | ||
| 17 | +import numpy as np | ||
| 18 | + | ||
| 19 | + | ||
| 20 | +M = 128 | ||
| 21 | +K = 128 | ||
| 22 | +N = 256 | ||
| 23 | +K_ROUND = 2 | ||
| 24 | +VECTOR_QUANT_SCENARIOS = (2, 4, 6) | ||
| 25 | +NZ_OUTPUT_SCENARIOS = (2, 3, 6) | ||
| 26 | +RELU_SCENARIOS = (1, 4, 6) | ||
| 27 | + | ||
| 28 | + | ||
| 29 | +def decode_quant_pre(quant_pre): | ||
| 30 | + quant_pre = int(quant_pre) | ||
| 31 | + alpha_bits = (quant_pre >> 13) & 0xFFFFF | ||
| 32 | + sign_bit = (alpha_bits >> 18) & 0x1 | ||
| 33 | + exponent = (alpha_bits >> 10) & 0xFF | ||
| 34 | + mantissa = alpha_bits & 0x3FF | ||
| 35 | + alpha = (-1) ** sign_bit * (1 + mantissa / 1024) * (2 ** (exponent - 127)) | ||
| 36 | + offset = (quant_pre >> 37) & 0x1FF | ||
| 37 | + sign = (quant_pre >> 46) & 0x1 | ||
| 38 | + shift = (quant_pre >> 32) & 0xF | ||
| 39 | + mode_control = (quant_pre >> 36) & 0x1 | ||
| 40 | + return alpha, offset, sign, shift, mode_control | ||
| 41 | + | ||
| 42 | + | ||
| 43 | +def decode_relu_pre(relu_pre): | ||
| 44 | + alpha_bits = (int(relu_pre) >> 13) & 0xFFFFF | ||
| 45 | + sign_bit = (alpha_bits >> 18) & 0x1 | ||
| 46 | + exponent = (alpha_bits >> 10) & 0xFF | ||
| 47 | + mantissa = alpha_bits & 0x3FF | ||
| 48 | + return (-1) ** sign_bit * (1 + mantissa / 1024) * (2 ** (exponent - 127)) | ||
| 49 | + | ||
| 50 | + | ||
| 51 | +def saturate(value, min_value, max_value, dtype): | ||
| 52 | + return np.round(np.clip(value, min_value, max_value)).astype(dtype) | ||
| 53 | + | ||
| 54 | + | ||
| 55 | +def snap_near_half_integer(value, epsilon=1e-5): | ||
| 56 | + absolute_value = abs(value) | ||
| 57 | + fractional_part = absolute_value - np.floor(absolute_value) | ||
| 58 | + if 0.5 - epsilon < fractional_part < 0.5: | ||
| 59 | + return np.sign(value) * (np.floor(absolute_value) + np.float32(0.5)) | ||
| 60 | + return value | ||
| 61 | + | ||
| 62 | + | ||
| 63 | +def apply_deqf16(value, quant_pre, relu_pre): | ||
| 64 | + alpha, offset, _, shift, mode_control = decode_quant_pre(quant_pre) | ||
| 65 | + if mode_control: | ||
| 66 | + value = saturate( | ||
| 67 | + value >> shift, | ||
| 68 | + np.finfo(np.float16).min, | ||
| 69 | + np.finfo(np.float16).max, | ||
| 70 | + np.float16, | ||
| 71 | + ) | ||
| 72 | + value = np.float32(value) | ||
| 73 | + value *= alpha if value >= 0 else decode_relu_pre(relu_pre) | ||
| 74 | + value = ( | ||
| 75 | + saturate( | ||
| 76 | + value, | ||
| 77 | + np.finfo(np.float16).min, | ||
| 78 | + np.finfo(np.float16).max, | ||
| 79 | + np.float16, | ||
| 80 | + ) | ||
| 81 | + + offset | ||
| 82 | + ) | ||
| 83 | + return saturate( | ||
| 84 | + value, | ||
| 85 | + np.finfo(np.float16).min, | ||
| 86 | + np.finfo(np.float16).max, | ||
| 87 | + np.float16, | ||
| 88 | + ) | ||
| 89 | + | ||
| 90 | + | ||
| 91 | +def apply_qf322b8(value, quant_pre, relu_pre): | ||
| 92 | + alpha, offset, sign, shift, mode_control = decode_quant_pre(quant_pre) | ||
| 93 | + if mode_control: | ||
| 94 | + value = saturate(value / (2**shift), -32768, 32767, np.int16) | ||
| 95 | + value = np.float32(value) | ||
| 96 | + value *= alpha if value >= 0 else decode_relu_pre(relu_pre) | ||
| 97 | + value = snap_near_half_integer(value) | ||
| 98 | + value = saturate(value, -256, 255, np.int16) + offset | ||
| 99 | + return ( | ||
| 100 | + saturate(value, -128, 127, np.int8) | ||
| 101 | + if sign | ||
| 102 | + else saturate(value, 0, 255, np.uint8) | ||
| 103 | + ) | ||
| 104 | + | ||
| 105 | + | ||
| 106 | +def apply_req8(value, quant_pre, relu_pre): | ||
| 107 | + alpha, offset, sign, shift, mode_control = decode_quant_pre(quant_pre) | ||
| 108 | + if mode_control: | ||
| 109 | + value = saturate(value >> shift, -32768, 32767, np.int16) | ||
| 110 | + value = np.float32(value) | ||
| 111 | + value *= alpha if value >= 0 else decode_relu_pre(relu_pre) | ||
| 112 | + value = snap_near_half_integer(value) | ||
| 113 | + value = saturate(value, -256, 255, np.int16) + offset | ||
| 114 | + return ( | ||
| 115 | + saturate(value, -128, 127, np.int8) | ||
| 116 | + if sign | ||
| 117 | + else saturate(value, 0, 255, np.uint8) | ||
| 118 | + ) | ||
| 119 | + | ||
| 120 | + | ||
| 121 | +def make_quant_params(): | ||
| 122 | + alpha_bits = struct.unpack("!I", struct.pack("!f", 2.0))[0] | ||
| 123 | + return np.full(N, np.uint64(alpha_bits | (1 << 46)), dtype=np.uint64) | ||
| 124 | + | ||
| 125 | + | ||
| 126 | +def apply_prequant(matrix, scenario_num, quant_params): | ||
| 127 | + relu_alpha = 0.0 if scenario_num in RELU_SCENARIOS else 2.0 | ||
| 128 | + relu_pre = struct.unpack("!I", struct.pack("!f", relu_alpha))[0] | ||
| 129 | + result_dtype = np.float16 if scenario_num in (1, 2) else np.int8 | ||
| 130 | + result = np.zeros((M, N), dtype=result_dtype) | ||
| 131 | + for row in range(M): | ||
| 132 | + for column in range(N): | ||
| 133 | + if scenario_num in (1, 2): | ||
| 134 | + result[row, column] = apply_deqf16( | ||
| 135 | + matrix[row, column], quant_params[column], relu_pre | ||
| 136 | + ) | ||
| 137 | + elif scenario_num in (3, 4): | ||
| 138 | + result[row, column] = apply_qf322b8( | ||
| 139 | + matrix[row, column], quant_params[column], relu_pre | ||
| 140 | + ) | ||
| 141 | + else: | ||
| 142 | + result[row, column] = apply_req8( | ||
| 143 | + matrix[row, column], quant_params[column], relu_pre | ||
| 144 | + ) | ||
| 145 | + return result | ||
| 146 | + | ||
| 147 | + | ||
| 148 | +def gen_golden_data(scenario_num): | ||
| 149 | + np.random.seed(9) | ||
| 150 | + if scenario_num in (1, 2, 5, 6): | ||
| 151 | + input_dtype = np.int8 | ||
| 152 | + mm_dtype = np.int32 | ||
| 153 | + output_dtype = np.float16 if scenario_num in (1, 2) else np.int8 | ||
| 154 | + x1 = np.random.randint(-3, 3, (M, K)).astype(input_dtype) | ||
| 155 | + x2 = np.random.randint(-3, 3, (K, N)).astype(input_dtype) | ||
| 156 | + else: | ||
| 157 | + input_dtype = np.float16 | ||
| 158 | + mm_dtype = np.float32 | ||
| 159 | + output_dtype = np.float32 if scenario_num == 7 else np.int8 | ||
| 160 | + x1 = np.random.uniform(-3.0, 3.0, (M, K)).astype(input_dtype) | ||
| 161 | + x2 = np.random.uniform(-3.0, 3.0, (K, N)).astype(input_dtype) | ||
| 162 | + | ||
| 163 | + golden = np.matmul(x1.astype(mm_dtype), x2.astype(mm_dtype)).astype(mm_dtype) | ||
| 164 | + quant_params = make_quant_params() | ||
| 165 | + if scenario_num != 7: | ||
| 166 | + golden = apply_prequant(golden, scenario_num, quant_params) | ||
| 167 | + | ||
| 168 | + os.makedirs("input", exist_ok=True) | ||
| 169 | + os.makedirs("output", exist_ok=True) | ||
| 170 | + if scenario_num in VECTOR_QUANT_SCENARIOS: | ||
| 171 | + quant_params.tofile("./input/quant_pre.bin") | ||
| 172 | + | ||
| 173 | + if scenario_num in NZ_OUTPUT_SCENARIOS: | ||
| 174 | + block_columns = 16 if output_dtype == np.float16 else 32 | ||
| 175 | + golden = golden.reshape( | ||
| 176 | + M // 16, 16, N // block_columns, block_columns | ||
| 177 | + ).transpose(2, 0, 1, 3) | ||
| 178 | + | ||
| 179 | + # The kernel consumes A as two consecutive [M, K / K_ROUND] chunks. | ||
| 180 | + x1.reshape(M, K_ROUND, K // K_ROUND).transpose(1, 0, 2).astype(input_dtype).tofile( | ||
| 181 | + "./input/x1_gm.bin" | ||
| 182 | + ) | ||
| 183 | + x2.astype(input_dtype).tofile("./input/x2_gm.bin") | ||
| 184 | + golden.astype(output_dtype).tofile("./output/golden.bin") | ||
| 185 | + | ||
| 186 | + | ||
| 187 | +if __name__ == "__main__": | ||
| 188 | + parser = argparse.ArgumentParser() | ||
| 189 | + parser.add_argument("-scenarioNum", type=int, default=1, choices=range(1, 8)) | ||
| 190 | + args = parser.parse_args() | ||
| 191 | + gen_golden_data(args.scenarioNum) | ||
| @@ -0,0 +1,70 @@ | |||
| 1 | +#!/usr/bin/env python3 | ||
| 2 | +# -*- coding: utf-8 -*- | ||
| 3 | +# ---------------------------------------------------------------------------------------------------------- | ||
| 4 | +# Copyright (c) 2026 Huawei Technologies Co., Ltd. | ||
| 5 | +# This program is free software, you can redistribute it and/or modify it under the terms and conditions of | ||
| 6 | +# CANN Open Software License Agreement Version 2.0 (the "License"). | ||
| 7 | +# Please refer to the License for details. You may not use this file except in compliance with the License. | ||
| 8 | +# THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED, | ||
| 9 | +# INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE. | ||
| 10 | +# See LICENSE in the root of the software repository for the full text of the License. | ||
| 11 | +# ---------------------------------------------------------------------------------------------------------- | ||
| 12 | + | ||
| 13 | +import argparse | ||
| 14 | +import sys | ||
| 15 | + | ||
| 16 | +import numpy as np | ||
| 17 | + | ||
| 18 | + | ||
| 19 | +RELATIVE_TOL = 1e-6 | ||
| 20 | +ABSOLUTE_TOL = 1e-9 | ||
| 21 | +FLOAT_OUTPUT_RELATIVE_TOL = 1e-4 | ||
| 22 | +FLOAT_OUTPUT_ABSOLUTE_TOL = 1e-4 | ||
| 23 | +ERROR_TOL = 1e-4 | ||
| 24 | + | ||
| 25 | + | ||
| 26 | +def get_tolerance(scenario_num): | ||
| 27 | + if scenario_num == 7: | ||
| 28 | + return FLOAT_OUTPUT_RELATIVE_TOL, FLOAT_OUTPUT_ABSOLUTE_TOL | ||
| 29 | + return RELATIVE_TOL, ABSOLUTE_TOL | ||
| 30 | + | ||
| 31 | + | ||
| 32 | +def verify_result(scenario_num, output_path, golden_path): | ||
| 33 | + output_dtype = ( | ||
| 34 | + np.float32 | ||
| 35 | + if scenario_num == 7 | ||
| 36 | + else (np.float16 if scenario_num in (1, 2) else np.int8) | ||
| 37 | + ) | ||
| 38 | + output = np.fromfile(output_path, dtype=output_dtype).reshape(-1) | ||
| 39 | + golden = np.fromfile(golden_path, dtype=output_dtype).reshape(-1) | ||
| 40 | + if output.size != golden.size: | ||
| 41 | + print( | ||
| 42 | + "element count mismatch: expected %d, actual %d" | ||
| 43 | + % (golden.size, output.size) | ||
| 44 | + ) | ||
| 45 | + return False | ||
| 46 | + | ||
| 47 | + relative_tol, absolute_tol = get_tolerance(scenario_num) | ||
| 48 | + match = np.isclose( | ||
| 49 | + output, golden, rtol=relative_tol, atol=absolute_tol, equal_nan=True | ||
| 50 | + ) | ||
| 51 | + mismatch_indices = np.where(~match)[0] | ||
| 52 | + for index in mismatch_indices[:101]: | ||
| 53 | + print( | ||
| 54 | + "data index: %06d, expected: %-.9f, actual: %-.9f" | ||
| 55 | + % (index, golden[index], output[index]) | ||
| 56 | + ) | ||
| 57 | + error_ratio = float(mismatch_indices.size) / golden.size | ||
| 58 | + return error_ratio <= ERROR_TOL | ||
| 59 | + | ||
| 60 | + | ||
| 61 | +if __name__ == "__main__": | ||
| 62 | + parser = argparse.ArgumentParser() | ||
| 63 | + parser.add_argument("-scenarioNum", type=int, default=1, choices=range(1, 8)) | ||
| 64 | + parser.add_argument("output", type=str) | ||
| 65 | + parser.add_argument("golden", type=str) | ||
| 66 | + args = parser.parse_args() | ||
| 67 | + if not verify_result(args.scenarioNum, args.output, args.golden): | ||
| 68 | + print("[ERROR] result error") | ||
| 69 | + sys.exit(1) | ||
| 70 | + print("test pass!") | ||


这个有对外接口