Skip to content

Commit a8755bb

Browse files
committed
torch dyanamo in c++ test
ghstack-source-id: 53fa37b8b40ac4c4b4778cef83f9405aba1d6259 Pull Request resolved: #299
1 parent 2e9a48a commit a8755bb

File tree

3 files changed

+84
-0
lines changed

3 files changed

+84
-0
lines changed

multipy/runtime/CMakeLists.txt

+12
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,10 @@ set(INTERPRETER_TEST_SOURCES_GPU
8888
${DEPLOY_DIR}/test_deploy_gpu.cpp
8989
)
9090

91+
set(INTERPRETER_TEST_SOURCES_COMPAT
92+
${DEPLOY_DIR}/test_dynamo_compat.cpp
93+
)
94+
9195
# TODO: Currently tests can only be done when ABI=1 as the testing infrustructure
9296
# used by ASSERT_TRUE requires ABI=1 in Github actions, we should fix this!
9397

@@ -99,6 +103,14 @@ target_link_libraries(test_deploy
99103
)
100104
target_include_directories(test_deploy PRIVATE ${CMAKE_SOURCE_DIR}/../..)
101105

106+
add_executable(test_compat ${INTERPRETER_TEST_SOURCES_COMPAT})
107+
# target_compile_definitions(test_compat PUBLIC TEST_CUSTOM_LIBRARY)
108+
target_include_directories(test_compat PRIVATE ${PYTORCH_ROOT}/torch)
109+
target_link_libraries(test_compat
110+
PUBLIC "-Wl,--no-as-needed -rdynamic" gtest dl torch_deploy_interface c10 torch_cpu
111+
)
112+
target_include_directories(test_compat PRIVATE ${CMAKE_SOURCE_DIR}/../..)
113+
102114
if(BUILD_CUDA_TESTS)
103115
LINK_DIRECTORIES("${PYTORCH_ROOT}/torch/lib")
104116
add_executable(test_deploy_gpu ${INTERPRETER_TEST_SOURCES_GPU})

multipy/runtime/test_deploy.cpp

+4
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,10 @@ TEST(TorchpyTest, SimpleModel) {
8888
compare_torchpy_jit(path("SIMPLE", simple), path("SIMPLE_JIT", simple_jit));
8989
}
9090

91+
TEST(TorchpyTest, DynamoTest) {
92+
93+
}
94+
9195
#ifdef FBCODE_CAFFE2
9296
TEST(TorchpyTest, LoadTextAndBinary) {
9397
torch::deploy::InterpreterManager manager(1);
+68
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
// Copyright (c) Meta Platforms, Inc. and affiliates.
2+
// All rights reserved.
3+
//
4+
// This source code is licensed under the BSD-style license found in the
5+
// LICENSE file in the root directory of this source tree.
6+
7+
#include <ATen/Parallel.h>
8+
#include <gtest/gtest.h>
9+
#include <libgen.h>
10+
#include <cstring>
11+
12+
#include <c10/util/irange.h>
13+
#include <libgen.h>
14+
#include <multipy/runtime/deploy.h>
15+
#include <torch/script.h>
16+
#include <torch/torch.h>
17+
18+
#include <future>
19+
#include <iostream>
20+
#include <string>
21+
22+
void compare_torchpy_jit(const char* model_filename, const char* jit_filename) {
23+
// Test
24+
25+
torch::deploy::InterpreterManager m(2);
26+
torch::deploy::Package p = m.loadPackage(model_filename);
27+
auto model = p.loadPickle("model", "model.pkl");
28+
at::IValue eg;
29+
{
30+
auto I = p.acquireSession();
31+
eg = I.self.attr("load_pickle")({"model", "example.pkl"}).toIValue();
32+
}
33+
auto I = p.acquireSession();
34+
auto cModelObj = I.global("torch", "compile")(model.toObj(&I));
35+
auto cModel = m.createMovable(cModelObj, &I);
36+
at::Tensor output = cModel(eg.toTupleRef().elements()).toTensor();
37+
38+
// Reference
39+
auto ref_model = torch::jit::load(jit_filename);
40+
at::Tensor ref_output =
41+
ref_model.forward(eg.toTupleRef().elements()).toTensor();
42+
43+
ASSERT_TRUE(ref_output.allclose(output, 1e-03, 1e-05));
44+
}
45+
46+
const char* simple = "multipy/runtime/example/generated/simple";
47+
const char* simple_jit = "multipy/runtime/example/generated/simple_jit";
48+
49+
const char* path(const char* envname, const char* path) {
50+
const char* e = getenv(envname);
51+
return e ? e : path;
52+
}
53+
54+
TEST(TorchpyTest, SimpleModel) {
55+
compare_torchpy_jit(path("SIMPLE", simple), path("SIMPLE_JIT", simple_jit));
56+
}
57+
58+
int main(int argc, char* argv[]) {
59+
::testing::InitGoogleTest(&argc, argv);
60+
char tempeh[256];
61+
getcwd(tempeh, 256);
62+
std::cout << "Current working directory: " << tempeh << std::endl;
63+
int rc = RUN_ALL_TESTS();
64+
char tmp[256];
65+
getcwd(tmp, 256);
66+
std::cout << "Current working directory: " << tmp << std::endl;
67+
return rc;
68+
}

0 commit comments

Comments
 (0)