mirror of
https://github.com/pytorch/pytorch.git
synced 2026-11-20 09:04:25 +08:00
[jit] make clone works for interface type (#42121)
Summary: Pull Request resolved: https://github.com/pytorch/pytorch/pull/42121 This PR changes the Module API to allow register a module with module interface type, and therefore allows Module::clone works on the case where there's a module interface type being shared by two submodules. interface type will be shared by the new cloned instance in the same compilation unit bc it only contains a list of functionSchema, which does not involve any attributes compared to classType. fixes https://github.com/pytorch/pytorch/issues/41882 Test Plan: Imported from OSS Reviewed By: suo Differential Revision: D22781205 Pulled By: wanchaol fbshipit-source-id: f97f4b75970f0b434e38b5a1f778eda2c4e5109b
This commit is contained in:
committed by
Facebook GitHub Bot
parent
352e15f1a2
commit
a9e7e787f8
@@ -1,10 +1,47 @@
|
||||
#include <test/cpp/jit/test_base.h>
|
||||
#include <test/cpp/jit/test_utils.h>
|
||||
|
||||
#include <ATen/core/qualified_name.h>
|
||||
#include <torch/csrc/jit/frontend/resolver.h>
|
||||
#include <torch/csrc/jit/serialization/import.h>
|
||||
#include <torch/csrc/jit/serialization/import_source.h>
|
||||
#include <torch/torch.h>
|
||||
|
||||
namespace torch {
|
||||
namespace jit {
|
||||
|
||||
static const auto moduleInterfaceSrc = R"JIT(
|
||||
class OneInterface(ModuleInterface):
|
||||
def one(self, x: Tensor, y: Tensor) -> Tensor:
|
||||
pass
|
||||
)JIT";
|
||||
|
||||
static const std::vector<std::string> subModuleMethodsSrc = {R"JIT(
|
||||
def one(self, x: Tensor, y: Tensor) -> Tensor:
|
||||
return self.attr * x + y + 1
|
||||
|
||||
def forward(self, x: Tensor) -> Tensor:
|
||||
return self.attr + x
|
||||
)JIT"};
|
||||
|
||||
static const auto parentForward = R"JIT(
|
||||
def forward(self, x: Tensor) -> Tensor:
|
||||
return self.subMod1.one(x, x) + self.subMod2.one(x, x)
|
||||
)JIT";
|
||||
|
||||
static void import_libs(
|
||||
std::shared_ptr<CompilationUnit> cu,
|
||||
const std::string& class_name,
|
||||
const std::shared_ptr<Source>& src,
|
||||
const std::vector<at::IValue>& tensor_table) {
|
||||
SourceImporter si(
|
||||
cu,
|
||||
&tensor_table,
|
||||
[&](const std::string& name) -> std::shared_ptr<Source> { return src; },
|
||||
/*version=*/2);
|
||||
si.loadType(QualifiedName(class_name));
|
||||
}
|
||||
|
||||
void testModuleClone() {
|
||||
auto cu = std::make_shared<CompilationUnit>();
|
||||
// creating child module
|
||||
@@ -34,6 +71,50 @@ void testModuleClone() {
|
||||
ASSERT_EQ(Module(p2.attr("c2").toObject()).attr(attr_name).toInt(), 3);
|
||||
}
|
||||
|
||||
void testModuleCloneWithModuleInterface() {
|
||||
auto cu = std::make_shared<CompilationUnit>();
|
||||
|
||||
// define a initial module with two submods share same interface
|
||||
Module parentMod("parentMod", cu);
|
||||
Module subMod1("subMod1", cu);
|
||||
Module subMod2("subMod2", cu);
|
||||
|
||||
std::vector<at::IValue> constantTable;
|
||||
import_libs(
|
||||
cu,
|
||||
"__torch__.OneInterface",
|
||||
std::make_shared<Source>(moduleInterfaceSrc),
|
||||
constantTable);
|
||||
|
||||
auto v1 = IValue(2);
|
||||
subMod1.register_attribute("attr", IntType::get(), v1, false);
|
||||
|
||||
auto v2 = IValue(4);
|
||||
subMod2.register_attribute("attr", IntType::get(), v2, false);
|
||||
|
||||
for (const std::string& method : subModuleMethodsSrc) {
|
||||
subMod1.define(method, nativeResolver());
|
||||
subMod2.define(method, nativeResolver());
|
||||
}
|
||||
|
||||
parentMod.register_attribute(
|
||||
"subMod1",
|
||||
cu->get_interface("__torch__.OneInterface"),
|
||||
subMod1._ivalue());
|
||||
parentMod.register_attribute(
|
||||
"subMod2",
|
||||
cu->get_interface("__torch__.OneInterface"),
|
||||
subMod2._ivalue());
|
||||
|
||||
parentMod.define(parentForward, nativeResolver());
|
||||
|
||||
Module clonedMod = parentMod.clone();
|
||||
|
||||
// clone will copy both type and data, therefore we'll have a
|
||||
// different type
|
||||
ASSERT_NE(clonedMod.type(), parentMod.type());
|
||||
}
|
||||
|
||||
void testModuleCopy() {
|
||||
auto cu = std::make_shared<CompilationUnit>();
|
||||
auto cls = ClassType::create("foo.bar", cu, true);
|
||||
|
||||
Reference in New Issue
Block a user