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_gather_v2.py 7.8 kB

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  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 mindspore as ms
  17. import mindspore.nn as nn
  18. from mindspore import Tensor
  19. from mindspore import context
  20. from mindspore.common.api import _executor
  21. from mindspore.ops import composite as C
  22. from mindspore.ops import operations as P
  23. from tests.ut.python.ops.test_math_ops import VirtualLoss
  24. grad_all = C.GradOperation(get_all=True)
  25. class NetWithLoss(nn.Cell):
  26. def __init__(self, network):
  27. super(NetWithLoss, self).__init__()
  28. self.loss = VirtualLoss()
  29. self.network = network
  30. def construct(self, x, y):
  31. predict = self.network(x, y)
  32. return self.loss(predict)
  33. class GradWrap(nn.Cell):
  34. def __init__(self, network):
  35. super(GradWrap, self).__init__()
  36. self.network = network
  37. def construct(self, x, y):
  38. return grad_all(self.network)(x, y)
  39. class Net(nn.Cell):
  40. def __init__(self, axis=0, strategy1=None, strategy2=None, shape=None, target=""):
  41. super().__init__()
  42. if shape is None:
  43. shape = [64, 64]
  44. self.gatherv2 = P.GatherV2().shard(strategy1).add_prim_attr("primitive_target", target)
  45. self.mul = P.Mul().shard(strategy2)
  46. self.index = Tensor(np.ones(shape), dtype=ms.int32)
  47. self.axis = axis
  48. def construct(self, x, y):
  49. out = self.gatherv2(x, self.index, self.axis)
  50. out = self.mul(out, y)
  51. return out
  52. def test_gatherv2_semi_auto0():
  53. context.set_auto_parallel_context(device_num=8, global_rank=0, parallel_mode="semi_auto_parallel")
  54. strategy1 = ((1, 8), (1, 1))
  55. strategy2 = ((4, 2, 1), (4, 2, 1))
  56. net = GradWrap(NetWithLoss(Net(0, strategy1, strategy2)))
  57. net.set_auto_parallel()
  58. x = Tensor(np.ones([64, 64]), dtype=ms.float32)
  59. y = Tensor(np.ones([64, 64, 64]), dtype=ms.float32)
  60. net.set_train()
  61. _executor.compile(net, x, y)
  62. def test_gatherv2_semi_auto1():
  63. context.set_auto_parallel_context(device_num=8, global_rank=0, parallel_mode="semi_auto_parallel")
  64. strategy1 = ((8, 1), (1, 1))
  65. strategy2 = ((4, 2, 1), (4, 2, 1))
  66. net = GradWrap(NetWithLoss(Net(0, strategy1, strategy2)))
  67. net.set_auto_parallel()
  68. x = Tensor(np.ones([64, 64]), dtype=ms.float32)
  69. y = Tensor(np.ones([64, 64, 64]), dtype=ms.float32)
  70. net.set_train()
  71. _executor.compile(net, x, y)
  72. def test_gatherv2_semi_auto2():
  73. context.set_auto_parallel_context(device_num=8, global_rank=0, parallel_mode="semi_auto_parallel")
  74. strategy1 = ((2, 4), (1, 1))
  75. strategy2 = ((4, 2, 1), (4, 2, 1))
  76. net = GradWrap(NetWithLoss(Net(0, strategy1, strategy2)))
  77. net.set_auto_parallel()
  78. x = Tensor(np.ones([64, 64]), dtype=ms.float32)
  79. y = Tensor(np.ones([64, 64, 64]), dtype=ms.float32)
  80. net.set_train()
  81. _executor.compile(net, x, y)
  82. def test_gatherv2_semi_auto3():
  83. context.set_auto_parallel_context(device_num=8, global_rank=0, parallel_mode="semi_auto_parallel")
  84. strategy1 = ((1, 8), (1, 1))
  85. strategy2 = ((4, 2, 1), (4, 2, 1))
  86. net = GradWrap(NetWithLoss(Net(1, strategy1, strategy2)))
  87. net.set_auto_parallel()
  88. x = Tensor(np.ones([64, 64]), dtype=ms.float32)
  89. y = Tensor(np.ones([64, 64, 64]), dtype=ms.float32)
  90. net.set_train()
  91. _executor.compile(net, x, y)
  92. def test_gatherv2_semi_auto4():
  93. context.set_auto_parallel_context(device_num=8, global_rank=0, parallel_mode="semi_auto_parallel")
  94. strategy1 = ((8, 1), (1, 1))
  95. strategy2 = ((4, 2, 1), (4, 2, 1))
  96. net = GradWrap(NetWithLoss(Net(1, strategy1, strategy2)))
  97. net.set_auto_parallel()
  98. x = Tensor(np.ones([64, 32]), dtype=ms.float32)
  99. y = Tensor(np.ones([64, 64, 64]), dtype=ms.float32)
  100. net.set_train()
  101. _executor.compile(net, x, y)
  102. def test_gatherv2_semi_auto5():
  103. context.set_auto_parallel_context(device_num=8, global_rank=0, parallel_mode="semi_auto_parallel")
  104. strategy1 = ((2, 4), (1, 1))
  105. strategy2 = ((4, 2, 1), (4, 2, 1))
  106. net = GradWrap(NetWithLoss(Net(1, strategy1, strategy2)))
  107. net.set_auto_parallel()
  108. x = Tensor(np.ones([64, 32]), dtype=ms.float32)
  109. y = Tensor(np.ones([64, 64, 64]), dtype=ms.float32)
  110. net.set_train()
  111. _executor.compile(net, x, y)
  112. def test_gatherv2_semi_auto6():
  113. context.set_auto_parallel_context(device_num=8, global_rank=0, parallel_mode="semi_auto_parallel")
  114. strategy2 = ((4, 2, 1), (4, 2, 1))
  115. net = GradWrap(NetWithLoss(Net(0, None, strategy2)))
  116. net.set_auto_parallel()
  117. x = Tensor(np.ones([64, 32]), dtype=ms.float32)
  118. y = Tensor(np.ones([64, 64, 32]), dtype=ms.float32)
  119. net.set_train()
  120. _executor.compile(net, x, y)
  121. def test_gatherv2_semi_auto7():
  122. context.set_auto_parallel_context(device_num=8, global_rank=0, parallel_mode="semi_auto_parallel")
  123. strategy2 = ((4, 2, 1), (4, 2, 1))
  124. net = GradWrap(NetWithLoss(Net(1, None, strategy2)))
  125. net.set_auto_parallel()
  126. x = Tensor(np.ones([64, 32]), dtype=ms.float32)
  127. y = Tensor(np.ones([64, 64, 64]), dtype=ms.float32)
  128. net.set_train()
  129. _executor.compile(net, x, y)
  130. def test_gatherv2_semi_auto8():
  131. context.set_auto_parallel_context(device_num=8, global_rank=0, parallel_mode="semi_auto_parallel")
  132. strategy1 = ((8,), (1, 1))
  133. strategy2 = ((4, 2), (4, 2))
  134. net = GradWrap(NetWithLoss(Net(0, strategy1, strategy2)))
  135. net.set_auto_parallel()
  136. x = Tensor(np.ones([64]), dtype=ms.float32)
  137. y = Tensor(np.ones([64, 64]), dtype=ms.float32)
  138. net.set_train()
  139. _executor.compile(net, x, y)
  140. def test_gatherv2_forward_all_reduce():
  141. context.set_auto_parallel_context(device_num=8, global_rank=0, parallel_mode="semi_auto_parallel")
  142. strategy1 = ((8, 1), (1, 1))
  143. strategy2 = ((2, 4, 1), (2, 4, 1))
  144. net = GradWrap(NetWithLoss(Net(0, strategy1, strategy2, shape=[2, 64])))
  145. net.set_auto_parallel()
  146. x = Tensor(np.ones([64, 64]), dtype=ms.float32)
  147. y = Tensor(np.ones([2, 64, 64]), dtype=ms.float32)
  148. net.set_train()
  149. _executor.compile(net, x, y)
  150. def test_gatherv2_split_axis_0_repeat_calc():
  151. context.set_auto_parallel_context(device_num=8, global_rank=7, parallel_mode="semi_auto_parallel")
  152. strategy1 = ((4, 1), (1, 1))
  153. strategy2 = ((2, 4, 1), (2, 4, 1))
  154. net = GradWrap(NetWithLoss(Net(0, strategy1, strategy2, shape=[2, 64])))
  155. net.set_auto_parallel()
  156. x = Tensor(np.ones([64, 64]), dtype=ms.float32)
  157. y = Tensor(np.ones([2, 64, 64]), dtype=ms.float32)
  158. net.set_train()
  159. _executor.compile(net, x, y)
  160. def test_gatherv2_auto0():
  161. context.set_auto_parallel_context(device_num=8, global_rank=0, parallel_mode="auto_parallel")
  162. net = GradWrap(NetWithLoss(Net(0)))
  163. net.set_auto_parallel()
  164. x = Tensor(np.ones([64, 32]), dtype=ms.float32)
  165. y = Tensor(np.ones([64, 64, 32]), dtype=ms.float32)
  166. net.set_train()
  167. _executor.compile(net, x, y)
  168. def test_gatherv2_auto1():
  169. context.set_auto_parallel_context(device_num=8, global_rank=0, parallel_mode="auto_parallel")
  170. net = GradWrap(NetWithLoss(Net(1)))
  171. net.set_auto_parallel()
  172. x = Tensor(np.ones([64, 32]), dtype=ms.float32)
  173. y = Tensor(np.ones([64, 64, 64]), dtype=ms.float32)
  174. net.set_train()
  175. _executor.compile(net, x, y)