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.8 kB

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  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.skip(reason='0 in shape is not support')
  118. @pytest.mark.level0
  119. @pytest.mark.platform_x86_gpu_training
  120. @pytest.mark.env_onecard
  121. def test_add_float64():
  122. add(np.float64)
  123. @pytest.mark.skip(reason='0 in shape is not support')
  124. @pytest.mark.level0
  125. @pytest.mark.platform_x86_gpu_training
  126. @pytest.mark.env_onecard
  127. def test_add_float32():
  128. add(np.float32)
  129. @pytest.mark.skip(reason='0 in shape is not support')
  130. @pytest.mark.level0
  131. @pytest.mark.platform_x86_gpu_training
  132. @pytest.mark.env_onecard
  133. def test_add_float16():
  134. add(np.float16)
  135. @pytest.mark.skip(reason='0 in shape is not support')
  136. @pytest.mark.level0
  137. @pytest.mark.platform_x86_gpu_training
  138. @pytest.mark.env_onecard
  139. def test_add_int64():
  140. add(np.int64)
  141. @pytest.mark.skip(reason='0 in shape is not support')
  142. @pytest.mark.level0
  143. @pytest.mark.platform_x86_gpu_training
  144. @pytest.mark.env_onecard
  145. def test_add_int32():
  146. add(np.int32)
  147. class Tensoradd_d(nn.Cell):
  148. def __init__(self):
  149. super(Tensoradd_d, self).__init__()
  150. self.test_dynamic = inner.GpuConvertToDynamicShape()
  151. self.add = P.Add()
  152. def construct(self, x, y):
  153. x = self.test_dynamic(x)
  154. y = self.test_dynamic(y)
  155. return self.add(x, y)
  156. def add_dynamic(nptype):
  157. context.set_context(device_target='GPU', mode=context.GRAPH_MODE)
  158. net = Tensoradd_d()
  159. x1 = Tensor(np.arange(3).reshape(3).astype(nptype))
  160. y1 = Tensor(np.array([2]).astype(nptype))
  161. x2 = Tensor(np.arange(3 * 3 * 3 * 3).reshape(3, 3, 3, 3).astype(nptype))
  162. y2 = Tensor(np.arange(3 * 3 * 3 * 3).reshape(3, 3, 3, 3).astype(nptype))
  163. expect1 = np.array([2, 3, 4])
  164. expect2 = np.array(
  165. [[[[0., 2., 4.],
  166. [6., 8., 10.],
  167. [12., 14., 16.]],
  168. [[18., 20., 22.],
  169. [24., 26., 28.],
  170. [30., 32., 34.]],
  171. [[36., 38., 40.],
  172. [42., 44., 46.],
  173. [48., 50., 52.]]],
  174. [[[54., 56., 58.],
  175. [60., 62., 64.],
  176. [66., 68., 70.]],
  177. [[72., 74., 76.],
  178. [78., 80., 82.],
  179. [84., 86., 88.]],
  180. [[90., 92., 94.],
  181. [96., 98., 100.],
  182. [102., 104., 106.]]],
  183. [[[108., 110., 112.],
  184. [114., 116., 118.],
  185. [120., 122., 124.]],
  186. [[126., 128., 130.],
  187. [132., 134., 136.],
  188. [138., 140., 142.]],
  189. [[144., 146., 148.],
  190. [150., 152., 154.],
  191. [156., 158., 160.]]]])
  192. output1 = net(x1, y1)
  193. output2 = net(x2, y2)
  194. assert (output1.asnumpy() == expect1).all()
  195. assert (output2.asnumpy() == expect2).all()
  196. @pytest.mark.level0
  197. @pytest.mark.platform_x86_gpu_training
  198. @pytest.mark.env_onecard
  199. def test_add_dynamic_float64():
  200. add_dynamic(np.float64)
  201. @pytest.mark.level0
  202. @pytest.mark.platform_x86_gpu_training
  203. @pytest.mark.env_onecard
  204. def test_add_dynamic_float32():
  205. add_dynamic(np.float32)
  206. @pytest.mark.level0
  207. @pytest.mark.platform_x86_gpu_training
  208. @pytest.mark.env_onecard
  209. def test_add_dynamic_float16():
  210. add_dynamic(np.float16)
  211. @pytest.mark.level0
  212. @pytest.mark.platform_x86_gpu_training
  213. @pytest.mark.env_onecard
  214. def test_add_dynamic_int64():
  215. add_dynamic(np.int64)
  216. @pytest.mark.level0
  217. @pytest.mark.platform_x86_gpu_training
  218. @pytest.mark.env_onecard
  219. def test_add_dynamic_int32():
  220. add_dynamic(np.int32)