Skip to content

Commit 20defc5

Browse files
authored
Use eye() function from dpctl.tensor. (#1271)
* Use eye() function from dpctl.tensor. * Add missed order in test for eye() function. * Updated copyright year. Added parameter like for eye() function. * Removed input argumet additional kwards for eye() function.
1 parent 09a387a commit 20defc5

File tree

9 files changed

+90
-87
lines changed

9 files changed

+90
-87
lines changed

dpnp/backend/include/dpnp_iface_fptr.hpp

-1
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,6 @@ enum class DPNPFuncName : size_t
155155
DPNP_FN_ERF, /**< Used in scipy.special.erf impl */
156156
DPNP_FN_ERF_EXT, /**< Used in scipy.special.erf impl, requires extra parameters */
157157
DPNP_FN_EYE, /**< Used in numpy.eye() impl */
158-
DPNP_FN_EYE_EXT, /**< Used in numpy.eye() impl, requires extra parameters */
159158
DPNP_FN_EXP, /**< Used in numpy.exp() impl */
160159
DPNP_FN_EXP_EXT, /**< Used in numpy.exp() impl, requires extra parameters */
161160
DPNP_FN_EXP2, /**< Used in numpy.exp2() impl */

dpnp/backend/kernels/dpnp_krnl_arraycreation.cpp

+1-13
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
//*****************************************************************************
2-
// Copyright (c) 2016-2022, Intel Corporation
2+
// Copyright (c) 2016-2023, Intel Corporation
33
// All rights reserved.
44
//
55
// Redistribution and use in source and binary forms, with or without
@@ -292,13 +292,6 @@ void dpnp_eye_c(void* result1, int k, const shape_elem_type* res_shape)
292292
template <typename _DataType>
293293
void (*dpnp_eye_default_c)(void*, int, const shape_elem_type*) = dpnp_eye_c<_DataType>;
294294

295-
template <typename _DataType>
296-
DPCTLSyclEventRef (*dpnp_eye_ext_c)(DPCTLSyclQueueRef,
297-
void*,
298-
int,
299-
const shape_elem_type*,
300-
const DPCTLEventVectorRef) = dpnp_eye_c<_DataType>;
301-
302295
template <typename _DataType>
303296
DPCTLSyclEventRef dpnp_full_c(DPCTLSyclQueueRef q_ref,
304297
void* array_in,
@@ -1319,11 +1312,6 @@ void func_map_init_arraycreation(func_map_t& fmap)
13191312
fmap[DPNPFuncName::DPNP_FN_EYE][eft_FLT][eft_FLT] = {eft_FLT, (void*)dpnp_eye_default_c<float>};
13201313
fmap[DPNPFuncName::DPNP_FN_EYE][eft_DBL][eft_DBL] = {eft_DBL, (void*)dpnp_eye_default_c<double>};
13211314

1322-
fmap[DPNPFuncName::DPNP_FN_EYE_EXT][eft_INT][eft_INT] = {eft_INT, (void*)dpnp_eye_ext_c<int32_t>};
1323-
fmap[DPNPFuncName::DPNP_FN_EYE_EXT][eft_LNG][eft_LNG] = {eft_LNG, (void*)dpnp_eye_ext_c<int64_t>};
1324-
fmap[DPNPFuncName::DPNP_FN_EYE_EXT][eft_FLT][eft_FLT] = {eft_FLT, (void*)dpnp_eye_ext_c<float>};
1325-
fmap[DPNPFuncName::DPNP_FN_EYE_EXT][eft_DBL][eft_DBL] = {eft_DBL, (void*)dpnp_eye_ext_c<double>};
1326-
13271315
fmap[DPNPFuncName::DPNP_FN_FULL][eft_INT][eft_INT] = {eft_INT, (void*)dpnp_full_default_c<int32_t>};
13281316
fmap[DPNPFuncName::DPNP_FN_FULL][eft_LNG][eft_LNG] = {eft_LNG, (void*)dpnp_full_default_c<int64_t>};
13291317
fmap[DPNPFuncName::DPNP_FN_FULL][eft_FLT][eft_FLT] = {eft_FLT, (void*)dpnp_full_default_c<float>};

dpnp/dpnp_algo/dpnp_algo_arraycreation.pyx

-34
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@ and the rest of the library
3737
__all__ += [
3838
"dpnp_copy",
3939
"dpnp_diag",
40-
"dpnp_eye",
4140
"dpnp_geomspace",
4241
"dpnp_identity",
4342
"dpnp_linspace",
@@ -84,9 +83,6 @@ ctypedef c_dpctl.DPCTLSyclEventRef(*custom_indexing_1out_func_ptr_t)(c_dpctl.DPC
8483
const size_t ,
8584
const int,
8685
const c_dpctl.DPCTLEventVectorRef) except +
87-
ctypedef c_dpctl.DPCTLSyclEventRef(*fptr_dpnp_eye_t)(c_dpctl.DPCTLSyclQueueRef,
88-
void *, int , const shape_elem_type * ,
89-
const c_dpctl.DPCTLEventVectorRef)
9086
ctypedef c_dpctl.DPCTLSyclEventRef(*fptr_dpnp_trace_t)(c_dpctl.DPCTLSyclQueueRef,
9187
const void *,
9288
void * ,
@@ -146,36 +142,6 @@ cpdef utils.dpnp_descriptor dpnp_diag(utils.dpnp_descriptor v, int k):
146142
return result
147143

148144

149-
cpdef utils.dpnp_descriptor dpnp_eye(N, M=None, k=0, dtype=None):
150-
if dtype is None:
151-
dtype = dpnp.float64
152-
153-
if M is None:
154-
M = N
155-
156-
cdef DPNPFuncType param1_type = dpnp_dtype_to_DPNPFuncType(dtype)
157-
158-
cdef DPNPFuncData kernel_data = get_dpnp_function_ptr(DPNP_FN_EYE_EXT, param1_type, param1_type)
159-
160-
cdef utils.dpnp_descriptor result = utils.create_output_descriptor((N, M), kernel_data.return_type, None)
161-
162-
result_sycl_queue = result.get_array().sycl_queue
163-
164-
cdef c_dpctl.SyclQueue q = <c_dpctl.SyclQueue> result_sycl_queue
165-
cdef c_dpctl.DPCTLSyclQueueRef q_ref = q.get_queue_ref()
166-
167-
cdef fptr_dpnp_eye_t func = <fptr_dpnp_eye_t > kernel_data.ptr
168-
169-
cdef shape_type_c result_shape = result.shape
170-
171-
cdef c_dpctl.DPCTLSyclEventRef event_ref = func(q_ref, result.get_data(), k, result_shape.data(), NULL)
172-
173-
with nogil: c_dpctl.DPCTLEvent_WaitAndThrow(event_ref)
174-
c_dpctl.DPCTLEvent_Delete(event_ref)
175-
176-
return result
177-
178-
179145
cpdef utils.dpnp_descriptor dpnp_geomspace(start, stop, num, endpoint, dtype, axis):
180146
cdef shape_type_c obj_shape = utils._object_to_tuple(num)
181147
cdef utils.dpnp_descriptor result = utils_py.create_output_descriptor_py(obj_shape, dtype, None)

dpnp/dpnp_container.py

+29-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# -*- coding: utf-8 -*-
22
# *****************************************************************************
3-
# Copyright (c) 2016-2022, Intel Corporation
3+
# Copyright (c) 2016-2023, Intel Corporation
44
# All rights reserved.
55
#
66
# Redistribution and use in source and binary forms, with or without
@@ -45,6 +45,7 @@
4545
"arange",
4646
"asarray",
4747
"empty",
48+
"eye",
4849
"full",
4950
"ones"
5051
"zeros",
@@ -150,6 +151,33 @@ def full(shape,
150151
return dpnp_array(array_obj.shape, buffer=array_obj, order=order)
151152

152153

154+
def eye(N,
155+
M=None,
156+
/,
157+
*,
158+
k=0,
159+
dtype=None,
160+
order="C",
161+
device=None,
162+
usm_type="device",
163+
sycl_queue=None):
164+
"""Validate input parameters before passing them into `dpctl.tensor` module"""
165+
dpu.validate_usm_type(usm_type, allow_none=False)
166+
sycl_queue_normalized = dpnp.get_normalized_queue_device(sycl_queue=sycl_queue, device=device)
167+
if order is None:
168+
order = 'C'
169+
170+
"""Creates `dpnp_array` with ones on the `k`th diagonal."""
171+
array_obj = dpt.eye(N,
172+
M,
173+
k=k,
174+
dtype=dtype,
175+
order=order,
176+
usm_type=usm_type,
177+
sycl_queue=sycl_queue_normalized)
178+
return dpnp_array(array_obj.shape, buffer=array_obj, order=order)
179+
180+
153181
def ones(shape,
154182
*,
155183
dtype=None,

dpnp/dpnp_iface_arraycreation.py

+30-18
Original file line numberDiff line numberDiff line change
@@ -572,31 +572,43 @@ def empty_like(x1,
572572
return call_origin(numpy.empty_like, x1, dtype, order, subok, shape)
573573

574574

575-
def eye(N, M=None, k=0, dtype=None, order='C', **kwargs):
575+
def eye(N,
576+
M=None,
577+
/,
578+
*,
579+
k=0,
580+
dtype=None,
581+
order="C",
582+
like=None,
583+
device=None,
584+
usm_type="device",
585+
sycl_queue=None):
576586
"""
577587
Return a 2-D array with ones on the diagonal and zeros elsewhere.
578588
For full documentation refer to :obj:`numpy.eye`.
579589
580590
Limitations
581591
-----------
582-
Input array is supported as :obj:`dpnp.ndarray`.
583-
Parameters ``order`` is supported only with default value.
584-
"""
585-
if (not use_origin_backend()):
586-
if not isinstance(N, (int, dpnp.int, dpnp.int32, dpnp.int64)):
587-
pass
588-
elif M is not None and not isinstance(M, (int, dpnp.int, dpnp.int32, dpnp.int64)):
589-
pass
590-
elif not isinstance(k, (int, dpnp.int, dpnp.int32, dpnp.int64)):
591-
pass
592-
elif order != 'C':
593-
pass
594-
elif len(kwargs) != 0:
595-
pass
596-
else:
597-
return dpnp_eye(N, M=M, k=k, dtype=dtype).get_pyobj()
592+
Parameter ``order`` is supported only with values ``"C"`` and ``"F"``.
593+
Parameter ``like`` is supported only with default value ``None``.
594+
Otherwise the function will be executed sequentially on CPU.
598595
599-
return call_origin(numpy.eye, N, M=M, k=k, dtype=dtype, order=order, **kwargs)
596+
"""
597+
if order not in ('C', 'c', 'F', 'f', None):
598+
pass
599+
elif like is not None:
600+
pass
601+
else:
602+
return dpnp_container.eye(N,
603+
M,
604+
k=k,
605+
dtype=dtype,
606+
order=order,
607+
device=device,
608+
usm_type=usm_type,
609+
sycl_queue=sycl_queue)
610+
611+
return call_origin(numpy.eye, N, M, k=k, dtype=dtype, order=order, like=None)
600612

601613

602614
def frombuffer(buffer, **kwargs):

tests/skipped_tests.tbl

-1
Original file line numberDiff line numberDiff line change
@@ -330,7 +330,6 @@ tests/third_party/cupy/creation_tests/test_basic.py::TestBasic::test_empty_like_
330330
tests/third_party/cupy/creation_tests/test_basic.py::TestBasic::test_empty_like_K_strides
331331
tests/third_party/cupy/creation_tests/test_basic.py::TestBasic::test_empty_like_subok
332332
tests/third_party/cupy/creation_tests/test_basic.py::TestBasic::test_empty_zero_sized_array_strides
333-
tests/third_party/cupy/creation_tests/test_basic.py::TestBasic::test_eye
334333
tests/third_party/cupy/creation_tests/test_basic.py::TestBasic::test_full_like_subok
335334
tests/third_party/cupy/creation_tests/test_basic.py::TestBasic::test_ones_like_subok
336335
tests/third_party/cupy/creation_tests/test_basic.py::TestBasic::test_zeros_like_subok

tests/test_arraycreation.py

+20-12
Original file line numberDiff line numberDiff line change
@@ -101,21 +101,29 @@ def test_diag(v, k):
101101

102102

103103
@pytest.mark.parametrize("N",
104-
[0, 1, 2, 3, 4],
105-
ids=['0', '1', '2', '3', '4'])
104+
[0, 1, 2, 3],
105+
ids=['0', '1', '2', '3'])
106106
@pytest.mark.parametrize("M",
107-
[None, 0, 1, 2, 3, 4],
108-
ids=['None', '0', '1', '2', '3', '4'])
107+
[None, 0, 1, 2, 3],
108+
ids=['None', '0', '1', '2', '3'])
109109
@pytest.mark.parametrize("k",
110-
[-5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5],
111-
ids=['-5', '-4', '-3', '-2', '-1', '0', '1', '2', '3', '4', '5'])
110+
[-4, -3, -2, -1, 0, 1, 2, 3, 4],
111+
ids=['-4', '-3', '-2', '-1', '0', '1', '2', '3', '4'])
112112
@pytest.mark.parametrize("dtype",
113-
[numpy.float64, numpy.float32, numpy.int64, numpy.int32],
114-
ids=['float64', 'float32', 'int64', 'int32'])
115-
def test_eye(N, M, k, dtype):
116-
expected = numpy.eye(N, M=M, k=k, dtype=dtype)
117-
result = dpnp.eye(N, M=M, k=k, dtype=dtype)
118-
assert_array_equal(expected, result)
113+
[None, numpy.complex128, numpy.complex64, numpy.float64, numpy.float32,
114+
numpy.float16, numpy.int64, numpy.int32, numpy.bool],
115+
ids=['None', 'complex128', 'complex64', 'float64', 'float32',
116+
'float16', 'int64', 'int32', 'bool'])
117+
@pytest.mark.parametrize("order",
118+
[None, "C", "F"],
119+
ids=['None', 'C', 'F'])
120+
def test_eye(N, M, k, dtype, order):
121+
func = lambda xp: xp.eye(N, M, k=k, dtype=dtype, order=order)
122+
if not is_dtype_supported(dtype, no_complex_check=True):
123+
assert_raises(RuntimeError, func, dpnp)
124+
return
125+
126+
assert_array_equal(func(numpy), func(dpnp))
119127

120128

121129
@pytest.mark.usefixtures("allow_fall_back_on_numpy")

tests/test_sycl_queue.py

+9-6
Original file line numberDiff line numberDiff line change
@@ -76,27 +76,30 @@ def vvsort(val, vec, size, xp):
7676
"func, arg, kwargs",
7777
[
7878
pytest.param("arange",
79-
-25.7,
79+
[-25.7],
8080
{'stop': 10**8, 'step': 15}),
8181
pytest.param("full",
82-
(2,2),
82+
[(2,2)],
8383
{'fill_value': 5}),
84+
pytest.param("eye",
85+
[4, 2],
86+
{}),
8487
pytest.param("ones",
85-
(2,2),
88+
[(2,2)],
8689
{}),
8790
pytest.param("zeros",
88-
(2,2),
91+
[(2,2)],
8992
{})
9093
])
9194
@pytest.mark.parametrize("device",
9295
valid_devices,
9396
ids=[device.filter_string for device in valid_devices])
9497
def test_array_creation(func, arg, kwargs, device):
95-
numpy_array = getattr(numpy, func)(arg, **kwargs)
98+
numpy_array = getattr(numpy, func)(*arg, **kwargs)
9699

97100
dpnp_kwargs = dict(kwargs)
98101
dpnp_kwargs['device'] = device
99-
dpnp_array = getattr(dpnp, func)(arg, **dpnp_kwargs)
102+
dpnp_array = getattr(dpnp, func)(*arg, **dpnp_kwargs)
100103

101104
numpy.testing.assert_array_equal(numpy_array, dpnp_array)
102105
assert dpnp_array.sycl_device == device

tests/third_party/cupy/creation_tests/test_basic.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ def test_empty_zero_sized_array_strides(self, order):
164164
@testing.for_all_dtypes()
165165
@testing.numpy_cupy_array_equal()
166166
def test_eye(self, xp, dtype):
167-
return xp.eye(5, 4, 1, dtype)
167+
return xp.eye(5, 4, k=1, dtype=dtype)
168168

169169
@testing.for_all_dtypes()
170170
@testing.numpy_cupy_array_equal()

0 commit comments

Comments
 (0)