Skip to content

okftype: reference.api description: "在AIC上以512字节分形为单位将L1 Buffer中的2D矩阵搬入L0B Buffer,支持非转置与转置两种模式,覆盖b4至b32数据类型。" tag:

  • roles: [data_movement]
  • operations: [copy, transpose]
  • traits: [datablock_granularity] dependencies:
  • path: docs/zh/api/SIMD-API/c_api/cube_datamove/cube_compute_load/cube_compute_input_move_constraint.md kind: constrained_by version: ">=9.1.0" version_changes: []

asc_copy_l12l0b(NPU架构版本3510,2D矩阵搬运)

产品支持情况​

  • Ascend 950PR&950DT系列产品:支持
  • Atlas A3系列产品:不支持
  • Atlas A2系列产品:不支持
  • Atlas 200I/500 A2推理产品:不支持
  • Atlas推理系列产品AI Core:不支持
  • Atlas推理系列产品Vector Core:不支持
  • Atlas训练系列产品:不支持

功能说明​

头文件路径为:"c_api/cube_datamove/cube_datamove.h"。

负责完成矩阵计算所需的2D格式数据的搬运,搬运粒度为大小为512字节的数据分形,支持非转置与转置矩阵从L1 Buffer到L0B buffer的数据搬运。

对于不同的数据类型,对应的分形矩阵如下:

  • 对于b4数据类型,每个数据分形在L0B Buffer中为一个64x16的矩阵。
  • 对于b8数据类型,每个数据分形在L0B Buffer中为一个32×16的矩阵。
  • 对于b16数据类型,每个数据分形为一个16×16的矩阵。
  • 对于b32数据类型,每个数据分形在L0B Buffer中为一个8×16的矩阵。

本接口仅在AIC上执行有效。

搬入前的数据排布参见关键分形格式详解;地址对齐与搬运粒度要求参见矩阵计算输入搬运约束,并结合本接口的参数和约束说明使用。

B矩阵输入布局与接口选择​

本接口的“非转置搬运”和“转置搬运”描述的是L1 Buffer到L0B Buffer的数据搬运方式,不直接代表矩阵乘法表达式中的右矩阵是否转置。

以目标计算C = A × B为例,其中A的形状为M×K,B的形状为K×N。当GM中的输入采用ND格式,并通过asc_copy_gm2l1_nd2nz转换为L1 Buffer中的Nz格式时,B侧接口选择如下:

GM中存储的数据GM输入形状L1到L0B搬运接口目标计算
BK×Nasc_copy_l12l0b_transposeC = A × B
Bᵀ,即预先转置的BN×Kasc_copy_l12l0bC = A × B

L0B Buffer使用Zn分形排布,应结合源矩阵的实际布局选择接口,不能仅根据接口名中的transpose判断最终乘法方向。转置搬运仍需满足本页参数说明和约束说明中的分形、对齐及空间要求。

函数原型​

2D搬运模式​

C++
__aicore__ inline void asc_copy_l12l0b(__cb__ <dtype>* dst,
                                       __cbuf__ <dtype>* src,
                                       uint16_t m_start_position,
                                       uint16_t k_start_position,
                                       uint8_t m_step,
                                       uint8_t k_step,
                                       int16_t src_stride,
                                       uint16_t dst_stride)

dtype支持数据类型​

dtype取值为:int4b_t、int8_t、uint8_t、fp4x2_e2m1_t、fp4x2_e1m2_t、hifloat8_t、fp8_e5m2_t、fp8_e4m3fn_t、int16_t、uint16_t、half、bfloat16_t、int32_t、uint32_t、float。

函数原型典型示例​

C++
// 示例:half类型的2D搬运。
__aicore__ inline void asc_copy_l12l0b(__cb__ half* dst,
                                       __cbuf__ half* src,
                                       uint16_t m_start_position,
                                       uint16_t k_start_position,
                                       uint8_t m_step,
                                       uint8_t k_step,
                                       int16_t src_stride,
                                       uint16_t dst_stride)

2D转置搬运模式​

C++
__aicore__ inline void asc_copy_l12l0b_transpose(__cb__ <dtype>* dst,
                                                 __cbuf__ <dtype>* src,
                                                 uint16_t m_start_position,
                                                 uint16_t k_start_position,
                                                 uint8_t m_step,
                                                 uint8_t k_step,
                                                 int16_t src_stride,
                                                 uint16_t dst_stride)

