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 ReduceMax(nn.Cell): 25 def __init__(self, keep_dims): 26 super(ReduceMax, self).__init__() 27 self.reduce_max = P.ReduceMax(keep_dims) 28 29 def construct(self, x, axis): 30 return self.reduce_max(x, axis) 31 32 33def get_output(x, axis, keep_dims, enable_graph_kernel=False): 34 context.set_context(enable_graph_kernel=enable_graph_kernel) 35 net = ReduceMax(keep_dims) 36 output = net(x, axis) 37 return output 38 39 40def test_reduce_max(): 41 x0 = Tensor(np.random.normal(0, 1, [2, 3, 4, 4]).astype(np.float32)) 42 axis0 = 3 43 keep_dims0 = True 44 expect = get_output(x0, axis0, keep_dims0, False) 45 output = get_output(x0, axis0, keep_dims0, True) 46 assert np.allclose(expect.asnumpy(), output.asnumpy(), 0.0001, 0.0001) 47 48 x1 = Tensor(np.random.normal(0, 1, [2, 3, 4, 4]).astype(np.float32)) 49 axis1 = 3 50 keep_dims1 = False 51 expect = get_output(x1, axis1, keep_dims1, False) 52 output = get_output(x1, axis1, keep_dims1, True) 53 assert np.allclose(expect.asnumpy(), output.asnumpy(), 0.0001, 0.0001) 54 55 x2 = Tensor(np.random.normal(0, 1, [2, 3, 1, 4]).astype(np.float32)) 56 axis2 = 2 57 keep_dims2 = True 58 expect = get_output(x2, axis2, keep_dims2, False) 59 output = get_output(x2, axis2, keep_dims2, True) 60 assert np.allclose(expect.asnumpy(), output.asnumpy(), 0.0001, 0.0001) 61 62 63@pytest.mark.level0 64@pytest.mark.platform_x86_gpu_training 65@pytest.mark.env_onecard 66def test_reduce_max_gpu(): 67 context.set_context(mode=context.GRAPH_MODE, device_target="GPU") 68 test_reduce_max() 69