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_parallel_cumsum.py 5.0 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. # Copyright 2021 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. import pytest
  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 _cell_graph_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. grad_all = C.GradOperation(get_all=True)
  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 grad_all(self.network)(x, y)
  39. def compile_net(net, x, y):
  40. net.set_auto_parallel()
  41. net.set_train()
  42. _cell_graph_executor.compile(net, x, y)
  43. def test_cumsum_semi():
  44. """
  45. Feature: CumSum operatorInfo in parallel.
  46. Description: MatMul->CumSum
  47. Expectation: Currently, CumSum does not support the axis dimension split. compile done without error.
  48. """
  49. class Net(nn.Cell):
  50. def __init__(self):
  51. super().__init__()
  52. self.matmul1 = P.MatMul().shard(((16, 1), (1, 1)))
  53. self.cumsum = P.CumSum().shard(((16, 1),))
  54. def construct(self, x, y):
  55. out = self.matmul1(x, y)
  56. out = self.cumsum(out, 0)
  57. return out
  58. size = 16
  59. context.set_auto_parallel_context(device_num=size, global_rank=0)
  60. x = Tensor(np.ones([128, 32]), dtype=ms.float32)
  61. y = Tensor(np.ones([32, 64]), dtype=ms.float32)
  62. net = GradWrap(NetWithLoss(Net()))
  63. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel")
  64. with pytest.raises(RuntimeError):
  65. compile_net(net, x, y)
  66. def test_cumsum_semi2():
  67. """
  68. Feature: CumSum operatorInfo in parallel.
  69. Description: MatMul->CumSum
  70. Expectation: Compile done without error.
  71. """
  72. class Net(nn.Cell):
  73. def __init__(self):
  74. super().__init__()
  75. self.matmul1 = P.MatMul().shard(((16, 1), (1, 1)))
  76. self.cumsum = P.CumSum().shard(((1, 16),))
  77. def construct(self, x, y):
  78. out = self.matmul1(x, y)
  79. out = self.cumsum(out, 0)
  80. return out
  81. size = 16
  82. context.set_auto_parallel_context(device_num=size, global_rank=0)
  83. x = Tensor(np.ones([128, 32]), dtype=ms.float32)
  84. y = Tensor(np.ones([32, 64]), dtype=ms.float32)
  85. net = GradWrap(NetWithLoss(Net()))
  86. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel")
  87. compile_net(net, x, y)
  88. def test_cumsum_semi3():
  89. """
  90. Feature: CumSum operatorInfo in parallel.
  91. Description: MatMul->CumSum
  92. Expectation: Compile done without error.
  93. """
  94. class Net(nn.Cell):
  95. def __init__(self):
  96. super().__init__()
  97. self.matmul1 = P.MatMul().shard(((16, 1), (1, 1)))
  98. self.cumsum = P.CumSum().shard(((2, 1),))
  99. def construct(self, x, y):
  100. out = self.matmul1(x, y)
  101. out = self.cumsum(out, 1)
  102. return out
  103. size = 16
  104. context.set_auto_parallel_context(device_num=size, global_rank=0)
  105. x = Tensor(np.ones([128, 32]), dtype=ms.float32)
  106. y = Tensor(np.ones([32, 64]), dtype=ms.float32)
  107. net = GradWrap(NetWithLoss(Net()))
  108. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel")
  109. compile_net(net, x, y)
  110. def test_cumsum_auto():
  111. """
  112. Feature: CumSum operatorInfo in parallel.
  113. Description: MatMul->CumSum
  114. Expectation: Compile done without error.
  115. """
  116. class Net(nn.Cell):
  117. def __init__(self):
  118. super().__init__()
  119. self.matmul1 = P.MatMul().shard(((16, 1), (1, 1)))
  120. self.cumsum = P.CumSum()
  121. def construct(self, x, y):
  122. out = self.matmul1(x, y)
  123. out = self.cumsum(out, -1)
  124. return out
  125. size = 16
  126. context.set_auto_parallel_context(device_num=size, global_rank=0)
  127. x = Tensor(np.ones([128, 32]), dtype=ms.float32)
  128. y = Tensor(np.ones([32, 64]), dtype=ms.float32)
  129. net = GradWrap(NetWithLoss(Net()))
  130. context.set_auto_parallel_context(parallel_mode="auto_parallel")
  131. compile_net(net, x, y)