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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  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 as ms
  18. from mindspore import context, Tensor, Parameter
  19. from mindspore.nn import Cell, Momentum
  20. from mindspore.ops import operations as P
  21. from mindspore.train import Model
  22. from tests.dataset_mock import MindData
  23. class Dataset(MindData):
  24. def __init__(self, predict, label, length=3):
  25. super(Dataset, self).__init__(size=length)
  26. self.predict = predict
  27. self.label = label
  28. self.index = 0
  29. self.length = length
  30. def __iter__(self):
  31. return self
  32. def __next__(self):
  33. if self.index >= self.length:
  34. raise StopIteration
  35. self.index += 1
  36. return self.predict, self.label
  37. def reset(self):
  38. self.index = 0
  39. class Net(Cell):
  40. def __init__(self, w1_shape, indices_shape, strategy1=None, strategy2=None, strategy3=None):
  41. super().__init__()
  42. self.mul = P.Mul().shard(strategy1)
  43. self.w1 = Parameter(Tensor(np.ones(w1_shape), dtype=ms.float32), "w1")
  44. self.indices = Tensor(np.ones(indices_shape), dtype=ms.int32)
  45. self.gathernd = P.GatherNd().shard(strategy2)
  46. self.relu = P.ReLU().shard(strategy3)
  47. def construct(self, x, b):
  48. out = self.mul(x, self.w1)
  49. out = self.gathernd(out, self.indices)
  50. out = self.relu(out)
  51. return out
  52. class Net2(Cell):
  53. def __init__(self, w1_shape, indices_shape, strategy1=None, strategy2=None, strategy3=None):
  54. super().__init__()
  55. self.mul = P.Mul().shard(strategy1)
  56. self.w1 = Parameter(Tensor(np.ones(w1_shape), dtype=ms.float32), "w1")
  57. self.indices = Tensor(np.ones(indices_shape), dtype=ms.int32)
  58. self.gathernd = P.GatherNd().shard(strategy2)
  59. self.relu = P.ReLU().shard(strategy3)
  60. def construct(self, x, b):
  61. out = self.mul(x, self.w1)
  62. out = self.gathernd(out, self.indices)
  63. return out
  64. class Net3(Cell):
  65. def __init__(self, w1_shape, indices_shape, strategy1=None, strategy2=None, strategy3=None):
  66. super().__init__()
  67. self.mul = P.Mul().shard(strategy1)
  68. self.w1 = Parameter(Tensor(np.ones(w1_shape), dtype=ms.float32), "w1")
  69. self.indices = Tensor(np.ones(indices_shape), dtype=ms.int32)
  70. self.gathernd = P.GatherNd().shard(strategy2)
  71. self.relu = P.ReLU().shard(strategy3)
  72. def construct(self, x, b):
  73. out = self.gathernd(x, self.indices)
  74. out = self.relu(out)
  75. out = self.mul(out, self.w1)
  76. return out
  77. # full_batch = false
  78. _x = Tensor(np.ones([1, 16, 32]), dtype=ms.float32)
  79. _b = Tensor(np.ones([1, 16, 32]), dtype=ms.float32)
  80. def compile_net(net):
  81. context.set_context(save_graphs=True)
  82. learning_rate = 0.1
  83. momentum = 0.9
  84. epoch_size = 2
  85. dataset = Dataset(_x, _b)
  86. opt = Momentum(net.trainable_params(), learning_rate, momentum)
  87. model = Model(net, optimizer=opt)
  88. model.train(epoch_size, dataset, dataset_sink_mode=False)
  89. context.reset_auto_parallel_context()
  90. def test_gathernd_data_parallel():
  91. context.set_auto_parallel_context(
  92. parallel_mode="semi_auto_parallel", device_num=8, global_rank=0)
  93. w1_shape = [8, 16, 32]
  94. indices_shape = [8, 4, 2, 1]
  95. strategy1 = ((8, 1, 1), (8, 1, 1))
  96. strategy2 = ((1, 1, 1), (8, 1, 1, 1))
  97. strategy3 = ((8, 1, 1, 1, 1),)
  98. net = Net(w1_shape, indices_shape, strategy1, strategy2, strategy3)
  99. compile_net(net)
  100. def test_gathernd_data_parallel2():
  101. context.set_auto_parallel_context(
  102. parallel_mode="semi_auto_parallel", device_num=8, global_rank=0)
  103. w1_shape = [8, 16, 32]
  104. indices_shape = [8, 4, 2, 2]
  105. strategy1 = ((8, 1, 1), (8, 1, 1))
  106. strategy2 = ((1, 1, 1), (8, 1, 1, 1))
  107. strategy3 = ((8, 1, 1, 1),)
  108. net = Net(w1_shape, indices_shape, strategy1, strategy2, strategy3)
  109. compile_net(net)
  110. def test_gathernd_data_parallel3():
  111. context.set_auto_parallel_context(
  112. parallel_mode="semi_auto_parallel", device_num=8, global_rank=0)
  113. w1_shape = [8, 16, 32]
  114. indices_shape = [8, 4, 2, 3]
  115. strategy1 = ((8, 1, 1), (8, 1, 1))
  116. strategy2 = ((1, 1, 1), (8, 1, 1, 1))
  117. strategy3 = ((8, 1, 1),)
  118. net = Net(w1_shape, indices_shape, strategy1, strategy2, strategy3)
  119. compile_net(net)
  120. def test_gathernd_data_parallel4():
  121. context.set_auto_parallel_context(
  122. parallel_mode="semi_auto_parallel", device_num=8, global_rank=0)
  123. w1_shape = [8, 16, 32]
  124. indices_shape = [8, 4, 2, 1]
  125. strategy1 = ((8, 1, 1), (8, 1, 1))
  126. strategy2 = ((1, 1, 1), (8, 1, 1, 1))
  127. strategy3 = ((8, 1, 1, 1, 1),)
  128. net = Net2(w1_shape, indices_shape, strategy1, strategy2, strategy3)
  129. compile_net(net)
  130. def test_gathernd_data_parallel5():
  131. context.set_auto_parallel_context(
  132. parallel_mode="semi_auto_parallel", device_num=8, global_rank=0)
  133. w1_shape = [8, 16, 32]
  134. indices_shape = [8, 4, 2, 2]
  135. strategy1 = ((8, 1, 1), (8, 1, 1))
  136. strategy2 = ((1, 1, 1), (8, 1, 1, 1))
  137. strategy3 = ((8, 1, 1, 1),)
  138. net = Net2(w1_shape, indices_shape, strategy1, strategy2, strategy3)
  139. compile_net(net)
  140. def test_gathernd_data_parallel6():
  141. context.set_auto_parallel_context(
  142. parallel_mode="semi_auto_parallel", device_num=8, global_rank=0)
  143. w1_shape = [8, 16, 32]
  144. indices_shape = [8, 4, 2, 3]
  145. strategy1 = ((8, 1, 1), (8, 1, 1))
  146. strategy2 = ((1, 1, 1), (8, 1, 1, 1))
  147. strategy3 = ((8, 1, 1),)
  148. net = Net2(w1_shape, indices_shape, strategy1, strategy2, strategy3)
  149. compile_net(net)
  150. def test_gathernd_data_parallel7():
  151. context.set_auto_parallel_context(
  152. parallel_mode="semi_auto_parallel", device_num=8, global_rank=0)
  153. w1_shape = [8, 4, 2, 16, 32]
  154. indices_shape = [8, 4, 2, 1]
  155. strategy1 = ((8, 1, 1, 1, 1), (8, 1, 1, 1, 1))
  156. strategy2 = ((1, 1, 1), (8, 1, 1, 1))
  157. strategy3 = ((8, 1, 1, 1, 1),)
  158. net = Net3(w1_shape, indices_shape, strategy1, strategy2, strategy3)
  159. compile_net(net)
  160. def test_gathernd_data_parallel8():
  161. context.set_auto_parallel_context(
  162. parallel_mode="semi_auto_parallel", device_num=8, global_rank=0)
  163. w1_shape = [8, 4, 2, 32]
  164. indices_shape = [8, 4, 2, 2]
  165. strategy1 = ((8, 1, 1, 1), (8, 1, 1, 1))
  166. strategy2 = ((1, 1, 1), (8, 1, 1, 1))
  167. strategy3 = ((8, 1, 1, 1),)
  168. net = Net3(w1_shape, indices_shape, strategy1, strategy2, strategy3)
  169. compile_net(net)
  170. def test_gathernd_data_parallel9():
  171. context.set_auto_parallel_context(
  172. parallel_mode="semi_auto_parallel", device_num=8, global_rank=0)
  173. w1_shape = [8, 4, 2]
  174. indices_shape = [8, 4, 2, 3]
  175. strategy1 = ((8, 1, 1), (8, 1, 1))
  176. strategy2 = ((1, 1, 1), (8, 1, 1, 1))
  177. strategy3 = ((8, 1, 1),)
  178. net = Net3(w1_shape, indices_shape, strategy1, strategy2, strategy3)
  179. compile_net(net)
  180. def test_gathernd_model_parallel():
  181. context.set_auto_parallel_context(
  182. parallel_mode="semi_auto_parallel", device_num=8, global_rank=0)
  183. w1_shape = [8, 16, 32]
  184. indices_shape = [8, 4, 2, 1]
  185. strategy1 = ((8, 1, 1), (8, 1, 1))
  186. strategy2 = ((1, 1, 1), (2, 2, 2, 1))
  187. strategy3 = ((8, 1, 1, 1, 1),)
  188. net = Net(w1_shape, indices_shape, strategy1, strategy2, strategy3)
  189. compile_net(net)
  190. def test_gathernd_model_parallel2():
  191. context.set_auto_parallel_context(
  192. parallel_mode="semi_auto_parallel", device_num=8, global_rank=0)
  193. w1_shape = [8, 16, 32]
  194. indices_shape = [8, 4, 2, 2]
  195. strategy1 = ((8, 1, 1), (8, 1, 1))
  196. strategy2 = ((1, 1, 1), (2, 2, 2, 1))
  197. strategy3 = ((8, 1, 1, 1),)
  198. net = Net(w1_shape, indices_shape, strategy1, strategy2, strategy3)
  199. compile_net(net)
  200. def test_gathernd_model_parallel3():
  201. context.set_auto_parallel_context(
  202. parallel_mode="semi_auto_parallel", device_num=8, global_rank=0)
  203. w1_shape = [8, 16, 32]
  204. indices_shape = [8, 4, 2, 3]
  205. strategy1 = ((8, 1, 1), (8, 1, 1))
  206. strategy2 = ((1, 1, 1), (2, 2, 2, 1))
  207. strategy3 = ((8, 1, 1),)
  208. net = Net(w1_shape, indices_shape, strategy1, strategy2, strategy3)
  209. compile_net(net)
  210. def test_gathernd_model_parallel4():
  211. context.set_auto_parallel_context(
  212. parallel_mode="semi_auto_parallel", device_num=8, global_rank=0)
  213. w1_shape = [8, 16, 32]
  214. indices_shape = [8, 4, 2, 1]
  215. strategy1 = ((8, 1, 1), (8, 1, 1))
  216. strategy2 = ((1, 1, 1), (2, 2, 2, 1))
  217. strategy3 = ((8, 1, 1, 1, 1),)
  218. net = Net2(w1_shape, indices_shape, strategy1, strategy2, strategy3)
  219. compile_net(net)
  220. def test_gathernd_model_parallel5():
  221. context.set_auto_parallel_context(
  222. parallel_mode="semi_auto_parallel", device_num=8, global_rank=0)
  223. w1_shape = [8, 16, 32]
  224. indices_shape = [8, 4, 2, 2]
  225. strategy1 = ((8, 1, 1), (8, 1, 1))
  226. strategy2 = ((1, 1, 1), (2, 2, 2, 1))
  227. strategy3 = ((8, 1, 1, 1),)
  228. net = Net2(w1_shape, indices_shape, strategy1, strategy2, strategy3)
  229. compile_net(net)
  230. def test_gathernd_model_parallel6():
  231. context.set_auto_parallel_context(
  232. parallel_mode="semi_auto_parallel", device_num=8, global_rank=0)
  233. w1_shape = [8, 16, 32]
  234. indices_shape = [8, 4, 2, 3]
  235. strategy1 = ((8, 1, 1), (8, 1, 1))
  236. strategy2 = ((1, 1, 1), (2, 2, 2, 1))
  237. strategy3 = ((8, 1, 1),)
  238. net = Net2(w1_shape, indices_shape, strategy1, strategy2, strategy3)
  239. compile_net(net)
  240. def test_gathernd_model_parallel7():
  241. context.set_auto_parallel_context(
  242. parallel_mode="semi_auto_parallel", device_num=8, global_rank=0)
  243. w1_shape = [8, 4, 2, 16, 32]
  244. indices_shape = [8, 4, 2, 1]
  245. strategy1 = ((8, 1, 1, 1, 1), (8, 1, 1, 1, 1))
  246. strategy2 = ((1, 1, 1), (2, 2, 2, 1))
  247. strategy3 = ((8, 1, 1, 1, 1),)
  248. net = Net3(w1_shape, indices_shape, strategy1, strategy2, strategy3)
  249. compile_net(net)
  250. def test_gathernd_model_parallel8():
  251. context.set_auto_parallel_context(
  252. parallel_mode="semi_auto_parallel", device_num=8, global_rank=0)
  253. w1_shape = [8, 4, 2, 32]
  254. indices_shape = [8, 4, 2, 2]
  255. strategy1 = ((8, 1, 1, 1), (8, 1, 1, 1))
  256. strategy2 = ((1, 1, 1), (2, 2, 2, 1))
  257. strategy3 = ((8, 1, 1, 1),)
  258. net = Net3(w1_shape, indices_shape, strategy1, strategy2, strategy3)
  259. compile_net(net)
  260. def test_gathernd_model_parallel9():
  261. context.set_auto_parallel_context(
  262. parallel_mode="semi_auto_parallel", device_num=8, global_rank=0)
  263. w1_shape = [8, 4, 2]
  264. indices_shape = [8, 4, 2, 3]
  265. strategy1 = ((8, 1, 1), (8, 1, 1))
  266. strategy2 = ((1, 1, 1), (2, 2, 2, 1))
  267. strategy3 = ((8, 1, 1),)
  268. net = Net3(w1_shape, indices_shape, strategy1, strategy2, strategy3)
  269. compile_net(net)
  270. def test_gathernd_auto_parallel():
  271. context.set_auto_parallel_context(
  272. parallel_mode="auto_parallel", device_num=8, global_rank=0)
  273. w1_shape = [8, 16, 32]
  274. indices_shape = [8, 4, 2, 1]
  275. net = Net(w1_shape, indices_shape)
  276. compile_net(net)
  277. def test_gathernd_auto_parallel2():
  278. context.set_auto_parallel_context(
  279. parallel_mode="auto_parallel", device_num=8, global_rank=0)
  280. w1_shape = [8, 16, 32]
  281. indices_shape = [8, 4, 2, 2]
  282. net = Net(w1_shape, indices_shape)
  283. compile_net(net)
  284. def test_gathernd_auto_parallel3():
  285. context.set_auto_parallel_context(
  286. parallel_mode="auto_parallel", device_num=8, global_rank=0)
  287. w1_shape = [8, 16, 32]
  288. indices_shape = [8, 4, 2, 3]
  289. net = Net(w1_shape, indices_shape)
  290. compile_net(net)
  291. def test_gathernd_strategy_error():
  292. context.set_auto_parallel_context(
  293. parallel_mode="semi_auto_parallel", device_num=8, global_rank=0)
  294. w1_shape = [8, 16, 32]
  295. indices_shape = [8, 4, 2, 3]
  296. strategy1 = ((8, 1, 1), (8, 1, 1))
  297. strategy2 = ((2, 1, 1), (1, 2, 2, 1))
  298. strategy3 = ((8, 1, 1),)
  299. net = Net(w1_shape, indices_shape, strategy1, strategy2, strategy3)
  300. with pytest.raises(RuntimeError):
  301. compile_net(net)
  302. def test_gathernd_strategy_error2():
  303. context.set_auto_parallel_context(
  304. parallel_mode="semi_auto_parallel", device_num=8, global_rank=0)
  305. w1_shape = [8, 16, 32]
  306. indices_shape = [8, 4, 2, 3]
  307. strategy1 = ((8, 1, 1), (8, 1, 1))
  308. strategy2 = ((1, 1, 1), (1, 2, 2, 2))
  309. strategy3 = ((8, 1, 1),)
  310. net = Net(w1_shape, indices_shape, strategy1, strategy2, strategy3)
  311. with pytest.raises(RuntimeError):
  312. compile_net(net)