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