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 6.6 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
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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. _executor.compile(net, x, y)
  61. def test_gatherv2_semi_auto1():
  62. context.set_auto_parallel_context(device_num=8, global_rank=0, parallel_mode="semi_auto_parallel")
  63. strategy1 = ((8, 1), (1, 1))
  64. strategy2 = ((4, 2, 1), (4, 2, 1))
  65. net = GradWrap(NetWithLoss(Net(0, strategy1, strategy2)))
  66. net.set_auto_parallel()
  67. x = Tensor(np.ones([64, 64]), dtype=ms.float32)
  68. y = Tensor(np.ones([64, 64, 64]), dtype=ms.float32)
  69. _executor.compile(net, x, y)
  70. def test_gatherv2_semi_auto2():
  71. context.set_auto_parallel_context(device_num=8, global_rank=0, parallel_mode="semi_auto_parallel")
  72. strategy1 = ((2, 4), (1, 1))
  73. strategy2 = ((4, 2, 1), (4, 2, 1))
  74. net = GradWrap(NetWithLoss(Net(0, strategy1, strategy2)))
  75. net.set_auto_parallel()
  76. x = Tensor(np.ones([64, 64]), dtype=ms.float32)
  77. y = Tensor(np.ones([64, 64, 64]), dtype=ms.float32)
  78. _executor.compile(net, x, y)
  79. def test_gatherv2_semi_auto3():
  80. context.set_auto_parallel_context(device_num=8, global_rank=0, parallel_mode="semi_auto_parallel")
  81. strategy1 = ((1, 8), (1, 1))
  82. strategy2 = ((4, 2, 1), (4, 2, 1))
  83. net = GradWrap(NetWithLoss(Net(1, strategy1, strategy2)))
  84. net.set_auto_parallel()
  85. x = Tensor(np.ones([64, 64]), dtype=ms.float32)
  86. y = Tensor(np.ones([64, 64, 64]), dtype=ms.float32)
  87. _executor.compile(net, x, y)
  88. def test_gatherv2_semi_auto4():
  89. context.set_auto_parallel_context(device_num=8, global_rank=0, parallel_mode="semi_auto_parallel")
  90. strategy1 = ((8, 1), (1, 1))
  91. strategy2 = ((4, 2, 1), (4, 2, 1))
  92. net = GradWrap(NetWithLoss(Net(1, strategy1, strategy2)))
  93. net.set_auto_parallel()
  94. x = Tensor(np.ones([64, 32]), dtype=ms.float32)
  95. y = Tensor(np.ones([64, 64, 64]), dtype=ms.float32)
  96. _executor.compile(net, x, y)
  97. def test_gatherv2_semi_auto5():
  98. context.set_auto_parallel_context(device_num=8, global_rank=0, parallel_mode="semi_auto_parallel")
  99. strategy1 = ((2, 4), (1, 1))
  100. strategy2 = ((4, 2, 1), (4, 2, 1))
  101. net = GradWrap(NetWithLoss(Net(1, strategy1, strategy2)))
  102. net.set_auto_parallel()
  103. x = Tensor(np.ones([64, 32]), dtype=ms.float32)
  104. y = Tensor(np.ones([64, 64, 64]), dtype=ms.float32)
  105. _executor.compile(net, x, y)
  106. def test_gatherv2_semi_auto6():
  107. context.set_auto_parallel_context(device_num=8, global_rank=0, parallel_mode="semi_auto_parallel")
  108. strategy2 = ((4, 2, 1), (4, 2, 1))
  109. net = GradWrap(NetWithLoss(Net(0, None, strategy2)))
  110. net.set_auto_parallel()
  111. x = Tensor(np.ones([64, 32]), dtype=ms.float32)
  112. y = Tensor(np.ones([64, 64, 32]), dtype=ms.float32)
  113. _executor.compile(net, x, y)
  114. def test_gatherv2_semi_auto7():
  115. context.set_auto_parallel_context(device_num=8, global_rank=0, parallel_mode="semi_auto_parallel")
  116. strategy2 = ((4, 2, 1), (4, 2, 1))
  117. net = GradWrap(NetWithLoss(Net(1, None, strategy2)))
  118. net.set_auto_parallel()
  119. x = Tensor(np.ones([64, 32]), dtype=ms.float32)
  120. y = Tensor(np.ones([64, 64, 64]), dtype=ms.float32)
  121. _executor.compile(net, x, y)
  122. def test_gatherv2_semi_auto8():
  123. context.set_auto_parallel_context(device_num=8, global_rank=0, parallel_mode="semi_auto_parallel")
  124. strategy1 = ((8,), (1, 1))
  125. strategy2 = ((4, 2), (4, 2))
  126. net = GradWrap(NetWithLoss(Net(0, strategy1, strategy2)))
  127. net.set_auto_parallel()
  128. x = Tensor(np.ones([64]), dtype=ms.float32)
  129. y = Tensor(np.ones([64, 64]), dtype=ms.float32)
  130. _executor.compile(net, x, y)
  131. def test_gatherv2_auto0():
  132. context.set_auto_parallel_context(device_num=8, global_rank=0, parallel_mode="auto_parallel")
  133. net = GradWrap(NetWithLoss(Net(0)))
  134. net.set_auto_parallel()
  135. x = Tensor(np.ones([64, 32]), dtype=ms.float32)
  136. y = Tensor(np.ones([64, 64, 32]), dtype=ms.float32)
  137. _executor.compile(net, x, y)
  138. def test_gatherv2_auto1():
  139. context.set_auto_parallel_context(device_num=8, global_rank=0, parallel_mode="auto_parallel")
  140. net = GradWrap(NetWithLoss(Net(1)))
  141. net.set_auto_parallel()
  142. x = Tensor(np.ones([64, 32]), dtype=ms.float32)
  143. y = Tensor(np.ones([64, 64, 64]), dtype=ms.float32)
  144. _executor.compile(net, x, y)