Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Dy2St]Raise TypeError when call to_static to convert a method of a common class #44781

Merged
merged 3 commits into from
Aug 2, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,10 @@ def __init__(self, function, input_spec=None, **kwargs):
self._dygraph_function = getattr(function, '__func__')
self._class_instance = getattr(function, '__self__')

if not hasattr(self._class_instance, '_original_funcs'):
raise TypeError(
"When using 'to_static' to convert method of a class, "
"please ensure the class inherits from nn.Layer")
self._class_instance._original_funcs[
function.__name__] = self._dygraph_function
else:
Expand Down Expand Up @@ -406,6 +410,10 @@ def __call__(self, *args, **kwargs):

def _is_train_mode(self):
if self._class_instance is not None:
if not hasattr(self._class_instance, 'training'):
raise TypeError(
"When using 'to_static' to convert method of a class, "
"please ensure the class inherits from nn.Layer")
return self._class_instance.training
else:
return self._training
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -478,5 +478,21 @@ def test_set_buffers2(self):
paddle.enable_static()


class ClassNoInheritLayer:

def func(self, x):
return x + 1


class TestClassNoInheritLayer(unittest.TestCase):

def test_to_static(self):
paddle.disable_static()
net = ClassNoInheritLayer()
input_spec = [paddle.static.InputSpec(name='x', shape=[1])]
with self.assertRaises(TypeError):
static_func = paddle.jit.to_static(net.func, input_spec=input_spec)


if __name__ == '__main__':
unittest.main()