Skip to content

okftype: reference.api description: "配置 L0C Buffer 搬出过程中随路 Scalar 激活所需的缩放系数,取值采用 1 位符号位、8 位指数位与 10 位尾数位的高 19 位格式,需与搬出接口的激活模式搭配使用。" tag:

  • roles: [data_movement_configuration] dependencies:
  • path: docs/zh/api/SIMD-API/c_api/cube_datamove/cube_compute_store/asc_copy_l0c2gm_arch_3510.md kind: configures condition: "需与搬出接口的 relu_pre_mode 搭配使用"
  • path: docs/zh/api/SIMD-API/c_api/cube_datamove/cube_compute_store/asc_copy_l0c2l1_arch_3510.md kind: configures condition: "需与搬出接口的 relu_pre_mode 搭配使用"
  • path: docs/zh/api/SIMD-API/c_api/cube_datamove/cube_compute_store/asc_copy_l0c2ub.md kind: configures condition: "需与搬出接口的 relu_pre_mode 搭配使用"
  • path: docs/zh/api/SIMD-API/c_api/cube_datamove/cube_store_key_features/accompanying_quantization_and_relu_scenario_combination.md kind: related
  • path: docs/zh/api/SIMD-API/c_api/cube_datamove/cube_store_key_features/accompanying_relu.md kind: related version: ">=9.2.0" version_changes:
  • since: "9.2.0" kind: renamed scope: "page" scope_basis: kind_default note: "由 asc_set_l0c2gm_lrelu_alpha(9.1.0 提供 half& 与 float& 两种参数形式)改为 asc_set_l0c_copy_lrelu_alpha,新接口只保留 float 标量参数形式;Release Notes CANN_9_2_0 重命名表记为「接口名变化;新接口仅支持 float 参数,不再提供 half 参数形式」,旧名保留为 deprecated_interface 页面。"

asc_set_l0c_copy_lrelu_alpha

产品支持情况​

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

功能说明​

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

本接口用于在L0C Buffer搬出过程中进行随路Scalar激活时,配置激活计算所需的缩放系数。配置完成后,由后续调用的asc_copy_l0c2gm、asc_copy_l0c2l1或asc_copy_l0c2ub接口使用,并且需要与搬出接口的relu_pre_mode搭配使用。

本接口为矩阵搬出相关配置接口,仅在AIC上生效。

激活模式及配套配置见随路ReLU;与量化同时使用时,参见随路量化与随路ReLU场景组合。

函数原型​

C++
__aicore__ inline void asc_set_l0c_copy_lrelu_alpha(float scalar_relu_pre_alpha);

参数说明​

表1 参数说明

参数名输入/输出描述
scalar_relu_pre_alpha输入Scalar激活缩放系数,仅高19位有效,为(1, 8, 10)浮点格式,即1bit符号位、8bit指数位和10bit尾数位,取值不能为inf或nan。

返回值说明​

无

流水类型​

PIPE_S

约束说明​

  • 本接口仅在AIC上生效,在AIV上调用将直接返回。
  • 本接口需在对应的L0C Buffer搬出接口执行前调用,并在搬出接口中配置对应的Scalar激活模式。
  • scalar_relu_pre_alpha不能为inf、nan或非规格化数,若要取值为0时,则指数位和尾数位均需为0。
  • 调用后配置会持续生效,直至再次调用本接口覆盖,不同搬出任务使用的系数不同时,需在对应搬出接口执行前重新配置。

调用示例​

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

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

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

以下调用示例代码仅Ascend 950PR&950DT系列产品支持。

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

