asc_set_l3d_rpt_b
产品支持情况
- 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"。
本接口用于配置3D img2col搬运模式下使用repeat模式搬运右矩阵时所需的参数,该配置供参数f_matrix_ctrl取值为true的asc_copy_l12l0b(3D矩阵搬运)接口使用。
本接口为矩阵搬入相关配置接口,仅在AIC上生效。
函数原型
C
__aicore__ inline void asc_set_l3d_rpt_b(uint64_t config)
参数说明
表1 参数说明
| 参数名 | 输入/输出 | 描述 |
|---|---|---|
| config | 输入 | repeat模式相关配置。接口不检查或转换字段值,需按照表2完成拼装。 |
表2 config字段说明
| 比特位 | 描述 |
|---|---|
| 15:0 | 相邻两次迭代起始位置的距离。M方向迭代时单位为16个元素;K方向迭代时单位为32 / sizeof(data_type)个元素,data_type为源操作数的数据类型。 |
| 23:16 | M或K方向的迭代次数。 |
| 24 | 0:沿height(M)方向迭代;1:沿width(K)方向迭代。 |
| 31:25 | 预留参数,当前须设置为0。 |
| 47:32 | 目的矩阵K方向的步长,单位为512字节的分形。 |
| 63:48 | 目的矩阵M方向的起始位置,单位为512字节的分形。 |
返回值说明
无
流水类型
PIPE_S
约束说明
- 本接口仅在AIC上生效,非AIC调用直接返回。
- 本接口必须在对应的3D搬运接口之前调用,3D搬运接口的参数
f_matrix_ctrl取值为true时配置才会生效。 - 调用后配置会持续生效,直至再次调用本接口覆盖,不同3D搬运任务使用的配置不同时,需在对应搬运接口执行前重新配置。
调用示例
asc_set_l3d_rpt_b需要调用者自行拼装64位配置。相同功能推荐使用参数化接口asc_set_l13d_rpt_b,以下示例使用该接口配置右矩阵的repeat参数。
以下示例使用asc_copy_l12l0b将2×2、16通道的全零Feature Map扩展为带一圈padding的4×4矩阵,并自动转置写入L0B Buffer。B侧padding值设置为2,f_matrix_ctrl设置为true。使用全1的L0A矩阵进行MMAD后,每个输出累加4×4区域中的12个padding位置,期望值为12 × 2 = 24。
将代码保存为example.asc后,可通过bisheng命令编译运行,其中--npu-arch参数需根据实际产品型号指定对应的NPU架构,具体产品与NPU架构的映射关系请参考__NPU_ARCH__。
以Ascend 950PR&950DT系列产品(对应NPU架构为dav-3510)为例,编译运行命令如下:
Bash
bisheng example.asc -o main --npu-arch=dav-3510 && ./main
C++
#include <cmath>
#include <cstdint>
#include <iostream>
#include <vector>
#include "acl/acl.h"
#include "c_api/asc_simd.h"
namespace {
constexpr uint32_t H = 2;
constexpr uint32_t W = 2;
constexpr uint32_t C0 = 16;
constexpr uint32_t CIN = C0;
constexpr uint32_t COUT = C0;
constexpr uint32_t PAD = 1;
constexpr uint32_t HOUT = H + 2 * PAD;
constexpr uint32_t WOUT = W + 2 * PAD;
constexpr uint32_t M = HOUT * WOUT;
constexpr uint32_t M_ALIGN = 16;
constexpr uint32_t K = CIN;
constexpr uint32_t K_ALIGN = 16;
constexpr half PAD_VALUE = static_cast<half>(2);
constexpr uint32_t FMAP_ELEMENTS = H * W * CIN;
constexpr uint32_t WEIGHT_ELEMENTS = COUT * K;
constexpr uint32_t OUTPUT_ELEMENTS = COUT * M;
__global__ __cube__ void asc_set_l13d_rpt_b_kernel(
__gm__ half* fmap, __gm__ half* weight, __gm__ half* output)
{
asc_init();
__cbuf__ half fmap_l1[FMAP_ELEMENTS];
__cbuf__ half weight_l1[WEIGHT_ELEMENTS];
__ca__ half weight_l0[COUT * K_ALIGN];
__cb__ half fmap_l0[K_ALIGN * M_ALIGN];
__cc__ float output_l0[COUT * M_ALIGN];
asc_set_gm2l1_nz_para(1, 1, H * W, 0);
asc_copy_gm2l1_nd2nz(fmap_l1, fmap, CIN * sizeof(half),
asc_load_l2_cache_mode::NORMAL_FIRST_VICTIM, H * W, CIN, 0, false);
asc_set_gm2l1_nz_para(1, 1, K_ALIGN, 0);
asc_copy_gm2l1_nd2nz(weight_l1, weight, K * sizeof(half),
asc_load_l2_cache_mode::NORMAL_FIRST_VICTIM, COUT, K, 0, false);
asc_sync_notify(PIPE_MTE2, PIPE_MTE1, EVENT_ID0);
asc_sync_wait(PIPE_MTE2, PIPE_MTE1, EVENT_ID0);
asc_copy_l12l0a(weight_l0, weight_l1, 0, 0, COUT / 16, K_ALIGN / 16, COUT / 16, COUT / 16);
// f_matrix_ctrl=true时,3D搬运从B侧寄存器组读取Feature Map描述和padding值。
asc_set_l13d_fmatrix_b(W, H, PAD, PAD, PAD, PAD);
asc_set_l12l0b_3d_padding(PAD_VALUE);
asc_set_l13d_rpt_b(0, 1, asc_l13d_repeat_direction::M_DIRECTION, M_ALIGN / 16, 0);
// 目的地址为L0B Buffer;f_matrix_ctrl=true,选择上面配置的B侧寄存器组。
asc_copy_l12l0b(
fmap_l0, fmap_l1, K_ALIGN, M, 0, 0, 1, 1, 1, 1, 1, 1, false, false, false, true, CIN);
asc_sync_notify(PIPE_MTE1, PIPE_M, EVENT_ID0);
asc_sync_wait(PIPE_MTE1, PIPE_M, EVENT_ID0);
asc_mmad(output_l0, weight_l0, fmap_l0, COUT, K_ALIGN, M,
asc_unit_flag_mode::DISABLE, false, false, true);
asc_sync_notify(PIPE_M, PIPE_FIX, EVENT_ID0);
asc_sync_wait(PIPE_M, PIPE_FIX, EVENT_ID0);
asc_set_l0c_copy_nz_para(1, 0, 0);
asc_copy_l0c2gm(output, output_l0, M, COUT, M, COUT,
asc_store_l2_cache_mode::NORMAL_FIRST_VICTIM, asc_unit_flag_mode::DISABLE, asc_quant_mode::F322F16,
asc_relu_pre_mode::NONE, false, true, false, false);
asc_sync();
}
} // namespace
int main()
{
// 有效Feature Map全为0;L0A矩阵全为1,使MMAD结果只反映L0B中的padding值。
std::vector<half> fmap(FMAP_ELEMENTS, static_cast<half>(0));
std::vector<half> weight(WEIGHT_ELEMENTS, static_cast<half>(0));
std::vector<half> output(OUTPUT_ELEMENTS);
for (uint32_t i = 0; i < WEIGHT_ELEMENTS; ++i) weight[i] = static_cast<half>(1);
half *fmap_device = nullptr, *weight_device = nullptr, *output_device = nullptr;
if (aclInit(nullptr) != ACL_SUCCESS || aclrtSetDevice(0) != ACL_SUCCESS ||
aclrtMalloc(reinterpret_cast<void**>(&fmap_device), fmap.size() * sizeof(half), ACL_MEM_MALLOC_HUGE_FIRST) != ACL_SUCCESS ||
aclrtMalloc(reinterpret_cast<void**>(&weight_device), weight.size() * sizeof(half), ACL_MEM_MALLOC_HUGE_FIRST) != ACL_SUCCESS ||
aclrtMalloc(reinterpret_cast<void**>(&output_device), output.size() * sizeof(half), ACL_MEM_MALLOC_HUGE_FIRST) != ACL_SUCCESS) {
return 1;
}
aclrtMemcpy(fmap_device, fmap.size() * sizeof(half), fmap.data(), fmap.size() * sizeof(half), ACL_MEMCPY_HOST_TO_DEVICE);
aclrtMemcpy(weight_device, weight.size() * sizeof(half), weight.data(), weight.size() * sizeof(half), ACL_MEMCPY_HOST_TO_DEVICE);
asc_set_l13d_rpt_b_kernel<<<1, 0>>>(fmap_device, weight_device, output_device);
const bool runtime_ok = aclrtSynchronizeDevice() == ACL_SUCCESS &&
aclrtMemcpy(output.data(), output.size() * sizeof(half), output_device, output.size() * sizeof(half), ACL_MEMCPY_DEVICE_TO_HOST) == ACL_SUCCESS;
uint32_t match_count = 0;
uint32_t unexpected_count = 0;
constexpr float EXPECTED_OUTPUT = 12.0f * static_cast<float>(PAD_VALUE);
for (uint32_t i = 0; i < OUTPUT_ELEMENTS; ++i) {
const float value = static_cast<float>(output[i]);
if (std::fabs(value - EXPECTED_OUTPUT) <= 1e-3f) ++match_count;
else {
++unexpected_count;
if (unexpected_count <= 12) std::cout << "UNEXPECTED index=" << i << " value=" << value << std::endl;
}
}
const bool passed = runtime_ok && match_count == OUTPUT_ELEMENTS && unexpected_count == 0;
std::cout << "RESULT status=" << (passed ? "PASS" : "FAIL") << " expected=" << EXPECTED_OUTPUT
<< " match_count=" << match_count << '/' << OUTPUT_ELEMENTS
<< " unexpected=" << unexpected_count << std::endl;
aclrtFree(output_device);
aclrtFree(weight_device);
aclrtFree(fmap_device);
aclrtResetDevice(0);
aclFinalize();
return passed ? 0 : 1;
}