assert
Product Support
|
Product |
Supported |
|---|---|
|
|
√ |
|
|
√ |
|
|
√ |
|
|
√ |
|
|
x |
|
|
x |
Function
To avoid conflicts with the standard assert API in some scenarios, the assert(expr, __gm__ const char *fmt, Args&&... The args) API will be deprecated and removed in later versions. Do not use that API. You can use the corresponding ascendc_assert API instead.
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.
1 2 3 |
int assertFlag = 10; assert(assertFlag == 10); |
The printing function of assert affects the actual 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 in the debugging phase. You can disable the printing function by setting ASCENDC_DUMP to 0.
1 2 |
DumpHead: AIV-0, CoreType=AIV, block dim=8, total_block_num=8, block_remain_len=696, block_initial_space=1024, rsv=0, magic=5aa5bccd [ASSERT] /home/.../add_custom.cpp:44: Assertion `assertFlag != 10' |
1 2 |
DumpHead: AIV-0, CoreType=AIV, block dim=8, total_block_num=8, block_remain_len=696, block_initial_space=1024, rsv=0, magic=5aa5bccd [ASSERT]/home/.../add_custom.cpp:44: Assertion `assertFlag != 10' |
Prototype
1 2 |
assert(expr) assert(expr, __gm__ const char *fmt, Args&&... args) // This API has been deprecated. Use ascendc_assert instead. |
Parameters
|
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.
Note:
|
|
args |
Input |
Additional parameters (a parameter list with variable quantities and types). Depending on the fmt string, the function may require a series of additional parameters. Each parameter 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. |
Returns
None
Restrictions
- This function is supported only in the following scenarios:
- Calling a single-operator API (aclnnxxx) indirectly: single-operator calling in the PyTorch framework.
- Do not include assert.h of the system during kernel development. Otherwise, macro definition conflicts may occur.
- This API is called in the same way as that in the C language, and the AscendC namespace is not required.
- This API does not allow the printing of escape characters, except for newline characters.
- This API does not support the simulator mode.
- The total amount of data printed by a single call to this API cannot exceed 1 MB (including a small amount of header and footer information required by the framework, which can usually be ignored). If this limit is exceeded, the data will not be printed.
- When a custom operator project is used, note the following restrictions:
- This API uses the dump function. The total amount of data dumped by all APIs that use the dump function for an operator on each core cannot exceed 1 MB. You need to control the amount of data to be printed. If the limit is exceeded, no content will be printed.
- The space used by this API on each core cannot exceed 1 KB. You need to control the amount of data to be printed. If the limit is exceeded, no content will be printed.
Example
1 2 3 |
int assertFlag = 10; // Assert condition. assert(assertFlag != 10); |
Assert is triggered when the program is running. The print effect is as follows:
1
|
[ASSERT] /home/.../add_custom.cpp:44: Assertion `assertFlag != 10' |