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

Preserve values with dtype for NumpyOps/CupyOps.asarray #897

Merged
merged 6 commits into from
Aug 11, 2023
Merged
Show file tree
Hide file tree
Changes from 5 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
2 changes: 2 additions & 0 deletions thinc/backends/cupy_ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,8 @@ def asarray(self, data, dtype=None):
array = tensorflow2xp(data)
elif is_mxnet_gpu_array(data):
array = mxnet2xp(data)
elif dtype is not None:
array = self.xp.array(data, dtype=dtype)
adrianeboyd marked this conversation as resolved.
Show resolved Hide resolved
else:
array = self.xp.array(data)

Expand Down
2 changes: 2 additions & 0 deletions thinc/backends/numpy_ops.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,8 @@ class NumpyOps(Ops):
array = data.numpy()
elif hasattr(data, "get"):
array = data.get()
elif dtype is not None:
array = self.xp.array(data, dtype=dtype)
else:
array = self.xp.array(data)

Expand Down
6 changes: 4 additions & 2 deletions thinc/layers/strings2arrays.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,10 @@ def strings2arrays() -> Model[InT, OutT]:


def forward(model: Model[InT, OutT], Xs: InT, is_train: bool) -> Tuple[OutT, Callable]:
hashes = [[hash_unicode(word) for word in X] for X in Xs]
hash_arrays = [model.ops.asarray2i(h, dtype="uint64") for h in hashes]
hashes = model.ops.asarray2i(
[[hash_unicode(word) for word in X] for X in Xs], dtype="int32"
)
hash_arrays = [model.ops.asarray1i(h, dtype="uint64") for h in hashes]
arrays = [model.ops.reshape2i(array, -1, 1) for array in hash_arrays]

def backprop(dX: OutT) -> InT:
Expand Down
7 changes: 7 additions & 0 deletions thinc/tests/backends/test_ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -1597,3 +1597,10 @@ def test_custom_kernel_compilation():
assert compiled_kernel is not None

assert compile_mmh() is not None


@pytest.mark.parametrize("ops", ALL_OPS)
def test_asarray_from_list_uint64(ops):
# list contains int values both above and below int64.max
uint64_list = [16, 11648197037703959513]
assert uint64_list == list(ops.asarray(uint64_list, dtype="uint64"))