forked from huawei/mindspore2022
delete duplication_code, and fix the format and line too long problem.
This commit is contained in:
parent
0fe1b5d9be
commit
3551ee5a27
|
|
@ -786,9 +786,9 @@ class Unfold(Cell):
|
|||
raise ValueError(f"For \'{prim_name}\' the format of {arg_name}s should be [1, {arg_name}_row, "
|
||||
f"{arg_name}_col, 1], but got {arg_val}.")
|
||||
if not isinstance(arg_val[1], int) or not isinstance(arg_val[2], int) or arg_val[1] < 1 or arg_val[2] < 1:
|
||||
raise ValueError(f"For '{prim_name}' the {arg_name}_row and {arg_name}_col in {arg_name}s should be an "
|
||||
f"positive integer number, but got {arg_name}_row is {arg_val[1]}, {arg_name}_col "
|
||||
f"is {arg_val[2]}")
|
||||
raise ValueError(f"For '{prim_name}' the {arg_name}_row and {arg_name}_col in {arg_name}s should be "
|
||||
f"an positive integer number, but got {arg_name}_row is {arg_val[1]}, "
|
||||
f"{arg_name}_col is {arg_val[2]}")
|
||||
|
||||
_check_tuple_or_list("ksize", ksizes, self.cls_name)
|
||||
_check_tuple_or_list("stride", strides, self.cls_name)
|
||||
|
|
|
|||
|
|
@ -18,7 +18,6 @@
|
|||
import numpy as np
|
||||
import mindspore as ms
|
||||
from mindspore.ops import composite as C
|
||||
from mindspore.common.tensor import Tensor
|
||||
from .. import operations as P
|
||||
from ..operations import _grad_ops as G
|
||||
from ..operations import _inner_ops as inner
|
||||
|
|
@ -30,6 +29,7 @@ from ..primitive import constexpr
|
|||
from ... import context
|
||||
from ...common import dtype as mstype
|
||||
from ...common.tensor import RowTensor
|
||||
from .._utils.utils import range_op, get_1d_shape
|
||||
|
||||
reduce_sum = P.ReduceSum()
|
||||
unsorted_segment_sum = P.UnsortedSegmentSum()
|
||||
|
|
@ -495,22 +495,6 @@ def get_bprop_sparse_gather_v2(self):
|
|||
return bprop
|
||||
|
||||
|
||||
@constexpr
|
||||
def _range_op(start, limit, delta, dtype):
|
||||
"""helper function for grad of Sort"""
|
||||
output_tensor = Tensor(list(range(start, limit, delta)), dtype)
|
||||
return output_tensor
|
||||
|
||||
|
||||
@constexpr
|
||||
def _get_1d_shape(in_shape):
|
||||
"""helper function for grad of Sort"""
|
||||
out_shape = 1
|
||||
for i in in_shape:
|
||||
out_shape *= i
|
||||
return (out_shape,)
|
||||
|
||||
|
||||
@constexpr
|
||||
def _get_transposition(axis, rank):
|
||||
"""helper function for grad of Sort"""
|
||||
|
|
@ -557,13 +541,12 @@ def get_bprop_sort(self):
|
|||
ind_2d = reshape_op(indices, (-1, ind_lastdim))
|
||||
outer_dim = ind_2d.shape[0]
|
||||
|
||||
# [0, outterdim, 2*outerdim, ..., (k-1)*outerdim]
|
||||
indices_dtype = dtype(indices)
|
||||
range_flatten_index = _range_op(0, outer_dim * in_lastdim, in_lastdim, indices_dtype)
|
||||
range_flatten_index = range_op(0, outer_dim * in_lastdim, in_lastdim, indices_dtype)
|
||||
|
||||
# expand_dims to (k, 1), then broadcast
|
||||
ind = reshape_op(ind_2d + expand_dims(range_flatten_index, -1), (-1,))
|
||||
x_shape_1d = _get_1d_shape(top_k_input_shape)
|
||||
x_shape_1d = get_1d_shape(top_k_input_shape)
|
||||
|
||||
if transposition is not None:
|
||||
dvalue = tranpose(dvalue, invert_permutation(transposition))
|
||||
|
|
|
|||
|
|
@ -417,6 +417,7 @@ def get_bprop_xlogy(self):
|
|||
|
||||
return bprop
|
||||
|
||||
|
||||
@bprop_getters.register(P.SquareSumAll)
|
||||
def get_bprop_square_sum_all(self):
|
||||
"""Grad definition for `Square` operation."""
|
||||
|
|
|
|||
|
|
@ -16,7 +16,6 @@
|
|||
"""Define the grad rules of neural network related operations."""
|
||||
import os
|
||||
from mindspore.ops.primitive import constexpr
|
||||
from mindspore.common.tensor import Tensor
|
||||
from mindspore.ops.operations import nn_ops as nps
|
||||
from .grad_base import bprop_getters
|
||||
from .. import functional as F
|
||||
|
|
@ -26,6 +25,7 @@ from ..composite.multitype_ops.zeros_like_impl import zeros_like
|
|||
from ..operations import _grad_ops as G
|
||||
from ..operations import _inner_ops as inner
|
||||
from ... import context
|
||||
from .._utils.utils import range_op, get_1d_shape
|
||||
|
||||
env_force_bprop_seq = os.getenv("ENV_FORCE_BPROP_SEQ")
|
||||
|
||||
|
|
@ -669,6 +669,7 @@ def get_bprop_fast_gelu_2(self):
|
|||
|
||||
return bprop
|
||||
|
||||
|
||||
@bprop_getters.register(P.InstanceNorm)
|
||||
def get_bprop_instance_norm(self):
|
||||
"""Grad definition for `InstanceNorm` operation."""
|
||||
|
|
@ -815,22 +816,6 @@ def get_bprop_onehot(self):
|
|||
return bprop
|
||||
|
||||
|
||||
@constexpr
|
||||
def _range_op(start, limit, delta, dtype):
|
||||
"""helper function for Grad TopK"""
|
||||
output_tensor = Tensor(list(range(start, limit, delta)), dtype)
|
||||
return output_tensor
|
||||
|
||||
|
||||
@constexpr
|
||||
def _get_1d_shape(in_shape):
|
||||
"""helper function for Grad TopK"""
|
||||
out_shape = 1
|
||||
for i in in_shape:
|
||||
out_shape *= i
|
||||
return (out_shape,)
|
||||
|
||||
|
||||
@bprop_getters.register(P.TopK)
|
||||
def get_bprop_top_kv2(self):
|
||||
"""Grad definition for `TopK` operation."""
|
||||
|
|
@ -853,11 +838,11 @@ def get_bprop_top_kv2(self):
|
|||
|
||||
# [0, outterdim, 2*outerdim, ..., (k-1)*outerdim]
|
||||
indices_dtype = dtype(indices)
|
||||
range_flatten_index = _range_op(0, outerdim * in_lastdim, in_lastdim, indices_dtype)
|
||||
range_flatten_index = range_op(0, outerdim * in_lastdim, in_lastdim, indices_dtype)
|
||||
|
||||
# expand_dims to (k, 1), then broadcast
|
||||
ind = reshape_op(ind_2d + expand_dims(range_flatten_index, -1), (-1,))
|
||||
in_shape_1d = _get_1d_shape(in_shape)
|
||||
in_shape_1d = get_1d_shape(in_shape)
|
||||
|
||||
out_grad = reshape_op(
|
||||
scatter(
|
||||
|
|
|
|||
|
|
@ -34,6 +34,7 @@ def get_bprop_fakequant_with_minmax(self):
|
|||
|
||||
return bprop
|
||||
|
||||
|
||||
@bprop_getters.register(Q.FakeQuantWithMinMaxVars)
|
||||
def get_bprop_fakequant_with_minmax_vars(self):
|
||||
"""Generate bprop for FakeQuantWithMinMaxVars for Ascend"""
|
||||
|
|
@ -184,6 +185,7 @@ def get_bprop_acts_ulq(self):
|
|||
op = Q.ActsULQInputGrad()
|
||||
op1 = Q.ActULQClampMinGrad()
|
||||
op2 = Q.ActULQClampMaxGrad()
|
||||
|
||||
def bprop(x, clamp_min, clamp_max, out, dout):
|
||||
dx = op(dout[0], out[1], out[2])
|
||||
dx1 = op1(dout[0], out[1], out[3])
|
||||
|
|
|
|||
|
|
@ -23,6 +23,7 @@ class Registry(UserDict):
|
|||
"""Registry class for registry functions for grad and vm_impl on Primitive."""
|
||||
|
||||
def register(self, prim):
|
||||
"""register the function."""
|
||||
def deco(fn):
|
||||
"""Decorate the function."""
|
||||
if isinstance(prim, str):
|
||||
|
|
|
|||
|
|
@ -15,9 +15,11 @@
|
|||
|
||||
"""utils for operator"""
|
||||
|
||||
from mindspore.common.tensor import Tensor
|
||||
from ..._checkparam import Validator as validator
|
||||
from ..._checkparam import Rel
|
||||
from ...common import dtype as mstype
|
||||
from ..primitive import constexpr
|
||||
|
||||
|
||||
def get_broadcast_shape(x_shape, y_shape, prim_name):
|
||||
|
|
@ -89,3 +91,19 @@ def get_concat_offset(x_shp, x_type, axis, prim_name):
|
|||
else:
|
||||
all_shp += v[axis]
|
||||
return offset, all_shp, axis
|
||||
|
||||
|
||||
@constexpr
|
||||
def range_op(start, limit, delta, dtype):
|
||||
"""helper function to get tensor in specified range."""
|
||||
output_tensor = Tensor(list(range(start, limit, delta)), dtype)
|
||||
return output_tensor
|
||||
|
||||
|
||||
@constexpr
|
||||
def get_1d_shape(in_shape):
|
||||
"""helper function to get 1d shape."""
|
||||
out_shape = 1
|
||||
for i in in_shape:
|
||||
out_shape *= i
|
||||
return (out_shape,)
|
||||
|
|
|
|||
|
|
@ -194,9 +194,9 @@ class GradOperation(GradOperation_):
|
|||
sens_param (bool): Whether to append sensitivity (gradient with respect to output) as input.
|
||||
If sens_param is False, a 'ones_like(outputs)' sensitivity will be attached automatically.
|
||||
Default: False.
|
||||
If the sensor_param is True, a sensitivity (gradient with respect to output) needs to be transferred through
|
||||
the location parameter or key-value pair parameter. If the value is transferred through the key-value pair
|
||||
parameter, the key must be sens.
|
||||
If the sensor_param is True, a sensitivity (gradient with respect to output) needs to be transferred
|
||||
through the location parameter or key-value pair parameter. If the value is transferred through
|
||||
the key-value pair parameter, the key must be sens.
|
||||
|
||||
Returns:
|
||||
The higher-order function which takes a function as argument and returns gradient function for it.
|
||||
|
|
|
|||
|
|
@ -441,6 +441,7 @@ def _check_batch_size(x1_batch_size, x2_batch_size):
|
|||
if x1_batch_size != x2_batch_size:
|
||||
raise ValueError("Require both inputs with the same batch sizes.")
|
||||
|
||||
|
||||
@constexpr
|
||||
def _get_output_shape(batch_size, x1_ret, x2_ret):
|
||||
"""
|
||||
|
|
@ -543,20 +544,24 @@ def batch_dot(x1, x2, axes=None):
|
|||
|
||||
return final_result
|
||||
|
||||
|
||||
@constexpr
|
||||
def _check_same_type(dtype1, dtype2):
|
||||
return dtype1 == dtype2
|
||||
|
||||
|
||||
@constexpr
|
||||
def _max(*args):
|
||||
"""Returns the maximum value."""
|
||||
return max(*args)
|
||||
|
||||
|
||||
@constexpr
|
||||
def _min(*args):
|
||||
"""Returns the minimum value."""
|
||||
return min(*args)
|
||||
|
||||
|
||||
@constexpr
|
||||
def _infer_shape_rem(shape1, shape2, ndim1, ndim2, transpose_b):
|
||||
"""Infers the shape of the last two dimensions after performing matmul."""
|
||||
|
|
@ -571,6 +576,7 @@ def _infer_shape_rem(shape1, shape2, ndim1, ndim2, transpose_b):
|
|||
shape_rem.append(shape2[-1])
|
||||
return tuple(shape_rem)
|
||||
|
||||
|
||||
@constexpr
|
||||
def _check_matmul_shapes(shape1, shape2):
|
||||
"""Checks shape1 and shape2 are valid to perform matmul, and returns output shape after broadcasting."""
|
||||
|
|
@ -588,6 +594,7 @@ def _check_matmul_shapes(shape1, shape2):
|
|||
shape_out.appendleft(max_size)
|
||||
return tuple(shape_out)
|
||||
|
||||
|
||||
@constexpr
|
||||
def _tile_size(shape, out_shape, ndim):
|
||||
"""Returns tile_size such that shape*tile_size = out_shape"""
|
||||
|
|
@ -597,22 +604,26 @@ def _tile_size(shape, out_shape, ndim):
|
|||
size[idx] = j
|
||||
return tuple(size)
|
||||
|
||||
|
||||
@constexpr
|
||||
def _check_need_broadcast(shape1, shape2):
|
||||
"""Returns True if broadcast is necessary for batchmatmul."""
|
||||
return shape1[:-2] != shape2[:-2]
|
||||
|
||||
|
||||
def _expand(x, ndim):
|
||||
"""Expand x to ndim from axis, which can be 0 or -1."""
|
||||
while F.rank(x) < ndim:
|
||||
x = F.expand_dims(x, 0)
|
||||
return x
|
||||
|
||||
|
||||
def _broadcast_to(x, shape_cur, shape_to, ndim_to):
|
||||
"""Broadcasts x from shape_cur to shape_to."""
|
||||
size = _tile_size(shape_cur, shape_to, ndim_to)
|
||||
return F.tile(x, size)
|
||||
|
||||
|
||||
def matmul(x1, x2, dtype=None):
|
||||
"""
|
||||
Returns the matrix product of two arrays.
|
||||
|
|
|
|||
|
|
@ -217,8 +217,10 @@ def _transform_indexing_tensor(broadcast_shape, final_shape, new_shape, item):
|
|||
|
||||
|
||||
def _transform_ellipsis_to_slice(data, tuple_index, op_name):
|
||||
"""Check if the tuple index len is longer than the data's dims and transform ellipsis in the indices
|
||||
to several slice"""
|
||||
"""
|
||||
Check if the tuple index len is longer than the data's dims and transform ellipsis in the indices
|
||||
to several slice.
|
||||
"""
|
||||
data_shape = F.shape(data)
|
||||
data_rank = len(data_shape)
|
||||
indexes_types = hyper_map(F.typeof, tuple_index)
|
||||
|
|
|
|||
|
|
@ -268,6 +268,7 @@ def _add_umonad_umonad(x, y):
|
|||
"""
|
||||
return x
|
||||
|
||||
|
||||
@_add_backward.register("IOMonad", "IOMonad")
|
||||
def _add_iomonad_iomonad(x, y):
|
||||
"""
|
||||
|
|
@ -282,6 +283,7 @@ def _add_iomonad_iomonad(x, y):
|
|||
"""
|
||||
return x
|
||||
|
||||
|
||||
@_add_backward.register("RowTensor", "Tensor")
|
||||
def _add_rowtensor_tensor(x, y):
|
||||
"""
|
||||
|
|
|
|||
|
|
@ -36,6 +36,7 @@ def _greater_equal_scala(x, y):
|
|||
"""
|
||||
return F.scalar_ge(x, y)
|
||||
|
||||
|
||||
@greater_equal.register("Tensor", "Number")
|
||||
@greater_equal.register("Number", "Tensor")
|
||||
@greater_equal.register("Tensor", "Tensor")
|
||||
|
|
|
|||
|
|
@ -36,6 +36,7 @@ def _greater_scalar(x, y):
|
|||
"""
|
||||
return F.scalar_gt(x, y)
|
||||
|
||||
|
||||
@greater.register("Tensor", "Number")
|
||||
@greater.register("Number", "Tensor")
|
||||
@greater.register("Tensor", "Tensor")
|
||||
|
|
|
|||
|
|
@ -36,6 +36,7 @@ def _less_equal_scala(x, y):
|
|||
"""
|
||||
return F.scalar_le(x, y)
|
||||
|
||||
|
||||
@less_equal.register("Tensor", "Number")
|
||||
@less_equal.register("Number", "Tensor")
|
||||
@less_equal.register("Tensor", "Tensor")
|
||||
|
|
|
|||
|
|
@ -36,6 +36,7 @@ def _less_scala(x, y):
|
|||
"""
|
||||
return F.scalar_lt(x, y)
|
||||
|
||||
|
||||
@less.register("Tensor", "Number")
|
||||
@less.register("Number", "Tensor")
|
||||
@less.register("Tensor", "Tensor")
|
||||
|
|
|
|||
|
|
@ -20,6 +20,7 @@ from mindspore.ops.composite import base
|
|||
# using ".register" decorator
|
||||
uadd = base.MultitypeFuncGraph("uadd", True)
|
||||
|
||||
|
||||
@uadd.register("Tensor")
|
||||
@uadd.register("Number")
|
||||
def _uadd_scala(x):
|
||||
|
|
|
|||
|
|
@ -382,7 +382,8 @@ class TBERegOp(RegOp):
|
|||
|
||||
def compute_cost(self, compute_cost):
|
||||
"""
|
||||
Define the calculation efficiency of operator, which refers to the value of the cost model in the tiling module.
|
||||
Define the calculation efficiency of operator, which refers to the value of the cost model
|
||||
in the tiling module.
|
||||
|
||||
Args:
|
||||
compute_cost (int): Value of compute cost. Default: 10.
|
||||
|
|
|
|||
|
|
@ -31,9 +31,9 @@ from .array_ops import (Argmax, Argmin, Cast, Concat, Pack, Stack, Unpack, Unsta
|
|||
ScatterNdAdd, ScatterNdSub, ScatterNonAliasingAdd, ReverseV2, Rint,
|
||||
Squeeze, StridedSlice, Tile, TensorScatterUpdate, EditDistance, Sort,
|
||||
Transpose, TruncatedNormal, TupleToArray, UnsortedSegmentMin, UnsortedSegmentMax,
|
||||
UnsortedSegmentProd, UnsortedSegmentSum, SpaceToDepth, DepthToSpace, SpaceToBatch, BatchToSpace,
|
||||
SpaceToBatchND, BatchToSpaceND, BroadcastTo, InplaceUpdate, ReverseSequence, EmbeddingLookup,
|
||||
Unique, GatherD, Identity, Range)
|
||||
UnsortedSegmentProd, UnsortedSegmentSum, SpaceToDepth, DepthToSpace, SpaceToBatch,
|
||||
BatchToSpace, SpaceToBatchND, BatchToSpaceND, BroadcastTo, InplaceUpdate, ReverseSequence,
|
||||
EmbeddingLookup, Unique, GatherD, Identity, Range)
|
||||
from .comm_ops import (AllGather, AllReduce, _AlltoAll, AllSwap, ReduceScatter, Broadcast,
|
||||
_MirrorOperator, _MirrorMiniStepOperator, _MiniStepAllGather, ReduceOp, _VirtualDataset,
|
||||
_VirtualOutput, _VirtualDiv, _GetTensorSlice, _VirtualAdd,
|
||||
|
|
@ -63,8 +63,9 @@ from .math_ops import (Abs, ACos, Asin, Asinh, AddN, AccumulateNV2, AssignAdd, A
|
|||
from .random_ops import (RandomChoiceWithMask, StandardNormal, Gamma, Poisson, UniformInt, UniformReal,
|
||||
RandomCategorical, StandardLaplace, Multinomial, UniformCandidateSampler,
|
||||
LogUniformCandidateSampler)
|
||||
from .nn_ops import (LSTM, SGD, Adam, FusedSparseAdam, FusedSparseLazyAdam, AdamNoUpdateParam, ApplyMomentum, BatchNorm,
|
||||
BiasAdd, Conv2D, Conv3D, Conv3DTranspose,
|
||||
|
||||
from .nn_ops import (LSTM, SGD, Adam, FusedSparseAdam, FusedSparseLazyAdam, AdamNoUpdateParam, ApplyMomentum,
|
||||
BatchNorm, BiasAdd, Conv2D, Conv3D, Conv3DTranspose,
|
||||
DepthwiseConv2dNative,
|
||||
DropoutDoMask, Dropout, Dropout2D, Dropout3D, DropoutGenMask, Flatten,
|
||||
InstanceNorm, BNTrainingReduce, BNTrainingUpdate,
|
||||
|
|
|
|||
|
|
@ -148,7 +148,8 @@ class RsqrtGrad(PrimitiveWithInfer):
|
|||
|
||||
def infer_dtype(self, x_dtype, dout_dtype):
|
||||
args = {"x": x_dtype, "dout": dout_dtype}
|
||||
validator.check_tensors_dtypes_same_and_valid(args, [mstype.float16, mstype.float32, mstype.int32, mstype.int8],
|
||||
validator.check_tensors_dtypes_same_and_valid(args,
|
||||
[mstype.float16, mstype.float32, mstype.int32, mstype.int8],
|
||||
self.name)
|
||||
return x_dtype
|
||||
|
||||
|
|
@ -971,6 +972,7 @@ def _get_max_pool3d_grad_pads_by_pad_mode(input_shape, kernel_size, strides, pad
|
|||
pads = pads_d + pads_h + pads_w
|
||||
return pads
|
||||
|
||||
|
||||
class MaxPool3DGrad(PrimitiveWithInfer):
|
||||
"""Gradients of the max pool3d operation."""
|
||||
|
||||
|
|
@ -981,7 +983,8 @@ class MaxPool3DGrad(PrimitiveWithInfer):
|
|||
validator.check_value_type('pad_mode', pad_mode, [str], self.name)
|
||||
self.format = validator.check_string(data_format, ['NCDHW'], 'format', self.name)
|
||||
self.pad_mode = validator.check_string(pad_mode.upper(), ['VALID', 'SAME'], 'pad_mode', self.name)
|
||||
self.kernel_size = _check_3d_int_or_tuple("kernel_size", kernel_size, self.name, allow_five=True, ret_five=True)
|
||||
self.kernel_size = _check_3d_int_or_tuple("kernel_size", kernel_size, self.name,
|
||||
allow_five=True, ret_five=True)
|
||||
self.add_prim_attr("kernel_size", self.kernel_size)
|
||||
self.strides = _check_3d_int_or_tuple("strides", strides, self.name, allow_five=True, ret_five=True)
|
||||
self.add_prim_attr("strides", self.strides)
|
||||
|
|
@ -1006,7 +1009,8 @@ class MaxPool3DGradGrad(PrimitiveWithInfer):
|
|||
validator.check_value_type('pad_mode', pad_mode, [str], self.name)
|
||||
self.format = validator.check_string(data_format, ['NCDHW'], 'format', self.name)
|
||||
self.pad_mode = validator.check_string(pad_mode.upper(), ['VALID', 'SAME'], 'pad_mode', self.name)
|
||||
self.kernel_size = _check_3d_int_or_tuple("kernel_size", kernel_size, self.name, allow_five=True, ret_five=True)
|
||||
self.kernel_size = _check_3d_int_or_tuple("kernel_size", kernel_size, self.name,
|
||||
allow_five=True, ret_five=True)
|
||||
self.add_prim_attr("kernel_size", self.kernel_size)
|
||||
self.strides = _check_3d_int_or_tuple("strides", strides, self.name, allow_five=True, ret_five=True)
|
||||
self.add_prim_attr("strides", self.strides)
|
||||
|
|
@ -1248,12 +1252,10 @@ class LSTMGradData(PrimitiveWithInfer):
|
|||
validator.check_int(dhy_shape[0], self.num_layers * self.num_directions, Rel.EQ, "h_shape[0]", self.name)
|
||||
validator.check_equal_int(dhy_shape[2], self.hidden_size, "h_shape[2]", self.name)
|
||||
|
||||
# dy: (seq_len, batch_size, hidden_size * num_directions)
|
||||
validator.check_equal_int(len(dy_shape), 3, "dy_shape", self.name)
|
||||
validator.check_equal_int(dy_shape[1], dhy_shape[1], "dy[1]", self.name)
|
||||
validator.check_int(dy_shape[2], self.hidden_size * self.num_directions, Rel.EQ, "dy[2]", self.name)
|
||||
|
||||
# (seq_len, batch_size, input_size)
|
||||
dx_shape = (y_shape[0], y_shape[1], self.input_size)
|
||||
dhx_shape = dhy_shape
|
||||
dcx_shape = dcy_shape
|
||||
|
|
@ -1332,12 +1334,10 @@ class LSTMGrad(PrimitiveWithInfer):
|
|||
validator.check_int(dhy_shape[0], self.num_layers * self.num_directions, Rel.EQ, "h_shape[0]", self.name)
|
||||
validator.check_equal_int(dhy_shape[2], self.hidden_size, "h_shape[2]", self.name)
|
||||
|
||||
# dy: (seq_len, batch_size, hidden_size * num_directions)
|
||||
validator.check_equal_int(len(dy_shape), 3, "dy_shape", self.name)
|
||||
validator.check_equal_int(dy_shape[1], dhy_shape[1], "dy[1]", self.name)
|
||||
validator.check_int(dy_shape[2], self.hidden_size * self.num_directions, Rel.EQ, "dy[2]", self.name)
|
||||
|
||||
# (seq_len, batch_size, input_size)
|
||||
dx_shape = (y_shape[0], y_shape[1], self.input_size)
|
||||
dhx_shape = dhy_shape
|
||||
dcx_shape = dcy_shape
|
||||
|
|
@ -1561,16 +1561,16 @@ class PReLUGrad(PrimitiveWithInfer):
|
|||
def __init__(self):
|
||||
pass
|
||||
|
||||
def infer_shape(self, y_backprop_shape, A_shape, w_shape):
|
||||
if len(A_shape) == 1:
|
||||
def infer_shape(self, y_backprop_shape, a_shape, w_shape):
|
||||
if len(a_shape) == 1:
|
||||
raise ValueError(f'For \'{self.name}\' input_x rank 1 is not supported.')
|
||||
return y_backprop_shape, w_shape
|
||||
|
||||
def infer_dtype(self, y_backprop_dtype, A_dtype, w_dtype):
|
||||
def infer_dtype(self, y_backprop_dtype, a_dtype, w_dtype):
|
||||
tuple(map(partial(validator.check_tensor_dtype_valid,
|
||||
valid_dtypes=(mstype.float16, mstype.float32), prim_name=self.name),
|
||||
('y_backprop', "input_x", "weight"),
|
||||
(y_backprop_dtype, A_dtype, w_dtype)))
|
||||
(y_backprop_dtype, a_dtype, w_dtype)))
|
||||
return y_backprop_dtype, w_dtype
|
||||
|
||||
|
||||
|
|
@ -1746,8 +1746,8 @@ class SigmoidGrad(PrimitiveWithInfer):
|
|||
return out
|
||||
|
||||
|
||||
class HSigmoidGrad(PrimitiveWithInfer):
|
||||
"""Gets the gradient of HSigmoid operation."""
|
||||
class _ActivationGrad(PrimitiveWithInfer):
|
||||
"""_ActivationGrad base class."""
|
||||
|
||||
@prim_attr_register
|
||||
def __init__(self):
|
||||
|
|
@ -1763,21 +1763,12 @@ class HSigmoidGrad(PrimitiveWithInfer):
|
|||
return x_dtype
|
||||
|
||||
|
||||
class HSwishGrad(PrimitiveWithInfer):
|
||||
class HSwishGrad(_ActivationGrad):
|
||||
"""Gets the gradient of HSwish operation."""
|
||||
|
||||
@prim_attr_register
|
||||
def __init__(self):
|
||||
self.init_prim_io_names(inputs=['y_grad', 'x'], outputs=['output'])
|
||||
|
||||
def infer_shape(self, y_grad_shape, x_shape):
|
||||
return x_shape
|
||||
|
||||
def infer_dtype(self, y_grad_dtype, x_dtype):
|
||||
valid_dtypes = (mstype.float16, mstype.float32)
|
||||
validator.check_tensor_dtype_valid("y_grad", y_grad_dtype, valid_dtypes, self.name)
|
||||
validator.check_tensor_dtype_valid("x", x_dtype, valid_dtypes, self.name)
|
||||
return x_dtype
|
||||
class HSigmoidGrad(_ActivationGrad):
|
||||
"""Gets the gradient of HSigmoid operation."""
|
||||
|
||||
|
||||
class SigmoidCrossEntropyWithLogitsGrad(PrimitiveWithInfer):
|
||||
|
|
|
|||
|
|
@ -65,9 +65,9 @@ class ExtractImagePatches(PrimitiveWithInfer):
|
|||
raise ValueError(f"For \'{prim_name}\' the format of {arg_name}s should be [1, {arg_name}_row, "
|
||||
f"{arg_name}_col, 1], but got {arg_val}.")
|
||||
if not isinstance(arg_val[2], int) or not isinstance(arg_val[3], int) or arg_val[2] < 1 or arg_val[3] < 1:
|
||||
raise ValueError(f"For '{prim_name}' the {arg_name}_row and {arg_name}_col in {arg_name}s should be an "
|
||||
f"positive integer number, but got {arg_name}_row is {arg_val[2]}, {arg_name}_col "
|
||||
f"is {arg_val[3]}")
|
||||
raise ValueError(f"For '{prim_name}' the {arg_name}_row and {arg_name}_col in {arg_name}s should be "
|
||||
f"an positive integer number, but got {arg_name}_row is {arg_val[2]}, "
|
||||
f"{arg_name}_col is {arg_val[3]}")
|
||||
|
||||
_check_tuple_or_list("ksize", ksizes, self.name)
|
||||
_check_tuple_or_list("stride", strides, self.name)
|
||||
|
|
|
|||
|
|
@ -1279,7 +1279,8 @@ class BatchNormFold2(PrimitiveWithInfer):
|
|||
validator.check("batch_std shape", batch_std_shape, "running_std shape", running_std_shape, Rel.EQ, self.name)
|
||||
validator.check("batch_std shape", batch_std_shape, "batch_mean shape", batch_mean_shape, Rel.EQ, self.name)
|
||||
validator.check("batch_std shape", batch_std_shape, "beta shape", beta_shape, Rel.EQ, self.name)
|
||||
validator.check("batch_std shape", batch_std_shape, "running_mean shape", running_mean_shape, Rel.EQ, self.name)
|
||||
validator.check("batch_std shape", batch_std_shape, "running_mean shape", running_mean_shape,
|
||||
Rel.EQ, self.name)
|
||||
validator.check("batch_std shape", batch_std_shape, "batch_mean shape", gamma_shape, Rel.EQ, self.name)
|
||||
validator.check("batch_std_shape[0]", batch_std_shape[0], "x_shape channel size", x_shape[self.channel_axis],
|
||||
Rel.EQ, self.name)
|
||||
|
|
@ -1327,7 +1328,8 @@ class BatchNormFold2Grad(PrimitiveWithInfer):
|
|||
running_std_shape, running_mean_shape, global_step_shape):
|
||||
validator.check("batch_std shape", batch_std_shape, "batch_mean shape", batch_mean_shape, Rel.EQ, self.name)
|
||||
validator.check("batch_std shape", batch_std_shape, "running_std shape", running_std_shape, Rel.EQ, self.name)
|
||||
validator.check("batch_std shape", batch_std_shape, "running_mean shape", running_mean_shape, Rel.EQ, self.name)
|
||||
validator.check("batch_std shape", batch_std_shape, "running_mean shape", running_mean_shape,
|
||||
Rel.EQ, self.name)
|
||||
validator.check("batch_std shape", batch_std_shape, "gamma shape", gamma_shape, Rel.EQ, self.name)
|
||||
validator.check("batch_std size", batch_std_shape[0], "dout channel size", dout_shape[self.channel_axis],
|
||||
Rel.EQ, self.name)
|
||||
|
|
|
|||
|
|
@ -1,4 +1,4 @@
|
|||
# Copyright 2020 Huawei Technologies Co., Ltd
|
||||
# Copyright 2020-2021 Huawei Technologies Co., Ltd
|
||||
#
|
||||
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||
# you may not use this file except in compliance with the License.
|
||||
|
|
@ -18,6 +18,7 @@ import math
|
|||
from ..primitive import prim_attr_register, PrimitiveWithInfer
|
||||
from ...common import dtype as mstype
|
||||
from ..._checkparam import Validator as validator
|
||||
from ..operations.nn_ops import _check_positive_int_or_tuple
|
||||
from ..._checkparam import Rel
|
||||
|
||||
__all__ = ["CusBatchMatMul",
|
||||
|
|
@ -34,37 +35,6 @@ __all__ = ["CusBatchMatMul",
|
|||
]
|
||||
|
||||
|
||||
def _check_positive_int_or_tuple(arg_name, arg_value, prim_name, allow_four=False, ret_four=False):
|
||||
"""
|
||||
Checks whether an argument is a positive int or tuple with 2 or 4(when allow_four is True) positive int elements.
|
||||
"""
|
||||
|
||||
def _raise_message():
|
||||
raise ValueError(f"For '{prim_name}' attr '{arg_name}' should be an positive int number or a tuple of two "
|
||||
f"{'or four ' if allow_four else ''}positive int numbers, but got {arg_value}")
|
||||
|
||||
def _get_return_value():
|
||||
if isinstance(arg_value, int):
|
||||
ret = (1, 1, arg_value, arg_value) if ret_four else (arg_value, arg_value)
|
||||
elif len(arg_value) == 2:
|
||||
ret = (1, 1, arg_value[0], arg_value[1]) if ret_four else arg_value
|
||||
elif len(arg_value) == 4:
|
||||
if not allow_four:
|
||||
_raise_message()
|
||||
ret = arg_value if ret_four else (arg_value[2], arg_value[3])
|
||||
else:
|
||||
_raise_message()
|
||||
return ret
|
||||
|
||||
validator.check_value_type(arg_name, arg_value, (int, tuple), prim_name)
|
||||
ret_value = _get_return_value()
|
||||
for item in ret_value:
|
||||
if isinstance(item, int) and item > 0:
|
||||
continue
|
||||
_raise_message()
|
||||
return ret_value
|
||||
|
||||
|
||||
class CusBatchMatMul(PrimitiveWithInfer):
|
||||
"""
|
||||
Multiplies matrix `a` by matrix `b` in batch.
|
||||
|
|
@ -205,7 +175,6 @@ class CusImg2Col(PrimitiveWithInfer):
|
|||
bs, c, h, w = data1_shape
|
||||
_, stride_h, stride_w, _ = self.strides
|
||||
_, k_w, k_h, _ = self.ksizes
|
||||
# assert m == n
|
||||
c0 = 16
|
||||
c1 = c // 16
|
||||
if c1 == 0:
|
||||
|
|
@ -483,7 +452,7 @@ class CusMatMulCubeFraczLeftCast(PrimitiveWithInfer):
|
|||
|
||||
class Im2Col(PrimitiveWithInfer):
|
||||
"""
|
||||
extracts image pathes from image.
|
||||
extracts image paths from image.
|
||||
|
||||
The rank of input_x1 must be `4`, data_format is "NCHW".
|
||||
|
||||
|
|
@ -574,7 +543,8 @@ class UpdateThorGradient(PrimitiveWithInfer):
|
|||
- **input_x1** (Tensor) - The first input is the diag part of the cov matrix of feature map.
|
||||
Supported dtype [float32].
|
||||
- **input_x2** (Tensor) - The second input is the corresponding 1st-order grad. Supported dtype [float32].
|
||||
- **input_x3** (Tensor) - The third input is the diag part of the cov matrix of dout. Supported dtype [float32].
|
||||
- **input_x3** (Tensor) - The third input is the diag part of the cov matrix of dout.
|
||||
Supported dtype [float32].
|
||||
|
||||
Outputs:
|
||||
Tensor, the shape is the same as the shape of input_x2, it will be used to update the weights.
|
||||
|
|
@ -608,71 +578,50 @@ class UpdateThorGradient(PrimitiveWithInfer):
|
|||
return x2_dtype
|
||||
|
||||
|
||||
class Cholesky(PrimitiveWithInfer):
|
||||
class _Cholesky(PrimitiveWithInfer):
|
||||
"""
|
||||
Inner API for _Cholesky base class.
|
||||
"""
|
||||
|
||||
@prim_attr_register
|
||||
def __init__(self, split_dim=0):
|
||||
self.init_prim_io_names(inputs=['x1'], outputs=['y'])
|
||||
self.split_dim = split_dim
|
||||
self.add_prim_attr('split_dim', self.split_dim)
|
||||
|
||||
def infer_shape(self, x1_shape):
|
||||
if self.split_dim != 0:
|
||||
assert len(x1_shape) == 2
|
||||
height = x1_shape[0]
|
||||
width = x1_shape[1]
|
||||
assert height == width
|
||||
if height <= self.split_dim:
|
||||
out_shape = [1, height, width]
|
||||
else:
|
||||
batch = height // self.split_dim
|
||||
if height != batch * self.split_dim:
|
||||
batch += 1
|
||||
out_shape = [batch, self.split_dim, self.split_dim]
|
||||
else:
|
||||
out_shape = x1_shape
|
||||
return out_shape
|
||||
|
||||
def infer_dtype(self, x1_dtype):
|
||||
validator.check_tensor_dtype_valid('x1', x1_dtype, [mstype.float32], self.name)
|
||||
return x1_dtype
|
||||
|
||||
|
||||
class Cholesky(_Cholesky):
|
||||
"""
|
||||
Inner API for positive-definite matrix Cholesky decomposition GPU backend.
|
||||
"""
|
||||
|
||||
@prim_attr_register
|
||||
def __init__(self, split_dim=0):
|
||||
self.init_prim_io_names(inputs=['x1'], outputs=['y'])
|
||||
self.split_dim = split_dim
|
||||
self.add_prim_attr('split_dim', self.split_dim)
|
||||
|
||||
def infer_shape(self, x1_shape):
|
||||
if self.split_dim != 0:
|
||||
assert len(x1_shape) == 2
|
||||
height = x1_shape[0]
|
||||
width = x1_shape[1]
|
||||
assert height == width
|
||||
if height <= self.split_dim:
|
||||
out_shape = [1, height, width]
|
||||
else:
|
||||
batch = height // self.split_dim
|
||||
if height != batch * self.split_dim:
|
||||
batch += 1
|
||||
out_shape = [batch, self.split_dim, self.split_dim]
|
||||
else:
|
||||
out_shape = x1_shape
|
||||
return out_shape
|
||||
|
||||
def infer_dtype(self, x1_dtype):
|
||||
validator.check_tensor_dtype_valid('x1', x1_dtype, [mstype.float32], self.name)
|
||||
return x1_dtype
|
||||
|
||||
|
||||
class CholeskyTrsm(PrimitiveWithInfer):
|
||||
class CholeskyTrsm(_Cholesky):
|
||||
"""
|
||||
Inner API for resnet50 THOR GPU backend.
|
||||
"""
|
||||
|
||||
@prim_attr_register
|
||||
def __init__(self, split_dim=0):
|
||||
self.init_prim_io_names(inputs=['x1'], outputs=['y'])
|
||||
self.split_dim = split_dim
|
||||
self.add_prim_attr('split_dim', self.split_dim)
|
||||
|
||||
def infer_shape(self, x1_shape):
|
||||
if self.split_dim != 0:
|
||||
assert len(x1_shape) == 2
|
||||
height = x1_shape[0]
|
||||
width = x1_shape[1]
|
||||
assert height == width
|
||||
if height <= self.split_dim:
|
||||
out_shape = [1, height, width]
|
||||
else:
|
||||
batch = height // self.split_dim
|
||||
if height != batch * self.split_dim:
|
||||
batch += 1
|
||||
out_shape = [batch, self.split_dim, self.split_dim]
|
||||
else:
|
||||
out_shape = x1_shape
|
||||
return out_shape
|
||||
|
||||
def infer_dtype(self, x1_dtype):
|
||||
validator.check_tensor_dtype_valid('x1', x1_dtype, [mstype.float32], self.name)
|
||||
return x1_dtype
|
||||
|
||||
|
||||
class DetTriangle(PrimitiveWithInfer):
|
||||
"""
|
||||
|
|
|
|||
|
|
@ -2362,16 +2362,16 @@ def _get_stack_shape(x_shape, x_type, axis, prim_name):
|
|||
validator.check_int(len(x_shape), 1, Rel.GE, "len of input_x", prim_name)
|
||||
validator.check_subclass("input_x[0]", x_type[0], mstype.tensor, prim_name)
|
||||
rank_base = len(x_shape[0])
|
||||
N = len(x_shape)
|
||||
n = len(x_shape)
|
||||
out_shape = x_shape[0]
|
||||
validator.check_int_range(axis, -rank_base - 1, rank_base, Rel.INC_BOTH, 'axis', prim_name)
|
||||
if axis < 0:
|
||||
axis = axis + rank_base + 1
|
||||
for i in range(1, N):
|
||||
for i in range(1, n):
|
||||
validator.check('x_type[%d]' % i, x_type[i], 'base', x_type[0], Rel.EQ, prim_name, TypeError)
|
||||
if x_shape[i] != x_shape[0]:
|
||||
raise ValueError(f"For \'{prim_name}\' element {i} shape in input can not pack with first element")
|
||||
out_shape.insert(axis, N)
|
||||
out_shape.insert(axis, n)
|
||||
return out_shape
|
||||
|
||||
|
||||
|
|
@ -2406,7 +2406,8 @@ class Stack(PrimitiveWithInfer):
|
|||
Stacks the list of input tensors with the same rank `R`, output is a tensor of rank `(R+1)`.
|
||||
|
||||
Given input tensors of shape :math:`(x_1, x_2, ..., x_R)`. Set the number of input tensors as `N`.
|
||||
If :math:`0 \le axis`, the shape of the output tensor is :math:`(x_1, x_2, ..., x_{axis}, N, x_{axis+1}, ..., x_R)`.
|
||||
If :math:`0 \le axis`, the shape of the output tensor is
|
||||
:math:`(x_1, x_2, ..., x_{axis}, N, x_{axis+1}, ..., x_R)`.
|
||||
|
||||
Args:
|
||||
axis (int): Dimension to stack. Default: 0.
|
||||
|
|
@ -2848,11 +2849,9 @@ def _compute_slicing_length(begin, end, stride, x_shape, i):
|
|||
if 0 <= end < x_dim:
|
||||
end += -x_dim
|
||||
if end < -x_dim - 1:
|
||||
# When slicing backward, if end < -x_dim - 1, set end = -x_dim - 1, which means
|
||||
# slicing to the 0th element.
|
||||
# Slicing to the 0th element.
|
||||
end = -x_dim - 1
|
||||
if begin <= end:
|
||||
# When slicing backward, if begin <= end, the length of the slicing is 0.
|
||||
slicing_length = 0
|
||||
else:
|
||||
slicing_length = 1 + (end + 1 - begin) // stride
|
||||
|
|
@ -3250,7 +3249,8 @@ class ScatterNd(PrimitiveWithInfer):
|
|||
r"""
|
||||
Scatters a tensor into a new tensor depending on the specified indices.
|
||||
|
||||
Creates an empty tensor with the given `shape`, and set values by scattering the update tensor depending on indices.
|
||||
Creates an empty tensor with the given `shape`, and set values by scattering the update tensor
|
||||
depending on indices.
|
||||
|
||||
The empty tensor has rank P and `indices` has rank Q where `Q >= 2`.
|
||||
|
||||
|
|
@ -3642,7 +3642,8 @@ class ScatterMax(_ScatterOp):
|
|||
``Ascend`` ``CPU``
|
||||
|
||||
Examples:
|
||||
>>> input_x = Parameter(Tensor(np.array([[1.0, 2.0, 3.0], [4.0, 5.0, 6.0]]), mindspore.float32), name="input_x")
|
||||
>>> input_x = Parameter(Tensor(np.array([[1.0, 2.0, 3.0], [4.0, 5.0, 6.0]]), mindspore.float32),
|
||||
... name="input_x")
|
||||
>>> indices = Tensor(np.array([[0, 0], [1, 1]]), mindspore.int32)
|
||||
>>> updates = Tensor(np.ones([2, 2, 3]) * 88, mindspore.float32)
|
||||
>>> scatter_max = ops.ScatterMax()
|
||||
|
|
@ -3691,7 +3692,8 @@ class ScatterMin(_ScatterOp):
|
|||
``Ascend`` ``CPU``
|
||||
|
||||
Examples:
|
||||
>>> input_x = Parameter(Tensor(np.array([[0.0, 1.0, 2.0], [0.0, 0.0, 0.0]]), mindspore.float32), name="input_x")
|
||||
>>> input_x = Parameter(Tensor(np.array([[0.0, 1.0, 2.0], [0.0, 0.0, 0.0]]), mindspore.float32),
|
||||
... name="input_x")
|
||||
>>> indices = Tensor(np.array([[0, 0], [1, 1]]), mindspore.int32)
|
||||
>>> update = Tensor(np.ones([2, 2, 3]), mindspore.float32)
|
||||
>>> scatter_min = ops.ScatterMin()
|
||||
|
|
@ -4142,7 +4144,8 @@ class DepthToSpace(PrimitiveWithInfer):
|
|||
- **x** (Tensor) - The target tensor. It must be a 4-D tensor with shape :math:`(N, C_{in}, H_{in}, W_{in})`.
|
||||
|
||||
Outputs:
|
||||
Tensor of shape :math:`(N, C_{in} / \text{block_size}, H_{in} * \text{block_size}, W_{in} * \text{block_size})`.
|
||||
Tensor of shape :math:`(N, C_{in} / \text{block_size}, H_{in} * \text{block_size},
|
||||
W_{in} * \text{block_size})`.
|
||||
|
||||
Raises:
|
||||
TypeError: If `block_size` is not an int.
|
||||
|
|
@ -4275,8 +4278,8 @@ class BatchToSpace(PrimitiveWithInfer):
|
|||
Divides batch dimension with blocks and interleaves these blocks back into spatial dimensions.
|
||||
|
||||
This operation will divide batch dimension N into blocks with block_size, the output tensor's N dimension
|
||||
is the corresponding number of blocks after division. The output tensor's H, W dimension is product of original H, W
|
||||
dimension and block_size with given amount to crop from dimension, respectively.
|
||||
is the corresponding number of blocks after division. The output tensor's H, W dimension is product of
|
||||
original H, W dimension and block_size with given amount to crop from dimension, respectively.
|
||||
|
||||
Args:
|
||||
block_size (int): The block size of division, has the value not less than 2.
|
||||
|
|
@ -4466,8 +4469,8 @@ class BatchToSpaceND(PrimitiveWithInfer):
|
|||
Divides batch dimension with blocks and interleaves these blocks back into spatial dimensions.
|
||||
|
||||
This operation will divide batch dimension N into blocks with block_shape, the output tensor's N dimension
|
||||
is the corresponding number of blocks after division. The output tensor's H, W dimension is product of original H, W
|
||||
dimension and block_shape with given amount to crop from dimension, respectively.
|
||||
is the corresponding number of blocks after division. The output tensor's H, W dimension is product of
|
||||
original H, W dimension and block_shape with given amount to crop from dimension, respectively.
|
||||
|
||||
Args:
|
||||
block_shape (Union[list(int), tuple(int), int]): The block shape of dividing block with all value greater
|
||||
|
|
@ -4580,8 +4583,8 @@ class BroadcastTo(Primitive):
|
|||
where it will be substituted by the input tensor's shape in that position, see example.
|
||||
|
||||
Inputs:
|
||||
- **input_x** (Tensor) - The input tensor. The data type should be one of the following types: float16, float32,
|
||||
int32, int8, uint8.
|
||||
- **input_x** (Tensor) - The input tensor. The data type should be one of the following types:
|
||||
float16, float32, int32, int8, uint8.
|
||||
|
||||
Outputs:
|
||||
Tensor, with the given `shape` and the same data type as `input_x`.
|
||||
|
|
|
|||
|
|
@ -23,6 +23,7 @@ from ...common import dtype as mstype
|
|||
from ..primitive import PrimitiveWithInfer, PrimitiveWithCheck, prim_attr_register
|
||||
from ...common.api import context
|
||||
|
||||
|
||||
class ReduceOp:
|
||||
"""
|
||||
Operation options for reducing tensors.
|
||||
|
|
@ -45,12 +46,14 @@ class ReduceOp:
|
|||
|
||||
target_dtypes = (mstype.int8, mstype.int32, mstype.float16, mstype.float32)
|
||||
|
||||
|
||||
def check_hcom_group_valid(group):
|
||||
if context.get_context("mode") == context.PYNATIVE_MODE and \
|
||||
context.get_context("device_target") == "Ascend" and \
|
||||
group != GlobalComm.WORLD_COMM_GROUP:
|
||||
raise RuntimeError("Only hccl_world_group is supported in Pynative mode, but got {}".format(group))
|
||||
|
||||
|
||||
class AllReduce(PrimitiveWithInfer):
|
||||
"""
|
||||
Reduces the tensor data across all devices in such a way that all devices will get the same final result.
|
||||
|
|
@ -149,7 +152,7 @@ class AllGather(PrimitiveWithInfer):
|
|||
``Ascend`` ``GPU``
|
||||
|
||||
Examples:
|
||||
>>> # This example should be run with two devices. Refer to the tutorial > Distributed Training on mindspore.cn.
|
||||
>>> # This example should be run with two devices. Refer to the tutorial > Distributed Training on mindspore.cn
|
||||
>>> import numpy as np
|
||||
>>> import mindspore.ops.operations as ops
|
||||
>>> import mindspore.nn as nn
|
||||
|
|
@ -304,7 +307,7 @@ class ReduceScatter(PrimitiveWithInfer):
|
|||
``Ascend`` ``GPU``
|
||||
|
||||
Examples:
|
||||
>>> # This example should be run with two devices. Refer to the tutorial > Distributed Training on mindspore.cn.
|
||||
>>> # This example should be run with two devices. Refer to the tutorial > Distributed Training on mindspore.cn
|
||||
>>> from mindspore import Tensor, context
|
||||
>>> from mindspore.communication import init
|
||||
>>> from mindspore.ops.operations.comm_ops import ReduceOp
|
||||
|
|
|
|||
|
|
@ -1844,7 +1844,8 @@ class Log1p(Primitive):
|
|||
Returns the natural logarithm of one plus the input tensor element-wise.
|
||||
|
||||
Inputs:
|
||||
- **input_x** (Tensor) - The input tensor. With float16 or float32 data type. The value must be greater than -1.
|
||||
- **input_x** (Tensor) - The input tensor. With float16 or float32 data type.
|
||||
The value must be greater than -1.
|
||||
|
||||
Outputs:
|
||||
Tensor, has the same shape as the `input_x`.
|
||||
|
|
|
|||
|
|
@ -1019,8 +1019,8 @@ class BNTrainingUpdate(PrimitiveWithInfer):
|
|||
|
||||
Inputs:
|
||||
- **x** (Tensor) - A 4-D Tensor with float16 or float32 data type. Tensor of shape :math:`(N, C, A, B)`.
|
||||
- **sum** (Tensor) - A 1-D Tensor with float16 or float32 data type for the output of operator BNTrainingReduce.
|
||||
Tensor of shape :math:`(C,)`.
|
||||
- **sum** (Tensor) - A 1-D Tensor with float16 or float32 data type for the output of operator
|
||||
BNTrainingReduce. Tensor of shape :math:`(C,)`.
|
||||
- **square_sum** (Tensor) - A 1-D Tensor with float16 or float32 data type for the output of operator
|
||||
BNTrainingReduce. Tensor of shape :math:`(C,)`.
|
||||
- **scale** (Tensor) - A 1-D Tensor with float16 or float32, for the scaling factor.
|
||||
|
|
@ -2690,7 +2690,8 @@ class SGD(PrimitiveWithCheck):
|
|||
>>> stat = Tensor(np.array([1.5, -0.3, 0.2, -0.7]), mindspore.float32)
|
||||
>>> output = sgd(parameters, gradient, learning_rate, accum, momentum, stat)
|
||||
>>> print(output)
|
||||
(Tensor(shape=[4], dtype=Float32, value= [ 1.98989999e+00, -4.90300000e-01, 1.69520009e+00, 3.98009992e+00]),)
|
||||
(Tensor(shape=[4], dtype=Float32,
|
||||
value= [ 1.98989999e+00, -4.90300000e-01, 1.69520009e+00, 3.98009992e+00]),)
|
||||
"""
|
||||
|
||||
@prim_attr_register
|
||||
|
|
@ -3234,14 +3235,15 @@ class OneHot(Primitive):
|
|||
If the input indices is rank `N`, the output will have rank `N+1`. The new axis is created at dimension `axis`.
|
||||
|
||||
Args:
|
||||
axis (int): Position to insert the value. e.g. If `indices` shape is [n, c], and `axis` is `-1` the output shape
|
||||
will be [n, c, depth], If `axis` is `0` the output shape will be [depth, n, c]. Default: -1.
|
||||
axis (int): Position to insert the value. e.g. If `indices` shape is [n, c], and `axis` is `-1` the output
|
||||
shape will be [n, c, depth], If `axis` is `0` the output shape will be [depth, n, c]. Default: -1.
|
||||
|
||||
Inputs:
|
||||
- **indices** (Tensor) - A tensor of indices. Tensor of shape :math:`(X_0, \ldots, X_n)`.
|
||||
Data type must be int32 or int64.
|
||||
- **depth** (int) - A scalar defining the depth of the one hot dimension.
|
||||
- **on_value** (Tensor) - A value to fill in output when `indices[j] = i`. With data type of float16 or float32.
|
||||
- **on_value** (Tensor) - A value to fill in output when `indices[j] = i`.
|
||||
With data type of float16 or float32.
|
||||
- **off_value** (Tensor) - A value to fill in output when `indices[j] != i`.
|
||||
Has the same data type with as `on_value`.
|
||||
|
||||
|
|
@ -3622,7 +3624,6 @@ class LSTM(PrimitiveWithInfer):
|
|||
self.num_directions = 1
|
||||
|
||||
def infer_shape(self, x_shape, h_shape, c_shape, w_shape):
|
||||
# (seq, batch_size, feature)
|
||||
validator.check_equal_int(len(x_shape), 3, "x rank", self.name)
|
||||
validator.check_equal_int(x_shape[2], self.input_size, "x[2]", self.name)
|
||||
|
||||
|
|
@ -3630,7 +3631,6 @@ class LSTM(PrimitiveWithInfer):
|
|||
validator.check_equal_int(len(h_shape), 3, "h rank", self.name)
|
||||
validator.check("h_shape", h_shape, "c_shape", c_shape, Rel.EQ, self.name)
|
||||
|
||||
# (num_layers * num_directions, batch, hidden_size)
|
||||
validator.check_int(h_shape[0], self.num_layers * self.num_directions, Rel.EQ, "h[0]", self.name)
|
||||
validator.check_equal_int(h_shape[1], x_shape[1], "h[1]", self.name)
|
||||
validator.check_int(h_shape[2], self.hidden_size, Rel.EQ, "h[2]", self.name)
|
||||
|
|
@ -4238,11 +4238,10 @@ class AdamNoUpdateParam(PrimitiveWithInfer):
|
|||
- **beta2_power** (Tensor) - :math:`beta_2^t(\beta_1^{t})` in the updating formula.
|
||||
The data type must be float32.
|
||||
- **lr** (Tensor) - :math:`l` in the updating formula. The data type must be float32.
|
||||
The paper suggested value is :math:`10^{-8}`
|
||||
- **beta1** (Tensor) - The exponential decay rate for the 1st moment estimations. The data type must be float32.
|
||||
The paper suggested value is :math:`0.9`
|
||||
- **beta2** (Tensor) - The exponential decay rate for the 2nd moment estimations. The data type must be float32.
|
||||
The paper suggested value is :math:`0.999`
|
||||
- **beta1** (Tensor) - The exponential decay rate for the 1st moment estimations.
|
||||
The data type must be float32.
|
||||
- **beta2** (Tensor) - The exponential decay rate for the 2nd moment estimations.
|
||||
The data type must be float32.
|
||||
- **epsilon** (Tensor) - Term added to the denominator to improve numerical stability. The data type must be
|
||||
float32.
|
||||
- **gradient** (Tensor) - Gradient, the shape must be the same as `m`, the data type must be float32.
|
||||
|
|
@ -4253,8 +4252,8 @@ class AdamNoUpdateParam(PrimitiveWithInfer):
|
|||
|
||||
Raises:
|
||||
TypeError: If neither `use_locking` nor `use_nesterov` is a bool.
|
||||
TypeError: If `m`, `v`, `beta1_power`, `beta2_power1`, `lr`, `beta1`, `beta2`, `epsilon` or `gradient` is not a
|
||||
Tensor.
|
||||
TypeError: If `m`, `v`, `beta1_power`, `beta2_power1`, `lr`, `beta1`, `beta2`, `epsilon` or `gradient`
|
||||
is not a Tensor.
|
||||
|
||||
Supported Platforms:
|
||||
``CPU``
|
||||
|
|
@ -4351,8 +4350,8 @@ class FusedSparseAdam(PrimitiveWithInfer):
|
|||
- **var** (Parameter) - Parameters to be updated with float32 data type.
|
||||
- **m** (Parameter) - The 1st moment vector in the updating formula, has the same type as `var` with
|
||||
float32 data type.
|
||||
- **v** (Parameter) - The 2nd moment vector in the updating formula. Mean square gradients, has the same type as
|
||||
`var` with float32 data type.
|
||||
- **v** (Parameter) - The 2nd moment vector in the updating formula.
|
||||
Mean square gradients, has the same type as `var` with float32 data type.
|
||||
- **beta1_power** (Tensor) - :math:`beta_1^t` in the updating formula with float32 data type.
|
||||
- **beta2_power** (Tensor) - :math:`beta_2^t` in the updating formula with float32 data type.
|
||||
- **lr** (Tensor) - :math:`l` in the updating formula. With float32 data type.
|
||||
|
|
@ -4498,8 +4497,8 @@ class FusedSparseLazyAdam(PrimitiveWithInfer):
|
|||
- **var** (Parameter) - Parameters to be updated with float32 data type.
|
||||
- **m** (Parameter) - The 1st moment vector in the updating formula, has the same type as `var` with
|
||||
float32 data type.
|
||||
- **v** (Parameter) - The 2nd moment vector in the updating formula. Mean square gradients, has the same type as
|
||||
`var` with float32 data type.
|
||||
- **v** (Parameter) - The 2nd moment vector in the updating formula.
|
||||
Mean square gradients, has the same type as `var` with float32 data type.
|
||||
- **beta1_power** (Tensor) - :math:`beta_1^t` in the updating formula with float32 data type.
|
||||
- **beta2_power** (Tensor) - :math:`beta_2^t` in the updating formula with float32 data type.
|
||||
- **lr** (Tensor) - :math:`l` in the updating formula with float32 data type.
|
||||
|
|
@ -4995,7 +4994,8 @@ class BinaryCrossEntropy(PrimitiveWithInfer):
|
|||
valid_dtypes = (mstype.float16, mstype.float32)
|
||||
validator.check_tensors_dtypes_same_and_valid(args, valid_dtypes, self.name)
|
||||
if weight_type:
|
||||
validator.check_tensors_dtypes_same_and_valid({'x': x_type, 'weight': weight_type}, valid_dtypes, self.name)
|
||||
validator.check_tensors_dtypes_same_and_valid({'x': x_type, 'weight': weight_type}, valid_dtypes,
|
||||
self.name)
|
||||
return x_type
|
||||
|
||||
|
||||
|
|
@ -6953,8 +6953,8 @@ class CTCLoss(Primitive):
|
|||
(`max_time`, `batch_size`, `num_classes`). `num_classes` must be `num_labels + 1` classes, `num_labels`
|
||||
indicates the number of actual labels. Blank labels are reserved. Default blank label is `num_classes - 1`.
|
||||
Data type must be float16, float32 or float64.
|
||||
- **labels_indices** (Tensor) - The indices of labels. `labels_indices[i, :] == [b, t]` means `labels_values[i]`
|
||||
stores the id for `(batch b, time t)`. The type must be int64 and rank must be 2.
|
||||
- **labels_indices** (Tensor) - The indices of labels. `labels_indices[i, :] == [b, t]` means
|
||||
`labels_values[i]` stores the id for `(batch b, time t)`. The type must be int64 and rank must be 2.
|
||||
- **labels_values** (Tensor) - A `1-D` input tensor. The values are associated with the given batch and time.
|
||||
The type must be int32. `labels_values[i]` must in the range of `[0, num_classes)`.
|
||||
- **sequence_length** (Tensor) - A tensor containing sequence lengths with the shape of (`batch_size`).
|
||||
|
|
@ -6966,8 +6966,8 @@ class CTCLoss(Primitive):
|
|||
- **gradient** (Tensor) - The gradient of `loss`, has the same type and shape with `inputs`.
|
||||
|
||||
Raises:
|
||||
TypeError: If `preprocess_collapse_repeated`, `ctc_merge_repeated` or `ignore_longer_outputs_than_inputs` is not
|
||||
a bool.
|
||||
TypeError: If `preprocess_collapse_repeated`, `ctc_merge_repeated` or `ignore_longer_outputs_than_inputs`
|
||||
is not a bool.
|
||||
TypeError: If `inputs`, `labels_indices`, `labels_values` or `sequence_length` is not a Tensor.
|
||||
TypeError: If dtype of `inputs` is not one of the following: float16, float32 or float64.
|
||||
TypeError: If dtype of `labels_indices` is not int64.
|
||||
|
|
@ -7088,8 +7088,6 @@ class BasicLSTMCell(PrimitiveWithInfer):
|
|||
Please use DynamicRNN instead.
|
||||
"""
|
||||
|
||||
# deprecate_new_name = "BasicLSTMCell"
|
||||
|
||||
@prim_attr_register
|
||||
def __init__(self, keep_prob=1.0, forget_bias=1.0, state_is_tuple=True, activation='tanh'):
|
||||
self.keep_prob = validator.check_value_type("keep_prob", keep_prob, [float], self.name)
|
||||
|
|
@ -7331,8 +7329,8 @@ class DynamicGRUV2(PrimitiveWithInfer):
|
|||
The data type must be float16.
|
||||
- **bias_input** (Tensor) - Input-hidden bias. Tensor of shape :math:`(3 \times \text{hidden_size})`, or None.
|
||||
Has the same data type with input `init_h`.
|
||||
- **bias_hidden** (Tensor) - Hidden-hidden bias. Tensor of shape :math:`(3 \times \text{hidden_size})`, or None.
|
||||
Has the same data type with input `init_h`.
|
||||
- **bias_hidden** (Tensor) - Hidden-hidden bias. Tensor of shape :math:`(3 \times \text{hidden_size})`,
|
||||
or None. Has the same data type with input `init_h`.
|
||||
- **seq_length** (Tensor) - The length of each batch. Tensor of shape :math:`(\text{batch_size})`.
|
||||
Only `None` is currently supported.
|
||||
- **init_h** (Tensor) - Hidden state of initial time.
|
||||
|
|
@ -7475,7 +7473,8 @@ class InTopK(PrimitiveWithInfer):
|
|||
k (int): Specifies the number of top elements to be used for computing precision.
|
||||
|
||||
Inputs:
|
||||
- **x1** (Tensor) - A 2D Tensor defines the predictions of a batch of samples with float16 or float32 data type.
|
||||
- **x1** (Tensor) - A 2D Tensor defines the predictions of a batch of samples with float16 or float32
|
||||
data type.
|
||||
- **x2** (Tensor) - A 1D Tensor defines the labels of a batch of samples with int32 data type. The size of x2
|
||||
must be equal to x1's first dimension. The values of `x2` can not be negative and
|
||||
must be equal to or less than index of x1's second dimension.
|
||||
|
|
@ -7712,10 +7711,10 @@ class Conv3D(PrimitiveWithInfer):
|
|||
|
||||
Args:
|
||||
out_channels (int): The number of output channel :math:`C_{out}`.
|
||||
kernel_size (Union[int, tuple[int]]): The data type is int or a tuple of 3 integers. Specifies the depth, height
|
||||
and width of the 3D convolution window. Single int means the value is for the depth, height and the width
|
||||
of the kernel. A tuple of 3 ints means the first value is for the depth, height and the other is for the
|
||||
width of the kernel.
|
||||
kernel_size (Union[int, tuple[int]]): The data type is int or a tuple of 3 integers. Specifies the depth,
|
||||
height and width of the 3D convolution window. Single int means the value is for the depth, height
|
||||
and the width of the kernel. A tuple of 3 ints means the first value is for the depth, height and
|
||||
the other is for the width of the kernel.
|
||||
mode (int): Modes for different convolutions. Not currently used.
|
||||
stride (Union[int, tuple[int]]): The distance of kernel moving, an int number that represents
|
||||
the depth, height and width of movement are both strides, or a tuple of three int numbers that
|
||||
|
|
@ -7755,7 +7754,8 @@ class Conv3D(PrimitiveWithInfer):
|
|||
- **input** (Tensor) - Tensor of shape :math:`(N, C_{in}, D_{in}, H_{in}, W_{in})`.
|
||||
Currently input data type only support float16 and float32.
|
||||
- **weight** (Tensor) - Set size of kernel is :math:`(k_d, K_h, K_w)`, then the shape is
|
||||
:math:`(C_{out}, C_{in}//groups, k_d, K_h, K_w)`. Currently weight data type only support float16 and float32.
|
||||
:math:`(C_{out}, C_{in}//groups, k_d, K_h, K_w)`.
|
||||
Currently weight data type only support float16 and float32.
|
||||
- **bias** (Tensor) - Tensor of shape :math:`C_{in}`. Currently, only support none.
|
||||
|
||||
Outputs:
|
||||
|
|
@ -8185,7 +8185,7 @@ class Conv3DTranspose(PrimitiveWithInfer):
|
|||
if isinstance(pad, int):
|
||||
pad = (pad,) * 6
|
||||
if len(pad) != 6:
|
||||
raise ValueError(f"For `conv3d` attr 'pad' should be an positive int number or a tuple of "
|
||||
raise ValueError(f"For `Conv3DTranspose` attr 'pad' should be an positive int number or a tuple of "
|
||||
f"six positive int numbers, but got `{len(pad)}`.")
|
||||
self.pad_list = pad
|
||||
validator.check_value_type('pad_mode', pad_mode, [str], self.name)
|
||||
|
|
|
|||
|
|
@ -119,6 +119,7 @@ class InplaceAssign(PrimitiveWithInfer):
|
|||
def infer_dtype(self, x, y, z):
|
||||
return z
|
||||
|
||||
|
||||
class Load(PrimitiveWithCheck):
|
||||
"""
|
||||
Load `Parameter` to a value.
|
||||
|
|
@ -142,6 +143,7 @@ class Load(PrimitiveWithCheck):
|
|||
if variable != mstype.type_refkey:
|
||||
validator.check_tensor_type_same({"variable": variable}, mstype.number_type, self.name)
|
||||
|
||||
|
||||
class BoundingBoxEncode(PrimitiveWithInfer):
|
||||
"""
|
||||
Encodes bounding boxes locations.
|
||||
|
|
@ -476,6 +478,7 @@ class Depend(Primitive):
|
|||
def __call__(self, value, expr):
|
||||
return value
|
||||
|
||||
|
||||
class UpdateState(Primitive):
|
||||
"""
|
||||
UpdateState is used for update side-effect state.
|
||||
|
|
@ -495,6 +498,7 @@ class UpdateState(Primitive):
|
|||
def __call__(self, state, expr):
|
||||
return state
|
||||
|
||||
|
||||
class CheckBprop(PrimitiveWithInfer):
|
||||
"""
|
||||
Checks whether the data type and the shape of corresponding elements from tuples x and y are the same.
|
||||
|
|
|
|||
|
|
@ -249,8 +249,8 @@ class Primitive(Primitive_):
|
|||
|
||||
class PrimitiveWithCheck(Primitive):
|
||||
"""
|
||||
PrimitiveWithCheck is the base class of primitives in python defines functions for checking operator input arguments
|
||||
but used the infer method registered in c++ source codes.
|
||||
PrimitiveWithCheck is the base class of primitives in python defines functions for checking operator
|
||||
input arguments but used the infer method registered in c++ source codes.
|
||||
|
||||
There are three methods can be override to define the check logic of the primitive: __check__(), check_shape(),
|
||||
check_dtype(). If __check__() is defined in primitive, the __check__() has highest priority to be called.
|
||||
|
|
@ -330,7 +330,8 @@ class PrimitiveWithCheck(Primitive):
|
|||
|
||||
class PrimitiveWithInfer(Primitive):
|
||||
"""
|
||||
PrimitiveWithInfer is the base class of primitives in python and defines functions for tracking inference in python.
|
||||
PrimitiveWithInfer is the base class of primitives in python and defines functions for tracking inference
|
||||
in python.
|
||||
|
||||
There are four method can be override to define the infer logic of the primitive: __infer__(), infer_shape(),
|
||||
infer_dtype(), and infer_value(). If __infer__() is defined in primitive, the __infer__() has highest priority
|
||||
|
|
|
|||
Loading…
Reference in New Issue