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_auto_parallel_rhombus.py 4.8 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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, Parameter
  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. def compile(net, x, y, b):
  38. net.set_auto_parallel()
  39. _executor.compile(net, x, y, b)
  40. def test_rhombus1():
  41. class Net(nn.Cell):
  42. def __init__(self):
  43. super().__init__()
  44. self.matmul = P.MatMul()
  45. self.tadd1 = P.TensorAdd()
  46. self.tadd2 = P.TensorAdd()
  47. self.weight = Parameter(Tensor(np.ones([128, 128]).astype(np.float32) * 0.01), "w", requires_grad=True)
  48. def construct(self, x, y, z):
  49. mm_out = self.matmul(x, self.weight)
  50. ta1_out = self.tadd1(y, z)
  51. out = self.tadd2(ta1_out, mm_out)
  52. return out
  53. size = 16
  54. context.set_auto_parallel_context(device_num=size, global_rank=0)
  55. x = Tensor(np.ones([128, 128]), dtype=ms.float32)
  56. y = Tensor(np.ones([128, 128]), dtype=ms.float32)
  57. b = Tensor(np.ones([128, 128]), dtype=ms.float32)
  58. net = GradWrap(NetWithLoss(Net()))
  59. context.set_auto_parallel_context(parallel_mode="auto_parallel")
  60. compile(net, x, y, b)
  61. def test_rhombus2():
  62. class Net(nn.Cell):
  63. def __init__(self):
  64. super().__init__()
  65. self.matmul1 = P.MatMul()
  66. self.matmul2 = P.MatMul()
  67. self.tadd1 = P.TensorAdd()
  68. self.tadd2 = P.TensorAdd()
  69. self.tadd3 = P.TensorAdd()
  70. self.weight1 = Parameter(Tensor(np.ones([128, 128]).astype(np.float32) * 0.01), "w", requires_grad=True)
  71. self.weight2 = Parameter(Tensor(np.ones([128, 128]).astype(np.float32) * 0.01), "w", requires_grad=True)
  72. def construct(self, x, y, z):
  73. mm1_out = self.matmul1(x, self.weight1)
  74. ta1_out = self.tadd1(y, z)
  75. ta2_out = self.tadd2(mm1_out, ta1_out)
  76. mm2_out = self.matmul2(ta1_out, self.weight2)
  77. ta3_out = self.tadd3(ta2_out, mm2_out)
  78. return ta3_out
  79. size = 16
  80. context.set_auto_parallel_context(device_num=size, global_rank=0)
  81. x = Tensor(np.ones([128, 128]), dtype=ms.float32)
  82. y = Tensor(np.ones([128, 128]), dtype=ms.float32)
  83. b = Tensor(np.ones([128, 128]), dtype=ms.float32)
  84. net = GradWrap(NetWithLoss(Net()))
  85. context.set_auto_parallel_context(parallel_mode="auto_parallel")
  86. compile(net, x, y, b)
  87. def test_rhombus3():
  88. class Net(nn.Cell):
  89. def __init__(self):
  90. super().__init__()
  91. self.matmul1 = P.MatMul()
  92. self.tadd1 = P.TensorAdd()
  93. self.tadd2 = P.TensorAdd()
  94. self.tadd3 = P.TensorAdd()
  95. self.tadd4 = P.TensorAdd()
  96. self.weight1 = Parameter(Tensor(np.ones([128, 128]).astype(np.float32) * 0.01), "w", requires_grad=True)
  97. self.t = Tensor(np.ones([128, 128]).astype(np.float32) * 0.01)
  98. def construct(self, x, y, z):
  99. mm1_out = self.matmul1(x, self.weight1)
  100. ta1_out = self.tadd1(y, z)
  101. ta2_out = self.tadd2(mm1_out, ta1_out)
  102. ta3_out = self.tadd3(ta1_out, self.t)
  103. ta4_out = self.tadd4(ta2_out, ta3_out)
  104. return ta4_out
  105. size = 16
  106. context.set_auto_parallel_context(device_num=size, global_rank=0)
  107. x = Tensor(np.ones([128, 128]), dtype=ms.float32)
  108. y = Tensor(np.ones([128, 128]), dtype=ms.float32)
  109. z = Tensor(np.ones([128, 128]), dtype=ms.float32)
  110. net = GradWrap(NetWithLoss(Net()))
  111. context.set_auto_parallel_context(parallel_mode="auto_parallel")
  112. compile(net, x, y, z)