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_model_without_loss.py 6.2 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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. from mindspore import context, Tensor, Parameter
  18. from mindspore.nn import Cell, Momentum
  19. from mindspore.ops import operations as P
  20. from mindspore.train import Model
  21. from tests.dataset_mock import MindData
  22. class Dataset(MindData):
  23. def __init__(self, predict, label, length=3):
  24. super(Dataset, self).__init__(size=length)
  25. self.predict = predict
  26. self.label = label
  27. self.index = 0
  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.predict, self.label
  36. def reset(self):
  37. self.index = 0
  38. class Net(Cell):
  39. def __init__(self, weight, weight2, strategy1=None, strategy2=None, is_parameter=True):
  40. super().__init__()
  41. self.concat = P.Concat(axis=0).shard(strategy1)
  42. if is_parameter:
  43. self.weight = Parameter(weight, "w1")
  44. else:
  45. self.weight = weight
  46. self.mul = P.Mul().shard(strategy2)
  47. self.weight2 = Parameter(weight2, "w2")
  48. def construct(self, x, b):
  49. out = self.concat((self.weight, self.weight2))
  50. out = self.mul(x, out)
  51. return out
  52. class Net2(Cell):
  53. def __init__(self, weight, strategy1=None, strategy2=None, axis=0):
  54. super().__init__()
  55. self.mul = P.Mul().shard(strategy1)
  56. self.concat = P.Concat(axis=axis).shard(strategy2)
  57. self.weight = Parameter(weight, "w")
  58. def construct(self, x, b):
  59. out = self.mul(x, x)
  60. out = self.concat((out, self.weight))
  61. return out
  62. class Net3(Cell):
  63. def __init__(self, weight, weight2, weight3, strategy1=None, strategy2=None, is_parameter=True):
  64. super().__init__()
  65. self.concat = P.Concat(axis=0).shard(strategy1)
  66. if is_parameter:
  67. self.weight = Parameter(weight, "w1")
  68. else:
  69. self.weight = weight
  70. self.mul = P.Mul().shard(strategy2)
  71. self.weight2 = Parameter(weight2, "w2")
  72. self.weight3 = Parameter(weight3, "w3")
  73. def construct(self, x, b):
  74. out = self.concat((self.weight, self.weight2, self.weight3))
  75. out = self.mul(x, out)
  76. return out
  77. _x = Tensor(np.ones([16, 64, 32]), dtype=ms.float32)
  78. _b = Tensor(np.ones([16, 64, 32, 32]), dtype=ms.int32)
  79. _w1 = Tensor(np.ones([96, 64, 32]), dtype=ms.float32)
  80. _w2 = Tensor(np.ones([32, 64, 32]), dtype=ms.float32)
  81. _w3 = Tensor(np.ones([128, 16, 32]), dtype=ms.float32)
  82. w1 = Tensor(np.ones([48, 64, 32]), dtype=ms.float32)
  83. w2 = Tensor(np.ones([16, 64, 32]), dtype=ms.float32)
  84. w3 = Tensor(np.ones([64, 64, 32]), dtype=ms.float32)
  85. def compile_net(net):
  86. context.set_context(save_graphs=True)
  87. learning_rate = 0.1
  88. momentum = 0.9
  89. epoch_size = 2
  90. dataset = Dataset(_x, _b)
  91. opt = Momentum(net.trainable_params(), learning_rate, momentum)
  92. model = Model(net, optimizer=opt, amp_level="O2")
  93. model.train(epoch_size, dataset, dataset_sink_mode=False)
  94. context.reset_auto_parallel_context()
  95. def test_concat_parameter():
  96. context.set_auto_parallel_context(
  97. parallel_mode="semi_auto_parallel", device_num=8, global_rank=0)
  98. strategy1 = ((1, 4, 2), (1, 4, 2))
  99. strategy2 = ((1, 4, 2), (1, 4, 2))
  100. net = Net(_w1, _w2, strategy1, strategy2, is_parameter=True)
  101. compile_net(net)
  102. def test_concat_parameter_no_full_split():
  103. context.set_auto_parallel_context(
  104. parallel_mode="semi_auto_parallel", device_num=8, global_rank=0)
  105. strategy1 = ((1, 2, 2), (1, 2, 2))
  106. strategy2 = ((1, 4, 2), (1, 4, 2))
  107. net = Net(_w1, _w2, strategy1, strategy2, is_parameter=True)
  108. compile_net(net)
  109. def test_concat_tensor_and_parameter():
  110. context.set_auto_parallel_context(
  111. parallel_mode="semi_auto_parallel", device_num=8, global_rank=0)
  112. strategy1 = ((1, 2, 2), (1, 2, 2))
  113. strategy2 = ((1, 4, 2), (1, 4, 2))
  114. net = Net(_w1, _w2, strategy1, strategy2, is_parameter=False)
  115. compile_net(net)
  116. def test_concat_output():
  117. context.set_auto_parallel_context(
  118. parallel_mode="semi_auto_parallel", device_num=8, global_rank=0)
  119. strategy1 = ((2, 2, 2), (2, 2, 2))
  120. strategy2 = ((1, 4, 2), (1, 4, 2))
  121. net = Net2(_w1, strategy1, strategy2)
  122. compile_net(net)
  123. def test_concat_output_no_full_split():
  124. context.set_auto_parallel_context(
  125. parallel_mode="semi_auto_parallel", device_num=8, global_rank=0)
  126. strategy1 = ((2, 2, 2), (2, 2, 2))
  127. strategy2 = ((1, 2, 2), (1, 2, 2))
  128. net = Net2(_w1, strategy1, strategy2)
  129. compile_net(net)
  130. def test_concat_no_strategy():
  131. context.set_auto_parallel_context(
  132. parallel_mode="semi_auto_parallel", device_num=8, global_rank=0)
  133. strategy1 = ((2, 2, 2), (2, 2, 2))
  134. strategy2 = None
  135. net = Net2(_w3, strategy1, strategy2, axis=1)
  136. compile_net(net)
  137. def test_concat_auto_parallel():
  138. context.set_auto_parallel_context(
  139. parallel_mode="auto_parallel", device_num=8, global_rank=0)
  140. net = Net2(_w2)
  141. compile_net(net)
  142. def test_concat_auto_parallel2():
  143. context.set_auto_parallel_context(
  144. parallel_mode="auto_parallel", device_num=8, global_rank=0)
  145. strategy1 = None
  146. strategy2 = None
  147. net = Net2(_w3, strategy1, strategy2, axis=1)
  148. compile_net(net)
  149. def test_concat_auto_parallel_3_tensor():
  150. context.set_auto_parallel_context(
  151. parallel_mode="auto_parallel", device_num=8, global_rank=0)
  152. net = Net3(w1, w2, w3)
  153. compile_net(net)