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_two_matmul.py 4.1 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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. class NetWithLoss(nn.Cell):
  24. def __init__(self, network):
  25. super(NetWithLoss, self).__init__()
  26. self.loss = VirtualLoss()
  27. self.network = network
  28. def construct(self, x, y, b):
  29. predict = self.network(x, y, b)
  30. return self.loss(predict)
  31. class GradWrap(nn.Cell):
  32. def __init__(self, network):
  33. super(GradWrap, self).__init__()
  34. self.network = network
  35. def construct(self, x, y, b):
  36. return C.grad_all(self.network)(x, y, b)
  37. # model_parallel test
  38. def test_two_matmul():
  39. class Net(nn.Cell):
  40. def __init__(self, strategy1, strategy2):
  41. super().__init__()
  42. self.matmul1 = P.MatMul().set_strategy(strategy1)
  43. self.matmul2 = P.MatMul().set_strategy(strategy2)
  44. def construct(self, x, y, b):
  45. out = self.matmul1(x, y)
  46. out = self.matmul2(out, b)
  47. return out
  48. context.set_auto_parallel_context(device_num=8, global_rank=0, mirror_mean=True)
  49. strategy1 = ((4, 2), (2, 1))
  50. strategy2 = ((2, 4), (4, 1))
  51. net = GradWrap(NetWithLoss(Net(strategy1, strategy2)))
  52. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel")
  53. x = Tensor(np.ones([128, 32]), dtype=ms.float32)
  54. y = Tensor(np.ones([32, 64]), dtype=ms.float32)
  55. b = Tensor(np.ones([64, 64]), dtype=ms.float32)
  56. _executor.compile(net, x, y, b)
  57. def test_two_matmul_repeated_calculation1():
  58. class Net(nn.Cell):
  59. def __init__(self, strategy1, strategy2):
  60. super().__init__()
  61. self.matmul1 = P.MatMul().set_strategy(strategy1)
  62. self.matmul2 = P.MatMul().set_strategy(strategy2)
  63. def construct(self, x, y, b):
  64. out = self.matmul1(x, y)
  65. out = self.matmul2(out, b)
  66. return out
  67. context.set_auto_parallel_context(device_num=64, global_rank=5, mirror_mean=True)
  68. strategy1 = ((2, 4), (4, 8))
  69. strategy2 = ((1, 1), (1, 1))
  70. net = GradWrap(NetWithLoss(Net(strategy1, strategy2)))
  71. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel")
  72. x = Tensor(np.ones([128, 32]), dtype=ms.float32)
  73. y = Tensor(np.ones([32, 64]), dtype=ms.float32)
  74. b = Tensor(np.ones([64, 64]), dtype=ms.float32)
  75. _executor.compile(net, x, y, b)
  76. def test_two_matmul_repeated_calculation2():
  77. class Net(nn.Cell):
  78. def __init__(self, strategy1, strategy2):
  79. super().__init__()
  80. self.matmul1 = P.MatMul().set_strategy(strategy1)
  81. self.matmul2 = P.MatMul().set_strategy(strategy2)
  82. def construct(self, x, y, b):
  83. out = self.matmul1(x, y)
  84. out = self.matmul2(out, b)
  85. return out
  86. context.set_auto_parallel_context(device_num=64, global_rank=15)
  87. strategy1 = ((2, 4), (4, 8))
  88. strategy2 = ((2, 2), (2, 1))
  89. net = GradWrap(NetWithLoss(Net(strategy1, strategy2)))
  90. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel")
  91. x = Tensor(np.ones([128, 32]), dtype=ms.float32)
  92. y = Tensor(np.ones([32, 64]), dtype=ms.float32)
  93. b = Tensor(np.ones([64, 64]), dtype=ms.float32)
  94. _executor.compile(net, x, y, b)