Implements the assert function in both the CPU domain and NPU domain for operators developed based on operator projects. During operator execution, if the internal assert condition is not true, the assert condition is output and the input information is formatted and printed on the screen.
Use assert to check the code where assertions need to be added to the implementation code on the kernel of the operator, and format the output of debugging information. An example is as follows:
int assertFlag = 10; assert(assertFlag == 10); assert(assertFlag == 10, "The assertFlag value is 10.\n"); assert(assertFlag == 10, "The assertFlag value is %d.\n", assertFlag);
The printing of assert APIs affects the runtime performance of the operator (each assert statement leads to an additional logic judgment by the system, and the number of assert statements used in the code determines the specific system performance). This function is usually used during debugging. Developers can disable the printing function in either of the following ways:
Custom operator project:
Modify the CMakeLists.txt file in the op_kernel directory of the operator project. Add the compilation option -DASCENDC_DUMP=0 to the first line to disable ASCENDC_DUMP.
When the assert condition is triggered, the CANN_VERSION_STR and CANN_TIMESTAMP values are automatically printed at the beginning of the assertion information. CANN_VERSION_STR and CANN_TIMESTAMP are macro definitions. CANN_VERSION_STR indicates the version number of the CANN package in the form of a string. CANN_TIMESTAMP indicates the timestamp when the CANN package is released, the value is in the format of uint64_t. Developers can directly use the two macros in the code. The following is an example of the assert information:
[ASSERT][CANN_VERSION : XXX.XX][TimeStamp : 20240807140556417] /home/.../add_custom.cpp:44: Assertion `assertFlag != 10' The assertFlag value is 10.
| Parameter | Input/Output | Description |
|---|---|---|
| expr | Input | Condition for asserting whether to terminate a program. If the value is true, the program continues to be executed. If the value is false, the program is terminated. |
| fmt | Input | Format control string, which contains two types of objects: common characters and conversion descriptions. - Common characters are printed as they are. - Conversion descriptions are not directly output. Instead, they are used to control the conversion and printing of parameters in printf. Each conversion description starts with a percent (%) character and ends with the specific conversion description, indicating the type of the output data. - The supported conversion types are as follows: - %d / %i: outputs a decimal number. - %f: outputs a real number. - %x: outputs a hexadecimal integer. - %s: outputs a string. - %u: outputs unsigned data. - %p: outputs the pointer address.
Currently, the supported data types include uint8_t, int8_t, int16_t, uint16_t, int32_t, uint32_t, int64_t, uint64_t, float, and half. |
| args | Input | Additional arguments (an output list with variable quantities and types). Depending on the fmt string, the function may require a series of additional arguments. Each argument contains a value to be inserted and replaces each % tag specified in the fmt parameter. The number of parameters must be the same as the number of % tags. |
Kirin 9020 series processors
Kirin 9030 series processors
Kirin X90 series processors
This function is supported only in the following scenario: Operators are called in Project-based Operator Development mode.
Do not include assert.h of the system during kernel development. Otherwise, macro definition conflicts may occur.
The assert API is called in the same way as the C language, and the AscendC namespace is not required.
This API does not allow the printing of escape characters, except for newline characters.
The sum size of the space used by the assert call, printf call, DumpTensor call, DumpAccChkPoint call, and framework dump function cannot exceed 1 MB on each core. Developers need to control the amount of data to be printed. If the limit is exceeded, no content will be printed.
Only the assert API is called in the program. The space used by the assert API on each core cannot exceed 1 KB. Developers need to control the amount of data to be printed. If the limit is exceeded, no content will be printed.
int assertFlag = 10; // Assert condition. assert(assertFlag == 10); // Print messages. assert(assertFlag == 10, "The assertFlag value is 10.\n"); // Format the print. assert(assertFlag == 10, "The assertFlag value is %d.\n", assertFlag); assert(assertFlag != 10, "The assertFlag value is %d.\n", assertFlag);
Assert is triggered when the program is running. The print effect is as follows:
[ASSERT][CANN_VERSION : XXX.XX][TimeStamp : 20240807140556417] /home/.../add_custom.cpp:44: Assertion `assertFlag != 10' The assertFlag value is 10.