Skip to content

Commit 6ff8c0e

Browse files
committed
Move from using bash linting, install & build script to using cmake for everything.
1 parent 4dd5cd5 commit 6ff8c0e

File tree

30 files changed

+550
-430
lines changed

30 files changed

+550
-430
lines changed

.gitignore

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,12 @@
3535
libtorch/
3636
build/
3737

38+
# Datasets
39+
data/
40+
41+
# Tools
42+
tools/
43+
3844
# Other
3945
.DS_Store
4046
.idea/

.travis.yml

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
sudo: false
21
language: cpp
32
compiler:
43
- gcc
@@ -11,11 +10,29 @@ addons:
1110
packages:
1211
- g++-6
1312
- clang-3.8
14-
- cmake-data
15-
- cmake
16-
before_install:
17-
- wget https://raw.githubusercontent.com/cpplint/cpplint/master/cpplint.py
13+
14+
install:
15+
# Download and install a more recent CMake version
16+
# Source: https://riptutorial.com/cmake/example/4723/configure-travis-ci-with-newest-cmake
17+
# Create directory for the CMake binaries
18+
- DEPS_DIR="${TRAVIS_BUILD_DIR}/deps"
19+
- mkdir ${DEPS_DIR} && cd ${DEPS_DIR}
20+
# Fetch cmake binaries
21+
- travis_retry wget --no-check-certificate https://www.cmake.org/files/v3.14/cmake-3.14.0-Linux-x86_64.tar.gz
22+
# Extract and move binaries
23+
- tar -xvf cmake-3.14.0-Linux-x86_64.tar.gz > /dev/null
24+
- mv cmake-3.14.0-Linux-x86_64 cmake-install
25+
# Add to PATH
26+
- PATH=${DEPS_DIR}/cmake-install:${DEPS_DIR}/cmake-install/bin:$PATH
27+
# Switch back to build directory
28+
- cd ${TRAVIS_BUILD_DIR}
29+
1830
script:
19-
- ./scripts.sh lintci
20-
- ./scripts.sh install
21-
- ./scripts.sh build
31+
# Show CMake version
32+
- cmake --version
33+
# Run linter
34+
- cmake -P cmake/cpplint.cmake
35+
# Generate build system
36+
- cmake -B build
37+
# Build
38+
- cmake --build build

CMakeLists.txt

Lines changed: 25 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,36 @@
1-
cmake_minimum_required(VERSION 3.0 FATAL_ERROR)
1+
cmake_minimum_required(VERSION 3.14 FATAL_ERROR)
22

33
project(pytorch-cpp VERSION 1.0.0 LANGUAGES CXX)
44

5-
find_package(Torch REQUIRED)
5+
list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_LIST_DIR}/cmake")
6+
7+
option(DOWNLOAD_DATASETS "Download datasets used in the tutorials." ON)
8+
9+
find_package(Torch QUIET PATHS "${CMAKE_SOURCE_DIR}/libtorch")
10+
11+
if(NOT Torch_FOUND)
12+
unset(Torch_FOUND)
13+
include(download_libtorch)
14+
endif()
15+
16+
if(DOWNLOAD_DATASETS)
17+
include(download_datasets)
18+
endif()
619

720
set(EXECUTABLE_NAME pytorch-cpp)
8-
add_executable(${EXECUTABLE_NAME} main.cpp)
21+
22+
add_executable(${EXECUTABLE_NAME})
23+
24+
target_sources(${EXECUTABLE_NAME} PRIVATE main.cpp)
25+
926
set_target_properties(${EXECUTABLE_NAME} PROPERTIES
1027
CXX_STANDARD 11
1128
CXX_STANDARD_REQUIRED YES
1229
)
1330

1431
target_link_libraries(${EXECUTABLE_NAME} "${TORCH_LIBRARIES}")
1532

16-
# Add tutorial projects:
33+
# Add tutorial sub-projects:
1734

1835
# Basic
1936
add_subdirectory("tutorials/basics/feedforward_neural_network")
@@ -32,15 +49,7 @@ add_subdirectory("tutorials/intermediate/language_model")
3249
add_subdirectory("tutorials/advanced/generative_adversarial_network")
3350
add_subdirectory("tutorials/advanced/variational_autoencoder")
3451

