Skip to content

Commit d191e97

Browse files
Merge pull request #1735 from IntelPython/dpctl-support-numpy-2.0
Dpctl support for numpy 2.0 in runtime
2 parents 71901c7 + 4df7704 commit d191e97

14 files changed

+131
-68
lines changed

.pre-commit-config.yaml

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,23 @@
22
# See https://pre-commit.com/hooks.html for more hooks
33
repos:
44
- repo: https://github.com/PyCQA/bandit
5-
rev: '1.7.4'
5+
rev: '1.7.9'
66
hooks:
77
- id: bandit
88
pass_filenames: false
99
args: ["-r", "dpctl", "-lll"]
1010
- repo: https://github.com/pre-commit/pre-commit-hooks
11-
rev: v4.3.0
11+
rev: v4.6.0
1212
hooks:
1313
- id: end-of-file-fixer
1414
- id: trailing-whitespace
1515
- repo: https://github.com/psf/black
16-
rev: 22.12.0
16+
rev: 24.4.2
1717
hooks:
1818
- id: black
1919
exclude: "versioneer.py|dpctl/_version.py"
2020
- repo: https://github.com/pycqa/isort
21-
rev: 5.12.0
21+
rev: 5.13.2
2222
hooks:
2323
- id: isort
2424
name: isort (python)
@@ -29,7 +29,7 @@ repos:
2929
name: isort (pyi)
3030
types: [pyi]
3131
- repo: https://github.com/pycqa/flake8
32-
rev: 5.0.4
32+
rev: 7.1.0
3333
hooks:
3434
- id: flake8
3535
- repo: https://github.com/pocc/pre-commit-hooks

dpctl/tensor/_accumulation.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,6 @@
1414
# See the License for the specific language governing permissions and
1515
# limitations under the License.
1616

17-
from numpy.core.numeric import normalize_axis_index
18-
1917
import dpctl
2018
import dpctl.tensor as dpt
2119
import dpctl.tensor._tensor_accumulation_impl as tai
@@ -27,6 +25,8 @@
2725
)
2826
from dpctl.utils import ExecutionPlacementError, SequentialOrderManager
2927

28+
from ._numpy_helper import normalize_axis_index
29+
3030

