CANN/asc-devkit Subnormal示例

Subnormal Example

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

Overview

This example demonstrates the behavioral differences of the Div operation in Ascend C Reg vector computation when Subnormal mode is enabled or disabled. Through two different Div precision algorithm configurations (PRECISION_1ULP_FTZ_FALSE and PRECISION_1ULP_FTZ_TRUE), it shows the impact of Subnormal floating-point number support on computation results.

Supported Products and CANN Versions

ProductCANN Version
Ascend 950PR/Ascend 950DT>= CANN 9.1.0

Directory Structure

subnormal
│   ├── scripts/             // Test script directory
│   │   └── gen_data.py      // Generate test input and golden data
│   ├── CMakeLists.txt       // Build configuration file
│   ├── data_utils.h         // Data read/write utility functions
│   ├── subnormal.asc        // Ascend C operator implementation & invocation example
│   └── README.md            // Example description document

Example Description

  • Example functionality:

    The example computes float type data, using PRECISION_1ULP_FTZ_FALSE and PRECISION_1ULP_FTZ_TRUE to configure DivSpecificMode as the precision algorithm parameter for the Div operation, executing division. The computation formula is as follows: $$z_i = \frac{x_i}{y_i}$$

  • Example specifications

    Example Type (OpType)AIV Example
    Example Inputnameshapedata type
    x[1024]float
    y[1024]float
    Example Outputz[1024]float
    Kernel Function Namesubnormal
  • Example implementation

    • Implementation flow:

      1. Allocate memory on the Host side and initialize input data
      2. Transfer data from Host memory to Device global memory
      3. Call the kernel function to execute computation on the Vector core
      4. Inside the kernel function: transfer input data from Global Memory to Unified Buffer via DataCopy
      5. Call the VF (Vector Function) function via asc_vf_call on the Unified Buffer
      6. Inside the VF function, use the DivSpecificMode template parameter to perform LoadAlign, Div, and StoreAlign operations
      7. Transfer computation results from Unified Buffer back to Global Memory
      8. Perform precision verification and golden data comparison on the Host side
    • Subnormal and FTZ concept description:

      • Subnormal: Floating-point numbers where the exponent is all zeros and the mantissa is not all zeros, representing extremely small values close to 0. The IEEE 754 standard supports Subnormal numbers, providing better numerical precision and gradual underflow characteristics.
      • FTZ: A mode that forces Subnormal numbers to 0. Although it simplifies hardware design and improves performance, it sacrifices numerical precision.
    • Multi-scenario description:

      • Scenario 1 (SCENARIO_NUM=1): Use PRECISION_1ULP_FTZ_FALSE as the precision algorithm parameter for Div

        • Implementation function: SubnormalFTZFalseVF
        • Implementation:
          static constexpr AscendC::Reg::DivSpecificMode mode = {ZEROING, false, PRECISION_1ULP_FTZ_FALSE};
          AscendC::Reg::Div<T, &mode>(zReg, xReg, yReg, mask);
          
        • Description: Supports Subnormal data computation, uses a single instruction to compute the result, with a maximum precision error of 1 ulp. When the division result is a Subnormal number, the value is preserved.
        • Application scenarios: Scientific computing, numerical simulation, and high-precision computation scenarios that require precise handling of Subnormal floating-point numbers.
      • Scenario 2 (SCENARIO_NUM=2): Use PRECISION_1ULP_FTZ_TRUE as the precision algorithm parameter for Div

        • Implementation function: SubnormalFTZTrueVF
        • Implementation:
          static constexpr AscendC::Reg::DivSpecificMode mode = {ZEROING, false, PRECISION_1ULP_FTZ_TRUE};
          AscendC::Reg::Div<T, &mode>(zReg, xReg, yReg, mask);
          
        • Description: Does not support Subnormal data computation. FTZ mode uses a single instruction to compute the result, with a maximum precision error of 1 ulp. When the division result is a Subnormal number, it is forced to 0.
        • Application scenarios: Deep learning, image processing, and real-time inference scenarios where Subnormal precision requirements are not high, simplifying hardware implementation and improving computation performance.
      • Computation data scenario comparison:

        No.Div CombinationDividendDivisorScenario 1 (golden match)AnalysisScenario 2 Output (precision loss)Analysis
        1N / N = S$2^{-126}$4.00x00200000Result is subnormal $2^{-128}$0x00000000Result falls in Subnormal range, forced to zero
        2N / S = N$2^{-125}$$2^{-127}$0x40800000Result is normal $2^{2}$0x7f800000Divisor S treated as 0, any non-zero positive divided by 0 is infinity
        3S / N = S$2^{-130}$2.00x00040000Result is subnormal $2^{-131}$0x00000000Dividend S treated as 0 on input, result is 0
        4S / N = N$2^{-130}$$2^{-20}$0x08800000Result is normal $2^{110}$0x00000000Dividend S treated as 0 on input, result is 0
        5S / S = N$2^{-140}$$2^{-145}$0x42000000Result is normal $2^{5}$0x7fffffffBoth dividend S and divisor S treated as 0, $0 \div 0$, result is NaN
        • N: normal data, normal number.
        • S: Subnormal data, subnormal number.
    • Constraints:

      • Input data length must be a multiple of GetVecLen()
      • The divisor cannot be 0. In practical applications, a check for zero divisors is required
      • The example currently only supports the float data type

Build and Run

  • Configure environment variables Configure environment variables based on the installation method of the CANN development kit on 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.

    # Scenario 2: Use PRECISION_1ULP_FTZ_TRUE (FTZ mode)
    SCENARIO_NUM=2
    mkdir -p build && cd build;                                               # Create and enter the build directory
    cmake .. -DSCENARIO_NUM=$SCENARIO_NUM; make -j;                           # Build the project
    python3 ../scripts/gen_data.py;                                           # Generate test input data
    ./demo                                                                    # Run the example
    

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

    Examples:

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

    Notice: Clear the cmake cache before switching build modes. Run rm CMakeCache.txt in the build directory and re-run cmake.

  • Build option description

    OptionValuesDescription
    SCENARIO_NUM1 (default), 21: Supports subnormal computation;
    2: Does not support subnormal computation;
    CMAKE_ASC_RUN_MODEnpu (default), cpu, simRun mode: NPU execution, CPU debug, NPU simulation
    CMAKE_ASC_ARCHITECTURESdav-3510NPU architecture: dav-3510 corresponds to Ascend 950PR/Ascend 950DT
  • Execution result 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、付费专栏及课程。

余额充值