diff --git a/pyproject.toml b/pyproject.toml index e31f25d6abc..c2887cdeddb 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -34,7 +34,7 @@ classifiers = [ dependencies = [ "networkx>=2.6, <=3.6.1", "ninja>=1.10.0.post2, <1.14", - "numpy>=1.24.0, <2.5.0", + "numpy>=1.24.0, <2.6.0", "openvino-telemetry>=2023.2.0", "packaging>=20.0", "psutil", diff --git a/src/nncf/quantization/algorithms/weight_compression/constants.py b/src/nncf/quantization/algorithms/weight_compression/constants.py index 449c1b67ff9..42d79bf7c80 100644 --- a/src/nncf/quantization/algorithms/weight_compression/constants.py +++ b/src/nncf/quantization/algorithms/weight_compression/constants.py @@ -103,7 +103,7 @@ ) -CENTER_OF_F4E2M1_QUANTILES = (F4E2M1_QUANTILES[1:] + F4E2M1_QUANTILES[:-1]) / 2 +CENTER_OF_F4E2M1_QUANTILES = (F4E2M1_QUANTILES[1:] + F4E2M1_QUANTILES[:-1]) / 2 # type: ignore[index] FP_MAX_VALUES = { diff --git a/src/nncf/tensor/functions/numpy_io.py b/src/nncf/tensor/functions/numpy_io.py index 10db7c4d172..677e3b82a6b 100644 --- a/src/nncf/tensor/functions/numpy_io.py +++ b/src/nncf/tensor/functions/numpy_io.py @@ -13,7 +13,6 @@ from typing import Any import numpy as np -from numpy.typing import NDArray from safetensors.numpy import load_file as np_load_file from safetensors.numpy import save_file as np_save_file @@ -22,7 +21,7 @@ from nncf.tensor.functions import io as io from nncf.tensor.functions.numpy_numeric import validate_device -T_NUMPY_ARRAY = NDArray[Any] +T_NUMPY_ARRAY = np.ndarray[Any, np.dtype[Any]] T_NUMPY = T_NUMPY_ARRAY | np.generic # type: ignore[type-arg] diff --git a/src/nncf/tensor/functions/numpy_linalg.py b/src/nncf/tensor/functions/numpy_linalg.py index dcc6f8919ae..a1561cdcb6c 100644 --- a/src/nncf/tensor/functions/numpy_linalg.py +++ b/src/nncf/tensor/functions/numpy_linalg.py @@ -12,13 +12,12 @@ from typing import Any, Literal import numpy as np -from numpy.typing import NDArray from scipy.linalg import lstsq from nncf.tensor.definitions import T_AXIS from nncf.tensor.functions import linalg -T_NUMPY_ARRAY = NDArray[Any] +T_NUMPY_ARRAY = np.ndarray[Any, np.dtype[Any]] @linalg.norm.register diff --git a/src/nncf/tensor/functions/numpy_numeric.py b/src/nncf/tensor/functions/numpy_numeric.py index 29567829622..ce215461921 100644 --- a/src/nncf/tensor/functions/numpy_numeric.py +++ b/src/nncf/tensor/functions/numpy_numeric.py @@ -12,7 +12,6 @@ from typing import Any, Callable, Literal, Sequence import numpy as np -from numpy.typing import NDArray from nncf.tensor.definitions import T_AXIS from nncf.tensor.definitions import T_NUMBER @@ -25,7 +24,7 @@ from nncf.tensor.functions import numeric as numeric from nncf.tensor.tensor import TTensor -T_NUMPY_ARRAY = NDArray[Any] +T_NUMPY_ARRAY = np.ndarray[Any, np.dtype[Any]] T_NUMPY = T_NUMPY_ARRAY | np.generic # type: ignore[type-arg] DTYPE_MAP: dict[TensorDataType, np.dtype] = { # type: ignore[type-arg] @@ -80,12 +79,12 @@ def _(a: T_NUMPY) -> T_NUMPY_ARRAY: @numeric.max.register def _(a: T_NUMPY, axis: T_AXIS = None, keepdims: bool = False) -> T_NUMPY_ARRAY: - return np.array(np.max(a, axis=axis, keepdims=keepdims)) + return np.array(np.max(a, axis=axis, keepdims=keepdims)) # type: ignore[call-overload] @numeric.min.register def _(a: T_NUMPY, axis: T_AXIS = None, keepdims: bool = False) -> T_NUMPY: - return np.array(np.min(a, axis=axis, keepdims=keepdims)) + return np.array(np.min(a, axis=axis, keepdims=keepdims)) # type: ignore[call-overload] @numeric.abs.register @@ -156,7 +155,7 @@ def _( *, range: tuple[float, float] | None = None, ) -> T_NUMPY: - return np.histogram(a=a, bins=bins, range=range)[0] + return np.histogram(a=a, bins=bins, range=range)[0] # type: ignore[index] @numeric.isempty.register @@ -172,7 +171,7 @@ def _( atol: float = 1e-08, equal_nan: bool = False, ) -> T_NUMPY_ARRAY: - return np.isclose(a, b, rtol=rtol, atol=atol, equal_nan=equal_nan) + return np.isclose(a, b, rtol=rtol, atol=atol, equal_nan=equal_nan) # type: ignore[return-value] @numeric.maximum.register @@ -244,7 +243,7 @@ def _( dtype: TensorDataType | None = None, ) -> T_NUMPY_ARRAY: np_dtype = convert_to_numpy_dtype(dtype) - return np.array(np.mean(a, axis=axis, keepdims=keepdims, dtype=np_dtype)) # type: ignore [arg-type] + return np.array(np.mean(a, axis=axis, keepdims=keepdims, dtype=np_dtype)) # type: ignore[call-overload] @numeric.median.register @@ -253,7 +252,7 @@ def _( axis: T_SHAPE | None = None, keepdims: bool = False, ) -> T_NUMPY_ARRAY: - return np.array(np.median(a, axis=axis, keepdims=keepdims)) # type: ignore [arg-type] + return np.array(np.median(a, axis=axis, keepdims=keepdims)) @numeric.floor.register @@ -263,7 +262,7 @@ def _(a: T_NUMPY) -> T_NUMPY: @numeric.round.register def _(a: T_NUMPY, decimals: int = 0) -> T_NUMPY_ARRAY: - return np.round(a, decimals=decimals) + return np.round(a, decimals=decimals) # type: ignore[return-value] @numeric.power.register @@ -334,7 +333,7 @@ def _(a: T_NUMPY) -> T_NUMBER: @numeric.sum.register def _(a: T_NUMPY, axis: T_AXIS = None, keepdims: bool = False) -> T_NUMPY_ARRAY: - return np.array(np.sum(a, axis=axis, keepdims=keepdims)) + return np.array(np.sum(a, axis=axis, keepdims=keepdims)) # type: ignore[call-overload] @numeric.cumsum.register @@ -354,7 +353,7 @@ def _( keepdims: bool = False, ddof: int = 0, ) -> T_NUMPY_ARRAY: - return np.array(np.var(a, axis=axis, keepdims=keepdims, ddof=ddof)) # type: ignore[arg-type] + return np.array(np.var(a, axis=axis, keepdims=keepdims, ddof=ddof)) # type: ignore[call-overload] @numeric.size.register @@ -409,7 +408,7 @@ def _( keepdims: bool = False, ) -> T_NUMPY_ARRAY: if mask is None: - return np.mean(x, axis=axis, keepdims=keepdims) + return np.mean(x, axis=axis, keepdims=keepdims) # type: ignore[call-overload] masked_x = np.ma.array(x, mask=mask) result = np.ma.mean(masked_x, axis=axis, keepdims=keepdims) if isinstance(result, np.ma.MaskedArray): @@ -422,7 +421,7 @@ def _(x: T_NUMPY_ARRAY, mask: T_NUMPY_ARRAY | None, axis: T_AXIS, keepdims: bool if mask is None: return np.median(x, axis=axis, keepdims=keepdims) masked_x = np.ma.array(x, mask=mask) - result = np.ma.median(masked_x, axis=axis, keepdims=keepdims) # type: ignore[no-untyped-call] + result = np.ma.median(masked_x, axis=axis, keepdims=keepdims) if isinstance(result, np.ma.MaskedArray): return result.data return result @@ -441,8 +440,8 @@ def _(a: T_NUMPY) -> T_NUMPY: @numeric.searchsorted.register def _( a: T_NUMPY_ARRAY, v: T_NUMPY_ARRAY, side: Literal["left", "right"] = "left", sorter: T_NUMPY_ARRAY | None = None -) -> T_NUMPY_ARRAY: - return np.searchsorted(a, v, side, sorter) +) -> T_NUMPY_ARRAY | float: + return np.searchsorted(a, v, side, sorter) # type: ignore[return-value] @numeric.as_numpy_tensor.register diff --git a/src/nncf/tensor/functions/openvino_numeric.py b/src/nncf/tensor/functions/openvino_numeric.py index de584a0cc02..515e91aeae6 100644 --- a/src/nncf/tensor/functions/openvino_numeric.py +++ b/src/nncf/tensor/functions/openvino_numeric.py @@ -10,8 +10,8 @@ # limitations under the License. from typing import Any +import numpy as np import openvino as ov # type: ignore -from numpy.typing import NDArray from nncf.tensor import Tensor from nncf.tensor import TensorDataType @@ -59,7 +59,7 @@ DTYPE_MAP_REV = {v: k for k, v in DTYPE_MAP.items()} -def from_numpy(a: NDArray[Any]) -> ov.Tensor: +def from_numpy(a: np.ndarray[Any, np.dtype[Any]]) -> ov.Tensor: """ Convert a numpy array to an OpenVINO tensor. @@ -104,7 +104,7 @@ def _(a: ov.Tensor, shape: int | tuple[int, ...]) -> ov.Tensor: @numeric.as_numpy_tensor.register -def _(a: ov.Tensor) -> NDArray[Any]: +def _(a: ov.Tensor) -> np.ndarray[Any, np.dtype[Any]]: # Cannot convert bfloat16, uint4, int4, nf4, f4e2m1, f8e8m0, f8e4m3, f8e5m2 to numpy directly a_dtype = DTYPE_MAP_REV[a.get_element_type()] if a_dtype in NATIVE_OV_CAST_DTYPES: diff --git a/src/nncf/tensor/functions/torch_numeric.py b/src/nncf/tensor/functions/torch_numeric.py index c95581180c1..dddd97f7beb 100644 --- a/src/nncf/tensor/functions/torch_numeric.py +++ b/src/nncf/tensor/functions/torch_numeric.py @@ -13,7 +13,6 @@ import numpy as np import torch -from numpy.typing import NDArray from nncf.tensor import TensorDataType from nncf.tensor import TensorDeviceType @@ -433,7 +432,7 @@ def _(x: torch.Tensor, mask: torch.Tensor | None, axis: T_AXIS, keepdims: bool = if not isinstance(axis, int): device = x.device np_masked_x = np.ma.array(x.detach().cpu().numpy(), mask=mask.detach().cpu().numpy()) - result = torch.tensor(np.ma.median(np_masked_x, axis=axis, keepdims=keepdims)) # type: ignore[no-untyped-call] + result = torch.tensor(np.ma.median(np_masked_x, axis=axis, keepdims=keepdims)) return result.type(x.dtype).to(device) pt_masked_x = x.masked_fill(mask, torch.nan) ret = torch.nanquantile(pt_masked_x, q=0.5, dim=axis, keepdim=keepdims) @@ -536,7 +535,7 @@ def arange( return torch.arange(start, end, step, dtype=pt_dtype, device=pt_device) -def from_numpy(ndarray: NDArray[Any]) -> torch.Tensor: +def from_numpy(ndarray: np.ndarray[Any, np.dtype[Any]]) -> torch.Tensor: return torch.from_numpy(ndarray) @@ -562,7 +561,7 @@ def tensor( @numeric.as_numpy_tensor.register -def _(a: torch.Tensor) -> NDArray[Any]: +def _(a: torch.Tensor) -> np.ndarray[Any, np.dtype[Any]]: return a.cpu().detach().numpy()