Skip to content

Commit 6005029

Browse files
authored
Add scriptmodule creation option to cmake (#62)
1 parent aaf9303 commit 6005029

File tree

9 files changed

+74
-21
lines changed

9 files changed

+74
-21
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ cmake-build-*/
6161
# Other
6262
.DS_Store
6363
.idea/
64+
.clangd/
6465

6566
# Notebooks
6667
.ipynb_checkpoints

CMakeLists.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ project(pytorch-cpp VERSION 1.0.0 LANGUAGES CXX)
55
list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_LIST_DIR}/cmake")
66

77
option(DOWNLOAD_DATASETS "Download datasets used in the tutorials." ON)
8+
option(CREATE_SCRIPTMODULES "Create all necessary scriptmodule files." OFF)
89

910
set(PYTORCH_VERSION "1.6.0")
1011

@@ -19,6 +20,10 @@ if(DOWNLOAD_DATASETS)
1920
include(download_datasets)
2021
endif()
2122

23+
if(CREATE_SCRIPTMODULES)
24+
find_package(Python3 COMPONENTS Interpreter REQUIRED)
25+
endif()
26+
2227
set(EXECUTABLE_NAME pytorch-cpp)
2328

2429
add_executable(${EXECUTABLE_NAME})

README.md

Lines changed: 40 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
</p>
1212

1313

