Pytorch in C++

1 minute read

Published:

Blog Post #1

A tutorial for using Pytorch library in C++.

Most of the people use Pytorch in python to build up the Convolutional Neural Network (CNN) model, such as in Jupyter Notebook. It is the most common and a faster way to do so. But if we want to make a hardware acceleration to speed up the performance, like using Vitis HLS from Xilinx, python is not suitable because it only supports C/C++. Hence, we may rebuild our CNN model in C++ with the aid of Pytorch which can make our lives more easier. As a result, we can accelerate the algorithm using the concepts like pipeline and loop unrolling from Vitis HLS.

This tutorial will show you how to load Pytorch library into C++ under the Visual Studio Code in macOS Catalina version 10.15.7 environment.

Prerequsite:
You should have install Pytorch in your environment already.

Step1:
Open terminal and run the following cmd to print out the location path of your Pytorch. And remember to store the location first.
python -c 'import torch; print(torch.utils.cmake_prefix_path)'

Step2:
Navigate to the directory containing your main.cpp (your .cpp file that build the CNN model)

Step3:
Create a file called CMakeLists.txt and paste the the following codes in and save it.

#########################################################<br>
cmake_minimum_required(VERSION 3.0 FATAL_ERROR)
project(example-app)

find_package(Torch REQUIRED)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${TORCH_CXX_FLAGS}")

add_executable(main main.cpp)
target_link_libraries(main "${TORCH_LIBRARIES}")
set_property(TARGET main PROPERTY CXX_STANDARD 14)
#########################################################

Step4:
Back to the terminal in Step1, type in the following cmd sequentially.
mkdir build
cd build
cmake -DCMAKE_PREFIX_PATH=[paste your pathway that you stored in Step1]
cmake --build . --config Release

Step5:
Done!
You may run the output file to see the result by cmd
./main

Demo codes are given as the followings:

#include <torch/torch.h>
#include <iostream>

int main() 
{
  torch::Tensor tensor = torch::rand({2, 3});
  std::cout << tensor << std::endl;
}    


After you run it, you should see a 2x3 matrix with random values is shown as below, like:

0.2063  0.6593  0.0866
0.0796  0.5841  0.1569
[ Variable[CPUFloatType]{2,3} ]           

Hope you enjoy this :)

Reference:
https://www.youtube.com/watch?v=IVWpv-610h4
https://pytorch.org/cppdocs/installing.html