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_pipeline_shared_parameter.py 4.4 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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. # ============================================================================
  15. import numpy as np
  16. import mindspore as ms
  17. import mindspore.nn as nn
  18. from mindspore import context
  19. from mindspore import Tensor
  20. from mindspore.ops import operations as P
  21. from mindspore.common.parameter import Parameter
  22. from mindspore.common.initializer import initializer
  23. from mindspore.train.model import Model
  24. from mindspore.nn.wrap.cell_wrapper import PipelineCell
  25. class DatasetLenet():
  26. def __init__(self, data, label, length=3):
  27. self.data = data
  28. self.label = label
  29. self.index = 1
  30. self.length = length
  31. def __iter__(self):
  32. return self
  33. def __next__(self):
  34. if self.index >= self.length:
  35. raise StopIteration
  36. self.index += 1
  37. return self.data, self.label
  38. def reset(self):
  39. self.index = 0
  40. def get_dataset_size(self):
  41. return 32
  42. def get_repeat_count(self):
  43. return 1
  44. def get_batch_size(self):
  45. return 32
  46. def create_tuple_iterator(self, num_epochs=1, do_copy=True):
  47. return self
  48. class MatMulCell(nn.Cell):
  49. def __init__(self, strategy1, strategy2):
  50. super().__init__()
  51. self.param = Parameter(initializer("zeros", [64, 64]), name="param")
  52. self.param1 = Parameter(initializer("zeros", [64, 64]), name="param1")
  53. self.matmul = P.MatMul().shard(strategy1)
  54. self.matmul1 = P.MatMul().shard(strategy2)
  55. def construct(self, x):
  56. out = self.matmul(x, self.param)
  57. out = self.matmul1(out, self.param1)
  58. return out, self.param
  59. class MatMulCell2(nn.Cell):
  60. def __init__(self, strategy1, strategy2):
  61. super().__init__()
  62. self.param1 = Parameter(initializer("zeros", [64, 64]), name="param1")
  63. self.matmul = P.MatMul().shard(strategy1)
  64. self.matmul1 = P.MatMul().shard(strategy2)
  65. def construct(self, x, param):
  66. out = self.matmul(x, param)
  67. out = self.matmul1(out, self.param1)
  68. return out
  69. class Net(nn.Cell):
  70. def __init__(self, strategy1, strategy2, param=None):
  71. super().__init__()
  72. self.cell1 = MatMulCell(strategy1, strategy2)
  73. self.cell1.pipeline_stage = 0
  74. self.cell2 = MatMulCell2(strategy1, strategy2)
  75. self.cell2.pipeline_stage = 1
  76. def construct(self, x, label):
  77. out, param = self.cell1(x)
  78. out = self.cell2(out, param)
  79. return out
  80. def test_pipeline_split_stage0():
  81. context.set_auto_parallel_context(device_num=8, global_rank=0, pipeline_stages=2)
  82. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel")
  83. data = Tensor(np.ones([32, 64]), dtype=ms.float32)
  84. label = Tensor(np.ones([64, 64]), dtype=ms.float32)
  85. strategy1 = ((4, 1), (1, 1))
  86. strategy2 = ((2, 1), (1, 1))
  87. net = PipelineCell(Net(strategy1, strategy2), 4)
  88. params = net.network.cell1.trainable_params()
  89. dataset = DatasetLenet(data, label, 3)
  90. optimizer = nn.Lamb(params, learning_rate=0.01)
  91. model = Model(net, optimizer=optimizer)
  92. model.train(2, dataset, dataset_sink_mode=False)
  93. def test_pipeline_split_stage1():
  94. context.set_auto_parallel_context(device_num=8, global_rank=4, pipeline_stages=2)
  95. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel")
  96. data = Tensor(np.ones([32, 64]), dtype=ms.float32)
  97. label = Tensor(np.ones([64, 64]), dtype=ms.float32)
  98. strategy1 = ((4, 1), (1, 1))
  99. strategy2 = ((2, 1), (1, 1))
  100. net = PipelineCell(Net(strategy1, strategy2), 4)
  101. params = net.network.cell2.trainable_params()
  102. dataset = DatasetLenet(data, label, 3)
  103. optimizer = nn.Lamb(params, learning_rate=0.01)
  104. model = Model(net, optimizer=optimizer)
  105. model.train(2, dataset, dataset_sink_mode=False)