diff --git a/mindspore/ops/_op_impl/aicpu/__init__.py b/mindspore/ops/_op_impl/aicpu/__init__.py index 475200e7ef87d3c762f9d05b7695d97fe8bdb804..24f4ad750cda85df9156b9bf4ec06f3787fff14b 100644 --- a/mindspore/ops/_op_impl/aicpu/__init__.py +++ b/mindspore/ops/_op_impl/aicpu/__init__.py @@ -25,3 +25,4 @@ from .squeeze import _squeeze_aicpu from .expand_dims import _expand_dims_aicpu from .random_choice_with_mask import _random_choice_with_mask_aicpu from .pack import _pack_aicpu +from .normal import _normal_aicpu diff --git a/mindspore/ops/_op_impl/aicpu/normal.py b/mindspore/ops/_op_impl/aicpu/normal.py new file mode 100644 index 0000000000000000000000000000000000000000..fdb96e362fd0e4048f52d1bc2c0697aeff004c56 --- /dev/null +++ b/mindspore/ops/_op_impl/aicpu/normal.py @@ -0,0 +1,33 @@ +# Copyright 2020 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. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# ============================================================================ + +"""Normal op""" +from mindspore.ops.op_info_register import op_info_register, AiCPURegOp, DataType + +normal_op_info = AiCPURegOp("Normal") \ + .fusion_type("OPAQUE") \ + .input(0, "shape", "required") \ + .input(1, "mean", "required") \ + .input(2, "stddev", "required") \ + .output(0, "y", "required") \ + .attr("seed", "int") \ + .dtype_format(DataType.I32_Default, DataType.F32_Default, DataType.F32_Default, DataType.F32_Default) \ + .dtype_format(DataType.I32_NCHW, DataType.F32_NCHW, DataType.F32_NCHW, DataType.F32_NCHW) \ + .get_op_info() + +@op_info_register(normal_op_info) +def _normal_aicpu(): + """Normal AiCPU register""" + return diff --git a/mindspore/ops/operations/__init__.py b/mindspore/ops/operations/__init__.py index f0ac503604846da84ab2b3b9a0382aac8e4a9412..57ffd969c14e8fa1fdf545cc5a0752df4f0cdf0f 100644 --- a/mindspore/ops/operations/__init__.py +++ b/mindspore/ops/operations/__init__.py @@ -53,7 +53,7 @@ from .math_ops import (Abs, ACos, Asin, Asinh, AddN, AssignAdd, AssignSub, Atan2 Sin, Sqrt, Rsqrt, BesselI0e, BesselI1e, Square, Sub, TensorAdd, Sign, Round, SquareSumAll, Atan, Atanh, Cosh, Sinh) -from .random_ops import (RandomChoiceWithMask) +from .random_ops import (RandomChoiceWithMask, Normal) from .nn_ops import (LSTM, SGD, Adam, SparseApplyAdam, SparseApplyLazyAdam, ApplyMomentum, BatchNorm, BiasAdd, Conv2D, DepthwiseConv2dNative, @@ -163,6 +163,7 @@ __all__ = [ 'HSigmoid', 'Tanh', 'RandomChoiceWithMask', + 'Normal', 'ResizeBilinear', 'ScalarSummary', 'ImageSummary', diff --git a/mindspore/ops/operations/random_ops.py b/mindspore/ops/operations/random_ops.py index 2692b43b46c7aa7bbe268a995964cb0097ccc2d1..7a457d099818b726b49f691b7cd4099abd7ca055 100644 --- a/mindspore/ops/operations/random_ops.py +++ b/mindspore/ops/operations/random_ops.py @@ -64,3 +64,47 @@ class RandomChoiceWithMask(PrimitiveWithInfer): def infer_dtype(self, x_dtype): validator.check_tensor_type_same({'x': x_dtype}, [mstype.bool_], self.name) return (mstype.int32, mstype.bool_) + + +class Normal(PrimitiveWithInfer): + """ + Generates random samples from a normal(Gaussian) distribution. + + Args: + seed (int): Random seed. Default: 0. + + Inputs: + - **shape** (tuple[int]) - The shape of output tensor. Only constant value is allowed. + - **mean** (Tensor) - The mean of the distribution, with float32 data type. + - **stddev** (Tensor) - The standard deviation of the distribution, with float32 data type. + + Outputs: + Tensor, with the given shape from the specific distribution and float32 data type. + + Examples: + >>> normal = P.Normal() + >>> mean = Tensor(0., mstype.float32) + >>> stddev = Tensor(1., mstype.float32) + >>> out = normal((32, 3, 3), mean, stddev) + """ + + @prim_attr_register + def __init__(self, seed=0): + """Init Normal""" + validator.check_value_type("seed", seed, [int], self.name) + + def __infer__(self, shape, mean, stddev): + shape_value = shape["value"] + if shape_value is None: + raise ValueError(f"For {self.name}, shape must be const.") + validator.check_value_type("shape", shape_value, [tuple], self.name) + for i, shape_i in enumerate(shape_value): + validator.check_integer("shape[%d]" % i, shape_i, 0, Rel.GE, self.name) + + validator.check_tensor_type_same({"mean": mean["dtype"]}, [mstype.float32], self.name) + validator.check_tensor_type_same({"stddev": stddev["dtype"]}, [mstype.float32], self.name) + + out = {"shape": shape_value, + "dtype": mstype.float32, + "value": None} + return out diff --git a/tests/st/ops/ascend/test_aicpu_ops/test_normal.py b/tests/st/ops/ascend/test_aicpu_ops/test_normal.py new file mode 100644 index 0000000000000000000000000000000000000000..66254caf21f162e55af424bfd1d3739fffb2c8b4 --- /dev/null +++ b/tests/st/ops/ascend/test_aicpu_ops/test_normal.py @@ -0,0 +1,43 @@ +# Copyright 2020 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. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# ============================================================================ +import mindspore.context as context +import mindspore.nn as nn +from mindspore.ops import operations as P +from mindspore.common import Tensor +from mindspore.common import dtype as mstype + + +context.set_context(mode=context.PYNATIVE_MODE, device_target="Ascend") + + +class Net(nn.Cell): + def __init__(self, shape=None, mean=0.0, stddev=1.0, seed=0): + super(Net, self).__init__() + self._mean = Tensor(mean, mstype.float32) + self._stddev = Tensor(stddev, mstype.float32) + self._normal = P.Normal(seed=seed) + self._shape = shape + + def construct(self): + return self._normal(self._shape, self._mean, self._stddev) + + +def test_net_3x2x4(): + mean = 0.0 + stddev = 1.0 + seed = 0 + net = Net((3, 2, 4), mean, stddev, seed) + out = net() + assert out.shape == (3, 2, 4) diff --git a/tests/ut/python/ops/test_ops.py b/tests/ut/python/ops/test_ops.py index ad18faa61ee8571dac2295384bd9a93671d85706..5927d97c50b54a8b765b6b9780f8f5fdecb6632b 100755 --- a/tests/ut/python/ops/test_ops.py +++ b/tests/ut/python/ops/test_ops.py @@ -399,6 +399,19 @@ class InplaceSubNet(nn.Cell): return out +class NormalNet(nn.Cell): + def __init__(self, shape=None, mean=0.0, stddev=1.0, seed=0): + super(NormalNet, self).__init__() + self.normal = P.Normal(seed=seed) + self.shape = shape + self.mean = Tensor(mean, mstype.float32) + self.stddev = Tensor(stddev, mstype.float32) + + def construct(self): + out = self.normal(self.shape, self.mean, self.stddev) + return out + + test_case_math_ops = [ ('BitwiseAnd', { 'block': P.BitwiseAnd(), @@ -895,6 +908,10 @@ test_case_math_ops = [ 'desc_inputs': [Tensor([-1.0, 0.0, 1.5, 2.0, 5.0, 15], mstype.float16), Tensor([0.0, 5.0], mstype.float16)], 'desc_bprop': [], 'skip': ['backward']}), + ('Normal', { + 'block': NormalNet((3, 2, 4), 0.0, 1.0, 0), + 'desc_inputs': [], + 'skip': ['backward']}), ] test_case_nn_ops = [