forked from mindspore-Ecosystem/mindspore
!9701 add CPU OnesLike and ZerosLike
From: @zhao_ting_v Reviewed-by: @liangchenghui,@wuxuejian Signed-off-by: @wuxuejian
This commit is contained in:
commit
9483666e60
|
@ -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;
|
||||
}
|
||||
|
|
|
@ -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
|
||||
|
||||
|
|
|
@ -69,7 +69,9 @@ enum OperateType {
|
|||
ABSGRAD,
|
||||
TANHGRAD,
|
||||
SQRTGRAD,
|
||||
SIGMOIDGRAD
|
||||
SIGMOIDGRAD,
|
||||
ONESLIKE,
|
||||
ZEROSLIKE
|
||||
};
|
||||
|
||||
class CPUKernel : public kernel::KernelMod {
|
||||
|
|
|
@ -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");
|
||||
|
|
|
@ -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()
|
||||
|
|
|
@ -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
|
|
@ -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…
Reference in New Issue