namespace {
constexpr uint32_t DIM = 16, ELEMENTS = DIM * DIM;
constexpr uint16_t HALF_ONE = 0x3c00;
constexpr uint16_t HALF_NEG_ONE = 0xbc00, HALF_NEG_HALF = 0xb800;

__global__ __cube__ void asc_set_l0c_copy_lrelu_alpha_kernel(
    __gm__ uint16_t* a, __gm__ uint16_t* b, __gm__ uint16_t* output)
{
    asc_init();
    __cbuf__ half a_l1[ELEMENTS], b_l1[ELEMENTS];
    __ca__ half a_l0[ELEMENTS];
    __cb__ half b_l0[ELEMENTS];
    __cc__ float c_l0[ELEMENTS];
    asc_set_gm2l1_nz_para(1, 1, 16, 0);
    asc_copy_gm2l1_nd2nz(a_l1, reinterpret_cast<__gm__ half*>(a), DIM * sizeof(half),
        asc_load_l2_cache_mode::NORMAL_FIRST_VICTIM, DIM, DIM, 0, false);
    asc_set_gm2l1_nz_para(1, 1, 16, 0);
    asc_copy_gm2l1_nd2nz(b_l1, reinterpret_cast<__gm__ half*>(b), DIM * sizeof(half),
        asc_load_l2_cache_mode::NORMAL_FIRST_VICTIM, DIM, DIM, 0, false);
    asc_sync_notify(PIPE_MTE2, PIPE_MTE1, EVENT_ID0);
    asc_sync_wait(PIPE_MTE2, PIPE_MTE1, EVENT_ID0);
    asc_copy_l12l0a(a_l0, a_l1, 0, 0, 1, 1, 1, 1);
    asc_copy_l12l0b_transpose(b_l0, b_l1, 0, 0, 1, 1, 1, 1);
    asc_sync_notify(PIPE_MTE1, PIPE_M, EVENT_ID0);
    asc_sync_wait(PIPE_MTE1, PIPE_M, EVENT_ID0);
    asc_mmad(c_l0, a_l0, b_l0, DIM, DIM, DIM, asc_unit_flag_mode::DISABLE, 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_lrelu_alpha(0.5f);
    asc_set_l0c_copy_nz_para(1, 0, 0);
    asc_copy_l0c2gm(reinterpret_cast<__gm__ half*>(output), c_l0, DIM, DIM, DIM, DIM,
        asc_store_l2_cache_mode::NORMAL_FIRST_VICTIM, asc_unit_flag_mode::DISABLE, asc_quant_mode::F322F16,
        asc_relu_pre_mode::SCALAR, false, true, false, false);
    asc_sync();
}

void print_row(const char* label, const std::vector<uint16_t>& data)
{
    std::cout << label << ':' << std::hex;
    for (uint32_t i = 0; i < 8; ++i) std::cout << " 0x" << data[i];
    std::cout << std::dec << " ..." << std::endl;
}
} // namespace

int main()
{
    std::vector<uint16_t> a(ELEMENTS), b(ELEMENTS);
    std::vector<uint16_t> output(ELEMENTS), golden(ELEMENTS);
    for (uint32_t i = 0; i < DIM; ++i) {
        a[i * DIM + i] = HALF_ONE;
        b[i * DIM + i] = HALF_NEG_ONE;
        golden[i * DIM + i] = HALF_NEG_HALF;
    }
    aclInit(nullptr);
    aclrtSetDevice(0);
    uint16_t *a_device = nullptr, *b_device = nullptr;
    uint16_t* output_device = nullptr;
    aclrtMalloc(reinterpret_cast<void**>(&a_device), a.size() * sizeof(uint16_t), ACL_MEM_MALLOC_HUGE_FIRST);
    aclrtMalloc(reinterpret_cast<void**>(&b_device), b.size() * sizeof(uint16_t), ACL_MEM_MALLOC_HUGE_FIRST);
    aclrtMalloc(reinterpret_cast<void**>(&output_device), output.size() * sizeof(uint16_t), ACL_MEM_MALLOC_HUGE_FIRST);
    aclrtMemcpy(a_device, a.size() * sizeof(uint16_t), a.data(), a.size() * sizeof(uint16_t), ACL_MEMCPY_HOST_TO_DEVICE);
    aclrtMemcpy(b_device, b.size() * sizeof(uint16_t), b.data(), b.size() * sizeof(uint16_t), ACL_MEMCPY_HOST_TO_DEVICE);
    asc_set_l0c_copy_lrelu_alpha_kernel<<<1, 0>>>(a_device, b_device, output_device);
    aclrtSynchronizeDevice();
    aclrtMemcpy(output.data(), output.size() * sizeof(uint16_t), output_device, output.size() * sizeof(uint16_t), ACL_MEMCPY_DEVICE_TO_HOST);
    print_row("Input B row 0 (FP16 bits)", b);
    print_row("Leaky ReLU output row 0", output);
    print_row("Golden row 0", golden);
    const bool passed = output == golden;
    std::cout << (passed ? "[Success] asc_set_l0c_copy_lrelu_alpha applied alpha 0.5 to -1.0."
                         : "[Failed] asc_set_l0c_copy_lrelu_alpha result mismatch.") << std::endl;
    aclrtFree(a_device); aclrtFree(b_device); aclrtFree(output_device);
    aclrtResetDevice(0);
    aclFinalize();
    return passed ? 0 : 1;
}

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