We use essential cookies for the website to function, as well as analytics cookies for analyzing and creating statistics of the website performance. To agree to the use of analytics cookies, click "Accept All". You can manage your preferences at any time by clicking "Cookie Settings" on the footer. More Information.

Only Essential Cookies
Accept All

Assert

Function Description

When the tool is used for operator debugging, the assert function is supported. It is used to interrupt the running process immediately and print related information when the condition in the kernel code is false, facilitating quick fault locating.

NOTE

The maximum available space allocated to each core for printing data is fixed at 1 MB. If the printing size exceeds 1 MB, the printed content will not be displayed. Therefore, the amount of data to be printed needs to be controlled.

Procedure (CLI-based)

  1. In the kernel function code, call the assert API at the target position as required. For details, see the API description. The following is an example:

    int32_t x = 31;
    assert(x < 0, "Invalid input_num: %d\n", x);
  2. In the simulator debugging scenario, run the following command to enable the dump function:

    ascendebug kernel --backend simulator --dump-mode normal ... {other simulator debugging parameters}

    --Set dump-mode to normal to enable the general printing Scalar mode. Set other parameters by referring to NPU Debugging Parameters.

  3. View the assertion result.

    If the judgment condition is false, the program is interrupted and the assertion (with the file name and line number) is displayed on the screen. The following is an example:

    [ASSERT] /home/.../add_custom.cpp:94: Assertion `x < 0' Invalid input_num: 31

API Description

The assert API is described as follows:

  • Prototype: __aicore__ inline void assert(bool assertFlag, __gm__ const char* fmt, Args&&...args);

  • Function usage: Debugs programs. Checks whether a condition is true when the program is running. If the condition is false, the program is interrupted immediately and the information is printed.

  • Parameter (IN):

    • assertFlag: condition for determining whether to interrupt the program and print the assertion statement.
      • false: The program is interrupted and exits. The printed content passed by assert is displayed on the screen.
      • true: The program is not interrupted at assert and no information is displayed.
    • fmt: printing prefix, a constant string entered by the developer.
    • args: name of the variable to be printed.
  • Parameter (OUT): N/A

  • Return value: N/A

  • Restrictions:

    • Escape characters cannot be printed.
    • The supported types of printing on the board are the same as those in printf/PRINTF function.
  • Calling example:

    assert(input_num > 0, "Invalid input_num: %d ", input_num);
    • If input_num is 0, the program is interrupted and prints "[ASSERT] /path_to/add_custom_test.cpp:25 Invalid input_num: 0".
    • If input_num is greater than 0, the program is not interrupted at assert.
Search in Guides
Enter a keyword.