|
| 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