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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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. learning_rate = 0.1
  87. momentum = 0.9
  88. epoch_size = 2
  89. dataset = Dataset(_x, _b)
  90. opt = Momentum(net.trainable_params(), learning_rate, momentum)
  91. model = Model(net, optimizer=opt, amp_level="O2")
  92. model.train(epoch_size, dataset, dataset_sink_mode=False)
  93. context.reset_auto_parallel_context()
  94. def test_concat_parameter():
  95. context.set_auto_parallel_context(
  96. parallel_mode="semi_auto_parallel", device_num=8, global_rank=0)
  97. strategy1 = ((1, 4, 2), (1, 4, 2))
  98. strategy2 = ((1, 4, 2), (1, 4, 2))
  99. net = Net(_w1, _w2, strategy1, strategy2, is_parameter=True)
  100. compile_net(net)
  101. def test_concat_parameter_no_full_split():
  102. context.set_auto_parallel_context(
  103. parallel_mode="semi_auto_parallel", device_num=8, global_rank=0)
  104. strategy1 = ((1, 2, 2), (1, 2, 2))
  105. strategy2 = ((1, 4, 2), (1, 4, 2))
  106. net = Net(_w1, _w2, strategy1, strategy2, is_parameter=True)
  107. compile_net(net)
  108. def test_concat_tensor_and_parameter():
  109. context.set_auto_parallel_context(
  110. parallel_mode="semi_auto_parallel", device_num=8, global_rank=0)
  111. strategy1 = ((1, 2, 2), (1, 2, 2))
  112. strategy2 = ((1, 4, 2), (1, 4, 2))
  113. net = Net(_w1, _w2, strategy1, strategy2, is_parameter=False)
  114. compile_net(net)
  115. def test_concat_output():
  116. context.set_auto_parallel_context(
  117. parallel_mode="semi_auto_parallel", device_num=8, global_rank=0)
  118. strategy1 = ((2, 2, 2), (2, 2, 2))
  119. strategy2 = ((1, 4, 2), (1, 4, 2))
  120. net = Net2(_w1, strategy1, strategy2)
  121. compile_net(net)
  122. def test_concat_output_no_full_split():
  123. context.set_auto_parallel_context(
  124. parallel_mode="semi_auto_parallel", device_num=8, global_rank=0)
  125. strategy1 = ((2, 2, 2), (2, 2, 2))
  126. strategy2 = ((1, 2, 2), (1, 2, 2))
  127. net = Net2(_w1, strategy1, strategy2)
  128. compile_net(net)
  129. def test_concat_no_strategy():
  130. context.set_auto_parallel_context(
  131. parallel_mode="semi_auto_parallel", device_num=8, global_rank=0)
  132. strategy1 = ((2, 2, 2), (2, 2, 2))
  133. strategy2 = None
  134. net = Net2(_w3, strategy1, strategy2, axis=1)
  135. compile_net(net)
  136. def test_concat_auto_parallel():
  137. context.set_auto_parallel_context(
  138. parallel_mode="auto_parallel", device_num=8, global_rank=0)
  139. net = Net2(_w2)
  140. compile_net(net)
  141. def test_concat_auto_parallel2():
  142. context.set_auto_parallel_context(
  143. parallel_mode="auto_parallel", device_num=8, global_rank=0)
  144. strategy1 = None
  145. strategy2 = None
  146. net = Net2(_w3, strategy1, strategy2, axis=1)
  147. compile_net(net)
  148. def test_concat_auto_parallel_3_tensor():
  149. context.set_auto_parallel_context(
  150. parallel_mode="auto_parallel", device_num=8, global_rank=0)
  151. net = Net3(w1, w2, w3)
  152. compile_net(net)