Skip to content

Commit

Permalink
Rearrange fill_value type check logic and implement in full_like
Browse files Browse the repository at this point in the history
  • Loading branch information
ndgrigorian committed Oct 23, 2024
1 parent 9aa1842 commit 48faeac
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 5 deletions.
14 changes: 10 additions & 4 deletions dpctl/tensor/_ctors.py
Original file line number Diff line number Diff line change
Expand Up @@ -1111,14 +1111,15 @@ def full(
sycl_queue=sycl_queue,
)
return dpt.copy(dpt.broadcast_to(X, shape), order=order)

sycl_queue = normalize_queue_device(sycl_queue=sycl_queue, device=device)
usm_type = usm_type if usm_type is not None else "device"
if not isinstance(fill_value, Number):
elif not isinstance(fill_value, Number):
raise TypeError(
"`full` array cannot be constructed with value of type "
f"{type(fill_value)}"
)

sycl_queue = normalize_queue_device(sycl_queue=sycl_queue, device=device)
usm_type = usm_type if usm_type is not None else "device"

dtype = _get_dtype(dtype, sycl_queue, ref_type=type(fill_value))
res = dpt.usm_ndarray(
shape,
Expand Down Expand Up @@ -1486,6 +1487,11 @@ def full_like(
)
_manager.add_event_pair(hev, copy_ev)
return res
elif not isinstance(fill_value, Number):
raise TypeError(
"`full` array cannot be constructed with value of type "
f"{type(fill_value)}"
)

dtype = _get_dtype(dtype, sycl_queue, ref_type=type(fill_value))
res = _empty_like_orderK(x, dtype, usm_type, sycl_queue)
Expand Down
6 changes: 5 additions & 1 deletion dpctl/tests/test_usm_ndarray_ctor.py
Original file line number Diff line number Diff line change
Expand Up @@ -2623,8 +2623,12 @@ def test_setitem_from_numpy_contig():
assert dpt.all(dpt.flip(Xdpt, axis=-1) == expected)


def test_full_raises_type_error():
def test_full_functions_raise_type_error():
get_queue_or_skip()

with pytest.raises(TypeError):
dpt.full(1, "0")

x = dpt.ones(1, dtype="i4")
with pytest.raises(TypeError):
dpt.full_like(x, "0")

0 comments on commit 48faeac

Please sign in to comment.