Skip to content

Commit 6c6a690

Browse files
authored
Use ones() function from dpctl.tensor. (#1256)
* Use ones() function from dpctl.tensor. * Added missing event delete for dpnp_ones_c() and dpnp_ones_like_c() functions. * Removed call_fptr_1out() * Fixed tests for ones() and ones_like() functions
1 parent 91b6880 commit 6c6a690

File tree

10 files changed

+141
-107
lines changed

10 files changed

+141
-107
lines changed

dpnp/backend/include/dpnp_iface_fptr.hpp

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -228,9 +228,7 @@ enum class DPNPFuncName : size_t
228228
DPNP_FN_NONZERO, /**< Used in numpy.nonzero() impl */
229229
DPNP_FN_NONZERO_EXT, /**< Used in numpy.nonzero() impl, requires extra parameters */
230230
DPNP_FN_ONES, /**< Used in numpy.ones() impl */
231-
DPNP_FN_ONES_EXT, /**< Used in numpy.ones() impl, requires extra parameters */
232231
DPNP_FN_ONES_LIKE, /**< Used in numpy.ones_like() impl */
233-
DPNP_FN_ONES_LIKE_EXT, /**< Used in numpy.ones_like() impl, requires extra parameters */
234232
DPNP_FN_PARTITION, /**< Used in numpy.partition() impl */
235233
DPNP_FN_PARTITION_EXT, /**< Used in numpy.partition() impl, requires extra parameters */
236234
DPNP_FN_PLACE, /**< Used in numpy.place() impl */

dpnp/backend/kernels/dpnp_krnl_arraycreation.cpp

Lines changed: 0 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -457,12 +457,6 @@ void dpnp_ones_c(void* result, size_t size)
457457
template <typename _DataType>
458458
void (*dpnp_ones_default_c)(void*, size_t) = dpnp_ones_c<_DataType>;
459459

460-
template <typename _DataType>
461-
DPCTLSyclEventRef (*dpnp_ones_ext_c)(DPCTLSyclQueueRef,
462-
void*,
463-
size_t,
464-
const DPCTLEventVectorRef) = dpnp_ones_c<_DataType>;
465-
466460
template <typename _DataType>
467461
DPCTLSyclEventRef dpnp_ones_like_c(DPCTLSyclQueueRef q_ref,
468462
void* result,
@@ -488,12 +482,6 @@ void dpnp_ones_like_c(void* result, size_t size)
488482
template <typename _DataType>
489483
void (*dpnp_ones_like_default_c)(void*, size_t) = dpnp_ones_like_c<_DataType>;
490484

491-
template <typename _DataType>
492-
DPCTLSyclEventRef (*dpnp_ones_like_ext_c)(DPCTLSyclQueueRef,
493-
void*,
494-
size_t,
495-
const DPCTLEventVectorRef) = dpnp_ones_like_c<_DataType>;
496-
497485
template <typename _DataType>
498486
DPCTLSyclEventRef dpnp_ptp_c(DPCTLSyclQueueRef q_ref,
499487
void* result1_out,
@@ -1378,14 +1366,6 @@ void func_map_init_arraycreation(func_map_t& fmap)
13781366
fmap[DPNPFuncName::DPNP_FN_ONES][eft_C128][eft_C128] = {eft_C128,
13791367
(void*)dpnp_ones_default_c<std::complex<double>>};
13801368

1381-
fmap[DPNPFuncName::DPNP_FN_ONES_EXT][eft_INT][eft_INT] = {eft_INT, (void*)dpnp_ones_ext_c<int32_t>};
1382-
fmap[DPNPFuncName::DPNP_FN_ONES_EXT][eft_LNG][eft_LNG] = {eft_LNG, (void*)dpnp_ones_ext_c<int64_t>};
1383-
fmap[DPNPFuncName::DPNP_FN_ONES_EXT][eft_FLT][eft_FLT] = {eft_FLT, (void*)dpnp_ones_ext_c<float>};
1384-
fmap[DPNPFuncName::DPNP_FN_ONES_EXT][eft_DBL][eft_DBL] = {eft_DBL, (void*)dpnp_ones_ext_c<double>};
1385-
fmap[DPNPFuncName::DPNP_FN_ONES_EXT][eft_BLN][eft_BLN] = {eft_BLN, (void*)dpnp_ones_ext_c<bool>};
1386-
fmap[DPNPFuncName::DPNP_FN_ONES_EXT][eft_C128][eft_C128] = {eft_C128,
1387-
(void*)dpnp_ones_ext_c<std::complex<double>>};
1388-
13891369
fmap[DPNPFuncName::DPNP_FN_ONES_LIKE][eft_INT][eft_INT] = {eft_INT, (void*)dpnp_ones_like_default_c<int32_t>};
13901370
fmap[DPNPFuncName::DPNP_FN_ONES_LIKE][eft_LNG][eft_LNG] = {eft_LNG, (void*)dpnp_ones_like_default_c<int64_t>};
13911371
fmap[DPNPFuncName::DPNP_FN_ONES_LIKE][eft_FLT][eft_FLT] = {eft_FLT, (void*)dpnp_ones_like_default_c<float>};
@@ -1394,14 +1374,6 @@ void func_map_init_arraycreation(func_map_t& fmap)
13941374
fmap[DPNPFuncName::DPNP_FN_ONES_LIKE][eft_C128][eft_C128] = {
13951375
eft_C128, (void*)dpnp_ones_like_default_c<std::complex<double>>};
13961376

1397-
fmap[DPNPFuncName::DPNP_FN_ONES_LIKE_EXT][eft_INT][eft_INT] = {eft_INT, (void*)dpnp_ones_like_ext_c<int32_t>};
1398-
fmap[DPNPFuncName::DPNP_FN_ONES_LIKE_EXT][eft_LNG][eft_LNG] = {eft_LNG, (void*)dpnp_ones_like_ext_c<int64_t>};
1399-
fmap[DPNPFuncName::DPNP_FN_ONES_LIKE_EXT][eft_FLT][eft_FLT] = {eft_FLT, (void*)dpnp_ones_like_ext_c<float>};
1400-
fmap[DPNPFuncName::DPNP_FN_ONES_LIKE_EXT][eft_DBL][eft_DBL] = {eft_DBL, (void*)dpnp_ones_like_ext_c<double>};
1401-
fmap[DPNPFuncName::DPNP_FN_ONES_LIKE_EXT][eft_BLN][eft_BLN] = {eft_BLN, (void*)dpnp_ones_like_ext_c<bool>};
1402-
fmap[DPNPFuncName::DPNP_FN_ONES_LIKE_EXT][eft_C128][eft_C128] = {
1403-
eft_C128, (void*)dpnp_ones_like_ext_c<std::complex<double>>};
1404-
14051377
fmap[DPNPFuncName::DPNP_FN_PTP][eft_INT][eft_INT] = {eft_INT, (void*)dpnp_ptp_default_c<int32_t>};
14061378
fmap[DPNPFuncName::DPNP_FN_PTP][eft_LNG][eft_LNG] = {eft_LNG, (void*)dpnp_ptp_default_c<int64_t>};
14071379
fmap[DPNPFuncName::DPNP_FN_PTP][eft_FLT][eft_FLT] = {eft_FLT, (void*)dpnp_ptp_default_c<float>};

dpnp/dpnp_algo/dpnp_algo.pxd

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -204,9 +204,7 @@ cdef extern from "dpnp_iface_fptr.hpp" namespace "DPNPFuncName": # need this na
204204
DPNP_FN_NONZERO
205205
DPNP_FN_NONZERO_EXT
206206
DPNP_FN_ONES
207-
DPNP_FN_ONES_EXT
208207
DPNP_FN_ONES_LIKE
209-
DPNP_FN_ONES_LIKE_EXT
210208
DPNP_FN_PARTITION
211209
DPNP_FN_PARTITION_EXT
212210
DPNP_FN_PLACE

dpnp/dpnp_algo/dpnp_algo.pyx

Lines changed: 0 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -281,34 +281,6 @@ cdef dpnp_DPNPFuncType_to_dtype(size_t type):
281281
utils.checker_throw_type_error("dpnp_DPNPFuncType_to_dtype", type)
282282

283283

284-
cdef utils.dpnp_descriptor call_fptr_1out(DPNPFuncName fptr_name,
285-
shape_type_c result_shape,
286-
result_dtype):
287-
288-
# Convert type to C enum DPNPFuncType
289-
cdef DPNPFuncType dtype_in = dpnp_dtype_to_DPNPFuncType(result_dtype)
290-
291-
# get the FPTR data structure
292-
cdef DPNPFuncData kernel_data = get_dpnp_function_ptr(fptr_name, dtype_in, dtype_in)
293-
294-
# Create result array with type given by FPTR data
295-
cdef utils.dpnp_descriptor result = utils.create_output_descriptor(result_shape, kernel_data.return_type, None)
296-
297-
result_sycl_queue = result.get_array().sycl_queue
298-
299-
cdef c_dpctl.SyclQueue q = <c_dpctl.SyclQueue> result_sycl_queue
300-
cdef c_dpctl.DPCTLSyclQueueRef q_ref = q.get_queue_ref()
301-
302-
cdef fptr_1out_t func = <fptr_1out_t > kernel_data.ptr
303-
# Call FPTR function
304-
cdef c_dpctl.DPCTLSyclEventRef event_ref = func(q_ref, result.get_data(), result.size, NULL)
305-
306-
with nogil: c_dpctl.DPCTLEvent_WaitAndThrow(event_ref)
307-
c_dpctl.DPCTLEvent_Delete(event_ref)
308-
309-
return result
310-
311-
312284
cdef utils.dpnp_descriptor call_fptr_1in_1out(DPNPFuncName fptr_name,
313285
utils.dpnp_descriptor x1,
314286
shape_type_c result_shape,

dpnp/dpnp_algo/dpnp_algo_arraycreation.pyx

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,6 @@ __all__ += [
4343
"dpnp_linspace",
4444
"dpnp_logspace",
4545
"dpnp_meshgrid",
46-
"dpnp_ones",
47-
"dpnp_ones_like",
4846
"dpnp_ptp",
4947
"dpnp_trace",
5048
"dpnp_tri",
@@ -307,14 +305,6 @@ cpdef list dpnp_meshgrid(xi, copy, sparse, indexing):
307305
return result
308306

309307

310-
cpdef utils.dpnp_descriptor dpnp_ones(result_shape, result_dtype):
311-
return call_fptr_1out(DPNP_FN_ONES_EXT, utils._object_to_tuple(result_shape), result_dtype)
312-
313-
314-
cpdef utils.dpnp_descriptor dpnp_ones_like(result_shape, result_dtype):
315-
return call_fptr_1out(DPNP_FN_ONES_LIKE_EXT, utils._object_to_tuple(result_shape), result_dtype)
316-
317-
318308
cpdef dpnp_ptp(utils.dpnp_descriptor arr, axis=None):
319309
cdef shape_type_c shape_arr = arr.shape
320310
cdef shape_type_c output_shape

dpnp/dpnp_container.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
"asarray",
4747
"empty",
4848
"full",
49+
"ones"
4950
"zeros",
5051
]
5152

@@ -144,6 +145,28 @@ def full(shape,
144145
return dpnp_array(array_obj.shape, buffer=array_obj, order=order)
145146

146147

148+
def ones(shape,
149+
*,
150+
dtype=None,
151+
order="C",
152+
device=None,
153+
usm_type="device",
154+
sycl_queue=None):
155+
"""Validate input parameters before passing them into `dpctl.tensor` module"""
156+
dpu.validate_usm_type(usm_type, allow_none=False)
157+
sycl_queue_normalized = dpnp.get_normalized_queue_device(sycl_queue=sycl_queue, device=device)
158+
if order is None:
159+
order = 'C'
160+
161+
"""Creates `dpnp_array` of ones with the given shape, dtype, and order."""
162+
array_obj = dpt.ones(shape,
163+
dtype=dtype,
164+
order=order,
165+
usm_type=usm_type,
166+
sycl_queue=sycl_queue_normalized)
167+
return dpnp_array(array_obj.shape, buffer=array_obj, order=order)
168+
169+
147170
def zeros(shape,
148171
*,
149172
dtype=None,

dpnp/dpnp_iface_arraycreation.py

Lines changed: 53 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1087,15 +1087,24 @@ def __getitem__(self, key):
10871087
ogrid = OGridClass()
10881088

10891089

1090-
def ones(shape, dtype=None, order='C'):
1090+
def ones(shape,
1091+
*,
1092+
dtype=None,
1093+
order="C",
1094+
like=None,
1095+
device=None,
1096+
usm_type="device",
1097+
sycl_queue=None):
10911098
"""
10921099
Return a new array of given shape and type, filled with ones.
10931100
10941101
For full documentation refer to :obj:`numpy.ones`.
10951102
10961103
Limitations
10971104
-----------
1098-
Parameter ``order`` is supported only with default value ``"C"``.
1105+
Parameter ``order`` is supported only with values ``"C"`` and ``"F"``.
1106+
Parameter ``like`` is supported only with default value ``None``.
1107+
Otherwise the function will be executed sequentially on CPU.
10991108
11001109
See Also
11011110
--------
@@ -1117,28 +1126,42 @@ def ones(shape, dtype=None, order='C'):
11171126
11181127
"""
11191128

1120-
if (not use_origin_backend()):
1121-
if order not in ('C', 'c', None):
1122-
pass
1123-
else:
1124-
_dtype = dtype if dtype is not None else dpnp.float64
1125-
1126-
return dpnp_ones(shape, _dtype).get_pyobj()
1129+
if like is not None:
1130+
pass
1131+
elif order not in ('C', 'c', 'F', 'f', None):
1132+
pass
1133+
else:
1134+
return dpnp_container.ones(shape,
1135+
dtype=dtype,
1136+
order=order,
1137+
device=device,
1138+
usm_type=usm_type,
1139+
sycl_queue=sycl_queue)
11271140

1128-
return call_origin(numpy.ones, shape, dtype=dtype, order=order)
1141+
return call_origin(numpy.ones, shape, dtype=dtype, order=order, like=like)
11291142

11301143

1131-
# numpy.ones_like(a, dtype=None, order='K', subok=True, shape=None)
1132-
def ones_like(x1, dtype=None, order='C', subok=False, shape=None):
1144+
def ones_like(x1,
1145+
/,
1146+
*,
1147+
dtype=None,
1148+
order="C",
1149+
subok=False,
1150+
shape=None,
1151+
device=None,
1152+
usm_type=None,
1153+
sycl_queue=None):
11331154
"""
11341155
Return an array of ones with the same shape and type as a given array.
11351156
11361157
For full documentation refer to :obj:`numpy.ones_like`.
11371158
11381159
Limitations
11391160
-----------
1140-
Parameter ``order`` is supported only with default value ``"C"``.
1161+
Parameters ``x1`` is supported only as :class:`dpnp.dpnp_array`.
1162+
Parameter ``order`` is supported with values ``"C"`` or ``"F"``.
11411163
Parameter ``subok`` is supported only with default value ``False``.
1164+
Otherwise the function will be executed sequentially on CPU.
11421165
11431166
See Also
11441167
--------
@@ -1157,18 +1180,22 @@ def ones_like(x1, dtype=None, order='C', subok=False, shape=None):
11571180
[1.0, 1.0, 1.0, 1.0, 1.0, 1.0]
11581181
11591182
"""
1160-
1161-
x1_desc = dpnp.get_dpnp_descriptor(x1, copy_when_nondefault_queue=False)
1162-
if x1_desc:
1163-
if order not in ('C', 'c', None):
1164-
pass
1165-
elif subok is not False:
1166-
pass
1167-
else:
1168-
_shape = shape if shape is not None else x1_desc.shape
1169-
_dtype = dtype if dtype is not None else x1_desc.dtype
1170-
1171-
return dpnp_ones_like(_shape, _dtype).get_pyobj()
1183+
if not isinstance(x1, dpnp.ndarray):
1184+
pass
1185+
elif order not in ('C', 'c', 'F', 'f', None):
1186+
pass
1187+
elif subok is not False:
1188+
pass
1189+
else:
1190+
_shape = x1.shape if shape is None else shape
1191+
_dtype = x1.dtype if dtype is None else dtype
1192+
_usm_type = x1.usm_type if usm_type is None else usm_type
1193+
_sycl_queue = dpnp.get_normalized_queue_device(x1, sycl_queue=sycl_queue, device=device)
1194+
return dpnp_container.ones(_shape,
1195+
dtype=_dtype,
1196+
order=order,
1197+
usm_type=_usm_type,
1198+
sycl_queue=_sycl_queue)
11721199

11731200
return call_origin(numpy.ones_like, x1, dtype, order, subok, shape)
11741201

@@ -1430,6 +1457,7 @@ def zeros(shape,
14301457

14311458

14321459
def zeros_like(x1,
1460+
/,
14331461
*,
14341462
dtype=None,
14351463
order="C",

tests/test_arraycreation.py

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -578,3 +578,47 @@ def test_zeros_like(array, dtype, order):
578578
result = dpnp.zeros_like(ia, dtype=dtype, order=order)
579579

580580
assert_array_equal(expected, result)
581+
582+
583+
@pytest.mark.parametrize("shape",
584+
[(), 0, (0,), (2, 0, 3), (3, 2)],
585+
ids=['()', '0', '(0,)', '(2, 0, 3)', '(3, 2)'])
586+
@pytest.mark.parametrize("dtype",
587+
[None, numpy.complex128, numpy.complex64, numpy.float64, numpy.float32,
588+
numpy.float16, numpy.int64, numpy.int32, numpy.bool],
589+
ids=['None', 'complex128', 'complex64', 'float64', 'float32',
590+
'float16', 'int64', 'int32', 'bool'])
591+
@pytest.mark.parametrize("order",
592+
[None, "C", "F"],
593+
ids=['None', 'C', 'F'])
594+
def test_ones(shape, dtype, order):
595+
func = lambda xp: xp.ones(shape, dtype=dtype, order=order)
596+
597+
if shape != 0 and not 0 in shape and not is_dtype_supported(dtype, no_complex_check=True):
598+
assert_raises(RuntimeError, func, dpnp)
599+
return
600+
601+
assert_array_equal(func(numpy), func(dpnp))
602+
603+
604+
@pytest.mark.parametrize("array",
605+
[[], 0, [1, 2, 3], [[1, 2], [3, 4]]],
606+
ids=['[]', '0', '[1, 2, 3]', '[[1, 2], [3, 4]]'])
607+
@pytest.mark.parametrize("dtype",
608+
[None, numpy.complex128, numpy.complex64, numpy.float64, numpy.float32,
609+
numpy.float16, numpy.int64, numpy.int32, numpy.bool],
610+
ids=['None', 'complex128', 'complex64', 'float64', 'float32',
611+
'float16', 'int64', 'int32', 'bool'])
612+
@pytest.mark.parametrize("order",
613+
[None, "C", "F"],
614+
ids=['None', 'C', 'F'])
615+
def test_ones_like(array, dtype, order):
616+
a = numpy.array(array)
617+
ia = dpnp.array(array)
618+
func = lambda xp, x: xp.ones_like(x, dtype=dtype, order=order)
619+
620+
if ia.size and not is_dtype_supported(dtype, no_complex_check=True):
621+
assert_raises(RuntimeError, func, dpnp, ia)
622+
return
623+
624+
assert_array_equal(func(numpy, a), func(dpnp, ia))

tests/test_sycl_queue.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,9 @@ def vvsort(val, vec, size, xp):
8181
pytest.param("full",
8282
(2,2),
8383
{'fill_value': 5}),
84+
pytest.param("ones",
85+
(2,2),
86+
{}),
8487
pytest.param("zeros",
8588
(2,2),
8689
{})
@@ -104,6 +107,8 @@ def test_array_creation(func, arg, kwargs, device):
104107
[
105108
pytest.param("full_like",
106109
{'fill_value': 5}),
110+
pytest.param("ones_like",
111+
{}),
107112
pytest.param("zeros_like",
108113
{})
109114
])

0 commit comments

Comments
 (0)