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_optimizer_clone_weight.py 3.5 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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.nn.optim import AdamWeightDecay
  22. from mindspore.nn import TrainOneStepCell
  23. class NetWithLoss(nn.Cell):
  24. def __init__(self, network, strategy3):
  25. super(NetWithLoss, self).__init__()
  26. self.loss = P.SoftmaxCrossEntropyWithLogits().set_strategy(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 test_optimizer_clone_weight():
  32. class Net(nn.Cell):
  33. def __init__(self, strategy1, strategy2, weight):
  34. super().__init__()
  35. self.weight = Parameter(weight, "w1")
  36. self.matmul = P.MatMul(transpose_a=False, transpose_b=True).set_strategy(strategy1)
  37. self.relu = P.ReLU().set_strategy(strategy2)
  38. def construct(self, x):
  39. out = self.matmul(x, self.weight)
  40. out = self.relu(out)
  41. return out
  42. context.set_auto_parallel_context(device_num=4, global_rank=0)
  43. strategy1 = ((2, 1), (2, 1))
  44. strategy2 = ((4, 1), )
  45. strategy3 = ((4, 1), (4, 1))
  46. x = Tensor(np.ones([64, 32]), dtype=ms.float32)
  47. weight = Tensor(np.ones([64, 32]), dtype=ms.float32)
  48. b = Tensor(np.ones([64, 64]), dtype=ms.float32)
  49. net = Net(strategy1, strategy2, weight)
  50. optimizer = AdamWeightDecay(net.trainable_params())
  51. net_with_loss = NetWithLoss(net, strategy3)
  52. train_net = TrainOneStepCell(net_with_loss, optimizer)
  53. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel")
  54. _Executor().compile(train_net, x, b)
  55. def test_optimizer_clone_weight2():
  56. class Net(nn.Cell):
  57. def __init__(self, strategy1, strategy2, weight):
  58. super().__init__()
  59. self.weight = Parameter(weight, "w1")
  60. self.matmul = P.MatMul(transpose_a=False, transpose_b=True).set_strategy(strategy1)
  61. self.relu = P.ReLU().set_strategy(strategy2)
  62. def construct(self, x):
  63. out = self.matmul(x, self.weight)
  64. out = self.relu(out)
  65. return out
  66. context.set_auto_parallel_context(device_num=4, global_rank=0)
  67. strategy1 = ((2, 1), (2, 1))
  68. strategy2 = ((4, 1), )
  69. strategy3 = ((4, 1), (4, 1))
  70. x = Tensor(np.ones([64, 32]), dtype=ms.float32)
  71. weight = Tensor(np.ones([64, 32]), dtype=ms.float32)
  72. b = Tensor(np.ones([64, 64]), dtype=ms.float32)
  73. net = Net(strategy1, strategy2, weight)
  74. optimizer = AdamWeightDecay(net.trainable_params())
  75. net_with_loss = NetWithLoss(net, strategy3)
  76. train_net = TrainOneStepCell(net_with_loss, optimizer)
  77. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel")
  78. _Executor().compile(train_net, x, b)