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