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.3 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
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  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.nn as nn
  17. from mindspore import Tensor, Parameter
  18. from mindspore import context
  19. from mindspore.common.api import _executor
  20. from mindspore.nn import TrainOneStepCell
  21. from mindspore.nn.optim import Momentum, LARS
  22. from mindspore.ops import operations as P
  23. class NetWithLoss(nn.Cell):
  24. def __init__(self, network, strategy3):
  25. super(NetWithLoss, self).__init__()
  26. self.loss = P.SoftmaxCrossEntropyWithLogits().shard(strategy3)
  27. self.network = network
  28. def construct(self, x, b):
  29. predict = self.network(x)
  30. return self.loss(predict, b)[0]
  31. def compile_net(net, x, b):
  32. net.set_auto_parallel()
  33. _executor.compile(net, x, b)
  34. def test_momentum():
  35. class Net(nn.Cell):
  36. def __init__(self, strategy1, strategy2, weight):
  37. super().__init__()
  38. self.weight = Parameter(weight, "w1")
  39. self.matmul = P.MatMul(transpose_a=False, transpose_b=True).shard(strategy1)
  40. self.relu = P.ReLU().shard(strategy2)
  41. def construct(self, x):
  42. out = self.matmul(x, self.weight)
  43. out = self.relu(out)
  44. return out
  45. context.set_auto_parallel_context(device_num=4, global_rank=0)
  46. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel")
  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. compile_net(train_net, x, b)
  58. def test_momentum_with_loss_scale():
  59. class Net(nn.Cell):
  60. def __init__(self, strategy1, strategy2, weight):
  61. super().__init__()
  62. self.weight = Parameter(weight, "w1")
  63. self.matmul = P.MatMul(transpose_a=False, transpose_b=True).shard(strategy1)
  64. self.relu = P.ReLU().shard(strategy2)
  65. def construct(self, x):
  66. out = self.matmul(x, self.weight)
  67. out = self.relu(out)
  68. return out
  69. context.set_auto_parallel_context(device_num=4, global_rank=0)
  70. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel")
  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. compile_net(train_net, x, b)
  82. def test_momentum_with_dynamic_lr():
  83. class Net(nn.Cell):
  84. def __init__(self, strategy1, strategy2, weight):
  85. super().__init__()
  86. self.weight = Parameter(weight, "w1")
  87. self.matmul = P.MatMul(transpose_a=False, transpose_b=True).shard(strategy1)
  88. self.relu = P.ReLU().shard(strategy2)
  89. def construct(self, x):
  90. out = self.matmul(x, self.weight)
  91. out = self.relu(out)
  92. return out
  93. context.set_auto_parallel_context(device_num=4, global_rank=0)
  94. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel")
  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. compile_net(train_net, x, b)
  107. def test_momentum_with_loss_scale_and_dynamic_lr():
  108. class Net(nn.Cell):
  109. def __init__(self, strategy1, strategy2, weight):
  110. super().__init__()
  111. self.weight = Parameter(weight, "w1")
  112. self.matmul = P.MatMul(transpose_a=False, transpose_b=True).shard(strategy1)
  113. self.relu = P.ReLU().shard(strategy2)
  114. def construct(self, x):
  115. out = self.matmul(x, self.weight)
  116. out = self.relu(out)
  117. return out
  118. context.set_auto_parallel_context(device_num=4, global_rank=0)
  119. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel")
  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. compile_net(train_net, x, b)
  132. def test_lars():
  133. class Net(nn.Cell):
  134. def __init__(self, strategy1, strategy2, weight):
  135. super().__init__()
  136. self.weight = Parameter(weight, "w1")
  137. self.matmul = P.MatMul(transpose_a=False, transpose_b=True).shard(strategy1)
  138. self.relu = P.ReLU().shard(strategy2)
  139. def construct(self, x):
  140. out = self.matmul(x, self.weight)
  141. out = self.relu(out)
  142. return out
  143. context.set_auto_parallel_context(device_num=4, global_rank=0)
  144. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel")
  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, coefficient=0.02,
  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. compile_net(train_net, x, b)