with self.assertRaisesRegex(RuntimeError, "Did you forget to add it to __constants"):
a = M(nn.ModuleList([nn.ReLU()]))
+ def test_attr_module_constants_error(self):
+ class M2(torch.jit.ScriptModule):
+ def __init__(self, mod_list):
+ super(M2, self).__init__(False)
+ self.mods = mod_list
+
+ @torch.jit.script_method
+ def forward(self, v):
+ return self.mods.forward(x)
+
+ with self.assertRaisesRegex(RuntimeError, "Did you forget to add it to __constants"):
+ M2(nn.Sequential(nn.ReLU()))
+ with self.assertRaisesRegex(RuntimeError, "Did you forget to add it to __constants"):
+ M2(nn.ModuleList([nn.ReLU()]))
+
def test_script_sequential_for(self):
class Sub(torch.jit.ScriptModule):
def __init__(self):
return ss.str();
}
+ void checkForAddToConstantsError(std::stringstream& ss) {
+ auto nn = py::module::import("torch.nn");
+ if (py::isinstance(self, nn.attr("ModuleList")) ||
+ py::isinstance(self, nn.attr("Sequential"))) {
+ ss << ". Did you forget to add it to __constants__? ";
+ }
+ }
+
std::vector<std::shared_ptr<SugaredValue>> asTuple(
const SourceRange& loc,
Method& m,
const std::string type_str = typeString(self);
std::stringstream ss;
ss << kind() << " cannot be used as a tuple";
- auto nn = py::module::import("torch.nn");
- if (py::isinstance(self, nn.attr("ModuleList")) ||
- py::isinstance(self, nn.attr("Sequential"))) {
- ss << ". Did you forget to add it to __constants__? ";
- }
+ checkForAddToConstantsError(ss);
+ throw ErrorReport(loc) << ss.str();
+ }
+
+ std::shared_ptr<SugaredValue> attr(
+ const SourceRange& loc,
+ Method& m,
+ const std::string& field) override {
+ const std::string type_str = typeString(self);
+ std::stringstream ss;
+ ss << "attribute lookup is not defined on " << kind();
+ checkForAddToConstantsError(ss);
throw ErrorReport(loc) << ss.str();
}