提交 65af9cfc 编写于 作者: M Megvii Engine Team

refactor(mge): use lower case for default string parameters in functional and module

GitOrigin-RevId: dbc1f27ff722c8006962a0440f824cec02057ab6
上级 d0aa9b41
...@@ -65,7 +65,7 @@ def _elwise(*args, mode): ...@@ -65,7 +65,7 @@ def _elwise(*args, mode):
def _matmul(inp1, inp2): def _matmul(inp1, inp2):
op = builtin.MatrixMul( op = builtin.MatrixMul(
transposeA=False, transposeB=False, compute_mode="DEFAULT", format="DEFAULT" transposeA=False, transposeB=False, compute_mode="default", format="default"
) )
inp1, inp2 = utils.convert_inputs(inp1, inp2) inp1, inp2 = utils.convert_inputs(inp1, inp2)
(result,) = apply(op, inp1, inp2) (result,) = apply(op, inp1, inp2)
...@@ -178,7 +178,7 @@ def _reduce(mode): ...@@ -178,7 +178,7 @@ def _reduce(mode):
def f(self, axis=None, keepdims: bool = False): def f(self, axis=None, keepdims: bool = False):
data = self data = self
(data,) = utils.convert_inputs(data) (data,) = utils.convert_inputs(data)
if mode == "MEAN": if mode == "mean":
data = data.astype("float32") data = data.astype("float32")
elif self.dtype == np.bool_: elif self.dtype == np.bool_:
data = data.astype("int32") data = data.astype("int32")
...@@ -204,7 +204,7 @@ def _reduce(mode): ...@@ -204,7 +204,7 @@ def _reduce(mode):
if not keepdims: if not keepdims:
result = _remove_axis(result, axis) result = _remove_axis(result, axis)
if self.dtype == np.bool_: if self.dtype == np.bool_:
if mode in ["MIN", "MAX"]: if mode in ["min", "max"]:
result = result.astype("bool") result = result.astype("bool")
if axis is None or self.ndim == 1: if axis is None or self.ndim == 1:
setscalar(result) setscalar(result)
...@@ -479,7 +479,7 @@ class ArrayMethodMixin(abc.ABC): ...@@ -479,7 +479,7 @@ class ArrayMethodMixin(abc.ABC):
10.0 10.0
""" """
return _reduce("SUM")(self, axis, keepdims) return _reduce("sum")(self, axis, keepdims)
def prod(self, axis=None, keepdims: bool = False): def prod(self, axis=None, keepdims: bool = False):
r""" r"""
...@@ -512,7 +512,7 @@ class ArrayMethodMixin(abc.ABC): ...@@ -512,7 +512,7 @@ class ArrayMethodMixin(abc.ABC):
24.0 24.0
""" """
return _reduce("PRODUCT")(self, axis, keepdims) return _reduce("product")(self, axis, keepdims)
def min(self, axis=None, keepdims: bool = False): def min(self, axis=None, keepdims: bool = False):
r""" r"""
...@@ -545,7 +545,7 @@ class ArrayMethodMixin(abc.ABC): ...@@ -545,7 +545,7 @@ class ArrayMethodMixin(abc.ABC):
1.0 1.0
""" """
return _reduce("MIN")(self, axis, keepdims) return _reduce("min")(self, axis, keepdims)
def max(self, axis=None, keepdims: bool = False): def max(self, axis=None, keepdims: bool = False):
r""" r"""
...@@ -578,7 +578,7 @@ class ArrayMethodMixin(abc.ABC): ...@@ -578,7 +578,7 @@ class ArrayMethodMixin(abc.ABC):
4.0 4.0
""" """
return _reduce("MAX")(self, axis, keepdims) return _reduce("max")(self, axis, keepdims)
def mean(self, axis=None, keepdims: bool = False): def mean(self, axis=None, keepdims: bool = False):
r""" r"""
...@@ -611,4 +611,4 @@ class ArrayMethodMixin(abc.ABC): ...@@ -611,4 +611,4 @@ class ArrayMethodMixin(abc.ABC):
2.5 2.5
""" """
return _reduce("MEAN")(self, axis, keepdims) return _reduce("mean")(self, axis, keepdims)
...@@ -267,6 +267,7 @@ def hinge_loss(pred: Tensor, label: Tensor, norm: str = "L1") -> Tensor: ...@@ -267,6 +267,7 @@ def hinge_loss(pred: Tensor, label: Tensor, norm: str = "L1") -> Tensor:
1.5 1.5
""" """
norm = norm.upper()
assert norm in ["L1", "L2"], "norm must be L1 or L2" assert norm in ["L1", "L2"], "norm must be L1 or L2"
# Converts binary labels to -1/1 labels. # Converts binary labels to -1/1 labels.
loss = relu(1.0 - pred * label) loss = relu(1.0 - pred * label)
......
...@@ -604,9 +604,9 @@ def argsort(inp: Tensor, descending: bool = False) -> Tensor: ...@@ -604,9 +604,9 @@ def argsort(inp: Tensor, descending: bool = False) -> Tensor:
""" """
assert len(inp.shape) <= 2, "Input should be 1d or 2d" assert len(inp.shape) <= 2, "Input should be 1d or 2d"
if descending: if descending:
order = "DESCENDING" order = "descending"
else: else:
order = "ASCENDING" order = "ascending"
op = builtin.Argsort(order=order) op = builtin.Argsort(order=order)
if len(inp.shape) == 1: if len(inp.shape) == 1:
...@@ -646,9 +646,9 @@ def sort(inp: Tensor, descending: bool = False) -> Tuple[Tensor, Tensor]: ...@@ -646,9 +646,9 @@ def sort(inp: Tensor, descending: bool = False) -> Tuple[Tensor, Tensor]:
""" """
assert len(inp.shape) <= 2, "Input should be 1d or 2d" assert len(inp.shape) <= 2, "Input should be 1d or 2d"
if descending: if descending:
order = "DESCENDING" order = "descending"
else: else:
order = "ASCENDING" order = "ascending"
op = builtin.Argsort(order=order) op = builtin.Argsort(order=order)
if len(inp.shape) == 1: if len(inp.shape) == 1:
...@@ -699,11 +699,11 @@ def topk( ...@@ -699,11 +699,11 @@ def topk(
inp = -inp inp = -inp
if kth_only: if kth_only:
mode = "KTH_ONLY" mode = "kth_only"
elif no_sort: elif no_sort:
mode = "VALUE_IDX_NOSORT" mode = "value_idx_nosort"
else: else:
mode = "VALUE_IDX_SORTED" mode = "value_idx_sorted"
op = builtin.TopK(mode=mode) op = builtin.TopK(mode=mode)
if not isinstance(k, Tensor): if not isinstance(k, Tensor):
...@@ -765,8 +765,8 @@ def matmul( ...@@ -765,8 +765,8 @@ def matmul(
inp2: Tensor, inp2: Tensor,
transpose_a=False, transpose_a=False,
transpose_b=False, transpose_b=False,
compute_mode="DEFAULT", compute_mode="default",
format="DEFAULT", format="default",
) -> Tensor: ) -> Tensor:
""" """
Performs a matrix multiplication of the matrices ``inp1`` and ``inp2``. Performs a matrix multiplication of the matrices ``inp1`` and ``inp2``.
...@@ -776,7 +776,9 @@ def matmul( ...@@ -776,7 +776,9 @@ def matmul(
- Both 1-D tensor, simply forward to ``dot``. - Both 1-D tensor, simply forward to ``dot``.
- Both 2-D tensor, normal matrix multiplication. - Both 2-D tensor, normal matrix multiplication.
- If one input tensor is 1-D, matrix vector multiplication. - If one input tensor is 1-D, matrix vector multiplication.
- If at least one tensor are 3-dimensional or >3-dimensional, the other tensor should have dim >= 2, the batched matrix-matrix is returned, and the tensor with smaller dimension will be broadcasted. For example: - If at least one tensor are 3-dimensional or >3-dimensional, the other tensor should have dim >= 2,
the batched matrix-matrix is returned, and the tensor with smaller dimension will be broadcasted.
For example:
- inp1: `(n, k, m)`, inp2: `(n, m, p)`, return: `(n, k, p)` - inp1: `(n, k, m)`, inp2: `(n, m, p)`, return: `(n, k, p)`
- inp1: `(n, k, m)`, inp2: `(m, p)`, return: `(n, k, p)` - inp1: `(n, k, m)`, inp2: `(m, p)`, return: `(n, k, p)`
......
...@@ -24,9 +24,9 @@ def conv_bias_activation( ...@@ -24,9 +24,9 @@ def conv_bias_activation(
padding: Union[int, Tuple[int, int]] = 0, padding: Union[int, Tuple[int, int]] = 0,
dilation: Union[int, Tuple[int, int]] = 1, dilation: Union[int, Tuple[int, int]] = 1,
groups: int = 1, groups: int = 1,
nonlinear_mode="IDENTITY", nonlinear_mode="identity",
conv_mode="CROSS_CORRELATION", conv_mode="cross_correlation",
compute_mode="DEFAULT", compute_mode="default",
) -> Tensor: ) -> Tensor:
""" """
Convolution bias with activation operation, only for inference. Convolution bias with activation operation, only for inference.
...@@ -35,27 +35,30 @@ def conv_bias_activation( ...@@ -35,27 +35,30 @@ def conv_bias_activation(
:param weight: convolution kernel. :param weight: convolution kernel.
:param bias: bias added to the result of convolution :param bias: bias added to the result of convolution
:param stride: stride of the 2D convolution operation. Default: 1 :param stride: stride of the 2D convolution operation. Default: 1
:param padding: size of the paddings added to the input on both sides of its spatial dimensions. Only zero-padding is supported. Default: 0 :param padding: size of the paddings added to the input on both sides
of its spatial dimensions. Only zero-padding is supported. Default: 0
:param dilation: dilation of the 2D convolution operation. Default: 1 :param dilation: dilation of the 2D convolution operation. Default: 1
:param groups: number of groups into which the input and output channels are divided, so as to perform a "grouped convolution". When ``groups`` is not 1, :param groups: number of groups into which the input and output channels are divided,
so as to perform a "grouped convolution". When ``groups`` is not 1,
``in_channels`` and ``out_channels`` must be divisible by ``groups``, ``in_channels`` and ``out_channels`` must be divisible by ``groups``,
and the shape of weight should be `(groups, out_channel // groups, and the shape of weight should be `(groups, out_channel // groups,
in_channels // groups, height, width)`. in_channels // groups, height, width)`.
:type conv_mode: string or :class:`Convolution.Mode`. :type conv_mode: string or :class:`Convolution.Mode`.
:param conv_mode: supports 'CROSS_CORRELATION' or 'CONVOLUTION'. Default: :param conv_mode: supports 'cross_correlation' or 'convolution'. Default:
'CROSS_CORRELATION' 'cross_correlation'
:param dtype: support for ``np.dtype``, Default: np.int8 :param dtype: support for ``np.dtype``, Default: np.int8
:type compute_mode: string or :type compute_mode: string or
:class:`Convolution.ComputeMode`. :class:`Convolution.ComputeMode`.
:param compute_mode: when set to "DEFAULT", no special requirements will be :param compute_mode: when set to "default", no special requirements will be
placed on the precision of intermediate results. When set to "FLOAT32", placed on the precision of intermediate results. When set to "float32",
"Float32" would be used for accumulator and intermediate result, but only effective when input and output are of Float16 dtype. "float32" would be used for accumulator and intermediate result,
but only effective when input and output are of float16 dtype.
""" """
ph, pw = _pair(padding) ph, pw = _pair(padding)
sh, sw = _pair_nonzero(stride) sh, sw = _pair_nonzero(stride)
dh, dw = _pair_nonzero(dilation) dh, dw = _pair_nonzero(dilation)
sparse_type = "DENSE" if groups == 1 else "GROUP" sparse_type = "dense" if groups == 1 else "group"
op = builtin.ConvBias( op = builtin.ConvBias(
stride_h=sh, stride_h=sh,
stride_w=sw, stride_w=sw,
...@@ -84,9 +87,9 @@ def batch_conv_bias_activation( ...@@ -84,9 +87,9 @@ def batch_conv_bias_activation(
padding: Union[int, Tuple[int, int]] = 0, padding: Union[int, Tuple[int, int]] = 0,
dilation: Union[int, Tuple[int, int]] = 1, dilation: Union[int, Tuple[int, int]] = 1,
groups: int = 1, groups: int = 1,
nonlinear_mode="IDENTITY", nonlinear_mode="identity",
conv_mode="CROSS_CORRELATION", conv_mode="cross_correlation",
compute_mode="DEFAULT", compute_mode="default",
) -> Tensor: ) -> Tensor:
""" """
Batch convolution bias with activation operation, only for inference. Batch convolution bias with activation operation, only for inference.
...@@ -95,27 +98,30 @@ def batch_conv_bias_activation( ...@@ -95,27 +98,30 @@ def batch_conv_bias_activation(
:param weight: convolution kernel in batched way. :param weight: convolution kernel in batched way.
:param bias: bias added to the result of convolution :param bias: bias added to the result of convolution
:param stride: stride of the 2D convolution operation. Default: 1 :param stride: stride of the 2D convolution operation. Default: 1
:param padding: size of the paddings added to the input on both sides of its spatial dimensions. Only zero-padding is supported. Default: 0 :param padding: size of the paddings added to the input on both sides
of its spatial dimensions. Only zero-padding is supported. Default: 0
:param dilation: dilation of the 2D convolution operation. Default: 1 :param dilation: dilation of the 2D convolution operation. Default: 1
:param groups: number of groups into which the input and output channels are divided, so as to perform a "grouped convolution". When ``groups`` is not 1, :param groups: number of groups into which the input and output channels are divided,
so as to perform a "grouped convolution". When ``groups`` is not 1,
``in_channels`` and ``out_channels`` must be divisible by ``groups``, ``in_channels`` and ``out_channels`` must be divisible by ``groups``,
and the shape of weight should be `(groups, out_channel // groups, and the shape of weight should be `(groups, out_channel // groups,
in_channels // groups, height, width)`. in_channels // groups, height, width)`.
:type conv_mode: string or :class:`Convolution.Mode`. :type conv_mode: string or :class:`Convolution.Mode`.
:param conv_mode: supports 'CROSS_CORRELATION' or 'CONVOLUTION'. Default: :param conv_mode: supports 'cross_correlation' or 'convolution'. Default:
'CROSS_CORRELATION' 'cross_correlation'
:param dtype: support for ``np.dtype``, Default: np.int8 :param dtype: support for ``np.dtype``, Default: np.int8
:type compute_mode: string or :type compute_mode: string or
:class:`Convolution.ComputeMode`. :class:`Convolution.ComputeMode`.
:param compute_mode: when set to "DEFAULT", no special requirements will be :param compute_mode: when set to "default", no special requirements will be
placed on the precision of intermediate results. When set to "FLOAT32", placed on the precision of intermediate results. When set to "float32",
"Float32" would be used for accumulator and intermediate result, but only effective when input and output are of Float16 dtype. "float32" would be used for accumulator and intermediate result,
but only effective when input and output are of float16 dtype.
""" """
ph, pw = _pair(padding) ph, pw = _pair(padding)
sh, sw = _pair_nonzero(stride) sh, sw = _pair_nonzero(stride)
dh, dw = _pair_nonzero(dilation) dh, dw = _pair_nonzero(dilation)
sparse_type = "DENSE" if groups == 1 else "GROUP" sparse_type = "dense" if groups == 1 else "group"
op = builtin.BatchConvBias( op = builtin.BatchConvBias(
stride_h=sh, stride_h=sh,
stride_w=sw, stride_w=sw,
......
...@@ -335,12 +335,8 @@ def split(inp, nsplits_or_sections, axis=0): ...@@ -335,12 +335,8 @@ def split(inp, nsplits_or_sections, axis=0):
y = F.split(x, 3) y = F.split(x, 3)
z = F.split(x, [6, 17], axis=1) z = F.split(x, [6, 17], axis=1)
if os.environ.get("MEGENGINE_USE_SYMBOLIC_SHAPE"): print([i.numpy().shape for i in y])
print([tuple(i.shape.numpy().tolist()) for i in y]) print([i.numpy().shape for i in z])
print([tuple(i.shape.numpy().tolist()) for i in z])
else:
print([i.shape for i in y])
print([i.shape for i in z])
Outputs: Outputs:
......
...@@ -46,6 +46,7 @@ def cvt_color(inp: Tensor, mode: str = ""): ...@@ -46,6 +46,7 @@ def cvt_color(inp: Tensor, mode: str = ""):
[[[[0.86555195]]]] [[[[0.86555195]]]]
""" """
mode = mode.upper()
assert mode in builtin.CvtColor.Mode.__dict__, "unspport mode for cvt_color" assert mode in builtin.CvtColor.Mode.__dict__, "unspport mode for cvt_color"
mode = getattr(builtin.CvtColor.Mode, mode) mode = getattr(builtin.CvtColor.Mode, mode)
assert isinstance(mode, builtin.CvtColor.Mode) assert isinstance(mode, builtin.CvtColor.Mode)
...@@ -92,9 +93,8 @@ def roi_pooling( ...@@ -92,9 +93,8 @@ def roi_pooling(
[[[-0.1383 -0.1383] [[[-0.1383 -0.1383]
[-0.5035 -0.5035]]] [-0.5035 -0.5035]]]
""" """
assert mode in ["max", "average"], "only max/average mode is supported" assert mode.lower() in ["max", "average"], "only max/average mode is supported"
if isinstance(output_shape, int): if isinstance(output_shape, int):
output_shape = (output_shape, output_shape) output_shape = (output_shape, output_shape)
...@@ -151,6 +151,7 @@ def roi_align( ...@@ -151,6 +151,7 @@ def roi_align(
[0.1359 0.1359]]] [0.1359 0.1359]]]
""" """
mode = mode.lower()
assert mode in ["max", "average"], "only max/average mode is supported" assert mode in ["max", "average"], "only max/average mode is supported"
if isinstance(output_shape, int): if isinstance(output_shape, int):
output_shape = (output_shape, output_shape) output_shape = (output_shape, output_shape)
...@@ -244,9 +245,9 @@ def nms( ...@@ -244,9 +245,9 @@ def nms(
def remap( def remap(
inp: Tensor, inp: Tensor,
map_xy: Tensor, map_xy: Tensor,
border_mode: str = "REPLICATE", border_mode: str = "replicate",
scalar: float = 0.0, scalar: float = 0.0,
interp_mode: str = "LINEAR", interp_mode: str = "linear",
) -> Tensor: ) -> Tensor:
r""" r"""
Applies remap transformation to batched 2D images. Applies remap transformation to batched 2D images.
...@@ -257,11 +258,11 @@ def remap( ...@@ -257,11 +258,11 @@ def remap(
:param inp: input image :param inp: input image
:param map_xy: (batch, oh, ow, 2) transformation matrix :param map_xy: (batch, oh, ow, 2) transformation matrix
:param border_mode: pixel extrapolation method. :param border_mode: pixel extrapolation method.
Default: "REPLICATE". Currently also support "CONSTANT", "REFLECT", Default: "replicate". Currently also support "constant", "reflect",
"REFLECT_101", "WRAP". "reflect_101", "wrap".
:param scalar: value used in case of a constant border. Default: 0 :param scalar: value used in case of a constant border. Default: 0
:param interp_mode: interpolation methods. :param interp_mode: interpolation methods.
Default: "LINEAR". Currently only support "LINEAR" mode. Default: "linear". Currently only support "linear" mode.
:return: output tensor. :return: output tensor.
Examples: Examples:
...@@ -301,10 +302,10 @@ def warp_affine( ...@@ -301,10 +302,10 @@ def warp_affine(
inp: Tensor, inp: Tensor,
weight: Tensor, weight: Tensor,
out_shape, out_shape,
border_mode="REPLICATE", border_mode="replicate",
border_val=0, border_val=0,
format="NHWC", format="NHWC",
imode="LINEAR", imode="linear",
): ):
""" """
Batched affine transform on 2D images. Batched affine transform on 2D images.
...@@ -313,13 +314,13 @@ def warp_affine( ...@@ -313,13 +314,13 @@ def warp_affine(
:param weight: weight tensor. :param weight: weight tensor.
:param out_shape: output tensor shape. :param out_shape: output tensor shape.
:param border_mode: pixel extrapolation method. :param border_mode: pixel extrapolation method.
Default: "WRAP". Currently "CONSTANT", "REFLECT", Default: "wrap". Currently "constant", "reflect",
"REFLECT_101", "ISOLATED", "WRAP", "REPLICATE", "TRANSPARENT" are supported. "reflect_101", "isolated", "wrap", "replicate", "transparent" are supported.
:param border_val: value used in case of a constant border. Default: 0 :param border_val: value used in case of a constant border. Default: 0
:param format: "NHWC" as default based on historical concerns, :param format: "NHWC" as default based on historical concerns,
"NCHW" is also supported. Default: "NCHW". "NCHW" is also supported. Default: "NHWC".
:param imode: interpolation methods. Could be "LINEAR", "NEAREST", "CUBIC", "AREA". :param imode: interpolation methods. Could be "linear", "nearest", "cubic", "area".
Default: "LINEAR". Default: "linear".
:return: output tensor. :return: output tensor.
.. note:: .. note::
...@@ -340,9 +341,9 @@ def warp_perspective( ...@@ -340,9 +341,9 @@ def warp_perspective(
inp: Tensor, inp: Tensor,
M: Tensor, M: Tensor,
dsize: Union[Tuple[int, int], int, Tensor], dsize: Union[Tuple[int, int], int, Tensor],
border_mode: str = "REPLICATE", border_mode: str = "replicate",
border_val: float = 0.0, border_val: float = 0.0,
interp_mode: str = "LINEAR", interp_mode: str = "linear",
) -> Tensor: ) -> Tensor:
r""" r"""
Applies perspective transformation to batched 2D images. Applies perspective transformation to batched 2D images.
...@@ -359,11 +360,11 @@ def warp_perspective( ...@@ -359,11 +360,11 @@ def warp_perspective(
:param M: `(batch, 3, 3)` transformation matrix. :param M: `(batch, 3, 3)` transformation matrix.
:param dsize: `(h, w)` size of the output image. :param dsize: `(h, w)` size of the output image.
:param border_mode: pixel extrapolation method. :param border_mode: pixel extrapolation method.
Default: "REPLICATE". Currently also support "CONSTANT", "REFLECT", Default: "replicate". Currently also support "constant", "reflect",
"REFLECT_101", "WRAP". "reflect_101", "wrap".
:param border_val: value used in case of a constant border. Default: 0 :param border_val: value used in case of a constant border. Default: 0
:param interp_mode: interpolation methods. :param interp_mode: interpolation methods.
Default: "LINEAR". Currently only support "LINEAR" mode. Default: "linear". Currently only support "linear" mode.
:return: output tensor. :return: output tensor.
Note: Note:
...@@ -409,7 +410,7 @@ def interpolate( ...@@ -409,7 +410,7 @@ def interpolate(
inp: Tensor, inp: Tensor,
size: Optional[Union[int, Tuple[int, int]]] = None, size: Optional[Union[int, Tuple[int, int]]] = None,
scale_factor: Optional[Union[float, Tuple[float, float]]] = None, scale_factor: Optional[Union[float, Tuple[float, float]]] = None,
mode: str = "BILINEAR", mode: str = "bilinear",
align_corners: Optional[bool] = None, align_corners: Optional[bool] = None,
) -> Tensor: ) -> Tensor:
r""" r"""
...@@ -419,9 +420,9 @@ def interpolate( ...@@ -419,9 +420,9 @@ def interpolate(
:param size: size of the output tensor. Default: None :param size: size of the output tensor. Default: None
:param scale_factor: scaling factor of the output tensor. Default: None :param scale_factor: scaling factor of the output tensor. Default: None
:param mode: interpolation methods, acceptable values are: :param mode: interpolation methods, acceptable values are:
"BILINEAR", "LINEAR". Default: "BILINEAR" "bilinear", "linear". Default: "bilinear"
:param align_corners: This only has an effect when `mode` :param align_corners: This only has an effect when `mode`
is "BILINEAR" or "LINEAR". Geometrically, we consider the pixels of the input is "bilinear" or "linear". Geometrically, we consider the pixels of the input
and output as squares rather than points. If set to ``True``, the input and output as squares rather than points. If set to ``True``, the input
and output tensors are aligned by the center points of their corner and output tensors are aligned by the center points of their corner
pixels, preserving the values at the corner pixels. If set to ``False``, pixels, preserving the values at the corner pixels. If set to ``False``,
...@@ -455,10 +456,10 @@ def interpolate( ...@@ -455,10 +456,10 @@ def interpolate(
[3. 3.25 3.75 4. ]]]] [3. 3.25 3.75 4. ]]]]
""" """
mode = mode.upper() mode = mode.lower()
if mode not in ["BILINEAR", "LINEAR"]: if mode not in ["bilinear", "linear"]:
raise ValueError("interpolate only support linear or bilinear mode") raise ValueError("interpolate only support linear or bilinear mode")
if mode not in ["BILINEAR", "LINEAR"]: if mode not in ["bilinear", "linear"]:
if align_corners is not None: if align_corners is not None:
raise ValueError( raise ValueError(
"align_corners option can only be set in the bilinear/linear interpolating mode" "align_corners option can only be set in the bilinear/linear interpolating mode"
...@@ -471,16 +472,16 @@ def interpolate( ...@@ -471,16 +472,16 @@ def interpolate(
size is not None size is not None
and scale_factor is None and scale_factor is None
and not align_corners and not align_corners
and mode == "BILINEAR" and mode == "bilinear"
and inp.ndim in [4, 5] and inp.ndim in [4, 5]
): ):
# fastpath for interpolate # fastpath for interpolate
op = builtin.Resize(imode="LINEAR", format="NCHW") op = builtin.Resize(imode="linear", format="NCHW")
shape = astensor1d(size, inp, dtype="int32", device=inp.device) shape = astensor1d(size, inp, dtype="int32", device=inp.device)
(result,) = apply(op, inp, shape) (result,) = apply(op, inp, shape)
return result return result
if mode == "LINEAR": if mode == "linear":
inp = expand_dims(inp, 3) inp = expand_dims(inp, 3)
if inp.ndim != 4: if inp.ndim != 4:
...@@ -492,14 +493,14 @@ def interpolate( ...@@ -492,14 +493,14 @@ def interpolate(
if isinstance(scale_factor, (float, int)): if isinstance(scale_factor, (float, int)):
scale_factor = float(scale_factor) scale_factor = float(scale_factor)
if mode == "LINEAR": if mode == "linear":
scale_factor = (scale_factor, float(1)) scale_factor = (scale_factor, float(1))
else: else:
scale_factor = (scale_factor, scale_factor) scale_factor = (scale_factor, scale_factor)
else: else:
if mode == "LINEAR": if mode == "linear":
raise ValueError( raise ValueError(
"under LINEAR mode, scale_factor can only be single value" "under linear mode, scale_factor can only be single value"
) )
assert len(scale_factor) == 2, "shape of scale_factor must be equal to (2, )" assert len(scale_factor) == 2, "shape of scale_factor must be equal to (2, )"
...@@ -524,8 +525,8 @@ def interpolate( ...@@ -524,8 +525,8 @@ def interpolate(
if isinstance(size, int): if isinstance(size, int):
size = (size, 1) size = (size, 1)
else: else:
if mode == "LINEAR": if mode == "linear":
raise ValueError("under LINEAR mode, size can only be single value") raise ValueError("under linear mode, size can only be single value")
dsize = size dsize = size
oh, ow = dsize[0], dsize[1] oh, ow = dsize[0], dsize[1]
...@@ -534,7 +535,7 @@ def interpolate( ...@@ -534,7 +535,7 @@ def interpolate(
if align_corners: if align_corners:
hscale = (ih - 1.0) / (oh - 1.0) hscale = (ih - 1.0) / (oh - 1.0)
wscale = 1.0 * iw / ow wscale = 1.0 * iw / ow
if mode != "LINEAR": if mode != "linear":
wscale = (iw - 1.0) / (ow - 1.0) wscale = (iw - 1.0) / (ow - 1.0)
row0 = concat( row0 = concat(
[wscale, Tensor([0, 0], dtype="float32", device=inp.device)], axis=0 [wscale, Tensor([0, 0], dtype="float32", device=inp.device)], axis=0
...@@ -570,8 +571,8 @@ def interpolate( ...@@ -570,8 +571,8 @@ def interpolate(
weight = broadcast_to(weight, (inp.shape[0], 3, 3)) weight = broadcast_to(weight, (inp.shape[0], 3, 3))
weight = weight.astype("float32") weight = weight.astype("float32")
ret = warp_perspective(inp, weight, dsize, interp_mode="LINEAR") ret = warp_perspective(inp, weight, dsize, interp_mode="linear")
if mode == "LINEAR": if mode == "linear":
ret = reshape(ret, ret.shape[0:3]) ret = reshape(ret, ret.shape[0:3])
return ret return ret
......
...@@ -24,7 +24,7 @@ class BatchMatMulActivation(Module): ...@@ -24,7 +24,7 @@ class BatchMatMulActivation(Module):
in_features: int, in_features: int,
out_features: int, out_features: int,
bias: bool = True, bias: bool = True,
nonlinear_mode="IDENTITY", nonlinear_mode="identity",
**kwargs **kwargs
): ):
super().__init__(**kwargs) super().__init__(**kwargs)
...@@ -37,7 +37,7 @@ class BatchMatMulActivation(Module): ...@@ -37,7 +37,7 @@ class BatchMatMulActivation(Module):
if bias: if bias:
b_shape = (out_features,) b_shape = (out_features,)
self.bias = Parameter(np.zeros(b_shape, dtype=np.float32)) self.bias = Parameter(np.zeros(b_shape, dtype=np.float32))
self.nonlinear_mode = nonlinear_mode self.nonlinear_mode = nonlinear_mode.lower()
self.reset_parameters() self.reset_parameters()
def _get_fanin(self): def _get_fanin(self):
...@@ -54,7 +54,7 @@ class BatchMatMulActivation(Module): ...@@ -54,7 +54,7 @@ class BatchMatMulActivation(Module):
res = matmul(weight, x) res = matmul(weight, x)
if self.bias is not None: if self.bias is not None:
res += bias res += bias
if self.nonlinear_mode == "RELU": if self.nonlinear_mode == "relu":
res = relu(res) res = relu(res)
return res return res
......
...@@ -138,11 +138,11 @@ class Conv1d(_ConvNd): ...@@ -138,11 +138,11 @@ class Conv1d(_ConvNd):
out_channel // groups, in_channels // groups, *kernel_size)`. out_channel // groups, in_channels // groups, *kernel_size)`.
:param bias: whether to add a bias onto the result of convolution. Default: :param bias: whether to add a bias onto the result of convolution. Default:
True True
:param conv_mode: Supports `CROSS_CORRELATION`. Default: :param conv_mode: Supports `cross_correlation`. Default:
`CROSS_CORRELATION` `cross_correlation`
:param compute_mode: When set to "DEFAULT", no special requirements will be :param compute_mode: When set to "default", no special requirements will be
placed on the precision of intermediate results. When set to "FLOAT32", placed on the precision of intermediate results. When set to "float32",
"Float32" would be used for accumulator and intermediate result, but only "float32" would be used for accumulator and intermediate result, but only
effective when input and output are of float16 dtype. effective when input and output are of float16 dtype.
Examples: Examples:
...@@ -176,8 +176,8 @@ class Conv1d(_ConvNd): ...@@ -176,8 +176,8 @@ class Conv1d(_ConvNd):
dilation: int = 1, dilation: int = 1,
groups: int = 1, groups: int = 1,
bias: bool = True, bias: bool = True,
conv_mode: str = "CROSS_CORRELATION", conv_mode: str = "cross_correlation",
compute_mode: str = "DEFAULT", compute_mode: str = "default",
**kwargs **kwargs
): ):
kernel_size = kernel_size kernel_size = kernel_size
...@@ -298,11 +298,11 @@ class Conv2d(_ConvNd): ...@@ -298,11 +298,11 @@ class Conv2d(_ConvNd):
out_channel // groups, in_channels // groups, *kernel_size)`. out_channel // groups, in_channels // groups, *kernel_size)`.
:param bias: whether to add a bias onto the result of convolution. Default: :param bias: whether to add a bias onto the result of convolution. Default:
True True
:param conv_mode: Supports `CROSS_CORRELATION`. Default: :param conv_mode: Supports `cross_correlation`. Default:
`CROSS_CORRELATION` `cross_correlation`
:param compute_mode: When set to "DEFAULT", no special requirements will be :param compute_mode: When set to "default", no special requirements will be
placed on the precision of intermediate results. When set to "FLOAT32", placed on the precision of intermediate results. When set to "float32",
"Float32" would be used for accumulator and intermediate result, but only "float32" would be used for accumulator and intermediate result, but only
effective when input and output are of float16 dtype. effective when input and output are of float16 dtype.
Examples: Examples:
...@@ -336,8 +336,8 @@ class Conv2d(_ConvNd): ...@@ -336,8 +336,8 @@ class Conv2d(_ConvNd):
dilation: Union[int, Tuple[int, int]] = 1, dilation: Union[int, Tuple[int, int]] = 1,
groups: int = 1, groups: int = 1,
bias: bool = True, bias: bool = True,
conv_mode: str = "CROSS_CORRELATION", conv_mode: str = "cross_correlation",
compute_mode: str = "DEFAULT", compute_mode: str = "default",
**kwargs **kwargs
): ):
kernel_size = _pair_nonzero(kernel_size) kernel_size = _pair_nonzero(kernel_size)
...@@ -436,15 +436,16 @@ class Conv3d(_ConvNd): ...@@ -436,15 +436,16 @@ class Conv3d(_ConvNd):
:param padding: size of the paddings added to the input on both sides of its :param padding: size of the paddings added to the input on both sides of its
spatial dimensions. Only zero-padding is supported. Default: 0 spatial dimensions. Only zero-padding is supported. Default: 0
:param dilation: dilation of the 3D convolution operation. Default: 1 :param dilation: dilation of the 3D convolution operation. Default: 1
:param groups: number of groups into which the input and output channels are divided, so as to perform a "grouped convolution". When ``groups`` is not 1, :param groups: number of groups into which the input and output channels are divided,
so as to perform a "grouped convolution". When ``groups`` is not 1,
``in_channels`` and ``out_channels`` must be divisible by ``groups``, ``in_channels`` and ``out_channels`` must be divisible by ``groups``,
and there would be an extra dimension at the beginning of the weight's and there would be an extra dimension at the beginning of the weight's
shape. Specifically, the shape of weight would be `(groups, shape. Specifically, the shape of weight would be `(groups,
out_channel // groups, in_channels // groups, *kernel_size)`. out_channel // groups, in_channels // groups, *kernel_size)`.
:param bias: whether to add a bias onto the result of convolution. Default: :param bias: whether to add a bias onto the result of convolution. Default:
True True
:param conv_mode: Supports `CROSS_CORRELATION`. Default: :param conv_mode: Supports `cross_correlation`. Default:
`CROSS_CORRELATION` `cross_correlation`
Examples: Examples:
...@@ -477,7 +478,7 @@ class Conv3d(_ConvNd): ...@@ -477,7 +478,7 @@ class Conv3d(_ConvNd):
dilation: Union[int, Tuple[int, int, int]] = 1, dilation: Union[int, Tuple[int, int, int]] = 1,
groups: int = 1, groups: int = 1,
bias: bool = True, bias: bool = True,
conv_mode: str = "CROSS_CORRELATION", conv_mode: str = "cross_correlation",
): ):
kernel_size = _triple_nonzero(kernel_size) kernel_size = _triple_nonzero(kernel_size)
stride = _triple_nonzero(stride) stride = _triple_nonzero(stride)
...@@ -566,11 +567,11 @@ class ConvTranspose2d(_ConvNd): ...@@ -566,11 +567,11 @@ class ConvTranspose2d(_ConvNd):
out_channels // groups, in_channels // groups, *kernel_size)``. Default: 1 out_channels // groups, in_channels // groups, *kernel_size)``. Default: 1
:param bias: wether to add a bias onto the result of convolution. Default: :param bias: wether to add a bias onto the result of convolution. Default:
True True
:param conv_mode: Supports `CROSS_CORRELATION`. Default: :param conv_mode: Supports `cross_correlation`. Default:
`CROSS_CORRELATION` `cross_correlation`
:param compute_mode: When set to "DEFAULT", no special requirements will be :param compute_mode: When set to "default", no special requirements will be
placed on the precision of intermediate results. When set to "FLOAT32", placed on the precision of intermediate results. When set to "float32",
"Float32" would be used for accumulator and intermediate result, but only "float32" would be used for accumulator and intermediate result, but only
effective when input and output are of float16 dtype. effective when input and output are of float16 dtype.
""" """
...@@ -584,8 +585,8 @@ class ConvTranspose2d(_ConvNd): ...@@ -584,8 +585,8 @@ class ConvTranspose2d(_ConvNd):
dilation: Union[int, Tuple[int, int]] = 1, dilation: Union[int, Tuple[int, int]] = 1,
groups: int = 1, groups: int = 1,
bias: bool = True, bias: bool = True,
conv_mode: str = "CROSS_CORRELATION", conv_mode: str = "cross_correlation",
compute_mode: str = "DEFAULT", compute_mode: str = "default",
**kwargs **kwargs
): ):
kernel_size = _pair_nonzero(kernel_size) kernel_size = _pair_nonzero(kernel_size)
...@@ -679,7 +680,7 @@ class LocalConv2d(Conv2d): ...@@ -679,7 +680,7 @@ class LocalConv2d(Conv2d):
padding: Union[int, Tuple[int, int]] = 0, padding: Union[int, Tuple[int, int]] = 0,
dilation: Union[int, Tuple[int, int]] = 1, dilation: Union[int, Tuple[int, int]] = 1,
groups: int = 1, groups: int = 1,
conv_mode: str = "CROSS_CORRELATION", conv_mode: str = "cross_correlation",
**kwargs **kwargs
): ):
self.input_height = input_height self.input_height = input_height
...@@ -758,11 +759,11 @@ class DeformableConv2d(_ConvNd): ...@@ -758,11 +759,11 @@ class DeformableConv2d(_ConvNd):
out_channel // groups, in_channels // groups, *kernel_size)`. out_channel // groups, in_channels // groups, *kernel_size)`.
:param bias: whether to add a bias onto the result of convolution. Default: :param bias: whether to add a bias onto the result of convolution. Default:
True True
:param conv_mode: Supports `CROSS_CORRELATION`. Default: :param conv_mode: Supports `cross_correlation`. Default:
`CROSS_CORRELATION` `cross_correlation`
:param compute_mode: When set to "DEFAULT", no special requirements will be :param compute_mode: When set to "default", no special requirements will be
placed on the precision of intermediate results. When set to "FLOAT32", placed on the precision of intermediate results. When set to "float32",
"Float32" would be used for accumulator and intermediate result, but only "float32" would be used for accumulator and intermediate result, but only
effective when input and output are of float16 dtype. effective when input and output are of float16 dtype.
""" """
...@@ -776,8 +777,8 @@ class DeformableConv2d(_ConvNd): ...@@ -776,8 +777,8 @@ class DeformableConv2d(_ConvNd):
dilation: Union[int, Tuple[int, int]] = 1, dilation: Union[int, Tuple[int, int]] = 1,
groups: int = 1, groups: int = 1,
bias: bool = True, bias: bool = True,
conv_mode: str = "CROSS_CORRELATION", conv_mode: str = "cross_correlation",
compute_mode: str = "DEFAULT", compute_mode: str = "default",
**kwargs **kwargs
): ):
kernel_size = _pair_nonzero(kernel_size) kernel_size = _pair_nonzero(kernel_size)
......
...@@ -24,8 +24,8 @@ class _ConvBnActivation2d(Module): ...@@ -24,8 +24,8 @@ class _ConvBnActivation2d(Module):
dilation: Union[int, Tuple[int, int]] = 1, dilation: Union[int, Tuple[int, int]] = 1,
groups: int = 1, groups: int = 1,
bias: bool = True, bias: bool = True,
conv_mode: str = "CROSS_CORRELATION", conv_mode: str = "cross_correlation",
compute_mode: str = "DEFAULT", compute_mode: str = "default",
eps=1e-5, eps=1e-5,
momentum=0.9, momentum=0.9,
affine=True, affine=True,
......
...@@ -18,58 +18,58 @@ class Elemwise(Module): ...@@ -18,58 +18,58 @@ class Elemwise(Module):
:param method: the elemwise method, support the following string. :param method: the elemwise method, support the following string.
It will do the normal elemwise operator for float. It will do the normal elemwise operator for float.
* "ADD": a + b * "add": a + b
* "FUSE_ADD_RELU": max(x+y, 0) * "fuse_add_relu": max(x+y, 0)
* "MUL": x * y * "mul": x * y
* "MIN": min(x, y) * "min": min(x, y)
* "MAX": max(x, y) * "max": max(x, y)
* "SUB": x - y * "sub": x - y
* "TRUE_DIV": x / y * "true_div": x / y
* "FUSE_ADD_SIGMOID": sigmoid(x + y) * "fuse_add_sigmoid": sigmoid(x + y)
* "FUSE_ADD_TANH": tanh(x + y) * "fuse_add_tanh": tanh(x + y)
* "RELU": x > 0 ? x : 0 * "relu": x > 0 ? x : 0
* "ABS": x > 0 ? x : -x * "abs": x > 0 ? x : -x
* "SIGMOID": sigmoid(x) * "sigmoid": sigmoid(x)
* "EXP": exp(x) * "exp": exp(x)
* "TANH": tanh(x) * "tanh": tanh(x)
* "FUSE_MUL_ADD3": x * y + z * "fuse_mul_add3": x * y + z
* "FAST_TANH": x * (27. + x * x) / (27. + 9. * x * x) * "fast_tanh": x * (27. + x * x) / (27. + 9. * x * x)
* "NEGATE": -x * "negate": -x
* "ACOS": acos(x) * "acos": acos(x)
* "ASIN": asin(x) * "asin": asin(x)
* "CEIL": ceil(x) * "ceil": ceil(x)
* "COS": cos(x) * "cos": cos(x)
* "EXPM1": expm1(x) * "expm1": expm1(x)
* "FLOOR": floor(x) * "floor": floor(x)
* "LOG": log(x) * "log": log(x)
* "LOG1P": log1p(x) * "log1p": log1p(x)
* "SIN": sin(x) * "sin": sin(x)
* "ROUND": round(x) * "round": round(x)
* "ERF": erf(x) * "erf": erf(x)
* "ERFINV": erfinv(x) * "erfinv": erfinv(x)
* "ERFC": erfc(x) * "erfc": erfc(x)
* "ERFCINV": erfcinv(x) * "erfcinv": erfcinv(x)
* "ABS_GRAD": abs_grad * "abs_grad": abs_grad
* "FLOOR_DIV": floor_div * "floor_div": floor_div
* "MOD": mod * "mod": mod
* "SIGMOID_GRAD": sigmoid_grad * "sigmoid_grad": sigmoid_grad
* "SWITCH_GT0": switch_gt0 * "switch_gt0": switch_gt0
* "TANH_GRAD": tanh_grad * "tanh_grad": tanh_grad
* "LT": less * "lt": less
* "LEQ": leq * "leq": leq
* "EQ": equal * "eq": equal
* "POW": pow * "pow": pow
* "LOG_SUM_EXP": log_sum_exp * "log_sum_exp": log_sum_exp
* "FAST_TANH_GRAD": fast_tanh_grad * "fast_tanh_grad": fast_tanh_grad
* "ATAN2": atan2 * "atan2": atan2
* "COND_LEQ_MOV": cond_leq_mov * "cond_leq_mov": cond_leq_mov
* "H_SWISH": h_swish * "h_swish": h_swish
* "FUSE_ADD_H_SWISH": h_swish(x+y) * "fuse_add_h_swish": h_swish(x+y)
* "H_SWISH_GRAD": h_swish_grad * "h_swish_grad": h_swish_grad
* "AND": bool binary: x && y * "and": bool binary: x && y
* "OR": bool binary: x || y * "or": bool binary: x || y
* "XOR": bool binary: x ^ y * "xor": bool binary: x ^ y
* "NOT": bool unary: ~x * "not": bool unary: ~x
""" """
def __init__(self, method, **kwargs): def __init__(self, method, **kwargs):
......
...@@ -27,7 +27,7 @@ class BatchMatMulActivation(Float.BatchMatMulActivation, QuantizedModule): ...@@ -27,7 +27,7 @@ class BatchMatMulActivation(Float.BatchMatMulActivation, QuantizedModule):
in_features: int, in_features: int,
out_features: int, out_features: int,
bias: bool = True, bias: bool = True,
nonlinear_mode="IDENTITY", nonlinear_mode="identity",
dtype=None, dtype=None,
**kwargs **kwargs
): ):
......
...@@ -34,8 +34,8 @@ class Conv2d(Float.Conv2d, QuantizedModule): ...@@ -34,8 +34,8 @@ class Conv2d(Float.Conv2d, QuantizedModule):
padding: Union[int, Tuple[int, int]] = 0, padding: Union[int, Tuple[int, int]] = 0,
dilation: Union[int, Tuple[int, int]] = 1, dilation: Union[int, Tuple[int, int]] = 1,
groups: int = 1, groups: int = 1,
conv_mode: str = "CROSS_CORRELATION", conv_mode: str = "cross_correlation",
compute_mode: str = "DEFAULT", compute_mode: str = "default",
dtype=None, dtype=None,
**kwargs **kwargs
): ):
...@@ -53,7 +53,7 @@ class Conv2d(Float.Conv2d, QuantizedModule): ...@@ -53,7 +53,7 @@ class Conv2d(Float.Conv2d, QuantizedModule):
) )
self.output_dtype = dtype self.output_dtype = dtype
def calc_conv_quantized(self, inp, nonlinear_mode="IDENTITY"): def calc_conv_quantized(self, inp, nonlinear_mode="identity"):
inp_scale = dtype.get_scale(inp.dtype) inp_scale = dtype.get_scale(inp.dtype)
w_scale = dtype.get_scale(self.weight.dtype) w_scale = dtype.get_scale(self.weight.dtype)
bias_scale = inp_scale * w_scale bias_scale = inp_scale * w_scale
...@@ -100,11 +100,11 @@ class Conv2d(Float.Conv2d, QuantizedModule): ...@@ -100,11 +100,11 @@ class Conv2d(Float.Conv2d, QuantizedModule):
return qconv return qconv
def forward(self, inp): def forward(self, inp):
return self.calc_conv_quantized(inp, nonlinear_mode="IDENTITY") return self.calc_conv_quantized(inp, nonlinear_mode="identity")
class ConvRelu2d(Conv2d): class ConvRelu2d(Conv2d):
r"""Quantized version of :class:`~.qat.ConvRelu2d`.""" r"""Quantized version of :class:`~.qat.ConvRelu2d`."""
def forward(self, inp): def forward(self, inp):
return self.calc_conv_quantized(inp, nonlinear_mode="RELU") return self.calc_conv_quantized(inp, nonlinear_mode="relu")
...@@ -50,11 +50,11 @@ class ConvBn2d(_ConvBnActivation2d): ...@@ -50,11 +50,11 @@ class ConvBn2d(_ConvBnActivation2d):
r"""Quantized version of :class:`~.qat.ConvBn2d`.""" r"""Quantized version of :class:`~.qat.ConvBn2d`."""
def forward(self, inp): def forward(self, inp):
return self.calc_conv_quantized(inp, nonlinear_mode="IDENTITY") return self.calc_conv_quantized(inp, nonlinear_mode="identity")
class ConvBnRelu2d(_ConvBnActivation2d): class ConvBnRelu2d(_ConvBnActivation2d):
r"""Quantized version of :class:`~.qat.ConvBnRelu2d`.""" r"""Quantized version of :class:`~.qat.ConvBnRelu2d`."""
def forward(self, inp): def forward(self, inp):
return self.calc_conv_quantized(inp, nonlinear_mode="RELU") return self.calc_conv_quantized(inp, nonlinear_mode="relu")
...@@ -16,7 +16,7 @@ class Elemwise(QuantizedModule): ...@@ -16,7 +16,7 @@ class Elemwise(QuantizedModule):
def __init__(self, method, dtype=None, **kwargs): def __init__(self, method, dtype=None, **kwargs):
super().__init__(**kwargs) super().__init__(**kwargs)
self.method = "Q" + method self.method = "q" + method
self.output_dtype = dtype self.output_dtype = dtype
def forward(self, *inps): def forward(self, *inps):
......
...@@ -16,9 +16,9 @@ fi ...@@ -16,9 +16,9 @@ fi
export MEGENGINE_LOGGING_LEVEL="ERROR" export MEGENGINE_LOGGING_LEVEL="ERROR"
pushd $(dirname "${BASH_SOURCE[0]}")/.. >/dev/null pushd $(dirname "${BASH_SOURCE[0]}")/.. >/dev/null
PYTHONPATH="." PY_IGNORE_IMPORTMISMATCH=1 python3 -m pytest $test_dirs -m 'not isolated_distributed' PYTHONPATH="." PY_IGNORE_IMPORTMISMATCH=1 python3 -m pytest -v $test_dirs -m 'not isolated_distributed'
if [[ "$TEST_PLAT" == cuda ]]; then if [[ "$TEST_PLAT" == cuda ]]; then
echo "test GPU pytest now" echo "test GPU pytest now"
PYTHONPATH="." PY_IGNORE_IMPORTMISMATCH=1 python3 -m pytest $test_dirs -m 'isolated_distributed' PYTHONPATH="." PY_IGNORE_IMPORTMISMATCH=1 python3 -m pytest -v $test_dirs -m 'isolated_distributed'
fi fi
popd >/dev/null popd >/dev/null
...@@ -372,7 +372,7 @@ def test_interpolate_fastpath(): ...@@ -372,7 +372,7 @@ def test_interpolate_fastpath():
x = mge.Tensor(x_np) x = mge.Tensor(x_np)
grad = Grad().wrt(x, callback=save_to(x)) grad = Grad().wrt(x, callback=save_to(x))
y = F.vision.interpolate(x, size=(16, 16), mode="BILINEAR") y = F.vision.interpolate(x, size=(16, 16), mode="bilinear")
grad(y, F.ones_like(y)) grad(y, F.ones_like(y))
np.testing.assert_equal(np.ones(x_np.shape, dtype=np.float32) / 4, x.grad.numpy()) np.testing.assert_equal(np.ones(x_np.shape, dtype=np.float32) / 4, x.grad.numpy())
......
...@@ -162,7 +162,7 @@ def test_qadd(): ...@@ -162,7 +162,7 @@ def test_qadd():
x = tensor(x, dtype=dtype.qint8(inp_scale)) x = tensor(x, dtype=dtype.qint8(inp_scale))
y = tensor(y, dtype=dtype.qint8(inp_scale)) y = tensor(y, dtype=dtype.qint8(inp_scale))
result_mge = F.elemwise._elemwise_multi_type( result_mge = F.elemwise._elemwise_multi_type(
x, y, mode="QADD", dtype=dtype.qint8(outp_scale) x, y, mode="qadd", dtype=dtype.qint8(outp_scale)
) )
result_mge = result_mge.astype("float32").numpy() result_mge = result_mge.astype("float32").numpy()
result_expect = x.astype("float32").numpy() + y.astype("float32").numpy() result_expect = x.astype("float32").numpy() + y.astype("float32").numpy()
......
...@@ -140,8 +140,8 @@ def test_interpolate(): ...@@ -140,8 +140,8 @@ def test_interpolate():
def linear_interpolate(): def linear_interpolate():
inp = tensor(np.arange(1, 3, dtype=np.float32).reshape(1, 1, 2)) inp = tensor(np.arange(1, 3, dtype=np.float32).reshape(1, 1, 2))
out = F.vision.interpolate(inp, scale_factor=2.0, mode="LINEAR") out = F.vision.interpolate(inp, scale_factor=2.0, mode="linear")
out2 = F.vision.interpolate(inp, 4, mode="LINEAR") out2 = F.vision.interpolate(inp, 4, mode="linear")
np.testing.assert_allclose( np.testing.assert_allclose(
out.numpy(), np.array([[[1.0, 1.25, 1.75, 2.0]]], dtype=np.float32) out.numpy(), np.array([[[1.0, 1.25, 1.75, 2.0]]], dtype=np.float32)
...@@ -170,13 +170,13 @@ def test_interpolate(): ...@@ -170,13 +170,13 @@ def test_interpolate():
inp = tensor(np.arange(1, 5, dtype=np.float32).reshape(1, 1, 2, 2)) inp = tensor(np.arange(1, 5, dtype=np.float32).reshape(1, 1, 2, 2))
with pytest.raises(ValueError): with pytest.raises(ValueError):
F.vision.interpolate(inp, scale_factor=2.0, mode="LINEAR") F.vision.interpolate(inp, scale_factor=2.0, mode="linear")
def inappropriate_scale_linear_interpolate(): def inappropriate_scale_linear_interpolate():
inp = tensor(np.arange(1, 3, dtype=np.float32).reshape(1, 1, 2)) inp = tensor(np.arange(1, 3, dtype=np.float32).reshape(1, 1, 2))
with pytest.raises(ValueError): with pytest.raises(ValueError):
F.vision.interpolate(inp, scale_factor=[2.0, 3.0], mode="LINEAR") F.vision.interpolate(inp, scale_factor=[2.0, 3.0], mode="linear")
linear_interpolate() linear_interpolate()
many_batch_interpolate() many_batch_interpolate()
...@@ -339,18 +339,18 @@ def test_interpolate_fastpath(): ...@@ -339,18 +339,18 @@ def test_interpolate_fastpath():
] ]
for inp_shape, target_shape in test_cases: for inp_shape, target_shape in test_cases:
x = tensor(np.random.randn(*inp_shape), dtype=np.float32) x = tensor(np.random.randn(*inp_shape), dtype=np.float32)
out = F.vision.interpolate(x, target_shape, mode="BILINEAR") out = F.vision.interpolate(x, target_shape, mode="bilinear")
assert out.shape[0] == x.shape[0] and out.shape[1] == x.shape[1] assert out.shape[0] == x.shape[0] and out.shape[1] == x.shape[1]
assert out.shape[2] == target_shape[0] and out.shape[3] == target_shape[1] assert out.shape[2] == target_shape[0] and out.shape[3] == target_shape[1]
# check value # check value
x = tensor(np.ones((3, 3, 10, 10)), dtype=np.float32) x = tensor(np.ones((3, 3, 10, 10)), dtype=np.float32)
out = F.vision.interpolate(x, (15, 5), mode="BILINEAR") out = F.vision.interpolate(x, (15, 5), mode="bilinear")
np.testing.assert_equal(out.numpy(), np.ones((3, 3, 15, 5)).astype(np.float32)) np.testing.assert_equal(out.numpy(), np.ones((3, 3, 15, 5)).astype(np.float32))
np_x = np.arange(32) np_x = np.arange(32)
x = tensor(np_x).astype(np.float32).reshape(1, 1, 32, 1) x = tensor(np_x).astype(np.float32).reshape(1, 1, 32, 1)
out = F.vision.interpolate(x, (1, 1), mode="BILINEAR") out = F.vision.interpolate(x, (1, 1), mode="bilinear")
np.testing.assert_equal(out.item(), np_x.mean()) np.testing.assert_equal(out.item(), np_x.mean())
...@@ -374,7 +374,7 @@ def test_warp_affine(): ...@@ -374,7 +374,7 @@ def test_warp_affine():
inp_shape = (1, 3, 3, 3) inp_shape = (1, 3, 3, 3)
x = tensor(np.arange(27, dtype=np.float32).reshape(inp_shape)) x = tensor(np.arange(27, dtype=np.float32).reshape(inp_shape))
weightv = [[[1.26666667, 0.6, -83.33333333], [-0.33333333, 1, 66.66666667]]] weightv = [[[1.26666667, 0.6, -83.33333333], [-0.33333333, 1, 66.66666667]]]
outp = F.vision.warp_affine(x, tensor(weightv), (2, 2), border_mode="WRAP") outp = F.vision.warp_affine(x, tensor(weightv), (2, 2), border_mode="wrap")
res = np.array( res = np.array(
[ [
[ [
...@@ -509,7 +509,7 @@ def test_conv_bias(): ...@@ -509,7 +509,7 @@ def test_conv_bias():
SH, SH,
SW, SW,
has_bias=True, has_bias=True,
nonlinear_mode="IDENTITY", nonlinear_mode="identity",
): ):
inp_v = np.random.normal(size=(N, IC, IH, IW)) inp_v = np.random.normal(size=(N, IC, IH, IW))
w_v = np.random.normal(size=(OC, IC, KH, KW)) w_v = np.random.normal(size=(OC, IC, KH, KW))
...@@ -541,7 +541,7 @@ def test_conv_bias(): ...@@ -541,7 +541,7 @@ def test_conv_bias():
O = F.conv2d( O = F.conv2d(
inp, w, b if has_bias else None, stride=(SH, SW), padding=(PH, PW), inp, w, b if has_bias else None, stride=(SH, SW), padding=(PH, PW),
) )
if nonlinear_mode == "RELU": if nonlinear_mode == "relu":
return F.relu(O) return F.relu(O)
else: else:
return O return O
...@@ -583,8 +583,8 @@ def test_conv_bias(): ...@@ -583,8 +583,8 @@ def test_conv_bias():
run(10, 12, 24, 46, 46, 1, 1, 2, 1, 3, 1) run(10, 12, 24, 46, 46, 1, 1, 2, 1, 3, 1)
run(10, 36, 8, 46, 26, 2, 2, 2, 1, 1, 2) run(10, 36, 8, 46, 26, 2, 2, 2, 1, 1, 2)
run(10, 36, 8, 46, 26, 2, 2, 2, 1, 1, 2, False, "RELU") run(10, 36, 8, 46, 26, 2, 2, 2, 1, 1, 2, False, "relu")
run(10, 36, 8, 46, 26, 2, 2, 2, 1, 1, 2, True, "RELU") run(10, 36, 8, 46, 26, 2, 2, 2, 1, 1, 2, True, "relu")
@pytest.mark.skipif( @pytest.mark.skipif(
......
...@@ -23,8 +23,8 @@ def test_module_elemwise(): ...@@ -23,8 +23,8 @@ def test_module_elemwise():
y = np.random.rand(100).astype("float32") y = np.random.rand(100).astype("float32")
x, y = tensor(x), tensor(y) x, y = tensor(x), tensor(y)
np.testing.assert_almost_equal( np.testing.assert_almost_equal(
test_func("H_SWISH", x), F.hswish(x).numpy(), decimal=6 test_func("h_swish", x), F.hswish(x).numpy(), decimal=6
) )
np.testing.assert_almost_equal( np.testing.assert_almost_equal(
test_func("ADD", x, y), F.add(x, y).numpy(), decimal=6 test_func("add", x, y), F.add(x, y).numpy(), decimal=6
) )
...@@ -133,7 +133,7 @@ def test_dequant_stub(): ...@@ -133,7 +133,7 @@ def test_dequant_stub():
np.testing.assert_allclose(q, fake_quant_normal.numpy()) np.testing.assert_allclose(q, fake_quant_normal.numpy())
@pytest.mark.parametrize("kind", ["COS", "RELU", "ADD", "MUL", "FUSE_ADD_RELU"]) @pytest.mark.parametrize("kind", ["cos", "relu", "add", "mul", "fuse_add_relu"])
def test_elemwise(kind): def test_elemwise(kind):
normal_net = Float.Elemwise(kind) normal_net = Float.Elemwise(kind)
normal_net.eval() normal_net.eval()
...@@ -167,7 +167,7 @@ def test_elemwise(kind): ...@@ -167,7 +167,7 @@ def test_elemwise(kind):
x2_int8 = quant(x2, x2_scale) x2_int8 = quant(x2, x2_scale)
# test correctness of `Float`, `QAT` and `Quantized` # test correctness of `Float`, `QAT` and `Quantized`
if kind in ("ADD", "MUL", "FUSE_ADD_RELU"): if kind in ("add", "mul", "fuse_add_relu"):
normal = normal_net(x1, x2) normal = normal_net(x1, x2)
qat_without_fakequant = qat_from_float(x1, x2) qat_without_fakequant = qat_from_float(x1, x2)
fake_quant_normal = fake_quant_act(normal_net(x1, x2), act_scale) fake_quant_normal = fake_quant_act(normal_net(x1, x2), act_scale)
......
...@@ -22,7 +22,7 @@ def fake_quant(x, scale): ...@@ -22,7 +22,7 @@ def fake_quant(x, scale):
return x return x
@pytest.mark.parametrize("kind", ["ABS", "SIN", "SUB", "MUL", "FUSE_ADD_TANH"]) @pytest.mark.parametrize("kind", ["abs", "sin", "sub", "mul", "fuse_add_tanh"])
def test_elemwise(kind): def test_elemwise(kind):
x1 = mge.tensor(np.random.normal(size=(3, 3)).astype("float32")) x1 = mge.tensor(np.random.normal(size=(3, 3)).astype("float32"))
x1_scale = np.float32(np.random.rand() + 1) x1_scale = np.float32(np.random.rand() + 1)
...@@ -39,8 +39,8 @@ def test_elemwise(kind): ...@@ -39,8 +39,8 @@ def test_elemwise(kind):
output_scale = np.float32(np.random.rand() + 1) output_scale = np.float32(np.random.rand() + 1)
output_dtype = dtype.qint8(output_scale) output_dtype = dtype.qint8(output_scale)
quantized_kind = "Q" + kind quantized_kind = "q" + kind
if kind in ("ABS", "SIN"): if kind in ("abs", "sin"):
desired_out = fake_quant(_elwise(x1, mode=kind), output_scale) desired_out = fake_quant(_elwise(x1, mode=kind), output_scale)
actual_out = ( actual_out = (
_elemwise_multi_type( _elemwise_multi_type(
...@@ -84,7 +84,7 @@ def test_conv_bias(): ...@@ -84,7 +84,7 @@ def test_conv_bias():
SH, SH,
SW, SW,
has_bias=True, has_bias=True,
nonlinear_mode="IDENTITY", nonlinear_mode="identity",
): ):
inp_v = np.random.normal(size=(N, IC, IH, IW)) inp_v = np.random.normal(size=(N, IC, IH, IW))
w_v = np.random.normal(size=(OC, IC, KH, KW)) w_v = np.random.normal(size=(OC, IC, KH, KW))
...@@ -116,7 +116,7 @@ def test_conv_bias(): ...@@ -116,7 +116,7 @@ def test_conv_bias():
O = F.conv2d( O = F.conv2d(
inp, w, b if has_bias else None, stride=(SH, SW), padding=(PH, PW), inp, w, b if has_bias else None, stride=(SH, SW), padding=(PH, PW),
) )
if nonlinear_mode == "RELU": if nonlinear_mode == "relu":
return F.relu(O) return F.relu(O)
else: else:
return O return O
...@@ -158,5 +158,5 @@ def test_conv_bias(): ...@@ -158,5 +158,5 @@ def test_conv_bias():
run(10, 12, 24, 46, 46, 1, 1, 2, 1, 3, 1) run(10, 12, 24, 46, 46, 1, 1, 2, 1, 3, 1)
run(10, 36, 8, 46, 26, 2, 2, 2, 1, 1, 2) run(10, 36, 8, 46, 26, 2, 2, 2, 1, 1, 2)
run(10, 36, 8, 46, 26, 2, 2, 2, 1, 1, 2, False, "RELU") run(10, 36, 8, 46, 26, 2, 2, 2, 1, 1, 2, False, "relu")
run(10, 36, 8, 46, 26, 2, 2, 2, 1, 1, 2, True, "RELU") run(10, 36, 8, 46, 26, 2, 2, 2, 1, 1, 2, True, "relu")
...@@ -280,7 +280,7 @@ def test_convbias(): ...@@ -280,7 +280,7 @@ def test_convbias():
@trace(symbolic=True, capture_as_const=True) @trace(symbolic=True, capture_as_const=True)
def fwd(inp, weight, bias): def fwd(inp, weight, bias):
return F.quantized.conv_bias_activation( return F.quantized.conv_bias_activation(
inp, weight, bias, dtype=dtype.qint8(scale=1.0), nonlinear_mode="RELU" inp, weight, bias, dtype=dtype.qint8(scale=1.0), nonlinear_mode="relu"
) )
inp = Tensor(np.random.random((1, 3, 64, 64)), dtype=dtype.qint8(scale=1.0)) inp = Tensor(np.random.random((1, 3, 64, 64)), dtype=dtype.qint8(scale=1.0))
...@@ -297,7 +297,7 @@ def test_batch_convbias(): ...@@ -297,7 +297,7 @@ def test_batch_convbias():
@trace(symbolic=True, capture_as_const=True) @trace(symbolic=True, capture_as_const=True)
def fwd(inp, weight, bias): def fwd(inp, weight, bias):
return F.quantized.batch_conv_bias_activation( return F.quantized.batch_conv_bias_activation(
inp, weight, bias, dtype=dtype.qint8(scale=1.0), nonlinear_mode="RELU" inp, weight, bias, dtype=dtype.qint8(scale=1.0), nonlinear_mode="relu"
) )
inp = Tensor(np.random.random((1, 3, 64, 64)), dtype=dtype.qint8(scale=1.0)) inp = Tensor(np.random.random((1, 3, 64, 64)), dtype=dtype.qint8(scale=1.0))
...@@ -358,7 +358,7 @@ def test_warpaffine(): ...@@ -358,7 +358,7 @@ def test_warpaffine():
@trace(symbolic=True, capture_as_const=True) @trace(symbolic=True, capture_as_const=True)
def fwd(x, weightv): def fwd(x, weightv):
return F.vision.warp_affine(x, weightv, (2, 2), border_mode="WRAP") return F.vision.warp_affine(x, weightv, (2, 2), border_mode="wrap")
outp = fwd(x, weightv) outp = fwd(x, weightv)
check_pygraph_dump(fwd, [x, weightv], [outp]) check_pygraph_dump(fwd, [x, weightv], [outp])
...@@ -387,7 +387,7 @@ def test_resize(): ...@@ -387,7 +387,7 @@ def test_resize():
@trace(symbolic=True, capture_as_const=True) @trace(symbolic=True, capture_as_const=True)
def fwd(x): def fwd(x):
return F.vision.interpolate(x, size=(16, 16), mode="BILINEAR") return F.vision.interpolate(x, size=(16, 16), mode="bilinear")
out = fwd(x) out = fwd(x)
check_pygraph_dump(fwd, [x], [out]) check_pygraph_dump(fwd, [x], [out])
...@@ -697,7 +697,7 @@ def test_assert_equal(): ...@@ -697,7 +697,7 @@ def test_assert_equal():
def test_elemwise_multitype(): def test_elemwise_multitype():
op = builtin.ElemwiseMultiType(mode="QADD", dtype=dtype.qint32(2.0)) op = builtin.ElemwiseMultiType(mode="qadd", dtype=dtype.qint32(2.0))
@trace(symbolic=True, capture_as_const=True) @trace(symbolic=True, capture_as_const=True)
def fwd(x, y): def fwd(x, y):
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册