Browse Source

!9701 add CPU OnesLike and ZerosLike

From: @zhao_ting_v
Reviewed-by: @liangchenghui,@wuxuejian
Signed-off-by: @wuxuejian
tags/v1.1.0
mindspore-ci-bot Gitee 5 years ago
parent
commit
9483666e60
7 changed files with 151 additions and 2 deletions
  1. +22
    -0
      mindspore/ccsrc/backend/kernel_compiler/cpu/arithmetic_self_cpu_kernel.cc
  2. +8
    -0
      mindspore/ccsrc/backend/kernel_compiler/cpu/arithmetic_self_cpu_kernel.h
  3. +3
    -1
      mindspore/ccsrc/backend/kernel_compiler/cpu/cpu_kernel.h
  4. +1
    -0
      mindspore/core/base/core_ops.h
  5. +1
    -1
      mindspore/ops/operations/array_ops.py
  6. +58
    -0
      tests/st/ops/cpu/test_oneslike_op.py
  7. +58
    -0
      tests/st/ops/cpu/test_zeroslike_op.py

+ 22
- 0
mindspore/ccsrc/backend/kernel_compiler/cpu/arithmetic_self_cpu_kernel.cc View File

@@ -35,6 +35,20 @@ void Neg(const T *in, T *out, size_t start, size_t end) {
out[i] = -in[i];
}
}

template <typename T>
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<T>(1);
}
}

template <typename T>
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<T>(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<AddressPtr> &inputs
threads.emplace_back(std::thread(Square<T>, input, output, start, end));
} else if (operate_type_ == NEG) {
threads.emplace_back(std::thread(Neg<T>, input, output, start, end));
} else if (operate_type_ == ONESLIKE) {
threads.emplace_back(std::thread(OnesLike<T>, input, output, start, end));
} else if (operate_type_ == ZEROSLIKE) {
threads.emplace_back(std::thread(ZerosLike<T>, input, output, start, end));
}
start += once_compute_size;
}


+ 8
- 0
mindspore/ccsrc/backend/kernel_compiler/cpu/arithmetic_self_cpu_kernel.h View File

@@ -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



+ 3
- 1
mindspore/ccsrc/backend/kernel_compiler/cpu/cpu_kernel.h View File

@@ -69,7 +69,9 @@ enum OperateType {
ABSGRAD,
TANHGRAD,
SQRTGRAD,
SIGMOIDGRAD
SIGMOIDGRAD,
ONESLIKE,
ZEROSLIKE
};
class CPUKernel : public kernel::KernelMod {


+ 1
- 0
mindspore/core/base/core_ops.h View File

@@ -183,6 +183,7 @@ inline const PrimitivePtr kPrimRelu = std::make_shared<Primitive>("ReLU");
inline const PrimitivePtr kPrimRelu6 = std::make_shared<Primitive>("ReLU6");
inline const PrimitivePtr kPrimReluV2 = std::make_shared<Primitive>("ReLUV2");
inline const PrimitivePtr kPrimZerosLike = std::make_shared<Primitive>("ZerosLike");
inline const PrimitivePtr kPrimOnesLike = std::make_shared<Primitive>("OnesLike");
inline const PrimitivePtr kPrimBpropCut = std::make_shared<Primitive>("bprop_cut");
inline const PrimitivePtr kPrimFakeQuantPerLayer = std::make_shared<Primitive>("FakeQuantPerLayer");
inline const PrimitivePtr kPrimFakeQuantPerChannel = std::make_shared<Primitive>("FakeQuantPerChannel");


+ 1
- 1
mindspore/ops/operations/array_ops.py View File

@@ -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()


+ 58
- 0
tests/st/ops/cpu/test_oneslike_op.py View File

@@ -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

+ 58
- 0
tests/st/ops/cpu/test_zeroslike_op.py View File

@@ -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

Loading…
Cancel
Save