Skip to content

Commit 01ccea8

Browse files
PaliCfacebook-github-bot
authored andcommitted
Add working code for quickstart example (#190)
Summary: Pull Request resolved: #190 Adds working code for our quickstart example, and integrates into CI to ensure that it actually works. Test Plan: Imported from OSS Reviewed By: d4l3k Differential Revision: D40645620 Pulled By: PaliC fbshipit-source-id: 259784746a5a683204fc0deff55335540c26639f
1 parent 5020ceb commit 01ccea8

File tree

6 files changed

+74
-1
lines changed

6 files changed

+74
-1
lines changed

.github/workflows/runtime_tests.yaml

+1
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ jobs:
3939
- name: Examples
4040
run: |
4141
docker run --rm multipy bash -c "if [[ ${{ matrix.python-minor-version }} -lt 8 ]]; then source ~/venvs/multipy/bin/activate; fi && examples/build/hello_world_example"
42+
docker run --rm multipy bash -c "if [[ ${{ matrix.python-minor-version }} -lt 8 ]]; then source ~/venvs/multipy/bin/activate; fi && python3 examples/quickstart/gen_package.py && ./examples/build/quickstart my_package.pt"
4243
4344
- name: Compat Tests
4445
run: |

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,5 @@ multipy/runtime/interpreter/cpython/**
99
**/CMakeFiles/**
1010
multipy/runtime/interpreter/frozen/**
1111
multipy/runtime/example/generated/
12+
examples/**/*.pt
1213
*.egg-info

examples/CMakeLists.txt

+4-1
Original file line numberDiff line numberDiff line change
@@ -18,5 +18,8 @@ set_target_properties(multipy_internal
1818
caffe2_interface_library(multipy_internal multipy)
1919

2020
# build our examples
21-
add_executable(hello_world_example hello_world_example.cpp)
21+
add_executable(hello_world_example hello_world/hello_world_example.cpp)
2222
target_link_libraries(hello_world_example PUBLIC "-Wl,--no-as-needed -rdynamic" dl pthread util multipy c10 torch_cpu)
23+
24+
add_executable(quickstart quickstart/quickstart.cpp)
25+
target_link_libraries(quickstart PUBLIC "-Wl,--no-as-needed -rdynamic" dl pthread util multipy c10 torch_cpu)

examples/hello_world_example.cpp renamed to examples/hello_world/hello_world_example.cpp

+4
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
/* Basic example of a single interpreter from `torch::deploy`
2+
to invoke python methods directly. Here we specifically
3+
invoke `print` to print out `Hello World`.
4+
*/
15
#include <multipy/runtime/deploy.h>
26
#include <multipy/runtime/path_environment.h>
37
#include <torch/script.h>

examples/quickstart/gen_package.py

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# An example usage of `torch.package` which is
2+
# used for our quickstart example.
3+
4+
import torchvision
5+
from torch.package import PackageExporter
6+
7+
# Instantiate some model
8+
model = torchvision.models.resnet.resnet18()
9+
10+
# Package and export it.
11+
with PackageExporter("my_package.pt") as e:
12+
e.intern("torchvision.**")
13+
e.extern("numpy.**")
14+
e.extern("sys")
15+
e.extern("PIL.*")
16+
e.extern("typing_extensions")
17+
e.save_pickle("model", "model.pkl", model)

examples/quickstart/quickstart.cpp

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/* quickstart.cpp highlights some of the most basic concepts of `torch::deploy`.
2+
Specifically loading a pytorch model which was serialized by `torch.package`
3+
and running it.
4+
5+
In order to run this file, one needs to provide an archive produced by
6+
`torch.package`. The one used in our example is created by `gen_package.py`
7+
which produces my_package.pt. This program takes the path to the archive as
8+
an argument.
9+
*/
10+
11+
#include <multipy/runtime/deploy.h>
12+
#include <multipy/runtime/path_environment.h>
13+
#include <torch/script.h>
14+
#include <torch/torch.h>
15+
16+
#include <iostream>
17+
#include <memory>
18+
19+
int main(int argc, const char* argv[]) {
20+
if (argc != 2) {
21+
std::cerr << "usage: example-app <path-to-exported-script-module>\n";
22+
return -1;
23+
}
24+
25+
// Start an interpreter manager governing 4 embedded interpreters.
26+
torch::deploy::InterpreterManager manager(4);
27+
torch::deploy::ReplicatedObj model;
28+
try {
29+
// Load the model from the multipy.package.
30+
torch::deploy::Package package = manager.loadPackage(argv[1]);
31+
model = package.loadPickle("model", "model.pkl");
32+
} catch (const c10::Error& e) {
33+
std::cerr << "error loading the model\n";
34+
std::cerr << e.msg();
35+
return -1;
36+
}
37+
38+
// Create a vector of inputs.
39+
std::vector<torch::jit::IValue> inputs;
40+
inputs.push_back(torch::ones({1, 3, 224, 224}));
41+
42+
// Execute the model and turn its output into a tensor.
43+
at::Tensor output = model(inputs).toTensor();
44+
std::cout << output.slice(/*dim=*/1, /*start=*/0, /*end=*/5) << '\n';
45+
46+
std::cout << "ok\n";
47+
}

0 commit comments

Comments
 (0)