Skip to content

Commit 0394707

Browse files
PaliCfacebook-github-bot
authored andcommitted
Add movable example (#225)
Summary: Pull Request resolved: #225 Creates a basic movable example and adds it to CI. Test Plan: Imported from OSS Reviewed By: d4l3k Differential Revision: D40646969 Pulled By: PaliC fbshipit-source-id: 18bde29f0e2f89def05033f95b246da2513487f1
1 parent 01ccea8 commit 0394707

File tree

3 files changed

+43
-0
lines changed

3 files changed

+43
-0
lines changed

.github/workflows/runtime_tests.yaml

+1
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ jobs:
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"
4242
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"
43+
docker run --rm multipy bash -c "if [[ ${{ matrix.python-minor-version }} -lt 8 ]]; then source ~/venvs/multipy/bin/activate; fi && ./examples/build/movable_example"
4344
4445
- name: Compat Tests
4546
run: |

examples/CMakeLists.txt

+4
Original file line numberDiff line numberDiff line change
@@ -23,3 +23,7 @@ target_link_libraries(hello_world_example PUBLIC "-Wl,--no-as-needed -rdynamic"
2323

2424
add_executable(quickstart quickstart/quickstart.cpp)
2525
target_link_libraries(quickstart PUBLIC "-Wl,--no-as-needed -rdynamic" dl pthread util multipy c10 torch_cpu)
26+
27+
add_executable(movable_example movable_example/movable_example.cpp)
28+
target_link_libraries(movable_example PUBLIC "-Wl,--no-as-needed -rdynamic" dl pthread util multipy c10 torch_cpu)
29+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
// Basic example of using `ReplicatedObject` in `torch::deploy`.
2+
#include <multipy/runtime/deploy.h>
3+
#include <multipy/runtime/path_environment.h>
4+
#include <torch/script.h>
5+
#include <torch/torch.h>
6+
7+
#include <iostream>
8+
#include <memory>
9+
10+
int main(int argc, const char* argv[]) {
11+
torch::deploy::InterpreterManager m(4);
12+
13+
try {
14+
// Load the model from the torch.package.
15+
auto I = m.acquireOne();
16+
std::vector<torch::jit::IValue> constructor_inputs;
17+
auto model_obj = I.global("torch.nn", "Conv2d")({6, 2, 2, 1});
18+
auto rObj = m.createMovable(model_obj, &I);
19+
auto I2 = m.acquireOne();
20+
auto model_obj2 = I2.fromMovable(rObj);
21+
rObj.unload(); // free the replicated object
22+
23+
// Create a vector of inputs.
24+
std::vector<torch::jit::IValue> inputs;
25+
inputs.push_back(torch::ones({1, 6, 6, 6}));
26+
27+
// Execute the model and turn its output into a tensor.
28+
at::Tensor output = model_obj2(inputs).toIValue().toTensor();
29+
std::cout << output.slice(/*dim=*/1, /*start=*/0, /*end=*/5) << '\n';
30+
31+
} catch (const c10::Error& e) {
32+
std::cerr << "error creating movable\n";
33+
std::cerr << e.msg();
34+
return -1;
35+
}
36+
37+
std::cout << "ok\n";
38+
}

0 commit comments

Comments
 (0)