Code Monkey home page Code Monkey logo

torchcpp1's Introduction

C++ example 1 for PyTorch/LibTorch

by Damir Cavar, June 2019

Introduction

Using PyTorch or LibTorch with C++ comes with some minor complications. This example solves some of the issues.

Follow the instructions at: Installing C++ Distributions of PyTorch

You might face two problems:

  • You cannot find torch/torch.h and you get compiler errors.
  • CMake does not find the LibTorch config files

In the following, I will provide solutions that use the path of LibTorch relative to my home directory, indicated by the ~. I also use simply the CPU-version of LibTorch.

The location of torch/torch.h is in the folder:

~/libtorch/include/torch/csrc/api/include/

This folder would be added to the $TORCH_INCLUDE_DIRS variable by CMake, if correctly configured.

To allow CMake to find this configuration file, make sure that this line in your CMakeLists.txt points to the directory ~/libtorch/share/cmake/Torch/:

set(CMAKE_PREFIX_PATH "~/libtorch/share/cmake/Torch")

The complete CMakeLists.txt in this example is:

cmake_minimum_required(VERSION 3.14)
project(TorchCPP1)

set(CMAKE_CXX_STANDARD 11)

set(CMAKE_PREFIX_PATH "~/libtorch/share/cmake/Torch")
find_package(Torch REQUIRED)

add_executable(TorchCPP1 main.cpp)
target_link_libraries(TorchCPP1 "${TORCH_LIBRARIES}")
target_include_directories(TorchCPP1 PRIVATE ${TORCH_LIBRARIES})
set_property(TARGET TorchCPP1 PROPERTY CXX_STANDARD 11)

The C++ source is taken from Installing C++ Distributions of PyTorch. To compile the code, create a build folder:

mkdir build

Change into this folder:

cd build

Let CMake generate the make-file by providing an extra parameter that points to libtorch, in this case, as mentioned, in my home directory:

cmake -DCMAKE_PREFIX_PATH=~/libtorch ..

Now compile the code:

make

You should see a binary TorchCPP1 in the directory. Running the binary ./TorchCPP1 should show some output similar to the following:

 0.5393  0.5165  0.8186
 0.8735  0.4660  0.6906
[ Variable[CPUFloatType]{2,3} ]

torchcpp1's People

Contributors

dcavar avatar

Stargazers

 avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.