针对Ascend C算子内存检测

操作说明

  1. 接口打桩。

    需要打桩的代码文件可以分为编译阶段和链接阶段。

    • 编译阶段,需添加编译选项-g --cce-enable-sanitizer,具体操作请参考环境准备
    • 链接阶段,需添加链接选项--cce-enable-sanitizer --cce-aicore-arch={ARCH} ,具体操作请参考环境准备

  2. 参考工具准备完成相关环境变量配置。
  3. 构建单算子可执行文件。

    Add算子可执行文件的构建命令示例如下:

    bash run.sh add_custom <soc_version> VectorCore npu

    一键式编译运行脚本完成后,在工程目录下生成NPU侧可执行文件add_custom_npu

  4. 使用mssanitizer检测工具拉起单算子可执行文件,具体参数说明可参考表1表2

    mssanitizer --tool=memcheck --leak-check=yes ./add_custom_npu

    单算子可执行文件add_custom_npu所在路径可配置为绝对路径或相对路径,请根据实际环境配置。

  5. 内存检测过程请参考示例说明

示例说明

  1. 步骤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);
        }
  2. 根据检测工具输出的报告,可以发现在add_custom.cpp的63行对GM存在 256 字节的非法读操作,与我们构造的异常场景对应。
    $ mssanitizer --tool=memcheck --leak-check=yes ./add_custom_npu
    [mssanitizer] logging to file: ./mssanitizer_20240124182331_37743.log
    ====== ERROR: illegel read of size 256
    ======    at 0x124080022f00 on GM
    ======    code in add_custom.cpp:63