Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dpctl support for numpy 2.0 in runtime #1735

Merged
merged 4 commits into from
Jul 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,23 @@
# See https://pre-commit.com/hooks.html for more hooks
repos:
- repo: https://github.com/PyCQA/bandit
rev: '1.7.4'
rev: '1.7.9'
hooks:
- id: bandit
pass_filenames: false
args: ["-r", "dpctl", "-lll"]
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.3.0
rev: v4.6.0
hooks:
- id: end-of-file-fixer
- id: trailing-whitespace
- repo: https://github.com/psf/black
rev: 22.12.0
rev: 24.4.2
hooks:
- id: black
exclude: "versioneer.py|dpctl/_version.py"
- repo: https://github.com/pycqa/isort
rev: 5.12.0
rev: 5.13.2
hooks:
- id: isort
name: isort (python)
Expand All @@ -29,7 +29,7 @@ repos:
name: isort (pyi)
types: [pyi]
- repo: https://github.com/pycqa/flake8
rev: 5.0.4
rev: 7.1.0
hooks:
- id: flake8
- repo: https://github.com/pocc/pre-commit-hooks
Expand Down
4 changes: 2 additions & 2 deletions dpctl/tensor/_accumulation.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@
# See the License for the specific language governing permissions and
# limitations under the License.

from numpy.core.numeric import normalize_axis_index

import dpctl
import dpctl.tensor as dpt
import dpctl.tensor._tensor_accumulation_impl as tai
Expand All @@ -27,6 +25,8 @@
)
from dpctl.utils import ExecutionPlacementError, SequentialOrderManager

from ._numpy_helper import normalize_axis_index


def _accumulate_common(
x,
Expand Down
43 changes: 27 additions & 16 deletions dpctl/tensor/_copy_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
import operator

import numpy as np
from numpy.core.numeric import normalize_axis_index

import dpctl
import dpctl.memory as dpm
Expand All @@ -28,6 +27,8 @@
from dpctl.tensor._device import normalize_queue_device
from dpctl.tensor._type_utils import _dtype_supported_by_device_impl

from ._numpy_helper import normalize_axis_index

__doc__ = (
"Implementation module for copy- and cast- operations on "
":class:`dpctl.tensor.usm_ndarray`."
Expand Down Expand Up @@ -382,9 +383,11 @@ def _empty_like_orderK(X, dt, usm_type=None, dev=None):
if min(st) < 0:
st_sorted = [st[i] for i in perm]
sl = tuple(
slice(None, None, -1)
if st_sorted[i] < 0
else slice(None, None, None)
(
slice(None, None, -1)
if st_sorted[i] < 0
else slice(None, None, None)
)
for i in range(X.ndim)
)
R = R[sl]
Expand Down Expand Up @@ -435,9 +438,11 @@ def _empty_like_pair_orderK(X1, X2, dt, res_shape, usm_type, dev):
R = dpt.empty(sh_sorted, dtype=dt, usm_type=usm_type, device=dev, order="C")
if max(min(st1_sorted), min(st2_sorted)) < 0:
sl = tuple(
slice(None, None, -1)
if (st1_sorted[i] < 0 and st2_sorted[i] < 0)
else slice(None, None, None)
(
slice(None, None, -1)
if (st1_sorted[i] < 0 and st2_sorted[i] < 0)
else slice(None, None, None)
)
for i in range(nd1)
)
R = R[sl]
Expand Down Expand Up @@ -503,9 +508,15 @@ def _empty_like_triple_orderK(X1, X2, X3, dt, res_shape, usm_type, dev):
R = dpt.empty(sh_sorted, dtype=dt, usm_type=usm_type, device=dev, order="C")
if max(min(st1_sorted), min(st2_sorted), min(st3_sorted)) < 0:
sl = tuple(
slice(None, None, -1)
if (st1_sorted[i] < 0 and st2_sorted[i] < 0 and st3_sorted[i] < 0)
else slice(None, None, None)
(
slice(None, None, -1)
if (
st1_sorted[i] < 0
and st2_sorted[i] < 0
and st3_sorted[i] < 0
)
else slice(None, None, None)
)
for i in range(nd1)
)
R = R[sl]
Expand Down Expand Up @@ -826,9 +837,9 @@ def _take_multi_index(ary, inds, p):
)
inds = tuple(
map(
lambda ind: ind
if ind.dtype == ind_dt
else dpt.astype(ind, ind_dt),
lambda ind: (
ind if ind.dtype == ind_dt else dpt.astype(ind, ind_dt)
),
inds,
)
)
Expand Down Expand Up @@ -975,9 +986,9 @@ def _put_multi_index(ary, inds, p, vals):
)
inds = tuple(
map(
lambda ind: ind
if ind.dtype == ind_dt
else dpt.astype(ind, ind_dt),
lambda ind: (
ind if ind.dtype == ind_dt else dpt.astype(ind, ind_dt)
),
inds,
)
)
Expand Down
3 changes: 1 addition & 2 deletions dpctl/tensor/_indexing_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,13 @@

