Skip to content

Commit 16a0f57

Browse files
authored
algo module. optimize imports a bit (#741)
1 parent 4eb0947 commit 16a0f57

14 files changed

+50
-95
lines changed

dpnp/dpnp_algo/dpnp_algo.pyx

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ and the rest of the library
3333
"""
3434

3535
from libc.time cimport time, time_t
36+
import dpnp
3637
import dpnp.config as config
3738
import numpy
3839

@@ -111,7 +112,7 @@ cpdef dparray dpnp_array(obj, dtype=None):
111112
if not cpython.PySequence_Check(obj):
112113
raise TypeError(f"DPNP array(): Unsupported non-sequence obj={type(obj)}")
113114

114-
obj_shape, elem_dtype = get_shape_dtype(obj)
115+
obj_shape, elem_dtype = utils.get_shape_dtype(obj)
115116
if dtype is not None:
116117
""" Set type from parameter. result might be empty array """
117118
result = dparray(obj_shape, dtype=dtype)
@@ -122,7 +123,7 @@ cpdef dparray dpnp_array(obj, dtype=None):
122123
else:
123124
result = dparray(obj_shape, dtype=elem_dtype)
124125

125-
copy_values_to_dparray(result, obj)
126+
utils.copy_values_to_dparray(result, obj)
126127

127128
return result
128129

@@ -241,9 +242,9 @@ cpdef dparray dpnp_matmul(dparray in_array1, dparray in_array2, dparray out=None
241242

242243
if out is not None:
243244
if out.dtype != result_type:
244-
checker_throw_value_error('matmul', 'out.dtype', out.dtype, result_type)
245+
utils.checker_throw_value_error('matmul', 'out.dtype', out.dtype, result_type)
245246
if out.shape != shape_result:
246-
checker_throw_value_error('matmul', 'out.shape', out.shape, shape_result)
247+
utils.checker_throw_value_error('matmul', 'out.shape', out.shape, shape_result)
247248
result = out
248249
else:
249250
result = dparray(shape_result, dtype=result_type)
@@ -302,7 +303,7 @@ cpdef DPNPFuncType dpnp_dtype_to_DPNPFuncType(dtype):
302303
elif dtype in [numpy.bool, numpy.bool_, 'bool', '?']:
303304
return DPNP_FT_BOOL
304305
else:
305-
checker_throw_type_error("dpnp_dtype_to_DPNPFuncType", dtype)
306+
utils.checker_throw_type_error("dpnp_dtype_to_DPNPFuncType", dtype)
306307

307308
cpdef dpnp_DPNPFuncType_to_dtype(size_t type):
308309
"""
@@ -322,7 +323,7 @@ cpdef dpnp_DPNPFuncType_to_dtype(size_t type):
322323
elif type == <size_t > DPNP_FT_BOOL:
323324
return numpy.bool
324325
else:
325-
checker_throw_type_error("dpnp_DPNPFuncType_to_dtype", type)
326+
utils.checker_throw_type_error("dpnp_DPNPFuncType_to_dtype", type)
326327

327328

328329
cdef dparray call_fptr_1out(DPNPFuncName fptr_name, result_shape, result_dtype):
@@ -373,23 +374,23 @@ cdef dparray call_fptr_2in_1out(DPNPFuncName fptr_name, object x1_obj, object x2
373374

374375
cdef dparray x1_dparray, x2_dparray
375376

376-
common_type = find_common_type(x1_obj, x2_obj)
377+
common_type = utils.find_common_type(x1_obj, x2_obj)
377378

378379
if x1_obj_is_dparray:
379380
x1_dparray = x1_obj
380381
else:
381382
x1_dparray = dparray((1,), dtype=common_type)
382-
copy_values_to_dparray(x1_dparray, (x1_obj,))
383+
utils.copy_values_to_dparray(x1_dparray, (x1_obj,))
383384

384385
if x2_obj_is_dparray:
385386
x2_dparray = x2_obj
386387
else:
387388
x2_dparray = dparray((1,), dtype=common_type)
388-
copy_values_to_dparray(x2_dparray, (x2_obj,))
389+
utils.copy_values_to_dparray(x2_dparray, (x2_obj,))
389390

390391
x1_shape = x1_dparray.shape
391392
x2_shape = x2_dparray.shape
392-
result_shape = get_common_shape(x1_shape, x2_shape)
393+
result_shape = utils.get_common_shape(x1_shape, x2_shape)
393394

394395
# Convert string type names (dparray.dtype) to C enum DPNPFuncType
395396
cdef DPNPFuncType x1_c_type = dpnp_dtype_to_DPNPFuncType(x1_dparray.dtype)
@@ -401,7 +402,7 @@ cdef dparray call_fptr_2in_1out(DPNPFuncName fptr_name, object x1_obj, object x2
401402
# TODO: apply parameters out and dtype after reafactoring fmap (required 4th level nesting)
402403

403404
# Create result array
404-
cdef dparray result = create_output_array(result_shape, kernel_data.return_type, out)
405+
cdef dparray result = utils.create_output_array(result_shape, kernel_data.return_type, out)
405406

406407
""" Call FPTR function """
407408
cdef fptr_2in_1out_t func = <fptr_2in_1out_t > kernel_data.ptr
@@ -411,7 +412,7 @@ cdef dparray call_fptr_2in_1out(DPNPFuncName fptr_name, object x1_obj, object x2
411412
return result
412413

413414
# this is replacement for "call_fptr_2in_1out". original function must be deleted after transotion.
414-
cdef dparray call_fptr_2in_1out_new(DPNPFuncName fptr_name, dpnp_descriptor x1_obj, dpnp_descriptor x2_obj,
415+
cdef dparray call_fptr_2in_1out_new(DPNPFuncName fptr_name, utils.dpnp_descriptor x1_obj, utils.dpnp_descriptor x2_obj,
415416
object dtype=None, dparray out=None, object where=True):
416417
# Convert string type names (dparray.dtype) to C enum DPNPFuncType
417418
cdef DPNPFuncType x1_c_type = dpnp_dtype_to_DPNPFuncType(x1_obj.dtype)
@@ -423,8 +424,8 @@ cdef dparray call_fptr_2in_1out_new(DPNPFuncName fptr_name, dpnp_descriptor x1_o
423424
# Create result array
424425
cdef dparray_shape_type x1_shape = x1_obj.shape
425426
cdef dparray_shape_type x2_shape = x2_obj.shape
426-
cdef dparray_shape_type result_shape = get_common_shape(x1_shape, x2_shape)
427-
cdef dparray result = create_output_array(result_shape, kernel_data.return_type, out)
427+
cdef dparray_shape_type result_shape = utils.get_common_shape(x1_shape, x2_shape)
428+
cdef dparray result = utils.create_output_array(result_shape, kernel_data.return_type, out)
428429

429430
""" Call FPTR function """
430431
cdef fptr_2in_1out_t func = <fptr_2in_1out_t > kernel_data.ptr

dpnp/dpnp_algo/dpnp_algo_arraycreation.pyx

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,7 @@ and the rest of the library
3232
3333
"""
3434

35-
36-
import dpnp
37-
import numpy
38-
39-
from dpnp.dpnp_utils cimport *
40-
from dpnp.dpnp_algo cimport *
41-
35+
# NO IMPORTs here. All imports must be placed into main "dpnp_algo.pyx" file
4236

4337
__all__ += [
4438
"dpnp_copy",

dpnp/dpnp_algo/dpnp_algo_bitwise.pyx

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,7 @@ and the rest of the library
3232
3333
"""
3434

35-
36-
from dpnp.dpnp_utils cimport *
37-
35+
# NO IMPORTs here. All imports must be placed into main "dpnp_algo.pyx" file
3836

3937
__all__ += [
4038
"dpnp_bitwise_and",

dpnp/dpnp_algo/dpnp_algo_counting.pyx

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,7 @@ and the rest of the library
3232
3333
"""
3434

35-
36-
import numpy
37-
from dpnp.dpnp_utils cimport *
38-
35+
# NO IMPORTs here. All imports must be placed into main "dpnp_algo.pyx" file
3936

4037
__all__ += [
4138
"dpnp_count_nonzero"

dpnp/dpnp_algo/dpnp_algo_indexing.pyx

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,7 @@ and the rest of the library
3232
3333
"""
3434

35-
36-
import numpy
37-
from dpnp.dpnp_utils cimport *
38-
from dpnp.dpnp_iface_counting import count_nonzero
39-
35+
# NO IMPORTs here. All imports must be placed into main "dpnp_algo.pyx" file
4036

4137
__all__ += [
4238
"dpnp_choose",
@@ -169,7 +165,7 @@ cpdef tuple dpnp_nonzero(dparray in_array1):
169165
res_count = in_array1.ndim
170166

171167
# have to go through array one extra time to count size of result arrays
172-
res_size = count_nonzero(in_array1)
168+
res_size = dpnp.count_nonzero(in_array1)
173169

174170
cdef DPNPFuncType param1_type = dpnp_dtype_to_DPNPFuncType(in_array1.dtype)
175171

@@ -185,7 +181,7 @@ cpdef tuple dpnp_nonzero(dparray in_array1):
185181

186182
res_list.append(res_arr)
187183

188-
result = _object_to_tuple(res_list)
184+
result = utils._object_to_tuple(res_list)
189185

190186
return result
191187

dpnp/dpnp_algo/dpnp_algo_linearalgebra.pyx

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,7 @@ and the rest of the library
3232
3333
"""
3434

35-
from dpnp.dpnp_utils cimport *
36-
cimport numpy
37-
35+
# NO IMPORTs here. All imports must be placed into main "dpnp_algo.pyx" file
3836

3937
__all__ += [
4038
"dpnp_dot",
@@ -78,7 +76,7 @@ cpdef dparray dpnp_dot(dparray in_array1, dparray in_array2):
7876
# vector
7977
# test: pytest tests/third_party/cupy/linalg_tests/test_product.py::TestProduct::test_dot_vec1 -v -s
8078
if size1 != size2:
81-
raise checker_throw_runtime_error("dpnp_dot", "input vectors must be of equal size")
79+
utils.checker_throw_runtime_error("dpnp_dot", "input vectors must be of equal size")
8280

8381
# convert string type names (dparray.dtype) to C enum DPNPFuncType
8482
cdef DPNPFuncType param1_type = dpnp_dtype_to_DPNPFuncType(in_array1.dtype)

dpnp/dpnp_algo/dpnp_algo_logic.pyx

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,7 @@ and the rest of the library
3232
3333
"""
3434

35-
36-
import dpnp
37-
import numpy
38-
39-
from dpnp.dpnp_utils cimport *
40-
from dpnp.dpnp_algo cimport *
41-
35+
# NO IMPORTs here. All imports must be placed into main "dpnp_algo.pyx" file
4236

4337
__all__ += [
4438
"dpnp_all",

dpnp/dpnp_algo/dpnp_algo_manipulation.pyx

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,7 @@ and the rest of the library
3232
3333
"""
3434

35-
36-
from dpnp.dpnp_utils cimport *
37-
35+
# NO IMPORTs here. All imports must be placed into main "dpnp_algo.pyx" file
3836

3937
__all__ += [
4038
"dpnp_atleast_2d",
@@ -98,21 +96,21 @@ cpdef dpnp_copyto(dparray dst, dparray src, where=True):
9896

9997

10098
cpdef dparray dpnp_expand_dims(dparray in_array, axis):
101-
axis_tuple = _object_to_tuple(axis)
99+
axis_tuple = utils._object_to_tuple(axis)
102100
result_ndim = len(axis_tuple) + in_array.ndim
103101

104102
if len(axis_tuple) == 0:
105103
axis_ndim = 0
106104
else:
107105
axis_ndim = max(-min(0, min(axis_tuple)), max(0, max(axis_tuple))) + 1
108106

109-
axis_norm = _object_to_tuple(normalize_axis(axis_tuple, result_ndim))
107+
axis_norm = utils._object_to_tuple(utils.normalize_axis(axis_tuple, result_ndim))
110108

111109
if axis_ndim - len(axis_norm) > in_array.ndim:
112-
checker_throw_axis_error("dpnp_expand_dims", "axis", axis, axis_ndim)
110+
utils.checker_throw_axis_error("dpnp_expand_dims", "axis", axis, axis_ndim)
113111

114112
if len(axis_norm) > len(set(axis_norm)):
115-
checker_throw_value_error("dpnp_expand_dims", "axis", axis, "no repeated axis")
113+
utils.checker_throw_value_error("dpnp_expand_dims", "axis", axis, "no repeated axis")
116114

117115
shape_list = []
118116
axis_idx = 0
@@ -123,7 +121,7 @@ cpdef dparray dpnp_expand_dims(dparray in_array, axis):
123121
shape_list.append(in_array.shape[axis_idx])
124122
axis_idx = axis_idx + 1
125123

126-
shape = _object_to_tuple(shape_list)
124+
shape = utils._object_to_tuple(shape_list)
127125
cdef dparray result = dpnp.copy(in_array).reshape(shape)
128126

129127
return result
@@ -195,15 +193,15 @@ cpdef dparray dpnp_squeeze(dparray in_array, axis):
195193
if in_array.shape[i] != 1:
196194
shape_list.append(in_array.shape[i])
197195
else:
198-
axis_norm = _object_to_tuple(normalize_axis(_object_to_tuple(axis), in_array.ndim))
196+
axis_norm = utils._object_to_tuple(utils.normalize_axis(utils._object_to_tuple(axis), in_array.ndim))
199197
for i in range(in_array.ndim):
200198
if i in axis_norm:
201199
if in_array.shape[i] != 1:
202-
checker_throw_value_error("dpnp_squeeze", "axis", axis, "axis has size not equal to one")
200+
utils.checker_throw_value_error("dpnp_squeeze", "axis", axis, "axis has size not equal to one")
203201
else:
204202
shape_list.append(in_array.shape[i])
205203

206-
shape = _object_to_tuple(shape_list)
204+
shape = utils._object_to_tuple(shape_list)
207205
cdef dparray result = dpnp.copy(in_array).reshape(shape)
208206

209207
return result

dpnp/dpnp_algo/dpnp_algo_mathematical.pyx

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,7 @@ and the rest of the library
3232
3333
"""
3434

35-
from dpnp.dpnp_utils cimport *
36-
import dpnp
37-
import numpy
38-
cimport numpy
39-
35+
# NO IMPORTs here. All imports must be placed into main "dpnp_algo.pyx" file
4036

4137
__all__ += [
4238
"dpnp_absolute",
@@ -257,7 +253,7 @@ cpdef dparray dpnp_minimum(object x1_obj, object x2_obj, object dtype=None, dpar
257253
return call_fptr_2in_1out(DPNP_FN_MINIMUM, x1_obj, x2_obj, dtype=dtype, out=out, where=where)
258254

259255

260-
cpdef tuple dpnp_modf(dpnp_descriptor x1):
256+
cpdef tuple dpnp_modf(utils.dpnp_descriptor x1):
261257
""" Convert string type names (dparray.dtype) to C enum DPNPFuncType """
262258
cdef DPNPFuncType param1_type = dpnp_dtype_to_DPNPFuncType(x1.dtype)
263259

@@ -355,16 +351,16 @@ cpdef dparray dpnp_prod(dparray input, object axis=None, object dtype=None, dpar
355351
cdef dparray_shape_type input_shape = input.shape
356352
cdef DPNPFuncType input_c_type = dpnp_dtype_to_DPNPFuncType(input.dtype)
357353

358-
cdef dparray_shape_type axis_shape = _object_to_tuple(axis)
354+
cdef dparray_shape_type axis_shape = utils._object_to_tuple(axis)
359355

360-
cdef dparray_shape_type result_shape = get_reduction_output_shape(input_shape, axis, keepdims)
361-
cdef DPNPFuncType result_c_type = get_output_c_type(DPNP_FN_PROD, input_c_type, out, dtype)
356+
cdef dparray_shape_type result_shape = utils.get_reduction_output_shape(input_shape, axis, keepdims)
357+
cdef DPNPFuncType result_c_type = utils.get_output_c_type(DPNP_FN_PROD, input_c_type, out, dtype)
362358

363359
""" select kernel """
364360
cdef DPNPFuncData kernel_data = get_dpnp_function_ptr(DPNP_FN_PROD, input_c_type, result_c_type)
365361

366362
""" Create result array """
367-
cdef dparray result = create_output_array(result_shape, result_c_type, out)
363+
cdef dparray result = utils.create_output_array(result_shape, result_c_type, out)
368364
cdef dpnp_reduction_c_t func = <dpnp_reduction_c_t > kernel_data.ptr
369365

370366
""" Call FPTR interface function """
@@ -390,16 +386,16 @@ cpdef dparray dpnp_sum(dparray input, object axis=None, object dtype=None, dparr
390386
cdef dparray_shape_type input_shape = input.shape
391387
cdef DPNPFuncType input_c_type = dpnp_dtype_to_DPNPFuncType(input.dtype)
392388

393-
cdef dparray_shape_type axis_shape = _object_to_tuple(axis)
389+
cdef dparray_shape_type axis_shape = utils._object_to_tuple(axis)
394390

395-
cdef dparray_shape_type result_shape = get_reduction_output_shape(input_shape, axis, keepdims)
396-
cdef DPNPFuncType result_c_type = get_output_c_type(DPNP_FN_SUM, input_c_type, out, dtype)
391+
cdef dparray_shape_type result_shape = utils.get_reduction_output_shape(input_shape, axis, keepdims)
392+
cdef DPNPFuncType result_c_type = utils.get_output_c_type(DPNP_FN_SUM, input_c_type, out, dtype)
397393

398394
""" select kernel """
399395
cdef DPNPFuncData kernel_data = get_dpnp_function_ptr(DPNP_FN_SUM, input_c_type, result_c_type)
400396

401397
""" Create result array """
402-
cdef dparray result = create_output_array(result_shape, result_c_type, out)
398+
cdef dparray result = utils.create_output_array(result_shape, result_c_type, out)
403399
cdef dpnp_reduction_c_t func = <dpnp_reduction_c_t > kernel_data.ptr
404400

405401
""" Call FPTR interface function """

dpnp/dpnp_algo/dpnp_algo_searching.pyx

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,7 @@ and the rest of the library
3232
3333
"""
3434

35-
from libcpp.string cimport string
36-
import cython
37-
import numpy
38-
39-
from dpnp.dpnp_utils cimport *
35+
# NO IMPORTs here. All imports must be placed into main "dpnp_algo.pyx" file
4036

4137
__all__ += [
4238
"dpnp_argmax",

0 commit comments

Comments
 (0)