mirror of
https://github.com/pytorch/pytorch.git
synced 2026-11-20 09:04:25 +08:00
Revert "Add upgrader related logic to flatbuffer"
This reverts commit dfae96171a.
This commit is contained in:
@@ -267,6 +267,23 @@ TEST(FlatbufferTest, Inline) {
|
||||
AT_ASSERT(output.toTensor().item<float>() == 7.0);
|
||||
}
|
||||
|
||||
#if defined ENABLE_FLATBUFFER
|
||||
TEST(FlatbufferTest, GetByteCodeVersion) {
|
||||
Module m("m");
|
||||
m.define(R"(
|
||||
def forward(self, input: Tensor):
|
||||
return input + 1
|
||||
)");
|
||||
std::stringstream ss;
|
||||
m._save_for_mobile(ss, {}, false, /*use_flatbuffer=*/true);
|
||||
auto version = _get_model_bytecode_version(ss);
|
||||
AT_ASSERT(version == caffe2::serialize::kProducedBytecodeVersion);
|
||||
ss.seekg(0, ss.beg);
|
||||
auto version_again = _get_model_bytecode_version(ss);
|
||||
AT_ASSERT(version == version_again);
|
||||
}
|
||||
#endif
|
||||
|
||||
TEST(FlatbufferTest, Tuple) {
|
||||
Module m("m");
|
||||
m.define(R"JIT(
|
||||
@@ -1272,429 +1289,5 @@ TEST(TestSourceFlatbuffer,
|
||||
}
|
||||
#endif
|
||||
|
||||
#if !defined FB_XPLAT_BUILD
|
||||
// The following test run in fbcode only
|
||||
TEST(FlatbufferUpgraderTest, DivTensorV2) {
|
||||
std::string filePath(__FILE__);
|
||||
auto test_model_file = filePath.substr(0, filePath.find_last_of("/\\") + 1);
|
||||
test_model_file.append("upgrader_models/test_versioned_div_tensor_v2.ptl.ff");
|
||||
/*
|
||||
(('__torch__.MyModule.forward',
|
||||
(('instructions',
|
||||
(('STOREN', 1, 3),
|
||||
('DROPR', 1, 0),
|
||||
('LOAD', 2, 0),
|
||||
('LOAD', 3, 0),
|
||||
('OP', 0, 0),
|
||||
('LOAD', 2, 0),
|
||||
('LOAD', 3, 0),
|
||||
('OP', 1, 0),
|
||||
('MOVE', 2, 0),
|
||||
('MOVE', 3, 0),
|
||||
('OP', 2, 0),
|
||||
('TUPLE_CONSTRUCT', 3, 0),
|
||||
('RET', 0, 0))),
|
||||
('operators',
|
||||
(('aten::div', 'Tensor'),
|
||||
('aten::div', 'Tensor'),
|
||||
('aten::div', 'Tensor'))),
|
||||
('constants', ()),
|
||||
('types', ()),
|
||||
('register_size', 3))),)
|
||||
|
||||
*/
|
||||
mobile::Module m_module = load_mobile_module_from_file(test_model_file);
|
||||
auto intrsuction_list =
|
||||
m_module.get_method("forward").function().get_code().instructions_;
|
||||
uint64_t number_of_call_instruction = 0;
|
||||
for (auto& instruction : intrsuction_list) {
|
||||
number_of_call_instruction += (instruction.op == OpCode::CALL);
|
||||
}
|
||||
// 3 operators will use upgrader
|
||||
ASSERT_EQ(number_of_call_instruction, 3);
|
||||
|
||||
std::vector<IValue> inputs = {
|
||||
IValue(6 * torch::ones({1})), IValue(3 * torch::ones({1}))};
|
||||
auto actual_output = m_module.forward(inputs);
|
||||
auto expect_output = 2.0 * torch::ones({1});
|
||||
auto actual_output_list = actual_output.toTuple()->elements();
|
||||
ASSERT_TRUE(actual_output_list[0].toTensor().equal(expect_output));
|
||||
}
|
||||
|
||||
TEST(FlatbufferUpgraderTest, DivTensorOutV2) {
|
||||
std::string filePath(__FILE__);
|
||||
auto test_model_file = filePath.substr(0, filePath.find_last_of("/\\") + 1);
|
||||
test_model_file.append(
|
||||
"upgrader_models/test_versioned_div_tensor_out_v2.ptl.ff");
|
||||
/*
|
||||
(('__torch__.MyModule.forward',
|
||||
(('instructions',
|
||||
(('STOREN', 1, 4),
|
||||
('DROPR', 1, 0),
|
||||
('MOVE', 2, 0),
|
||||
('MOVE', 3, 0),
|
||||
('MOVE', 4, 0),
|
||||
('OP', 0, 0),
|
||||
('RET', 0, 0))),
|
||||
('operators', (('aten::div', 'out'),)),
|
||||
('constants', ()),
|
||||
('types', ()),
|
||||
('register_size', 4))),)
|
||||
*/
|
||||
mobile::Module m_module = load_mobile_module_from_file(test_model_file);
|
||||
|
||||
auto intrsuction_list =
|
||||
m_module.get_method("forward").function().get_code().instructions_;
|
||||
uint64_t number_of_call_instruction = 0;
|
||||
for (auto& instruction : intrsuction_list) {
|
||||
number_of_call_instruction += (instruction.op == OpCode::CALL);
|
||||
}
|
||||
// One operator will use upgrader
|
||||
ASSERT_EQ(number_of_call_instruction, 1);
|
||||
|
||||
std::vector<IValue> inputs{
|
||||
IValue(6 * torch::ones({1})),
|
||||
IValue(3 * torch::ones({1})),
|
||||
IValue(torch::empty({1}))};
|
||||
m_module.forward(inputs);
|
||||
auto expect_output = 2.0 * torch::ones({1});
|
||||
auto actual_output = inputs[2].toTensor();
|
||||
// The out argument will be overwritten with the output
|
||||
ASSERT_TRUE(actual_output.equal(expect_output));
|
||||
}
|
||||
|
||||
TEST(FlatbufferUpgraderTest, DivTensorInplaceV2) {
|
||||
std::string filePath(__FILE__);
|
||||
auto test_model_file = filePath.substr(0, filePath.find_last_of("/\\") + 1);
|
||||
test_model_file.append(
|
||||
"upgrader_models/test_versioned_div_tensor_inplace_v2.ptl.ff");
|
||||
/*
|
||||
(('__torch__.MyModule.forward',
|
||||
(('instructions',
|
||||
(('STOREN', 1, 3),
|
||||
('DROPR', 1, 0),
|
||||
('MOVE', 2, 0),
|
||||
('MOVE', 3, 0),
|
||||
('OP', 0, 0),
|
||||
('RET', 0, 0))),
|
||||
('operators', (('aten::div_', 'Tensor'),)),
|
||||
('constants', ()),
|
||||
('types', ()),
|
||||
('register_size', 3))),)
|
||||
*/
|
||||
mobile::Module m_module = load_mobile_module_from_file(test_model_file);
|
||||
|
||||
auto intrsuction_list =
|
||||
m_module.get_method("forward").function().get_code().instructions_;
|
||||
uint64_t number_of_call_instruction = 0;
|
||||
for (auto& instruction : intrsuction_list) {
|
||||
number_of_call_instruction += (instruction.op == OpCode::CALL);
|
||||
}
|
||||
// One operator will use upgrader
|
||||
ASSERT_EQ(number_of_call_instruction, 1);
|
||||
|
||||
std::vector<IValue> inputs{
|
||||
IValue(6 * torch::ones({1})), IValue(3 * torch::ones({1}))};
|
||||
m_module.forward(inputs);
|
||||
auto expect_output = 2.0 * torch::ones({1});
|
||||
auto actual_output = inputs[0].toTensor();
|
||||
// The out argument will be overwritten with the output
|
||||
ASSERT_TRUE(actual_output.equal(expect_output));
|
||||
}
|
||||
|
||||
TEST(FlatbufferUpgraderTest, DivScalarFloatV2) {
|
||||
std::string filePath(__FILE__);
|
||||
auto test_model_file = filePath.substr(0, filePath.find_last_of("/\\") + 1);
|
||||
test_model_file.append(
|
||||
"upgrader_models/test_versioned_div_scalar_float_v2.ptl.ff");
|
||||
/*
|
||||
(('__torch__.MyModuleFloat.forward',
|
||||
(('instructions',
|
||||
(('STOREN', 1, 3),
|
||||
('DROPR', 1, 0),
|
||||
('MOVE', 2, 0),
|
||||
('MOVE', 3, 0),
|
||||
('OP', 0, 0),
|
||||
('RET', 0, 0))),
|
||||
('operators', (('aten::div', 'Scalar'),)),
|
||||
('constants', ()),
|
||||
('types', ()),
|
||||
('register_size', 3))),)
|
||||
*/
|
||||
|
||||
mobile::Module m_module = load_mobile_module_from_file(test_model_file);
|
||||
|
||||
auto intrsuction_list =
|
||||
m_module.get_method("forward").function().get_code().instructions_;
|
||||
uint64_t number_of_call_instruction = 0;
|
||||
for (auto& instruction : intrsuction_list) {
|
||||
number_of_call_instruction += (instruction.op == OpCode::CALL);
|
||||
}
|
||||
// One operator will use upgrader
|
||||
ASSERT_EQ(number_of_call_instruction, 1);
|
||||
|
||||
std::vector<IValue> inputs{IValue(6 * torch::ones({1})), IValue(3.0)};
|
||||
auto output = m_module.forward(inputs);
|
||||
auto expect_output = 2.0 * torch::ones({1});
|
||||
auto actual_output = output.toTensor();
|
||||
|
||||
// The out argument will be overwritten with the output
|
||||
ASSERT_TRUE(actual_output.equal(expect_output));
|
||||
}
|
||||
|
||||
TEST(FlatbufferUpgraderTest, DivScalarReciprocalFloatV2) {
|
||||
std::string filePath(__FILE__);
|
||||
auto test_model_file = filePath.substr(0, filePath.find_last_of("/\\") + 1);
|
||||
test_model_file.append(
|
||||
"upgrader_models/test_versioned_div_scalar_reciprocal_float_v2.ptl.ff");
|
||||
/*
|
||||
(('__torch__.MyModuleFloat.forward',
|
||||
(('instructions',
|
||||
(('STOREN', 1, 3),
|
||||
('DROPR', 1, 0),
|
||||
('MOVE', 2, 0),
|
||||
('OP', 0, 0),
|
||||
('MOVE', 3, 0),
|
||||
('OP', 1, 0),
|
||||
('RET', 0, 0))),
|
||||
('operators', (('aten::reciprocal', ''), ('aten::mul', 'Scalar'))),
|
||||
('constants', ()),
|
||||
('types', ()),
|
||||
('register_size', 3))),)
|
||||
*/
|
||||
mobile::Module m_module = load_mobile_module_from_file(test_model_file);
|
||||
|
||||
auto intrsuction_list =
|
||||
m_module.get_method("forward").function().get_code().instructions_;
|
||||
uint64_t number_of_call_instruction = 0;
|
||||
for (auto& instruction : intrsuction_list) {
|
||||
number_of_call_instruction += (instruction.op == OpCode::CALL);
|
||||
}
|
||||
// No operator will use upgrader
|
||||
ASSERT_EQ(number_of_call_instruction, 0);
|
||||
|
||||
std::vector<IValue> inputs{IValue(6 * torch::ones({1})), IValue(3.0)};
|
||||
auto output = m_module.forward(inputs);
|
||||
auto expect_output = 0.5 * torch::ones({1});
|
||||
auto actual_output = output.toTensor();
|
||||
// The out argument will be overwritten with the output
|
||||
ASSERT_TRUE(actual_output.equal(expect_output));
|
||||
}
|
||||
|
||||
TEST(FlatbufferUpgraderTest, DivScalarReciprocalIntV2) {
|
||||
std::string filePath(__FILE__);
|
||||
auto test_model_file = filePath.substr(0, filePath.find_last_of("/\\") + 1);
|
||||
test_model_file.append(
|
||||
"upgrader_models/test_versioned_div_scalar_reciprocal_int_v2.ptl.ff");
|
||||
/*
|
||||
(('__torch__.MyModuleInt.forward',
|
||||
(('instructions',
|
||||
(('STOREN', 1, 3),
|
||||
('DROPR', 1, 0),
|
||||
('MOVE', 2, 0),
|
||||
('OP', 0, 0),
|
||||
('MOVE', 3, 0),
|
||||
('OP', 1, 0),
|
||||
('RET', 0, 0))),
|
||||
('operators', (('aten::reciprocal', ''), ('aten::mul', 'Scalar'))),
|
||||
('constants', ()),
|
||||
('types', ()),
|
||||
('register_size', 3))),)
|
||||
*/
|
||||
mobile::Module m_module = load_mobile_module_from_file(test_model_file);
|
||||
|
||||
auto intrsuction_list =
|
||||
m_module.get_method("forward").function().get_code().instructions_;
|
||||
uint64_t number_of_call_instruction = 0;
|
||||
for (auto& instruction : intrsuction_list) {
|
||||
number_of_call_instruction += (instruction.op == OpCode::CALL);
|
||||
}
|
||||
// No operator will use upgrader
|
||||
ASSERT_EQ(number_of_call_instruction, 0);
|
||||
|
||||
std::vector<IValue> inputs{IValue(6 * torch::ones({1})), IValue(3.0)};
|
||||
auto output = m_module.forward(inputs);
|
||||
auto expect_output = 0.5 * torch::ones({1});
|
||||
auto actual_output = output.toTensor();
|
||||
|
||||
// The out argument will be overwritten with the output
|
||||
ASSERT_TRUE(actual_output.equal(expect_output));
|
||||
}
|
||||
|
||||
TEST(FlatbufferUpgraderTest, DivScalarScalarV2) {
|
||||
std::string filePath(__FILE__);
|
||||
auto test_model_file = filePath.substr(0, filePath.find_last_of("/\\") + 1);
|
||||
test_model_file.append(
|
||||
"upgrader_models/test_versioned_div_scalar_scalar_v2.ptl.ff");
|
||||
/*
|
||||
(('__torch__.MyModule.forward',
|
||||
(('instructions',
|
||||
(('STOREN', 1, 5),
|
||||
('DROPR', 1, 0),
|
||||
('LOAD', 2, 0),
|
||||
('LOAD', 3, 0),
|
||||
('OP', 0, 0),
|
||||
('MOVE', 2, 0),
|
||||
('LOAD', 4, 0),
|
||||
('OP', 1, 0),
|
||||
('LOAD', 3, 0),
|
||||
('MOVE', 4, 0),
|
||||
('OP', 2, 0),
|
||||
('MOVE', 3, 0),
|
||||
('MOVE', 5, 0),
|
||||
('OP', 3, 0),
|
||||
('TUPLE_CONSTRUCT', 4, 0),
|
||||
('RET', 0, 0))),
|
||||
('operators',
|
||||
(('aten::div', ''),
|
||||
('aten::div', 'float'),
|
||||
('aten::div', ''),
|
||||
('aten::div', 'int'))),
|
||||
('constants', ()),
|
||||
('types', ()),
|
||||
('register_size', 5))),)
|
||||
*/
|
||||
mobile::Module m_module = load_mobile_module_from_file(test_model_file);
|
||||
auto intrsuction_list =
|
||||
m_module.get_method("forward").function().get_code().instructions_;
|
||||
uint64_t number_of_call_instruction = 0;
|
||||
for (auto& instruction : intrsuction_list) {
|
||||
number_of_call_instruction += (instruction.op == OpCode::CALL);
|
||||
}
|
||||
// No operator will use upgrader
|
||||
ASSERT_EQ(number_of_call_instruction, 0);
|
||||
|
||||
std::vector<IValue> inputs{IValue(20.0), IValue(10), IValue(2.0), IValue(5)};
|
||||
auto output = m_module.forward(inputs);
|
||||
auto output_list = output.toTupleRef().elements();
|
||||
auto expect_output = std::vector<IValue>(
|
||||
{IValue(2.0), IValue(10.0), IValue(5.0), IValue(2.0)});
|
||||
// auto actual_output = output.toTensor();
|
||||
for (size_t i = 0; i < expect_output.size(); i++) {
|
||||
ASSERT_EQ(output_list[i], expect_output[i]);
|
||||
}
|
||||
}
|
||||
|
||||
TEST(FlatbufferUpgraderTest, DivScalarIntV2) {
|
||||
std::string filePath(__FILE__);
|
||||
auto test_model_file = filePath.substr(0, filePath.find_last_of("/\\") + 1);
|
||||
test_model_file.append(
|
||||
"upgrader_models/test_versioned_div_scalar_int_v2.ptl.ff");
|
||||
/*
|
||||
(('__torch__.MyModuleInt.forward',
|
||||
(('instructions',
|
||||
(('STOREN', 1, 3),
|
||||
('DROPR', 1, 0),
|
||||
('MOVE', 2, 0),
|
||||
('MOVE', 3, 0),
|
||||
('OP', 0, 0),
|
||||
('RET', 0, 0))),
|
||||
('operators', (('aten::div', 'Scalar'),)),
|
||||
('constants', ()),
|
||||
('types', ()),
|
||||
('register_size', 3))),)
|
||||
*/
|
||||
mobile::Module m_module = load_mobile_module_from_file(test_model_file);
|
||||
|
||||
auto intrsuction_list =
|
||||
m_module.get_method("forward").function().get_code().instructions_;
|
||||
uint64_t number_of_call_instruction = 0;
|
||||
for (auto& instruction : intrsuction_list) {
|
||||
number_of_call_instruction += (instruction.op == OpCode::CALL);
|
||||
}
|
||||
// One operator will use upgrader
|
||||
ASSERT_EQ(number_of_call_instruction, 1);
|
||||
|
||||
std::vector<IValue> inputs{IValue(6 * torch::ones({1})), IValue(3)};
|
||||
auto output = m_module.forward(inputs);
|
||||
auto expect_output = 2.0 * torch::ones({1});
|
||||
auto actual_output = output.toTensor();
|
||||
|
||||
// The out argument will be overwritten with the output
|
||||
ASSERT_TRUE(actual_output.equal(expect_output));
|
||||
}
|
||||
|
||||
TEST(FlatbufferUpgraderTest, DivScalarInplaceFloatV2) {
|
||||
std::string filePath(__FILE__);
|
||||
auto test_model_file = filePath.substr(0, filePath.find_last_of("/\\") + 1);
|
||||
test_model_file.append(
|
||||
"upgrader_models/test_versioned_div_scalar_inplace_float_v2.ptl.ff");
|
||||
/*
|
||||
(('__torch__.MyModuleFloat.forward',
|
||||
(('instructions',
|
||||
(('STOREN', 1, 3),
|
||||
('DROPR', 1, 0),
|
||||
('MOVE', 2, 0),
|
||||
('MOVE', 3, 0),
|
||||
('OP', 0, 0),
|
||||
('RET', 0, 0))),
|
||||
('operators', (('aten::div_', 'Scalar'),)),
|
||||
('constants', ()),
|
||||
('types', ()),
|
||||
('register_size', 3))),)
|
||||
*/
|
||||
|
||||
mobile::Module m_module = load_mobile_module_from_file(test_model_file);
|
||||
|
||||
auto intrsuction_list =
|
||||
m_module.get_method("forward").function().get_code().instructions_;
|
||||
uint64_t number_of_call_instruction = 0;
|
||||
for (auto& instruction : intrsuction_list) {
|
||||
number_of_call_instruction += (instruction.op == OpCode::CALL);
|
||||
}
|
||||
// One operator will use upgrader
|
||||
ASSERT_EQ(number_of_call_instruction, 1);
|
||||
|
||||
std::vector<IValue> inputs{IValue(6 * torch::ones({1})), IValue(3.0)};
|
||||
auto output = m_module.forward(inputs);
|
||||
auto expect_output = 2.0 * torch::ones({1});
|
||||
auto actual_output = output.toTensor();
|
||||
|
||||
// The out argument will be overwritten with the output
|
||||
ASSERT_TRUE(actual_output.equal(expect_output));
|
||||
}
|
||||
|
||||
TEST(FlatbufferUpgraderTest, DivScalarInplaceIntV2) {
|
||||
std::string filePath(__FILE__);
|
||||
auto test_model_file = filePath.substr(0, filePath.find_last_of("/\\") + 1);
|
||||
test_model_file.append(
|
||||
"upgrader_models/test_versioned_div_scalar_inplace_int_v2.ptl.ff");
|
||||
/*
|
||||
(('__torch__.MyModuleInt.forward',
|
||||
(('instructions',
|
||||
(('STOREN', 1, 3),
|
||||
('DROPR', 1, 0),
|
||||
('MOVE', 2, 0),
|
||||
('MOVE', 3, 0),
|
||||
('OP', 0, 0),
|
||||
('RET', 0, 0))),
|
||||
('operators', (('aten::div_', 'Scalar'),)),
|
||||
('constants', ()),
|
||||
('types', ()),
|
||||
('register_size', 3))),)
|
||||
*/
|
||||
|
||||
mobile::Module m_module = load_mobile_module_from_file(test_model_file);
|
||||
|
||||
auto intrsuction_list =
|
||||
m_module.get_method("forward").function().get_code().instructions_;
|
||||
uint64_t number_of_call_instruction = 0;
|
||||
for (auto& instruction : intrsuction_list) {
|
||||
number_of_call_instruction += (instruction.op == OpCode::CALL);
|
||||
}
|
||||
// One operator will use upgrader
|
||||
ASSERT_EQ(number_of_call_instruction, 1);
|
||||
|
||||
std::vector<IValue> inputs{IValue(6 * torch::ones({1})), IValue(3)};
|
||||
auto output = m_module.forward(inputs);
|
||||
auto expect_output = 2.0 * torch::ones({1});
|
||||
auto actual_output = output.toTensor();
|
||||
|
||||
// The out argument will be overwritten with the output
|
||||
ASSERT_TRUE(actual_output.equal(expect_output));
|
||||
}
|
||||
|
||||
#endif // !defined(FB_XPLAT_BUILD)
|
||||
|
||||
} // namespace jit
|
||||
} // namespace torch
|
||||
|
||||
Reference in New Issue
Block a user