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

CupyOps: Simplify asarray #661

Merged
merged 4 commits into from
May 17, 2022
Merged
Changes from all commits
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
17 changes: 4 additions & 13 deletions thinc/backends/cupy_ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,29 +59,20 @@ def gemm(self, x, y, out=None, trans1=False, trans2=False):
return out

def asarray(self, data, dtype=None):
# This is sort of frustrating, but we can't easily otherwise pass
# forward "unset".
dtype = {"dtype": dtype} if dtype is not None else {}

# We'll try to perform a zero-copy conversion if possible.
array = None
cast_array = False
if is_cupy_array(data):
array = self.xp.asarray(data, **dtype)
array = data
elif is_torch_gpu_array(data):
array = torch2xp(data)
cast_array = True
elif is_tensorflow_gpu_array(data):
array = tensorflow2xp(data)
cast_array = True
elif is_mxnet_gpu_array(data):
array = mxnet2xp(data)
cast_array = True
else:
array = self.xp.array(data, **dtype)
array = self.xp.array(data)

if cast_array and dtype != {}:
array = array.astype(dtype["dtype"])
if dtype is not None:
array = array.astype(dtype=dtype, copy=False)

return array

Expand Down