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

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