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_control_flow.py 3.3 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. # Copyright 2021 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
  18. from mindspore import context
  19. from mindspore.train.model import Model
  20. from mindspore.common.initializer import initializer
  21. from mindspore.common.parameter import Parameter
  22. from mindspore.ops import operations as P
  23. class DatasetLenet():
  24. def __init__(self, data, label, length=3):
  25. self.data = data
  26. self.label = label
  27. self.index = 1
  28. self.length = length
  29. def __iter__(self):
  30. return self
  31. def __next__(self):
  32. if self.index >= self.length:
  33. raise StopIteration
  34. self.index += 1
  35. return self.data, self.label
  36. def reset(self):
  37. self.index = 0
  38. def get_dataset_size(self):
  39. return 32
  40. def get_repeat_count(self):
  41. return 1
  42. def get_batch_size(self):
  43. return 32
  44. def create_tuple_iterator(self, num_epochs=1, do_copy=True):
  45. return self
  46. class MatMulCell(nn.Cell):
  47. def __init__(self):
  48. super().__init__()
  49. self.matmul = P.MatMul()
  50. self.relu = P.ReLU()
  51. self.weight = Parameter(initializer("ones", [64, 64]), name="param1")
  52. def construct(self, x):
  53. out = self.matmul(x, self.weight)
  54. out = self.relu(out)
  55. return out
  56. class Net(nn.Cell):
  57. def __init__(self, strategy1, strategy2):
  58. super().__init__()
  59. self.matmul = P.MatMul().shard(strategy1)
  60. self.weight = Parameter(initializer("ones", [64, 64]), name="param")
  61. self.cell1 = MatMulCell()
  62. self.cell2 = MatMulCell()
  63. self.cell3 = MatMulCell()
  64. self.cell4 = MatMulCell()
  65. self.relu = P.ReLU().shard(strategy2)
  66. self.reduce = P.ReduceSum()
  67. def construct(self, x, y):
  68. out = self.matmul(x, self.weight)
  69. if self.reduce(y) == 1.0:
  70. out = self.cell1(out)
  71. elif self.reduce(y) == 2.0:
  72. out = self.cell2(out)
  73. elif self.reduce(y) == 3.0:
  74. out = self.cell3(out)
  75. else:
  76. out = self.cell4(out)
  77. out = self.relu(out)
  78. out = out + x
  79. return out
  80. def test_control_flow():
  81. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel")
  82. context.set_auto_parallel_context(device_num=8, global_rank=0)
  83. strategy1 = ((2, 4), (4, 1))
  84. strategy2 = ((8, 1),)
  85. net = Net(strategy1, strategy2)
  86. data = Tensor(np.ones([128, 64]), dtype=ms.float32)
  87. label = Tensor(np.ones([8, 8]), dtype=ms.float32)
  88. dataset = DatasetLenet(data, label, 3)
  89. opt = nn.Lamb(net.trainable_params(), learning_rate=0.01)
  90. model = Model(net, optimizer=opt)
  91. model.train(2, dataset, dataset_sink_mode=False)