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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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 test_no_grad():
  45. class Net(nn.Cell):
  46. def __init__(self, strategy1, strategy2):
  47. super().__init__()
  48. self.matmul1 = P.MatMul().set_strategy(strategy1)
  49. self.matmul2 = P.MatMul().set_strategy(strategy2)
  50. def construct(self, x, y, b):
  51. out = self.matmul1(x, y)
  52. out = self.matmul2(out, b)
  53. return out
  54. context.set_auto_parallel_context(device_num=8, global_rank=0)
  55. strategy1 = ((4, 2), (2, 1))
  56. strategy2 = ((2, 4), (4, 1))
  57. net = Net(strategy1, strategy2)
  58. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel")
  59. x = Tensor(np.ones([128, 32]), dtype=ms.float32)
  60. y = Tensor(np.ones([32, 64]), dtype=ms.float32)
  61. b = Tensor(np.ones([64, 64]), dtype=ms.float32)
  62. _executor.compile(net, x, y, b)
  63. def test_grad_sens_parameter_type():
  64. class Net(nn.Cell):
  65. def __init__(self, strategy1, strategy2):
  66. super().__init__()
  67. self.matmul1 = P.MatMul().set_strategy(strategy1)
  68. self.matmul2 = P.MatMul().set_strategy(strategy2)
  69. def construct(self, x, y, b):
  70. out = self.matmul1(x, y)
  71. out = self.matmul2(out, b)
  72. return out
  73. context.set_auto_parallel_context(device_num=8, global_rank=0)
  74. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel")
  75. strategy1 = ((4, 2), (2, 1))
  76. strategy2 = ((2, 4), (4, 1))
  77. net = GradWrap(Net(strategy1, strategy2))
  78. x = Tensor(np.ones([128, 32]), dtype=ms.float32)
  79. y = Tensor(np.ones([32, 64]), dtype=ms.float32)
  80. b = Tensor(np.ones([64, 64]), dtype=ms.float32)
  81. sens = Tensor(np.ones([128, 64]), dtype=ms.float32)
  82. # net(x, y, b, sens)
  83. _executor.compile(net, x, y, b, sens)
  84. def test_grad_sens_tensor_type():
  85. class Net(nn.Cell):
  86. def __init__(self, strategy1, strategy2):
  87. super().__init__()
  88. self.matmul1 = P.MatMul().set_strategy(strategy1)
  89. self.matmul2 = P.MatMul().set_strategy(strategy2)
  90. def construct(self, x, y, b):
  91. out = self.matmul1(x, y)
  92. out = self.matmul2(out, b)
  93. return out
  94. context.set_auto_parallel_context(device_num=8, global_rank=0)
  95. strategy1 = ((4, 2), (2, 1))
  96. strategy2 = ((2, 4), (4, 1))
  97. net = GradWrap2(Net(strategy1, strategy2))
  98. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel")
  99. x = Tensor(np.ones([128, 32]), dtype=ms.float32)
  100. y = Tensor(np.ones([32, 64]), dtype=ms.float32)
  101. b = Tensor(np.ones([64, 64]), dtype=ms.float32)
  102. _executor.compile(net, x, y, b)
  103. def test_grad_sens_scalar_broadcast():
  104. class Net(nn.Cell):
  105. def __init__(self, strategy0, strategy1):
  106. super().__init__()
  107. self.fc_nobias = P.MatMul(transpose_b=True).set_strategy(strategy0)
  108. self.reduce_sum = P.ReduceSum(keep_dims=False).set_strategy(strategy1)
  109. def construct(self, x, y, bias):
  110. out = self.fc_nobias(x, y)
  111. out = self.reduce_sum(out, (0,1))
  112. return out
  113. context.set_auto_parallel_context(device_num=16, global_rank=0)
  114. strategy0 = ((4, 1), (4, 1))
  115. strategy1 = ((4, 1), )
  116. net = GradWrap3(Net(strategy0, strategy1))
  117. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel")
  118. x = Tensor(np.ones([64, 32]), dtype=ms.float32)
  119. y = Tensor(np.ones([64, 32]), dtype=ms.float32)
  120. bias = Tensor(np.ones([64]), dtype=ms.float32)
  121. _executor.compile(net, x, y, bias)