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_sparse_gather_v2.py 7.1 kB

5 years ago
5 years ago
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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 _cell_graph_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.SparseGatherV2().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 compile_net(net, index_shape, emb_shape, device_num=8, parallel_mode="semi_auto_parallel"):
  53. context.set_auto_parallel_context(device_num=device_num, global_rank=0, parallel_mode=parallel_mode)
  54. net.set_auto_parallel()
  55. x = Tensor(np.ones(index_shape), dtype=ms.float32)
  56. y = Tensor(np.ones(emb_shape), dtype=ms.float32)
  57. net.set_train()
  58. _cell_graph_executor.compile(net, x, y)
  59. def test_gatherv2_semi_auto0():
  60. """
  61. Feature: distribute operator SparseGatherV2 in auto parallel.
  62. Description: gather net with strategy in semi auto parallel, gather axis is 1.
  63. Expectation: compile done without error.
  64. """
  65. strategy1 = ((8, 1), (1, 1))
  66. strategy2 = ((4, 2, 1), (4, 2, 1))
  67. net = GradWrap(NetWithLoss(Net(0, strategy1, strategy2)))
  68. compile_net(net, [64, 64], [64, 64, 64])
  69. def test_gatherv2_semi_auto1():
  70. """
  71. Feature: distribute operator SparseGatherV2 in auto parallel.
  72. Description: gather net with strategy in semi auto parallel, gather axis is 1.
  73. Expectation: compile done without error.
  74. """
  75. strategy1 = ((1, 8), (1, 1))
  76. strategy2 = ((4, 2, 1), (4, 2, 1))
  77. net = GradWrap(NetWithLoss(Net(1, strategy1, strategy2)))
  78. compile_net(net, [64, 64], [64, 64, 64])
  79. def test_gatherv2_semi_auto2():
  80. """
  81. Feature: distribute operator SparseGatherV2 in auto parallel.
  82. Description: gather net with strategy in semi auto parallel, gather axis is 1.
  83. Expectation: compile done without error.
  84. """
  85. strategy1 = ((8, 1), (1, 1))
  86. strategy2 = ((4, 2, 1), (4, 2, 1))
  87. net = GradWrap(NetWithLoss(Net(1, strategy1, strategy2)))
  88. compile_net(net, [64, 32], [64, 64, 64])
  89. def test_gatherv2_semi_auto3():
  90. """
  91. Feature: distribute operator SparseGatherV2 in auto parallel.
  92. Description: gather net with strategy in semi auto parallel, gather axis is 1.
  93. Expectation: compile done without error.
  94. """
  95. strategy1 = ((2, 4), (1, 1))
  96. strategy2 = ((4, 2, 1), (4, 2, 1))
  97. net = GradWrap(NetWithLoss(Net(1, strategy1, strategy2)))
  98. compile_net(net, [64, 32], [64, 64, 64])
  99. def test_gatherv2_semi_auto4():
  100. """
  101. Feature: distribute operator SparseGatherV2 in auto parallel.
  102. Description: gather net with strategy in semi auto parallel, gather axis is 0.
  103. Expectation: compile done without error.
  104. """
  105. context.set_auto_parallel_context(dataset_strategy="full_batch")
  106. strategy2 = ((4, 2, 1), (4, 2, 1))
  107. net = GradWrap(NetWithLoss(Net(0, None, strategy2)))
  108. compile_net(net, [64, 32], [64, 64, 32])
  109. def test_gatherv2_semi_auto5():
  110. """
  111. Feature: distribute operator SparseGatherV2 in auto parallel.
  112. Description: gather net with strategy in semi auto parallel, gather axis is 1.
  113. Expectation: compile done without error.
  114. """
  115. strategy2 = ((4, 2, 1), (4, 2, 1))
  116. net = GradWrap(NetWithLoss(Net(1, None, strategy2)))
  117. compile_net(net, [64, 32], [64, 64, 64])
  118. def test_gatherv2_auto0():
  119. """
  120. Feature: distribute operator SparseGatherV2 in auto parallel.
  121. Description: gather net with strategy in semi auto parallel, gather axis is 1.
  122. Expectation: compile done without error.
  123. """
  124. context.set_auto_parallel_context(dataset_strategy="full_batch")
  125. net = GradWrap(NetWithLoss(Net(0)))
  126. compile_net(net, [64, 32], [64, 64, 32], parallel_mode="auto_parallel")
  127. def test_gatherv2_auto1():
  128. """
  129. Feature: distribute operator SparseGatherV2 in auto parallel.
  130. Description: gather net with strategy in semi auto parallel, gather axis is 1.
  131. Expectation: compile done without error.
  132. """
  133. net = GradWrap(NetWithLoss(Net(1)))
  134. compile_net(net, [64, 32], [64, 64, 64], parallel_mode="auto_parallel")
  135. def test_gatherv2_cpu0():
  136. """
  137. Feature: distribute operator SparseGatherV2 in auto parallel.
  138. Description: gather net with strategy in semi auto parallel, gather axis is 1. target is cpu.
  139. Expectation: compile done without error.
  140. """
  141. strategy1 = ((8, 1), (1, 1))
  142. strategy2 = ((4, 2, 1), (4, 2, 1))
  143. net = NetWithLoss(Net(0, strategy1, strategy2, None, "CPU"))
  144. compile_net(net, [64, 64], [64, 64, 64])
  145. def test_gatherv2_cpu1():
  146. """
  147. Feature: distribute operator SparseGatherV2 in auto parallel.
  148. Description: gather net with strategy in semi auto parallel, gather axis is 1. target is cpu.
  149. Expectation: compile done without error.
  150. """
  151. strategy1 = ((16, 1), (1, 1))
  152. strategy2 = ((4, 2, 1), (4, 2, 1))
  153. net = NetWithLoss(Net(0, strategy1, strategy2, None, "CPU"))
  154. compile_net(net, [64, 64], [64, 64, 64], device_num=16)
  155. def test_gatherv2_cpu2():
  156. """
  157. Feature: distribute operator SparseGatherV2 in auto parallel.
  158. Description: gather net with strategy in semi auto parallel, gather axis is 1. target is cpu.
  159. Expectation: compile done without error.
  160. """
  161. strategy1 = ((1, 8), (1, 1))
  162. strategy2 = ((4, 2, 1), (4, 2, 1))
  163. net = NetWithLoss(Net(0, strategy1, strategy2, None, "CPU"))
  164. compile_net(net, [64, 64], [64, 64, 64])