-
-
Notifications
You must be signed in to change notification settings - Fork 50
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
Cannot determine the import path of callable object #481
Comments
There isn't any bug here. The only thing that def my_default_optimizer_callable(params) -> Optimizer: ... or be an instance defined directly in the module, as: my_default_optimizer_callable = MyOptimizerCallable(lr=0.2) Then the parameter defined as Also note that using class instances as defaults is discouraged. See class-type-defaults to understand why. If you use |
Hi @mauvilsa, # test.py
from jsonargparse import ActionConfigFile, ArgumentParser, lazy_instance
...
class MyModel:
def __init__(self, optimizer: OptimizerCallable = lazy_instance(MyOptimizerCallable(lr=0.2))):
self.optimizer = optimizer
...
model = MyModel() The instantiation ( ValueError: Problem with given class_path '__main__.MyModel':
'LazyInstance_MyOptimizerCallable' object has no attribute 'lr' This seems that jsonargparse/jsonargparse/_typehints.py Line 1286 in 2bc81ea
class LazyInitBaseClass:
...
def __call__(self, *args, **kwargs):
if call := self.__dict__.get("__call__", None):
return call(*args, **kwargs) Can we regard this as a bug? |
In the description it says you are using v4.27.6, which is not the latest. If so, please upgrade and try again. It could be already fixed by #473. |
I upgraded it to
$ python test5.py --model __main__.MyModel
SGD (
Parameter Group 0
dampening: 0
differentiable: False
foreach: None
lr: 0.2
maximize: False
momentum: 0
nesterov: False
weight_decay: 0
)
Namespace(model=<__main__.MyModel object at 0x74bfef32b790>, config=None)
Traceback (most recent call last):
File "/home/vinnamki/otx/training_extensions/test5.py", line 29, in <module>
print(MyModel())
^^^^^^^^^
File "/home/vinnamki/otx/training_extensions/test5.py", line 19, in __init__
self.optimizer = optimizer(model.parameters())
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/home/vinnamki/otx/training_extensions/test5.py", line 13, in __call__
return SGD(params, lr=self.lr)
^^^^^^^
AttributeError: 'LazyInstance_MyOptimizerCallable' object has no attribute 'lr' |
Okay, I need to look at this. |
This is a bug. But the fix can't be like suggested in #481 (comment), since this would make all lazy instances callable, which shouldn't be the case. I will push a fix in the next days. |
@vinnamkim I have pushed a fix in #487. Please try it out. |
🐛 Bug report
Same as title. Please see the following reproducible Python script.
To reproduce
Then, execute this Python script such as
python test.py --model __main__.MyModel --print_config
Expected behavior
Expect the following configuration
However, the actual result is
Environment
pip install jsonargparse[all]
):pip install jsonargparse
The text was updated successfully, but these errors were encountered: