/pytorch-cpp

C++ Implementation of PyTorch Tutorials for Everyone

Primary LanguageC++MIT LicenseMIT

C++ Implementation of PyTorch Tutorials for Everyone

OS (Compiler)\LibTorch 1.6.0 nightly
macOS (clang 9.1) Status
macOS (clang 10.0) Status
macOS (clang 11.0) Status
Linux (gcc 5) Status
Linux (gcc 6) Status
Linux (gcc 7) Status
Linux (gcc 8) Status
Windows (msvc 2017) Status

Table of Contents

This repository provides tutorial code in C++ for deep learning researchers to learn PyTorch (i.e. Section 1 to 3)
Python Tutorial: https://github.com/yunjey/pytorch-tutorial

1. Basics

2. Intermediate

3. Advanced

4. Interactive Tutorials

5. Other Popular Tutorials

Getting Started

Requirements

  1. C++
  2. CMake
  3. LibTorch v1.6.0
  4. Conda

For Interactive Tutorials

Note: Interactive Tutorials are currently running on LibTorch Nightly Version.
So there are some tutorials which can break when working with nightly version.

conda create --name pytorch-cpp
conda activate pytorch-cpp
conda install xeus-cling notebook -c conda-forge

Clone, build and run tutorials

In Google Colab

Open In Colab

On Local Machine

git clone https://github.com/prabhuomkar/pytorch-cpp.git
cd pytorch-cpp

Generate build system

cmake -B build #<options>

Note for Windows users:
Libtorch only supports 64bit Windows and an x64 generator needs to be specified. For Visual Studio this can be done by appending -A x64 to the above command.

Some useful options:

Option Default Description
-D CUDA_V=(9.2 [Linux only]|10.1|10.2|none) none Download LibTorch for a CUDA version (none = download CPU version).
-D DOWNLOAD_DATASETS=(OFF|ON) ON Download all datasets used in the tutorials.
-D CMAKE_PREFIX_PATH=path/to/libtorch/share/cmake/Torch <empty> Skip the downloading of LibTorch and use your own local version (see Requirements) instead.
-D CMAKE_BUILD_TYPE=(Release|Debug) <empty> (Release when downloading LibTorch on Windows) Set the build type (Release = compile with optimizations).
-D CREATE_SCRIPTMODULES=(OFF|ON) OFF Create all needed scriptmodule files for prelearned models / weights. Requires installed python3 with pytorch and torchvision.
Example Linux
Aim
  • Use existing Python, PyTorch (see Requirements) and torchvision installation.
  • Download all datasets and create all necessary scriptmodule files.
Command
cmake -B build \
-D CMAKE_BUILD_TYPE=Release \
-D CMAKE_PREFIX_PATH=/path/to/libtorch/share/cmake/Torch \
-D CREATE_SCRIPTMODULES=ON 
Example Windows
Aim
  • Automatically download LibTorch for CUDA 10.2 and all necessary datasets.
  • Do not create scriptmodule files.
Command
cmake -B build \
-A x64 \
-D CUDA_V=10.2

Build

cmake --build build

Note for Windows users:
The CMake script downloads the Release version of LibTorch, so --config Release has to be appended to the build command.

General Note:
By default all tutorials will be built. If you only want to build one specific tutorial, specify the target parameter for the build command. For example to only build the language model tutorial, append --target language-model (target name = tutorial foldername with all underscores replaced with hyphens).

Run Tutorials

  1. (IMPORTANT!) First change into the tutorial's directory within build/tutorials. For example, assuming you are in the pytorch-cpp directory and want to change to the pytorch basics tutorial folder:
    cd build/tutorials/basics/pytorch_basics
    # In general: cd build/tutorials/{basics|intermediate|advanced}/{tutorial_name}
  2. Run the executable. Note that the executable's name is the tutorial's foldername with all underscores replaced with hyphens (e.g. for tutorial folder: pytorch_basics -> executable name: pytorch-basics (or pytorch-basics.exe on Windows)). For example, to run the pytorch basics tutorial:

    Linux/Mac
    ./pytorch-basics
    # In general: ./{tutorial-name}
    Windows
    .\pytorch-basics.exe
    # In general: .\{tutorial-name}.exe

Using Docker

Find the latest and previous version images on Docker Hub.

You can build and run the tutorials (on CPU) in a Docker container using the provided Dockerfile and docker-compose.yml files:

  1. From the root directory of the cloned repo build the image:
    docker-compose build --build-arg USER_ID=$(id -u) --build-arg GROUP_ID=$(id -g)

    Note:
    When you run the Docker container, the host repo directory is mounted as a volume in the Docker container in order to cache build and downloaded dependency files so that it is not necessary to rebuild or redownload everything when a container is restarted. In order to have correct file permissions it is necessary to provide your user and group ids as build arguments when building the image on Linux.

  2. Now start the container and build the tutorials using:
    docker-compose run --rm pytorch-cpp
    This fetches all necessary dependencies and builds the tutorials. After the build is done, by default the container starts bash in interactive mode in the build/tutorials folder. As an alternative, you can also directly run a tutorial by instead invoking the above command with the tutorial as additional argument, for example:
    docker-compose run --rm pytorch-cpp pytorch-basics
    # In general: docker-compose run --rm pytorch-cpp {tutorial-name} 
    This will - if necessary - build all tutorials and then start the provided tutorial in a container.

License

This repository is licensed under MIT as given in LICENSE.