forked from mindspore-Ecosystem/mindspore
commit
478775a322
|
@ -98,6 +98,9 @@ int BitwiseCpuKernelMod::Resize(const BaseOperatorPtr &base_operator, const std:
|
|||
}
|
||||
|
||||
switch (input_type_1_) {
|
||||
case kNumberTypeBool:
|
||||
InitFunc<bool>();
|
||||
break;
|
||||
case kNumberTypeInt8:
|
||||
InitFunc<int8_t>();
|
||||
break;
|
||||
|
@ -236,10 +239,11 @@ bool BitwiseCpuKernelMod::LaunchKernel(const std::vector<kernel::AddressPtr> &in
|
|||
|
||||
const std::vector<std::pair<KernelAttr, BitwiseCpuKernelMod::KernelRunFunc>> &BitwiseCpuKernelMod::GetFuncList() const {
|
||||
static const std::vector<std::pair<KernelAttr, BitwiseCpuKernelMod::KernelRunFunc>> func_list = {
|
||||
{BITWISE_CPU_KERNEL_MATCH(kNumberTypeInt8, int8_t)}, {BITWISE_CPU_KERNEL_MATCH(kNumberTypeInt16, int16_t)},
|
||||
{BITWISE_CPU_KERNEL_MATCH(kNumberTypeInt32, int32_t)}, {BITWISE_CPU_KERNEL_MATCH(kNumberTypeInt64, int64_t)},
|
||||
{BITWISE_CPU_KERNEL_MATCH(kNumberTypeUInt8, uint8_t)}, {BITWISE_CPU_KERNEL_MATCH(kNumberTypeUInt16, uint16_t)},
|
||||
{BITWISE_CPU_KERNEL_MATCH(kNumberTypeUInt32, uint32_t)}, {BITWISE_CPU_KERNEL_MATCH(kNumberTypeUInt64, uint64_t)},
|
||||
{BITWISE_CPU_KERNEL_MATCH(kNumberTypeBool, bool)}, {BITWISE_CPU_KERNEL_MATCH(kNumberTypeInt8, int8_t)},
|
||||
{BITWISE_CPU_KERNEL_MATCH(kNumberTypeInt16, int16_t)}, {BITWISE_CPU_KERNEL_MATCH(kNumberTypeInt32, int32_t)},
|
||||
{BITWISE_CPU_KERNEL_MATCH(kNumberTypeInt64, int64_t)}, {BITWISE_CPU_KERNEL_MATCH(kNumberTypeUInt8, uint8_t)},
|
||||
{BITWISE_CPU_KERNEL_MATCH(kNumberTypeUInt16, uint16_t)}, {BITWISE_CPU_KERNEL_MATCH(kNumberTypeUInt32, uint32_t)},
|
||||
{BITWISE_CPU_KERNEL_MATCH(kNumberTypeUInt64, uint64_t)},
|
||||
};
|
||||
return func_list;
|
||||
}
|
||||
|
|
|
@ -44,7 +44,7 @@ TypePtr BitwiseAndInferType(const PrimitivePtr &prim, const std::vector<Abstract
|
|||
std::map<std::string, TypePtr> types;
|
||||
(void)types.emplace("x", input_args[0]->BuildType());
|
||||
(void)types.emplace("y", input_args[1]->BuildType());
|
||||
const std::set<TypePtr> valid_types = {kInt8, kInt16, kInt32, kInt64, kUInt8, kUInt16, kUInt32, kUInt64};
|
||||
const std::set<TypePtr> valid_types = {kBool, kInt8, kInt16, kInt32, kInt64, kUInt8, kUInt16, kUInt32, kUInt64};
|
||||
return CheckAndConvertUtils::CheckTensorTypeSame(types, valid_types, prim->name());
|
||||
}
|
||||
} // namespace
|
||||
|
|
|
@ -44,7 +44,7 @@ TypePtr BitwiseOrInferType(const PrimitivePtr &prim, const std::vector<AbstractB
|
|||
std::map<std::string, TypePtr> types;
|
||||
(void)types.emplace("x", input_args[0]->BuildType());
|
||||
(void)types.emplace("y", input_args[1]->BuildType());
|
||||
const std::set<TypePtr> valid_types = {kInt8, kInt16, kInt32, kInt64, kUInt8, kUInt16, kUInt32, kUInt64};
|
||||
const std::set<TypePtr> valid_types = {kBool, kInt8, kInt16, kInt32, kInt64, kUInt8, kUInt16, kUInt32, kUInt64};
|
||||
return CheckAndConvertUtils::CheckTensorTypeSame(types, valid_types, prim->name());
|
||||
}
|
||||
} // namespace
|
||||
|
|
|
@ -44,7 +44,7 @@ TypePtr BitwiseXorInferType(const PrimitivePtr &prim, const std::vector<Abstract
|
|||
std::map<std::string, TypePtr> types;
|
||||
(void)types.emplace("x", input_args[0]->BuildType());
|
||||
(void)types.emplace("y", input_args[1]->BuildType());
|
||||
const std::set<TypePtr> valid_types = {kInt8, kInt16, kInt32, kInt64, kUInt8, kUInt16, kUInt32, kUInt64};
|
||||
const std::set<TypePtr> valid_types = {kBool, kInt8, kInt16, kInt32, kInt64, kUInt8, kUInt16, kUInt32, kUInt64};
|
||||
return CheckAndConvertUtils::CheckTensorTypeSame(types, valid_types, prim->name());
|
||||
}
|
||||
} // namespace
|
||||
|
|
|
@ -301,22 +301,16 @@ class Tensor(Tensor_):
|
|||
return tensor_operator_registry.get('__add__')(self, other)
|
||||
|
||||
def __and__(self, other):
|
||||
if Tensor._use_logical_kernel(self, other):
|
||||
return tensor_operator_registry.get('logical_and')(self, other)
|
||||
if isinstance(other, (int, bool, float, Tensor)):
|
||||
return tensor_operator_registry.get('bitwise_and')(self, other)
|
||||
raise TypeError("Unsupported operand type(s) for &: 'Tensor' and '{}'".format(type(other)))
|
||||
|
||||
def __xor__(self, other):
|
||||
if Tensor._use_logical_kernel(self, other):
|
||||
return tensor_operator_registry.get('logical_xor')(self, other)
|
||||
if isinstance(other, (int, bool, float, Tensor)):
|
||||
return tensor_operator_registry.get('bitwise_xor')(self, other)
|
||||
raise TypeError("Unsupported operand type(s) for ^: 'Tensor' and '{}'".format(type(other)))
|
||||
|
||||
def __or__(self, other):
|
||||
if Tensor._use_logical_kernel(self, other):
|
||||
return tensor_operator_registry.get('logical_or')(self, other)
|
||||
if isinstance(other, (int, bool, float, Tensor)):
|
||||
return tensor_operator_registry.get('bitwise_or')(self, other)
|
||||
raise TypeError("Unsupported operand type(s) for |: 'Tensor' and '{}'".format(type(other)))
|
||||
|
@ -513,19 +507,6 @@ class Tensor(Tensor_):
|
|||
|
||||
return Tensor(Tensor_.from_numpy(array))
|
||||
|
||||
@staticmethod
|
||||
def _use_logical_kernel(me, other) -> bool:
|
||||
"""
|
||||
Decide to use logical kernel or bitwise kernel for &|^ operations.
|
||||
If self or other is bool or bool tensor, then return true, use logical kernel,
|
||||
else false to use bitwise kernel.
|
||||
"""
|
||||
def _is_bool_or_bool_tensor(data):
|
||||
return isinstance(data, bool) or (isinstance(data, Tensor) and data.dtype == mstype.bool_)
|
||||
if _is_bool_or_bool_tensor(me) and _is_bool_or_bool_tensor(other):
|
||||
return True
|
||||
return False
|
||||
|
||||
def ndimension(self):
|
||||
r"""
|
||||
Alias for :func:`mindspore.Tensor.ndim`.
|
||||
|
|
|
@ -18,6 +18,7 @@ import pytest
|
|||
|
||||
import mindspore.context as context
|
||||
import mindspore.nn as nn
|
||||
import mindspore as ms
|
||||
from mindspore import Tensor
|
||||
from mindspore.ops import operations as P
|
||||
from mindspore.ops import functional as F
|
||||
|
@ -37,18 +38,23 @@ class OpNetWrapper(nn.Cell):
|
|||
return self.op(*inputs)
|
||||
|
||||
|
||||
suport_type_list = [np.bool_, np.int8, np.int16, np.int32, np.int64, np.uint8, np.uint16, np.uint32, np.uint64]
|
||||
mode_list = [context.PYNATIVE_MODE, context.GRAPH_MODE]
|
||||
|
||||
|
||||
@pytest.mark.level0
|
||||
@pytest.mark.platform_x86_cpu
|
||||
@pytest.mark.env_onecard
|
||||
@pytest.mark.parametrize('shape', [(2,), (4, 5), (3, 4, 5, 6), (3, 4, 5, 6, 2)])
|
||||
@pytest.mark.parametrize('dtype', [np.int8, np.int16, np.int32, np.int64, np.uint8, np.uint16, np.uint32, np.uint64])
|
||||
def test_bitwise_and(shape, dtype):
|
||||
@pytest.mark.parametrize('dtype', suport_type_list)
|
||||
@pytest.mark.parametrize('mode_cpu', mode_list)
|
||||
def test_bitwise_and(shape, dtype, mode_cpu):
|
||||
"""
|
||||
Feature: BitwiseAnd cpu kernel.
|
||||
Description: test the rightness of BitwiseAnd cpu kernel.
|
||||
Expectation: Success.
|
||||
"""
|
||||
context.set_context(mode=context.GRAPH_MODE, device_target='CPU')
|
||||
context.set_context(mode=mode_cpu, device_target='CPU')
|
||||
op = P.BitwiseAnd()
|
||||
op_wrapper = OpNetWrapper(op)
|
||||
|
||||
|
@ -67,14 +73,15 @@ def test_bitwise_and(shape, dtype):
|
|||
@pytest.mark.platform_x86_cpu
|
||||
@pytest.mark.env_onecard
|
||||
@pytest.mark.parametrize('shape', [(2,), (4, 5), (3, 4, 5, 6), (3, 4, 5, 6, 2)])
|
||||
@pytest.mark.parametrize('dtype', [np.int8, np.int16, np.int32, np.int64, np.uint8, np.uint16, np.uint32, np.uint64])
|
||||
def test_bitwise_or(shape, dtype):
|
||||
@pytest.mark.parametrize('dtype', suport_type_list)
|
||||
@pytest.mark.parametrize('mode_cpu', mode_list)
|
||||
def test_bitwise_or(shape, dtype, mode_cpu):
|
||||
"""
|
||||
Feature: BitwiseOr cpu kernel.
|
||||
Description: test the rightness of BitwiseOr cpu kernel.
|
||||
Expectation: Success.
|
||||
"""
|
||||
context.set_context(mode=context.GRAPH_MODE, device_target='CPU')
|
||||
context.set_context(mode=mode_cpu, device_target='CPU')
|
||||
op = P.BitwiseOr()
|
||||
op_wrapper = OpNetWrapper(op)
|
||||
|
||||
|
@ -93,14 +100,15 @@ def test_bitwise_or(shape, dtype):
|
|||
@pytest.mark.platform_x86_cpu
|
||||
@pytest.mark.env_onecard
|
||||
@pytest.mark.parametrize('shape', [(2,), (4, 5), (3, 4, 5, 6), (3, 4, 5, 6, 2)])
|
||||
@pytest.mark.parametrize('dtype', [np.int8, np.int16, np.int32, np.int64, np.uint8, np.uint16, np.uint32, np.uint64])
|
||||
def test_bitwise_xor(shape, dtype):
|
||||
@pytest.mark.parametrize('dtype', suport_type_list)
|
||||
@pytest.mark.parametrize('mode_cpu', mode_list)
|
||||
def test_bitwise_xor(shape, dtype, mode_cpu):
|
||||
"""
|
||||
Feature: BitwiseXor cpu kernel.
|
||||
Description: test the rightness of BitwiseXor cpu kernel.
|
||||
Expectation: Success.
|
||||
"""
|
||||
context.set_context(mode=context.GRAPH_MODE, device_target='CPU')
|
||||
context.set_context(mode=mode_cpu, device_target='CPU')
|
||||
op = P.BitwiseXor()
|
||||
op_wrapper = OpNetWrapper(op)
|
||||
|
||||
|
@ -119,7 +127,7 @@ def test_bitwise_xor(shape, dtype):
|
|||
@pytest.mark.platform_x86_cpu
|
||||
@pytest.mark.env_onecard
|
||||
@pytest.mark.parametrize('op', [P.BitwiseAnd(), P.BitwiseOr(), P.BitwiseXor()])
|
||||
@pytest.mark.parametrize('dtype', [np.int8, np.int16, np.int32, np.int64, np.uint8, np.uint16, np.uint32, np.uint64])
|
||||
@pytest.mark.parametrize('dtype', suport_type_list)
|
||||
def test_bitwise_vmap(op, dtype):
|
||||
"""
|
||||
Feature: Bitwise cpu kernel.
|
||||
|
@ -151,7 +159,7 @@ def test_bitwise_vmap(op, dtype):
|
|||
@pytest.mark.level0
|
||||
@pytest.mark.platform_x86_cpu
|
||||
@pytest.mark.env_onecard
|
||||
@pytest.mark.parametrize('dtype', [np.int8, np.int16, np.int32, np.int64, np.uint8, np.uint16, np.uint32, np.uint64])
|
||||
@pytest.mark.parametrize('dtype', suport_type_list)
|
||||
@pytest.mark.parametrize('mode', [context.GRAPH_MODE, context.PYNATIVE_MODE])
|
||||
@pytest.mark.parametrize('shape', [(2,), (4, 5), (3, 4, 5, 6), (3, 4, 5, 6, 2)])
|
||||
def test_bitwise_and_tensor_interface_operator(dtype, mode, shape):
|
||||
|
@ -173,7 +181,7 @@ def test_bitwise_and_tensor_interface_operator(dtype, mode, shape):
|
|||
@pytest.mark.level0
|
||||
@pytest.mark.platform_x86_cpu
|
||||
@pytest.mark.env_onecard
|
||||
@pytest.mark.parametrize('dtype', [np.int8, np.int16, np.int32, np.int64, np.uint8, np.uint16, np.uint32, np.uint64])
|
||||
@pytest.mark.parametrize('dtype', suport_type_list)
|
||||
@pytest.mark.parametrize('mode', [context.GRAPH_MODE, context.PYNATIVE_MODE])
|
||||
@pytest.mark.parametrize('shape', [(2,), (4, 5), (3, 4, 5, 6), (3, 4, 5, 6, 2)])
|
||||
def test_bitwise_or_tensor_interface_operator(dtype, mode, shape):
|
||||
|
@ -195,7 +203,7 @@ def test_bitwise_or_tensor_interface_operator(dtype, mode, shape):
|
|||
@pytest.mark.level0
|
||||
@pytest.mark.platform_x86_cpu
|
||||
@pytest.mark.env_onecard
|
||||
@pytest.mark.parametrize('dtype', [np.int8, np.int16, np.int32, np.int64, np.uint8, np.uint16, np.uint32, np.uint64])
|
||||
@pytest.mark.parametrize('dtype', suport_type_list)
|
||||
@pytest.mark.parametrize('mode', [context.GRAPH_MODE, context.PYNATIVE_MODE])
|
||||
@pytest.mark.parametrize('shape', [(2,), (4, 5), (3, 4, 5, 6), (3, 4, 5, 6, 2)])
|
||||
def test_bitwise_xor_tensor_interface_operator(dtype, mode, shape):
|
||||
|
@ -217,7 +225,7 @@ def test_bitwise_xor_tensor_interface_operator(dtype, mode, shape):
|
|||
@pytest.mark.level0
|
||||
@pytest.mark.platform_x86_cpu
|
||||
@pytest.mark.env_onecard
|
||||
@pytest.mark.parametrize('dtype', [np.int8, np.int16, np.int32, np.int64, np.uint8, np.uint16, np.uint32, np.uint64])
|
||||
@pytest.mark.parametrize('dtype', suport_type_list)
|
||||
@pytest.mark.parametrize('mode', [context.GRAPH_MODE, context.PYNATIVE_MODE])
|
||||
@pytest.mark.parametrize('shape', [(2,), (4, 5), (3, 4, 5, 6), (3, 4, 5, 6, 2)])
|
||||
def test_bitwise_and_tensor_interface(dtype, mode, shape):
|
||||
|
@ -239,7 +247,7 @@ def test_bitwise_and_tensor_interface(dtype, mode, shape):
|
|||
@pytest.mark.level0
|
||||
@pytest.mark.platform_x86_cpu
|
||||
@pytest.mark.env_onecard
|
||||
@pytest.mark.parametrize('dtype', [np.int8, np.int16, np.int32, np.int64, np.uint8, np.uint16, np.uint32, np.uint64])
|
||||
@pytest.mark.parametrize('dtype', suport_type_list)
|
||||
@pytest.mark.parametrize('mode', [context.GRAPH_MODE, context.PYNATIVE_MODE])
|
||||
@pytest.mark.parametrize('shape', [(2,), (4, 5), (3, 4, 5, 6), (3, 4, 5, 6, 2)])
|
||||
def test_bitwise_or_tensor_interface(dtype, mode, shape):
|
||||
|
@ -261,7 +269,7 @@ def test_bitwise_or_tensor_interface(dtype, mode, shape):
|
|||
@pytest.mark.level0
|
||||
@pytest.mark.platform_x86_cpu
|
||||
@pytest.mark.env_onecard
|
||||
@pytest.mark.parametrize('dtype', [np.int8, np.int16, np.int32, np.int64, np.uint8, np.uint16, np.uint32, np.uint64])
|
||||
@pytest.mark.parametrize('dtype', suport_type_list)
|
||||
@pytest.mark.parametrize('mode', [context.GRAPH_MODE, context.PYNATIVE_MODE])
|
||||
@pytest.mark.parametrize('shape', [(2,), (4, 5), (3, 4, 5, 6), (3, 4, 5, 6, 2)])
|
||||
def test_bitwise_xor_tensor_interface(dtype, mode, shape):
|
||||
|
@ -285,7 +293,7 @@ def test_bitwise_xor_tensor_interface(dtype, mode, shape):
|
|||
@pytest.mark.env_onecard
|
||||
@pytest.mark.parametrize('xshape', [(2, 3)])
|
||||
@pytest.mark.parametrize('yshape', [(1, 1), (1, 3), (2, 1)])
|
||||
@pytest.mark.parametrize('dtype', [np.int8, np.int16, np.int32, np.int64, np.uint8, np.uint16, np.uint32, np.uint64])
|
||||
@pytest.mark.parametrize('dtype', suport_type_list)
|
||||
def test_bitwise_and_broadcast(xshape, yshape, dtype):
|
||||
"""
|
||||
Feature: BitwiseAnd cpu kernel.
|
||||
|
@ -312,7 +320,7 @@ def test_bitwise_and_broadcast(xshape, yshape, dtype):
|
|||
@pytest.mark.env_onecard
|
||||
@pytest.mark.parametrize('xshape', [(2, 3)])
|
||||
@pytest.mark.parametrize('yshape', [(1, 1), (1, 3), (2, 1)])
|
||||
@pytest.mark.parametrize('dtype', [np.int8, np.int16, np.int32, np.int64, np.uint8, np.uint16, np.uint32, np.uint64])
|
||||
@pytest.mark.parametrize('dtype', suport_type_list)
|
||||
def test_bitwise_or_broadcast(xshape, yshape, dtype):
|
||||
"""
|
||||
Feature: BitwiseOr cpu kernel.
|
||||
|
@ -339,7 +347,7 @@ def test_bitwise_or_broadcast(xshape, yshape, dtype):
|
|||
@pytest.mark.env_onecard
|
||||
@pytest.mark.parametrize('xshape', [(2, 3)])
|
||||
@pytest.mark.parametrize('yshape', [(1, 1), (1, 3), (2, 1)])
|
||||
@pytest.mark.parametrize('dtype', [np.int8, np.int16, np.int32, np.int64, np.uint8, np.uint16, np.uint32, np.uint64])
|
||||
@pytest.mark.parametrize('dtype', suport_type_list)
|
||||
def test_bitwise_xor_broadcast(xshape, yshape, dtype):
|
||||
"""
|
||||
Feature: BitwiseXor cpu kernel.
|
||||
|
@ -359,3 +367,46 @@ def test_bitwise_xor_broadcast(xshape, yshape, dtype):
|
|||
|
||||
assert np.allclose(outputs.asnumpy(), expect)
|
||||
assert np.allclose(outputs_functional.asnumpy(), expect)
|
||||
|
||||
|
||||
class NetBitwise(nn.Cell):
|
||||
"""NetBitwise"""
|
||||
|
||||
def construct(self, input_x, input_y):
|
||||
"""construct"""
|
||||
out_and = input_x & input_y
|
||||
out_or = input_x | input_y
|
||||
out_xor = input_x ^ input_y
|
||||
return out_and, out_or, out_xor
|
||||
|
||||
|
||||
@pytest.mark.level0
|
||||
@pytest.mark.platform_x86_cpu
|
||||
@pytest.mark.env_onecard
|
||||
@pytest.mark.parametrize('mode_cpu', mode_list)
|
||||
def test_bitwise_bool(mode_cpu):
|
||||
"""
|
||||
Feature: Bitwise cpu kernel.
|
||||
Description: test the rightness of Bitwise cpu kernel tensor operations.
|
||||
Expectation: Success.
|
||||
"""
|
||||
context.set_context(mode=mode_cpu, device_target='CPU')
|
||||
|
||||
input_x = ms.Tensor([True, False], dtype=ms.bool_)
|
||||
input_y = ms.Tensor([True, True], dtype=ms.bool_)
|
||||
|
||||
net = NetBitwise()
|
||||
out = net(input_x, input_y)
|
||||
expect_and = np.array([True, False])
|
||||
expect_or = np.array([True, True])
|
||||
expect_xor = np.array([False, True])
|
||||
assert np.allclose(out[0].asnumpy(), expect_and)
|
||||
assert np.allclose(out[1].asnumpy(), expect_or)
|
||||
assert np.allclose(out[2].asnumpy(), expect_xor)
|
||||
|
||||
res_and = input_x & input_y
|
||||
res_or = input_x | input_y
|
||||
res_xor = input_x ^ input_y
|
||||
assert np.allclose(res_and.asnumpy(), expect_and)
|
||||
assert np.allclose(res_or.asnumpy(), expect_or)
|
||||
assert np.allclose(res_xor.asnumpy(), expect_xor)
|
||||
|
|
|
@ -0,0 +1,164 @@
|
|||
# Copyright 2022 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
|
||||
import mindspore as ms
|
||||
from mindspore import Tensor
|
||||
from mindspore.ops import operations as P
|
||||
from mindspore.ops import functional as F
|
||||
|
||||
|
||||
class OpNetWrapperBitwise(nn.Cell):
|
||||
"""OpNetWrapperBitwise"""
|
||||
|
||||
def __init__(self, op):
|
||||
"""__init__"""
|
||||
super(OpNetWrapperBitwise, self).__init__()
|
||||
self.op = op
|
||||
|
||||
def construct(self, *inputs):
|
||||
"""construct"""
|
||||
return self.op(*inputs)
|
||||
|
||||
|
||||
suport_type_list = [np.bool_, np.int8, np.int16, np.int32, np.int64, np.uint8, np.uint16, np.uint32, np.uint64]
|
||||
mode_list = [context.PYNATIVE_MODE, context.GRAPH_MODE]
|
||||
|
||||
|
||||
@pytest.mark.level0
|
||||
@pytest.mark.platform_x86_gpu_training
|
||||
@pytest.mark.env_onecard
|
||||
@pytest.mark.parametrize('shape', [(2,), (4, 5), (3, 4, 5, 6), (3, 4, 5, 6, 2)])
|
||||
@pytest.mark.parametrize('dtype', suport_type_list)
|
||||
@pytest.mark.parametrize('mode', mode_list)
|
||||
def test_bitwise_and(shape, dtype, mode):
|
||||
"""
|
||||
Feature: BitwiseAnd gpu kernel.
|
||||
Description: test the rightness of BitwiseAnd gpu kernel.
|
||||
Expectation: Success.
|
||||
"""
|
||||
context.set_context(mode=mode, device_target='GPU')
|
||||
op = P.BitwiseAnd()
|
||||
op_wrapper = OpNetWrapperBitwise(op)
|
||||
|
||||
prop = 100 if np.random.random() > 0.5 else -100
|
||||
x_np = (np.random.randn(*shape) * prop).astype(dtype)
|
||||
y_np = (np.random.randn(*shape) * prop).astype(dtype)
|
||||
outputs = op_wrapper(Tensor(x_np), Tensor(y_np))
|
||||
outputs_func = F.bitwise_and(Tensor(x_np), Tensor(y_np))
|
||||
expect = np.bitwise_and(x_np, y_np)
|
||||
|
||||
assert np.allclose(outputs.asnumpy(), expect)
|
||||
assert np.allclose(outputs_func.asnumpy(), expect)
|
||||
|
||||
|
||||
@pytest.mark.level0
|
||||
@pytest.mark.platform_x86_gpu_training
|
||||
@pytest.mark.env_onecard
|
||||
@pytest.mark.parametrize('shape', [(2,), (4, 5), (3, 4, 5, 6), (3, 4, 5, 6, 2)])
|
||||
@pytest.mark.parametrize('dtype', suport_type_list)
|
||||
@pytest.mark.parametrize('mode', mode_list)
|
||||
def test_bitwise_or(shape, dtype, mode):
|
||||
"""
|
||||
Feature: BitwiseOr gpu kernel.
|
||||
Description: test the rightness of BitwiseOr gpu kernel.
|
||||
Expectation: Success.
|
||||
"""
|
||||
context.set_context(mode=mode, device_target='GPU')
|
||||
op = P.BitwiseOr()
|
||||
op_wrapper = OpNetWrapperBitwise(op)
|
||||
|
||||
prop = 100 if np.random.random() > 0.5 else -100
|
||||
x_np = (np.random.randn(*shape) * prop).astype(dtype)
|
||||
y_np = (np.random.randn(*shape) * prop).astype(dtype)
|
||||
outputs = op_wrapper(Tensor(x_np), Tensor(y_np))
|
||||
outputs_func = F.bitwise_or(Tensor(x_np), Tensor(y_np))
|
||||
expect = np.bitwise_or(x_np, y_np)
|
||||
|
||||
assert np.allclose(outputs.asnumpy(), expect)
|
||||
assert np.allclose(outputs_func.asnumpy(), expect)
|
||||
|
||||
|
||||
@pytest.mark.level0
|
||||
@pytest.mark.platform_x86_gpu_training
|
||||
@pytest.mark.env_onecard
|
||||
@pytest.mark.parametrize('shape', [(2,), (4, 5), (3, 4, 5, 6), (3, 4, 5, 6, 2)])
|
||||
@pytest.mark.parametrize('dtype', suport_type_list)
|
||||
@pytest.mark.parametrize('mode', mode_list)
|
||||
def test_bitwise_xor(shape, dtype, mode):
|
||||
"""
|
||||
Feature: BitwiseXor gpu kernel.
|
||||
Description: test the rightness of BitwiseXor gpu kernel.
|
||||
Expectation: Success.
|
||||
"""
|
||||
context.set_context(mode=mode, device_target='GPU')
|
||||
op = P.BitwiseXor()
|
||||
op_wrapper = OpNetWrapperBitwise(op)
|
||||
|
||||
prop = 100 if np.random.random() > 0.5 else -100
|
||||
x_np = (np.random.randn(*shape) * prop).astype(dtype)
|
||||
y_np = (np.random.randn(*shape) * prop).astype(dtype)
|
||||
outputs = op_wrapper(Tensor(x_np), Tensor(y_np))
|
||||
outputs_func = F.bitwise_xor(Tensor(x_np), Tensor(y_np))
|
||||
expect = np.bitwise_xor(x_np, y_np)
|
||||
|
||||
assert np.allclose(outputs.asnumpy(), expect)
|
||||
assert np.allclose(outputs_func.asnumpy(), expect)
|
||||
|
||||
|
||||
class NetBitwiseGPU(nn.Cell):
|
||||
"""NetBitwiseGPU"""
|
||||
|
||||
def construct(self, input_x, input_y):
|
||||
"""construct"""
|
||||
out_and = input_x & input_y
|
||||
out_or = input_x | input_y
|
||||
out_xor = input_x ^ input_y
|
||||
return out_and, out_or, out_xor
|
||||
|
||||
|
||||
@pytest.mark.level0
|
||||
@pytest.mark.platform_x86_gpu_training
|
||||
@pytest.mark.env_onecard
|
||||
@pytest.mark.parametrize('mode', mode_list)
|
||||
def test_bitwise_bool(mode):
|
||||
"""
|
||||
Feature: Bitwise gpu kernel.
|
||||
Description: test the rightness of Bitwise cpu kernel tensor operations.
|
||||
Expectation: Success.
|
||||
"""
|
||||
context.set_context(mode=mode, device_target='CPU')
|
||||
|
||||
input_x = ms.Tensor([True, False], dtype=ms.bool_)
|
||||
input_y = ms.Tensor([True, True], dtype=ms.bool_)
|
||||
|
||||
net = NetBitwiseGPU()
|
||||
out = net(input_x, input_y)
|
||||
expect_and_gpu = np.array([True, False])
|
||||
expect_or_gpu = np.array([True, True])
|
||||
expect_xor_gpu = np.array([False, True])
|
||||
assert np.allclose(out[0].asnumpy(), expect_and_gpu)
|
||||
assert np.allclose(out[1].asnumpy(), expect_or_gpu)
|
||||
assert np.allclose(out[2].asnumpy(), expect_xor_gpu)
|
||||
|
||||
res_and = input_x & input_y
|
||||
res_or = input_x | input_y
|
||||
res_xor = input_x ^ input_y
|
||||
assert np.allclose(res_and.asnumpy(), expect_and_gpu)
|
||||
assert np.allclose(res_or.asnumpy(), expect_or_gpu)
|
||||
assert np.allclose(res_xor.asnumpy(), expect_xor_gpu)
|
Loading…
Reference in New Issue