Skip to content

Commit 7a5d057

Browse files
authored
Upgrade to libtorch v1.13.1 (#106)
* Upgrade to libtorch v1.13.1. * Update linear regression tutorial device selection. * Update colab notebook.
1 parent 214d708 commit 7a5d057

File tree

6 files changed

+74
-38
lines changed

6 files changed

+74
-38
lines changed

CMakeLists.txt

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,20 @@ list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake")
77
option(DOWNLOAD_DATASETS "Automatically download required datasets at build-time." ON)
88
option(CREATE_SCRIPTMODULES "Automatically create all required scriptmodule files at build-time (requires python3)." OFF)
99

10-
set(PYTORCH_VERSION "1.12.0")
10+
set(PYTORCH_VERSION "1.13.1")
11+
set(PYTORCH_MIN_VERSION "1.12.0")
1112

12-
find_package(Torch ${PYTORCH_VERSION} EXACT QUIET PATHS "${CMAKE_SOURCE_DIR}/libtorch")
13+
find_package(Torch QUIET PATHS "${CMAKE_SOURCE_DIR}/libtorch")
1314

14-
if(NOT Torch_FOUND)
15+
if((NOT Torch_FOUND) OR (("${Torch_VERSION}" VERSION_LESS "${PYTORCH_MIN_VERSION}") OR
16+
("${Torch_VERSION}" VERSION_GREATER "${PYTORCH_VERSION}")))
1517
unset(Torch_FOUND)
18+
message(STATUS "Could not find compatible Torch version (>= ${PYTORCH_MIN_VERSION}, <= ${PYTORCH_VERSION})")
1619
include(fetch_libtorch)
1720
endif()
1821

22+
message(STATUS "Torch version ${Torch_VERSION}")
23+
1924
if(CREATE_SCRIPTMODULES)
2025
find_package(Python3 COMPONENTS Interpreter REQUIRED)
2126
endif()

Dockerfile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,8 @@ RUN curl --silent --show-error --location --output ~/miniconda.sh https://repo.a
3131

3232
FROM conda AS conda-installs
3333
# Install pytorch for CPU and torchvision.
34-
ARG PYTORCH_VERSION=1.12.0
35-
ARG TORCHVISION_VERSION=0.13.0
34+
ARG PYTORCH_VERSION=1.13.1
35+
ARG TORCHVISION_VERSION=0.14.1
3636
ENV NO_CUDA=1
3737
RUN conda install pytorch==${PYTORCH_VERSION} torchvision==${TORCHVISION_VERSION} cpuonly -y -c pytorch && conda clean -ya
3838

README.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,12 @@
55
C++ Implementation of PyTorch Tutorials for Everyone
66
<br />
77
<img src="https://img.shields.io/github/license/prabhuomkar/pytorch-cpp">
8-
<img src="https://img.shields.io/badge/libtorch-1.12.0-ee4c2c">
8+
<img src="https://img.shields.io/badge/libtorch-1.13.1-ee4c2c">
99
<img src="https://img.shields.io/badge/cmake-3.14-064f8d">
1010
</p>
1111

1212

13-
| OS (Compiler)\\LibTorch | 1.12.0 |
13+
| OS (Compiler)\\LibTorch | 1.13.1 |
1414
| :--------------------- | :--------------------------------------------------------------------------------------------------- |
1515
| macOS (clang 11, 12, 13) | [![Status](https://github.com/prabhuomkar/pytorch-cpp/actions/workflows/build_macos.yml/badge.svg?branch=master)](https://github.com/prabhuomkar/pytorch-cpp/actions?query=workflow%3Aci-build-macos) |
1616
| Linux (gcc 9, 10, 11) | [![Status](https://github.com/prabhuomkar/pytorch-cpp/actions/workflows/build_ubuntu.yml/badge.svg?branch=master)](https://github.com/prabhuomkar/pytorch-cpp/actions?query=workflow%3Aci-build-ubuntu) |
@@ -52,7 +52,7 @@ This repository provides tutorial code in C++ for deep learning researchers to l
5252

5353
1. [C++-17](http://www.cplusplus.com/doc/tutorial/introduction/) compatible compiler
5454
2. [CMake](https://cmake.org/download/) (minimum version 3.14)
55-
3. [LibTorch v1.12.0](https://pytorch.org/cppdocs/installing.html)
55+
3. [LibTorch version >= 1.12.0 and <= 1.13.1](https://pytorch.org/cppdocs/installing.html)
5656
4. [Conda](https://docs.conda.io/projects/conda/en/latest/user-guide/install/download.html)
5757

5858

@@ -89,7 +89,7 @@ Some useful options:
8989

9090
| Option | Default | Description |
9191
| :------------- |:------------|-----:|
92-
| `-D CUDA_V=(11.3\|11.6\|none)` | `none` | Download LibTorch for a CUDA version (`none` = download CPU version). |
92+
| `-D CUDA_V=(11.6\|11.7\|none)` | `none` | Download LibTorch for a CUDA version (`none` = download CPU version). |
9393
| `-D LIBTORCH_DOWNLOAD_BUILD_TYPE=(Release\|Debug)` | `Release` | Determines which libtorch build type version to download (only relevant on **Windows**).|
9494
| `-D DOWNLOAD_DATASETS=(OFF\|ON)` | `ON` | Download required datasets during build (only if they do not already exist in `pytorch-cpp/data`). |
9595
|`-D CREATE_SCRIPTMODULES=(OFF\|ON)` | `OFF` | Create all required scriptmodule files for prelearned models / weights during build. Requires installed python3 with pytorch and torchvision. |
@@ -116,14 +116,14 @@ cmake -B build \
116116
<summary><b>Example Windows</b></summary>
117117

118118
##### Aim
119-
* Automatically download LibTorch for CUDA 11.6 (Release version) and all necessary datasets.
119+
* Automatically download LibTorch for CUDA 11.7 (Release version) and all necessary datasets.
120120
* Do not create scriptmodule files.
121121

122122
##### Command
123123
```bash
124124
cmake -B build \
125125
-A x64 \
126-
-D CUDA_V=11.6
126+
-D CUDA_V=11.7
127127
```
128128
</details>
129129

cmake/fetch_libtorch.cmake

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,16 @@ cmake_minimum_required(VERSION 3.14 FATAL_ERROR)
22

33
include(FetchContent)
44

5-
set(CUDA_V "none" CACHE STRING "Determines libtorch CUDA version to download (11.3, 11.6 or none).")
5+
set(CUDA_V "none" CACHE STRING "Determines libtorch CUDA version to download (11.6, 11.7 or none).")
66

77
if(${CUDA_V} STREQUAL "none")
88
set(LIBTORCH_DEVICE "cpu")
9-
elseif(${CUDA_V} STREQUAL "11.3")
10-
set(LIBTORCH_DEVICE "cu113")
119
elseif(${CUDA_V} STREQUAL "11.6")
1210
set(LIBTORCH_DEVICE "cu116")
11+
elseif(${CUDA_V} STREQUAL "11.7")
12+
set(LIBTORCH_DEVICE "cu117")
1313
else()
14-
message(FATAL_ERROR "Invalid CUDA version specified, must be 11.3, 11.6 or none!")
14+
message(FATAL_ERROR "Invalid CUDA version specified, must be 11.6, 11.7 or none!")
1515
endif()
1616

1717
if(${CMAKE_SYSTEM_NAME} STREQUAL "Windows")

notebooks/pytorch_cpp_colab_notebook.ipynb

Lines changed: 42 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,8 @@
33
"nbformat_minor": 0,
44
"metadata": {
55
"colab": {
6-
"name": "Copy of Copy of pytorch_cpp_colab_notebook.ipynb",
76
"private_outputs": true,
87
"provenance": [],
9-
"collapsed_sections": [
10-
"VbwYTgxWvfMD"
11-
],
128
"toc_visible": true
139
},
1410
"kernelspec": {
@@ -27,7 +23,8 @@
2723
"\n",
2824
"![Pytorch Cpp](https://github.com/prabhuomkar/pytorch-cpp/raw/master/images/pytorch_cpp.png)\n",
2925
"\n",
30-
"This notebook is intended to be used on [Google Colab](https://colab.research.google.com). It allows you\n",
26+
"* **This notebook is intended to be used on [Google Colab](https://colab.research.google.com) ONLY!** \n",
27+
"* It allows you\n",
3128
"to build and run the [pytorch-cpp](https://github.com/prabhuomkar/pytorch-cpp) tutorials on a hosted GPU equipped system for free."
3229
]
3330
},
@@ -138,15 +135,30 @@
138135
"id": "cs1nFZkH59Nb"
139136
},
140137
"source": [
141-
"##Install more recent CMake Version"
138+
"##Install more recent versions of dependencies and build tools\n",
139+
"(This takes several minutes)"
142140
]
143141
},
144142
{
145143
"cell_type": "code",
146-
"metadata": {
147-
"id": "m2Lf4RSjou6t"
148-
},
149144
"source": [
145+
"# Update CUDA \n",
146+
"!wget https://developer.download.nvidia.com/compute/cuda/repos/ubuntu1804/x86_64/cuda-ubuntu1804.pin\n",
147+
"!sudo mv cuda-ubuntu1804.pin /etc/apt/preferences.d/cuda-repository-pin-600\n",
148+
"!wget https://developer.download.nvidia.com/compute/cuda/11.6.0/local_installers/cuda-repo-ubuntu1804-11-6-local_11.6.0-510.39.01-1_amd64.deb\n",
149+
"!sudo dpkg -i 'cuda-repo-ubuntu1804-11-6-local_11.6.0-510.39.01-1_amd64.deb'\n",
150+
"!sudo apt-key add /var/cuda-repo-ubuntu1804-11-6-local/7fa2af80.pub\n",
151+
"!sudo apt-get update\n",
152+
"!sudo DEBIAN_FRONTEND=noninteractive apt-get -y install cuda-11-6\n",
153+
"!sudo cp /usr/include/x86_64-linux-gnu/cudnn*.h /usr/local/cuda-11.6/include\n",
154+
"!sudo cp /usr/lib/x86_64-linux-gnu/libcudnn* /usr/local/cuda-11.6/lib64\n",
155+
"!sudo chmod a+r /usr/local/cuda-11.6/include/cudnn*.h /usr/local/cuda-11.6/lib64/libcudnn*\n",
156+
"old_ld_library_path = %env LD_LIBRARY_PATH\n",
157+
"new_ld_library_path = \"/usr/local/cuda-11.6/lib64:\" + old_ld_library_path\n",
158+
"%env LD_LIBRARY_PATH={new_ld_library_path}\n",
159+
"%env CUDA_HOME=/usr/local/cuda-11.6\n",
160+
"\n",
161+
"# Update CMake\n",
150162
"!rm -rf deps\n",
151163
"!mkdir deps\n",
152164
"%cd /content/deps\n",
@@ -157,8 +169,17 @@
157169
"new_path = \"/content/deps/cmake-install:/content/deps/cmake-install/bin:\" + old_path\n",
158170
"%env PATH={new_path}\n",
159171
"%cd /content/\n",
160-
"!cmake --version"
172+
"!cmake --version\n",
173+
"\n",
174+
"# Update g++\n",
175+
"!sudo add-apt-repository ppa:ubuntu-toolchain-r/test -y\n",
176+
"!sudo apt-get update\n",
177+
"!sudo apt-get install gcc-10 g++-10 -y\n",
178+
"!sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-10 60 --slave /usr/bin/g++ g++ /usr/bin/g++-10"
161179
],
180+
"metadata": {
181+
"id": "yS5dYChCtqzP"
182+
},
162183
"execution_count": null,
163184
"outputs": []
164185
},
@@ -189,8 +210,8 @@
189210
"echo \"\"\n",
190211
"pip show torchvision\n",
191212
"echo \"\"\n",
192-
"# GPU Informations\n",
193-
"nvidia-smi"
213+
"# CUDA Information\n",
214+
"nvcc --version"
194215
],
195216
"execution_count": null,
196217
"outputs": []
@@ -232,8 +253,9 @@
232253
"id": "rbeD5lKStWhS"
233254
},
234255
"source": [
256+
"%cd /content/pytorch-cpp\n",
235257
"%rm -rf build\n",
236-
"!cmake -B build -D CUDA_V=11.1 -D CMAKE_BUILD_TYPE=Release"
258+
"!cmake -B build -D CUDA_V=11.6 -D CMAKE_BUILD_TYPE=Release"
237259
],
238260
"execution_count": null,
239261
"outputs": []
@@ -253,6 +275,7 @@
253275
"id": "H-n_1d4Mt8MG"
254276
},
255277
"source": [
278+
"%cd /content/pytorch-cpp\n",
256279
"!cmake --build build"
257280
],
258281
"execution_count": null,
@@ -273,7 +296,7 @@
273296
"* [Feedforward Neural Network](#scrollTo=ywmM2xkt1mi0)\n",
274297
"\n",
275298
"### 2. Intermediate\n",
276-
"* [Convolutional Neural Network](#scrollTo=2gjZC6b_2PEz)\n",
299+
"* [Convolutional Neural Network](#scrollTo=Convolutional_Neural_Network)\n",
277300
"* [Deep Residual Network](#scrollTo=Kla2SaVv228f)\n",
278301
"* [Recurrent Neural Network](#scrollTo=aTrOcUke3Zxu)\n",
279302
"* [Bidirectional Recurrent Neural Network](#scrollTo=FVVx0XFY3yvU)\n",
@@ -322,7 +345,7 @@
322345
},
323346
"source": [
324347
"# Create required torchscript module:\n",
325-
"%cd /content/pytorch-cpp/tutorials/basics/pytorch_basics/models/\n",
348+
"%cd /content/pytorch-cpp/tutorials/basics/pytorch_basics/model/\n",
326349
"!python create_resnet18_scriptmodule.py"
327350
],
328351
"execution_count": null,
@@ -473,12 +496,12 @@
473496
},
474497
{
475498
"cell_type": "markdown",
476-
"metadata": {
477-
"id": "2gjZC6b_2PEz"
478-
},
479499
"source": [
480500
"###Convolutional Neural Network"
481-
]
501+
],
502+
"metadata": {
503+
"id": "amr2BK2XpJ5j"
504+
}
482505
},
483506
{
484507
"cell_type": "code",

tutorials/basics/linear_regression/main.cpp

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,11 @@
55

66
int main() {
77
std::cout << "Linear Regression\n\n";
8-
std::cout << "Training on CPU.\n";
8+
9+
// Device
10+
auto cuda_available = torch::cuda::is_available();
11+
torch::Device device(cuda_available ? torch::kCUDA : torch::kCPU);
12+
std::cout << (cuda_available ? "CUDA available. Training on GPU." : "Training on CPU.") << '\n';
913

1014
// Hyper parameters
1115
const int64_t input_size = 1;
@@ -14,11 +18,15 @@ int main() {
1418
const double learning_rate = 0.001;
1519

1620
// Sample dataset
17-
auto x_train = torch::randint(0, 10, {15, 1});
18-
auto y_train = torch::randint(0, 10, {15, 1});
21+
auto x_train = torch::randint(0, 10, {15, 1},
22+
torch::TensorOptions(torch::kFloat).device(device));
23+
24+
auto y_train = torch::randint(0, 10, {15, 1},
25+
torch::TensorOptions(torch::kFloat).device(device));
1926

2027
// Linear regression model
2128
torch::nn::Linear model(input_size, output_size);
29+
model->to(device);
2230

2331
// Optimizer
2432
torch::optim::SGD optimizer(model->parameters(), torch::optim::SGDOptions(learning_rate));
@@ -31,7 +39,7 @@ int main() {
3139
// Train the model
3240
for (size_t epoch = 0; epoch != num_epochs; ++epoch) {
3341
// Forward pass
34-
auto output = model(x_train);
42+
auto output = model->forward(x_train);
3543
auto loss = torch::nn::functional::mse_loss(output, y_train);
3644

3745
// Backward pass and optimize

0 commit comments

Comments
 (0)