1# Copyright 2023 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 18 19from mindspore import ops 20import mindspore as ms 21 22 23@ms.jit 24def greater_forward_func(x, y): 25 return ops.greater(x, y) 26 27 28@ms.jit 29def greater_backward_func(x, y): 30 return ops.grad(greater_forward_func, (0,))(x, y) 31 32 33@pytest.mark.level1 34@pytest.mark.env_onecard 35@pytest.mark.platform_x86_cpu 36@pytest.mark.platform_x86_gpu_training 37@pytest.mark.platform_arm_ascend_training 38def test_greater_forward(): 39 """ 40 Feature: Ops. 41 Description: test op greater. 42 Expectation: expect correct result. 43 """ 44 x = ms.Tensor(np.array([1, 2, 3]), ms.int32) 45 y = ms.Tensor(np.array([1, 1, 4]), ms.int32) 46 expect_out = np.array([False, True, False]) 47 out = greater_forward_func(x, y) 48 assert np.allclose(out.asnumpy(), expect_out) 49 50 51@pytest.mark.level1 52@pytest.mark.env_onecard 53@pytest.mark.platform_x86_cpu 54@pytest.mark.platform_x86_gpu_training 55@pytest.mark.platform_arm_ascend_training 56def test_greater_backward(): 57 """ 58 Feature: Auto grad. 59 Description: test auto grad of op greater. 60 Expectation: expect correct result. 61 """ 62 x = ms.Tensor(np.array([1, 2, 3]), ms.int32) 63 y = ms.Tensor(np.array([1, 1, 4]), ms.int32) 64 expect_out = np.array([0, 0, 0]) 65 grads = greater_backward_func(x, y) 66 assert np.allclose(grads.asnumpy(), expect_out) 67 68 69@pytest.mark.level1 70@pytest.mark.env_onecard 71@pytest.mark.platform_x86_cpu 72@pytest.mark.platform_x86_gpu_training 73def test_greater_vmap(): 74 """ 75 Feature: test vmap function. 76 Description: test greater op vmap. 77 Expectation: expect correct result. 78 """ 79 in_axes = -1 80 x = ms.Tensor(np.array([[[1, 2, 3]]]), ms.int32) 81 y = ms.Tensor(np.array([[[1, 1, 4]]]), ms.int32) 82 expect_out = np.array([[[False]], [[True]], [[False]]]) 83 nest_vmap = ops.vmap(ops.vmap( 84 greater_forward_func, in_axes=in_axes, out_axes=0), in_axes=in_axes, out_axes=0) 85 out = nest_vmap(x, y) 86 assert np.allclose(out.asnumpy(), expect_out) 87