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_atomic_add.py 4.7 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. # Copyright 2020 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 as P
  21. class SumOutNet(Cell):
  22. def __init__(self):
  23. super(SumOutNet, self).__init__()
  24. self.square = P.Square()
  25. self.sum = P.ReduceSum()
  26. def construct(self, x):
  27. mul_res = self.square(x)
  28. return self.sum(mul_res, (0,))
  29. class SingleOutNet(Cell):
  30. def __init__(self):
  31. super(SingleOutNet, self).__init__()
  32. self.add = P.Add()
  33. self.mul = P.Mul()
  34. self.sum = P.ReduceSum()
  35. def construct(self, x, y):
  36. mul_res = self.mul(x, y)
  37. sum_res = self.sum(mul_res, ())
  38. return self.add(sum_res, x)
  39. class MultiOutNet(Cell):
  40. def __init__(self):
  41. super(MultiOutNet, self).__init__()
  42. self.add = P.Add()
  43. self.mul = P.Mul()
  44. self.sum = P.ReduceSum()
  45. def construct(self, x, y):
  46. add_res = self.add(x, y)
  47. mul_res = self.mul(add_res, add_res)
  48. sum_res = self.sum(mul_res, ())
  49. return self.add(add_res, sum_res)
  50. def atomic_add_sum_output():
  51. np.random.seed(0)
  52. input_x = np.random.normal(0, 1, [2, 3, 4, 3]).astype(np.float32)
  53. expect = np.sum(np.square(input_x), axis=(0,))
  54. net = SumOutNet()
  55. result = net(Tensor(input_x))
  56. res = np.allclose(expect, result.asnumpy(), rtol=1.e-4, atol=1.e-7, equal_nan=True)
  57. assert res
  58. def atomic_add_single_output():
  59. np.random.seed(0)
  60. input_x = np.random.normal(0, 1, [2, 2, 2, 256]).astype(np.float32)
  61. input_y = np.random.normal(0, 1, [2, 2, 2, 256]).astype(np.float32)
  62. expect = np.sum(input_x * input_y) + input_x
  63. net = SingleOutNet()
  64. result = net(Tensor(input_x), Tensor(input_y))
  65. res = np.allclose(expect, result.asnumpy(), rtol=1.e-4, atol=1.e-7, equal_nan=True)
  66. assert res
  67. def atomic_add_multi_output():
  68. np.random.seed(0)
  69. input_x = np.random.normal(0, 1, [2, 2, 2, 256]).astype(np.float32)
  70. input_y = np.random.normal(0, 1, [2, 2, 2, 256]).astype(np.float32)
  71. expect = np.sum(np.square(input_x + input_y)) + (input_x + input_y)
  72. net = MultiOutNet()
  73. result = net(Tensor(input_x), Tensor(input_y))
  74. res = np.allclose(expect, result.asnumpy(), rtol=1.e-4, atol=1.e-7, equal_nan=True)
  75. assert res
  76. @pytest.mark.level0
  77. @pytest.mark.platform_x86_gpu_training
  78. @pytest.mark.env_onecard
  79. def test_atomic_add_sum_output_gpu():
  80. context.set_context(mode=context.GRAPH_MODE, enable_graph_kernel=True, device_target="GPU")
  81. atomic_add_sum_output()
  82. @pytest.mark.level0
  83. @pytest.mark.platform_x86_gpu_training
  84. @pytest.mark.env_onecard
  85. def test_atomic_add_single_output_gpu():
  86. context.set_context(mode=context.GRAPH_MODE, enable_graph_kernel=True, device_target="GPU")
  87. atomic_add_single_output()
  88. @pytest.mark.level0
  89. @pytest.mark.platform_x86_gpu_training
  90. @pytest.mark.env_onecard
  91. def test_atomic_add_multi_output_gpu():
  92. context.set_context(mode=context.GRAPH_MODE, enable_graph_kernel=True, device_target="GPU")
  93. atomic_add_multi_output()
  94. @pytest.mark.level0
  95. @pytest.mark.platform_arm_ascend_training
  96. @pytest.mark.platform_x86_ascend_training
  97. @pytest.mark.env_onecard
  98. def test_atomic_add_sum_output_ascend():
  99. context.set_context(mode=context.GRAPH_MODE, enable_graph_kernel=True, device_target="Ascend")
  100. atomic_add_sum_output()
  101. @pytest.mark.level0
  102. @pytest.mark.platform_arm_ascend_training
  103. @pytest.mark.platform_x86_ascend_training
  104. @pytest.mark.env_onecard
  105. def test_atomic_add_single_output_ascend():
  106. context.set_context(mode=context.GRAPH_MODE, enable_graph_kernel=True, device_target="Ascend")
  107. atomic_add_single_output()
  108. @pytest.mark.level0
  109. @pytest.mark.platform_arm_ascend_training
  110. @pytest.mark.platform_x86_ascend_training
  111. @pytest.mark.env_onecard
  112. def test_atomic_add_multi_output_ascend():
  113. context.set_context(mode=context.GRAPH_MODE, enable_graph_kernel=True, device_target="Ascend")
  114. atomic_add_multi_output()