Skip to content

Commit f3c149d

Browse files
committed
Added tests for full() and full_like() functions
1 parent 909e7a1 commit f3c149d

File tree

11 files changed

+123
-76
lines changed

11 files changed

+123
-76
lines changed

dpnp/backend/include/dpnp_iface_fptr.hpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,6 @@ enum class DPNPFuncName : size_t
179179
DPNP_FN_FMOD_EXT, /**< Used in numpy.fmod() impl, requires extra parameters */
180180
DPNP_FN_FULL, /**< Used in numpy.full() impl */
181181
DPNP_FN_FULL_LIKE, /**< Used in numpy.full_like() impl */
182-
DPNP_FN_FULL_LIKE_EXT, /**< Used in numpy.full_like() impl, requires extra parameters */
183182
DPNP_FN_HYPOT, /**< Used in numpy.hypot() impl */
184183
DPNP_FN_HYPOT_EXT, /**< Used in numpy.hypot() impl, requires extra parameters */
185184
DPNP_FN_IDENTITY, /**< Used in numpy.identity() impl */

dpnp/backend/kernels/dpnp_krnl_arraycreation.cpp

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -346,13 +346,6 @@ void dpnp_full_like_c(void* array_in, void* result, const size_t size)
346346
template <typename _DataType>
347347
void (*dpnp_full_like_default_c)(void*, void*, const size_t) = dpnp_full_like_c<_DataType>;
348348

349-
template <typename _DataType>
350-
DPCTLSyclEventRef (*dpnp_full_like_ext_c)(DPCTLSyclQueueRef,
351-
void*,
352-
void*,
353-
const size_t,
354-
const DPCTLEventVectorRef) = dpnp_full_like_c<_DataType>;
355-
356349
template <typename _KernelNameSpecialization>
357350
class dpnp_identity_c_kernel;
358351

@@ -1341,14 +1334,6 @@ void func_map_init_arraycreation(func_map_t& fmap)
13411334
fmap[DPNPFuncName::DPNP_FN_FULL_LIKE][eft_C128][eft_C128] = {
13421335
eft_C128, (void*)dpnp_full_like_default_c<std::complex<double>>};
13431336

1344-
fmap[DPNPFuncName::DPNP_FN_FULL_LIKE_EXT][eft_INT][eft_INT] = {eft_INT, (void*)dpnp_full_like_ext_c<int32_t>};
1345-
fmap[DPNPFuncName::DPNP_FN_FULL_LIKE_EXT][eft_LNG][eft_LNG] = {eft_LNG, (void*)dpnp_full_like_ext_c<int64_t>};
1346-
fmap[DPNPFuncName::DPNP_FN_FULL_LIKE_EXT][eft_FLT][eft_FLT] = {eft_FLT, (void*)dpnp_full_like_ext_c<float>};
1347-
fmap[DPNPFuncName::DPNP_FN_FULL_LIKE_EXT][eft_DBL][eft_DBL] = {eft_DBL, (void*)dpnp_full_like_ext_c<double>};
1348-
fmap[DPNPFuncName::DPNP_FN_FULL_LIKE_EXT][eft_BLN][eft_BLN] = {eft_BLN, (void*)dpnp_full_like_ext_c<bool>};
1349-
fmap[DPNPFuncName::DPNP_FN_FULL_LIKE_EXT][eft_C128][eft_C128] = {
1350-
eft_C128, (void*)dpnp_full_like_ext_c<std::complex<double>>};
1351-
13521337
fmap[DPNPFuncName::DPNP_FN_IDENTITY][eft_INT][eft_INT] = {eft_INT, (void*)dpnp_identity_default_c<int32_t>};
13531338
fmap[DPNPFuncName::DPNP_FN_IDENTITY][eft_LNG][eft_LNG] = {eft_LNG, (void*)dpnp_identity_default_c<int64_t>};
13541339
fmap[DPNPFuncName::DPNP_FN_IDENTITY][eft_FLT][eft_FLT] = {eft_FLT, (void*)dpnp_identity_default_c<float>};

