You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
It's impossible to use needle.autograd.Tensor as type hints for functions under needle.init.
For example executing import needle as ndl in jupyter notebook fails with following error: AttributeError: module 'needle' has no attribute 'Tensor' if init.py contains following function:
importneedleasndldefrand(*shape, low=0.0, high=1.0, device=None, dtype="float32", requires_grad=False) ->ndl.Tensor:
""" Generate random numbers uniform between low and high """device=ndl.cpu() ifdeviceisNoneelsedevicearray=device.rand(*shape) * (high-low) +lowreturnndl.Tensor(array, device=device, dtype=dtype, requires_grad=requires_grad)
However, import needle as ndl works fine if there is no type hint for return value in init.py function:
importneedleasndldefrand(*shape, low=0.0, high=1.0, device=None, dtype="float32", requires_grad=False):
""" Generate random numbers uniform between low and high """device=ndl.cpu() ifdeviceisNoneelsedevicearray=device.rand(*shape) * (high-low) +lowreturnndl.Tensor(array, device=device, dtype=dtype, requires_grad=requires_grad)
The reason is that:
__init__.py imports autograd.Tensor before init module:
import of autograd.Tensor leads to a subsequent import of init module because autograd.py contains following line:
fromneedleimportinit
by the way, relative import would be much clear in this case: from . import init. but that's not the issue
when init.py is imported by autograd.py, function signatures inside init.py are analyzed.
type hint def rand(...) -> ndl.Tensor leads to an error AttributeError: module 'needle' has no attribute 'Tensor'
because the process of importing autograd in not finished yet.
on import, only function signatures are analyzed. it allows to use ndl.Tensor inside function body (as is right now). the reason, I guess, is when functions are executed, all imports (including needle.autograd.Tensor) are alredy resolved.
To wrap up, it is impossible to use ndl.Tensor as type hints in init.py because of the current order of imports: __init__.py -> autograd.py -> init.py
Solution
Solution is to:
change the order of imports in __init.py__ to import init earlier than autograd:
Description
It's impossible to use
needle.autograd.Tensor
as type hints for functions underneedle.init
.For example executing
import needle as ndl
in jupyter notebook fails with following error:AttributeError: module 'needle' has no attribute 'Tensor'
ifinit.py
contains following function:However,
import needle as ndl
works fine if there is no type hint for return value ininit.py
function:The reason is that:
__init__.py
importsautograd.Tensor
beforeinit
module:autograd.Tensor
leads to a subsequent import ofinit
module becauseautograd.py
contains following line:from . import init
. but that's not the issueinit.py
is imported byautograd.py
, function signatures insideinit.py
are analyzed.type hint
def rand(...) -> ndl.Tensor
leads to an errorAttributeError: module 'needle' has no attribute 'Tensor'
because the process of importing
autograd
in not finished yet.on import, only function signatures are analyzed. it allows to use
ndl.Tensor
inside function body (as is right now). the reason, I guess, is when functions are executed, all imports (includingneedle.autograd.Tensor
) are alredy resolved.To wrap up, it is impossible to use
ndl.Tensor
as type hints ininit.py
because of the current order of imports:__init__.py -> autograd.py -> init.py
Solution
Solution is to:
__init.py__
to importinit
earlier thanautograd
:init.py
to a relative one:replace
The text was updated successfully, but these errors were encountered: