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_matmul_tensor.py 5.4 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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.context import set_auto_parallel_context
  16. from mindspore import context
  17. import mindspore.nn as nn
  18. from mindspore.ops import operations as P
  19. from mindspore import Tensor
  20. from tests.ut.python.ops.test_math_ops import VirtualLoss
  21. import mindspore as ms
  22. from mindspore.common.api import _executor
  23. from mindspore.ops import composite as C
  24. import mindspore.common.dtype as mstype
  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 C.grad_all(self.network)(x, y)
  39. def compile(net, x, y):
  40. net.set_auto_parallel()
  41. _executor.compile(net, x, y)
  42. # model_parallel test
  43. def test_two_matmul():
  44. class Net(nn.Cell):
  45. def __init__(self, strategy1, strategy2, strategy3):
  46. super().__init__()
  47. self.matmul1 = P.MatMul().set_strategy(strategy1)
  48. self.matmul2 = P.MatMul().set_strategy(strategy2)
  49. self.matmul3 = P.MatMul().set_strategy(strategy3)
  50. self.diag = P.Diag()
  51. self.fill = P.Fill()
  52. def construct(self, x, y):
  53. fill = self.diag(self.fill(mstype.float32, (128, ), 1.0))
  54. out1 = self.matmul1(fill, x)
  55. out2 = self.matmul2(y, fill)
  56. out = self.matmul3(out1, out2)
  57. return out
  58. set_auto_parallel_context(device_num=8, global_rank=0)
  59. strategy1 = ((2, 2), (2, 2))
  60. strategy2 = ((1, 8), (8, 1))
  61. strategy3 = ((2, 4), (4, 1))
  62. net = GradWrap(NetWithLoss(Net(strategy1, strategy2, strategy3)))
  63. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel")
  64. x = Tensor(np.ones([128, 32]), dtype=ms.float32)
  65. y = Tensor(np.ones([32, 128]), dtype=ms.float32)
  66. compile(net, x, y)
  67. def test_matmul_mul_broadcast2():
  68. class Net(nn.Cell):
  69. def __init__(self, strategy1, strategy2):
  70. super().__init__()
  71. self.matmul = P.MatMul().set_strategy(strategy1)
  72. self.mul = P.Mul().set_strategy(strategy2)
  73. self.t = Tensor(0.9, ms.float32)
  74. def construct(self, x, y):
  75. out = self.matmul(x, y)
  76. out = self.mul(out, self.t)
  77. return out
  78. context.set_auto_parallel_context(device_num=8, global_rank=0)
  79. strategy1 = ((2, 4), (4, 1))
  80. strategy2 = ((4, 1), ())
  81. net = GradWrap(NetWithLoss(Net(strategy1, strategy2)))
  82. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel")
  83. x = Tensor(np.ones([64, 32]), dtype=ms.float32)
  84. y = Tensor(np.ones([32, 1]), dtype=ms.float32)
  85. compile(net, x, y)
  86. def test_two_matmul1():
  87. class Net(nn.Cell):
  88. def __init__(self, strategy1, strategy2, strategy3):
  89. super().__init__()
  90. self.matmul1 = P.MatMul().set_strategy(strategy1)
  91. self.matmul2 = P.MatMul().set_strategy(strategy2)
  92. self.matmul3 = P.MatMul().set_strategy(strategy3)
  93. self.diag = P.Diag()
  94. self.fill = P.Fill()
  95. def construct(self, x, y):
  96. fill = self.diag(self.fill(mstype.float32, (128, ), 1.0))
  97. out1 = self.matmul1(fill, x)
  98. out2 = self.matmul2(fill, y)
  99. out = self.matmul3(out1, out2)
  100. return out
  101. set_auto_parallel_context(device_num=8, global_rank=0)
  102. strategy1 = ((2, 2), (2, 2))
  103. strategy2 = ((1, 8), (8, 1))
  104. strategy3 = ((2, 4), (4, 1))
  105. net = GradWrap(NetWithLoss(Net(strategy1, strategy2, strategy3)))
  106. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel")
  107. x = Tensor(np.ones([128, 128]), dtype=ms.float32)
  108. y = Tensor(np.ones([128, 128]), dtype=ms.float32)
  109. compile(net, x, y)
  110. def test_matmul_add_tensor():
  111. class Net(nn.Cell):
  112. def __init__(self, strategy1, strategy2):
  113. super().__init__()
  114. self.matmul = P.MatMul().set_strategy(strategy1)
  115. self.add = P.TensorAdd().set_strategy(strategy2)
  116. self.b = Tensor(0.9, ms.float32)
  117. def construct(self, x, y):
  118. out = self.matmul(x, y)
  119. out = self.add(out, self.b)
  120. return out
  121. context.set_auto_parallel_context(device_num=8, global_rank=0)
  122. strategy1 = ((2, 2), (2, 2))
  123. strategy2 = ((4, 2), ())
  124. net = GradWrap(NetWithLoss(Net(strategy1, strategy2)))
  125. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel")
  126. x = Tensor(np.ones([64, 32]), dtype=ms.float32)
  127. y = Tensor(np.ones([32, 64]), dtype=ms.float32)
  128. compile(net, x, y)