dpnp/dpnp_algo/dpnp_algo.pxd

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,6 @@ cdef extern from "dpnp_iface_fptr.hpp" namespace "DPNPFuncName": # need this na
155155
DPNP_FN_FMOD_EXT
156156
DPNP_FN_FULL
157157
DPNP_FN_FULL_LIKE
158-
DPNP_FN_FULL_LIKE_EXT
159158
DPNP_FN_HYPOT
160159
DPNP_FN_HYPOT_EXT
161160
DPNP_FN_IDENTITY

dpnp/dpnp_algo/dpnp_algo_arraycreation.pyx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# cython: language_level=3
22
# -*- coding: utf-8 -*-
33
# *****************************************************************************
4-
# Copyright (c) 2016-2020, Intel Corporation
4+
# Copyright (c) 2016-2022, Intel Corporation
55
# All rights reserved.
66
#
77
# Redistribution and use in source and binary forms, with or without

dpnp/dpnp_container.py

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -124,12 +124,9 @@ def full(shape,
124124
sycl_queue=None):
125125
"""Validate input parameters before passing them into `dpctl.tensor` module"""
126126
dpu.validate_usm_type(usm_type, allow_none=True)
127-
if sycl_queue is None and device is None:
128-
sycl_queue_normalized = None
129-
else:
130-
sycl_queue_normalized = dpnp.get_normalized_queue_device(sycl_queue=sycl_queue, device=device)
127+
sycl_queue_normalized = dpnp.get_normalized_queue_device(fill_value, sycl_queue=sycl_queue, device=device)
131128

132-
if isinstance(fill_value, dpnp.ndarray):
129+
if isinstance(fill_value, dpnp_array):
133130
fill_value = fill_value.get_array()
134131

135132
"""Creates `dpnp_array` where every element is equal to fill_value."""

