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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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 test_auto_parallel_arithmetic():
  41. class Net(nn.Cell):
  42. def __init__(self):
  43. super().__init__()
  44. self.matmul = P.MatMul()
  45. self.floordiv = P.FloorDiv()
  46. def construct(self, x, y, b):
  47. out = self.matmul(x, y)
  48. out = self.floordiv(out, b)
  49. return out
  50. context.set_auto_parallel_context(device_num=8, global_rank=0)
  51. net = NetWithLoss(Net())
  52. context.set_auto_parallel_context(parallel_mode="auto_parallel")
  53. reset_op_id()
  54. x = Tensor(np.ones([64, 32]), dtype=ms.float32)
  55. y = Tensor(np.ones([32, 128]), dtype=ms.float32)
  56. b = Tensor(np.ones([64, 128]), dtype=ms.float32)
  57. _executor.compile(net, x, y, b, phase='train')
  58. strategies = _executor._get_strategy(net)
  59. expected_strategies = {'Default/network-Net/FloorDiv-op0': [[2, 4], [2, 4]],
  60. 'Default/network-Net/MatMul-op1': [[2, 1], [1, 4]]}
  61. assert strategies == expected_strategies
  62. def test_auto_parallel_arithmetic_broadcast_both():
  63. class Net(nn.Cell):
  64. def __init__(self):
  65. super().__init__()
  66. self.matmul = P.MatMul()
  67. self.floordiv = P.FloorDiv()
  68. def construct(self, x, y, b):
  69. out = self.matmul(x, y)
  70. out = self.floordiv(out, b)
  71. return out
  72. context.set_auto_parallel_context(device_num=8, global_rank=0)
  73. net = NetWithLoss(Net())
  74. context.set_auto_parallel_context(parallel_mode="auto_parallel")
  75. reset_op_id()
  76. x = Tensor(np.ones([64, 32]), dtype=ms.float32)
  77. y = Tensor(np.ones([32, 1]), dtype=ms.float32)
  78. b = Tensor(np.ones([1, 64]), dtype=ms.float32)
  79. _executor.compile(net, x, y, b, phase='train')
  80. strategies = _executor._get_strategy(net)
  81. expected_strategies = {'Default/network-Net/FloorDiv-op0': [[8, 1], [1, 1]],
  82. 'Default/network-Net/MatMul-op1': [[8, 1], [1, 1]]}
  83. assert strategies == expected_strategies
  84. def test_auto_parallel_arithmetic_broadcast_right():
  85. class Net(nn.Cell):
  86. def __init__(self):
  87. super().__init__()
  88. self.matmul = P.MatMul()
  89. self.floordiv = P.FloorDiv()
  90. def construct(self, x, y, b):
  91. out = self.matmul(x, y)
  92. out = self.floordiv(out, b)
  93. return out
  94. context.set_auto_parallel_context(device_num=8, global_rank=0)
  95. net = NetWithLoss(Net())
  96. context.set_auto_parallel_context(parallel_mode="auto_parallel")
  97. reset_op_id()
  98. x = Tensor(np.ones([64, 32]), dtype=ms.float32)
  99. y = Tensor(np.ones([32, 32]), dtype=ms.float32)
  100. b = Tensor(np.ones([32]), dtype=ms.float32)
  101. _executor.compile(net, x, y, b, phase='train')
  102. strategies = _executor._get_strategy(net)
  103. expected_strategies = {'Default/network-Net/FloorDiv-op0': [[4, 2], [2]],
  104. 'Default/network-Net/MatMul-op1': [[4, 1], [1, 2]]}
  105. assert strategies == expected_strategies
  106. def test_auto_parallel_arithmetic_broadcast_left():
  107. class Net(nn.Cell):
  108. def __init__(self):
  109. super().__init__()
  110. self.matmul = P.MatMul()
  111. self.floordiv = P.FloorDiv()
  112. def construct(self, x, y, b):
  113. out = self.matmul(x, y)
  114. out = self.floordiv(out, b)
  115. return out
  116. context.set_auto_parallel_context(device_num=8, global_rank=0)
  117. net = NetWithLoss(Net())
  118. context.set_auto_parallel_context(parallel_mode="auto_parallel")
  119. reset_op_id()
  120. x = Tensor(np.ones([64, 32]), dtype=ms.float32)
  121. y = Tensor(np.ones([32, 32]), dtype=ms.float32)
  122. b = Tensor(np.ones([128, 64, 32]), dtype=ms.float32)
  123. _executor.compile(net, x, y, b, phase="train")
  124. strategies = _executor._get_strategy(net)
  125. expected_strategies = {'Default/network-Net/FloorDiv-op0': [[4, 2], [1, 4, 2]],
  126. 'Default/network-Net/MatMul-op1': [[4, 1], [1, 2]]}
  127. assert strategies == expected_strategies