Skip to content

Commit 76dafc1

Browse files
committed
Use triu() function from dpctl.tensor
1 parent 05402b0 commit 76dafc1

12 files changed

+71
-99
lines changed

dpnp/backend/include/dpnp_iface_fptr.hpp

-1
Original file line numberDiff line numberDiff line change
@@ -371,7 +371,6 @@ enum class DPNPFuncName : size_t
371371
DPNP_FN_TRI_EXT, /**< Used in numpy.tri() impl, requires extra parameters */
372372
DPNP_FN_TRIL, /**< Used in numpy.tril() impl */
373373
DPNP_FN_TRIU, /**< Used in numpy.triu() impl */
374-
DPNP_FN_TRIU_EXT, /**< Used in numpy.triu() impl, requires extra parameters */
375374
DPNP_FN_TRUNC, /**< Used in numpy.trunc() impl */
376375
DPNP_FN_TRUNC_EXT, /**< Used in numpy.trunc() impl, requires extra parameters */
377376
DPNP_FN_VANDER, /**< Used in numpy.vander() impl */

dpnp/backend/kernels/dpnp_krnl_arraycreation.cpp

-16
Original file line numberDiff line numberDiff line change
@@ -1207,17 +1207,6 @@ void (*dpnp_triu_default_c)(void*,
12071207
const size_t,
12081208
const size_t) = dpnp_triu_c<_DataType>;
12091209