dpnp/dpnp_iface_arraycreation.py

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -697,12 +697,13 @@ def full(shape,
697697
"""
698698
if like is not None:
699699
pass
700-
elif order not in "cCfF":
700+
elif not isinstance(order, str) or len(order) != 1 or order not in "CcFf":
701701
pass
702702
else:
703703
return dpnp_container.full(shape,
704704
fill_value,
705705
dtype=dtype,
706+
order=order,
706707
device=device,
707708
usm_type=usm_type,
708709
sycl_queue=sycl_queue)
@@ -711,6 +712,7 @@ def full(shape,
711712

712713

713714
def full_like(x1,
715+
/,
714716
fill_value,
715717
*,
716718
dtype=None,
@@ -729,6 +731,7 @@ def full_like(x1,
729731
-----------
730732
Parameter ``order`` is supported only with values ``"C"`` and ``"F"``.
731733
Parameter ``subok`` is supported only with default value ``False``.
734+
Otherwise the function will be executed sequentially on CPU.
732735
733736
See Also
734737
--------
@@ -746,20 +749,24 @@ def full_like(x1,
746749
[1.0, 1.0, 1.0, 1.0, 1.0, 1.0]
747750
748751
"""
749-
if order not in "cCfF":
752+
if not isinstance(x1, dpnp.ndarray):
753+
pass
754+
elif not isinstance(order, str) or len(order) != 1 or order not in "CcFf":
750755
pass
751-
elif subok is True:
756+
elif subok is not False:
752757
pass
753758
else:
754759
_shape = shape if shape is not None else x1.shape
755760
_dtype = dtype if dtype is not None else x1.dtype
756761
_usm_type = usm_type if usm_type is not None else x1.usm_type
757-
if sycl_queue is None and device is None:
758-
_sycl_queue = x1.sycl_queue
759-
else:
760-
_sycl_queue = dpnp.get_normalized_queue_device(sycl_queue=sycl_queue, device=device)
762+
_sycl_queue = dpnp.get_normalized_queue_device(x1, sycl_queue=sycl_queue, device=device)
761763

762-
return full(_shape, fill_value, dtype=_dtype, usm_type=_usm_type, sycl_queue=_sycl_queue)
764+
return dpnp_container.full(_shape,
765+
fill_value,
766+
dtype=_dtype,
767+
order=order,
768+
usm_type=_usm_type,
769+
sycl_queue=_sycl_queue)
763770
return numpy.full_like(x1, fill_value, dtype, order, subok, shape)
764771

765772

tests/skipped_tests.tbl

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -289,9 +289,6 @@ tests/third_party/cupy/creation_tests/test_basic.py::TestBasic::test_empty_int
289289
tests/third_party/cupy/creation_tests/test_basic.py::TestBasic::test_empty_like
290290
tests/third_party/cupy/creation_tests/test_basic.py::TestBasic::test_empty_like_contiguity
291291
tests/third_party/cupy/creation_tests/test_basic.py::TestBasic::test_empty_scalar
292-
tests/third_party/cupy/creation_tests/test_basic.py::TestBasic::test_full
293-
tests/third_party/cupy/creation_tests/test_basic.py::TestBasic::test_full_default_dtype_cpu_input
294-
tests/third_party/cupy/creation_tests/test_basic.py::TestBasic::test_full_like
295292
tests/third_party/cupy/creation_tests/test_basic.py::TestBasic::test_zeros_scalar
296293
tests/third_party/cupy/creation_tests/test_basic.py::TestBasicReshape_param_0_{shape=4}::test_empty_like_reshape
297294
tests/third_party/cupy/creation_tests/test_basic.py::TestBasicReshape_param_0_{shape=4}::test_empty_like_reshape_contiguity
@@ -364,7 +361,6 @@ tests/third_party/cupy/creation_tests/test_basic.py::TestBasic::test_empty_like_
364361
tests/third_party/cupy/creation_tests/test_basic.py::TestBasic::test_empty_like_subok
365362
tests/third_party/cupy/creation_tests/test_basic.py::TestBasic::test_empty_zero_sized_array_strides
366363
tests/third_party/cupy/creation_tests/test_basic.py::TestBasic::test_eye
367-
tests/third_party/cupy/creation_tests/test_basic.py::TestBasic::test_full_default_dtype
368364
tests/third_party/cupy/creation_tests/test_basic.py::TestBasic::test_full_like_subok
369365
tests/third_party/cupy/creation_tests/test_basic.py::TestBasic::test_ones_like_subok
370366
tests/third_party/cupy/creation_tests/test_basic.py::TestBasic::test_zeros_like_subok

tests/test_arraycreation.py

Lines changed: 54 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -350,24 +350,42 @@ def test_vander(array, type, n, increase):
350350
numpy.testing.assert_array_equal(expected, result)
351351

352352

353-
def test_full_compute_follows_data():
354-
q1 = dpnp.dpnp_queue_initialize()
355-
q2 = dpnp.dpnp_queue_initialize()
353+
@pytest.mark.parametrize("shape",
354+
[(), 0, (0,), (2, 0, 3), (3, 2)],
355+
ids=['()', '0', '(0,)', '(2, 0, 3)', '(1, 2)'])
356+
@pytest.mark.parametrize("fill_value",
357+
[1.5, 2, 1.5+0.j],
358+
ids=['1.5', '2', '1.5+0.j'])
359+
@pytest.mark.parametrize("dtype",
360+
[numpy.complex128, numpy.complex64, numpy.float64, numpy.float32, numpy.float16, numpy.int64, numpy.int32],
361+
ids=['complex128', 'complex64', 'float64', 'float32', 'float16', 'int64', 'int32'])
362+
def test_full(shape, fill_value, dtype):
363+
expected = numpy.full(shape, fill_value, dtype=dtype)
364+
result = dpnp.full(shape, fill_value, dtype=dtype)
356365

357-
X = dpnp.full(10, 3, dtype="i4", sycl_queue=q1, usm_type="shared")
358-
Y = dpnp.full(10, X[3])
366+
assert expected.dtype == result.dtype
367+
numpy.testing.assert_array_equal(expected, result)
359368

360-
assert Y.dtype == X.dtype
361-
assert Y.usm_type == X.usm_type
362-
assert Y.sycl_queue.__eq__(X.sycl_queue)
363-
assert numpy.array_equal(dpnp.asnumpy(Y), numpy.full(10, 3, dtype="i4"))
364369

365-
Y = dpnp.full(10, X[3], dtype="f4", sycl_queue=q2, usm_type="host")
370+
@pytest.mark.parametrize("array",
371+
[[], 0, [1, 2, 3], [[1, 2], [3, 4]]],
372+
ids=['[]', '0', '[1, 2, 3]', '[[1, 2], [3, 4]]'])
373+
@pytest.mark.parametrize("fill_value",
374+
[1.5, 2, 1.5+0.j],
375+
ids=['1.5', '2', '1.5+0.j'])
376+
@pytest.mark.parametrize("dtype",
377+
[numpy.complex128, numpy.complex64, numpy.float64, numpy.float32, numpy.float16, numpy.int64, numpy.int32],
378+
ids=['complex128', 'complex64', 'float64', 'float32', 'float16', 'int64', 'int32'])
379+
def test_full_like(array, fill_value, dtype):
380+
a = numpy.array(array)
381+
ia = dpnp.array(array)
382+
383+
expected = numpy.full_like(a, fill_value, dtype=dtype)
384+
result = dpnp.full_like(ia, fill_value, dtype=dtype)
366385

367-
assert Y.dtype == dpnp.dtype("f4")
368-
assert Y.usm_type == "host"
369-
assert Y.sycl_queue.__eq__(dpnp.empty(0, sycl_queue=q2).sycl_queue)
370-
assert numpy.array_equal(dpnp.asnumpy(Y), numpy.full(10, 3, dtype="f4"))
386+
assert expected.shape == result.shape
387+
assert expected.dtype == result.dtype
388+
numpy.testing.assert_array_equal(expected, result)
371389

372390

373391
@pytest.mark.skip(reason="dpnp.ndarray.flags are not implemented")
@@ -378,32 +396,29 @@ def test_full_compute_follows_data():
378396
["F", "C"],
379397
ids=['F', 'C'])
380398
def test_full_order(order1, order2):
381-
Xnp = numpy.array([1, 2, 3], order=order1)
382-
Ynp = numpy.full((3, 3), Xnp, order=order2)
383-
Y = dpnp.full((3, 3), Xnp, order=order2)
399+
array = numpy.array([1, 2, 3], order=order1)
400+
a = numpy.full((3, 3), array, order=order2)
401+
ia = dpnp.full((3, 3), array, order=order2)
384402

