【推理引擎】在 VS Code 调试 ONNXRuntime 的测试单元
阅读原文时间:2022年03月30日阅读:1

背景:在学习如何往ONNXRuntime中添加新算子时,参考了官方测试代码:onnxruntime/test/shared_lib/test_inference.cc,代码内部使用GTest作为单元测试工具。为了清楚地学习运行过程,一步一步地调试是不可缺少的。

开始调试前需要以Debug方式编译代码库,同时别忘了开启测试开关:

// cmake/CMakeLists.txt
...
option(onnxruntime_BUILD_UNIT_TESTS "Build ONNXRuntime unit tests" ON)
...

编译完成之后,在 build/Linux/Debug 文件夹下有一个可执行程序:onnxruntime_shared_lib_test,当然,文件夹下还有其它关于测试的可执行程序,比如onnxruntime_test_all、onnxruntime_perf_test、onnx_test_runner等等。

接着需要在 .vscode/launch.json 文件中添加调试信息:

{
    "configurations": [
    {
        "name": "(gdb) Launch",
        "type": "cppdbg",
        "request": "launch",
        "program": "${workspaceFolder}/build/Linux/Debug/onnxruntime_shared_lib_test",
        "args": [

        ],
        "stopAtEntry": false,
        "cwd": "${workspaceFolder}/onnxruntime/test/",
        "environment": [],
        "externalConsole": false,
        "MIMode": "gdb",
        "setupCommands": [
            {
                "description": "Enable pretty-printing for gdb",
                "text": "-enable-pretty-printing",
                "ignoreFailures": true
            },
            {
                "description":  "Set Disassembly Flavor to Intel",
                "text": "-gdb-set disassembly-flavor intel",
                "ignoreFailures": true
            }
        ]
    }
    ]
}

配置内容几乎都是自动生成的,我们只改动了其中两项:

  1. "program": "${workspaceFolder}/build/Linux/Debug/onnxruntime_shared_lib_test":配置调试程序的路径
  2. "cwd": "${workspaceFolder}/onnxruntime/test/":解决相对路径问题

至此,我们就可以“愉快地”开始接下来的调试任务了。