Skip to content

Use empty() function from dpctl.tensor module for empty_like() function #1262

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

Merged
merged 5 commits into from
Dec 23, 2022
Merged
Show file tree
Hide file tree
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
9 changes: 7 additions & 2 deletions dpnp/dpnp_container.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,12 +102,17 @@ def asarray(x1,


def empty(shape,
dtype="f4",
*,
dtype=None,
order="C",
device=None,
usm_type="device",
sycl_queue=None):
sycl_queue_normalized = dpnp.get_normalized_queue_device(device=device, sycl_queue=sycl_queue)
"""Validate input parameters before passing them into `dpctl.tensor` module"""
dpu.validate_usm_type(usm_type, allow_none=False)
sycl_queue_normalized = dpnp.get_normalized_queue_device(sycl_queue=sycl_queue, device=device)
if order is None:
order = 'C'

"""Creates `dpnp_array` from uninitialized USM allocation."""
array_obj = dpt.empty(shape,
Expand Down
55 changes: 38 additions & 17 deletions dpnp/dpnp_iface_arraycreation.py
Original file line number Diff line number Diff line change
Expand Up @@ -464,7 +464,8 @@ def diagflat(x1, k=0):


def empty(shape,
dtype="f8",
*,
dtype=None,
order="C",
like=None,
device=None,
Expand All @@ -477,7 +478,9 @@ def empty(shape,

Limitations
-----------
Parameter ``order`` is supported only with values ``"C"`` and ``"F"``.
Parameter ``like`` is supported only with default value ``None``.
Otherwise the function will be executed sequentially on CPU.

See Also
--------
Expand All @@ -497,6 +500,8 @@ def empty(shape,

if like is not None:
pass
elif order not in ('C', 'c', 'F', 'f', None):
pass
else:
return dpnp_container.empty(shape,
dtype=dtype,
Expand All @@ -508,16 +513,27 @@ def empty(shape,
return call_origin(numpy.empty, shape, dtype=dtype, order=order, like=like)


def empty_like(prototype, dtype=None, order='C', subok=False, shape=None):
def empty_like(x1,
/,
*,
dtype=None,
order="C",
subok=False,
shape=None,
device=None,
usm_type=None,
sycl_queue=None):
"""
Return a new array with the same shape and type as a given array.

For full documentation refer to :obj:`numpy.empty_like`.

Limitations
-----------
Parameter ``order`` is supported only with default value ``"C"``.
Parameters ``x1`` is supported only as :class:`dpnp.dpnp_array`.
Parameter ``order`` is supported with values ``"C"`` or ``"F"``.
Parameter ``subok`` is supported only with default value ``False``.
Otherwise the function will be executed sequentially on CPU.

See Also
--------
Expand All @@ -529,26 +545,31 @@ def empty_like(prototype, dtype=None, order='C', subok=False, shape=None):
Examples
--------
>>> import dpnp as np
>>> prototype = np.array([1, 2, 3])
>>> x = np.empty_like(prototype)
>>> a = np.array([1, 2, 3])
>>> x = np.empty_like(a)
>>> [i for i in x]
[0, 0, 0]

"""

if (not use_origin_backend()):
if order not in ('C', 'c', None):
pass
elif subok is not False:
pass
else:
_shape = shape if shape is not None else prototype.shape
_dtype = dtype if dtype is not None else prototype.dtype.type

result = create_output_descriptor_py(_object_to_tuple(_shape), _dtype, None).get_pyobj()
return result
if not isinstance(x1, dpnp.ndarray):
pass
elif order not in ('C', 'c', 'F', 'f', None):
pass
elif subok is not False:
pass
else:
_shape = x1.shape if shape is None else shape
_dtype = x1.dtype if dtype is None else dtype
_usm_type = x1.usm_type if usm_type is None else usm_type
_sycl_queue = dpnp.get_normalized_queue_device(x1, sycl_queue=sycl_queue, device=device)
return dpnp_container.empty(_shape,
dtype=_dtype,
order=order,
usm_type=_usm_type,
sycl_queue=_sycl_queue)

return call_origin(numpy.empty_like, prototype, dtype, order, subok, shape)
return call_origin(numpy.empty_like, x1, dtype, order, subok, shape)


def eye(N, M=None, k=0, dtype=None, order='C', **kwargs):
Expand Down
9 changes: 0 additions & 9 deletions tests/skipped_tests.tbl
Original file line number Diff line number Diff line change
Expand Up @@ -282,20 +282,11 @@ tests/third_party/cupy/core_tests/test_ndarray_reduction.py::TestCubReduction_pa
tests/third_party/cupy/core_tests/test_ndarray_reduction.py::TestCubReduction_param_6_{order='F', shape=(10, 20, 30)}::test_cub_min
tests/third_party/cupy/core_tests/test_ndarray_reduction.py::TestCubReduction_param_7_{order='F', shape=(10, 20, 30, 40)}::test_cub_max
tests/third_party/cupy/core_tests/test_ndarray_reduction.py::TestCubReduction_param_7_{order='F', shape=(10, 20, 30, 40)}::test_cub_min
tests/third_party/cupy/creation_tests/test_basic.py::TestBasic::test_empty
tests/third_party/cupy/creation_tests/test_basic.py::TestBasic::test_empty_int
tests/third_party/cupy/creation_tests/test_basic.py::TestBasic::test_empty_like
tests/third_party/cupy/creation_tests/test_basic.py::TestBasic::test_empty_like_contiguity
tests/third_party/cupy/creation_tests/test_basic.py::TestBasic::test_empty_scalar
tests/third_party/cupy/creation_tests/test_basic.py::TestBasicReshape_param_0_{shape=4}::test_empty_like_reshape
tests/third_party/cupy/creation_tests/test_basic.py::TestBasicReshape_param_0_{shape=4}::test_empty_like_reshape_contiguity
tests/third_party/cupy/creation_tests/test_basic.py::TestBasicReshape_param_1_{shape=(4,)}::test_empty_like_reshape
tests/third_party/cupy/creation_tests/test_basic.py::TestBasicReshape_param_1_{shape=(4,)}::test_empty_like_reshape_contiguity
tests/third_party/cupy/creation_tests/test_basic.py::TestBasicReshape_param_2_{shape=(4, 2)}::test_empty_like_reshape
tests/third_party/cupy/creation_tests/test_basic.py::TestBasicReshape_param_2_{shape=(4, 2)}::test_empty_like_reshape_contiguity
tests/third_party/cupy/creation_tests/test_basic.py::TestBasicReshape_param_3_{shape=(4, 2, 3)}::test_empty_like_reshape
tests/third_party/cupy/creation_tests/test_basic.py::TestBasicReshape_param_3_{shape=(4, 2, 3)}::test_empty_like_reshape_contiguity
tests/third_party/cupy/creation_tests/test_basic.py::TestBasicReshape_param_4_{shape=(5, 4, 2, 3)}::test_empty_like_reshape
tests/third_party/cupy/creation_tests/test_basic.py::TestBasicReshape_param_4_{shape=(5, 4, 2, 3)}::test_empty_like_reshape_contiguity
tests/third_party/cupy/creation_tests/test_basic.py::TestBasicReshape_param_0_{shape=4}::test_empty_like_K_strides_reshape
tests/third_party/cupy/creation_tests/test_basic.py::TestBasicReshape_param_0_{shape=4}::test_empty_like_reshape_contiguity2
Expand Down
9 changes: 0 additions & 9 deletions tests/skipped_tests_gpu.tbl
Original file line number Diff line number Diff line change
Expand Up @@ -458,20 +458,11 @@ tests/third_party/cupy/core_tests/test_ndarray_reduction.py::TestCubReduction_pa
tests/third_party/cupy/core_tests/test_ndarray_reduction.py::TestCubReduction_param_6_{order='F', shape=(10, 20, 30)}::test_cub_min
tests/third_party/cupy/core_tests/test_ndarray_reduction.py::TestCubReduction_param_7_{order='F', shape=(10, 20, 30, 40)}::test_cub_max
tests/third_party/cupy/core_tests/test_ndarray_reduction.py::TestCubReduction_param_7_{order='F', shape=(10, 20, 30, 40)}::test_cub_min
tests/third_party/cupy/creation_tests/test_basic.py::TestBasic::test_empty
tests/third_party/cupy/creation_tests/test_basic.py::TestBasic::test_empty_int
tests/third_party/cupy/creation_tests/test_basic.py::TestBasic::test_empty_like
tests/third_party/cupy/creation_tests/test_basic.py::TestBasic::test_empty_like_contiguity
tests/third_party/cupy/creation_tests/test_basic.py::TestBasic::test_empty_scalar
tests/third_party/cupy/creation_tests/test_basic.py::TestBasicReshape_param_0_{shape=4}::test_empty_like_reshape
tests/third_party/cupy/creation_tests/test_basic.py::TestBasicReshape_param_0_{shape=4}::test_empty_like_reshape_contiguity
tests/third_party/cupy/creation_tests/test_basic.py::TestBasicReshape_param_1_{shape=(4,)}::test_empty_like_reshape
tests/third_party/cupy/creation_tests/test_basic.py::TestBasicReshape_param_1_{shape=(4,)}::test_empty_like_reshape_contiguity
tests/third_party/cupy/creation_tests/test_basic.py::TestBasicReshape_param_2_{shape=(4, 2)}::test_empty_like_reshape
tests/third_party/cupy/creation_tests/test_basic.py::TestBasicReshape_param_2_{shape=(4, 2)}::test_empty_like_reshape_contiguity
tests/third_party/cupy/creation_tests/test_basic.py::TestBasicReshape_param_3_{shape=(4, 2, 3)}::test_empty_like_reshape
tests/third_party/cupy/creation_tests/test_basic.py::TestBasicReshape_param_3_{shape=(4, 2, 3)}::test_empty_like_reshape_contiguity
tests/third_party/cupy/creation_tests/test_basic.py::TestBasicReshape_param_4_{shape=(5, 4, 2, 3)}::test_empty_like_reshape
tests/third_party/cupy/creation_tests/test_basic.py::TestBasicReshape_param_4_{shape=(5, 4, 2, 3)}::test_empty_like_reshape_contiguity
tests/third_party/cupy/creation_tests/test_basic.py::TestBasicReshape_param_0_{shape=4}::test_empty_like_K_strides_reshape
tests/third_party/cupy/creation_tests/test_basic.py::TestBasicReshape_param_0_{shape=4}::test_empty_like_reshape_contiguity2
Expand Down
39 changes: 39 additions & 0 deletions tests/test_arraycreation.py
Original file line number Diff line number Diff line change
Expand Up @@ -580,6 +580,45 @@ def test_zeros_like(array, dtype, order):
assert_array_equal(expected, result)


@pytest.mark.parametrize("shape",
[(), 0, (0,), (2, 0, 3), (3, 2)],
ids=['()', '0', '(0,)', '(2, 0, 3)', '(3, 2)'])
@pytest.mark.parametrize("dtype",
[None, numpy.complex128, numpy.complex64, numpy.float64, numpy.float32,
numpy.float16, numpy.int64, numpy.int32, numpy.bool],
ids=['None', 'complex128', 'complex64', 'float64', 'float32',
'float16', 'int64', 'int32', 'bool'])
@pytest.mark.parametrize("order",
[None, "C", "F"],
ids=['None', 'C', 'F'])
def test_empty(shape, dtype, order):
expected = numpy.empty(shape, dtype=dtype, order=order)
result = dpnp.empty(shape, dtype=dtype, order=order)

assert expected.shape == result.shape


@pytest.mark.parametrize("array",
[[], 0, [1, 2, 3], [[1, 2], [3, 4]]],
ids=['[]', '0', '[1, 2, 3]', '[[1, 2], [3, 4]]'])
@pytest.mark.parametrize("dtype",
[None, numpy.complex128, numpy.complex64, numpy.float64, numpy.float32,
numpy.float16, numpy.int64, numpy.int32, numpy.bool],
ids=['None', 'complex128', 'complex64', 'float64', 'float32',
'float16', 'int64', 'int32', 'bool'])
@pytest.mark.parametrize("order",
[None, "C", "F"],
ids=['None', 'C', 'F'])
def test_empty_like(array, dtype, order):
a = numpy.array(array)
ia = dpnp.array(array)

expected = numpy.empty_like(a, dtype=dtype, order=order)
result = dpnp.empty_like(ia, dtype=dtype, order=order)

assert expected.shape == result.shape


@pytest.mark.parametrize("shape",
[(), 0, (0,), (2, 0, 3), (3, 2)],
ids=['()', '0', '(0,)', '(2, 0, 3)', '(3, 2)'])
Expand Down
22 changes: 22 additions & 0 deletions tests/test_sycl_queue.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,28 @@ def test_array_creation(func, arg, kwargs, device):
assert dpnp_array.sycl_device == device


@pytest.mark.parametrize("device",
valid_devices,
ids=[device.filter_string for device in valid_devices])
def test_empty(device):
dpnp_array = dpnp.empty((2, 2), device=device)
assert dpnp_array.sycl_device == device


@pytest.mark.parametrize("device_x",
valid_devices,
ids=[device.filter_string for device in valid_devices])
@pytest.mark.parametrize("device_y",
valid_devices,
ids=[device.filter_string for device in valid_devices])
def test_empty_like(device_x, device_y):
x = dpnp.ndarray([1, 2, 3], device=device_x)
y = dpnp.empty_like(x)
assert_sycl_queue_equal(y.sycl_queue, x.sycl_queue)
y = dpnp.empty_like(x, device=device_y)
assert_sycl_queue_equal(y.sycl_queue, x.to_device(device_y).sycl_queue)


@pytest.mark.parametrize(
"func, kwargs",
[
Expand Down
2 changes: 2 additions & 0 deletions tests/test_usm_type.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ def test_coerced_usm_types_mul(usm_type_x, usm_type_y):
['x0']),
pytest.param("ones_like",
['x0']),
pytest.param("empty_like",
['x0']),
])
@pytest.mark.parametrize("usm_type_x", list_of_usm_types, ids=list_of_usm_types)
@pytest.mark.parametrize("usm_type_y", list_of_usm_types, ids=list_of_usm_types)
Expand Down
2 changes: 1 addition & 1 deletion tests/third_party/cupy/creation_tests/test_basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ def test_empty_huge_size_fill0(self):
@testing.for_all_dtypes()
@testing.numpy_cupy_array_equal()
def test_empty_scalar(self, xp, dtype, order):
a = xp.empty(None, dtype=dtype, order=order)
a = xp.empty((), dtype=dtype, order=order)
a.fill(0)
return a

Expand Down