Skip to content
Open
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
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down
3 changes: 1 addition & 2 deletions src/nncf/tensor/functions/numpy_io.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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]


Expand Down
3 changes: 1 addition & 2 deletions src/nncf/tensor/functions/numpy_linalg.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
29 changes: 14 additions & 15 deletions src/nncf/tensor/functions/numpy_numeric.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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]
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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):
Expand All @@ -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
Expand All @@ -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]
Comment thread
AlexanderDokuchaev marked this conversation as resolved.


@numeric.as_numpy_tensor.register
Expand Down
6 changes: 3 additions & 3 deletions src/nncf/tensor/functions/openvino_numeric.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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.

Expand Down Expand Up @@ -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:
Expand Down
7 changes: 3 additions & 4 deletions src/nncf/tensor/functions/torch_numeric.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)


Expand All @@ -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()


Expand Down
Loading