Skip to content

Commit

Permalink
Back out "[pytorch][PR] Add ability for a mobile::Module to save as f…
Browse files Browse the repository at this point in the history
…latbuffer" (pytorch#69796)

Summary:
Pull Request resolved: pytorch#69796

(Note: this ignores all push blocking failures!)

Test Plan: External CI + Sandcastle

Reviewed By: zhxchen17

Differential Revision: D33032671

fbshipit-source-id: dbf6690e960e25d6a5f19043cbe792add2acd7ef
  • Loading branch information
gmagogsfm authored and facebook-github-bot committed Dec 11, 2021
1 parent 3906f82 commit 17f3179
Show file tree
Hide file tree
Showing 29 changed files with 19 additions and 2,348 deletions.
3 changes: 0 additions & 3 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -311,6 +311,3 @@ pr.diff

# coverage files
*/**/.coverage.*

# generated flatbuffer schema header
torch/csrc/jit/serialization/mobile_bytecode_generated.h
3 changes: 0 additions & 3 deletions .gitmodules
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,3 @@
[submodule "third_party/breakpad"]
path = third_party/breakpad
url = https://github.com/driazati/breakpad.git
[submodule "third_party/flatbuffers"]
path = third_party/flatbuffers
url = https://github.com/google/flatbuffers.git
2 changes: 0 additions & 2 deletions .jenkins/pytorch/win-build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,6 @@ if [[ $PYLONG_API_CHECK == 0 ]]; then
fi
set -ex

echo 'python %*' > /c/Windows/py.bat

"$SCRIPT_HELPERS_DIR"/build_pytorch.bat

