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

[Bug] fix for module_has_exports #50680

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
18 changes: 18 additions & 0 deletions test/jit/test_tracer.py
Expand Up @@ -1877,6 +1877,24 @@ def forward(self, inputs):
tm = torch.jit.trace(m, torch.tensor(1.))
self.assertFalse(hasattr(tm, "submod"))

def test_trace_with_conditional_property(self):
class Net(nn.Module):
def __init__(self, attr=None):
super(Net, self).__init__()
if attr is not None:
self._attr = attr
self.attr_name = '_attr'

@property
def attr(self):
return getattr(self, self.attr_name)

def forward(self, x):
return x

x = torch.ones(1)
torch.jit.trace(Net(), x)


class TestMixTracingScripting(JitTestCase):
def test_trace_script(self):
Expand Down
9 changes: 5 additions & 4 deletions torch/_jit_internal.py
Expand Up @@ -598,10 +598,11 @@ def _copy_to_script_wrapper(fn):

def module_has_exports(mod):
for name in dir(mod):
item = getattr(mod, name)
if callable(item):
if get_torchscript_modifier(item) is FunctionModifiers.EXPORT:
return True
if hasattr(mod, name):
item = getattr(mod, name)
if callable(item):
if get_torchscript_modifier(item) is FunctionModifiers.EXPORT:
return True
return False

def should_drop(fn):
Expand Down