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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. # Copyright 2020 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.context as context
  17. from mindspore.common.api import _executor
  18. from mindspore import Tensor, Parameter
  19. import mindspore.nn as nn
  20. from mindspore.nn import Cell, TrainOneStepCell, Momentum
  21. from mindspore.ops import operations as P
  22. class TwoInputBpropOperator(Cell):
  23. def __init__(self):
  24. super().__init__()
  25. self.op = P.Mul()
  26. self.bp = P.Add()
  27. def construct(self, x, y):
  28. return self.op(x, y)
  29. def bprop(self, x, y, out, dout):
  30. return self.bp(5, x), self.bp(y, 8)
  31. class ParallelFloorDivBpropNet(Cell):
  32. def __init__(self, mul_size, test_size, strategy=None, strategy2=None):
  33. super().__init__()
  34. mul_np = np.full(mul_size, 0.5, dtype=np.float32)
  35. floordiv_np = np.full(test_size, 0.1, dtype=np.float32)
  36. self.mul_weight = Parameter(Tensor(mul_np), name="mul_weight")
  37. self.floordiv_weight = Parameter(Tensor(floordiv_np), name="floordiv_weight")
  38. self.mul = TwoInputBpropOperator()
  39. self.floor_div = P.FloorDiv()
  40. self.bn = nn.BatchNorm1d(num_features=96)
  41. if strategy is not None:
  42. self.mul.op.shard(strategy2)
  43. self.mul.bp.shard(strategy2)
  44. self.floor_div.shard(strategy)
  45. def construct(self, inputs, label):
  46. x = self.mul(inputs, self.mul_weight)
  47. x = self.floor_div(x, self.floordiv_weight)
  48. x = self.bn(x)
  49. return x
  50. inputs_ = Tensor(np.random.randn(128, 96).astype(np.float32), dtype=ms.float32)
  51. label_ = Tensor(np.random.randn(128, 96).astype(np.float32), dtype=ms.float32)
  52. def compile_net(net):
  53. optimizer = Momentum(net.trainable_params(), learning_rate=0.1, momentum=0.9)
  54. train_net = TrainOneStepCell(net, optimizer)
  55. train_net.set_auto_parallel()
  56. train_net.set_train()
  57. _executor.compile(train_net, inputs_, label_)
  58. context.reset_auto_parallel_context()
  59. def test_net():
  60. context.set_context(mode=context.GRAPH_MODE, device_target="Ascend")
  61. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel", device_num=4, global_rank=0)
  62. strategy = ((4, 1), (4, 1))
  63. net = ParallelFloorDivBpropNet(mul_size=(128, 96), test_size=(128, 96), strategy=strategy, strategy2=strategy)
  64. compile_net(net)