import operator

from numpy.core.numeric import normalize_axis_index

import dpctl
import dpctl.tensor as dpt
import dpctl.tensor._tensor_impl as ti
import dpctl.utils

from ._copy_utils import _extract_impl, _nonzero_impl
from ._numpy_helper import normalize_axis_index


def _get_indexing_mode(name):
Expand Down
20 changes: 11 additions & 9 deletions dpctl/tensor/_linear_algebra_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@

import operator

from numpy.core.numeric import normalize_axis_index, normalize_axis_tuple

import dpctl
import dpctl.tensor as dpt
import dpctl.tensor._tensor_elementwise_impl as tei
Expand All @@ -32,9 +30,11 @@
)
from dpctl.utils import ExecutionPlacementError, SequentialOrderManager

from ._numpy_helper import normalize_axis_index, normalize_axis_tuple


def matrix_transpose(x):
"""matrix_transpose(x)
r"""matrix_transpose(x)

Transposes the innermost two dimensions of `x`, where `x` is a
2-dimensional matrix or a stack of 2-dimensional matrices.
Expand Down Expand Up @@ -65,7 +65,7 @@ def matrix_transpose(x):


def tensordot(x1, x2, axes=2):
"""tensordot(x1, x2, axes=2)
r"""tensordot(x1, x2, axes=2)

Returns a tensor contraction of `x1` and `x2` over specific axes.

Expand Down Expand Up @@ -308,7 +308,7 @@ def tensordot(x1, x2, axes=2):


def vecdot(x1, x2, axis=-1):
"""vecdot(x1, x2, axis=-1)
r"""vecdot(x1, x2, axis=-1)

Computes the (vector) dot product of two arrays.

Expand Down Expand Up @@ -574,7 +574,7 @@ def vecdot(x1, x2, axis=-1):


def matmul(x1, x2, out=None, dtype=None, order="K"):
"""matmul(x1, x2, out=None, order="K")
r"""matmul(x1, x2, out=None, order="K")

Computes the matrix product. Implements the same semantics
as the built-in operator `@`.
Expand Down Expand Up @@ -721,7 +721,8 @@ def matmul(x1, x2, out=None, dtype=None, order="K"):
buf1_dt = res_dt
else:
raise ValueError(
f"`matmul` input `x1` cannot be cast from {x1_dtype} to "
r"`matmul` input `x1` cannot be cast from "
f"{x1_dtype} to "
f"requested type {res_dt} according to the casting rule "
"''same_kind''."
)
Expand All @@ -730,7 +731,8 @@ def matmul(x1, x2, out=None, dtype=None, order="K"):
buf2_dt = res_dt
else:
raise ValueError(
f"`matmul` input `x2` cannot be cast from {x2_dtype} to "
r"`matmul` input `x2` cannot be cast from "
f"{x2_dtype} to "
f"requested type {res_dt} according to the casting rule "
"''same_kind''."
)
Expand Down Expand Up @@ -762,7 +764,7 @@ def matmul(x1, x2, out=None, dtype=None, order="K"):

if res_dt != out.dtype:
raise ValueError(
f"Output array of type {res_dt} is needed," f"got {out.dtype}"
f"Output array of type {res_dt} is needed, got {out.dtype}"
)

if dpctl.utils.get_execution_queue((exec_q, out.sycl_queue)) is None:
Expand Down
2 changes: 1 addition & 1 deletion dpctl/tensor/_manipulation_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,14 @@
import operator

import numpy as np
from numpy.core.numeric import normalize_axis_index, normalize_axis_tuple

import dpctl
import dpctl.tensor as dpt
import dpctl.tensor._tensor_impl as ti
import dpctl.utils as dputils

