-
Notifications
You must be signed in to change notification settings - Fork 4
BUG: make array-detection logic in array(...) more robust #138
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -449,12 +449,28 @@ def __dlpack_device__(self): | |
return self.tensor.__dlpack_device__() | ||
|
||
|
||
def _tolist(obj): | ||
"""Recusrively convert tensors into lists.""" | ||
a1 = [] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Bring here the if obj is isntance list/tuple, otherwise this function assumes that obj is an iterable, whcih is a non-trivial thing to assume in this context. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's guarded with an if isinstance check at the call site? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah. What I'm saying is that the logic of this function assumes that if implicitly. The if instance should be inside this function, not in the caller site.. |
||
for elem in obj: | ||
if isinstance(elem, (list, tuple)): | ||
elem = _tolist(elem) | ||
if isinstance(elem, ndarray): | ||
a1.append(elem.tensor.tolist()) | ||
else: | ||
a1.append(elem) | ||
return a1 | ||
|
||
|
||
# This is the ideally the only place which talks to ndarray directly. | ||
# The rest goes through asarray (preferred) or array. | ||
|
||
|
||
def array(obj, dtype=None, *, copy=True, order="K", subok=False, ndmin=0, like=None): | ||
_util.subok_not_ok(like, subok) | ||
if subok is not False: | ||
raise NotImplementedError(f"'subok' parameter is not supported.") | ||
if like is not None: | ||
raise NotImplementedError(f"'like' parameter is not supported.") | ||
if order != "K": | ||
raise NotImplementedError | ||
|
||
|
@@ -469,13 +485,7 @@ def array(obj, dtype=None, *, copy=True, order="K", subok=False, ndmin=0, like=N | |
|
||
# lists of ndarrays: [1, [2, 3], ndarray(4)] convert to lists of lists | ||
if isinstance(obj, (list, tuple)): | ||
a1 = [] | ||
for elem in obj: | ||
if isinstance(elem, ndarray): | ||
a1.append(elem.tensor.tolist()) | ||
else: | ||
a1.append(elem) | ||
obj = a1 | ||
obj = _tolist(obj) | ||
Comment on lines
487
to
+488
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Also, in what context do we need this? >>>torch.as_tensor([[2,3], np.array([2,3])])
tensor([[2, 3],
[2, 3]]) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In main, it only looks into the first level of nesting:
EDIT: the traceback shows it's not from a branch not main, but the point stands. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. But I think this works in PyTorch? >>> torch.as_tensor([[1,2],[np.int8(1), 2]])
tensor([[1, 2],
[1, 2]]) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. With numpy ndarrays, yes. With torch_np wrapper ndarrays, not in main, yes with this PR. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Then why calling There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nvm, I just read #138 (comment). Can you add a comment explaining this point? |
||
|
||
# is obj an ndarray already? | ||
if isinstance(obj, ndarray): | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit Recursively