Expose class constant through attr and setattr in object (#29219)

Summary:
Pull Request resolved: https://github.com/pytorch/pytorch/pull/29219

We added class constant in previous PRs, this PR allows access to
class constant in the object API

Test Plan:
build/bin/test_jit
python test/test_jit.py

Imported from OSS

Differential Revision: D18846851

fbshipit-source-id: 888a6517d5f747d1f8ced283c0c2c30b2f6c72c6
This commit is contained in:
Jerry Zhang
2020-01-04 11:07:37 -08:00
committed by Facebook Github Bot
parent 6f62c311a1
commit ebe69236d1
6 changed files with 98 additions and 16 deletions
+19
View File
@@ -41,5 +41,24 @@ void testModuleCloneInstance() {
ASSERT_EQ(m3.attr(attr_name).toInt(), 3);
}
void testModuleConstant() {
auto cu = std::make_shared<CompilationUnit>();
auto cls = ClassType::create("foo.bar", cu, true);
auto attr_name = "attr";
auto const_name = "const";
cls->addAttribute(attr_name, IntType::get());
cls->addConstant(const_name, IValue(3));
Module m(cu, cls);
auto v = IValue(2);
m.register_attribute(attr_name,
IntType::get(),
v,
false);
ASSERT_TRUE(m.hasattr(attr_name));
ASSERT_TRUE(m.hasattr(const_name));
ASSERT_EQ(m.attr(attr_name).toInt(), 2);
ASSERT_EQ(m.attr(const_name).toInt(), 3);
}
} // namespace jit
} // namespace torch