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.6 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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 numpy as np
  15. from mindspore import context
  16. import mindspore.nn as nn
  17. from mindspore.ops import operations as P
  18. from mindspore import Tensor
  19. from tests.ut.python.ops.test_math_ops import VirtualLoss
  20. import mindspore as ms
  21. from mindspore.common.api import _executor
  22. from mindspore.ops import composite as C
  23. from mindspore.parallel._utils import _reset_op_id as reset_op_id
  24. from mindspore import context
  25. context.set_context(mode=context.GRAPH_MODE)
  26. class NetWithLoss(nn.Cell):
  27. def __init__(self, network):
  28. super(NetWithLoss, self).__init__()
  29. self.loss = VirtualLoss()
  30. self.network = network
  31. def construct(self, x, y, b):
  32. predict = self.network(x, y, b)
  33. return self.loss(predict)
  34. class GradWrap(nn.Cell):
  35. def __init__(self, network):
  36. super(GradWrap, self).__init__()
  37. self.network = network
  38. def construct(self, x, y, b):
  39. return C.grad_all(self.network)(x, y, b)
  40. def compile(net, x, y, b, phase):
  41. net.set_auto_parallel()
  42. _executor.compile(net, x, y, b, phase=phase)
  43. def test_auto_parallel_arithmetic():
  44. class Net(nn.Cell):
  45. def __init__(self):
  46. super().__init__()
  47. self.matmul = P.MatMul()
  48. self.floordiv = P.FloorDiv()
  49. def construct(self, x, y, b):
  50. out = self.matmul(x, y)
  51. out = self.floordiv(out, b)
  52. return out
  53. context.set_auto_parallel_context(device_num=8, global_rank=0)
  54. net = NetWithLoss(Net())
  55. context.set_auto_parallel_context(parallel_mode="auto_parallel")
  56. reset_op_id()
  57. x = Tensor(np.ones([64, 32]), dtype=ms.float32)
  58. y = Tensor(np.ones([32, 128]), dtype=ms.float32)
  59. b = Tensor(np.ones([64, 128]), dtype=ms.float32)
  60. compile(net, x, y, b, phase='train')
  61. strategies = _executor._get_strategy(net)
  62. expected_strategies = {'Default/network-Net/FloorDiv-op0': [[2, 4], [2, 4]],
  63. 'Default/network-Net/MatMul-op1': [[2, 1], [1, 4]]}
  64. assert strategies == expected_strategies
  65. def test_auto_parallel_arithmetic_broadcast_both():
  66. class Net(nn.Cell):
  67. def __init__(self):
  68. super().__init__()
  69. self.matmul = P.MatMul()
  70. self.floordiv = P.FloorDiv()
  71. def construct(self, x, y, b):
  72. out = self.matmul(x, y)
  73. out = self.floordiv(out, b)
  74. return out
  75. context.set_auto_parallel_context(device_num=8, global_rank=0)
  76. net = NetWithLoss(Net())
  77. context.set_auto_parallel_context(parallel_mode="auto_parallel")
  78. reset_op_id()
  79. x = Tensor(np.ones([64, 32]), dtype=ms.float32)
  80. y = Tensor(np.ones([32, 1]), dtype=ms.float32)
  81. b = Tensor(np.ones([1, 64]), dtype=ms.float32)
  82. compile(net, x, y, b, phase='train')
  83. strategies = _executor._get_strategy(net)
  84. expected_strategies = {'Default/network-Net/FloorDiv-op0': [[8, 1], [1, 1]],
  85. 'Default/network-Net/MatMul-op1': [[8, 1], [1, 1]]}
  86. assert strategies == expected_strategies
  87. def test_auto_parallel_arithmetic_broadcast_right():
  88. class Net(nn.Cell):
  89. def __init__(self):
  90. super().__init__()
  91. self.matmul = P.MatMul()
  92. self.floordiv = P.FloorDiv()
  93. def construct(self, x, y, b):
  94. out = self.matmul(x, y)
  95. out = self.floordiv(out, b)
  96. return out
  97. context.set_auto_parallel_context(device_num=8, global_rank=0)
  98. net = NetWithLoss(Net())
  99. context.set_auto_parallel_context(parallel_mode="auto_parallel")
  100. reset_op_id()
  101. x = Tensor(np.ones([64, 32]), dtype=ms.float32)
  102. y = Tensor(np.ones([32, 32]), dtype=ms.float32)
  103. b = Tensor(np.ones([32]), dtype=ms.float32)
  104. compile(net, x, y, b, phase='train')
  105. strategies = _executor._get_strategy(net)
  106. expected_strategies = {'Default/network-Net/FloorDiv-op0': [[4, 2], [2]],
  107. 'Default/network-Net/MatMul-op1': [[4, 1], [1, 2]]}
  108. assert strategies == expected_strategies
  109. def test_auto_parallel_arithmetic_broadcast_left():
  110. class Net(nn.Cell):
  111. def __init__(self):
  112. super().__init__()
  113. self.matmul = P.MatMul()
  114. self.floordiv = P.FloorDiv()
  115. def construct(self, x, y, b):
  116. out = self.matmul(x, y)
  117. out = self.floordiv(out, b)
  118. return out
  119. context.set_auto_parallel_context(device_num=8, global_rank=0)
  120. net = NetWithLoss(Net())
  121. context.set_auto_parallel_context(parallel_mode="auto_parallel")
  122. reset_op_id()
  123. x = Tensor(np.ones([64, 32]), dtype=ms.float32)
  124. y = Tensor(np.ones([32, 32]), dtype=ms.float32)
  125. b = Tensor(np.ones([128, 64, 32]), dtype=ms.float32)
  126. compile(net, x, y, b, phase="train")
  127. strategies = _executor._get_strategy(net)
  128. expected_strategies = {'Default/network-Net/FloorDiv-op0': [[4, 2], [1, 4, 2]],
  129. 'Default/network-Net/MatMul-op1': [[4, 1], [1, 2]]}
  130. assert strategies == expected_strategies