assert

Supported Products

Product

Supported/Unsupported

Atlas A3 training products / Atlas A3 inference products

Atlas A2 training products / Atlas A2 inference products

Atlas 200I/500 A2 inference products

Atlas inference product 's AI Core

Atlas inference product 's Vector Core

x

Atlas training products

x

Functions

To avoid conflicts with the standard assert API in some scenarios, assert(expr, __gm__ const char *fmt, Args&&... The args) API will be deprecated and removed in later versions. Do not use this API. You can use the corresponding API of ascendc_assert as the replacement.

Implements the assert function in CPU/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. The following is an example.
1
2
3
int assertFlag = 10;

assert(assertFlag == 10);

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. You can disable the print function by setting ASCENDC_DUMP to 0.

The following is an example of the assert print information in the NPU domain (the DumpHead information is printed only when the custom operator project is used):
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'
The following is an example of the assert print information in the CPU domain:
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.

  • 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 ASON software supports conversion between the following ASON services and traditional services:
    • %d / %i: outputs decimal numbers. The data types that can be printed are bool/int8_t/int16_t/int32_t/int64_t.
    • %f: outputs a real number. The supported data types are float and half.
    • %x: outputs hexadecimal integers. The data types that can be printed are int8_t/int16_t/int32_t/int64_t/uint8_t/uint16_t/uint32_t/uint64_t.
    • %s: outputs character strings.
    • %u: outputs unsigned data. The data types that can be printed are bool/uint8_t/uint16_t/uint32_t/uint64_t.
    • %p: outputs pointer addresses.
Note:
  • The preceding data types are supported by the NPU domain for debugging. In the CPU domain, the supported data types are the same as those in the C/C++ specifications.
  • When the conversion type is %x, that is, a hexadecimal integer is output, the output in the NPU domain is 64 bits, and the output in the CPU domain is 32 bits.

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.

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.
  • The kernel development must not contain the system assert.h. Otherwise, macro definition conflicts will occur.
  • The calling form of this API is the same as that of the C language. Therefore, the AscendC namespace is not required.
  • This API does not support the printing of escape characters except newline characters.
  • This API does not support the simulator mode.
  • The total size of data printed by a single call to this API cannot exceed 1 MB (including the header and trailer information required by the framework, which can be ignored). If the limit is exceeded, the data will not be printed.
  • When using the custom operator project, pay attention to the following restrictions:
    • This API uses the dump function. The total size of data dumped by all APIs that use the dump function of 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.

Examples

1
2
3
int assertFlag = 10;
// Assert condition
assert(assertFlag != 10);

Assert is triggered when the program is running. The print is as follows:

1
[ASSERT] /home/.../add_custom.cpp:44: Assertion `assertFlag != 10'