文档
注册

assert功能

功能介绍

使用工具进行算子调测时,支持断言功能。当核函数代码中条件判断为False时,则立即中断运行流程并打印相关信息,方便快速定位错误。

固定为每个核分配的打印数据的最大可使用空间为1M,目前该大小不支持修改,若打印超过1M,打印内容不再显示,请开发者控制待打印的数据量。

使用方法(命令行)

  1. 在核函数代码中按需在目标位置调用assert接口,接口说明参见assert接口说明,样例如下:
    int32_t x = 31; 
    assert(x < 0, "Invalid input_num: %d\n", x);
  2. NPU调测场景执行如下命令,使能Dump开关。
    ascendebug kernel --backend npu --dump-mode normal ... {其他NPU调测参数}

    --dump-mode取normal,开启通用打印Scalar模式,其他参数说明请参考NPU调测参数,用户按需配置即可。

  3. 查看断言结果。

    若调用时传入的判断条件为false,会中断程序并打屏显示断言内容(带文件名、行号),结果示例如下。

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

使用方法(API)

  1. 在核函数代码中按需在目标位置调用assert接口,接口说明参见assert接口说明,样例如下:
    int32_t x = 31; 
    assert(x < 0, "Invalid input_num: %d\n", x);
  2. 设置dump_mode='normal',调用算子编译、运行API接口。这里以标准自定义场景下实现NPU上板打印断言为例:
    compile_npu_options = ascendebug.CompileNpuOptions(dump_mode='normal')
    name, kernel_file, extern = op_executor.compile_custom_npu(customize_path, tiling_info.tiling_key, compile_npu_options)
    npu_compile_info = ascendebug.NpuCompileInfo(syncall=extern['cross_core_sync'], task_ration=extern['task_ration'], dump_mode='normal')
    run_npu_options = ascendebug.RunNpuOptions()
    op_executor.run_npu(kernel_file, run_npu_options, npu_compile_info=npu_compile_info, tiling_info=tiling_info)
  3. 查看断言结果。

    若调用时传入的判断条件为false,会中断程序并打屏显示断言内容(带文件名、行号),结果示例如下。

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

assert接口说明

表1 assert接口说明表

函数原型

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

函数功能

用于程序调试。在程序运行时检查一个条件是否为真,若条件为假,立即中断程序并打印信息。

参数(IN)

assertFlag

判断是否要中断程序打印断言语句的条件。

  • false:程序中断退出,打屏显示assert传入的打印内容。
  • true: 程序不会在assert中断,无打印。

fmt

用户输入常量字符串,作为打印的前缀修饰。

args

用户需要打印的变量名。

参数(OUT)

NA

-

返回值

NA

-

使用约束

调用示例

assert(input_num > 0, "Invalid input_num: %d ", input_num);
  • 若input_num==0,程序中断,打印内容为“[ASSERT] /path_to/add_custom_test.cpp:25 Invalid input_num: 0”。
  • 若input_num>0,程序不会在assert处中断。
搜索结果
找到“0”个结果

当前产品无相关内容

未找到相关内容,请尝试其他搜索词