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.4 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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
  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. import mindspore.common.dtype as mstype
  24. class GradWrap(nn.Cell):
  25. def __init__(self, network):
  26. super(GradWrap, self).__init__()
  27. self.network = network
  28. def construct(self, x, y, b, sens):
  29. return C.grad_all_with_sens(self.network)(x, y, b, sens)
  30. class GradWrap2(nn.Cell):
  31. def __init__(self, network):
  32. super(GradWrap2, self).__init__()
  33. self.network = network
  34. def construct(self, x, y, b):
  35. loss = self.network(x, y, b)
  36. sens = P.Fill()(mstype.float32, P.Shape()(loss), 1.0)
  37. return C.grad_all_with_sens(self.network)(x, y, b, sens)
  38. class GradWrap3(nn.Cell):
  39. def __init__(self, network):
  40. super(GradWrap3, self).__init__()
  41. self.network = network
  42. def construct(self, x, y, bias):
  43. return C.grad_all(self.network)(x, y, bias)
  44. def compile(net, x, y, b):
  45. net.set_auto_parallel()
  46. _executor.compile(net, x, y, b)
  47. def test_no_grad():
  48. class Net(nn.Cell):
  49. def __init__(self, strategy1, strategy2):
  50. super().__init__()
  51. self.matmul1 = P.MatMul().set_strategy(strategy1)
  52. self.matmul2 = P.MatMul().set_strategy(strategy2)
  53. def construct(self, x, y, b):
  54. out = self.matmul1(x, y)
  55. out = self.matmul2(out, b)
  56. return out
  57. context.set_auto_parallel_context(device_num=8, global_rank=0)
  58. strategy1 = ((4, 2), (2, 1))
  59. strategy2 = ((2, 4), (4, 1))
  60. net = Net(strategy1, strategy2)
  61. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel")
  62. x = Tensor(np.ones([128, 32]), dtype=ms.float32)
  63. y = Tensor(np.ones([32, 64]), dtype=ms.float32)
  64. b = Tensor(np.ones([64, 64]), dtype=ms.float32)
  65. compile(net, x, y, b)
  66. def test_grad_sens_parameter_type():
  67. class Net(nn.Cell):
  68. def __init__(self, strategy1, strategy2):
  69. super().__init__()
  70. self.matmul1 = P.MatMul().set_strategy(strategy1)
  71. self.matmul2 = P.MatMul().set_strategy(strategy2)
  72. def construct(self, x, y, b):
  73. out = self.matmul1(x, y)
  74. out = self.matmul2(out, b)
  75. return out
  76. context.set_auto_parallel_context(device_num=8, global_rank=0)
  77. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel")
  78. strategy1 = ((4, 2), (2, 1))
  79. strategy2 = ((2, 4), (4, 1))
  80. net = GradWrap(Net(strategy1, strategy2))
  81. x = Tensor(np.ones([128, 32]), dtype=ms.float32)
  82. y = Tensor(np.ones([32, 64]), dtype=ms.float32)
  83. b = Tensor(np.ones([64, 64]), dtype=ms.float32)
  84. sens = Tensor(np.ones([128, 64]), dtype=ms.float32)
  85. # net(x, y, b, sens)
  86. net.set_auto_parallel()
  87. _executor.compile(net, x, y, b, sens)
  88. def test_grad_sens_tensor_type():
  89. class Net(nn.Cell):
  90. def __init__(self, strategy1, strategy2):
  91. super().__init__()
  92. self.matmul1 = P.MatMul().set_strategy(strategy1)
  93. self.matmul2 = P.MatMul().set_strategy(strategy2)
  94. def construct(self, x, y, b):
  95. out = self.matmul1(x, y)
  96. out = self.matmul2(out, b)
  97. return out
  98. context.set_auto_parallel_context(device_num=8, global_rank=0)
  99. strategy1 = ((4, 2), (2, 1))
  100. strategy2 = ((2, 4), (4, 1))
  101. net = GradWrap2(Net(strategy1, strategy2))
  102. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel")
  103. x = Tensor(np.ones([128, 32]), dtype=ms.float32)
  104. y = Tensor(np.ones([32, 64]), dtype=ms.float32)
  105. b = Tensor(np.ones([64, 64]), dtype=ms.float32)
  106. compile(net, x, y, b)
  107. def test_grad_sens_scalar_broadcast():
  108. class Net(nn.Cell):
  109. def __init__(self, strategy0, strategy1):
  110. super().__init__()
  111. self.fc_nobias = P.MatMul(transpose_b=True).set_strategy(strategy0)
  112. self.reduce_sum = P.ReduceSum(keep_dims=False).set_strategy(strategy1)
  113. def construct(self, x, y, bias):
  114. out = self.fc_nobias(x, y)
  115. out = self.reduce_sum(out, (0,1))
  116. return out
  117. context.set_auto_parallel_context(device_num=16, global_rank=0)
  118. strategy0 = ((4, 1), (4, 1))
  119. strategy1 = ((4, 1), )
  120. net = GradWrap3(Net(strategy0, strategy1))
  121. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel")
  122. x = Tensor(np.ones([64, 32]), dtype=ms.float32)
  123. y = Tensor(np.ones([64, 32]), dtype=ms.float32)
  124. bias = Tensor(np.ones([64]), dtype=ms.float32)
  125. compile(net, x, y, bias)