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_auto_parallel_arithmetic.py 5.7 kB

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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. import re
  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 mindspore.parallel._utils import _reset_op_id as reset_op_id
  24. from tests.ut.python.ops.test_math_ops import VirtualLoss
  25. context.set_context(mode=context.GRAPH_MODE)
  26. grad_all = C.GradOperation(get_all=True)
  27. class NetWithLoss(nn.Cell):
  28. def __init__(self, network):
  29. super(NetWithLoss, self).__init__()
  30. self.loss = VirtualLoss()
  31. self.network = network
  32. def construct(self, x, y, b):
  33. predict = self.network(x, y, b)
  34. return self.loss(predict)
  35. class GradWrap(nn.Cell):
  36. def __init__(self, network):
  37. super(GradWrap, self).__init__()
  38. self.network = network
  39. def construct(self, x, y, b):
  40. return grad_all(self.network)(x, y, b)
  41. def compile_net(net, x, y, b, phase):
  42. net.set_auto_parallel()
  43. net.set_train()
  44. _executor.compile(net, x, y, b, phase=phase)
  45. def test_auto_parallel_arithmetic():
  46. class Net(nn.Cell):
  47. def __init__(self):
  48. super().__init__()
  49. self.matmul = P.MatMul()
  50. self.floordiv = P.FloorDiv()
  51. def construct(self, x, y, b):
  52. out = self.matmul(x, y)
  53. out = self.floordiv(out, b)
  54. return out
  55. context.set_auto_parallel_context(device_num=8, global_rank=0)
  56. net = NetWithLoss(Net())
  57. context.set_auto_parallel_context(parallel_mode="auto_parallel")
  58. reset_op_id()
  59. x = Tensor(np.ones([64, 32]), dtype=ms.float32)
  60. y = Tensor(np.ones([32, 128]), dtype=ms.float32)
  61. b = Tensor(np.ones([64, 128]), dtype=ms.float32)
  62. compile_net(net, x, y, b, phase='train')
  63. strategies = _executor._get_shard_strategy(net)
  64. for (k, v) in strategies.items():
  65. if re.search('FloorDiv-op', k) is not None:
  66. assert v == [[2, 4], [2, 4]]
  67. elif re.search('MatMul-op', k) is not None:
  68. assert v == [[2, 1], [1, 4]]
  69. def test_auto_parallel_arithmetic_broadcast_both():
  70. class Net(nn.Cell):
  71. def __init__(self):
  72. super().__init__()
  73. self.matmul = P.MatMul()
  74. self.floordiv = P.FloorDiv()
  75. def construct(self, x, y, b):
  76. out = self.matmul(x, y)
  77. out = self.floordiv(out, b)
  78. return out
  79. context.set_auto_parallel_context(device_num=8, global_rank=0)
  80. net = NetWithLoss(Net())
  81. context.set_auto_parallel_context(parallel_mode="auto_parallel")
  82. reset_op_id()
  83. x = Tensor(np.ones([64, 32]), dtype=ms.float32)
  84. y = Tensor(np.ones([32, 1]), dtype=ms.float32)
  85. b = Tensor(np.ones([1, 64]), dtype=ms.float32)
  86. compile_net(net, x, y, b, phase='train')
  87. strategies = _executor._get_shard_strategy(net)
  88. for (k, v) in strategies.items():
  89. if re.search('FloorDiv-op', k) is not None:
  90. assert v == [[8, 1], [1, 1]]
  91. elif re.search('MatMul-op', k) is not None:
  92. assert v == [[8, 1], [1, 1]]
  93. def test_auto_parallel_arithmetic_broadcast_right():
  94. class Net(nn.Cell):
  95. def __init__(self):
  96. super().__init__()
  97. self.matmul = P.MatMul()
  98. self.floordiv = P.FloorDiv()
  99. def construct(self, x, y, b):
  100. out = self.matmul(x, y)
  101. out = self.floordiv(out, b)
  102. return out
  103. context.set_auto_parallel_context(device_num=8, global_rank=0)
  104. net = NetWithLoss(Net())
  105. context.set_auto_parallel_context(parallel_mode="auto_parallel")
  106. reset_op_id()
  107. x = Tensor(np.ones([64, 32]), dtype=ms.float32)
  108. y = Tensor(np.ones([32, 32]), dtype=ms.float32)
  109. b = Tensor(np.ones([32]), dtype=ms.float32)
  110. compile_net(net, x, y, b, phase='train')
  111. strategies = _executor._get_shard_strategy(net)
  112. for (k, v) in strategies.items():
  113. if re.search('FloorDiv-op', k) is not None:
  114. assert v == [[4, 2], [2]]
  115. elif re.search('MatMul-op', k) is not None:
  116. assert v == [[4, 1], [1, 2]]
  117. def test_auto_parallel_arithmetic_broadcast_left():
  118. class Net(nn.Cell):
  119. def __init__(self):
  120. super().__init__()
  121. self.matmul = P.MatMul()
  122. self.floordiv = P.FloorDiv()
  123. def construct(self, x, y, b):
  124. out = self.matmul(x, y)
  125. out = self.floordiv(out, b)
  126. return out
  127. context.set_auto_parallel_context(device_num=8, global_rank=0)
  128. net = NetWithLoss(Net())
  129. context.set_auto_parallel_context(parallel_mode="auto_parallel")
  130. reset_op_id()
  131. x = Tensor(np.ones([64, 32]), dtype=ms.float32)
  132. y = Tensor(np.ones([32, 32]), dtype=ms.float32)
  133. b = Tensor(np.ones([128, 64, 32]), dtype=ms.float32)
  134. compile_net(net, x, y, b, phase="train")
  135. strategies = _executor._get_shard_strategy(net)
  136. for (k, v) in strategies.items():
  137. if re.search('FloorDiv-op', k) is not None:
  138. assert v == [[4, 2], [1, 4, 2]]
  139. elif re.search('MatMul-op', k) is not None:
  140. assert v == [[4, 1], [1, 2]]