1# Copyright 2021 Huawei Technologies Co., Ltd 2# 3# Licensed under the Apache License, Version 2.0 (the "License"); 4# you may not use this file except in compliance with the License. 5# You may obtain a copy of the License at 6# 7# http://www.apache.org/licenses/LICENSE-2.0 8# 9# Unless required by applicable law or agreed to in writing, software 10# distributed under the License is distributed on an "AS IS" BASIS, 11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12# See the License for the specific language governing permissions and 13# limitations under the License. 14# ============================================================================ 15 16import numpy as np 17import pytest 18import mindspore.context as context 19import mindspore.nn as nn 20from mindspore import Tensor 21from mindspore.ops import operations as P 22 23 24class EqualCount(nn.Cell): 25 def __init__(self): 26 super(EqualCount, self).__init__() 27 self.op = P.EqualCount() 28 29 def construct(self, *inp): 30 return self.op(*inp) 31 32def get_output(*inp, enable_graph_kernel=False): 33 context.set_context(enable_graph_kernel=enable_graph_kernel) 34 output = EqualCount()(*inp) 35 return output 36 37def basic_test(datatype): 38 x = Tensor(np.array([[1, 1, 1, 1], [3, 3, 3, 3]]).astype(datatype)) 39 y = Tensor(np.array([[1, 2, 1, 2], [1, 1, 3, 3]]).astype(datatype)) 40 expect = get_output(x, y, enable_graph_kernel=False) 41 output = get_output(x, y, enable_graph_kernel=True) 42 expect_np = expect.asnumpy().copy() 43 output_np = output.asnumpy().copy() 44 assert np.allclose(expect_np, output_np, 1.e-4, 1.e-7) 45 46@pytest.mark.level0 47@pytest.mark.platform_x86_gpu_training 48@pytest.mark.env_onecard 49def test_gpu_fp16(): 50 context.set_context(mode=context.GRAPH_MODE, device_target="GPU") 51 basic_test(np.float16) 52 53@pytest.mark.level0 54@pytest.mark.platform_x86_gpu_training 55@pytest.mark.env_onecard 56def test_gpu_fp32(): 57 context.set_context(mode=context.GRAPH_MODE, device_target="GPU") 58 basic_test(np.float32) 59