Skip to content

Commit 03c1185

Browse files
Reshape dpctl impl (#957)
* call reshape from dpctl * reshape in non-skipped tests * use dpctl.tensor.reshape
1 parent 1fc46a2 commit 03c1185

14 files changed

+52
-22
lines changed

dpnp/dpnp_algo/dpnp_algo.pyx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ import dpnp.dpnp_utils as utils_py
4040
from dpnp.dpnp_container import container_copy
4141

4242
import numpy
43+
import dpctl
4344

4445
cimport cpython
4546
cimport dpnp.dpnp_utils as utils

dpnp/dpnp_algo/dpnp_algo_indexing.pyx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -407,7 +407,7 @@ cpdef object dpnp_take_along_axis(object arr, object indices, int axis):
407407
result_array[result_offset] = arr_elem
408408

409409
dpnp_array = dpnp.array(result_array, dtype=res_type)
410-
dpnp_result_array = dpnp_array.reshape(res_shape)
410+
dpnp_result_array = dpnp.reshape(dpnp_array, res_shape)
411411
return dpnp_result_array
412412

413413
else:

dpnp/dpnp_algo/dpnp_algo_manipulation.pyx

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ __all__ += [
4040
"dpnp_copyto",
4141
"dpnp_expand_dims",
4242
"dpnp_repeat",
43+
"dpnp_reshape",
4344
"dpnp_transpose",
4445
"dpnp_squeeze",
4546
]
@@ -125,7 +126,7 @@ cpdef utils.dpnp_descriptor dpnp_expand_dims(utils.dpnp_descriptor in_array, axi
125126
shape_list.push_back(in_array.shape[axis_idx])
126127
axis_idx = axis_idx + 1
127128

128-
cdef utils.dpnp_descriptor result = dpnp.get_dpnp_descriptor(dpnp_copy(in_array).get_pyobj().reshape(shape_list))
129+
cdef utils.dpnp_descriptor result = dpnp.get_dpnp_descriptor(dpnp.reshape(dpnp_copy(in_array).get_pyobj(), (shape_list)))
129130

130131
return result
131132

@@ -145,6 +146,11 @@ cpdef utils.dpnp_descriptor dpnp_repeat(utils.dpnp_descriptor array1, repeats, a
145146
return result
146147

147148

149+
cpdef utils.dpnp_descriptor dpnp_reshape(utils.dpnp_descriptor array1, newshape, order=None):
150+
# return dpnp.get_dpnp_descriptor(dpctl.tensor.usm_ndarray(newshape, dtype=numpy.dtype(array1.dtype).name, buffer=array1.get_pyobj()))
151+
return dpnp.get_dpnp_descriptor(dpctl.tensor.reshape(array1.get_pyobj(), newshape))
152+
153+
148154
cpdef utils.dpnp_descriptor dpnp_transpose(utils.dpnp_descriptor array1, axes=None):
149155
cdef shape_type_c input_shape = array1.shape
150156
cdef size_t input_shape_size = array1.ndim
@@ -203,6 +209,6 @@ cpdef utils.dpnp_descriptor dpnp_squeeze(utils.dpnp_descriptor in_array, axis):
203209
else:
204210
shape_list.push_back(in_array.shape[i])
205211

206-
cdef utils.dpnp_descriptor result = dpnp.get_dpnp_descriptor(dpnp_copy(in_array).get_pyobj().reshape(shape_list))
212+
cdef utils.dpnp_descriptor result = dpnp.get_dpnp_descriptor(dpnp.reshape(dpnp_copy(in_array).get_pyobj(), (shape_list)))
207213

208214
return result

dpnp/dpnp_algo/dpnp_algo_mathematical.pyx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,7 @@ cpdef object dpnp_diff(utils.dpnp_descriptor input, int n):
202202
counter = 0
203203

204204
dpnp_array = dpnp.array(res, dtype=input.dtype)
205-
arr = dpnp_array.reshape(output_shape)
205+
arr = dpnp.reshape(dpnp_array, output_shape)
206206
return arr
207207
else:
208208
return dpnp.array([], dtype=input.dtype)

dpnp/dpnp_algo/dpnp_algo_statistics.pyx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -326,7 +326,7 @@ cpdef object dpnp_mean(utils.dpnp_descriptor input, axis):
326326
if i not in axis_:
327327
del_ = del_ / shape_input[i]
328328
dpnp_array = dpnp.array(result_array, dtype=input.dtype)
329-
dpnp_result_array = dpnp_array.reshape(output_shape)
329+
dpnp_result_array = dpnp.reshape(dpnp_array, output_shape)
330330
return dpnp_result_array / del_
331331

332332

dpnp/dpnp_iface_manipulation.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@
6262
"moveaxis",
6363
"ravel",
6464
"repeat",
65+
"reshape",
6566
"rollaxis",
6667
"squeeze",
6768
"stack",
@@ -465,6 +466,28 @@ def repeat(x1, repeats, axis=None):
465466
return call_origin(numpy.repeat, x1, repeats, axis)
466467

467468

469+
def reshape(x1, newshape, order='C'):
470+
"""
471+
Gives a new shape to an array without changing its data.
472+
473+
For full documentation refer to :obj:`numpy.reshape`.
474+
475+
Limitations
476+
-----------
477+
Only 'C' order is supported.
478+
479+
"""
480+
481+
x1_desc = dpnp.get_dpnp_descriptor(x1)
482+
if x1_desc:
483+
if order != 'C':
484+
pass
485+
else:
486+
return dpnp_reshape(x1_desc, newshape, order).get_pyobj()
487+
488+
return call_origin(numpy.reshape, x1, newshape, order)
489+
490+
468491
def rollaxis(x1, axis, start=0):
469492
"""
470493
Roll the specified axis backwards, until it lies in a given position.

dpnp/linalg/dpnp_algo_linalg.pyx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -250,14 +250,14 @@ cpdef object dpnp_norm(object input, ord=None, axis=None):
250250
for i in range(absx_size):
251251
absx_elem = absx.item(i)
252252
absx_power[i] = absx_elem ** ord
253-
absx_ = absx_power.reshape(absx.shape)
253+
absx_ = dpnp.reshape(absx_power, absx.shape)
254254
ret = dpnp.sum(absx_, axis=axis)
255255
ret_size = ret.size
256256
ret_power = utils_py.create_output_descriptor_py((ret_size,), None, None).get_pyobj()
257257
for i in range(ret_size):
258258
ret_elem = ret.item(i)
259259
ret_power[i] = ret_elem ** (1 / ord)
260-
ret_ = ret_power.reshape(ret.shape)
260+
ret_ = dpnp.reshape(ret_power, ret.shape)
261261
return ret_
262262
elif len_axis == 2:
263263
row_axis, col_axis = axis_

examples/example1.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,8 @@
5252

5353

5454
def run_dgemm(executor, name, size, test_type, repetition):
55-
x1 = executor.arange(size * size, dtype=test_type).reshape((size, size))
56-
x2 = executor.arange(size * size, dtype=test_type).reshape((size, size))
55+
x1 = executor.reshape(executor.arange(size * size, dtype=test_type), (size, size))
56+
x2 = executor.reshape(executor.arange(size * size, dtype=test_type), (size, size))
5757

5858
times = []
5959
for iteration in range(repetition):

examples/example10.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@
4747

4848

4949
def run(executor, size, test_type, repetition):
50-
x = executor.arange(size * size, dtype=test_type).reshape((size, size))
50+
x = executor.reshape(executor.arange(size * size, dtype=test_type), (size, size))
5151

5252
times = []
5353
for _ in range(repetition):

examples/example7.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@
5252

5353

5454
def run_function(executor, name, size, test_type, repetition):
55-
x = executor.arange(size * size, dtype=test_type).reshape((size, size))
55+
x = executor.reshape(executor.arange(size * size, dtype=test_type), (size, size))
5656

5757
times = []
5858
for iteration in range(repetition):

0 commit comments

Comments
 (0)