Skip to content

Commit ff71682

Browse files
authored
Leverage dpctl.tensor.copy() implementation (#1540)
* Leveraged dpctl.tensor.copy() implementation * Renamed test files, since coverage tool requires unique names * dpctl accepted lowercase order
1 parent ec46177 commit ff71682

File tree

13 files changed

+361
-56
lines changed

13 files changed

+361
-56
lines changed

.github/workflows/conda-package.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ env:
1515
test_arraycreation.py
1616
test_dot.py
1717
test_dparray.py
18+
test_copy.py
1819
test_fft.py
1920
test_linalg.py
2021
test_logic.py

dpnp/backend/kernels/dpnp_krnl_elemwise.cpp

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1326,8 +1326,6 @@ static void func_map_init_elemwise_1arg_1type(func_map_t &fmap)
13261326
{ \
13271327
constexpr size_t lws = 64; \
13281328
constexpr unsigned int vec_sz = 8; \
1329-
constexpr sycl::access::address_space global_space = \
1330-
sycl::access::address_space::global_space; \
13311329
\
13321330
auto gws_range = sycl::range<1>( \
13331331
((result_size + lws * vec_sz - 1) / (lws * vec_sz)) * \

dpnp/backend/kernels/dpnp_krnl_logic.cpp

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -521,8 +521,6 @@ DPCTLSyclEventRef (*dpnp_any_ext_c)(DPCTLSyclQueueRef,
521521
else { \
522522
constexpr size_t lws = 64; \
523523
constexpr unsigned int vec_sz = 8; \
524-
constexpr sycl::access::address_space global_space = \
525-
sycl::access::address_space::global_space; \
526524
\
527525
auto gws_range = sycl::range<1>( \
528526
((result_size + lws * vec_sz - 1) / (lws * vec_sz)) * lws); \

dpnp/dpnp_algo/dpnp_algo_mathematical.pxi

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -352,7 +352,7 @@ cpdef tuple dpnp_modf(utils.dpnp_descriptor x1):
352352

353353

354354
cpdef utils.dpnp_descriptor dpnp_nancumprod(utils.dpnp_descriptor x1):
355-
cur_x1 = dpnp_copy(x1).get_pyobj()
355+
cur_x1 = x1.get_pyobj().copy()
356356

357357
cur_x1_flatiter = cur_x1.flat
358358

@@ -365,7 +365,7 @@ cpdef utils.dpnp_descriptor dpnp_nancumprod(utils.dpnp_descriptor x1):
365365

366366

367367
cpdef utils.dpnp_descriptor dpnp_nancumsum(utils.dpnp_descriptor x1):
368-
cur_x1 = dpnp_copy(x1).get_pyobj()
368+
cur_x1 = x1.get_pyobj().copy()
369369

370370
cur_x1_flatiter = cur_x1.flat
371371

dpnp/dpnp_array.py

Lines changed: 50 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,15 @@ def __complex__(self):
168168
return self._array_obj.__complex__()
169169

170170
# '__contains__',
171-
# '__copy__',
171+
172+
def __copy__(self):
173+
"""
174+
Used if :func:`copy.copy` is called on an array. Returns a copy of the array.
175+
176+
Equivalent to ``a.copy(order="K")``.
177+
"""
178+
return self.copy(order="K")
179+
172180
# '__deepcopy__',
173181
# '__delattr__',
174182
# '__delitem__',
@@ -651,7 +659,47 @@ def conjugate(self):
651659
else:
652660
return dpnp.conjugate(self)
653661

654-
# 'copy',
662+
def copy(self, order="C"):
663+
"""
664+
Return a copy of the array.
665+
666+
Returns
667+
-------
668+
out : dpnp.ndarray
669+
A copy of the array.
670+
671+
See also
672+
--------
673+
:obj:`dpnp.copy` : Similar function with different default behavior
674+
:obj:`dpnp.copyto` : Copies values from one array to another.
675+
676+
Notes
677+
-----
678+
This function is the preferred method for creating an array copy. The
679+
function :func:`dpnp.copy` is similar, but it defaults to using order 'K'.
680+
681+
Examples
682+
--------
683+
>>> import dpnp as np
684+
>>> x = np.array([[1, 2, 3], [4, 5, 6]], order='F')
685+
>>> y = x.copy()
686+
>>> x.fill(0)
687+
688+
>>> x
689+
array([[0, 0, 0],
690+
[0, 0, 0]])
691+
692+
>>> y
693+
array([[1, 2, 3],
694+
[4, 5, 6]])
695+
696+
>>> y.flags['C_CONTIGUOUS']
697+
True
698+
699+
"""
700+
701+
return dpnp.copy(self, order=order)
702+
655703
# 'ctypes',
656704
# 'cumprod',
657705

dpnp/dpnp_container.py

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
__all__ = [
4444
"arange",
4545
"asarray",
46+
"copy",
4647
"empty",
4748
"eye",
4849
"full",
@@ -135,6 +136,15 @@ def asarray(
135136
return dpnp_array(array_obj.shape, buffer=array_obj, order=order)
136137

137138

139+
def copy(x1, /, *, order="K"):
140+
"""Creates `dpnp_array` as a copy of given instance of input array."""
141+
if order is None:
142+
order = "K"
143+
144+
array_obj = dpt.copy(dpnp.get_usm_ndarray(x1), order=order)
145+
return dpnp_array(array_obj.shape, buffer=array_obj, order="K")
146+
147+
138148
def empty(
139149
shape,
140150
*,
@@ -264,9 +274,7 @@ def meshgrid(*xi, indexing="xy"):
264274
"""Creates list of `dpnp_array` coordinate matrices from vectors."""
265275
if len(xi) == 0:
266276
return []
267-
arrays = tuple(
268-
x.get_array() if isinstance(x, dpnp_array) else x for x in xi
269-
)
277+
arrays = tuple(dpnp.get_usm_ndarray(x) for x in xi)
270278
arrays_obj = dpt.meshgrid(*arrays, indexing=indexing)
271279
return [
272280
dpnp_array._create_from_usm_ndarray(array_obj)
@@ -304,17 +312,13 @@ def ones(
304312

305313
def tril(x1, /, *, k=0):
306314
"""Creates `dpnp_array` as lower triangular part of an input array."""
307-
array_obj = dpt.tril(
308-
x1.get_array() if isinstance(x1, dpnp_array) else x1, k
309-
)
315+
array_obj = dpt.tril(dpnp.get_usm_ndarray(x1), k)
310316
return dpnp_array(array_obj.shape, buffer=array_obj, order="K")
311317

312318

313319
def triu(x1, /, *, k=0):
314320
"""Creates `dpnp_array` as upper triangular part of an input array."""
315-
array_obj = dpt.triu(
316-
x1.get_array() if isinstance(x1, dpnp_array) else x1, k
317-
)
321+
array_obj = dpt.triu(dpnp.get_usm_ndarray(x1), k)
318322
return dpnp_array(array_obj.shape, buffer=array_obj, order="K")
319323

320324

0 commit comments

Comments
 (0)