3131
def _accumulate_common(
3232
x,

dpctl/tensor/_copy_utils.py

Lines changed: 27 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
import operator
1818

1919
import numpy as np
20-
from numpy.core.numeric import normalize_axis_index
2120

2221
import dpctl
2322
import dpctl.memory as dpm
@@ -28,6 +27,8 @@
2827
from dpctl.tensor._device import normalize_queue_device
2928
from dpctl.tensor._type_utils import _dtype_supported_by_device_impl
3029

30+
from ._numpy_helper import normalize_axis_index
31+
3132
__doc__ = (
3233
"Implementation module for copy- and cast- operations on "
3334
":class:`dpctl.tensor.usm_ndarray`."
@@ -382,9 +383,11 @@ def _empty_like_orderK(X, dt, usm_type=None, dev=None):
382383
if min(st) < 0:
383384
st_sorted = [st[i] for i in perm]
384385
sl = tuple(
385-
slice(None, None, -1)
386-
if st_sorted[i] < 0
387-
else slice(None, None, None)
386+
(
387+
slice(None, None, -1)
388+
if st_sorted[i] < 0
389+
else slice(None, None, None)
390+
)
388391
for i in range(X.ndim)
389392
)
390393
R = R[sl]
@@ -435,9 +438,11 @@ def _empty_like_pair_orderK(X1, X2, dt, res_shape, usm_type, dev):
435438
R = dpt.empty(sh_sorted, dtype=dt, usm_type=usm_type, device=dev, order="C")
436439
if max(min(st1_sorted), min(st2_sorted)) < 0:
437440
sl = tuple(
438-
slice(None, None, -1)
439-
if (st1_sorted[i] < 0 and st2_sorted[i] < 0)
440-
else slice(None, None, None)
441+
(
442+
slice(None, None, -1)
443+
if (st1_sorted[i] < 0 and st2_sorted[i] < 0)
444+
else slice(None, None, None)
445+
)
441446
for i in range(nd1)
442447
)
443448
R = R[sl]
@@ -503,9 +508,15 @@ def _empty_like_triple_orderK(X1, X2, X3, dt, res_shape, usm_type, dev):
503508
R = dpt.empty(sh_sorted, dtype=dt, usm_type=usm_type, device=dev, order="C")
504509
if max(min(st1_sorted), min(st2_sorted), min(st3_sorted)) < 0:
505510
sl = tuple(
506-
slice(None, None, -1)
507-
if (st1_sorted[i] < 0 and st2_sorted[i] < 0 and st3_sorted[i] < 0)
508-
else slice(None, None, None)
511+
(
512+
slice(None, None, -1)
513+
if (
514+
st1_sorted[i] < 0
515+
and st2_sorted[i] < 0
516+
and st3_sorted[i] < 0
517+
)
518+
else slice(None, None, None)
519+
)
509520
for i in range(nd1)
510521
)
511522
R = R[sl]
@@ -826,9 +837,9 @@ def _take_multi_index(ary, inds, p):
826837
)
827838
inds = tuple(
828839
map(
829-
lambda ind: ind
830-
if ind.dtype == ind_dt
831-
else dpt.astype(ind, ind_dt),
840+
lambda ind: (
841+
ind if ind.dtype == ind_dt else dpt.astype(ind, ind_dt)
842+
),
832843
inds,
833844
)
834845
)
@@ -975,9 +986,9 @@ def _put_multi_index(ary, inds, p, vals):
975986
)
976987
inds = tuple(
977988
map(
978-
lambda ind: ind
979-
if ind.dtype == ind_dt
980-
else dpt.astype(ind, ind_dt),
989+
lambda ind: (
990+
ind if ind.dtype == ind_dt else dpt.astype(ind, ind_dt)
991+
),
981992
inds,
982993
)
983994
)

dpctl/tensor/_indexing_functions.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,13 @@
1616

1717
import operator
1818

19-
from numpy.core.numeric import normalize_axis_index
20-
2119
import dpctl
2220
import dpctl.tensor as dpt
2321
import dpctl.tensor._tensor_impl as ti
2422
import dpctl.utils
2523

2624
from ._copy_utils import _extract_impl, _nonzero_impl
25+
from ._numpy_helper import normalize_axis_index
2726

2827

2928
def _get_indexing_mode(name):

dpctl/tensor/_linear_algebra_functions.py

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,6 @@
1616

1717
import operator
1818

19-
from numpy.core.numeric import normalize_axis_index, normalize_axis_tuple
20-
2119
import dpctl
2220
import dpctl.tensor as dpt
2321
import dpctl.tensor._tensor_elementwise_impl as tei
@@ -32,9 +30,11 @@
3230
)
3331
from dpctl.utils import ExecutionPlacementError, SequentialOrderManager
3432

33+
from ._numpy_helper import normalize_axis_index, normalize_axis_tuple
34+
3535

3636
def matrix_transpose(x):
37-
"""matrix_transpose(x)
37+
r"""matrix_transpose(x)
3838
3939
Transposes the innermost two dimensions of `x`, where `x` is a
4040
2-dimensional matrix or a stack of 2-dimensional matrices.
@@ -65,7 +65,7 @@ def matrix_transpose(x):
6565

6666

6767
def tensordot(x1, x2, axes=2):
68-
"""tensordot(x1, x2, axes=2)
68+
r"""tensordot(x1, x2, axes=2)
6969
7070
Returns a tensor contraction of `x1` and `x2` over specific axes.
7171
@@ -308,7 +308,7 @@ def tensordot(x1, x2, axes=2):
308308