1210-
template <typename _DataType>
1211-
DPCTLSyclEventRef (*dpnp_triu_ext_c)(DPCTLSyclQueueRef,
1212-
void*,
1213-
void*,
1214-
const int,
1215-
shape_elem_type*,
1216-
shape_elem_type*,
1217-
const size_t,
1218-
const size_t,
1219-
const DPCTLEventVectorRef) = dpnp_triu_c<_DataType>;
1220-
12211210
template <typename _DataType>
12221211
DPCTLSyclEventRef dpnp_zeros_c(DPCTLSyclQueueRef q_ref,
12231212
void* result,
@@ -1433,11 +1422,6 @@ void func_map_init_arraycreation(func_map_t& fmap)
14331422
fmap[DPNPFuncName::DPNP_FN_TRIU][eft_FLT][eft_FLT] = {eft_FLT, (void*)dpnp_triu_default_c<float>};
14341423
fmap[DPNPFuncName::DPNP_FN_TRIU][eft_DBL][eft_DBL] = {eft_DBL, (void*)dpnp_triu_default_c<double>};
14351424

1436-
fmap[DPNPFuncName::DPNP_FN_TRIU_EXT][eft_INT][eft_INT] = {eft_INT, (void*)dpnp_triu_ext_c<int32_t>};
1437-
fmap[DPNPFuncName::DPNP_FN_TRIU_EXT][eft_LNG][eft_LNG] = {eft_LNG, (void*)dpnp_triu_ext_c<int64_t>};
1438-
fmap[DPNPFuncName::DPNP_FN_TRIU_EXT][eft_FLT][eft_FLT] = {eft_FLT, (void*)dpnp_triu_ext_c<float>};
1439-
fmap[DPNPFuncName::DPNP_FN_TRIU_EXT][eft_DBL][eft_DBL] = {eft_DBL, (void*)dpnp_triu_ext_c<double>};
1440-
14411425
fmap[DPNPFuncName::DPNP_FN_ZEROS][eft_INT][eft_INT] = {eft_INT, (void*)dpnp_zeros_default_c<int32_t>};
14421426
fmap[DPNPFuncName::DPNP_FN_ZEROS][eft_LNG][eft_LNG] = {eft_LNG, (void*)dpnp_zeros_default_c<int64_t>};
14431427
fmap[DPNPFuncName::DPNP_FN_ZEROS][eft_FLT][eft_FLT] = {eft_FLT, (void*)dpnp_zeros_default_c<float>};

dpnp/dpnp_algo/dpnp_algo_arraycreation.pyx

-45
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,6 @@ __all__ += [
4545
"dpnp_ptp",
4646
"dpnp_trace",
4747
"dpnp_tri",
48-
"dpnp_triu",
4948
"dpnp_vander",
5049
]
5150

@@ -425,50 +424,6 @@ cpdef utils.dpnp_descriptor dpnp_tri(N, M=None, k=0, dtype=dpnp.float):
425424
return result
426425

427426

428-
cpdef utils.dpnp_descriptor dpnp_triu(utils.dpnp_descriptor m, int k):
429-
cdef shape_type_c input_shape = m.shape
430-
cdef shape_type_c result_shape
431-
432-
if m.ndim == 1:
433-
result_shape = (m.shape[0], m.shape[0])
434-
else:
435-
result_shape = m.shape
436-
437-
cdef DPNPFuncType param1_type = dpnp_dtype_to_DPNPFuncType(m.dtype)
438-
cdef DPNPFuncData kernel_data = get_dpnp_function_ptr(DPNP_FN_TRIU_EXT, param1_type, param1_type)
439-
440-
m_obj = m.get_array()
441-
442-
# ceate result array with type given by FPTR data
443-
cdef utils.dpnp_descriptor result = utils.create_output_descriptor(result_shape,
444-
kernel_data.return_type,
445-
None,
446-
device=m_obj.sycl_device,
447-
usm_type=m_obj.usm_type,
448-
sycl_queue=m_obj.sycl_queue)
449-
450-
result_sycl_queue = result.get_array().sycl_queue
451-
452-
cdef c_dpctl.SyclQueue q = <c_dpctl.SyclQueue> result_sycl_queue
453-
cdef c_dpctl.DPCTLSyclQueueRef q_ref = q.get_queue_ref()
454-
455-
cdef custom_1in_1out_func_ptr_t func = <custom_1in_1out_func_ptr_t > kernel_data.ptr
456-
cdef c_dpctl.DPCTLSyclEventRef event_ref = func(q_ref,
457-
m.get_data(),
458-
result.get_data(),
459-
k,
460-
input_shape.data(),
461-
result_shape.data(),
462-
m.ndim,
463-
result.ndim,
464-
NULL) # dep_events_ref
465-
466-
with nogil: c_dpctl.DPCTLEvent_WaitAndThrow(event_ref)
467-
c_dpctl.DPCTLEvent_Delete(event_ref)
468-
469-
return result
470-
471-
472427
cpdef utils.dpnp_descriptor dpnp_vander(utils.dpnp_descriptor x1, int N, int increasing):
473428
cdef DPNPFuncType param1_type = dpnp_dtype_to_DPNPFuncType(x1.dtype)
474429
cdef DPNPFuncData kernel_data = get_dpnp_function_ptr(DPNP_FN_VANDER_EXT, param1_type, DPNP_FT_NONE)

dpnp/dpnp_container.py

+15-3
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939

4040
from dpnp.dpnp_array import dpnp_array
4141
import dpnp
42+
import operator
4243

4344

4445
__all__ = [
@@ -48,7 +49,8 @@
4849
"eye",
4950
"full",
5051
"ones"
51-
"tril"
52+
"tril",
53+
"triu",
5254
"zeros",
5355
]
5456

@@ -202,9 +204,19 @@ def ones(shape,
202204

203205

204206
def tril(x1, /, *, k=0):
205-
""""Creates `dpnp_array` as lower triangle of an input array."""
207+
k = operator.index(k)
208+
order = "F" if (x1.flags.f_contiguous) else "C"
209+
""""Creates `dpnp_array` as lower triangular part of an input array."""
206210
array_obj = dpt.tril(x1.get_array() if isinstance(x1, dpnp_array) else x1, k)
207-
return dpnp_array(array_obj.shape, buffer=array_obj)
211+
return dpnp_array(array_obj.shape, buffer=array_obj, order=order)
212+
213+
214+
def triu(x1, /, *, k=0):
215+
k = operator.index(k)
216+
order = "F" if (x1.flags.f_contiguous) else "C"
217+
""""Creates `dpnp_array` as upper triangular part of an input array."""
218+
array_obj = dpt.triu(x1.get_array() if isinstance(x1, dpnp_array) else x1, k)
219+
return dpnp_array(array_obj.shape, buffer=array_obj, order=order)
208220

209221

210222
def zeros(shape,

dpnp/dpnp_iface.py

+1-12
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,7 @@
6666
"dpnp_queue_is_cpu",
6767
"get_dpnp_descriptor",
6868
"get_include",
69-
"get_normalized_queue_device",
70-
"isarray"
69+
"get_normalized_queue_device"
7170
]
7271

7372
from dpnp import (
@@ -339,13 +338,3 @@ def get_normalized_queue_device(obj=None,
339338
if hasattr(dpt._device, 'normalize_queue_device'):
340339
return dpt._device.normalize_queue_device(sycl_queue=sycl_queue, device=device)
341340
return sycl_queue
342-
343-
344-
def isarray(obj):
345-
"""
346-
Return True if:
347-
`obj` has a supported array type
348-
Return False if:
349-
`obj` has an unsupported array type or other data type
350-
"""
351-
return isinstance(obj, (dpnp_array, dpt.usm_ndarray))

dpnp/dpnp_iface_arraycreation.py

+21-10
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@
4848
from dpnp.dpnp_utils import *
4949

5050
import dpnp.dpnp_container as dpnp_container
51+
import dpctl.tensor as dpt
5152

5253

5354
__all__ = [
@@ -1341,8 +1342,8 @@ def tril(x1, /, *, k=0):
13411342
13421343
Limitations
13431344
-----------
1344-
Parameter ``x1`` is supported only as :class:`dpnp.dpnp_array` with two or more dimensions.
1345-
Parameter ``k`` is supported only as int data type.
1345+
Parameter `x1` is supported as :class:`dpnp.dpnp_array` or :class:`dpctl.tensor.usm_ndarray` with two or more dimensions.
1346+
Parameter `k` is supported only of integer data type.
13461347
Otherwise the function will be executed sequentially on CPU.
13471348
13481349
Examples
@@ -1356,19 +1357,20 @@ def tril(x1, /, *, k=0):
13561357
13571358
"""
13581359

1359-
if not dpnp.isarray(x1):
1360+
if not isinstance(x1, (dpnp.ndarray, dpt.usm_ndarray)):
13601361
pass
13611362
elif x1.ndim < 2:
13621363
pass
13631364
elif not isinstance(k, int):
1364-
pass
1365+
if not (isinstance(k, (dpnp.ndarray, dpt.usm_ndarray)) and numpy.issubdtype(k.dtype, int)):
1366+
pass
13651367
else:
13661368
return dpnp_container.tril(x1, k=k)
13671369

13681370
return call_origin(numpy.tril, x1, k)
13691371

13701372

1371-
def triu(x1, k=0):
1373+
def triu(x1, /, *, k=0):
13721374
"""
13731375
Upper triangle of an array.
13741376
@@ -1377,6 +1379,12 @@ def triu(x1, k=0):
13771379
13781380
For full documentation refer to :obj:`numpy.triu`.
13791381
1382+
Limitations
1383+
-----------
1384+
Parameter `x1` is supported as :class:`dpnp.dpnp_array` or :class:`dpctl.tensor.usm_ndarray` with two or more dimensions.
1385+
Parameter `k` is supported only of integer data type.
1386+
Otherwise the function will be executed sequentially on CPU.
1387+
13801388
Examples
13811389
--------
13821390
>>> import dpnp as np
@@ -1388,12 +1396,15 @@ def triu(x1, k=0):
13881396
13891397
"""
13901398

1391-
x1_desc = dpnp.get_dpnp_descriptor(x1, copy_when_nondefault_queue=False)
1392-
if x1_desc:
1393-
if not isinstance(k, int):
1399+
if not isinstance(x1, (dpnp.ndarray, dpt.usm_ndarray)):
1400+
pass
1401+
elif x1.ndim < 2:
1402+
pass
1403+
elif not isinstance(k, int):
1404+
if not (isinstance(k, (dpnp.ndarray, dpt.usm_ndarray)) and numpy.issubdtype(k.dtype, int)):
13941405
pass
1395-
else:
1396-
return dpnp_triu(x1_desc, k).get_pyobj()
1406+
else:
1407+
return dpnp_container.triu(x1, k=k)
13971408

13981409
return call_origin(numpy.triu, x1, k)
13991410

tests/skipped_tests.tbl

+3
Original file line numberDiff line numberDiff line change
@@ -433,6 +433,9 @@ tests/third_party/cupy/creation_tests/test_ranges.py::TestRanges::test_linspace_
433433
tests/third_party/cupy/creation_tests/test_matrix.py::TestTriLowerAndUpper_param_0_{shape=(2,)}::test_tril
434434
tests/third_party/cupy/creation_tests/test_matrix.py::TestTriLowerAndUpper_param_0_{shape=(2,)}::test_tril_nega
435435
tests/third_party/cupy/creation_tests/test_matrix.py::TestTriLowerAndUpper_param_0_{shape=(2,)}::test_tril_posi
436+
tests/third_party/cupy/creation_tests/test_matrix.py::TestTriLowerAndUpper_param_0_{shape=(2,)}::test_triu
437+
tests/third_party/cupy/creation_tests/test_matrix.py::TestTriLowerAndUpper_param_0_{shape=(2,)}::test_triu_nega
438+
tests/third_party/cupy/creation_tests/test_matrix.py::TestTriLowerAndUpper_param_0_{shape=(2,)}::test_triu_posi
436439
tests/third_party/cupy/indexing_tests/test_generate.py::TestAxisConcatenator::test_AxisConcatenator_init1
437440
tests/third_party/cupy/indexing_tests/test_generate.py::TestAxisConcatenator::test_len
438441
tests/third_party/cupy/indexing_tests/test_generate.py::TestC_::test_c_1

tests/skipped_tests_gpu.tbl

+3
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,9 @@ tests/third_party/cupy/creation_tests/test_matrix.py::TestMatrix::test_diag_extr
193193
tests/third_party/cupy/creation_tests/test_matrix.py::TestTriLowerAndUpper_param_0_{shape=(2,)}::test_tril
194194
tests/third_party/cupy/creation_tests/test_matrix.py::TestTriLowerAndUpper_param_0_{shape=(2,)}::test_tril_nega
195195
tests/third_party/cupy/creation_tests/test_matrix.py::TestTriLowerAndUpper_param_0_{shape=(2,)}::test_tril_posi
196+
tests/third_party/cupy/creation_tests/test_matrix.py::TestTriLowerAndUpper_param_0_{shape=(2,)}::test_triu
197+
tests/third_party/cupy/creation_tests/test_matrix.py::TestTriLowerAndUpper_param_0_{shape=(2,)}::test_triu_nega
198+
tests/third_party/cupy/creation_tests/test_matrix.py::TestTriLowerAndUpper_param_0_{shape=(2,)}::test_triu_posi
196199

197200
tests/third_party/cupy/indexing_tests/test_insert.py::TestFillDiagonal_param_4_{shape=(3, 3), val=(2,), wrap=True}::test_1darray
198201
tests/third_party/cupy/indexing_tests/test_insert.py::TestFillDiagonal_param_4_{shape=(3, 3), val=(2,), wrap=True}::test_fill_diagonal

tests/test_arraycreation.py

+9-10
Original file line numberDiff line numberDiff line change
@@ -281,22 +281,21 @@ def test_tril(m, k, dtype):
281281

282282

283283
@pytest.mark.parametrize("k",
284-
[-4, -3, -2, -1, 0, 1, 2, 3, 4],
285-
ids=['-4', '-3', '-2', '-1', '0', '1', '2', '3', '4'])
284+
[-3, -2, -1, 0, 1, 2, 3, 4, 5],
285+
ids=['-3', '-2', '-1', '0', '1', '2', '3', '4', '5'])
286286
@pytest.mark.parametrize("m",
287-
[[0, 1, 2, 3, 4],
288-
[[1, 2], [3, 4]],
287+
[[[1, 2], [3, 4]],
289288
[[0, 1, 2], [3, 4, 5], [6, 7, 8]],
290289
[[0, 1, 2, 3, 4], [5, 6, 7, 8, 9]]],
291-
ids=['[0, 1, 2, 3, 4]',
292-
'[[1, 2], [3, 4]]',
290+
ids=['[[1, 2], [3, 4]]',
293291
'[[0, 1, 2], [3, 4, 5], [6, 7, 8]]',
294292
'[[0, 1, 2, 3, 4], [5, 6, 7, 8, 9]]'])
295-
def test_triu(m, k):
296-
a = numpy.array(m)
293+
@pytest.mark.parametrize("dtype", get_all_dtypes(no_float16=False))
294+
def test_triu(m, k, dtype):
295+
a = numpy.array(m, dtype=dtype)
297296
ia = dpnp.array(a)
298-
expected = numpy.triu(a, k)
299-
result = dpnp.triu(ia, k)
297+
expected = numpy.triu(a, k=k)
298+
result = dpnp.triu(ia, k=k)
300299
assert_array_equal(expected, result)
301300

302301

tests/test_sycl_queue.py

+10
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,16 @@ def test_array_creation_like(func, kwargs, device_x, device_y):
161161
assert_sycl_queue_equal(y.sycl_queue, x.to_device(device_y).sycl_queue)
162162

163163

164+
@pytest.mark.parametrize("func", ["tril", "triu"], ids=["tril", "triu"])
165+
@pytest.mark.parametrize("device",
166+
valid_devices,
167+
ids=[device.filter_string for device in valid_devices])
168+
def test_tril_triu(func, device):
169+
x0 = dpnp.ones((3,3), device=device)
170+
x = getattr(dpnp, func)(x0)
171+
assert x.sycl_device == device
172+
173+
164174
@pytest.mark.usefixtures("allow_fall_back_on_numpy")
165175
@pytest.mark.parametrize(
166176
"func,data",

tests/test_usm_type.py

+7
Original file line numberDiff line numberDiff line change
@@ -61,3 +61,10 @@ def test_array_creation(func, args, usm_type_x, usm_type_y):
6161

6262
assert x.usm_type == usm_type_x
6363
assert y.usm_type == usm_type_y
64+
65+
@pytest.mark.parametrize("func", ["tril", "triu"], ids=["tril", "triu"])
66+
@pytest.mark.parametrize("usm_type", list_of_usm_types, ids=list_of_usm_types)
67+
def test_tril_triu(func, usm_type):
68+
x0 = dp.ones((3,3), usm_type=usm_type)
69+
x = getattr(dp, func)(x0)
70+
assert x.usm_type == usm_type

tests/third_party/cupy/creation_tests/test_matrix.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -180,10 +180,10 @@ def test_triu_array_like(self, xp):
180180
@testing.numpy_cupy_array_equal()
181181
def test_triu_nega(self, xp, dtype):
182182
m = testing.shaped_arange(self.shape, xp, dtype)
183-
return xp.triu(m, -1)
183+
return xp.triu(m, k=-1)
184184

185185
@testing.for_all_dtypes(no_complex=True)
186186
@testing.numpy_cupy_array_equal()
187187
def test_triu_posi(self, xp, dtype):
188188
m = testing.shaped_arange(self.shape, xp, dtype)
189-
return xp.triu(m, 1)
189+
return xp.triu(m, k=1)

0 commit comments

Comments
 (0)