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

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