Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

path env example #230

Open
wants to merge 1 commit into
base: gh/PaliC/74/base
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions examples/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,6 @@ target_link_libraries(quickstart PUBLIC "-Wl,--no-as-needed -rdynamic" dl pthrea
add_executable(movable_example movable_example/movable_example.cpp)
target_link_libraries(movable_example PUBLIC "-Wl,--no-as-needed -rdynamic" dl pthread util multipy c10 torch_cpu)

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

1 change: 0 additions & 1 deletion examples/movable_example/movable_example.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ int main(int argc, const char* argv[]) {
try {
// Load the model from the torch.package.
auto I = m.acquireOne();
std::vector<torch::jit::IValue> constructor_inputs;
auto model_obj = I.global("torch.nn", "Conv2d")({6, 2, 2, 1});
auto rObj = m.createMovable(model_obj, &I);
auto I2 = m.acquireOne();
Expand Down
32 changes: 32 additions & 0 deletions examples/path_env/path_env.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// Basic example of using `ReplicatedObject` in `torch::deploy`.
#include <multipy/runtime/deploy.h>
#include <multipy/runtime/path_environment.h>
#include <torch/script.h>
#include <torch/torch.h>

#include <iostream>
#include <memory>

int main(int argc, const char* argv[]) {
torch::deploy::InterpreterManager m(4);
// Start an interpreter manager governing 4 embedded interpreters.
std::shared_ptr<multipy::runtime::Environment> env =
std::make_shared<multipy::runtime::PathEnvironment>(
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)
);

try {
// Load the model from the torch.package.
auto I = m.acquireOne();
auto model_obj = I.global("torch.nn", "Conv2d")({6, 2, 2, 1});
at::Tensor output = model_obj(inputs).toIValue().toTensor();
std::cout << output.slice(/*dim=*/1, /*start=*/0, /*end=*/5) << '\n';

} catch (const c10::Error& e) {
std::cerr << "error creating movable\n";
std::cerr << e.msg();
return -1;
}

std::cout << "ok\n";
}