dtype支持数据类型​

dtype取值为:int4b_t、int8_t、uint8_t、fp4x2_e2m1_t、fp4x2_e1m2_t、hifloat8_t、fp8_e5m2_t、fp8_e4m3fn_t、int16_t、uint16_t、half、bfloat16_t、int32_t、uint32_t、float。

函数原型典型示例​

C++
// 示例:half类型的2D搬运。
__aicore__ inline void asc_copy_l12l0b_transpose(__cb__ half* dst,
                                                 __cbuf__ half* src,
                                                 uint16_t m_start_position,
                                                 uint16_t k_start_position,
                                                 uint8_t m_step,
                                                 uint8_t k_step,
                                                 int16_t src_stride,
                                                 uint16_t dst_stride)

参数说明​

表1 参数说明

参数名输入/输出含义
dst输出目的矩阵,存储位置为L0B Buffer。起始地址需要按照512字节对齐。
src输入源矩阵,存储位置为L1 Buffer。起始地址需要按照32字节对齐。
m_start_position输入源矩阵行方向起始分形位置。单位为16个元素,对应分形行方向的固定宽度。取值范围:[0, ]。
k_start_position输入源矩阵列方向起始分形位置。单位为32字节,对应分形列方向的基本寻址粒度。取值范围:[0, ]。
m_step输入源矩阵行方向搬运的分形行数。单位为16个元素,对应分形行方向的固定宽度。取值范围:[0, 255]。
转置场景下(调用asc_copy_l12l0b_transpose),m_step除需满足取值范围外,还需满足以下额外约束:
  •当数据类型为b4时,m_step必须是4的倍数;
  •当数据类型为b8时,m_step必须是2的倍数;
  •当数据类型为b16时,m_step必须是1的倍数;
  •当数据类型为b32时,m_step无额外约束。
注:m_step=0表示不执行搬运,该接口将被视为NOP(空操作)。
k_step输入源矩阵列方向搬运的分形列数。单位为32字节,对应分形列方向的基本寻址粒度。取值范围:[0, 255]。
转置场景下(调用asc_copy_l12l0b_transpose),k_step除需满足取值范围外,还需满足以下额外约束:
  •当数据类型为b4、b8或b16时,k_step没有额外约束;
  •当数据类型为b32时,k_step必须是2的倍数。
注:k_step=0表示不执行搬运,该接口将被视为NOP(空操作)。
src_stride输入源矩阵列方向相邻分形起始地址的间隔。单位为512字节,即一个数据分形的大小。取值范围:[, ]。
dst_stride输入目的矩阵列方向相邻分形起始地址的间隔。单位为512字节,即一个数据分形的大小。取值范围:[1, ]。

返回值说明​

无

流水类型​

PIPE_MTE1

约束说明​

通用约束​

  • 本接口非AIC调用直接返回。
  • src位于L1 Buffer,dst位于L0B Buffer,二者位于不同的物理存储单元,不存在地址重叠。
  • 如果本指令与其他指令存在数据依赖,需要插入同步指令,保证多个指令串行化,防止出现异常数据。
  • L0B Buffer容量上限:L0B Buffer总容量64KB,dst偏移量与搬运大小之和不可越界,否则触发写溢出异常。
  • L1 Buffer容量上限:L1 Buffer总容量512KB,src偏移量与源矩阵占用大小之和不可越界,否则触发异常。