assert_git_not_dirty
Expand Down
38 changes: 0 additions & 38 deletions BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ load("//:tools/build_variables.bzl", "torch_cpp_srcs", "libtorch_python_core_sou
load("//tools/rules:cu.bzl", "cu_library")
load("//tools/config:defs.bzl", "if_cuda")
load("//:aten.bzl", "intern_build_aten_ops")
load("@com_github_google_flatbuffers//:build_defs.bzl", "flatbuffer_cc_library")

COMMON_COPTS = [
"-DHAVE_MALLOC_USABLE_SIZE=1",
Expand Down Expand Up @@ -1810,14 +1809,6 @@ genrule(
tools = [':gen_version_header']
)

flatbuffer_cc_library(
name = "mobile_bytecode_header",
srcs = ["torch/csrc/jit/serialization/mobile_bytecode.fbs"],
out_prefix = "torch/csrc/jit/serialization/",
flatc_args=["--gen-mutable", "--scoped-enums",],
)


torch_cuda_headers = glob(["torch/csrc/cuda/*.h"])
cc_library(
name = "torch_headers",
Expand Down Expand Up @@ -1849,7 +1840,6 @@ cc_library(
":aten_headers",
":c10_headers",
":caffe2_headers",
":mobile_bytecode_header",
"@local_config_python//:python_headers",
"@onnx",
],
Expand Down Expand Up @@ -1892,32 +1882,6 @@ cc_library(
alwayslink = True,
)

cc_library(
name = "flatbuffer_loader",
srcs = [
"torch/csrc/jit/mobile/flatbuffer_loader.cpp"
],
hdrs = [
"torch/csrc/jit/mobile/flatbuffer_loader.h"
],
deps = [
":torch"
]
)

cc_library(
name = "flatbuffer_serializer",
srcs = [
"torch/csrc/jit/serialization/flatbuffer_serializer.cpp"
],
hdrs = [
"torch/csrc/jit/serialization/flatbuffer_serializer.h"
],
deps = [
":torch"
]
)

cc_library(
name = "shm",
srcs = glob(["torch/lib/libshm/*.cpp"]),
Expand Down Expand Up @@ -2068,8 +2032,6 @@ cc_test(
],
deps = [
":torch",
":flatbuffer_serializer",
":flatbuffer_loader",
"@com_google_googletest//:gtest_main",
],
)
Expand Down
1 change: 0 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -774,7 +774,6 @@ if(NOT MSVC)
endif()
if(CMAKE_COMPILER_IS_GNUCXX AND NOT (CMAKE_CXX_COMPILER_VERSION VERSION_LESS 7.0.0))
string(APPEND CMAKE_CXX_FLAGS " -Wno-stringop-overflow")
string(APPEND CMAKE_CXX_FLAGS " -Wno-noexcept-type")
endif()
if(CMAKE_COMPILER_IS_GNUCXX)
# Suppress "The ABI for passing parameters with 64-byte alignment has changed in GCC 4.6"
Expand Down
5 changes: 0 additions & 5 deletions WORKSPACE
Original file line number Diff line number Diff line change
Expand Up @@ -181,8 +181,3 @@ new_empty_repository(
name = "cuda",
build_file = "//third_party:cuda.BUILD",
)

local_repository(
name = "com_github_google_flatbuffers",
path = "third_party/flatbuffers",
)
29 changes: 0 additions & 29 deletions caffe2/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -560,7 +560,6 @@ if(NOT INTERN_BUILD_MOBILE OR NOT BUILD_CAFFE2_MOBILE)
${TORCH_SRC_DIR}/csrc/jit/mobile/interpreter.cpp
${TORCH_SRC_DIR}/csrc/jit/mobile/model_compatibility.cpp
${TORCH_SRC_DIR}/csrc/jit/mobile/module.cpp
${TORCH_SRC_DIR}/csrc/jit/mobile/flatbuffer_loader.cpp
${TORCH_SRC_DIR}/csrc/jit/mobile/observer.cpp
${TORCH_SRC_DIR}/csrc/jit/mobile/parse_bytecode.cpp
${TORCH_SRC_DIR}/csrc/jit/mobile/parse_operators.cpp
Expand Down Expand Up @@ -595,10 +594,8 @@ if(NOT INTERN_BUILD_MOBILE OR NOT BUILD_CAFFE2_MOBILE)
${TORCH_SRC_DIR}/csrc/jit/serialization/export.cpp
${TORCH_SRC_DIR}/csrc/jit/serialization/export_bytecode.cpp
${TORCH_SRC_DIR}/csrc/jit/serialization/export_module.cpp
${TORCH_SRC_DIR}/csrc/jit/serialization/flatbuffer_serializer.cpp
${TORCH_SRC_DIR}/csrc/jit/codegen/fuser/cpu/fused_kernel.cpp
${TORCH_SRC_DIR}/csrc/jit/api/module_save.cpp
${TORCH_SRC_DIR}/csrc/jit/testing/module_differ.cpp
${TORCH_SRC_DIR}/csrc/utils/byte_order.cpp
)

Expand Down Expand Up @@ -1644,32 +1641,6 @@ if(APPLE AND USE_PYTORCH_METAL)
endif()
endif()


set(schema ${TORCH_SRC_DIR}/csrc/jit/serialization/mobile_bytecode.fbs)
set(generated_include
"${TORCH_ROOT}/build/torch/csrc/jit/serialization/mobile_bytecode_generated.h")
## cann add--reflect-names
add_custom_command(
OUTPUT ${generated_include}
COMMAND bash ${TORCH_ROOT}/scripts/gen_flatbuffer.sh
DEPENDS ${schema}
WORKING_DIRECTORY "${TORCH_ROOT}"
COMMENT "Generating mobile_bytecode_generated.h"
)
add_library(mobile_bytecode_generated_h INTERFACE)
target_sources(
mobile_bytecode_generated_h
INTERFACE ${generated_include}
)
add_dependencies(mobile_bytecode_generated_h ${generated_include})
target_include_directories(
mobile_bytecode_generated_h
INTERFACE ${TORCH_ROOT}/build)

add_dependencies(torch_cpu mobile_bytecode_generated_h)
target_link_libraries(
torch_cpu PRIVATE mobile_bytecode_generated_h flatbuffers)

# Note [Global dependencies]
# Some libraries (e.g. OpenMPI) like to dlopen plugins after they're initialized,
# and they assume that all of their symbols will be available in the global namespace.
Expand Down
3 changes: 0 additions & 3 deletions cmake/Dependencies.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -1995,6 +1995,3 @@ if(USE_KINETO)
message(STATUS "Configured Kineto")
endif()
endif()

# Include google/FlatBuffers
include(${CMAKE_CURRENT_LIST_DIR}/FlatBuffers.cmake)
10 changes: 0 additions & 10 deletions cmake/FlatBuffers.cmake

This file was deleted.

1 change: 0 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
# Python dependencies required for development
astunparse
expecttest
flatbuffers
future
numpy
psutil
Expand Down
26 changes: 0 additions & 26 deletions scripts/gen_flatbuffer.sh

This file was deleted.

26 changes: 0 additions & 26 deletions test/cpp/jit/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -98,32 +98,6 @@ add_executable(test_jit
${JIT_TEST_SRCS}
)

set(schema ${TORCH_SRC_DIR}/csrc/jit/serialization/mobile_bytecode.fbs)
set(generated_include
"${TORCH_ROOT}/build2/torch/csrc/jit/serialization/mobile_bytecode_generated.h")
## cann add--reflect-names
add_custom_command(
OUTPUT ${generated_include}
COMMAND bash ${TORCH_ROOT}/scripts/gen_flatbuffer.sh
DEPENDS ${schema}
WORKING_DIRECTORY "${TORCH_ROOT}"
COMMENT "Generating mobile_bytecode_generated.h"
)
add_library(mobile_bytecode_generated_h_test INTERFACE)
target_sources(
mobile_bytecode_generated_h_test
INTERFACE ${generated_include}
)
add_dependencies(mobile_bytecode_generated_h_test flatc ${generated_include})
target_include_directories(
mobile_bytecode_generated_h_test
INTERFACE ${TORCH_ROOT}/build)

add_dependencies(test_jit mobile_bytecode_generated_h_test)
target_link_libraries(
test_jit PRIVATE mobile_bytecode_generated_h_test flatbuffers)


# TODO temporary until we can delete the old gtest polyfills.
target_compile_definitions(test_jit PRIVATE USE_GTEST)

Expand Down
58 changes: 1 addition & 57 deletions test/cpp/jit/test_backend.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,13 @@
#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"(
Expand Down Expand Up @@ -149,11 +141,6 @@ 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) {
Expand Down Expand Up @@ -196,12 +183,8 @@ 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()));
AT_ASSERT(res_jit.toTensor().equal(res_mobile.toTensor()));
}

Module getCompositeModuleWithSameNameSubModules() {
Expand Down Expand Up @@ -258,11 +241,6 @@ 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) {
Expand All @@ -278,11 +256,6 @@ 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
Expand Down Expand Up @@ -381,13 +354,6 @@ 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) {
Expand Down Expand Up @@ -448,12 +414,6 @@ 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(
Expand Down Expand Up @@ -552,13 +512,7 @@ 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) {
Expand Down Expand Up @@ -640,11 +594,6 @@ 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(
Expand Down Expand Up @@ -772,11 +721,6 @@ 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
Expand Down
Loading

0 comments on commit 17f3179

Please sign in to comment.