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

lazy_instance could not be applied for torch.nn.Module #96

Closed
quancs opened this issue Oct 16, 2021 · 3 comments
Closed

lazy_instance could not be applied for torch.nn.Module #96

quancs opened this issue Oct 16, 2021 · 3 comments
Labels
bug Something isn't working

Comments

@quancs
Copy link

quancs commented Oct 16, 2021

code:

>>> import torch.nn as nn
>>> from jsonargparse import lazy_instance
>>> lazy_instance(nn.Module)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/quancs/miniconda3/lib/python3.8/site-packages/jsonargparse/typehints.py", line 561, in lazy_instance
    return LazyInitClass(class_type, kwargs)
  File "/home/quancs/miniconda3/lib/python3.8/site-packages/jsonargparse/typehints.py", line 520, in __init__
    for name, method in inspect.getmembers(self, predicate=inspect.ismethod):
  File "/home/quancs/miniconda3/lib/python3.8/inspect.py", line 334, in getmembers
    names = dir(object)
  File "/home/quancs/miniconda3/lib/python3.8/site-packages/torch/nn/modules/module.py", line 1557, in __dir__
    parameters = list(self._parameters.keys())
  File "/home/quancs/miniconda3/lib/python3.8/site-packages/torch/nn/modules/module.py", line 947, in __getattr__
    raise AttributeError("'{}' object has no attribute '{}'".format(
AttributeError: 'LazyInitClass' object has no attribute '_parameters'
@quancs
Copy link
Author

quancs commented Oct 16, 2021

change the __dir__ of a subclass of Module to

    def __dir__(self):
        try:
            return super().__dir__()
        except:
            return ['__init__', 'forward']

can ignore the exception

@mauvilsa mauvilsa added the bug Something isn't working label Oct 19, 2021
@mauvilsa
Copy link
Member

Thanks for reporting. Indeed it is not working and after trying a few things I still don't understand what is the underlying issue. Implementing __dir__ is not a solution since you would need to manually give all possible methods that could be called with an instance. Will keep looking at how to fix it.

@mauvilsa
Copy link
Member

mauvilsa commented Nov 10, 2021

I have pushed a fix to master. The lazy_instance idea isn't as simple as I initially thought. There might be classes for which it still does not work. Specific issues can be created if this is the case. For now closing this one. The fix will be included in version 4.0.0 scheduled for next week.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants