From 3b9e9068c7c08b8e6668c5974ad91d3a778438b7 Mon Sep 17 00:00:00 2001 From: zhaoting Date: Wed, 9 Dec 2020 11:29:44 +0800 Subject: [PATCH] add CPU OnesLike and ZerosLike --- .../cpu/arithmetic_self_cpu_kernel.cc | 22 +++++++ .../cpu/arithmetic_self_cpu_kernel.h | 8 +++ .../backend/kernel_compiler/cpu/cpu_kernel.h | 4 +- mindspore/core/base/core_ops.h | 1 + mindspore/ops/operations/array_ops.py | 2 +- tests/st/ops/cpu/test_oneslike_op.py | 58 +++++++++++++++++++ tests/st/ops/cpu/test_zeroslike_op.py | 58 +++++++++++++++++++ 7 files changed, 151 insertions(+), 2 deletions(-) create mode 100644 tests/st/ops/cpu/test_oneslike_op.py create mode 100644 tests/st/ops/cpu/test_zeroslike_op.py diff --git a/mindspore/ccsrc/backend/kernel_compiler/cpu/arithmetic_self_cpu_kernel.cc b/mindspore/ccsrc/backend/kernel_compiler/cpu/arithmetic_self_cpu_kernel.cc index fe0f3f9833..322b24b8c7 100644 --- a/mindspore/ccsrc/backend/kernel_compiler/cpu/arithmetic_self_cpu_kernel.cc +++ b/mindspore/ccsrc/backend/kernel_compiler/cpu/arithmetic_self_cpu_kernel.cc @@ -35,6 +35,20 @@ void Neg(const T *in, T *out, size_t start, size_t end) { out[i] = -in[i]; } } + +template +void OnesLike(const T *in, T *out, size_t start, size_t end) { + for (size_t i = start; i < end; i++) { + out[i] = static_cast(1); + } +} + +template +void ZerosLike(const T *in, T *out, size_t start, size_t end) { + for (size_t i = start; i < end; i++) { + out[i] = static_cast(0); + } +} } // namespace void ArithmeticSelfCPUKernel::InitKernel(const CNodePtr &kernel_node) { @@ -42,6 +56,10 @@ void ArithmeticSelfCPUKernel::InitKernel(const CNodePtr &kernel_node) { std::string kernel_name = AnfAlgo::GetCNodeName(kernel_node); if (kernel_name == prim::kPrimSquare->name()) { operate_type_ = SQUARE; + } else if (kernel_name == prim::kPrimOnesLike->name()) { + operate_type_ = ONESLIKE; + } else if (kernel_name == prim::kPrimZerosLike->name()) { + operate_type_ = ZEROSLIKE; } else if (kernel_name == prim::kPrimNeg->name()) { operate_type_ = NEG; } @@ -89,6 +107,10 @@ void ArithmeticSelfCPUKernel::LaunchKernel(const std::vector &inputs threads.emplace_back(std::thread(Square, input, output, start, end)); } else if (operate_type_ == NEG) { threads.emplace_back(std::thread(Neg, input, output, start, end)); + } else if (operate_type_ == ONESLIKE) { + threads.emplace_back(std::thread(OnesLike, input, output, start, end)); + } else if (operate_type_ == ZEROSLIKE) { + threads.emplace_back(std::thread(ZerosLike, input, output, start, end)); } start += once_compute_size; } diff --git a/mindspore/ccsrc/backend/kernel_compiler/cpu/arithmetic_self_cpu_kernel.h b/mindspore/ccsrc/backend/kernel_compiler/cpu/arithmetic_self_cpu_kernel.h index ebfeb8411f..270d3c076f 100644 --- a/mindspore/ccsrc/backend/kernel_compiler/cpu/arithmetic_self_cpu_kernel.h +++ b/mindspore/ccsrc/backend/kernel_compiler/cpu/arithmetic_self_cpu_kernel.h @@ -46,6 +46,14 @@ MS_REG_CPU_KERNEL(Neg, KernelAttr().AddInputAttr(kNumberTypeFloat32).AddOutputAt ArithmeticSelfCPUKernel); MS_REG_CPU_KERNEL(Neg, KernelAttr().AddInputAttr(kNumberTypeInt32).AddOutputAttr(kNumberTypeInt32), ArithmeticSelfCPUKernel); +MS_REG_CPU_KERNEL(ZerosLike, KernelAttr().AddInputAttr(kNumberTypeFloat32).AddOutputAttr(kNumberTypeFloat32), + ArithmeticSelfCPUKernel); +MS_REG_CPU_KERNEL(ZerosLike, KernelAttr().AddInputAttr(kNumberTypeInt32).AddOutputAttr(kNumberTypeInt32), + ArithmeticSelfCPUKernel); +MS_REG_CPU_KERNEL(OnesLike, KernelAttr().AddInputAttr(kNumberTypeFloat32).AddOutputAttr(kNumberTypeFloat32), + ArithmeticSelfCPUKernel); +MS_REG_CPU_KERNEL(OnesLike, KernelAttr().AddInputAttr(kNumberTypeInt32).AddOutputAttr(kNumberTypeInt32), + ArithmeticSelfCPUKernel); } // namespace kernel } // namespace mindspore diff --git a/mindspore/ccsrc/backend/kernel_compiler/cpu/cpu_kernel.h b/mindspore/ccsrc/backend/kernel_compiler/cpu/cpu_kernel.h index ed157648e8..a3a366b7ac 100644 --- a/mindspore/ccsrc/backend/kernel_compiler/cpu/cpu_kernel.h +++ b/mindspore/ccsrc/backend/kernel_compiler/cpu/cpu_kernel.h @@ -69,7 +69,9 @@ enum OperateType { ABSGRAD, TANHGRAD, SQRTGRAD, - SIGMOIDGRAD + SIGMOIDGRAD, + ONESLIKE, + ZEROSLIKE }; class CPUKernel : public kernel::KernelMod { diff --git a/mindspore/core/base/core_ops.h b/mindspore/core/base/core_ops.h index 8f433a324c..f0008dbfce 100644 --- a/mindspore/core/base/core_ops.h +++ b/mindspore/core/base/core_ops.h @@ -183,6 +183,7 @@ inline const PrimitivePtr kPrimRelu = std::make_shared("ReLU"); inline const PrimitivePtr kPrimRelu6 = std::make_shared("ReLU6"); inline const PrimitivePtr kPrimReluV2 = std::make_shared("ReLUV2"); inline const PrimitivePtr kPrimZerosLike = std::make_shared("ZerosLike"); +inline const PrimitivePtr kPrimOnesLike = std::make_shared("OnesLike"); inline const PrimitivePtr kPrimBpropCut = std::make_shared("bprop_cut"); inline const PrimitivePtr kPrimFakeQuantPerLayer = std::make_shared("FakeQuantPerLayer"); inline const PrimitivePtr kPrimFakeQuantPerChannel = std::make_shared("FakeQuantPerChannel"); diff --git a/mindspore/ops/operations/array_ops.py b/mindspore/ops/operations/array_ops.py index 0ed8b0a2cb..0ff9c632cd 100644 --- a/mindspore/ops/operations/array_ops.py +++ b/mindspore/ops/operations/array_ops.py @@ -1262,7 +1262,7 @@ class OnesLike(PrimitiveWithInfer): Tensor, has the same shape and type as `input_x` but filled with ones. Supported Platforms: - ``Ascend`` ``GPU`` + ``Ascend`` ``GPU`` ``CPU`` Examples: >>> oneslike = ops.OnesLike() diff --git a/tests/st/ops/cpu/test_oneslike_op.py b/tests/st/ops/cpu/test_oneslike_op.py new file mode 100644 index 0000000000..f16dd1c1d4 --- /dev/null +++ b/tests/st/ops/cpu/test_oneslike_op.py @@ -0,0 +1,58 @@ +# Copyright 2019 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 numpy as np +import pytest + +import mindspore.context as context +import mindspore.nn as nn +from mindspore import Tensor +from mindspore.ops import operations as P + + +class NetOnesLike(nn.Cell): + def __init__(self): + super(NetOnesLike, self).__init__() + self.ones_like = P.OnesLike() + + def construct(self, x): + return self.ones_like(x) + + +@pytest.mark.level0 +@pytest.mark.platform_x86_cpu_training +@pytest.mark.env_onecard +def test_OnesLike(): + x0_np = np.random.uniform(-2, 2, (2, 3, 4, 4)).astype(np.float32) + x1_np = np.random.uniform(-2, 2, 1).astype(np.float32) + + x0 = Tensor(x0_np) + x1 = Tensor(x1_np) + + context.set_context(mode=context.GRAPH_MODE, device_target="CPU") + ones_like = NetOnesLike() + output0 = ones_like(x0) + expect0 = np.ones_like(x0_np) + diff0 = output0.asnumpy() - expect0 + error0 = np.ones(shape=expect0.shape) * 1.0e-5 + assert np.all(diff0 < error0) + assert output0.shape == expect0.shape + + output1 = ones_like(x1) + expect1 = np.ones_like(x1_np) + diff1 = output1.asnumpy() - expect1 + error1 = np.ones(shape=expect1.shape) * 1.0e-5 + assert np.all(diff1 < error1) + assert output1.shape == expect1.shape diff --git a/tests/st/ops/cpu/test_zeroslike_op.py b/tests/st/ops/cpu/test_zeroslike_op.py new file mode 100644 index 0000000000..45411558bb --- /dev/null +++ b/tests/st/ops/cpu/test_zeroslike_op.py @@ -0,0 +1,58 @@ +# Copyright 2019 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 numpy as np +import pytest + +import mindspore.context as context +import mindspore.nn as nn +from mindspore import Tensor +from mindspore.ops import operations as P + + +class NetZerosLike(nn.Cell): + def __init__(self): + super(NetZerosLike, self).__init__() + self.zeros_like = P.ZerosLike() + + def construct(self, x): + return self.zeros_like(x) + + +@pytest.mark.level0 +@pytest.mark.platform_x86_cpu_training +@pytest.mark.env_onecard +def test_ZerosLike(): + x0_np = np.random.uniform(-2, 2, (2, 3, 4, 4)).astype(np.float32) + x1_np = np.random.uniform(-2, 2, 1).astype(np.float32) + + x0 = Tensor(x0_np) + x1 = Tensor(x1_np) + + context.set_context(mode=context.GRAPH_MODE, device_target="CPU") + zeros_like = NetZerosLike() + output0 = zeros_like(x0) + expect0 = np.zeros_like(x0_np) + diff0 = output0.asnumpy() - expect0 + error0 = np.ones(shape=expect0.shape) * 1.0e-5 + assert np.all(diff0 < error0) + assert output0.shape == expect0.shape + + output1 = zeros_like(x1) + expect1 = np.zeros_like(x1_np) + diff1 = output1.asnumpy() - expect1 + error1 = np.ones(shape=expect1.shape) * 1.0e-5 + assert np.all(diff1 < error1) + assert output1.shape == expect1.shape