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

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