| Files | Last commit | Last update |
|---|---|---|
| 2 months ago | ||
| 2 months ago | ||
| 1 month ago | ||
| 1 month ago | ||
| 2 months ago | ||
| 2 months ago |
Subnormal Compatibility Example
Overview
This example uses the Ln interface to demonstrate the Subnormal computation mode compatibility adaptation of vector compute interfaces, isolating different hardware implementations through compile-time macros.
SubNormal floating-point numbers are those with all exponent bits set to 0 and a non-zero mantissa, used to represent values smaller than the minimum normal number. The 3510 architecture version does not support Subnormal by default; Subnormal floating-point numbers are treated as 0 in computation (FTZ, Flush To Zero).
- Atlas A2/A3 Training/Inference Series Products: The hardware supports Subnormal by default. No extra configuration is required when calling the
Lninterface. - Ascend 950PR&950DT products: Subnormal is not supported by default. The
algoparameter ofLnConfigmust be set toLnAlgo::PRECISION_1ULP_FTZ_FALSEso that Subnormal computation results are preserved through software simulation.
Supported Products and CANN Versions
| Product | CANN Version |
|---|---|
| Ascend 950PR&950DT products | >= CANN 9.1.0 |
| Atlas A3 products | >= CANN 9.0.0 |
| Atlas A2 products | >= CANN 9.0.0 |
Directory Structure
├── subnormal
│ ├── scripts
│ │ ├── gen_data.py // Input data and ground truth generation script
│ │ └── verify_result.py // Verify whether output data matches ground truth
│ ├── CMakeLists.txt // Build project file
│ ├── data_utils.h // Data read/write functions
│ ├── subnormal_custom.asc // Ascend C example implementation & invocation example
│ └── README.md // Example documentation
Example Specifications
| Category | name | shape | data type | format |
|---|---|---|---|---|
| Example Input | x | [1, 1024] | half | ND |
| Example Output | z | [1, 1024] | half | ND |
| Kernel Function Name | subnormal_custom |
Note: The input data
xcontains Subnormal half values (all exponent bits 0, non-zero mantissa, in the range(0, 2^-14)i.e.(0, 6.10e-5)). The ground truthzis the natural logarithm of the input computed at high precision, preserving the true logarithm of Subnormal inputs, instead of the-infobtained when Subnormals are approximated as 0.
Example Implementation
The data flow is: GM -> UB (Unified Buffer) -> GM.
- Copy the input data from GM to UB.
- Call the
Lninterface to compute the natural logarithm, using different configurations depending on the architecture to preserve Subnormal computation results:- Atlas A2/A3 Training/Inference Series Products: Call the
Lninterface directly; the hardware supports Subnormal by default. - Ascend 950PR&950DT products: Configure
algoofLnConfigtoLnAlgo::PRECISION_1ULP_FTZ_FALSEand then call theLninterface; Subnormal computation results are preserved through software simulation.
- Atlas A2/A3 Training/Inference Series Products: Call the
- Copy the computation result from UB back to GM.
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.shNote:
${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 this example directory.
mkdir -p build && cd build; cmake -DCMAKE_ASC_ARCHITECTURES=dav-2201 ..;make -j; python3 ../scripts/gen_data.py ./demo python3 ../scripts/verify_result.py output/output.bin output/golden.binTo use CPU debug or NPU simulation mode, add the
-DCMAKE_ASC_RUN_MODE=cpuor-DCMAKE_ASC_RUN_MODE=simparameter.Examples:
cmake -DCMAKE_ASC_RUN_MODE=cpu -DCMAKE_ASC_ARCHITECTURES=dav-2201 ..;make -j; # CPU debug mode cmake -DCMAKE_ASC_RUN_MODE=sim -DCMAKE_ASC_ARCHITECTURES=dav-2201 ..;make -j; # NPU simulation modeTo build for Ascend 950PR&950DT products:
cmake -DCMAKE_ASC_ARCHITECTURES=dav-3510 ..;make -j; # Build project (Ascend 950PR&950DT products)Notice: Clear the cmake cache before switching build modes. Run
rm CMakeCache.txtin the build directory and then re-run cmake. -
Build option description
Option Values Description CMAKE_ASC_RUN_MODEnpu(default),cpu,simRun mode: NPU execution, CPU debug, NPU simulation CMAKE_ASC_ARCHITECTURESdav-2201(default),dav-3510NPU architecture: dav-2201 corresponds to Atlas A2 products and Atlas A3 products, dav-3510 corresponds to Ascend 950PR&950DT products -
Execution results
The following execution result indicates that the accuracy comparison succeeded:
test pass!