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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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 compile(net, x, b):
  32. net.set_auto_parallel()
  33. _Executor().compile(net, x, b)
  34. def test_optimizer_clone_weight():
  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).set_strategy(strategy1)
  40. self.relu = P.ReLU().set_strategy(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. strategy1 = ((2, 1), (2, 1))
  47. strategy2 = ((4, 1), )
  48. strategy3 = ((4, 1), (4, 1))
  49. x = Tensor(np.ones([64, 32]), dtype=ms.float32)
  50. weight = Tensor(np.ones([64, 32]), dtype=ms.float32)
  51. b = Tensor(np.ones([64, 64]), dtype=ms.float32)
  52. net = Net(strategy1, strategy2, weight)
  53. optimizer = AdamWeightDecay(net.trainable_params())
  54. net_with_loss = NetWithLoss(net, strategy3)
  55. train_net = TrainOneStepCell(net_with_loss, optimizer)
  56. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel")
  57. compile(train_net, x, b)
  58. def test_optimizer_clone_weight2():
  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).set_strategy(strategy1)
  64. self.relu = P.ReLU().set_strategy(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. strategy1 = ((2, 1), (2, 1))
  71. strategy2 = ((4, 1), )
  72. strategy3 = ((4, 1), (4, 1))
  73. x = Tensor(np.ones([64, 32]), dtype=ms.float32)
  74. weight = Tensor(np.ones([64, 32]), dtype=ms.float32)
  75. b = Tensor(np.ones([64, 64]), dtype=ms.float32)
  76. net = Net(strategy1, strategy2, weight)
  77. optimizer = AdamWeightDecay(net.trainable_params())
  78. net_with_loss = NetWithLoss(net, strategy3)
  79. train_net = TrainOneStepCell(net_with_loss, optimizer)
  80. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel")
  81. compile(train_net, x, b)