You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

test_maximum_grad.py 2.6 kB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. # Copyright 2020-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. import numpy as np
  16. import pytest
  17. import mindspore.context as context
  18. from mindspore import Tensor
  19. from mindspore.nn import Cell
  20. import mindspore.ops.operations._grad_ops as G
  21. class MaxmumGradNet(Cell):
  22. def __init__(self):
  23. super(MaxmumGradNet, self).__init__()
  24. self.maximum_grad = G.MaximumGrad()
  25. def construct(self, x, y, dy):
  26. return self.maximum_grad(x, y, dy)
  27. def gen_data():
  28. np.random.seed(0)
  29. input_x_np = np.random.normal(0, 1, [2, 3]).astype(np.float32)
  30. input_y_np = np.random.normal(0, 1, [1]).astype(np.float32)
  31. input_dout_np = np.maximum(input_x_np, input_y_np).astype(np.float32)
  32. input_x = Tensor(input_x_np)
  33. input_y = Tensor(input_y_np)
  34. input_dout = Tensor(input_dout_np)
  35. return input_x, input_y, input_dout
  36. def get_maximum_grad_output(input_x, input_y, input_dout, enable_graph_kernel=False):
  37. context.set_context(enable_graph_kernel=enable_graph_kernel)
  38. net = MaxmumGradNet()
  39. result = net(input_x, input_y, input_dout)
  40. return result[0].asnumpy(), result[1].asnumpy()
  41. def test_maximum_grad():
  42. input_x, input_y, input_dout = gen_data()
  43. result_off = get_maximum_grad_output(input_x, input_y, input_dout, False)
  44. result_on = get_maximum_grad_output(input_x, input_y, input_dout, True)
  45. assert np.allclose(result_on[0], result_off[0], rtol=1.e-4, atol=1.e-8, equal_nan=True)
  46. assert np.allclose(result_on[1], result_off[1], rtol=1.e-4, atol=1.e-8, equal_nan=True)\
  47. @pytest.mark.level0
  48. @pytest.mark.platform_x86_gpu_training
  49. @pytest.mark.env_onecard
  50. def test_maximum_grad_gpu():
  51. context.set_context(mode=context.GRAPH_MODE, device_target="GPU")
  52. test_maximum_grad()
  53. @pytest.mark.level0
  54. @pytest.mark.platform_arm_ascend_training
  55. @pytest.mark.platform_x86_ascend_training
  56. @pytest.mark.env_onecard
  57. def test_maximum_grad_ascend():
  58. context.set_context(mode=context.GRAPH_MODE, device_target="Ascend")
  59. test_maximum_grad()