Skip to content

Commit

Permalink
[CODEMOD][caffe2] use npt.NDArray instead of np.ndarray in type annot…
Browse files Browse the repository at this point in the history
…ations (pytorch#136288)

Summary:
To facilitate PSS-2 upgrade, this uses `ndt.NDArray` instead of `nd.ndarray` in type annotations. In Numpy-1.19 (PSS-1) it's an alias to `nd.ndarray` -- a noop.
In Numpy-1.24, `ndt.NDArray` a proper generic type, and without this change uses of `nd.ndarray` generate this Pyre type error:
```counterexample
 Invalid type parameters [24]: Generic type `np.ndarray` expects 2 type parameters.
```

Test Plan: Sandcastle plus visual inspection

Differential Revision: D62977370

Pull Request resolved: pytorch#136288
Approved by: https://github.com/kit1980
  • Loading branch information
igorsugak authored and pytorchmergebot committed Sep 19, 2024
1 parent 908a568 commit bce52d0
Show file tree
Hide file tree
Showing 7 changed files with 21 additions and 15 deletions.
5 changes: 3 additions & 2 deletions benchmarks/dynamo/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
from unittest.mock import MagicMock

import numpy as np
import numpy.typing as npt
import pandas as pd
import psutil
import yaml
Expand Down Expand Up @@ -1565,14 +1566,14 @@ def format_pt_inputs(self, pt_inputs: Any) -> Sequence[torch.Tensor]:
def format_pt_outputs(self, pt_outputs: Any) -> Sequence[torch.Tensor]:
...

def adapt_pt_inputs_to_onnx(self, pt_inputs) -> Mapping[str, np.ndarray]:
def adapt_pt_inputs_to_onnx(self, pt_inputs) -> Mapping[str, npt.NDArray]:
pt_inputs = self.format_pt_inputs(pt_inputs)
return {
ort_input.name: pt_input.cpu().numpy()
for ort_input, pt_input in zip(self.onnx_session.get_inputs(), pt_inputs)
}

def adapt_onnx_outputs_to_pt(self, onnx_outputs: List[np.ndarray]) -> Any:
def adapt_onnx_outputs_to_pt(self, onnx_outputs: List[npt.NDArray]) -> Any:
pt_outputs = [
torch.from_numpy(onnx_output).to(current_device)
for onnx_output in onnx_outputs
Expand Down
3 changes: 2 additions & 1 deletion torch/ao/quantization/experimental/linear.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# mypy: allow-untyped-defs
import numpy as np
import numpy.typing as npt

import torch
from torch.ao.nn.quantized.modules.utils import WeightedQuantizedModule
Expand Down Expand Up @@ -148,7 +149,7 @@ def forward(self, activation: torch.Tensor) -> torch.FloatTensor:
weight_rows = self.weight_transposed.size()[0]
weight_cols = self.weight_transposed.size()[1]

decomposed_weight: np.ndarray = np.empty(
decomposed_weight: npt.NDArray = np.empty(
shape=(weight_rows, weight_cols), dtype=object
)
for row in range(weight_rows):
Expand Down
6 changes: 3 additions & 3 deletions torch/onnx/_internal/exporter/_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
if typing.TYPE_CHECKING:
import os

import numpy as np
import numpy.typing as npt


# Define utilities to convert PyTorch data types so users do not need to specify manually
Expand Down Expand Up @@ -100,7 +100,7 @@ def __init__(self, tensor: torch.Tensor, name: str | None = None):
tensor, dtype=_torch_dtype_to_onnx_dtype(tensor.dtype), name=name
)

def numpy(self) -> np.ndarray:
def numpy(self) -> npt.NDArray:
self.raw: torch.Tensor
if self.dtype == ir.DataType.BFLOAT16:
return self.raw.view(torch.uint16).numpy(force=True)
Expand All @@ -114,7 +114,7 @@ def numpy(self) -> np.ndarray:
return self.raw.view(torch.uint8).numpy(force=True)
return self.raw.numpy(force=True)

def __array__(self, dtype: Any = None, copy: bool | None = None) -> np.ndarray:
def __array__(self, dtype: Any = None, copy: bool | None = None) -> npt.NDArray:
del copy # Unused, but needed for the signature
if dtype is None:
return self.numpy()
Expand Down
3 changes: 2 additions & 1 deletion torch/onnx/verification.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
from typing import Any, Callable, Collection, Mapping, Sequence, Tuple, Union

import numpy as np
import numpy.typing as npt

import torch
import torch._C._onnx as _C_onnx
Expand Down Expand Up @@ -98,7 +99,7 @@ def _flatten_tuples(elem):


# TODO(justinchuby): Add type checking by narrowing down the return type when input is None
def _to_numpy(elem) -> list | np.ndarray:
def _to_numpy(elem) -> list | npt.NDArray:
if isinstance(elem, torch.Tensor):
if elem.requires_grad:
return elem.detach().cpu().numpy()
Expand Down
13 changes: 7 additions & 6 deletions torch/testing/_internal/common_methods_invocations.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

import torch
import numpy as np
import numpy.typing as npt
from torch import inf, nan

from typing import Any, Dict, List, Tuple, Union, Sequence
Expand Down Expand Up @@ -11342,7 +11343,7 @@ def _tanh_gelu_ref(X):
return _gelu_ref(X)


def reference_one_hot(a: np.ndarray, num_classes: int = -1) -> np.ndarray:
def reference_one_hot(a: npt.NDArray, num_classes: int = -1) -> npt.NDArray:
if num_classes == -1:
num_classes = int(np.amax(a) + 1)

Expand All @@ -11362,11 +11363,11 @@ def reference_mse_loss(input, target, reduction="mean"):
return se


def reference_layer_norm(inp: np.ndarray, normalized_shape: Tuple[int], weight=None, bias=None, eps=1e-5):
def reference_layer_norm(inp: npt.NDArray, normalized_shape: Tuple[int], weight=None, bias=None, eps=1e-5):
return reference_native_layer_norm(inp, normalized_shape, weight, bias, eps)[0]


def reference_native_layer_norm(inp: np.ndarray, normalized_shape: Tuple[int], weight, bias, eps):
def reference_native_layer_norm(inp: npt.NDArray, normalized_shape: Tuple[int], weight, bias, eps):
feature_size = np.prod(normalized_shape)
inp_view = inp.reshape(-1, feature_size) # type: ignore[call-overload]
mean = inp_view.mean(axis=-1, keepdims=True)
Expand All @@ -11383,7 +11384,7 @@ def reference_native_layer_norm(inp: np.ndarray, normalized_shape: Tuple[int], w
return Y.reshape(*inp.shape), mean.reshape(stat_shape), (1.0 / np.sqrt(var + eps)).reshape(stat_shape)


def reference_rms_norm(inp: np.ndarray, normalized_shape: Tuple[int], weight=None, eps=None):
def reference_rms_norm(inp: npt.NDArray, normalized_shape: Tuple[int], weight=None, eps=None):
if eps is None:
eps = torch.finfo(numpy_to_torch_dtype(inp.dtype)).eps
feature_size = np.prod(normalized_shape)
Expand All @@ -11395,7 +11396,7 @@ def reference_rms_norm(inp: np.ndarray, normalized_shape: Tuple[int], weight=Non
return Y.reshape(*inp.shape)


def reference_group_norm(inp: np.ndarray, num_groups: int, weight=None, bias=None, eps=1e-5):
def reference_group_norm(inp: npt.NDArray, num_groups: int, weight=None, bias=None, eps=1e-5):
inp_view = inp
if np.prod(inp.shape) != 0:
inp_view = inp.reshape((inp.shape[0], num_groups, -1))
Expand Down Expand Up @@ -11481,7 +11482,7 @@ def reference_std_var(f):
g = reference_reduction_numpy(f)

@wraps(g)
def wrapper(x: np.ndarray, *args, **kwargs):
def wrapper(x: npt.NDArray, *args, **kwargs):
assert not ('unbiased' in kwargs and 'correction' in kwargs)

if 'unbiased' in kwargs:
Expand Down
3 changes: 2 additions & 1 deletion torch/testing/_internal/opinfo/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from typing import Sequence

import numpy as np
import numpy.typing as npt

import torch
from torch.testing._internal.common_cuda import TEST_CUDA
Expand Down Expand Up @@ -206,7 +207,7 @@ def reference_reduction_numpy(f, supports_keepdims=True):
"""

@wraps(f)
def wrapper(x: np.ndarray, *args, **kwargs):
def wrapper(x: npt.NDArray, *args, **kwargs):
# Copy keys into a set
keys = set(kwargs.keys())

Expand Down
3 changes: 2 additions & 1 deletion torch/utils/tensorboard/_utils.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# mypy: allow-untyped-defs
import numpy as np
import numpy.typing as npt


# Functions for converting
Expand All @@ -21,7 +22,7 @@ def figure_to_image(figures, close=True):
def render_to_rgb(figure):
canvas = plt_backend_agg.FigureCanvasAgg(figure)
canvas.draw()
data: np.ndarray = np.frombuffer(canvas.buffer_rgba(), dtype=np.uint8)
data: npt.NDArray = np.frombuffer(canvas.buffer_rgba(), dtype=np.uint8)
w, h = figure.canvas.get_width_height()
image_hwc = data.reshape([h, w, 4])[:, :, 0:3]
image_chw = np.moveaxis(image_hwc, source=2, destination=0)
Expand Down

0 comments on commit bce52d0

Please sign in to comment.