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

NumpyOps: Better type-casting in asarray #656

Merged
merged 6 commits into from
May 17, 2022
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 16 additions & 9 deletions thinc/backends/numpy_ops.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -61,20 +61,27 @@ class NumpyOps(Ops):
raise ValueError("BLIS support requires blis: pip install blis")

def asarray(self, data, dtype=None):
dtype = {"dtype": dtype} if dtype is not None else {}

array = None
shadeMe marked this conversation as resolved.
Show resolved Hide resolved
cast_array = True

if isinstance(data, self.xp.ndarray):
if dtype is not None:
return self.xp.asarray(data, dtype=dtype)
else:
return self.xp.asarray(data)
array = self.xp.asarray(data, **dtype)
shadeMe marked this conversation as resolved.
Show resolved Hide resolved
cast_array = False
elif hasattr(data, 'numpy'):
# Handles PyTorch Tensor
return data.numpy()
array = data.numpy()
elif hasattr(data, "get"):
return data.get()
elif dtype is not None:
return self.xp.array(data, dtype=dtype)
array = data.get()
else:
return self.xp.array(data)
array = self.xp.array(data)

if cast_array and dtype != {}:
array = array.astype(dtype["dtype"])

return array


def alloc(self, shape: Shape, *, dtype: Optional[DTypes] = "float32", zeros: bool = True) -> ArrayXd:
if zeros:
Expand Down