| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Name | Name | Last commit date | ||
|---|---|---|---|---|
C++ Implementation of PyTorch Tutorials for Everyone
| OS (Compiler)\LibTorch | 2.8.0 |
|---|---|
| macOS (clang 15, 16) | |
| Linux (gcc 13, 14) | |
| Windows (msvc 2022, 2025) |
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
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-forgegit clone https://github.com/prabhuomkar/pytorch-cpp.git
cd pytorch-cppcmake -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=(11.8|12.4|12.6|12.8|12.9|none) | none | Download LibTorch for a CUDA version (none = download CPU version). |
| -D LIBTORCH_DOWNLOAD_BUILD_TYPE=(Release|Debug) | Release | Determines which libtorch build type version to download (only relevant on Windows). |
| -D DOWNLOAD_DATASETS=(OFF|ON) | ON | Download required datasets during build (only if they do not already exist in pytorch-cpp/data). |
| -D CREATE_SCRIPTMODULES=(OFF|ON) | OFF | Create all required scriptmodule files for prelearned models / weights during build. Requires installed python3 with pytorch and torchvision. |
| -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> | Determines the CMake build-type for single-configuration generators (see CMake docs). |
cmake -B build \
-D CMAKE_BUILD_TYPE=Release \
-D CMAKE_PREFIX_PATH=/path/to/libtorch/share/cmake/Torch \
-D CREATE_SCRIPTMODULES=ON cmake -B build \
-A x64 \
-D CUDA_V=11.8Note for Windows (Visual Studio) users:
The CMake script downloads the Release version of LibTorch, so --config Release has to be appended to the build command.
How dataset download and scriptmodule creation work:
To build all tutorials use
cmake --build buildYou can choose to only build tutorials in one of the categories basics, intermediate, advanced or popular. For example, if you are only interested in the basics tutorials:
cmake --build build --target basics
# In general: cmake --build build --target {category}You can also choose to only build a single tutorial. For example to build the language model tutorial only:
cmake --build build --target language-model
# In general: cmake --build build --target {tutorial-name}Note:
The target argument is the tutorial's foldername with all underscores replaced by hyphens.
Tip for users of CMake version >= 3.15:
You can specify several targets separated by spaces, for example:cmake --build build --target language-model image-captioning
cd build/tutorials/basics/pytorch_basics
# In general: cd build/tutorials/{basics|intermediate|advanced|popular/blitz}/{tutorial_name}./pytorch-basics
# In general: ./{tutorial-name}.\pytorch-basics.exe
# In general: .\{tutorial-name}.exeFind 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:
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.
docker-compose run --rm pytorch-cppdocker-compose run --rm pytorch-cpp {category}docker-compose run --rm pytorch-cpp pytorch-basics
# In general: docker-compose run --rm pytorch-cpp {tutorial-name} This repository is licensed under MIT as given in LICENSE.
| Back | FazBrowse Home | New Git URL |