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_two_weights_parameter.py 2.9 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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, ParameterTuple
  19. import mindspore as ms
  20. from mindspore.common.api import _executor
  21. from mindspore.ops import composite as C
  22. from mindspore.ops import functional as F
  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. class OneStepCell(nn.Cell):
  32. def __init__(self, network):
  33. super(OneStepCell, self).__init__(auto_prefix=False)
  34. self.network = network
  35. self.weights = ParameterTuple(network.network.trainable_params())
  36. def construct(self, data, label):
  37. weights = self.weights
  38. grads = C.grad_by_list(self.network, weights)(data, label)
  39. return grads
  40. def test_two_weights_parameter():
  41. class Net(nn.Cell):
  42. def __init__(self, strategy1, strategy2, weight, weight2):
  43. super().__init__()
  44. self.weight = Parameter(weight, "w1", requires_grad=True)
  45. self.weight2 = Parameter(weight2, "w2", requires_grad=True)
  46. self.matmul = P.MatMul().set_strategy(strategy1)
  47. self.matmul2 = P.MatMul().set_strategy(strategy2)
  48. def construct(self, x):
  49. out = self.matmul(x, self.weight)
  50. out = self.matmul2(out, self.weight2)
  51. return out
  52. context.set_auto_parallel_context(device_num=8, global_rank=0)
  53. strategy1 = ((4, 1), (1, 2))
  54. strategy2 = ((4, 2), (2, 1))
  55. strategy3 = ((8, 1), (8, 1))
  56. x = Tensor(np.ones([64, 32]), dtype=ms.float32)
  57. weight = Tensor(np.ones([32, 64]), dtype=ms.float32)
  58. weight2 = Tensor(np.ones([64, 64]), dtype=ms.float32)
  59. b = Tensor(np.ones([64, 64]), dtype=ms.float32)
  60. net = Net(strategy1, strategy2, weight, weight2)
  61. net_with_loss = NetWithLoss(net, strategy3)
  62. train_net = OneStepCell(net_with_loss)
  63. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel")
  64. _executor.compile(train_net, x, b)