35-
# The following code block is suggested to be used on Windows.
36-
# According to https://github.com/pytorch/pytorch/issues/25457,
37-
# the DLLs need to be copied to avoid memory errors.
38-
# See https://pytorch.org/cppdocs/installing.html.
39-
if (MSVC)
40-
file(GLOB TORCH_DLLS "${TORCH_INSTALL_PREFIX}/lib/*.dll")
41-
add_custom_command(TARGET ${EXECUTABLE_NAME}
42-
POST_BUILD
43-
COMMAND ${CMAKE_COMMAND} -E copy_if_different
44-
${TORCH_DLLS}
45-
$<TARGET_FILE_DIR:${EXECUTABLE_NAME}>)
46-
endif (MSVC)
52+
if(MSVC)
53+
include(copy_torch_dlls)
54+
copy_torch_dlls(${EXECUTABLE_NAME})
55+
endif(MSVC)

README.md

Lines changed: 29 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -9,37 +9,53 @@ This repository provides tutorial code in C++ for deep learning researchers to l
99
**Python Tutorial**: [https://github.com/yunjey/pytorch-tutorial](https://github.com/yunjey/pytorch-tutorial)
1010

1111
## Getting Started
12-
- Fork/Clone and Install
12+
### Fork/Clone and Build
13+
1314
```bash
14-
$ git clone https://github.com/prabhuomkar/pytorch-cpp.git
15-
$ chmod +x scripts.sh
16-
$ ./scripts.sh install #optional: --cuda=(9.2 or 10.1) to install libtorch cuda versions (by default cpu version is installed)
15+
git clone https://github.com/prabhuomkar/pytorch-cpp.git
16+
cd pytorch-cpp
1717
```
18-
- Download all datasets used in the tutorials
19-
```bash
20-
$ ./scripts.sh download_datasets
18+
#### Generate build system
19+
```cmake
20+
cmake -B build #<options>
2121
```
22-
- Build
23-
```bash
24-
$ ./scripts.sh build
22+
> **_Note for Windows users:_**<br>
23+
>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.
24+
25+
Some useful options:
26+
27+
| Option | Default | Description |
28+
| :------------- |:------------|-----:|
29+
| `-D CUDA_V=(9.2\|10.1\|none)` | `none` | Download libtorch for a CUDA version (`none` = download CPU version). |
30+
| `-D DOWNLOAD_DATASETS=(OFF\|ON)` | `ON` | Download all datasets used in the tutorials. |
31+
| `-D CMAKE_PREFIX_PATH=path/to/libtorch/share/cmake/Torch` | | Skip the downloading of libtorch and use your own local version instead. |
32+
33+
#### Build
34+
```cmake
35+
cmake --build build
2536
```
37+
>**_Note for Windows users:_** <br>
38+
>The CMake script downloads the *Release* version of libtorch, so `--config Release` has to be appended to the build command.
39+
>
40+
>**_General Note:_** <br>
41+
>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).
2642
2743
## Table of Contents
2844

29-
#### 1. Basics
45+
### 1. Basics
3046
* [PyTorch Basics](tutorials/basics/pytorch_basics/main.cpp)
3147
* [Linear Regression](tutorials/basics/linear_regression/main.cpp)
3248
* [Logistic Regression](tutorials/basics/logistic_regression/main.cpp)
3349
* [Feedforward Neural Network](tutorials/basics/feedforward_neural_network/src/main.cpp)
3450

35-
#### 2. Intermediate
51+
### 2. Intermediate
3652
* [Convolutional Neural Network](tutorials/intermediate/convolutional_neural_network/src/main.cpp)
3753
* [Deep Residual Network](tutorials/intermediate/deep_residual_network/src/main.cpp)
3854
* [Recurrent Neural Network](tutorials/intermediate/recurrent_neural_network/src/main.cpp)
3955
* [Bidirectional Recurrent Neural Network](tutorials/intermediate/bidirectional_recurrent_neural_network/src/main.cpp)
4056
* [Language Model (RNN-LM)](tutorials/intermediate/language_model/src/main.cpp)
4157

