https://github.com/dcavar/torchcpp1

LibTorch first example based on the online tutorial with functioning CMake configuration

https://github.com/dcavar/torchcpp1

Science Score: 13.0%

This score indicates how likely this project is to be science-related based on various indicators:

  • CITATION.cff file
  • codemeta.json file
    Found codemeta.json file
  • .zenodo.json file
  • DOI references
  • Academic publication links
  • Committers with academic emails
  • Institutional organization owner
  • JOSS paper metadata
  • Scientific vocabulary similarity
    Low similarity (10.2%) to scientific vocabulary

Keywords

cmake cpp11 libtorch pytorch
Last synced: 4 months ago · JSON representation

Repository

LibTorch first example based on the online tutorial with functioning CMake configuration

Basic Info
  • Host: GitHub
  • Owner: dcavar
  • License: apache-2.0
  • Language: CMake
  • Default Branch: master
  • Homepage: http://damir.cavar.me/
  • Size: 6.84 KB
Statistics
  • Stars: 1
  • Watchers: 1
  • Forks: 0
  • Open Issues: 0
  • Releases: 0
Topics
cmake cpp11 libtorch pytorch
Created over 6 years ago · Last pushed over 6 years ago
Metadata Files
Readme License

README.md

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} ]

Owner

  • Name: Damir Cavar
  • Login: dcavar
  • Kind: user
  • Location: Bloomington, IN
  • Company: Indiana University

GitHub Events

Total
Last Year

Committers

Last synced: 9 months ago

All Time
  • Total Commits: 3
  • Total Committers: 1
  • Avg Commits per committer: 3.0
  • Development Distribution Score (DDS): 0.0
Past Year
  • Commits: 0
  • Committers: 0
  • Avg Commits per committer: 0.0
  • Development Distribution Score (DDS): 0.0
Top Committers
Name Email Commits
Damir Cavar d****r@m****m 3
Committer Domains (Top 20 + Academic)
me.com: 1

Issues and Pull Requests

Last synced: 9 months ago

All Time
  • Total issues: 0
  • Total pull requests: 0
  • Average time to close issues: N/A
  • Average time to close pull requests: N/A
  • Total issue authors: 0
  • Total pull request authors: 0
  • Average comments per issue: 0
  • Average comments per pull request: 0
  • Merged pull requests: 0
  • Bot issues: 0
  • Bot pull requests: 0
Past Year
  • Issues: 0
  • Pull requests: 0
  • Average time to close issues: N/A
  • Average time to close pull requests: N/A
  • Issue authors: 0
  • Pull request authors: 0
  • Average comments per issue: 0
  • Average comments per pull request: 0
  • Merged pull requests: 0
  • Bot issues: 0
  • Bot pull requests: 0
Top Authors
Issue Authors
Pull Request Authors
Top Labels
Issue Labels
Pull Request Labels