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_index_add_op.py 13 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  1. # Copyright 2019 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
  18. import mindspore.context as context
  19. import mindspore.nn as nn
  20. from mindspore import Tensor, Parameter, ParameterTuple
  21. from mindspore.ops import operations as P
  22. from mindspore.ops import composite as C
  23. class NetIndexAdd(nn.Cell):
  24. def __init__(self, x, axis):
  25. super(NetIndexAdd, self).__init__()
  26. self.input_x = Parameter(Tensor(x), name='x')
  27. self.index_add = P.IndexAdd(axis)
  28. def construct(self, idx, y):
  29. z = self.index_add(self.input_x, idx, y)
  30. return z
  31. @pytest.mark.level0
  32. @pytest.mark.platform_x86_gpu_training
  33. @pytest.mark.env_onecard
  34. def test_index_add():
  35. x = np.arange(2 * 3 * 4 * 4).reshape(2, 3, 4, 4).astype(np.float32)
  36. y0 = np.ones((1, 3, 4, 4), dtype=np.float32)
  37. idx0 = np.array([1]).astype(np.int32)
  38. axis0 = 0
  39. expect = np.copy(x)
  40. expect[idx0, :, :, :] = expect[idx0, :, :, :] + y0
  41. context.set_context(mode=context.PYNATIVE_MODE, device_target="GPU")
  42. net = NetIndexAdd(x, axis0)
  43. output = net(Tensor(idx0), Tensor(y0))
  44. assert (output.asnumpy() == expect).all()
  45. context.set_context(mode=context.GRAPH_MODE, device_target='GPU')
  46. net = NetIndexAdd(x, axis0)
  47. output = net(Tensor(idx0), Tensor(y0))
  48. assert (output.asnumpy() == expect).all()
  49. y1 = np.ndarray((2, 2, 4, 4)).astype(np.float32)
  50. y1.fill(0.1)
  51. idx1 = np.array([0, 2]).astype(np.int32)
  52. axis1 = 1
  53. expect = np.copy(x)
  54. expect[:, idx1, :, :] = expect[:, idx1, :, :] + y1
  55. context.set_context(mode=context.PYNATIVE_MODE, device_target='GPU')
  56. net = NetIndexAdd(x, axis1)
  57. output = net(Tensor(idx1), Tensor(y1))
  58. assert (output.asnumpy() == expect).all()
  59. context.set_context(mode=context.GRAPH_MODE, device_target='GPU')
  60. net = NetIndexAdd(x, axis1)
  61. output = net(Tensor(idx1), Tensor(y1))
  62. assert (output.asnumpy() == expect).all()
  63. y2 = np.ones((2, 3, 2, 4)).astype(np.float32)
  64. y2.fill(5.5)
  65. idx2 = np.array([1, 3]).astype(np.int32)
  66. axis2 = 2
  67. expect = np.copy(x)
  68. expect[:, :, idx2, :] = expect[:, :, idx2, :] + y2
  69. context.set_context(mode=context.PYNATIVE_MODE, device_target='GPU')
  70. net = NetIndexAdd(x, axis2)
  71. output = net(Tensor(idx2), Tensor(y2))
  72. assert (output.asnumpy() == expect).all()
  73. context.set_context(mode=context.GRAPH_MODE, device_target='GPU')
  74. net = NetIndexAdd(x, axis2)
  75. output = net(Tensor(idx2), Tensor(y2))
  76. assert (output.asnumpy() == expect).all()
  77. y3 = np.ones((2, 3, 4, 3)).astype(np.float32)
  78. y3.fill(1000.00)
  79. idx3 = np.array([0, 2, 3]).astype(np.int32)
  80. axis3 = 3
  81. expect = np.copy(x)
  82. expect[:, :, :, idx3] = expect[:, :, :, idx3] + y3
  83. context.set_context(mode=context.PYNATIVE_MODE, device_target='GPU')
  84. net = NetIndexAdd(x, axis3)
  85. output = net(Tensor(idx3), Tensor(y3))
  86. assert (output.asnumpy() == expect).all()
  87. context.set_context(mode=context.GRAPH_MODE, device_target='GPU')
  88. net = NetIndexAdd(x, axis3)
  89. output = net(Tensor(idx3), Tensor(y3))
  90. assert (output.asnumpy() == expect).all()
  91. @pytest.mark.level0
  92. @pytest.mark.platform_x86_gpu_training
  93. @pytest.mark.env_onecard
  94. def test_index_add_float16():
  95. x = np.arange(2 * 3 * 4).reshape(2, 3, 4).astype(np.float16)
  96. y = np.ones((2, 2, 4), dtype=np.float16)
  97. idx = np.array([0, 2]).astype(np.int32)
  98. axis = 1
  99. expect = np.copy(x)
  100. expect[:, idx, :] = expect[:, idx, :] + y
  101. context.set_context(mode=context.PYNATIVE_MODE, device_target="GPU")
  102. net = NetIndexAdd(x, axis)
  103. output = net(Tensor(idx), Tensor(y))
  104. assert (output.asnumpy() == expect).all()
  105. context.set_context(mode=context.GRAPH_MODE, device_target='GPU')
  106. net = NetIndexAdd(x, axis)
  107. output = net(Tensor(idx), Tensor(y))
  108. assert (output.asnumpy() == expect).all()
  109. @pytest.mark.level0
  110. @pytest.mark.platform_x86_gpu_training
  111. @pytest.mark.env_onecard
  112. def test_index_add_int32():
  113. x = np.arange(2 * 3 * 4).reshape(2, 3, 4).astype(np.int32)
  114. y = np.ones((2, 2, 4), dtype=np.int32)
  115. idx = np.array([0, 2]).astype(np.int32)
  116. axis = 1
  117. expect = np.copy(x)
  118. expect[:, idx, :] = expect[:, idx, :] + y
  119. context.set_context(mode=context.PYNATIVE_MODE, device_target="GPU")
  120. net = NetIndexAdd(x, axis)
  121. output = net(Tensor(idx), Tensor(y))
  122. assert (output.asnumpy() == expect).all()
  123. context.set_context(mode=context.GRAPH_MODE, device_target='GPU')
  124. net = NetIndexAdd(x, axis)
  125. output = net(Tensor(idx), Tensor(y))
  126. assert (output.asnumpy() == expect).all()
  127. @pytest.mark.level1
  128. @pytest.mark.platform_x86_gpu_training
  129. @pytest.mark.env_onecard
  130. def test_index_add_int8():
  131. x = np.arange(2 * 3 * 4).reshape(2, 3, 4).astype(np.int8)
  132. y = np.ones((2, 2, 4), dtype=np.int8)
  133. idx = np.array([0, 2]).astype(np.int32)
  134. axis = 1
  135. expect = np.copy(x)
  136. expect[:, idx, :] = expect[:, idx, :] + y
  137. context.set_context(mode=context.PYNATIVE_MODE, device_target="GPU")
  138. net = NetIndexAdd(x, axis)
  139. output = net(Tensor(idx), Tensor(y))
  140. assert (output.asnumpy() == expect).all()
  141. context.set_context(mode=context.GRAPH_MODE, device_target='GPU')
  142. net = NetIndexAdd(x, axis)
  143. output = net(Tensor(idx), Tensor(y))
  144. assert (output.asnumpy() == expect).all()
  145. @pytest.mark.level1
  146. @pytest.mark.platform_x86_gpu_training
  147. @pytest.mark.env_onecard
  148. def test_index_add_uint8():
  149. x = np.arange(2 * 3 * 4).reshape(2, 3, 4).astype(np.uint8)
  150. y = np.ones((2, 2, 4), dtype=np.uint8)
  151. idx = np.array([0, 2]).astype(np.int32)
  152. axis = 1
  153. expect = np.copy(x)
  154. expect[:, idx, :] = expect[:, idx, :] + y
  155. context.set_context(mode=context.PYNATIVE_MODE, device_target="GPU")
  156. net = NetIndexAdd(x, axis)
  157. output = net(Tensor(idx), Tensor(y))
  158. assert (output.asnumpy() == expect).all()
  159. context.set_context(mode=context.GRAPH_MODE, device_target='GPU')
  160. net = NetIndexAdd(x, axis)
  161. output = net(Tensor(idx), Tensor(y))
  162. assert (output.asnumpy() == expect).all()
  163. @pytest.mark.level0
  164. @pytest.mark.platform_x86_gpu_training
  165. @pytest.mark.env_onecard
  166. def test_index_add_float64():
  167. x = np.arange(2 * 3 * 4).reshape(2, 3, 4).astype(np.float64)
  168. y = np.ones((2, 2, 4), dtype=np.float64)
  169. idx = np.array([0, 2]).astype(np.int32)
  170. axis = 1
  171. expect = np.copy(x)
  172. expect[:, idx, :] = expect[:, idx, :] + y
  173. context.set_context(mode=context.PYNATIVE_MODE, device_target="GPU")
  174. net = NetIndexAdd(x, axis)
  175. output = net(Tensor(idx), Tensor(y))
  176. assert (output.asnumpy() == expect).all()
  177. context.set_context(mode=context.GRAPH_MODE, device_target='GPU')
  178. net = NetIndexAdd(x, axis)
  179. output = net(Tensor(idx), Tensor(y))
  180. assert (output.asnumpy() == expect).all()
  181. @pytest.mark.level0
  182. @pytest.mark.platform_x86_gpu_training
  183. @pytest.mark.env_onecard
  184. def test_index_add_int16():
  185. x = np.arange(2 * 3 * 4).reshape(2, 3, 4).astype(np.int16)
  186. y = np.ones((2, 2, 4), dtype=np.int16)
  187. idx = np.array([0, 2]).astype(np.int32)
  188. axis = 1
  189. expect = np.copy(x)
  190. expect[:, idx, :] = expect[:, idx, :] + y
  191. context.set_context(mode=context.PYNATIVE_MODE, device_target="GPU")
  192. net = NetIndexAdd(x, axis)
  193. output = net(Tensor(idx), Tensor(y))
  194. assert (output.asnumpy() == expect).all()
  195. context.set_context(mode=context.GRAPH_MODE, device_target='GPU')
  196. net = NetIndexAdd(x, axis)
  197. output = net(Tensor(idx), Tensor(y))
  198. assert (output.asnumpy() == expect).all()
  199. @pytest.mark.level0
  200. @pytest.mark.platform_x86_gpu_training
  201. @pytest.mark.env_onecard
  202. def test_index_add_invalid_inputs():
  203. context.set_context(mode=context.GRAPH_MODE, device_target='GPU')
  204. x = np.arange(2 * 3 * 4).reshape(2, 3, 4).astype(np.uint8)
  205. y = np.ones((2, 2, 4), dtype=np.uint8)
  206. with pytest.raises(TypeError):
  207. #axis not int
  208. net = NetIndexAdd(x, 1.0)
  209. #x and y don't have the same type
  210. y = np.ones((2, 2, 4), dtype=np.float32)
  211. idx = np.array([0, 1]).astype(np.int32)
  212. net = NetIndexAdd(x, 1)
  213. _ = net(Tensor(idx), Tensor(y))
  214. with pytest.raises(ValueError):
  215. #index size not the same as len(y[axis])
  216. idx = np.array([0]).astype(np.int32)
  217. net = NetIndexAdd(x, 1)
  218. _ = net(Tensor(idx), Tensor(y))
  219. #x and y don't have same rank
  220. y = np.ones((2, 2), dtype=np.uint8)
  221. idx = np.array([0, 1]).astype(np.int32)
  222. net = NetIndexAdd(x, 1)
  223. _ = net(Tensor(idx), Tensor(y))
  224. #x and y don't have same shape on dimensions other than axis-th dimension
  225. y = np.ones((2, 2, 5), dtype=np.uint8)
  226. idx = np.array([0, 1]).astype(np.int32)
  227. net = NetIndexAdd(x, 1)
  228. _ = net(Tensor(idx), Tensor(y))
  229. class IndexAddGradNet(nn.Cell):
  230. def __init__(self, network):
  231. super(IndexAddGradNet, self).__init__()
  232. self.grad = C.GradOperation(get_all=True, sens_param=True, get_by_list=True)
  233. self.network = network
  234. self.params = ParameterTuple(network.trainable_params())
  235. def construct(self, idx, y, dout):
  236. out = self.grad(self.network, self.params)(idx, y, dout)
  237. return out
  238. def index_add_grad_with_type(nptype):
  239. x = np.arange(15).reshape(5, 3).astype(nptype)
  240. net = NetIndexAdd(x, 1)
  241. grad_net = IndexAddGradNet(net)
  242. y = Tensor(np.arange(5).reshape(5, 1).astype(nptype))
  243. dout = Tensor(np.array([[63., 64., 65.],
  244. [66., 67., 68.],
  245. [69., 70., 71.],
  246. [72., 73., 74.],
  247. [75., 76., 77.]]).astype(nptype))
  248. index = Tensor(np.array([1]), dtype=mindspore.int32)
  249. output = grad_net(index, y, dout)
  250. ygrad = output[0][1]
  251. xgrad = output[1][0]
  252. expect_xgrad = np.array([[63., 64., 65.],
  253. [66., 67., 68.],
  254. [69., 70., 71.],
  255. [72., 73., 74.],
  256. [75., 76., 77.]]).astype(nptype)
  257. expect_ygrad = np.array([[64.],
  258. [67.],
  259. [70.],
  260. [73.],
  261. [76.]]).astype(nptype)
  262. np.testing.assert_array_equal(xgrad.asnumpy(), expect_xgrad)
  263. np.testing.assert_array_equal(ygrad.asnumpy(), expect_ygrad)
  264. @pytest.mark.level0
  265. @pytest.mark.platform_x86_gpu_training
  266. @pytest.mark.env_onecard
  267. def test_index_add_grad_float64():
  268. context.set_context(mode=context.GRAPH_MODE, device_target='GPU')
  269. index_add_grad_with_type(np.float64)
  270. context.set_context(mode=context.PYNATIVE_MODE, device_target='GPU')
  271. index_add_grad_with_type(np.float64)
  272. @pytest.mark.level0
  273. @pytest.mark.platform_x86_gpu_training
  274. @pytest.mark.env_onecard
  275. def test_index_add_grad_float32():
  276. context.set_context(mode=context.GRAPH_MODE, device_target='GPU')
  277. index_add_grad_with_type(np.float32)
  278. context.set_context(mode=context.PYNATIVE_MODE, device_target='GPU')
  279. index_add_grad_with_type(np.float32)
  280. @pytest.mark.level1
  281. @pytest.mark.platform_x86_gpu_training
  282. @pytest.mark.env_onecard
  283. def test_index_add_grad_float16():
  284. context.set_context(mode=context.GRAPH_MODE, device_target='GPU')
  285. index_add_grad_with_type(np.float16)
  286. context.set_context(mode=context.PYNATIVE_MODE, device_target='GPU')
  287. index_add_grad_with_type(np.float16)
  288. @pytest.mark.level1
  289. @pytest.mark.platform_x86_gpu_training
  290. @pytest.mark.env_onecard
  291. def test_index_add_grad_int32():
  292. context.set_context(mode=context.GRAPH_MODE, device_target='GPU')
  293. index_add_grad_with_type(np.int32)
  294. context.set_context(mode=context.PYNATIVE_MODE, device_target='GPU')
  295. index_add_grad_with_type(np.int32)
  296. @pytest.mark.level0
  297. @pytest.mark.platform_x86_gpu_training
  298. @pytest.mark.env_onecard
  299. def test_index_add_grad_int16():
  300. context.set_context(mode=context.GRAPH_MODE, device_target='GPU')
  301. index_add_grad_with_type(np.int16)
  302. context.set_context(mode=context.PYNATIVE_MODE, device_target='GPU')
  303. index_add_grad_with_type(np.int16)
  304. @pytest.mark.level1
  305. @pytest.mark.platform_x86_gpu_training
  306. @pytest.mark.env_onecard
  307. def test_index_add_grad_int8():
  308. context.set_context(mode=context.GRAPH_MODE, device_target='GPU')
  309. index_add_grad_with_type(np.int8)
  310. context.set_context(mode=context.PYNATIVE_MODE, device_target='GPU')
  311. index_add_grad_with_type(np.int8)
  312. @pytest.mark.level1
  313. @pytest.mark.platform_x86_gpu_training
  314. @pytest.mark.env_onecard
  315. def test_index_add_grad_uint8():
  316. context.set_context(mode=context.GRAPH_MODE, device_target='GPU')
  317. index_add_grad_with_type(np.uint8)
  318. context.set_context(mode=context.PYNATIVE_MODE, device_target='GPU')
  319. index_add_grad_with_type(np.uint8)