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