2D分形搬运约束​

  • m_step或k_step为0时不执行搬运,本接口被视为NOP(空操作)。应避免无意传入0,否则不会产生有效数据且会增加指令调度开销。
  • 最小搬运粒度为1个512字节的数据分形。矩阵边缘不足一个分形时,源矩阵需要补齐到完整分形;搬运会包含补齐区域中的无效数据。
  • 对于矩阵转置场景,不同的数据类型需要满足不同的m_step和k_step约束:b4数据类型时,m_step必须是4的倍数;b8数据类型时,m_step必须是2的倍数;b16数据类型时,m_step必须是1的倍数;b32数据类型时,k_step必须是2的倍数。
  • 对于矩阵转置场景,由于存在m_step和k_step约束,需要确保L1 Buffer预留足够的空间,避免发生读越界,导致搬运出现未定义行为;
  • 对于b8转置场景搬运,即调用asc_copy_l12l0b_transpose接口,假设C矩阵大小为(M,N),若且:
    • 此场景下若只调用一次asc_copy_l12l0b_transpose搬运接口,由于b8转置场景最小搬运单位为两个分形,会导致N方向多搬1个无效分形,后续Mmad计算时需要让此分型参与计算,否则会导致计算结果错误,并且最后在矩阵搬出时,只搬出有效区域。在此调用方式下,计算的数据大小和搬出的数据大小不一致,导致无法在矩阵搬出时开启UnitFlag功能。
    • 若多次调用for循环调用asc_copy_l12l0b_transpose搬运接口,需合理设置dst_stride参数,写入L0B Buffer时跳过转置多读的N方向无效分型,则Mmad计算时无需让此分型参与计算,计算和矩阵搬出的数据大小保持一致,此场景下可以在矩阵搬出时开启UnitFlag功能。但需要注意采用for循环多次搬运,会容易导致MTE1指令队列满,极致流水场景下可能会造成流水阻塞,需合理选择是否使用for循环多次搬运。

调用示例​

以下示例演示asc_copy_l12l0b的非转置搬运。示例输入矩阵A的形状为M×K,输入矩阵B的形状为N×K,实际计算为C = A × Bᵀ,输出形状为M×N。Host侧Golden按相同公式计算。

本例中的B指代码变量b所存储的输入矩阵;它相当于前述接口选择表中预先转置后的输入。

涵盖A/B矩阵转置与非转置输入、int8_t/half/float数据类型的完整工程,请参考load_data_l12l0_950样例。

将代码保存为examples.asc后,可通过bisheng命令编译运行,其中--npu-arch参数需根据实际产品型号指定对应的NPU架构,具体产品与NPU架构的映射关系请参考__NPU_ARCH__。

以Ascend 950PR&950DT系列产品(对应NPU架构为dav-3510)为例,编译运行命令如下:

Bash
bisheng examples.asc -o main --npu-arch=dav-3510 && ./main

样例单核占用32KB L1 Buffer、16KB L0A Buffer、16KB L0B Buffer和64KB L0C Buffer。样例依次复用EVENT_ID0完成MTE2到MTE1、MTE1到M、M到FIX的同步。

C++
#include <cstdint>
#include <iostream>
#include <vector>
#include "c_api/asc_simd.h"
#include "acl/acl.h"

namespace {
constexpr uint32_t M = 128;
constexpr uint32_t K = 128;
constexpr uint32_t N = 128;
constexpr uint32_t ELEMENTS = M * N;
constexpr uint32_t CUBE_M = 16;
constexpr uint32_t CUBE_K = 32;

__global__ __cube__ void AscCopyL12l0bKernel(__gm__ int8_t* a, __gm__ int8_t* b,
    __gm__ int32_t* output)
{
    asc_init();
    __cbuf__ int8_t a_l1[M * K], b_l1[N * K];
    __ca__ int8_t a_l0[M * K];
    __cb__ int8_t b_l0[K * N];
    __cc__ int32_t c_l0[ELEMENTS];

    // 将形状为M×K的A矩阵和形状为N×K的B矩阵从GM搬入L1 Buffer,并转换为Nz格式。
    asc_set_gm2l1_nz_para(1, 1, 128, 0);
    asc_copy_gm2l1_nd2nz(a_l1, a, K, asc_load_l2_cache_mode::NORMAL_FIRST_VICTIM, M, K, 0, false);
    asc_set_gm2l1_nz_para(1, 1, 128, 0);
    asc_copy_gm2l1_nd2nz(b_l1, b, K, asc_load_l2_cache_mode::NORMAL_FIRST_VICTIM, N, K, 0, false);
    asc_sync_notify(PIPE_MTE2, PIPE_MTE1, EVENT_ID0);
    asc_sync_wait(PIPE_MTE2, PIPE_MTE1, EVENT_ID0);

    // 将A矩阵和B矩阵从L1 Buffer分别搬入L0A Buffer和L0B Buffer,B侧使用asc_copy_l12l0b非转置搬运。
    asc_copy_l12l0a(a_l0, a_l1, 0, 0, M / CUBE_M, K / CUBE_K, M / CUBE_M, M / CUBE_M);
    asc_copy_l12l0b(b_l0, b_l1, 0, 0, N / CUBE_M, K / CUBE_K, N / CUBE_M, N / CUBE_M);
    asc_sync_notify(PIPE_MTE1, PIPE_M, EVENT_ID0);
    asc_sync_wait(PIPE_MTE1, PIPE_M, EVENT_ID0);

    // 执行C = A × Bᵀ的矩阵乘法,其中M=N=K=128。
    asc_mmad(c_l0, a_l0, b_l0, M, K, N, asc_unit_flag_mode::DISABLE, true, false, true);
    asc_sync_notify(PIPE_M, PIPE_FIX, EVENT_ID0);
    asc_sync_wait(PIPE_M, PIPE_FIX, EVENT_ID0);

    // 将L0C Buffer中的Nz结果转换为ND格式并搬出到GM。
    asc_set_l0c_copy_nz_para(1, 0, 0);
    asc_copy_l0c2gm(output, c_l0, N, M, N, M, asc_store_l2_cache_mode::NORMAL_FIRST_VICTIM,
        asc_unit_flag_mode::DISABLE, QuantMode_t::NoQuant, asc_relu_pre_mode::NONE, false, true, false, false);
    asc_sync_pipe(PIPE_FIX);
}

template <typename T>
void PrintRow(const char* label, const std::vector<T>& data)
{
    std::cout << label << ':';
    for (uint32_t i = 0; i < 8; ++i) {
        std::cout << ' ' << +data[i];
    }
    std::cout << " ..." << std::endl;
}
} // namespace

