-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Fix 'NoneType' Error on jupyter notebooks #3337
Conversation
Some objects don't return anything for `getmodule()`, e.g., jupyter notebook. While I am using it on jupyter notebooks, the code throws an Error: ``` --------------------------------------------------------------------------- AttributeError Traceback (most recent call last) <ipython-input-13-720eba80f94f> in <module>() 87 88 @register_trainer ---> 89 class Trainer(BaseTrainer): 90 def __init__(self, model, 91 train_dataloader, val_dataloader, loss_fn, optimizer, device=torch.device('cpu'), trainer_kwargs={'max_epochs': 10}): ~/anaconda3/lib/python3.7/site-packages/nni/retiarii/utils.py in register_trainer(cls) 117 """ 118 frm = inspect.stack()[1] --> 119 module_name = inspect.getmodule(frm[0]).__name__ 120 return _blackbox_cls(cls, module_name, 'full') 121 AttributeError: 'NoneType' object has no attribute '__name__' ```
I found that this problem is related to the framework design. The core of the problem is that we have to generate different codes according to the user's settings and manage their operation. To solve the problem of "how to find the location of custom modules (including custom blackbox_module, Trainer, etc.) in the generated code", I made a lot of changes, but I am not sure whether they can cover many scenarios, so I don’t submit and respect the choices of existing developers. Now, NNI's codes depend on two main ideas, i.e., hard-code definition and dynamic
the generated code would be:
Obviously, it did not meet our expectations. But we can create a new module and folder for the custom layer, so that it can be generated normally. For example, in the
and finish the definition in
In this way, it will be all good. |
In a conclusion, I believe that the existing design makes some sense, but it is not very robust and users need to follow a template project architecture. I will close this PR, if you have other questions, feel free to reopen it. |
@tczhangzhi thanks a lot for reporting this issue and kindly providing your solution:). First, by design we allow blackbox_module class to be put in the "main" python file. thanks for reporting the bug. for your example, you can make the following two changes:
we should handle blackbox_module in the "main" python file specially.
encourage you to update your pr accordingly, or you have better solutions feel free to tell us:) |
Thanks for your fast and detailed reply and producing a warning for users is a wise choice. I totally agree with it and look forward to a better NNI. |
Many thanks to @QuanluZhang, I believe his idea may be the best practice to solve the location problem of the custom module.
|
Some objects don't return anything for
getmodule()
, e.g., jupyter notebook.While I am using it on jupyter notebooks, the code throws an Error:
In this case, we can use
getsourcefile
to get the name of the current cell in the jupyter notebook, for example,<ipython-input-14-f75f6173b0d5>
means the 14th cell and the subsequent programs can run normally.