检测内核调用符方式的Ascend C算子
操作步骤
内存检测示例说明
- 在步骤1之前,需要在Add 算子中构造一个非法读写的场景,将DataCopy内存拷贝长度从TILE_LENGTH 改为2 * TILE_LENGTH ,此时最后一次拷贝会发生内存读写越界。
__aicore__ inline void CopyOut(int32_t progress) { // deque output tensor from VECOUT queue LocalTensor<half> zLocal = outQueueZ.DeQue<half>(); // copy progress_th tile from local tensor to global tensor // 构造非法读写场景 DataCopy(zGm[progress * TILE_LENGTH], zLocal, 2 * TILE_LENGTH); // free output tensor for reuse outQueueZ.FreeTensor(zLocal); }
- 根据检测工具输出的报告,可以发现在add_custom.cpp的65行对GM存在 224 字节的非法写操作,与我们构造的异常场景对应。
$ mssanitizer --tool=memcheck ./add_npu ====== ERROR: illegal write of size 224 ====== at 0x12c0c002ef00 on GM ====== in block aiv(7) ====== code in pc current 0x1644 (serialNo:2342) ====== #0 ${ASCEND_HOME_PATH}/compiler/tikcpp/tikcfw/impl/dav_c220/kernel_operator_data_copy_impl.h:107:9 ====== #1 ${ASCEND_HOME_PATH}/compiler/tikcpp/tikcfw/inner_interface/inner_kernel_operator_data_copy_intf.cppm:155:9 ====== #2 ${ASCEND_HOME_PATH}/compiler/tikcpp/tikcfw/inner_interface/inner_kernel_operator_data_copy_intf.cppm:459:5 ====== #3 samples/operator/AddCustomSample/KernelLaunch/AddKernelInvocation/add_custom.cpp:65:9 ====== #4 samples/operator/AddCustomSample/KernelLaunch/AddKernelInvocation/add_custom.cpp:38:13 ====== #5 samples/operator/AddCustomSample/KernelLaunch/AddKernelInvocation/add_custom.cpp:82:8
竞争检测示例说明
- 在步骤1之前,需要在Add 算子中构造一个核间竞争的场景,将DataCopy内存拷贝长度从TILE_LENGTH 改为2 * TILE_LENGTH ,此时会在GM内存上存在核间竞争。
__aicore__ inline void CopyOut(int32_t progress) { // deque output tensor from VECOUT queue LocalTensor<half> zLocal = outQueueZ.DeQue<half>(); // copy progress_th tile from local tensor to global tensor // 构造核间竞争场景 DataCopy(zGm[progress * TILE_LENGTH], zLocal, 2 * TILE_LENGTH); // free output tensor for reuse outQueueZ.FreeTensor(zLocal); }
- 根据检测工具输出的报告,可以发现在add_kernel.cpp的65行,AIV的0核和1核存在核间竞争,符合我们构造的异常场景。
$ mssanitizer --tool=racecheck ./add_npu ====== ERROR: Potential WAW hazard detected at GM : ====== PIPE_MTE3 Write at WAW()+0x12c0c0025f00 in block 0 (aiv) at pc current 0x1644 (serialNo:305) ====== #0 ${ASCEND_HOME_PATH}/compiler/tikcpp/tikcfw/impl/dav_c220/kernel_operator_data_copy_impl.h:107:9 ====== #1 ${ASCEND_HOME_PATH}/compiler/tikcpp/tikcfw/inner_interface/inner_kernel_operator_data_copy_intf.cppm:155:9 ====== #2 ${ASCEND_HOME_PATH}/compiler/tikcpp/tikcfw/inner_interface/inner_kernel_operator_data_copy_intf.cppm:459:5 ====== #3 samples/operator/AddCustomSample/KernelLaunch/AddKernelInvocation/add_custom.cpp:65:9 ====== #4 samples/operator/AddCustomSample/KernelLaunch/AddKernelInvocation/add_custom.cpp:38:13 ====== #5 samples/operator/AddCustomSample/KernelLaunch/AddKernelInvocation/add_custom.cpp:82:8 ====== PIPE_MTE3 Write at WAW()+0x12c0c0026000 in block 1 (aiv) at pc current 0x1644 (serialNo:329) ====== #0 ${ASCEND_HOME_PATH}/compiler/tikcpp/tikcfw/impl/dav_c220/kernel_operator_data_copy_impl.h:107:9 ====== #1 ${ASCEND_HOME_PATH}/compiler/tikcpp/tikcfw/inner_interface/inner_kernel_operator_data_copy_intf.cppm:155:9 ====== #2 ${ASCEND_HOME_PATH}/compiler/tikcpp/tikcfw/inner_interface/inner_kernel_operator_data_copy_intf.cppm:459:5 ====== #3 samples/operator/AddCustomSample/KernelLaunch/AddKernelInvocation/add_custom.cpp:65:9 ====== #4 samples/operator/AddCustomSample/KernelLaunch/AddKernelInvocation/add_custom.cpp:38:13 ====== #5 samples/operator/AddCustomSample/KernelLaunch/AddKernelInvocation/add_custom.cpp:82:8
父主题: 典型案例