assert功能
功能介绍
使用工具进行算子调测时,支持断言功能。当核函数代码中条件判断为False时,则立即中断运行流程并打印相关信息,方便快速定位错误。
 
 固定为每个核分配的打印数据的最大可使用空间为1M,目前该大小不支持修改,若打印超过1M,打印内容不再显示,请开发者控制待打印的数据量。
使用方法(命令行)
- 在核函数代码中按需在目标位置调用assert接口,接口说明参见表1,样例如下:int32_t x = 31; assert(x < 0, "Invalid input_num: %d\n", x); 
- NPU调测场景执行如下命令,使能Dump开关。ascendebug kernel --backend npu --dump-mode normal ... {其他NPU调测参数}--dump-mode取normal,开启通用打印Scalar模式,其他参数参考NPU调测参数按需配置。 
- 查看断言结果。若调用时传入的判断条件为false,会中断程序并打屏显示断言内容(带文件名、行号),结果示例如下。 [ASSERT] /home/.../add_custom.cpp:94: Assertion `x < 0' Invalid input_num: 31 
使用方法(API)
- 在核函数代码中按需在目标位置调用assert接口,接口说明参见表1,样例如下:int32_t x = 31; assert(x < 0, "Invalid input_num: %d\n", x); 
- 调用算子编译及运行接口,对应options配置为设置dump_mode='normal'。以标准自定义场景下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) 
- 查看断言结果。若调用时传入的判断条件为false,会中断程序并打屏显示断言内容(带文件名、行号),结果示例如下。 [ASSERT] /home/.../add_custom.cpp:94: Assertion `x < 0' Invalid input_num: 31 
接口说明
| 函数原型 | __aicore__ inline void assert(bool assertFlag, __gm__ const char* fmt, Args&&... args); | |
| 函数功能 | 用于程序调试。在程序运行时检查一个条件是否为真,若条件为假,立即中断程序并打印信息。 | |
| 参数(IN) | assertFlag | 判断是否要中断程序打印断言语句的条件。 
 | 
| fmt | 用户输入常量字符串,作为打印的前缀修饰。 | |
| args | 用户需要打印的变量名。 | |
| 参数(OUT) | NA | - | 
| 返回值 | NA | - | 
| 使用约束 | 
 | |
| 调用示例 | assert(input_num > 0, "Invalid input_num: %d ", input_num); 
 | |
父主题: 更多功能