Add ability for a mobile::Module to save as flatbuffer (#67351)

Summary:
Included functions:

* save_mobile_module -> saves a mobile::Module to flatbuffer
* load_mobile_module_from_file -> loads a flatbuffer into mobile::Module
* parse_mobile_module -> parses from bytes or deserialized flatbuffer
      Module object

Fixes #{issue number}

Pull Request resolved: https://github.com/pytorch/pytorch/pull/67351

Reviewed By: iseeyuan

Differential Revision: D32010095

Pulled By: qihqi

fbshipit-source-id: d763b0557780f7c2661b6485105b045e41a5e8f1
This commit is contained in:
Han Qi
2021-12-01 23:56:41 -08:00
committed by Facebook GitHub Bot
parent 40fb28ea87
commit 41d35dc201
29 changed files with 2307 additions and 19 deletions
+57 -1
View File
@@ -2,13 +2,21 @@
#include <test/cpp/jit/test_utils.h>
#include <torch/csrc/jit/api/module.h>
#include <torch/csrc/jit/backends/backend_detail.h>
#include <torch/csrc/jit/mobile/flatbuffer_loader.h>
#include <torch/csrc/jit/mobile/import.h>
#include <torch/csrc/jit/serialization/flatbuffer_serializer.h>
#include <torch/csrc/jit/serialization/import.h>
#include <torch/torch.h>
// Tests go in torch::jit
namespace torch {
namespace jit {
mobile::Module load_mobile_module(void* data, size_t) {
auto* flatbuffer_module = mobile::serialization::GetMutableModule(data);
return initialize_mobile_module(flatbuffer_module);
}
TEST(BackendTest, ToBackend) {
Module m("m");
m.define(R"(
@@ -141,6 +149,11 @@ TEST(BackendTest, TestCompiler) {
auto mlm = _load_for_mobile(ss);
auto mres = mlm.forward(inputs);
AT_ASSERT(mres.toTensor().equal(ref.toTensor()));
auto buff = save_mobile_module_to_bytes(mlm);
mobile::Module mlm2 = load_mobile_module(buff.data(), buff.size());
auto mres2 = mlm2.forward(inputs);
AT_ASSERT(mres2.toTensor().equal(ref.toTensor()));
}
TEST(BackendTest, TestComposite) {
@@ -183,8 +196,12 @@ TEST(BackendTest, TestComposite) {
c._save_for_mobile(ss);
auto mc = _load_for_mobile(ss);
auto res_mobile = mc.forward(inputs);
AT_ASSERT(res_jit.toTensor().equal(res_mobile.toTensor()));
auto buff = save_mobile_module_to_bytes(mc);
mobile::Module mlm2 = load_mobile_module(buff.data(), buff.size());
auto mres2 = mlm2.forward(inputs);
AT_ASSERT(mres2.toTensor().equal(res_jit.toTensor()));
}
Module getCompositeModuleWithSameNameSubModules() {
@@ -241,6 +258,11 @@ TEST(BackendTest, TestCompositeWithSetStates) {
auto mc = _load_for_mobile(ss);
auto res_mobile = mc.forward(inputs);
AT_ASSERT(res_jit.toTensor().equal(res_mobile.toTensor()));
auto buff = save_mobile_module_to_bytes(mc);
mobile::Module mlm2 = load_mobile_module(buff.data(), buff.size());
auto mres2 = mlm2.forward(inputs);
AT_ASSERT(mres2.toTensor().equal(res_jit.toTensor()));
}
TEST(BackendTest, TestConsistencyOfCompositeWithSetStates) {
@@ -256,6 +278,11 @@ TEST(BackendTest, TestConsistencyOfCompositeWithSetStates) {
auto mc = _load_for_mobile(ss);
auto res_mobile = mc.forward(inputs);
auto buff = save_mobile_module_to_bytes(mc);
mobile::Module mlm2 = load_mobile_module(buff.data(), buff.size());
auto mres2 = mlm2.forward(inputs);
AT_ASSERT(mres2.toTensor().equal(res_mobile.toTensor()));
// check if the methods names are always the same
// by reloading the script module and saving it back as mobile
// The below checks ensure that the names of Methods
@@ -354,6 +381,13 @@ Traceback of TorchScript (most recent call last):
~~~~~ <--- HERE
)";
ASSERT_THROWS_WITH_MESSAGE(mlm.forward(inputs), error_pattern);
/* TODO(add debug info to flatbuffer)
auto buff = save_mobile_module_to_bytes(mlm);
mobile::Module mlm2 = load_mobile_module(buff.data(), buff.size());
mlm2.forward(inputs);
ASSERT_THROWS_WITH_MESSAGE(mlm2.forward(inputs), error_pattern);
*/
}
TEST(BackendTestDebugInfo, TestExceptionStackForCompilerWithModuleHierarchy) {
@@ -414,6 +448,12 @@ Traceback of TorchScript (most recent call last):
~~~~~ <--- HERE
)";
ASSERT_THROWS_WITH_MESSAGE(mlm.forward(inputs), error_pattern);
/* TODO(add debug info to flatbuffer)
auto buff = save_mobile_module_to_bytes(mlm);
mobile::Module mlm2 = load_mobile_module(buff.data(), buff.size());
ASSERT_THROWS_WITH_MESSAGE(mlm2.forward(inputs), error_pattern);
*/
}
TEST(
@@ -512,7 +552,13 @@ Traceback of TorchScript (most recent call last):
return x + y
~~~~~ <--- HERE
)";
ASSERT_THROWS_WITH_MESSAGE(mlm.forward(inputs), error_pattern);
/* TODO(add debug info to flatbuffer)
auto buff = save_mobile_module_to_bytes(mlm);
mobile::Module mlm2 = load_mobile_module(buff.data(), buff.size());
ASSERT_THROWS_WITH_MESSAGE(mlm2.forward(inputs), error_pattern);
*/
}
TEST(BackendTestDebugInfo, TestExceptionStackForCompilerWithLoweredSubModule) {
@@ -594,6 +640,11 @@ Traceback of TorchScript (most recent call last):
~~~~~ <--- HERE
)";
ASSERT_THROWS_WITH_MESSAGE(c_loaded.forward(inputs), error_pattern);
/* TODO(add debug info to flatbuffer)
auto buff = save_mobile_module_to_bytes(c_loaded);
mobile::Module mlm2 = load_mobile_module(buff.data(), buff.size());
ASSERT_THROWS_WITH_MESSAGE(mlm2.forward(inputs), error_pattern);
*/
}
TEST(
@@ -721,6 +772,11 @@ Traceback of TorchScript (most recent call last):
~~~~~ <--- HERE
)";
ASSERT_THROWS_WITH_MESSAGE(c_loaded.forward(inputs), error_pattern);
/* TODO(add debug info to flatbuffer)
auto buff = save_mobile_module_to_bytes(c_loaded);
mobile::Module mlm2 = load_mobile_module(buff.data(), buff.size());
ASSERT_THROWS_WITH_MESSAGE(mlm2.forward(inputs), error_pattern);
*/
}
} // namespace jit