309309

310310
def vecdot(x1, x2, axis=-1):
311-
"""vecdot(x1, x2, axis=-1)
311+
r"""vecdot(x1, x2, axis=-1)
312312
313313
Computes the (vector) dot product of two arrays.
314314
@@ -574,7 +574,7 @@ def vecdot(x1, x2, axis=-1):
574574

575575

576576
def matmul(x1, x2, out=None, dtype=None, order="K"):
577-
"""matmul(x1, x2, out=None, order="K")
577+
r"""matmul(x1, x2, out=None, order="K")
578578
579579
Computes the matrix product. Implements the same semantics
580580
as the built-in operator `@`.
@@ -721,7 +721,8 @@ def matmul(x1, x2, out=None, dtype=None, order="K"):
721721
buf1_dt = res_dt
722722
else:
723723
raise ValueError(
724-
f"`matmul` input `x1` cannot be cast from {x1_dtype} to "
724+
r"`matmul` input `x1` cannot be cast from "
725+
f"{x1_dtype} to "
725726
f"requested type {res_dt} according to the casting rule "
726727
"''same_kind''."
727728
)
@@ -730,7 +731,8 @@ def matmul(x1, x2, out=None, dtype=None, order="K"):
730731
buf2_dt = res_dt
731732
else:
732733
raise ValueError(
733-
f"`matmul` input `x2` cannot be cast from {x2_dtype} to "
734+
r"`matmul` input `x2` cannot be cast from "
735+
f"{x2_dtype} to "
734736
f"requested type {res_dt} according to the casting rule "
735737
"''same_kind''."
736738
)
@@ -762,7 +764,7 @@ def matmul(x1, x2, out=None, dtype=None, order="K"):
762764

763765
if res_dt != out.dtype:
764766
raise ValueError(
765-
f"Output array of type {res_dt} is needed," f"got {out.dtype}"
767+
f"Output array of type {res_dt} is needed, got {out.dtype}"
766768
)
767769

768770
if dpctl.utils.get_execution_queue((exec_q, out.sycl_queue)) is None:

dpctl/tensor/_manipulation_functions.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,14 @@
1919
import operator
2020

2121
import numpy as np
22-
from numpy.core.numeric import normalize_axis_index, normalize_axis_tuple
2322

2423
import dpctl
2524
import dpctl.tensor as dpt
2625
import dpctl.tensor._tensor_impl as ti
2726
import dpctl.utils as dputils
2827

2928
from ._copy_utils import _broadcast_strides
29+
from ._numpy_helper import normalize_axis_index, normalize_axis_tuple
3030
from ._type_utils import _supported_dtype, _to_device_supported_dtype
3131

