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_reciprocal_grad_op.py 3.6 kB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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. import numpy as np
  16. import pytest
  17. import mindspore.context as context
  18. import mindspore.nn as nn
  19. from mindspore import Tensor
  20. from mindspore.ops.operations import _grad_ops as G
  21. class NetReciprocalGrad(nn.Cell):
  22. def __init__(self):
  23. super(NetReciprocalGrad, self).__init__()
  24. self.grad = G.ReciprocalGrad()
  25. def construct(self, y, dy):
  26. return self.grad(y, dy)
  27. @pytest.mark.level0
  28. @pytest.mark.platform_x86_gpu_training
  29. @pytest.mark.env_onecard
  30. def test_reciprocal_grad_float32():
  31. context.set_context(mode=context.PYNATIVE_MODE, device_target="GPU")
  32. y = Tensor(np.array([[[[-1, 1, 12],
  33. [5, 34, 6],
  34. [10, 2, -1]]]]).astype(np.float32))
  35. dy = Tensor(np.array([[[[29, 1, 55],
  36. [2.2, 63, 2],
  37. [3, 3, 12]]]]).astype(np.float32))
  38. expect = np.array([[[[-29, -1, -7920],
  39. [-55, -72828, -72],
  40. [-300, -12, -12]]]]).astype(np.float32)
  41. net = NetReciprocalGrad()
  42. output = net(y, dy)
  43. np.testing.assert_array_almost_equal(output.asnumpy(), expect)
  44. context.set_context(mode=context.GRAPH_MODE, device_target="GPU")
  45. y = Tensor(np.array([[[[-1, 1, 12],
  46. [5, 34, 6],
  47. [10, 2, -1]]]]).astype(np.float32))
  48. dy = Tensor(np.array([[[[29, 1, 55],
  49. [2.2, 63, 2],
  50. [3, 3, 12]]]]).astype(np.float32))
  51. expect = np.array([[[[-29, -1, -7920],
  52. [-55, -72828, -72],
  53. [-300, -12, -12]]]]).astype(np.float32)
  54. net = NetReciprocalGrad()
  55. output = net(y, dy)
  56. np.testing.assert_array_almost_equal(output.asnumpy(), expect)
  57. @pytest.mark.level0
  58. @pytest.mark.platform_x86_gpu_training
  59. @pytest.mark.env_onecard
  60. def test_reciprocal_grad_float16():
  61. context.set_context(mode=context.PYNATIVE_MODE, device_target="GPU")
  62. y = Tensor(np.array([[0.01, 0.2, 0.22],
  63. [10.002, 2, -1]]).astype(np.float16))
  64. dy = Tensor(np.array([[34, 1, 55],
  65. [3, 3, 63]]).astype(np.float16))
  66. expect = np.array([[-0.0034, -0.03998, -2.662],
  67. [-300, -12, -63]]).astype(np.float16)
  68. net = NetReciprocalGrad()
  69. output = net(y, dy)
  70. np.testing.assert_array_almost_equal(output.asnumpy(), expect)
  71. context.set_context(mode=context.GRAPH_MODE, device_target="GPU")
  72. y = Tensor(np.array([[0.01, 0.2, 0.22],
  73. [10.002, 2, -1]]).astype(np.float16))
  74. dy = Tensor(np.array([[34, 1, 55],
  75. [3, 3, 63]]).astype(np.float16))
  76. expect = np.array([[-0.0034, -0.03998, -2.662],
  77. [-300, -12, -63]]).astype(np.float16)
  78. net = NetReciprocalGrad()
  79. output = net(y, dy)
  80. np.testing.assert_array_almost_equal(output.asnumpy(), expect)