mindspore/tests/st/ops/cpu/test_square_sum_all.py

133 lines
4.3 KiB
Python

# 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
from mindspore import Tensor
from mindspore.nn import Cell
import mindspore.ops.operations as P
class Net(Cell):
def __init__(self):
super(Net, self).__init__()
self.squaresumall = P.SquareSumAll()
def construct(self, x0, x1):
return self.squaresumall(x0, x1)
def run_net(datatype, input_tensors, output_tensors):
inp0 = Tensor(np.array(input_tensors[0]).astype(datatype))
inp1 = Tensor(np.array(input_tensors[1]).astype(datatype))
net = Net()
[output0, output1] = net(inp0, inp1)
expect0 = Tensor(output_tensors[0])
expect1 = Tensor(output_tensors[1])
assert output0 == expect0
assert output1 == expect1
assert output0.dtype == inp0.dtype
assert output1.dtype == inp1.dtype
@pytest.mark.level0
@pytest.mark.platform_x86_cpu
@pytest.mark.env_onecard
@pytest.mark.parametrize('dtype', [np.float16, np.float32])
@pytest.mark.parametrize('input_tensors, output_tensors', [
([[1, 2, 4], [0, 1, -1]], [21, 2]),
([[[1, 2], [3, 4]], [[0, 0], [1, 0]]], [30, 1])
])
def test_cpu(dtype, input_tensors, output_tensors):
"""
Feature: SquareSumAll cpu op.
Description: test data type is float16 and float32.
Expectation: success.
"""
context.set_context(mode=context.GRAPH_MODE, device_target="CPU")
run_net(dtype, input_tensors, output_tensors)
@pytest.mark.level0
@pytest.mark.platform_x86_cpu
@pytest.mark.env_onecard
def test_cpu_exception_dtype_diff():
"""
Feature: SquareSumAll cpu op.
Description: Test data type of two input tensors is different.
Expectation: Throw TypeError exception.
"""
context.set_context(mode=context.GRAPH_MODE, device_target="CPU")
with pytest.raises(TypeError):
inp0 = Tensor(np.array([1, 2, 4]).astype(np.float16))
inp1 = Tensor(np.array([0, 1, -1]).astype(np.float32))
net = Net()
_ = net(inp0, inp1)
@pytest.mark.level0
@pytest.mark.platform_x86_cpu
@pytest.mark.env_onecard
def test_cpu_exception_dtype_not_support():
"""
Feature: SquareSumAll cpu op.
Description: Test unsupported data type.
Expectation: Throw TypeError exception.
"""
context.set_context(mode=context.GRAPH_MODE, device_target="CPU")
with pytest.raises(TypeError):
inp0 = Tensor(np.array([1, 2, 4]).astype(np.float64))
inp1 = Tensor(np.array([0, 1, -1]).astype(np.float64))
net = Net()
_ = net(inp0, inp1)
@pytest.mark.level0
@pytest.mark.platform_x86_cpu
@pytest.mark.env_onecard
def test_cpu_exception_shape_diff():
"""
Feature: SquareSumAll cpu op.
Description: Test shape of two input tensors is different.
Expectation: Throw ValueError exception.
"""
context.set_context(mode=context.GRAPH_MODE, device_target="CPU")
with pytest.raises(ValueError):
inp0 = Tensor(np.array([1, 2, 4]).astype(np.float32))
inp1 = Tensor(np.array([0, 1]).astype(np.float32))
net = Net()
_ = net(inp0, inp1)
@pytest.mark.level0
@pytest.mark.platform_x86_cpu
@pytest.mark.env_onecard
def test_cpu_float16():
"""
Feature: SquareSumAll cpu op.
Description: test data type is float16.
Expectation: success.
"""
context.set_context(mode=context.GRAPH_MODE, device_target="CPU")
inp0 = Tensor(np.arange(0, 1, 0.001).astype(np.float16))
inp1 = Tensor(np.arange(0, 1, 0.001).astype(np.float16))
net = Net()
[output0, output1] = net(inp0, inp1)
expect0 = np.array(332.75).astype(np.float16)
expect1 = np.array(332.75).astype(np.float16)
assert output0.asnumpy() == expect0
assert output1.asnumpy() == expect1