memo: LibTorch | Debug

Table of contents

C++ Extensions - Docs

Install LibTorch

libtorch is the C++ API for PyTorch. Guide: INSTALLING C++ DISTRIBUTIONS OF PYTORCH

  1. Download the binary files with modified URL for specified version:

    1
    2
    3
    4
    5
    6
    7
    
    # Download here (cxx11 ABI):
    wget https://download.pytorch.org/libtorch/cu116/libtorch-cxx11-abi-shared-with-deps-1.12.1%2Bcu116.zip
    
    unzip libtorch-cxx11-abi-shared-with-deps-1.12.1+cu116.zip
    
    # Optional
    sudo mv libtorch/ /usr/local/
    
  2. Create folder “example-app”, and write code into “example-app.cpp”

    1
    2
    3
    4
    5
    6
    7
    8
    
    #include <torch/torch.h>
    #include <iostream>
    
    int main(){
        torch::Tensor tensor = torch::rand({2,3});
        std::cout << tensor << std::endl;
        std::cout << "size of Tensor type: "<< sizeof(torch::Tensor) << std::endl;
    }
    
  3. Set dependence for LibTorch via CMakeLists.txt:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    
    cmake_minimum_required(VERSION 3.18 FATAL_ERROR)
    project(MyLibTorchApp)    # name
    
    find_package(Torch REQUIRED)
    set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${TORCH_CXX_FLAGS}")
    
    add_executable(MyLibTorchApp main.cpp)
    target_link_libraries(MyLibTorchApp "${TORCH_LIBRARIES}")
    set_property(TARGET MyLibTorchApp PROPERTY CXX_STANDARD 17)
    
  4. Build (externally) inside the dir “example-app/build”

    1
    2
    3
    4
    
    # mkdir build
    # cd build
    cmake -DCMAKE_PREFIX_PATH=/absolute/path/to/libtorch ..
    cmake --build . --config Release
    
  5. (Optional) For C/C++ extension, set up includePath in “c_cpp_properties.json” for correct intellisense:

     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    
    {
        "configurations": [
            {
                "name": "Linux",
                "includePath": [
                    "${workspaceFolder}/**",
                    "/usr/local/libtorch",
                    "/home/yi/anaconda3/envs/AIkui/include/python3.10",
                    "/home/yi/anaconda3/envs/AIkui/lib/python3.10/site-packages/torch/include",
                    "/home/yi/anaconda3/envs/AIkui/lib/python3.10/site-packages/torch/include/torch/csrc/api/include"
                ],
                "defines": [],
                "compilerPath": "/usr/bin/g++",
                "cStandard": "c17",
                "cppStandard": "gnu++14",
                "intelliSenseMode": "linux-gcc-x64"
            }
        ],
        "version": 4
    }
    

Debug LibTorch

(2023-11-12)

launch.json

Modify launch.json in .vscode to debug C/C++ binary file:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
{
    "version": "0.2.0",
    "configurations": [ 
        {
            "name": "LibTorch Launch",
            "type": "cppdbg",
            "request": "launch",
            "program": "${fileDirname}/build/MyLibTorchApp",
            "args": [],
            "preLaunchTask": "Build with cmake", 
            "cwd": "${workspaceFolder}",
        }
    ]
}

task.json

