CANN/asc-devkit DeepNorm归一化样例

DeepNorm Example

【免费下载链接】asc-devkit 本项目是CANN 推出的昇腾AI处理器专用的算子程序开发语言,原生支持C和C++标准规范,主要由类库和语言扩展层构成,提供多层级API,满足多维场景算子开发诉求。 【免费下载链接】asc-devkit 项目地址: https://gitcode.com/cann/asc-devkit

Overview

This example uses the DeepNorm high-level API provided by Ascend C in a deep neural network training scenario to implement DeepNorm normalization. This API performs LayerNorm normalization while improving training stability of deep Transformer networks by scaling the residual connection (alpha coefficient).

Supported Products and CANN Software Versions

ProductCANN Software Version
Ascend 950PR/Ascend 950DT>= CANN 9.1.0
Atlas A3 Training Series Products/Atlas A3 Inference Series Products>= CANN 9.0.0
Atlas A2 Training Series Products/Atlas A2 Inference Series Products>= CANN 9.0.0

Directory Structure

├── deepnorm
│   ├── scripts
│   │   ├── gen_data.py         // Script for generating input data and ground truth data
│   ├── CMakeLists.txt          // Build project file
│   ├── data_utils.h            // Data read/write functions
│   ├── deepnorm.asc            // Ascend C example implementation & invocation example
│   └── README.md               // Example documentation

Example Description

  • Example function:
    This example implements DeepNorm normalization for input data with shape [B, S, H]. The computation formula is: $$ DeepNorm(x) = LayerNorm(\alpha * X + SubLayer(X)) $$

  • Example specifications:

    Example Type (OpType) deepnorm
    Example Input
    nameshapedata typeformat
    inputX[4, 16, 64]floatND
    inputGx[4, 16, 64]floatND
    beta[1, 64]floatND
    gamma[1, 64]floatND
    Example Output
    output[4, 16, 64]floatND
    outputMean[4, 16]floatND
    outputVariance[4, 16]floatND
    Kernel Function Namedeepnorm_custom
  • Example implementation:

    • Kernel implementation

      The computation logic is:
      Use the DeepNorm high-level API to complete the deepnorm computation, obtain the final result, and transfer it to external storage. For detailed API information, refer to DeepNorm.

    • Tiling implementation

      The tiling implementation process is as follows:

      1. Call AscendC::GetDeepNormMaxMinTmpSize to obtain and use the minimum temporary space size required by the DeepNorm API.
      2. Call AscendC::GetDeepNormTilingInfo to obtain the tiling parameters required by the kernel-side API based on the input shape and workspace size.
    • Invocation implementation
      Use the kernel invocation operator <<<>>> to call the kernel function.

Build and Run

Run the following steps in the root directory of this example to build and run the example.

  • Configure environment variables
    Configure environment variables based on the installation method of the CANN development kit in the current environment.

    source ${install_path}/cann/set_env.sh
    

    Note: ${install_path} is the CANN package installation directory. When no installation directory is specified, the default installation path is /usr/local/Ascend.

  • Run the example

    Run the following commands in the example directory.

    mkdir -p build && cd build;   # Create and enter the build directory
    cmake ..;make -j;             # Build the project (NPU mode by default)
    python3 ../scripts/gen_data.py   # Generate test input data
    ./demo                        # Run the compiled executable to execute the example
    

    To use CPU debugging or NPU simulation mode, add the -DCMAKE_ASC_RUN_MODE=cpu or -DCMAKE_ASC_RUN_MODE=sim parameter.

    For example:

    cmake -DCMAKE_ASC_RUN_MODE=cpu -DCMAKE_ASC_ARCHITECTURES=dav-2201 ..;make -j; # CPU debugging mode
    cmake -DCMAKE_ASC_RUN_MODE=sim -DCMAKE_ASC_ARCHITECTURES=dav-2201 ..;make -j; # NPU simulation mode
    

    Notice: Before switching the build mode, clear the cmake cache by running rm CMakeCache.txt in the build directory and then re-run cmake.

  • Build option description

    OptionValuesDescription
    CMAKE_ASC_RUN_MODEnpu (default), cpu, simRun mode: NPU run, CPU debug, NPU simulation
    CMAKE_ASC_ARCHITECTURESdav-2201 (default), dav-3510NPU architecture: dav-2201 corresponds to Atlas A2 Training Series Products/Atlas A2 Inference Series Products and Atlas A3 Training Series Products/Atlas A3 Inference Series Products; dav-3510 corresponds to Ascend 950PR/Ascend 950DT

    The following execution result indicates that the precision comparison is successful.

    test pass!
    

【免费下载链接】asc-devkit 本项目是CANN 推出的昇腾AI处理器专用的算子程序开发语言,原生支持C和C++标准规范,主要由类库和语言扩展层构成,提供多层级API,满足多维场景算子开发诉求。 【免费下载链接】asc-devkit 项目地址: https://gitcode.com/cann/asc-devkit

创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值