from ._copy_utils import _broadcast_strides
from ._numpy_helper import normalize_axis_index, normalize_axis_tuple
from ._type_utils import _supported_dtype, _to_device_supported_dtype

__doc__ = (
Expand Down
32 changes: 32 additions & 0 deletions dpctl/tensor/_numpy_helper.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# Data Parallel Control (dpctl)
#
# Copyright 2020-2024 Intel Corporation
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import numpy as np

_npver = np.lib.NumpyVersion(np.__version__)

if _npver < "1.25.0":
from numpy import AxisError
else:
from numpy.exceptions import AxisError

if _npver >= "2.0.0":
from numpy._core.numeric import normalize_axis_index, normalize_axis_tuple
else:
from numpy.core.numeric import normalize_axis_index, normalize_axis_tuple


__all__ = ["AxisError", "normalize_axis_index", "normalize_axis_tuple"]
7 changes: 4 additions & 3 deletions dpctl/tensor/_reduction.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,13 @@
# See the License for the specific language governing permissions and
# limitations under the License.

from numpy.core.numeric import normalize_axis_tuple

import dpctl
import dpctl.tensor as dpt
import dpctl.tensor._tensor_impl as ti
import dpctl.tensor._tensor_reductions_impl as tri
from dpctl.utils import ExecutionPlacementError, SequentialOrderManager

from ._numpy_helper import normalize_axis_tuple
from ._type_utils import (
_default_accumulation_dtype,
_default_accumulation_dtype_fp_types,
Expand Down Expand Up @@ -617,7 +616,9 @@ def _search_over_axis(x, axis, keepdims, out, _reduction_fn):
axis = (axis,)
else:
raise TypeError(
f"`axis` argument expected `int` or `None`, got {type(axis)}"
f"'axis' argument expected to have type 'int' "
r"or be `None`, "
f"got type {type(axis)}"
)
axis = normalize_axis_tuple(axis, nd, "axis")
perm = [i for i in range(nd) if i not in axis] + list(axis)
Expand Down
3 changes: 1 addition & 2 deletions dpctl/tensor/_sorting.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,11 @@
# See the License for the specific language governing permissions and
# limitations under the License.

from numpy.core.numeric import normalize_axis_index

import dpctl.tensor as dpt
import dpctl.tensor._tensor_impl as ti
import dpctl.utils as du

from ._numpy_helper import normalize_axis_index
from ._tensor_sorting_impl import (
_argsort_ascending,
_argsort_descending,
Expand Down
4 changes: 2 additions & 2 deletions dpctl/tensor/_statistical_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,14 @@
# See the License for the specific language governing permissions and
# limitations under the License.

from numpy.core.numeric import normalize_axis_tuple

import dpctl.tensor as dpt
import dpctl.tensor._tensor_elementwise_impl as tei
import dpctl.tensor._tensor_impl as ti
import dpctl.tensor._tensor_reductions_impl as tri
import dpctl.utils as du

from ._numpy_helper import normalize_axis_tuple


def _var_impl(x, axis, correction, keepdims):
nd = x.ndim
Expand Down
18 changes: 17 additions & 1 deletion dpctl/tensor/_utility_functions.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,26 @@
from numpy.core.numeric import normalize_axis_tuple
# Data Parallel Control (dpctl)
#
# Copyright 2020-2024 Intel Corporation
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import dpctl.tensor as dpt
import dpctl.tensor._tensor_impl as ti
import dpctl.tensor._tensor_reductions_impl as tri
import dpctl.utils as du

from ._numpy_helper import normalize_axis_tuple


def _boolean_reduction(x, axis, keepdims, func):
if not isinstance(x, dpt.usm_ndarray):
Expand Down
4 changes: 3 additions & 1 deletion dpctl/tests/elementwise/test_log1p.py
Original file line number Diff line number Diff line change
Expand Up @@ -164,4 +164,6 @@ def test_log1p_special_cases():

tol = dpt.finfo(X.dtype).resolution
with np.errstate(invalid="ignore"):
assert_allclose(dpt.asnumpy(dpt.log1p(X)), res, atol=tol, rtol=tol)
dpt_res = dpt.asnumpy(dpt.log1p(X))
assert_allclose(np.real(dpt_res), np.real(res), atol=tol, rtol=tol)
assert_allclose(np.imag(dpt_res), np.imag(res), atol=tol, rtol=tol)
Loading
Loading