3232
__doc__ = (

dpctl/tensor/_numpy_helper.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# Data Parallel Control (dpctl)
2+
#
3+
# Copyright 2020-2024 Intel Corporation
4+
#
5+
# Licensed under the Apache License, Version 2.0 (the "License");
6+
# you may not use this file except in compliance with the License.
7+
# You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing, software
12+
# distributed under the License is distributed on an "AS IS" BASIS,
13+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
# See the License for the specific language governing permissions and
15+
# limitations under the License.
16+
17+
import numpy as np
18+
19+
_npver = np.lib.NumpyVersion(np.__version__)
20+
21+
if _npver < "1.25.0":
22+
from numpy import AxisError
23+
else:
24+
from numpy.exceptions import AxisError
25+
26+
if _npver >= "2.0.0":
27+
from numpy._core.numeric import normalize_axis_index, normalize_axis_tuple
28+
else:
29+
from numpy.core.numeric import normalize_axis_index, normalize_axis_tuple
30+
31+
32+
__all__ = ["AxisError", "normalize_axis_index", "normalize_axis_tuple"]

dpctl/tensor/_reduction.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,13 @@
1414
# See the License for the specific language governing permissions and
1515
# limitations under the License.
1616

17-
from numpy.core.numeric import normalize_axis_tuple
18-
1917
import dpctl
2018
import dpctl.tensor as dpt
2119
import dpctl.tensor._tensor_impl as ti
2220
import dpctl.tensor._tensor_reductions_impl as tri
2321
from dpctl.utils import ExecutionPlacementError, SequentialOrderManager
2422

23+
from ._numpy_helper import normalize_axis_tuple
2524
from ._type_utils import (
2625
_default_accumulation_dtype,
2726
_default_accumulation_dtype_fp_types,
@@ -617,7 +616,9 @@ def _search_over_axis(x, axis, keepdims, out, _reduction_fn):
617616
axis = (axis,)
618617
else:
619618
raise TypeError(
620-
f"`axis` argument expected `int` or `None`, got {type(axis)}"
619+
f"'axis' argument expected to have type 'int' "
620+
r"or be `None`, "
621+
f"got type {type(axis)}"
621622
)
622623
axis = normalize_axis_tuple(axis, nd, "axis")
623624
perm = [i for i in range(nd) if i not in axis] + list(axis)

dpctl/tensor/_sorting.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,11 @@
1414
# See the License for the specific language governing permissions and
1515
# limitations under the License.
1616

17-
from numpy.core.numeric import normalize_axis_index
18-
1917
import dpctl.tensor as dpt
2018
import dpctl.tensor._tensor_impl as ti
2119
import dpctl.utils as du
2220

21+
from ._numpy_helper import normalize_axis_index
2322
from ._tensor_sorting_impl import (
2423
_argsort_ascending,
2524
_argsort_descending,

dpctl/tensor/_statistical_functions.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,14 @@
1414
# See the License for the specific language governing permissions and
1515
# limitations under the License.
1616

17-
from numpy.core.numeric import normalize_axis_tuple
18-
1917
import dpctl.tensor as dpt
2018
import dpctl.tensor._tensor_elementwise_impl as tei
2119
import dpctl.tensor._tensor_impl as ti
2220
import dpctl.tensor._tensor_reductions_impl as tri
2321
import dpctl.utils as du
2422

23+
from ._numpy_helper import normalize_axis_tuple
24+
2525

2626
def _var_impl(x, axis, correction, keepdims):
2727
nd = x.ndim

dpctl/tensor/_utility_functions.py

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,26 @@
1-
from numpy.core.numeric import normalize_axis_tuple
1+
# Data Parallel Control (dpctl)
2+
#
3+
# Copyright 2020-2024 Intel Corporation
4+
#
5+
# Licensed under the Apache License, Version 2.0 (the "License");
6+
# you may not use this file except in compliance with the License.
7+
# You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing, software
12+
# distributed under the License is distributed on an "AS IS" BASIS,
13+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
# See the License for the specific language governing permissions and
15+
# limitations under the License.
216

317
import dpctl.tensor as dpt
418
import dpctl.tensor._tensor_impl as ti
519
import dpctl.tensor._tensor_reductions_impl as tri
620
import dpctl.utils as du
721

22+
from ._numpy_helper import normalize_axis_tuple
23+
824

925
def _boolean_reduction(x, axis, keepdims, func):
1026
if not isinstance(x, dpt.usm_ndarray):

dpctl/tests/elementwise/test_log1p.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,4 +164,6 @@ def test_log1p_special_cases():
164164

165165
tol = dpt.finfo(X.dtype).resolution
166166
with np.errstate(invalid="ignore"):
167-
assert_allclose(dpt.asnumpy(dpt.log1p(X)), res, atol=tol, rtol=tol)
167+
dpt_res = dpt.asnumpy(dpt.log1p(X))
168+
assert_allclose(np.real(dpt_res), np.real(res), atol=tol, rtol=tol)
169+
assert_allclose(np.imag(dpt_res), np.imag(res), atol=tol, rtol=tol)

0 commit comments

Comments
 (0)