Table of contents
Install LibTorch
libtorch is the C++ API for PyTorch. Guide: INSTALLING C++ DISTRIBUTIONS OF PYTORCH
-
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/ -
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; } -
Set dependence for LibTorch via CMakeLists.txt:
1 2 3 4 5 6 7 8 9cmake_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) -
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 -
(Optional) For C/C++ extension, set up
includePathin “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:
|
|
programis the compiled binary file, not the main.cpp file.- Debug C++ in Visual Studio Code - Docs
- General attributes: Options for Debugging
task.json
Set up “tasks.json” for building project with cmake:
|
|
-
Multiple tasks can be combined to one, which then is able tobe passed to
preLaunchTaskin “launch.json”. (Can’t pass 2 tasks at once.) -
The above 2 tasks both are custom tasks executed in shell.
-
CMake requires two steps, so 2 tasks are needed.
-
The tag
-DCMAKE_BUILD_TYPE=Debugis needed for generating debug info. Otherwise, the breakpoints won’t take effect:1Module containing this breakpoint has not yet loaded or the breakpoint address could not be obtained. -
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. -
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:
Inspect Tensor
(2023-11-19)
-
Save tensors to a file.
-
In GDB:
*(float_t*)x.data_ptr()1 2int main(){ const torch::Tensor bkg = torch::full({3}, 0., torch::device(torch::kCUDA));GDB:
*(float_t*)bkg.data_ptr()return0.Is it possible to view values of a at::Tensor in Visual codes debug variable view (linux)?