mirror of
https://github.com/pytorch/pytorch.git
synced 2026-11-20 09:04:25 +08:00
[jit] Pull (most) tests out of libtorch_python (#44795)
Summary: Pull Request resolved: https://github.com/pytorch/pytorch/pull/44795 Today, we build our cpp tests twice, once as a standalone gtest binary, and once linked in `libtorch_python` so we can call them from `test_jit.py`. This is convenient (it means that `test_jit.py` is a single entry point for all our tests), but has a few drawbacks: 1. We can't actually use the gtest APIs, since we don't link gtest into `libtorch_python`. We're stuck with the subset that we want to write polyfills for, and an awkward registration scheme where you have to write a test then include it in `tests.h`). 2. More seriously, we register custom operators and classes in these tests. In a world where we may be linking many `libtorch_python`s, this has a tendency to cause errors with `libtorch`. So now, only tests that explicitly require cooperation with Python are built into `libtorch_python`. The rest are built into `build/bin/test_jit`. There are tests which require that we define custom classes and operators. In these cases, I've built thm into separate `.so`s that we call `torch.ops.load_library()` on. Test Plan: Imported from OSS Reviewed By: SplitInfinity, ZolotukhinM Differential Revision: D23735520 Pulled By: suo fbshipit-source-id: d146bf4e7eb908afa6f96b394e4d395d63ad72ff
This commit is contained in:
committed by
Facebook GitHub Bot
parent
af3fc9725d
commit
374e9373b5
@@ -1,6 +1,8 @@
|
||||
#include <ATen/core/ivalue.h>
|
||||
#include <c10/util/Exception.h>
|
||||
#include <test/cpp/jit/tests.h>
|
||||
#include <test/cpp/tensorexpr/tests.h>
|
||||
#include <torch/csrc/WindowsTorchApiMacro.h>
|
||||
#include <torch/csrc/jit/api/module.h>
|
||||
#include <torch/script.h>
|
||||
|
||||
namespace torch {
|
||||
namespace jit {
|
||||
@@ -11,32 +13,73 @@ namespace jit {
|
||||
#define JIT_TEST_API TORCH_API
|
||||
#endif
|
||||
|
||||
#define JIT_TEST(name) test##name();
|
||||
JIT_TEST_API void runJITCPPTests(bool runCuda) {
|
||||
TH_FORALL_TESTS(JIT_TEST)
|
||||
if (runCuda) {
|
||||
TH_FORALL_TESTS_CUDA(JIT_TEST)
|
||||
namespace {
|
||||
|
||||
bool isSandcastle() {
|
||||
return (
|
||||
(std::getenv("SANDCASTLE")) ||
|
||||
(std::getenv("TW_JOB_USER") &&
|
||||
std::string(std::getenv("TW_JOB_USER")) == "sandcastle"));
|
||||
}
|
||||
|
||||
void testEvalModeForLoadedModule() {
|
||||
if (isSandcastle())
|
||||
return; // The module file to load is not generated in Sandcastle
|
||||
std::string module_path = "dropout_model.pt";
|
||||
torch::jit::Module module = torch::jit::load(module_path);
|
||||
AT_ASSERT(module.attr("dropout").toModule().is_training());
|
||||
module.eval();
|
||||
AT_ASSERT(!module.attr("dropout").toModule().is_training());
|
||||
module.train();
|
||||
AT_ASSERT(module.attr("dropout").toModule().is_training());
|
||||
}
|
||||
|
||||
void testSerializationInterop() {
|
||||
if (isSandcastle()) {
|
||||
// The module file to load is not generated in Sandcastle
|
||||
return;
|
||||
}
|
||||
|
||||
// This test is special since it requires prior setup in python.
|
||||
// So it is not part of the general test list (which is shared between the
|
||||
// gtest and python test runners), but is instead invoked manually by the
|
||||
// torch_python_test.cpp
|
||||
// This should be generated by `test/cpp/jit/tests_setup.py`
|
||||
std::ifstream input_stream("ivalue.pt");
|
||||
std::vector<char> input;
|
||||
input.insert(
|
||||
input.begin(),
|
||||
std::istream_iterator<char>(input_stream),
|
||||
std::istream_iterator<char>());
|
||||
IValue ivalue = pickle_load(input);
|
||||
|
||||
auto elements = ivalue.toTuple()->elements();
|
||||
auto ones = torch::ones({2, 2});
|
||||
AT_ASSERT(ones.equal(elements.at(0).toTensor()));
|
||||
|
||||
auto twos = torch::ones({3, 5}) * 2;
|
||||
AT_ASSERT(twos.equal(elements.at(1).toTensor()));
|
||||
}
|
||||
|
||||
void testTorchSaveError() {
|
||||
if (isSandcastle()) {
|
||||
// The file to load is not generated in Sandcastle
|
||||
return;
|
||||
}
|
||||
|
||||
// This should be generated by `test/cpp/jit/tests_setup.py`
|
||||
bool passed = true;
|
||||
try {
|
||||
torch::jit::load("eager_value.pt");
|
||||
passed = false;
|
||||
} catch (const std::exception& c) {
|
||||
}
|
||||
// Ensure torch::jit::load did not run
|
||||
AT_ASSERT(passed);
|
||||
}
|
||||
} // namespace
|
||||
|
||||
JIT_TEST_API void runJITCPPTests() {
|
||||
// TODO: this test never ran before and is broken.
|
||||
// testSerializationInterop();
|
||||
testEvalModeForLoadedModule();
|
||||
testTorchSaveError();
|
||||
}
|
||||
#undef JIT_TEST
|
||||
|
||||
#define JIT_TEST(name) test##name();
|
||||
JIT_TEST_API void runTENSOREXPRCPPTests(bool runCuda) {
|
||||
TH_FORALL_TENSOREXPR_TESTS(JIT_TEST)
|
||||
if (runCuda) {
|
||||
#ifdef USE_CUDA
|
||||
TH_FORALL_TENSOREXPR_TESTS_CUDA(JIT_TEST)
|
||||
#endif
|
||||
}
|
||||
}
|
||||
#undef JIT_TEST
|
||||
|
||||
} // namespace jit
|
||||
} // namespace torch
|
||||
|
||||
Reference in New Issue
Block a user