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
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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. class NetWithLoss(nn.Cell):
  25. def __init__(self, network):
  26. super(NetWithLoss, self).__init__()
  27. self.loss = VirtualLoss()
  28. self.network = network
  29. def construct(self, x, y):
  30. predict = self.network(x, y)
  31. return self.loss(predict)
  32. class GradWrap(nn.Cell):
  33. def __init__(self, network):
  34. super(GradWrap, self).__init__()
  35. self.network = network
  36. def construct(self, x, y):
  37. return C.grad_all(self.network)(x, y)
  38. class Net(nn.Cell):
  39. def __init__(self, axis=0, strategy1=None, strategy2=None, shape=None, target=""):
  40. super().__init__()
  41. if shape is None:
  42. shape = [64, 64]
  43. self.gatherv2 = P.GatherV2().set_strategy(strategy1).add_prim_attr("primitive_target", target)
  44. self.mul = P.Mul().set_strategy(strategy2)
  45. self.index = Tensor(np.ones(shape), dtype=ms.int32)
  46. self.axis = axis
  47. def construct(self, x, y):
  48. out = self.gatherv2(x, self.index, self.axis)
  49. out = self.mul(out, y)
  50. return out
  51. def test_gatherv2_semi_auto0():
  52. context.set_auto_parallel_context(device_num=8, global_rank=0, parallel_mode="semi_auto_parallel")
  53. strategy1 = ((1, 8), (1, 1))
  54. strategy2 = ((4, 2, 1), (4, 2, 1))
  55. net = GradWrap(NetWithLoss(Net(0, strategy1, strategy2)))
  56. net.set_auto_parallel()
  57. x = Tensor(np.ones([64, 64]), dtype=ms.float32)
  58. y = Tensor(np.ones([64, 64, 64]), dtype=ms.float32)
  59. _executor.compile(net, x, y)
  60. def test_gatherv2_semi_auto1():
  61. context.set_auto_parallel_context(device_num=8, global_rank=0, parallel_mode="semi_auto_parallel")
  62. strategy1 = ((8, 1), (1, 1))
  63. strategy2 = ((4, 2, 1), (4, 2, 1))
  64. net = GradWrap(NetWithLoss(Net(0, strategy1, strategy2)))
  65. net.set_auto_parallel()
  66. x = Tensor(np.ones([64, 64]), dtype=ms.float32)
  67. y = Tensor(np.ones([64, 64, 64]), dtype=ms.float32)
  68. _executor.compile(net, x, y)
  69. def test_gatherv2_semi_auto2():
  70. context.set_auto_parallel_context(device_num=8, global_rank=0, parallel_mode="semi_auto_parallel")
  71. strategy1 = ((2, 4), (1, 1))
  72. strategy2 = ((4, 2, 1), (4, 2, 1))
  73. net = GradWrap(NetWithLoss(Net(0, strategy1, strategy2)))
  74. net.set_auto_parallel()
  75. x = Tensor(np.ones([64, 64]), dtype=ms.float32)
  76. y = Tensor(np.ones([64, 64, 64]), dtype=ms.float32)
  77. _executor.compile(net, x, y)
  78. def test_gatherv2_semi_auto3():
  79. context.set_auto_parallel_context(device_num=8, global_rank=0, parallel_mode="semi_auto_parallel")
  80. strategy1 = ((1, 8), (1, 1))
  81. strategy2 = ((4, 2, 1), (4, 2, 1))
  82. net = GradWrap(NetWithLoss(Net(1, strategy1, strategy2)))
  83. net.set_auto_parallel()
  84. x = Tensor(np.ones([64, 64]), dtype=ms.float32)
  85. y = Tensor(np.ones([64, 64, 64]), dtype=ms.float32)
  86. _executor.compile(net, x, y)
  87. def test_gatherv2_semi_auto4():
  88. context.set_auto_parallel_context(device_num=8, global_rank=0, parallel_mode="semi_auto_parallel")
  89. strategy1 = ((8, 1), (1, 1))
  90. strategy2 = ((4, 2, 1), (4, 2, 1))
  91. net = GradWrap(NetWithLoss(Net(1, strategy1, strategy2)))
  92. net.set_auto_parallel()
  93. x = Tensor(np.ones([64, 32]), dtype=ms.float32)
  94. y = Tensor(np.ones([64, 64, 64]), dtype=ms.float32)
  95. _executor.compile(net, x, y)
  96. def test_gatherv2_semi_auto5():
  97. context.set_auto_parallel_context(device_num=8, global_rank=0, parallel_mode="semi_auto_parallel")
  98. strategy1 = ((2, 4), (1, 1))
  99. strategy2 = ((4, 2, 1), (4, 2, 1))
  100. net = GradWrap(NetWithLoss(Net(1, strategy1, strategy2)))
  101. net.set_auto_parallel()
  102. x = Tensor(np.ones([64, 32]), dtype=ms.float32)
  103. y = Tensor(np.ones([64, 64, 64]), dtype=ms.float32)
  104. _executor.compile(net, x, y)
  105. def test_gatherv2_semi_auto6():
  106. context.set_auto_parallel_context(device_num=8, global_rank=0, parallel_mode="semi_auto_parallel")
  107. strategy2 = ((4, 2, 1), (4, 2, 1))
  108. net = GradWrap(NetWithLoss(Net(0, None, strategy2)))
  109. net.set_auto_parallel()
  110. x = Tensor(np.ones([64, 32]), dtype=ms.float32)
  111. y = Tensor(np.ones([64, 64, 32]), dtype=ms.float32)
  112. _executor.compile(net, x, y)
  113. def test_gatherv2_semi_auto7():
  114. context.set_auto_parallel_context(device_num=8, global_rank=0, parallel_mode="semi_auto_parallel")
  115. strategy2 = ((4, 2, 1), (4, 2, 1))
  116. net = GradWrap(NetWithLoss(Net(1, None, strategy2)))
  117. net.set_auto_parallel()
  118. x = Tensor(np.ones([64, 32]), dtype=ms.float32)
  119. y = Tensor(np.ones([64, 64, 64]), dtype=ms.float32)
  120. _executor.compile(net, x, y)
  121. def test_gatherv2_semi_auto8():
  122. context.set_auto_parallel_context(device_num=8, global_rank=0, parallel_mode="semi_auto_parallel")
  123. strategy1 = ((8,), (1, 1))
  124. strategy2 = ((4, 2), (4, 2))
  125. net = GradWrap(NetWithLoss(Net(0, strategy1, strategy2)))
  126. net.set_auto_parallel()
  127. x = Tensor(np.ones([64]), dtype=ms.float32)
  128. y = Tensor(np.ones([64, 64]), dtype=ms.float32)
  129. _executor.compile(net, x, y)
  130. def test_gatherv2_auto0():
  131. context.set_auto_parallel_context(device_num=8, global_rank=0, parallel_mode="auto_parallel")
  132. net = GradWrap(NetWithLoss(Net(0)))
  133. net.set_auto_parallel()
  134. x = Tensor(np.ones([64, 32]), dtype=ms.float32)
  135. y = Tensor(np.ones([64, 64, 32]), dtype=ms.float32)
  136. _executor.compile(net, x, y)
  137. def test_gatherv2_auto1():
  138. context.set_auto_parallel_context(device_num=8, global_rank=0, parallel_mode="auto_parallel")
  139. net = GradWrap(NetWithLoss(Net(1)))
  140. net.set_auto_parallel()
  141. x = Tensor(np.ones([64, 32]), dtype=ms.float32)
  142. y = Tensor(np.ones([64, 64, 64]), dtype=ms.float32)
  143. _executor.compile(net, x, y)