核函数NPU上板打印
本场景以AddCustom算子为例,通过torch生成输入数据和标杆数据,核函数NPU上板打印过程如下。请根据自身实际情况,按需修改示例代码。
import torch
import numpy as np
import ascendebug
# 设置和清理日志文件
ascendebug.set_log_file('test.log', clean=True)
# 1.生成输入/标杆数据
x = torch.rand(size=(1, 16384), dtype=torch.float16)
y = torch.rand(size=(1, 16384), dtype=torch.float16)
z = x + y
# 2.构建算子信息
debug_op = ascendebug.create_debug_op('add_custom', 'VectorCore', '${chip_version}') \
.scalar_input('tileNumIn', 'uint32', 10) \
.tensor_input('x', x) \
.tensor_input('y', y) \
.tensor_output('z', z)
# 3.创建调试对象并初始化工作空间
install_pkg = "/home/run_pkg/"
op_executor = ascendebug.create_op_executor(debug_op=debug_op, install_path=install_pkg)
# 4.配置核函数源码信息
kernel_info = ascendebug.OpKernelInfo("/src_path/add_custom.cpp", 'add_custom', [])
# 5.调用NPU编译接口,打开通用的printf/PRINTF/DumpTensor/assert打印功能
npu_option = ascendebug.CompileNpuOptions(dump_mode='normal')
kernel_name, kernel_file, extern= op_executor.compile_call_kernel_npu(kernel_info, npu_option)
# 6.调用NPU运行接口,打开通用的printf/PRINTF/DumpTensor/assert打印功能
run_npu_options = ascendebug.RunNpuOptions(block_num=32)
npu_compile_info = ascendebug.NpuCompileInfo(syncall=extern['cross_core_sync'], task_ration=extern['task_ration'], dump_mode='normal')
op_executor.run_npu(kernel_file, npu_options=run_npu_options, npu_compile_info=npu_compile_info)
算子NPU上板调测时打印的Scalar、Tensor、assert信息示例可以参见更多功能。
父主题: 精度调试
