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_split_grad_sens.py 5.9 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
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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.ops import composite as C
  22. from mindspore.ops import operations as P
  23. grad_all = C.GradOperation(get_all=True)
  24. grad_all_with_sens = C.GradOperation(get_all=True, sens_param=True)
  25. class GradWrap(nn.Cell):
  26. def __init__(self, network):
  27. super(GradWrap, self).__init__()
  28. self.network = network
  29. def construct(self, x, y, b, sens):
  30. return grad_all_with_sens(self.network)(x, y, b, sens)
  31. class GradWrap2(nn.Cell):
  32. def __init__(self, network):
  33. super(GradWrap2, self).__init__()
  34. self.network = network
  35. def construct(self, x, y, b):
  36. loss = self.network(x, y, b)
  37. sens = P.Fill()(mstype.float32, P.Shape()(loss), 1.0)
  38. return grad_all_with_sens(self.network)(x, y, b, sens)
  39. class GradWrap3(nn.Cell):
  40. def __init__(self, network):
  41. super(GradWrap3, self).__init__()
  42. self.network = network
  43. def construct(self, x, y, bias):
  44. return grad_all(self.network)(x, y, bias)
  45. class GradWrap4(nn.Cell):
  46. def __init__(self, network):
  47. super(GradWrap4, self).__init__()
  48. self.network = network
  49. def construct(self, x, y):
  50. return grad_all(self.network)(x, y)
  51. def compile_net(net, x, y, b):
  52. net.set_auto_parallel()
  53. _executor.compile(net, x, y, b)
  54. def compile_net_no_bias(net, x, y):
  55. net.set_auto_parallel()
  56. _executor.compile(net, x, y)
  57. def test_no_grad():
  58. class Net(nn.Cell):
  59. def __init__(self, strategy1, strategy2):
  60. super().__init__()
  61. self.matmul1 = P.MatMul().shard(strategy1)
  62. self.matmul2 = P.MatMul().shard(strategy2)
  63. def construct(self, x, y, b):
  64. out = self.matmul1(x, y)
  65. out = self.matmul2(out, b)
  66. return out
  67. context.set_auto_parallel_context(device_num=8, global_rank=0)
  68. strategy1 = ((4, 2), (2, 1))
  69. strategy2 = ((2, 4), (4, 1))
  70. net = Net(strategy1, strategy2)
  71. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel")
  72. x = Tensor(np.ones([128, 32]), dtype=ms.float32)
  73. y = Tensor(np.ones([32, 64]), dtype=ms.float32)
  74. b = Tensor(np.ones([64, 64]), dtype=ms.float32)
  75. compile_net(net, x, y, b)
  76. def test_grad_sens_parameter_type():
  77. class Net(nn.Cell):
  78. def __init__(self, strategy1, strategy2):
  79. super().__init__()
  80. self.matmul1 = P.MatMul().shard(strategy1)
  81. self.matmul2 = P.MatMul().shard(strategy2)
  82. def construct(self, x, y, b):
  83. out = self.matmul1(x, y)
  84. out = self.matmul2(out, b)
  85. return out
  86. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel", device_num=64, global_rank=0)
  87. strategy1 = ((8, 1), (1, 8))
  88. strategy2 = ((8, 8), (8, 1))
  89. net = GradWrap(Net(strategy1, strategy2))
  90. x = Tensor(np.ones([128, 32]), dtype=ms.float32)
  91. y = Tensor(np.ones([32, 64]), dtype=ms.float32)
  92. b = Tensor(np.ones([64, 64]), dtype=ms.float32)
  93. sens = Tensor(np.ones([128, 64]), dtype=ms.float32)
  94. net.set_auto_parallel()
  95. _executor.compile(net, x, y, b, sens, phase='train', auto_parallel_mode=True)
  96. x_layout = [[8, 8], [1, -1], [16, 32], [0], [1]]
  97. y_layout = [[8, 8], [-1, 0], [32, 8], [0], [1]]
  98. b_layout = [[8, 8], [0, -1], [8, 64], [0], [1]]
  99. sens_layout = [[8, 8], [1, -1], [16, 64], [0], [1]]
  100. expect_dict = {'x': x_layout, 'y': y_layout, 'b': b_layout, 'sens': sens_layout}
  101. assert net.parameter_layout_dict == expect_dict
  102. def test_grad_sens_tensor_type():
  103. class Net(nn.Cell):
  104. def __init__(self, strategy1, strategy2):
  105. super().__init__()
  106. self.matmul1 = P.MatMul().shard(strategy1)
  107. self.matmul2 = P.MatMul().shard(strategy2)
  108. def construct(self, x, y, b):
  109. out = self.matmul1(x, y)
  110. out = self.matmul2(out, b)
  111. return out
  112. context.set_auto_parallel_context(device_num=8, global_rank=0)
  113. strategy1 = ((4, 2), (2, 1))
  114. strategy2 = ((2, 4), (4, 1))
  115. net = GradWrap2(Net(strategy1, strategy2))
  116. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel")
  117. x = Tensor(np.ones([128, 32]), dtype=ms.float32)
  118. y = Tensor(np.ones([32, 64]), dtype=ms.float32)
  119. b = Tensor(np.ones([64, 64]), dtype=ms.float32)
  120. compile_net(net, x, y, b)
  121. def test_grad_sens_scalar_broadcast():
  122. class Net(nn.Cell):
  123. def __init__(self, strategy0, strategy1):
  124. super().__init__()
  125. self.fc_nobias = P.MatMul(transpose_b=True).shard(strategy0)
  126. self.reduce_sum = P.ReduceSum(keep_dims=False).shard(strategy1)
  127. def construct(self, x, y):
  128. out = self.fc_nobias(x, y)
  129. out = self.reduce_sum(out, (0, 1))
  130. return out
  131. context.set_auto_parallel_context(device_num=16, global_rank=0)
  132. strategy0 = ((4, 1), (4, 1))
  133. strategy1 = ((4, 1),)
  134. net = GradWrap4(Net(strategy0, strategy1))
  135. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel")
  136. x = Tensor(np.ones([64, 32]), dtype=ms.float32)
  137. y = Tensor(np.ones([64, 32]), dtype=ms.float32)
  138. compile_net_no_bias(net, x, y)