CPU debugging for operators can be preferred if you want to quickly debug code logic. Common printing and GDB debugging methods can be used to quickly locate code problems.
CPU debugging can be used before operator running on the NPU board, to preliminarily locate operator accuracy issues and improve the success rate of NPU board running.
CPU twin debugging generates the required binary .bin file based on your input, and then automatically compiles and runs operators. Table 1 lists the debugging items supported in this phase.
Table 1 CPU debugging functions
| Function Name | Function Description |
|---|---|
| Automatic accuracy comparison | If golden data is configured, the tool automatically compares the accuracy of the actual debugging result with that of the golden data. |
| printf/PRINTF | Displays scalar data, such as constants and character strings. For details, see printf/PRINTF Function. |
| DumpTensor | Dumps tensor data. The function and artifacts are similar to those of the dump function on the NPU board. For details, see DumpTensor Function. |
| DumpAccChkPoint | Dumps tensor data at the offset position. For details, see DumpAccChkPoint Function. |
| Assert | Displays assertions on the screen. For details, see Assert Function. |
| CCE instruction stream printing | This function is enabled by default. For details about the CCE instruction stream file, see Artifact Description. |
| GDB debugging | You can use GDB to debug the operator computation precision step by step. For details, see GDB Debugging. |
The key steps for performing CPU debugging using the CLI are as follows:
Set up the environment and prepare the input/golden data file.
Run the following command to perform CPU debugging. Only key parameters are provided. For details about other parameters, see CPU Debugging Parameters.
ascendebug kernel --backend cpu --chip-version ${chip_version} --repo-type customize --json-file ${op_config_json_file} --core-type ${core_type} --work-dir ${work_dir}... { Other parameters} To view all parameters involved in CPU debugging, you can use ascendebug kernel -h or ascendebug kernel --help.
View the result file. For details, see Artifact Description.
The CPU debugging result is stored in the ${root}/${work_dir}/cpu directory. ${root} indicates the current operation path, ${work_dir} indicates the debugging workspace which defaults to the /debug_workspace/${op_type} directory, and ${op_type} indicates the operator name. The directory structure is as follows:
├ ${op_type} // Operator name
├── cpu
│ ├── build // Directory for storing the intermediate files generated after CPU compilation
│ │ ├── cceprint // Directory for storing the output files of the CCE instruction stream
│ │ │ └── auto_gen_add_custom_kernel_0_0_mix.cce // CCE instruction stream file
│ ├── xxx_cpu // Executable program of the operator generated after CPU compilation
│ ├── output // Directory for storing the output file and accuracy comparison result file after CPU compilation and running
│ ├── y.bin // Original data output by running
│ ├── y.txt // Accuracy comparison result file
│ ├── src // Directory for storing the temporary code files generated after CPU compilation
│ ├── CMakeLists.txt
│ ├── data_definition.txt
│ ├── add_custom_main.cpp
│ ├── add_custom_tiling.h
│ ├── _gen_kernel_${op_type}.cpp
│ ├── dump // Directory for storing dump files
│ ├── PARSER_${timestamp}
│ ├── dump_data
│ ├──0 // Core ID
│ ├──index_1 // index is the unique identifier of the dump API description.
│ ├──core_0_index_1_loop_0.bin
│ ├──core_0_index_1_loop_0.txt Viewing the accuracy comparison result
In the output directory, check whether the output file (.bin) and accuracy comparison file (.txt) are generated.
Confirm the operator accuracy comparison result based on the accuracy comparison file (.txt).
The following shows an example of the accuracy comparison result, including the mean value, partial difference comparison, and final comparison conclusion (success or failure) of two data copies. If the result is failure, the data with the maximum difference is displayed.
data_cmp mean is -1.41e-05 data_gd mean is -1.41e-05 split_count:2359296.0; max_diff_hd:0.1; --------------------------------------------------------------------------------------- Loop ExpectOut RealOut FpDiff RateDiff --------------------------------------------------------------------------------------- 00000001 0.0395813 0.0395813 0.0000000 0.0000000 00000002 0.0160980 0.0160980 0.0000000 0.0000000 00000003 -0.0443420 -0.0443420 0.0000000 0.0000000 00000004 -0.0847778 -0.0847778 0.0000000 0.0000000 00000005 -0.0066605 -0.0066605 0.0000000 0.0000000 00000006 0.0880737 0.0880737 0.0000000 0.0000000 00000007 0.0848389 0.0848389 0.0000000 0.0000000 00000008 0.1083374 0.1083374 0.0000000 0.0000000 00000009 0.0838623 0.0838623 0.0000000 0.0000000 00000010 0.0887451 0.0887451 0.0000000 0.0000000 00000011 0.0572205 0.0572205 0.0000000 0.0000000 00000012 0.0741577 0.0741577 0.0000000 0.0000000 00000013 -0.0762329 -0.0762329 0.0000000 0.0000000 00000014 -0.0957642 -0.0957642 0.0000000 0.0000000 00000015 0.0102234 0.0102234 0.0000000 0.0000000 ... ... ... ... ... --------------------------------------------------------------------------------------- DiffThd PctThd PctRlt Result --------------------------------------------------------------------------------------- 0.0050 99.50% 100.000000% Pass Success Success Success Success Success
Table 2 Description of the accuracy comparison result
| Item | Description |
|---|---|
| data_cmp mean | Mean value of the output data. |
| data_gd mean | Mean value of the golden data. |
| split_count | Count of output data records. |
| max_diff_hd | Maximum difference threshold between the output data and golden data. |
| Detailed comparison data display (partial) | Loop (data position), ExpectOut (expected output value), RealOut (actual output value), FpDiff (absolute difference value), and RateDiff (relative difference value). |
| Overall comparison result display | DiffThd (relative difference threshold), PctThd (percentage threshold of data whose accuracy meets the requirement), PctRlt (percentage of data whose accuracy meets the requirement), and Result (comparison result). |
| Error line display | If the accuracy comparison result is Failed, detailed information about some data with large errors is displayed. The information format is the same as that of the accuracy comparison result. |
(Optional) Viewing the dump result
If the DumpTensor Function or DumpAccChkPoint Function is enabled, the result file is stored in the dump directory. For details about the result directory, see Artifact Description.