int main()
{
    // 构造输入矩阵,并在Host侧按C = A × Bᵀ计算Golden结果。
    std::vector<int8_t> a(M * K), b(N * K);
    std::vector<int32_t> output(ELEMENTS), golden(ELEMENTS);
    for (uint32_t row = 0; row < M; ++row) {
        for (uint32_t k = 0; k < K; ++k) {
            a[row * K + k] = static_cast<int8_t>(static_cast<int32_t>((row + 2 * k) % 5) - 2);
        }
    }
    for (uint32_t col = 0; col < N; ++col) {
        for (uint32_t k = 0; k < K; ++k) {
            b[col * K + k] = static_cast<int8_t>(static_cast<int32_t>((3 * col + k) % 5) - 2);
        }
    }
    for (uint32_t row = 0; row < M; ++row) {
        for (uint32_t col = 0; col < N; ++col) {
            for (uint32_t k = 0; k < K; ++k) {
                golden[row * N + col] += a[row * K + k] * b[col * K + k];
            }
        }
    }

    // 分配Device内存、执行核函数(Kernel)并取回结果。
    aclInit(nullptr);
    aclrtSetDevice(0);
    int8_t *a_device = nullptr, *b_device = nullptr;
    int32_t* output_device = nullptr;
    aclrtMalloc(reinterpret_cast<void**>(&a_device), a.size(), ACL_MEM_MALLOC_HUGE_FIRST);
    aclrtMalloc(reinterpret_cast<void**>(&b_device), b.size(), ACL_MEM_MALLOC_HUGE_FIRST);
    aclrtMalloc(reinterpret_cast<void**>(&output_device), output.size() * sizeof(int32_t),
        ACL_MEM_MALLOC_HUGE_FIRST);
    aclrtMemcpy(a_device, a.size(), a.data(), a.size(), ACL_MEMCPY_HOST_TO_DEVICE);
    aclrtMemcpy(b_device, b.size(), b.data(), b.size(), ACL_MEMCPY_HOST_TO_DEVICE);
    AscCopyL12l0bKernel<<<1, 0>>>(a_device, b_device, output_device);
    aclrtSynchronizeDevice();
    aclrtMemcpy(output.data(), output.size() * sizeof(int32_t), output_device,
        output.size() * sizeof(int32_t), ACL_MEMCPY_DEVICE_TO_HOST);

    PrintRow("Output row 0", output);
    PrintRow("Golden row 0", golden);
    const bool passed = output == golden;
    std::cout << (passed ? "[Success] asc_copy_l12l0b result is correct."
                         : "[Failed] asc_copy_l12l0b result mismatch.") << std::endl;

    aclrtFree(a_device);
    aclrtFree(b_device);
    aclrtFree(output_device);
    aclrtResetDevice(0);
    aclFinalize();
    return passed ? 0 : 1;
}

免责声明:本站内容由 asc-devkit 仓 master 分支自动编译生成,属于持续开发版本,可能存在缺陷,仅供预览与参考。如需稳定及商用资料,请查阅官方 昇腾社区。