Skip to content

Commit 5289675

Browse files
committed
path env example
ghstack-source-id: aca654ad4e7475605aa3e2a81270c266b0ff4e41 Pull Request resolved: #230
1 parent f5d0290 commit 5289675

File tree

3 files changed

+35
-1
lines changed

3 files changed

+35
-1
lines changed

examples/CMakeLists.txt

+3
Original file line numberDiff line numberDiff line change
@@ -27,3 +27,6 @@ target_link_libraries(quickstart PUBLIC "-Wl,--no-as-needed -rdynamic" dl pthrea
2727
add_executable(movable_example movable_example/movable_example.cpp)
2828
target_link_libraries(movable_example PUBLIC "-Wl,--no-as-needed -rdynamic" dl pthread util multipy c10 torch_cpu)
2929

30+
add_executable(movable_example path_env/path_env.cpp)
31+
target_link_libraries(path_env PUBLIC "-Wl,--no-as-needed -rdynamic" dl pthread util multipy c10 torch_cpu)
32+

examples/movable_example/movable_example.cpp

-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ int main(int argc, const char* argv[]) {
1313
try {
1414
// Load the model from the torch.package.
1515
auto I = m.acquireOne();
16-
std::vector<torch::jit::IValue> constructor_inputs;
1716
auto model_obj = I.global("torch.nn", "Conv2d")({6, 2, 2, 1});
1817
auto rObj = m.createMovable(model_obj, &I);
1918
auto I2 = m.acquireOne();

examples/path_env/path_env.cpp

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
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+
// Start an interpreter manager governing 4 embedded interpreters.
13+
std::shared_ptr<multipy::runtime::Environment> env =
14+
std::make_shared<multipy::runtime::PathEnvironment>(
15+
std::getenv("PATH_TO_EXTERN_PYTHON_PACKAGES") // Ensure to set this environment variable (e.g. /home/user/anaconda3/envs/multipy-example/lib/python3.8/site-packages)
16+
);
17+
18+
try {
19+
// Load the model from the torch.package.
20+
auto I = m.acquireOne();
21+
auto model_obj = I.global("torch.nn", "Conv2d")({6, 2, 2, 1});
22+
at::Tensor output = model_obj(inputs).toIValue().toTensor();
23+
std::cout << output.slice(/*dim=*/1, /*start=*/0, /*end=*/5) << '\n';
24+
25+
} catch (const c10::Error& e) {
26+
std::cerr << "error creating movable\n";
27+
std::cerr << e.msg();
28+
return -1;
29+
}
30+
31+
std::cout << "ok\n";
32+
}

0 commit comments

Comments
 (0)