[BE][flatbuffer] Remove code duplications and refactor (#79184)

Summary:
Remove code dup in import.cpp / export_modules.cpp such that
1. Only one copy of switching logic (detect flatbuffer / is_flatbuffer);
2. Move detection of includeness of flatbuffer to runtime (so no more macros)

This also reverts the dependency of import.cpp -> flatbuffer_loader.cpp to flatbuffer_loader.cpp -> import.cpp.

Differential Revision: D36926217

Pull Request resolved: https://github.com/pytorch/pytorch/pull/79184
Approved by: https://github.com/zhxchen17
This commit is contained in:
Han Qi (qihqi)
2022-06-20 16:37:38 +00:00
committed by PyTorch MergeBot
parent 7de231a813
commit fed12ff680
24 changed files with 617 additions and 560 deletions
+2 -16
View File
@@ -174,7 +174,7 @@ TEST(FlatbufferTest, MethodInvocation) { // NOLINT (use =delete in gtest)
}
}
#if defined(ENABLE_FLATBUFFER) && !defined(FB_XPLAT_BUILD)
#if !defined(FB_XPLAT_BUILD)
TEST(FlatbufferTest, FlatbufferBackPortTest) {
Module m("m");
m.define(R"(
@@ -188,7 +188,7 @@ TEST(FlatbufferTest, FlatbufferBackPortTest) {
bool backPortSuccess = _backport_for_mobile(ss, oss, 5);
ASSERT_TRUE(backPortSuccess);
}
#endif // defined(ENABLE_FLATBUFFER) && !defined(FB_XPLAT_BUILD)
#endif // !defined(FB_XPLAT_BUILD)
TEST(FlatbufferTest, ExtraFiles) {
const auto script = R"JIT(
@@ -207,7 +207,6 @@ TEST(FlatbufferTest, ExtraFiles) {
extra_files["mobile_info.json"] = "{\"key\": 23}";
std::unordered_map<std::string, std::string> loaded_extra_files;
#if defined ENABLE_FLATBUFFER
std::stringstream ss;
module->_save_for_mobile(ss, extra_files, true, /*use_flatbuffer=*/true);
@@ -219,17 +218,6 @@ TEST(FlatbufferTest, ExtraFiles) {
// load it twice using the same stream
auto mobile_module2 = _load_for_mobile(ss, c10::nullopt, loaded_extra_files);
#else
CompilationOptions options;
mobile::Module bc = jitModuleToMobile(*module, options);
auto buff = save_mobile_module_to_bytes(bc, extra_files);
loaded_extra_files["metadata.json"] = "";
auto* flatbuffer_module =
mobile::serialization::GetMutableModule(buff.data());
parseExtraFiles(flatbuffer_module, loaded_extra_files);
#endif
ASSERT_EQ(loaded_extra_files["metadata.json"], "abc");
ASSERT_EQ(loaded_extra_files["mobile_info.json"], "{\"key\": 23}");
@@ -1283,7 +1271,6 @@ Module jitModuleFromBuffer(void* data) {
mobilem._ivalue(), files, constants, 8);
}
#if defined(ENABLE_FLATBUFFER)
TEST(TestSourceFlatbuffer, UpsampleNearest2d) {
Module m("m");
m.define(R"(
@@ -1375,7 +1362,6 @@ TEST(TestSourceFlatbuffer,
AT_ASSERT(resd == refd);
}
}
#endif
#if !defined FB_XPLAT_BUILD
// The following test run in fbcode only