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_add_op.py 7.5 kB

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. # Copyright 2019-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.common.api import ms_function
  21. from mindspore.common.initializer import initializer
  22. from mindspore.common.parameter import Parameter
  23. from mindspore.ops import operations as P
  24. from mindspore.ops.operations import _inner_ops as inner
  25. class AddNet(nn.Cell):
  26. def __init__(self, nptype):
  27. super(AddNet, self).__init__()
  28. self.add = P.Add()
  29. np.random.seed(0)
  30. self.x = Parameter(initializer(
  31. Tensor(np.random.randn(2, 0).astype(nptype)), [2, 0]), name='x')
  32. self.y = Parameter(initializer(
  33. Tensor(np.random.randn(2, 1).astype(nptype)), [2, 1]), name='y')
  34. self.x1 = Parameter(initializer(
  35. Tensor(np.arange(3).reshape(3).astype(nptype)), [3]), name='x1')
  36. self.y1 = Parameter(initializer(
  37. Tensor(np.array([2]).astype(nptype)), [1]), name='y1')
  38. self.x2 = Parameter(initializer(
  39. Tensor(np.arange(3 * 3 * 3 * 3).reshape(3, 3, 3, 3).astype(nptype)), [3, 3, 3, 3]), name='x2')
  40. self.y2 = Parameter(initializer(
  41. Tensor(np.arange(3 * 3 * 3 * 3).reshape(3, 3, 3, 3).astype(nptype)), [3, 3, 3, 3]), name='y2')
  42. self.x3 = Parameter(initializer(
  43. Tensor(np.arange(1 * 1 * 3 * 3).reshape(1, 1, 3, 3).astype(nptype)), [1, 1, 3, 3]), name='x3')
  44. self.y3 = Parameter(initializer(
  45. Tensor(np.arange(3 * 3 * 3 * 3).reshape(3, 3, 3, 3).astype(nptype)), [3, 3, 3, 3]), name='y3')
  46. @ms_function
  47. def construct(self):
  48. return (
  49. self.add(self.x, self.y), self.add(self.x1, self.y1), self.add(self.x2, self.y2),
  50. self.add(self.x3, self.y3))
  51. def add(nptype):
  52. context.set_context(device_target='GPU')
  53. add_net = AddNet(nptype)
  54. output = add_net()
  55. expect0 = np.array([])
  56. expect1 = np.array([2, 3, 4]).astype(nptype)
  57. expect2 = np.array(
  58. [[[[0., 2., 4.],
  59. [6., 8., 10.],
  60. [12., 14., 16.]],
  61. [[18., 20., 22.],
  62. [24., 26., 28.],
  63. [30., 32., 34.]],
  64. [[36., 38., 40.],
  65. [42., 44., 46.],
  66. [48., 50., 52.]]],
  67. [[[54., 56., 58.],
  68. [60., 62., 64.],
  69. [66., 68., 70.]],
  70. [[72., 74., 76.],
  71. [78., 80., 82.],
  72. [84., 86., 88.]],
  73. [[90., 92., 94.],
  74. [96., 98., 100.],
  75. [102., 104., 106.]]],
  76. [[[108., 110., 112.],
  77. [114., 116., 118.],
  78. [120., 122., 124.]],
  79. [[126., 128., 130.],
  80. [132., 134., 136.],
  81. [138., 140., 142.]],
  82. [[144., 146., 148.],
  83. [150., 152., 154.],
  84. [156., 158., 160.]]]]).astype(nptype)
  85. expect3 = np.array(
  86. [[[[0., 2., 4.],
  87. [6., 8., 10.],
  88. [12., 14., 16.]],
  89. [[9., 11., 13.],
  90. [15., 17., 19.],
  91. [21., 23., 25.]],
  92. [[18., 20., 22.],
  93. [24., 26., 28.],
  94. [30., 32., 34.]]],
  95. [[[27., 29., 31.],
  96. [33., 35., 37.],
  97. [39., 41., 43.]],
  98. [[36., 38., 40.],
  99. [42., 44., 46.],
  100. [48., 50., 52.]],
  101. [[45., 47., 49.],
  102. [51., 53., 55.],
  103. [57., 59., 61.]]],
  104. [[[54., 56., 58.],
  105. [60., 62., 64.],
  106. [66., 68., 70.]],
  107. [[63., 65., 67.],
  108. [69., 71., 73.],
  109. [75., 77., 79.]],
  110. [[72., 74., 76.],
  111. [78., 80., 82.],
  112. [84., 86., 88.]]]]).astype(nptype)
  113. assert (output[0].asnumpy() == expect0).all()
  114. assert (output[1].asnumpy() == expect1).all()
  115. assert (output[2].asnumpy() == expect2).all()
  116. assert (output[3].asnumpy() == expect3).all()
  117. @pytest.mark.level0
  118. @pytest.mark.platform_x86_gpu_training
  119. @pytest.mark.env_onecard
  120. def test_add_float64():
  121. add(np.float64)
  122. @pytest.mark.level0
  123. @pytest.mark.platform_x86_gpu_training
  124. @pytest.mark.env_onecard
  125. def test_add_float32():
  126. add(np.float32)
  127. @pytest.mark.level0
  128. @pytest.mark.platform_x86_gpu_training
  129. @pytest.mark.env_onecard
  130. def test_add_float16():
  131. add(np.float16)
  132. @pytest.mark.level0
  133. @pytest.mark.platform_x86_gpu_training
  134. @pytest.mark.env_onecard
  135. def test_add_int64():
  136. add(np.int64)
  137. @pytest.mark.level0
  138. @pytest.mark.platform_x86_gpu_training
  139. @pytest.mark.env_onecard
  140. def test_add_int32():
  141. add(np.int32)
  142. class Tensoradd_d(nn.Cell):
  143. def __init__(self):
  144. super(Tensoradd_d, self).__init__()
  145. self.test_dynamic = inner.GpuConvertToDynamicShape()
  146. self.add = P.Add()
  147. def construct(self, x, y):
  148. x = self.test_dynamic(x)
  149. y = self.test_dynamic(y)
  150. return self.add(x, y)
  151. def add_dynamic(nptype):
  152. context.set_context(device_target='GPU', mode=context.GRAPH_MODE)
  153. net = Tensoradd_d()
  154. x1 = Tensor(np.arange(3).reshape(3).astype(nptype))
  155. y1 = Tensor(np.array([2]).astype(nptype))
  156. x2 = Tensor(np.arange(3 * 3 * 3 * 3).reshape(3, 3, 3, 3).astype(nptype))
  157. y2 = Tensor(np.arange(3 * 3 * 3 * 3).reshape(3, 3, 3, 3).astype(nptype))
  158. expect1 = np.array([2, 3, 4])
  159. expect2 = np.array(
  160. [[[[0., 2., 4.],
  161. [6., 8., 10.],
  162. [12., 14., 16.]],
  163. [[18., 20., 22.],
  164. [24., 26., 28.],
  165. [30., 32., 34.]],
  166. [[36., 38., 40.],
  167. [42., 44., 46.],
  168. [48., 50., 52.]]],
  169. [[[54., 56., 58.],
  170. [60., 62., 64.],
  171. [66., 68., 70.]],
  172. [[72., 74., 76.],
  173. [78., 80., 82.],
  174. [84., 86., 88.]],
  175. [[90., 92., 94.],
  176. [96., 98., 100.],
  177. [102., 104., 106.]]],
  178. [[[108., 110., 112.],
  179. [114., 116., 118.],
  180. [120., 122., 124.]],
  181. [[126., 128., 130.],
  182. [132., 134., 136.],
  183. [138., 140., 142.]],
  184. [[144., 146., 148.],
  185. [150., 152., 154.],
  186. [156., 158., 160.]]]])
  187. output1 = net(x1, y1)
  188. output2 = net(x2, y2)
  189. assert (output1.asnumpy() == expect1).all()
  190. assert (output2.asnumpy() == expect2).all()
  191. @pytest.mark.level0
  192. @pytest.mark.platform_x86_gpu_training
  193. @pytest.mark.env_onecard
  194. def test_add_dynamic_float64():
  195. add_dynamic(np.float64)
  196. @pytest.mark.level0
  197. @pytest.mark.platform_x86_gpu_training
  198. @pytest.mark.env_onecard
  199. def test_add_dynamic_float32():
  200. add_dynamic(np.float32)
  201. @pytest.mark.level0
  202. @pytest.mark.platform_x86_gpu_training
  203. @pytest.mark.env_onecard
  204. def test_add_dynamic_float16():
  205. add_dynamic(np.float16)
  206. @pytest.mark.level0
  207. @pytest.mark.platform_x86_gpu_training
  208. @pytest.mark.env_onecard
  209. def test_add_dynamic_int64():
  210. add_dynamic(np.int64)
  211. @pytest.mark.level0
  212. @pytest.mark.platform_x86_gpu_training
  213. @pytest.mark.env_onecard
  214. def test_add_dynamic_int32():
  215. add_dynamic(np.int32)