Set up “tasks.json” for building project with cmake:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
{
	"version": "2.0.0",
	"tasks": [
	  {
		"label": "cmake-configure",
		"type": "shell",
		"command": "cmake",
		"args": [
		  "-DCMAKE_BUILD_TYPE=Debug",
		  "-DCMAKE_PREFIX_PATH=/absolute/path/to/libtorch",
		  "..",
		],
		"options": {
		  "cwd": "${workspaceFolder}/build"  // Set the build directory
		},
		"group": {
		  "kind": "build",
		  "isDefault": true
		}
	  },
	  {
		"label": "cmake-build",
		"type": "shell",
		"command": "cmake",
		"args": [
		  "--build",
		  ".",
		  "--config",
		  "Debug"
		],
		"options": {
		  "cwd": "${workspaceFolder}/build"  // Set the build directory
		},
		"group": {
		  "kind": "build",
		  "isDefault": true
		}
	  },
	  {
		"label": "Build with cmake",
		"dependsOn": ["cmake-configure", "cmake-build"]
	  }
	]
}
  1. Multiple tasks can be combined to one, which then is able tobe passed to preLaunchTask in “launch.json”. (Can’t pass 2 tasks at once.)

    Integrate with External Tools via Tasks - Docs

  2. The above 2 tasks both are custom tasks executed in shell.

  3. CMake requires two steps, so 2 tasks are needed.

  4. The tag -DCMAKE_BUILD_TYPE=Debug is needed for generating debug info. Otherwise, the breakpoints won’t take effect:

    1
    
    Module containing this breakpoint has not yet loaded or the breakpoint address could not be obtained.
    
  5. However, sometimes, camke build failed:

    Expand the error
     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    
     *  Executing task: cmake --build . --config Debug 
    
    -- Caffe2: CUDA detected: 11.6
    -- Caffe2: CUDA nvcc is: /usr/local/cuda-11.6/bin/nvcc
    -- Caffe2: CUDA toolkit directory: /usr/local/cuda-11.6
    CMake Error at /usr/local/libtorch/share/cmake/Caffe2/public/cuda.cmake:88 (message):
      Caffe2: Couldn't determine version from header: Change Dir:
      '/home/yi/Downloads/example-app/build/CMakeFiles/CMakeTmp'
    
      Run Build Command(s): /usr/local/bin/cmake -E env VERBOSE=1 /usr/bin/make
      -f Makefile cmTC_a6e2f/fast
    
      make[1]: Entering directory
      '/home/yi/Downloads/example-app/build/CMakeFiles/CMakeTmp'
    
      /usr/bin/make -f CMakeFiles/cmTC_a6e2f.dir/build.make
      CMakeFiles/cmTC_a6e2f.dir/build
    
      make[2]: Entering directory
      '/home/yi/Downloads/example-app/build/CMakeFiles/CMakeTmp'
    
      Building CXX object CMakeFiles/cmTC_a6e2f.dir/detect_cuda_version.cc.o
    
      /usr/bin/c++ -I/usr/local/cuda-11.6/include -o
      CMakeFiles/cmTC_a6e2f.dir/detect_cuda_version.cc.o -c
      /home/yi/Downloads/example-app/build/detect_cuda_version.cc
    
      Assembler messages:
    
      Fatal error: can't create
      CMakeFiles/cmTC_a6e2f.dir/detect_cuda_version.cc.o: No such file or
      directory
    
      make[2]: *** [CMakeFiles/cmTC_a6e2f.dir/build.make:78:
      CMakeFiles/cmTC_a6e2f.dir/detect_cuda_version.cc.o] Error 1
    
      make[2]: Leaving directory
      '/home/yi/Downloads/example-app/build/CMakeFiles/CMakeTmp'
    
      make[1]: *** [Makefile:127: cmTC_a6e2f/fast] Error 2
    
      make[1]: Leaving directory
      '/home/yi/Downloads/example-app/build/CMakeFiles/CMakeTmp'
    
    Call Stack (most recent call first):
      /usr/local/libtorch/share/cmake/Caffe2/Caffe2Config.cmake:88 (include)
      /usr/local/libtorch/share/cmake/Torch/TorchConfig.cmake:68 (find_package)
      CMakeLists.txt:4 (find_package)
    
    
    -- Configuring incomplete, errors occurred!
    make: *** [Makefile:179: cmake_check_build_system] Error 1
    
     *  The terminal process "/usr/bin/bash '-i', '-c', 'cmake --build . --config Debug'" terminated with exit code: 2. 
    
  6. I tried g++ command, but I had problem with linking torch libraries (-L) and including header files (-I).

    1
    2
    3
    4
    5
    6
    7
    
     *  Executing task: C/C++: g++ build active file 
    
    Starting build...
    /usr/bin/g++ -fdiagnostics-color=always -g /home/yi/Downloads/example-app/main.cpp -o /home/yi/Downloads/example-app/main
    /home/yi/Downloads/example-app/main.cpp:1:10: fatal error: torch/torch.h: No such file or directory
        1 | #include <torch/torch.h>
          |          ^~~-~~-~~-~~-~~-~~
    

Ref:

  1. CMake+VSCode编译运行C++程序简单教程 - 迷楼的文章 - 知乎

  2. Linux环境下使用VScode调试CMake工程 - Colorful的文章 - 知乎

  3. libtorch 常用api函数示例(史上最全、最详细)- 无左无右 - 博客园


Inspect Tensor

(2023-11-19)

  1. Save tensors to a file.

    (LibTorch -> PyTorch tensors) How to save a C++ Libtorch Tensor and load it into a Python Pytorch project?

  2. In GDB: *(float_t*)x.data_ptr()

    1
    2
    
    int main(){
    const torch::Tensor bkg = torch::full({3}, 0., torch::device(torch::kCUDA));
    

    GDB: *(float_t*)bkg.data_ptr() return 0.

    Is it possible to view values of a at::Tensor in Visual codes debug variable view (linux)?

Built with Hugo
Theme Stack designed by Jimmy