385-
assert Y.flags.c_contiguous == Ynp.flags.c_contiguous
386-
assert Y.flags.f_contiguous == Ynp.flags.f_contiguous
387-
assert numpy.array_equal(dpnp.asnumpy(Y), Ynp)
403+
assert ia.flags.c_contiguous == a.flags.c_contiguous
404+
assert ia.flags.f_contiguous == a.flags.f_contiguous
405+
assert numpy.array_equal(dpnp.asnumpy(ia), a)
388406

389407

390408
def test_full_strides():
391-
X = dpnp.full((3, 3), dpnp.arange(3, dtype="i4"))
392-
Xnp = numpy.full((3, 3), numpy.arange(3, dtype="i4"))
393-
assert X.strides == tuple(el // Xnp.itemsize for el in Xnp.strides)
394-
assert numpy.array_equal(dpnp.asnumpy(X), Xnp)
395-
396-
X = dpnp.full((3, 3), dpnp.arange(6, dtype="i4")[::2])
397-
Xnp = numpy.full((3, 3), numpy.arange(6, dtype="i4")[::2])
398-
assert X.strides == tuple(el // Xnp.itemsize for el in Xnp.strides)
399-
assert numpy.array_equal(dpnp.asnumpy(X), Xnp)
400-
401-
402-
def test_full_like_compute_follows_data():
403-
X = dpnp.full(10, 3, dtype="i4", usm_type="shared")
404-
Y = dpnp.full_like(X, 4, shape=(8))
405-
406-
assert Y.dtype == X.dtype
407-
assert Y.usm_type == X.usm_type
408-
assert Y.sycl_queue.__eq__(X.sycl_queue)
409-
assert numpy.array_equal(dpnp.asnumpy(Y), numpy.full(8, 4, dtype="i4"))
409+
a = numpy.full((3, 3), numpy.arange(3, dtype="i4"))
410+
ia = dpnp.full((3, 3), dpnp.arange(3, dtype="i4"))
411+
assert ia.strides == tuple(el // a.itemsize for el in a.strides)
412+
assert numpy.array_equal(dpnp.asnumpy(ia), a)
413+
414+
a = numpy.full((3, 3), numpy.arange(6, dtype="i4")[::2])
415+
ia = dpnp.full((3, 3), dpnp.arange(6, dtype="i4")[::2])
416+
assert ia.strides == tuple(el // a.itemsize for el in a.strides)
417+
assert numpy.array_equal(dpnp.asnumpy(ia), a)
418+
419+
420+
def test_full_invalid_fill_value():
421+
with pytest.raises(ValueError):
422+
dpnp.full(10, [])
423+
with pytest.raises(ValueError):
424+
dpnp.full(10, dpnp.full(0, 0))

tests/test_sycl_queue.py

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,10 @@ def vvsort(val, vec, size, xp):
7777
[
7878
pytest.param("arange",
7979
-25.7,
80-
{'stop': 10**8, 'step': 15})
80+
{'stop': 10**8, 'step': 15}),
81+
pytest.param("full",
82+
(2,2),
83+
{'fill_value': 5})
8184
])
8285
@pytest.mark.parametrize("device",
8386
valid_devices,
@@ -93,6 +96,36 @@ def test_array_creation(func, arg, kwargs, device):
9396
assert dpnp_array.sycl_device == device
9497

9598

99+
@pytest.mark.parametrize(
100+
"func, kwargs",
101+
[
102+
pytest.param("full_like",
103+
{'fill_value': 5})
104+
])
105+
@pytest.mark.parametrize("device_x",
106+
valid_devices,
107+
ids=[device.filter_string for device in valid_devices])
108+
@pytest.mark.parametrize("device_y",
109+
valid_devices,
110+
ids=[device.filter_string for device in valid_devices])
111+
def test_array_creation_like(func, kwargs, device_x, device_y):
112+
x_orig = numpy.ndarray([1, 2, 3])
113+
y_orig = getattr(numpy, func)(x_orig, **kwargs)
114+
115+
x = dpnp.ndarray([1, 2, 3], device=device_x)
116+
117+
y = getattr(dpnp, func)(x, **kwargs)
118+
numpy.testing.assert_array_equal(y_orig, y)
119+
assert y.sycl_device == device_x
120+
121+
dpnp_kwargs = dict(kwargs)
122+
dpnp_kwargs['device'] = device_y
123+
124+
y = getattr(dpnp, func)(x, **dpnp_kwargs)
125+
numpy.testing.assert_array_equal(y_orig, y)
126+
assert y.sycl_device == device_y
127+
128+
96129
@pytest.mark.usefixtures("allow_fall_back_on_numpy")
97130
@pytest.mark.parametrize(
98131
"func,data",

tests/test_usm_type.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,3 +34,19 @@ def test_coerced_usm_types_mul(usm_type_x, usm_type_y):
3434
assert x.usm_type == usm_type_x
3535
assert y.usm_type == usm_type_y
3636
assert z.usm_type == du.get_coerced_usm_type([usm_type_x, usm_type_y])
37+
38+
39+
@pytest.mark.parametrize("usm_type_x", list_of_usm_types, ids=list_of_usm_types)
40+
@pytest.mark.parametrize("usm_type_y", list_of_usm_types, ids=list_of_usm_types)
41+
def test_array_creation(usm_type_x, usm_type_y):
42+
x0 = dp.full(10, 3, usm_type=usm_type_x)
43+
44+
x = dp.full(10, x0[3])
45+
y = dp.full(10, x0[3], usm_type=usm_type_y)
46+
assert x.usm_type == usm_type_x
47+
assert y.usm_type == usm_type_y
48+
49+
x = dp.full_like(x0, 4)
50+
y = dp.full_like(x0, 4, usm_type=usm_type_y)
51+
assert x.usm_type == usm_type_x
52+
assert y.usm_type == usm_type_y

tests/third_party/cupy/core_tests/test_ndarray_conversion.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ class TestNdarrayItem(unittest.TestCase):
1818
@testing.for_all_dtypes()
1919
@testing.numpy_cupy_equal()
2020
def test_item(self, xp, dtype):
21-
a = xp.full(self.shape, 3, dtype)
21+
a = xp.full(self.shape, 3, dtype=dtype)
2222
return a.item()
2323

2424

0 commit comments

Comments
 (0)