2020-09-24 07:19:05 +00:00
|
|
|
#include <gtest/gtest.h>
|
2019-05-10 20:01:15 +00:00
|
|
|
|
|
|
|
|
#include <ATen/core/qualified_name.h>
|
2020-09-24 07:19:05 +00:00
|
|
|
#include <test/cpp/jit/test_utils.h>
|
2020-02-27 20:18:24 +00:00
|
|
|
#include <torch/csrc/jit/frontend/resolver.h>
|
2020-03-26 18:15:49 +00:00
|
|
|
#include <torch/csrc/jit/serialization/import_source.h>
|
2019-05-10 20:01:15 +00:00
|
|
|
#include <torch/torch.h>
|
2019-05-08 05:44:57 +00:00
|
|
|
|
|
|
|
|
namespace torch {
|
|
|
|
|
namespace jit {
|
2019-08-18 23:46:56 +00:00
|
|
|
|
2021-10-19 06:15:25 +00:00
|
|
|
static constexpr c10::string_view classSrcs1 = R"JIT(
|
2019-05-08 05:44:57 +00:00
|
|
|
class FooNestedTest:
|
|
|
|
|
def __init__(self, y):
|
|
|
|
|
self.y = y
|
2019-05-10 20:01:15 +00:00
|
|
|
|
2019-05-08 05:44:57 +00:00
|
|
|
class FooNestedTest2:
|
|
|
|
|
def __init__(self, y):
|
|
|
|
|
self.y = y
|
|
|
|
|
self.nested = __torch__.FooNestedTest(y)
|
2019-05-10 20:01:15 +00:00
|
|
|
|
2019-05-08 05:44:57 +00:00
|
|
|
class FooTest:
|
|
|
|
|
def __init__(self, x):
|
|
|
|
|
self.class_attr = __torch__.FooNestedTest(x)
|
|
|
|
|
self.class_attr2 = __torch__.FooNestedTest2(x)
|
|
|
|
|
self.x = self.class_attr.y + self.class_attr2.y
|
|
|
|
|
)JIT";
|
|
|
|
|
|
2021-10-19 06:15:25 +00:00
|
|
|
static constexpr c10::string_view classSrcs2 = R"JIT(
|
2019-05-08 05:44:57 +00:00
|
|
|
class FooTest:
|
|
|
|
|
def __init__(self, x):
|
|
|
|
|
self.dx = x
|
|
|
|
|
)JIT";
|
|
|
|
|
|
2019-09-26 18:36:53 +00:00
|
|
|
static void import_libs(
|
|
|
|
|
std::shared_ptr<CompilationUnit> cu,
|
|
|
|
|
const std::string& class_name,
|
|
|
|
|
const std::shared_ptr<Source>& src,
|
2020-07-09 16:07:35 +00:00
|
|
|
const std::vector<at::IValue>& tensor_table) {
|
2019-09-26 18:36:53 +00:00
|
|
|
SourceImporter si(
|
|
|
|
|
cu,
|
|
|
|
|
&tensor_table,
|
2020-03-26 18:15:49 +00:00
|
|
|
[&](const std::string& name) -> std::shared_ptr<Source> { return src; },
|
2019-10-17 05:45:50 +00:00
|
|
|
/*version=*/2);
|
2020-04-25 00:40:48 +00:00
|
|
|
si.loadType(QualifiedName(class_name));
|
2019-09-26 18:36:53 +00:00
|
|
|
}
|
|
|
|
|
|
2020-09-24 07:19:05 +00:00
|
|
|
TEST(ClassImportTest, Basic) {
|
2019-07-16 18:59:53 +00:00
|
|
|
auto cu1 = std::make_shared<CompilationUnit>();
|
|
|
|
|
auto cu2 = std::make_shared<CompilationUnit>();
|
2020-07-09 16:07:35 +00:00
|
|
|
std::vector<at::IValue> constantTable;
|
2019-05-08 05:44:57 +00:00
|
|
|
// Import different versions of FooTest into two namespaces.
|
2019-06-01 06:37:38 +00:00
|
|
|
import_libs(
|
|
|
|
|
cu1,
|
2019-09-26 18:36:53 +00:00
|
|
|
"__torch__.FooTest",
|
2019-06-01 06:37:38 +00:00
|
|
|
std::make_shared<Source>(classSrcs1),
|
2019-09-26 18:36:53 +00:00
|
|
|
constantTable);
|
2019-06-01 06:37:38 +00:00
|
|
|
import_libs(
|
|
|
|
|
cu2,
|
2019-09-26 18:36:53 +00:00
|
|
|
"__torch__.FooTest",
|
2019-06-01 06:37:38 +00:00
|
|
|
std::make_shared<Source>(classSrcs2),
|
2019-09-26 18:36:53 +00:00
|
|
|
constantTable);
|
2019-05-08 05:44:57 +00:00
|
|
|
|
|
|
|
|
// We should get the correct version of `FooTest` for whichever namespace we
|
|
|
|
|
// are referencing
|
|
|
|
|
c10::QualifiedName base("__torch__");
|
2019-07-16 18:59:53 +00:00
|
|
|
auto classType1 = cu1->get_class(c10::QualifiedName(base, "FooTest"));
|
2019-05-08 05:44:57 +00:00
|
|
|
ASSERT_TRUE(classType1->hasAttribute("x"));
|
|
|
|
|
ASSERT_FALSE(classType1->hasAttribute("dx"));
|
|
|
|
|
|
2019-07-16 18:59:53 +00:00
|
|
|
auto classType2 = cu2->get_class(c10::QualifiedName(base, "FooTest"));
|
2019-05-08 05:44:57 +00:00
|
|
|
ASSERT_TRUE(classType2->hasAttribute("dx"));
|
|
|
|
|
ASSERT_FALSE(classType2->hasAttribute("x"));
|
|
|
|
|
|
|
|
|
|
// We should only see FooNestedTest in the first namespace
|
2019-07-16 18:59:53 +00:00
|
|
|
auto c = cu1->get_class(c10::QualifiedName(base, "FooNestedTest"));
|
2019-05-08 05:44:57 +00:00
|
|
|
ASSERT_TRUE(c);
|
|
|
|
|
|
2019-07-16 18:59:53 +00:00
|
|
|
c = cu2->get_class(c10::QualifiedName(base, "FooNestedTest"));
|
2019-05-08 05:44:57 +00:00
|
|
|
ASSERT_FALSE(c);
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-24 07:19:05 +00:00
|
|
|
TEST(ClassImportTest, ScriptObject) {
|
2019-06-25 20:20:43 +00:00
|
|
|
Module m1("m1");
|
|
|
|
|
Module m2("m2");
|
2020-07-09 16:07:35 +00:00
|
|
|
std::vector<at::IValue> constantTable;
|
2019-05-10 20:01:15 +00:00
|
|
|
import_libs(
|
2019-11-18 06:56:49 +00:00
|
|
|
m1._ivalue()->compilation_unit(),
|
2019-09-26 18:36:53 +00:00
|
|
|
"__torch__.FooTest",
|
2019-06-01 06:37:38 +00:00
|
|
|
std::make_shared<Source>(classSrcs1),
|
2019-09-26 18:36:53 +00:00
|
|
|
constantTable);
|
2019-05-10 20:01:15 +00:00
|
|
|
import_libs(
|
2019-11-18 06:56:49 +00:00
|
|
|
m2._ivalue()->compilation_unit(),
|
2019-09-26 18:36:53 +00:00
|
|
|
"__torch__.FooTest",
|
2019-06-01 06:37:38 +00:00
|
|
|
std::make_shared<Source>(classSrcs2),
|
2019-09-26 18:36:53 +00:00
|
|
|
constantTable);
|
2019-05-10 20:01:15 +00:00
|
|
|
|
|
|
|
|
// Incorrect arguments for constructor should throw
|
|
|
|
|
c10::QualifiedName base("__torch__");
|
Make PyTorch code-base clang-tidy compliant (#56892)
Summary:
This is an automatic change generated by the following script:
```
#!/usr/bin/env python3
from subprocess import check_output, check_call
import os
def get_compiled_files_list():
import json
with open("build/compile_commands.json") as f:
data = json.load(f)
files = [os.path.relpath(node['file']) for node in data]
for idx, fname in enumerate(files):
if fname.startswith('build/') and fname.endswith('.DEFAULT.cpp'):
files[idx] = fname[len('build/'):-len('.DEFAULT.cpp')]
return files
def run_clang_tidy(fname):
check_call(["python3", "tools/clang_tidy.py", "-c", "build", "-x", fname,"-s"])
changes = check_output(["git", "ls-files", "-m"])
if len(changes) == 0:
return
check_call(["git", "commit","--all", "-m", f"NOLINT stubs for {fname}"])
def main():
git_files = check_output(["git", "ls-files"]).decode("ascii").split("\n")
compiled_files = get_compiled_files_list()
for idx, fname in enumerate(git_files):
if fname not in compiled_files:
continue
if fname.startswith("caffe2/contrib/aten/"):
continue
print(f"[{idx}/{len(git_files)}] Processing {fname}")
run_clang_tidy(fname)
if __name__ == "__main__":
main()
```
Pull Request resolved: https://github.com/pytorch/pytorch/pull/56892
Reviewed By: H-Huang
Differential Revision: D27991944
Pulled By: malfet
fbshipit-source-id: 5415e1eb2c1b34319a4f03024bfaa087007d7179
2021-04-28 21:09:06 +00:00
|
|
|
// NOLINTNEXTLINE(cppcoreguidelines-avoid-goto,hicpp-avoid-goto)
|
2019-05-10 20:01:15 +00:00
|
|
|
ASSERT_ANY_THROW(m1.create_class(c10::QualifiedName(base, "FooTest"), {1}));
|
|
|
|
|
auto x = torch::ones({2, 3});
|
|
|
|
|
auto obj = m2.create_class(c10::QualifiedName(base, "FooTest"), x).toObject();
|
|
|
|
|
auto dx = obj->getAttr("dx");
|
2019-08-18 23:46:56 +00:00
|
|
|
ASSERT_TRUE(almostEqual(x, dx.toTensor()));
|
2019-05-10 20:01:15 +00:00
|
|
|
|
|
|
|
|
auto new_x = torch::rand({2, 3});
|
|
|
|
|
obj->setAttr("dx", new_x);
|
|
|
|
|
auto new_dx = obj->getAttr("dx");
|
2019-08-18 23:46:56 +00:00
|
|
|
ASSERT_TRUE(almostEqual(new_x, new_dx.toTensor()));
|
2019-05-10 20:01:15 +00:00
|
|
|
}
|
|
|
|
|
|
2019-08-15 17:14:08 +00:00
|
|
|
static const auto methodSrc = R"JIT(
|
|
|
|
|
def __init__(self, x):
|
|
|
|
|
return x
|
|
|
|
|
)JIT";
|
|
|
|
|
|
2020-09-24 07:19:05 +00:00
|
|
|
TEST(ClassImportTest, ClassDerive) {
|
2019-08-15 17:14:08 +00:00
|
|
|
auto cu = std::make_shared<CompilationUnit>();
|
|
|
|
|
auto cls = ClassType::create("foo.bar", cu);
|
|
|
|
|
const auto self = SimpleSelf(cls);
|
|
|
|
|
auto methods = cu->define("foo.bar", methodSrc, nativeResolver(), &self);
|
|
|
|
|
auto method = methods[0];
|
|
|
|
|
cls->addAttribute("attr", TensorType::get());
|
2020-05-06 22:20:31 +00:00
|
|
|
ASSERT_TRUE(cls->findMethod(method->name()));
|
2019-08-15 17:14:08 +00:00
|
|
|
|
|
|
|
|
// Refining a new class should retain attributes and methods
|
|
|
|
|
auto newCls = cls->refine({TensorType::get()});
|
|
|
|
|
ASSERT_TRUE(newCls->hasAttribute("attr"));
|
2020-05-06 22:20:31 +00:00
|
|
|
ASSERT_TRUE(newCls->findMethod(method->name()));
|
2019-08-15 17:14:08 +00:00
|
|
|
|
|
|
|
|
auto newCls2 = cls->withContained({TensorType::get()})->expect<ClassType>();
|
|
|
|
|
ASSERT_TRUE(newCls2->hasAttribute("attr"));
|
2020-05-06 22:20:31 +00:00
|
|
|
ASSERT_TRUE(newCls2->findMethod(method->name()));
|
2019-08-15 17:14:08 +00:00
|
|
|
}
|
|
|
|
|
|
2021-10-19 06:15:25 +00:00
|
|
|
static constexpr c10::string_view torchbindSrc = R"JIT(
|
2020-02-06 18:42:23 +00:00
|
|
|
class FooBar1234(Module):
|
|
|
|
|
__parameters__ = []
|
2020-03-24 07:34:43 +00:00
|
|
|
f : __torch__.torch.classes._TorchScriptTesting._StackString
|
2020-02-06 18:42:23 +00:00
|
|
|
training : bool
|
|
|
|
|
def forward(self: __torch__.FooBar1234) -> str:
|
|
|
|
|
return (self.f).top()
|
|
|
|
|
)JIT";
|
|
|
|
|
|
2020-09-24 07:19:05 +00:00
|
|
|
TEST(ClassImportTest, CustomClass) {
|
2020-02-06 18:42:23 +00:00
|
|
|
auto cu1 = std::make_shared<CompilationUnit>();
|
2020-07-09 16:07:35 +00:00
|
|
|
std::vector<at::IValue> constantTable;
|
2020-02-06 18:42:23 +00:00
|
|
|
// Import different versions of FooTest into two namespaces.
|
|
|
|
|
import_libs(
|
|
|
|
|
cu1,
|
|
|
|
|
"__torch__.FooBar1234",
|
|
|
|
|
std::make_shared<Source>(torchbindSrc),
|
|
|
|
|
constantTable);
|
|
|
|
|
}
|
|
|
|
|
|
2019-05-08 05:44:57 +00:00
|
|
|
} // namespace jit
|
|
|
|
|
} // namespace torch
|