14-
| OS (Compiler)\\libtorch | 1.6.0 | nightly |
14+
| OS (Compiler)\\LibTorch | 1.6.0 | nightly |
1515
| :---------------------: | :---------------------------------------------------------------------------------------------------: | :-----: |
1616
| macOS (clang 9.1) | ![Status](https://travis-matrix-badges.herokuapp.com/repos/prabhuomkar/pytorch-cpp/branches/master/1) | |
1717
| macOS (clang 10.0) | ![Status](https://travis-matrix-badges.herokuapp.com/repos/prabhuomkar/pytorch-cpp/branches/master/2) | |
@@ -58,7 +58,7 @@ cd pytorch-cpp
5858

5959
#### Generate build system
6060

61-
```cmake
61+
```bash
6262
cmake -B build #<options>
6363
```
6464
> **_Note for Windows users:_**<br>
@@ -68,18 +68,50 @@ Some useful options:
6868

6969
| Option | Default | Description |
7070
| :------------- |:------------|-----:|
71-
| `-D CUDA_V=(9.2 [Linux only]\|10.1\|10.2\|none)` | `none` | Download libtorch for a CUDA version (`none` = download CPU version). |
71+
| `-D CUDA_V=(9.2 [Linux only]\|10.1\|10.2\|none)` | `none` | Download LibTorch for a CUDA version (`none` = download CPU version). |
7272
| `-D DOWNLOAD_DATASETS=(OFF\|ON)` | `ON` | Download all datasets used in the tutorials. |
73-
| `-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. |
74-
| `-D CMAKE_BUILD_TYPE=(Release\|Debug)` | `<empty>` (`Release` when downloading libtorch on Windows) | Set the build type (`Release` = compile with optimizations)|
73+
| `-D CMAKE_PREFIX_PATH=path/to/libtorch/share/cmake/Torch` | `<empty>` | Skip the downloading of LibTorch and use your own local version (see [Requirements](#requirements)) instead. |
74+
| `-D CMAKE_BUILD_TYPE=(Release\|Debug)` | `<empty>` (`Release` when downloading LibTorch on Windows) | Set the build type (`Release` = compile with optimizations).|
75+
|`-D CREATE_SCRIPTMODULES=(OFF\|ON)` | `OFF` | Create all needed scriptmodule files for prelearned models / weights. Requires installed python3 with pytorch and torchvision. |
76+
77+
<details>
78+
<summary><b>Example Linux</b></summary>
79+
80+
##### Aim
81+
* Use existing Python, PyTorch (see [Requirements](#requirements)) and torchvision installation.
82+
* Download all datasets and create all necessary scriptmodule files.
83+
84+
##### Command
85+
```bash
86+
cmake -B build \
87+
-D CMAKE_BUILD_TYPE=Release \
88+
-D CMAKE_PREFIX_PATH=/path/to/libtorch/share/cmake/Torch \
89+
-D CREATE_SCRIPTMODULES=ON
90+
```
91+
</details>
92+
93+
<details>
94+
<summary><b>Example Windows</b></summary>
95+
96+
##### Aim
97+
* Automatically download LibTorch for CUDA 10.2 and all necessary datasets.
98+
* Do not create scriptmodule files.
99+
100+
##### Command
101+
```bash
102+
cmake -B build \
103+
-A x64 \
104+
-D CUDA_V=10.2
105+
```
106+
</details>
75107

76108
#### Build
77109

78-
```cmake
110+
```bash
79111
cmake --build build
80112
```
81113
>**_Note for Windows users:_** <br>
82-
>The CMake script downloads the *Release* version of libtorch, so `--config Release` has to be appended to the build command.
114+
>The CMake script downloads the *Release* version of LibTorch, so `--config Release` has to be appended to the build command.
83115
>
84116
>**_General Note:_** <br>
85117
>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).
@@ -126,4 +158,4 @@ cmake --build build
126158
* [Tensor Slicing](notebooks/tensor_slicing.ipynb)
127159

128160
## License
129-
This repository is licensed under MIT as given in [LICENSE](LICENSE).
161+
This repository is licensed under MIT as given in [LICENSE](LICENSE).

cmake/download_flickr8k.cmake

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,25 +7,25 @@ set(FLICKR8K_DIR "${CMAKE_SOURCE_DIR}/data/flickr_8k")
77
set(FLICKR8K_DATA_URL "https://github.com/jbrownlee/Datasets/releases/download/Flickr8k/Flickr8k_Dataset.zip")
88
set(FLICKR8K_TEXT_URL "https://github.com/jbrownlee/Datasets/releases/download/Flickr8k/Flickr8k_text.zip")
99

10-
set(FLICKR8k_DATA_SOURCE_DIR "${FLICKR8K_DIR}/Flickr8k_Dataset")
11-
set(FLICKR8k_TEXT_SOURCE_DIR "${FLICKR8K_DIR}/Flickr8k_text")
10+
set(FLICKR8K_DATA_SOURCE_DIR "${FLICKR8K_DIR}/Flickr8k_Dataset")
11+
set(FLICKR8K_TEXT_SOURCE_DIR "${FLICKR8K_DIR}/Flickr8k_text")
1212

13-
if(NOT EXISTS ${FLICKR8k_TEXT_SOURCE_DIR} OR NOT EXISTS ${FLICKR8k_DATA_SOURCE_DIR})
13+
if(NOT EXISTS ${FLICKR8K_TEXT_SOURCE_DIR} OR NOT EXISTS ${FLICKR8K_DATA_SOURCE_DIR})
1414
message(STATUS "Fetching Flickr8k dataset...")
1515

1616
FetchContent_Declare(
1717
flickr_8k_text
1818
DOWNLOAD_DIR ${FLICKR8K_DIR}/download
19-
SOURCE_DIR ${FLICKR8k_TEXT_SOURCE_DIR}
19+
SOURCE_DIR ${FLICKR8K_TEXT_SOURCE_DIR}
2020
URL ${FLICKR8K_TEXT_URL}
2121
)
2222

2323
FetchContent_MakeAvailable(flickr_8k_text)
24-
24+
set(FETCHCONTENT_QUIET OFF CACHE BOOL "" FORCE)
2525
FetchContent_Declare(
2626
flickr_8k_data
2727
DOWNLOAD_DIR ${FLICKR8K_DIR}/download
28-
SOURCE_DIR ${FLICKR8k_DATA_SOURCE_DIR}
28+
SOURCE_DIR ${FLICKR8K_DATA_SOURCE_DIR}
2929
URL ${FLICKR8K_DATA_URL}
3030
)
3131

@@ -36,4 +36,4 @@ if(NOT EXISTS ${FLICKR8k_TEXT_SOURCE_DIR} OR NOT EXISTS ${FLICKR8k_DATA_SOURCE_D
3636
message(STATUS "Fetching Flickr8k dataset - done")
3737
else()
3838
message(STATUS "Flickr8k dataset already present, skipping...")
39-
endif()
39+
endif()

tutorials/advanced/image_captioning/CMakeLists.txt

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,15 @@ set_target_properties(${EXECUTABLE_NAME} PROPERTIES
4141
add_custom_command(TARGET ${EXECUTABLE_NAME}
4242
POST_BUILD
4343
COMMAND ${CMAKE_COMMAND} -E make_directory output)
44+
45+
if(CREATE_SCRIPTMODULES AND NOT EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/model/encoder_cnn_backbone.pt")
46+
execute_process(COMMAND ${Python3_EXECUTABLE} "create_encoder_cnn_backbone_scriptmodule.py"
47+
WORKING_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}/model")
48+
endif()
4449

4550
if(MSVC)
4651
set_target_properties(${EXECUTABLE_NAME} PROPERTIES RUNTIME_OUTPUT_DIRECTORY_RELEASE ${CMAKE_CURRENT_BINARY_DIR})
4752
set_target_properties(${EXECUTABLE_NAME} PROPERTIES RUNTIME_OUTPUT_DIRECTORY_DEBUG ${CMAKE_CURRENT_BINARY_DIR})
4853
include(copy_torch_dlls)
4954
copy_torch_dlls(${EXECUTABLE_NAME})
50-
endif(MSVC)
55+
endif(MSVC)

tutorials/advanced/neural_style_transfer/CMakeLists.txt

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,14 @@ add_custom_command(TARGET ${EXECUTABLE_NAME}
2727
POST_BUILD
2828
COMMAND ${CMAKE_COMMAND} -E make_directory output)
2929

30+
if(CREATE_SCRIPTMODULES AND NOT EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/model/vgg19_layers.pt")
31+
execute_process(COMMAND ${Python3_EXECUTABLE} "create_vgg19_layers_scriptmodule.py"
32+
WORKING_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}/model")
33+
endif()
34+
3035
if(MSVC)
3136
set_target_properties(${EXECUTABLE_NAME} PROPERTIES RUNTIME_OUTPUT_DIRECTORY_RELEASE ${CMAKE_CURRENT_BINARY_DIR})
3237
set_target_properties(${EXECUTABLE_NAME} PROPERTIES RUNTIME_OUTPUT_DIRECTORY_DEBUG ${CMAKE_CURRENT_BINARY_DIR})
3338
include(copy_torch_dlls)
3439
copy_torch_dlls(${EXECUTABLE_NAME})
35-
endif(MSVC)
40+
endif(MSVC)

tutorials/basics/pytorch_basics/CMakeLists.txt

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,14 @@ add_custom_command(TARGET ${EXECUTABLE_NAME}
2323
POST_BUILD
2424
COMMAND ${CMAKE_COMMAND} -E make_directory output)
2525

26+
if(CREATE_SCRIPTMODULES AND NOT EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/model/resnet18_scriptmodule.pt")
27+
execute_process(COMMAND ${Python3_EXECUTABLE} "create_resnet18_scriptmodule.py"
28+
WORKING_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}/model")
29+
endif()
30+
2631
if(MSVC)
2732
set_target_properties(${EXECUTABLE_NAME} PROPERTIES RUNTIME_OUTPUT_DIRECTORY_RELEASE ${CMAKE_CURRENT_BINARY_DIR})
2833
set_target_properties(${EXECUTABLE_NAME} PROPERTIES RUNTIME_OUTPUT_DIRECTORY_DEBUG ${CMAKE_CURRENT_BINARY_DIR})
2934
include(copy_torch_dlls)
3035
copy_torch_dlls(${EXECUTABLE_NAME})
31-
endif(MSVC)
36+
endif(MSVC)

tutorials/basics/pytorch_basics/main.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -232,8 +232,8 @@ int main() {
232232
// Path to serialized ScriptModule of pretrained resnet18 model,
233233
// created in Python.
234234
// You can use the provided Python-script "create_resnet18_scriptmodule.py" in
235-
// tutorials/basics/pytorch-basics/models to create the necessary file.
236-
const std::string pretrained_model_path = "../../../../tutorials/basics/pytorch_basics/models/"
235+
// tutorials/basics/pytorch-basics/model to create the necessary file.
236+
const std::string pretrained_model_path = "../../../../tutorials/basics/pytorch_basics/model/"
237237
"resnet18_scriptmodule.pt";
238238

239239
torch::jit::script::Module resnet;
@@ -244,7 +244,7 @@ int main() {
244244
catch (const torch::Error& error) {
245245
std::cerr << "Could not load scriptmodule from file " << pretrained_model_path << ".\n"
246246
<< "You can create this file using the provided Python script 'create_resnet18_scriptmodule.py' "
247-
"in tutorials/basics/pytorch-basics/models/.\n";
247+
"in tutorials/basics/pytorch-basics/model/.\n";
248248
return -1;
249249
}
250250

0 commit comments

Comments
 (0)