42-
#### 3. Advanced
58+
### 3. Advanced
4359
* [Generative Adversarial Networks](tutorials/advanced/generative_adversarial_network/main.cpp)
4460
* [Variational Auto-Encoder](tutorials/advanced/variational_autoencoder/src/main.cpp)
4561
* [Neural Style Transfer]()
@@ -52,4 +68,3 @@ $ ./scripts.sh build
5268
## Authors
5369
- Omkar Prabhu - [prabhuomkar](https://github.com/prabhuomkar)
5470
- Markus Fleischhacker - [mfl28](https://github.com/mfl28)
55-

cmake/check_files.cmake

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
cmake_minimum_required(VERSION 3.14 FATAL_ERROR)
2+
3+
function(check_files BASE_DIR FILES_TO_CHECK FILE_MD5S MISSING_FILES)
4+
foreach(FILE_TO_CHECK ${${FILES_TO_CHECK}})
5+
if(EXISTS "${BASE_DIR}/${FILE_TO_CHECK}")
6+
list(FIND ${FILES_TO_CHECK} ${FILE_TO_CHECK} MD5_IDX)
7+
list(GET ${FILE_MD5S} ${MD5_IDX} EXPECTED_FILE_MD5)
8+
9+
file(MD5 "${BASE_DIR}/${FILE_TO_CHECK}" ACTUAL_FILE_MD5)
10+
11+
if(NOT (EXPECTED_FILE_MD5 STREQUAL ACTUAL_FILE_MD5))
12+
list(APPEND RESULT_FILES ${FILE_TO_CHECK})
13+
endif()
14+
else()
15+
list(APPEND RESULT_FILES ${FILE_TO_CHECK})
16+
endif()
17+
endforeach()
18+
19+
set(${MISSING_FILES} ${RESULT_FILES} PARENT_SCOPE)
20+
endfunction()

cmake/copy_torch_dlls.cmake

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
cmake_minimum_required(VERSION 3.14 FATAL_ERROR)
2+
3+
function(copy_torch_dlls TARGET_NAME)
4+
# According to https://github.com/pytorch/pytorch/issues/25457
5+
# the torch DLLs need to be copied to avoid memory errors.
6+
# (see also: https://pytorch.org/cppdocs/installing.html )
7+
# Here hardlinks to the DLLs are created instead (if possible), to safe disk space.
8+
list(GET CMAKE_MODULE_PATH 0 CMAKE_SCRIPT_DIR)
9+
10+
add_custom_command(TARGET ${TARGET_NAME}
11+
POST_BUILD
12+
COMMAND ${CMAKE_COMMAND}
13+
-D "TORCH_INSTALL_PREFIX=${TORCH_INSTALL_PREFIX}"
14+
-D "DESTINATION_DIR=$<TARGET_FILE_DIR:${TARGET_NAME}>"
15+
-P "${CMAKE_SCRIPT_DIR}/create_torch_dll_hardlinks.cmake")
16+
endfunction()

cmake/cpplint.cmake

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
cmake_minimum_required(VERSION 3.14 FATAL_ERROR)
2+
3+
find_program(CPPLINT cpplint)
4+
5+
if(CPPLINT)
6+
set(CPPLINT_COMMAND ${CPPLINT})
7+
else()
8+
get_filename_component(CPPLINT_PY_FILE "cpplint.py" ABSOLUTE BASE_DIR "${CMAKE_CURRENT_LIST_DIR}/../tools/")
9+
10+
if(NOT EXISTS ${CPPLINT_PY_FILE})
11+
message(STATUS "Downloading cpplint python script from https://raw.githubusercontent.com/cpplint/cpplint/master/cpplint.py...")
12+
file(DOWNLOAD "https://raw.githubusercontent.com/cpplint/cpplint/master/cpplint.py" ${CPPLINT_PY_FILE})
13+
message(STATUS "Downloading cpplint python script - done")
14+
endif()
15+
16+
message(STATUS "Using cpplint python script at ${CPPLINT_PY_FILE}")
17+
18+
set(CPPLINT_COMMAND python ${CPPLINT_PY_FILE})
19+
endif()
20+
21+
execute_process(COMMAND ${CPPLINT_COMMAND}
22+
"--linelength=120"
23+
"--recursive"
24+
"--filter=-build/include_subdir,-build/include_what_you_use"
25+
"--exclude=tutorials/advanced/utils/include/external/*"
26+
"main.cpp"
27+
"tutorials"
28+
WORKING_DIRECTORY "${CMAKE_CURRENT_LIST_DIR}/..")
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
cmake_minimum_required(VERSION 3.14 FATAL_ERROR)
2+
3+
file(GLOB TORCH_DLLS "${TORCH_INSTALL_PREFIX}/lib/*.dll")
4+
5+
foreach(TORCH_DLL_FILE ${TORCH_DLLS})
6+
get_filename_component(TORCH_DLL_NAME ${TORCH_DLL_FILE} NAME)
7+
# Create hardlink to the DLL files, if possible, otherwise fall back to copy.
8+
file(CREATE_LINK ${TORCH_DLL_FILE} "${DESTINATION_DIR}/${TORCH_DLL_NAME}" COPY_ON_ERROR)
9+
endforeach()

cmake/download_cifar10.cmake

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
cmake_minimum_required(VERSION 3.14 FATAL_ERROR)
2+
3+
set(CIFAR_DIR "${CMAKE_SOURCE_DIR}/data/cifar10")
4+
set(CIFAR_URL "https://www.cs.toronto.edu/~kriz/cifar-10-binary.tar.gz")
5+
set(CIFAR_EXTRACTED_FILES "data_batch_1.bin"
6+
"data_batch_2.bin"
7+
"data_batch_3.bin"
8+
"data_batch_4.bin"
9+
"data_batch_5.bin"
10+
"test_batch.bin")
11+
12+
set(CIFAR_EXTRACTED_FILE_MD5s "5dd7e06a14cb22eb9f671a540d1b7c25"
13+
"5ea93a67294ea407fff1d09f752e9692"
14+
"942cd6a4bcdd0dd3c604fbe906cb4421"
15+
"ae636b3ba5c66a11e91e8cb52e771fcb"
16+
"53f37980c15c3d472c316c40844f3f0d"
17+
"803d5f7f4d78ea53de84dbe85f74fb6d")
18+
19+
include(check_files)
20+
21+
check_files(${CIFAR_DIR} CIFAR_EXTRACTED_FILES CIFAR_EXTRACTED_FILE_MD5s MISSING_FILES)
22+
23+
if(NOT MISSING_FILES)
24+
message(STATUS "CIFAR10 dataset already present, skipping...")
25+
else()
26+
message(STATUS "Fetching CIFAR10 dataset...")
27+
set(CIFAR10_ARCHIVE_NAME "cifar-10-binary.tar.gz")
28+
set(DOWNLOAD_DIR "${CMAKE_SOURCE_DIR}/data")
29+
30+
file(REMOVE_RECURSE ${CIFAR_DIR})
31+
32+
file(DOWNLOAD ${CIFAR_URL} "${DOWNLOAD_DIR}/${CIFAR10_ARCHIVE_NAME}" EXPECTED_MD5 "c32a1d4ab5d03f1284b67883e8d87530")
33+
34+
execute_process(COMMAND ${CMAKE_COMMAND} -E tar x "${DOWNLOAD_DIR}/${CIFAR10_ARCHIVE_NAME}" WORKING_DIRECTORY ${DOWNLOAD_DIR})
35+
36+
execute_process(COMMAND ${CMAKE_COMMAND} -E rename "${DOWNLOAD_DIR}/cifar-10-batches-bin" ${CIFAR_DIR})
37+
38+
file(REMOVE "${DOWNLOAD_DIR}/${CIFAR10_ARCHIVE_NAME}")
39+
message(STATUS "Fetching CIFAR10 dataset - done")
40+
endif()

cmake/download_datasets.cmake

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
cmake_minimum_required(VERSION 3.14 FATAL_ERROR)
2+
3+
message(STATUS "Fetching datasets")
4+
include(download_mnist)
5+
include(download_cifar10)
6+
include(download_penntreebank)
7+
message(STATUS "Fetching datasets - done")

0 commit comments

Comments
 (0)