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_loss_and_optimizer.py 7.4 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  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. import mindspore as ms
  20. from mindspore.common.api import _executor
  21. from mindspore.ops import composite as C
  22. from mindspore.nn.optim import Momentum, LARS
  23. from mindspore.nn import TrainOneStepCell, WithLossCell
  24. class NetWithLoss(nn.Cell):
  25. def __init__(self, network, strategy3):
  26. super(NetWithLoss, self).__init__()
  27. self.loss = P.SoftmaxCrossEntropyWithLogits().set_strategy(strategy3)
  28. self.network = network
  29. def construct(self, x, b):
  30. predict = self.network(x)
  31. return self.loss(predict, b)[0]
  32. def compile(net, x, b):
  33. net.set_auto_parallel()
  34. _executor.compile(net, x, b)
  35. def test_momentum():
  36. class Net(nn.Cell):
  37. def __init__(self, strategy1, strategy2, weight):
  38. super().__init__()
  39. self.weight = Parameter(weight, "w1")
  40. self.matmul = P.MatMul(transpose_a=False, transpose_b=True).set_strategy(strategy1)
  41. self.relu = P.ReLU().set_strategy(strategy2)
  42. def construct(self, x):
  43. out = self.matmul(x, self.weight)
  44. out = self.relu(out)
  45. return out
  46. context.set_auto_parallel_context(device_num=4, global_rank=0)
  47. strategy1 = ((2, 1), (2, 1))
  48. strategy2 = ((4, 1), )
  49. strategy3 = ((4, 1), (4, 1))
  50. x = Tensor(np.ones([64, 32]), dtype=ms.float32)
  51. weight = Tensor(np.ones([64, 32]), dtype=ms.float32)
  52. b = Tensor(np.ones([64, 64]), dtype=ms.float32)
  53. net = Net(strategy1, strategy2, weight)
  54. optimizer = Momentum(net.trainable_params(), learning_rate=0.1, momentum=0.9)
  55. net_with_loss = NetWithLoss(net, strategy3)
  56. train_net = TrainOneStepCell(net_with_loss, optimizer)
  57. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel")
  58. compile(train_net, x, b)
  59. def test_momentum_with_loss_scale():
  60. class Net(nn.Cell):
  61. def __init__(self, strategy1, strategy2, weight):
  62. super().__init__()
  63. self.weight = Parameter(weight, "w1")
  64. self.matmul = P.MatMul(transpose_a=False, transpose_b=True).set_strategy(strategy1)
  65. self.relu = P.ReLU().set_strategy(strategy2)
  66. def construct(self, x):
  67. out = self.matmul(x, self.weight)
  68. out = self.relu(out)
  69. return out
  70. context.set_auto_parallel_context(device_num=4, global_rank=0)
  71. strategy1 = ((2, 1), (2, 1))
  72. strategy2 = ((4, 1), )
  73. strategy3 = ((4, 1), (4, 1))
  74. x = Tensor(np.ones([64, 32]), dtype=ms.float32)
  75. weight = Tensor(np.ones([64, 32]), dtype=ms.float32)
  76. b = Tensor(np.ones([64, 64]), dtype=ms.float32)
  77. net = Net(strategy1, strategy2, weight)
  78. optimizer = Momentum(net.trainable_params(), learning_rate=0.1, momentum=0.9, loss_scale=0.5)
  79. net_with_loss = NetWithLoss(net, strategy3)
  80. train_net = TrainOneStepCell(net_with_loss, optimizer)
  81. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel")
  82. compile(train_net, x, b)
  83. def test_momentum_with_dynamic_lr():
  84. class Net(nn.Cell):
  85. def __init__(self, strategy1, strategy2, weight):
  86. super().__init__()
  87. self.weight = Parameter(weight, "w1")
  88. self.matmul = P.MatMul(transpose_a=False, transpose_b=True).set_strategy(strategy1)
  89. self.relu = P.ReLU().set_strategy(strategy2)
  90. def construct(self, x):
  91. out = self.matmul(x, self.weight)
  92. out = self.relu(out)
  93. return out
  94. context.set_auto_parallel_context(device_num=4, global_rank=0)
  95. strategy1 = ((2, 1), (2, 1))
  96. strategy2 = ((4, 1), )
  97. strategy3 = ((4, 1), (4, 1))
  98. x = Tensor(np.ones([64, 32]), dtype=ms.float32)
  99. weight = Tensor(np.ones([64, 32]), dtype=ms.float32)
  100. b = Tensor(np.ones([64, 64]), dtype=ms.float32)
  101. net = Net(strategy1, strategy2, weight)
  102. lr = Tensor(np.ones([6]), dtype=ms.float32)
  103. optimizer = Momentum(net.trainable_params(), learning_rate=lr, momentum=0.9)
  104. net_with_loss = NetWithLoss(net, strategy3)
  105. train_net = TrainOneStepCell(net_with_loss, optimizer)
  106. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel")
  107. compile(train_net, x, b)
  108. def test_momentum_with_loss_scale_and_dynamic_lr():
  109. class Net(nn.Cell):
  110. def __init__(self, strategy1, strategy2, weight):
  111. super().__init__()
  112. self.weight = Parameter(weight, "w1")
  113. self.matmul = P.MatMul(transpose_a=False, transpose_b=True).set_strategy(strategy1)
  114. self.relu = P.ReLU().set_strategy(strategy2)
  115. def construct(self, x):
  116. out = self.matmul(x, self.weight)
  117. out = self.relu(out)
  118. return out
  119. context.set_auto_parallel_context(device_num=4, global_rank=0)
  120. strategy1 = ((2, 1), (2, 1))
  121. strategy2 = ((4, 1), )
  122. strategy3 = ((4, 1), (4, 1))
  123. x = Tensor(np.ones([64, 32]), dtype=ms.float32)
  124. weight = Tensor(np.ones([64, 32]), dtype=ms.float32)
  125. b = Tensor(np.ones([64, 64]), dtype=ms.float32)
  126. net = Net(strategy1, strategy2, weight)
  127. lr = Tensor(np.ones([6]), dtype=ms.float32)
  128. optimizer = Momentum(net.trainable_params(), learning_rate=lr, momentum=0.9, loss_scale=0.5)
  129. net_with_loss = NetWithLoss(net, strategy3)
  130. train_net = TrainOneStepCell(net_with_loss, optimizer)
  131. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel")
  132. compile(train_net, x, b)
  133. def test_lars():
  134. class Net(nn.Cell):
  135. def __init__(self, strategy1, strategy2, weight):
  136. super().__init__()
  137. self.weight = Parameter(weight, "w1")
  138. self.matmul = P.MatMul(transpose_a=False, transpose_b=True).set_strategy(strategy1)
  139. self.relu = P.ReLU().set_strategy(strategy2)
  140. def construct(self, x):
  141. out = self.matmul(x, self.weight)
  142. out = self.relu(out)
  143. return out
  144. context.set_auto_parallel_context(device_num=4, global_rank=0)
  145. strategy1 = ((2, 1), (2, 1))
  146. strategy2 = ((4, 1), )
  147. strategy3 = ((4, 1), (4, 1))
  148. x = Tensor(np.ones([64, 32]), dtype=ms.float32)
  149. weight = Tensor(np.ones([64, 32]), dtype=ms.float32)
  150. b = Tensor(np.ones([64, 64]), dtype=ms.float32)
  151. net = Net(strategy1, strategy2, weight)
  152. lr = Tensor(np.ones([6]), dtype=ms.float32)
  153. SGD = Momentum(net.trainable_params(), lr, 0.9)
  154. optimizer = LARS(SGD, epsilon=1e-08, hyperpara=0.02, decay_filter=lambda x: 'bn' not in x.name,
  155. lars_filter=lambda x: 'bn' not in x.name)
  156. net_with_loss = NetWithLoss(net, strategy3)
  157. train_net = TrainOneStepCell(net_with_loss, optimizer)
  158. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel")
  159. compile(train_net, x, b)