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_select.py 3.6 kB

4 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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. # ============================================================================
  15. import numpy as np
  16. import pytest
  17. import mindspore as ms
  18. from mindspore import context, Tensor, Parameter
  19. from mindspore.nn import Cell, Momentum
  20. from mindspore.ops import operations as P
  21. from mindspore.train import Model
  22. from tests.dataset_mock import MindData
  23. class Dataset(MindData):
  24. def __init__(self, predict, label, length=3):
  25. super(Dataset, self).__init__(size=length)
  26. self.predict = predict
  27. self.label = label
  28. self.index = 0
  29. self.length = length
  30. def __iter__(self):
  31. return self
  32. def __next__(self):
  33. if self.index >= self.length:
  34. raise StopIteration
  35. self.index += 1
  36. return self.predict, self.label
  37. def reset(self):
  38. self.index = 0
  39. class Net(Cell):
  40. def __init__(self, w1, w2, strategy1=None, strategy2=None):
  41. super().__init__()
  42. self.less = P.Less().shard(strategy1)
  43. self.w1 = Parameter(w1, "w1")
  44. self.w2 = Parameter(w2, "w2")
  45. self.select = P.Select().shard(strategy2)
  46. def construct(self, x, b):
  47. out = self.less(x, b)
  48. out = self.select(out, self.w1, self.w2)
  49. return out
  50. _x = Tensor(np.ones([16, 64, 32]), dtype=ms.float32)
  51. _b = Tensor(np.ones([16, 64, 32]), dtype=ms.float32)
  52. _w1 = Tensor(np.ones([128, 64, 32]), dtype=ms.float32)
  53. _w2 = Tensor(np.ones([128, 64, 32]), dtype=ms.float32)
  54. def compile_net(net):
  55. context.set_context(save_graphs=True)
  56. learning_rate = 0.1
  57. momentum = 0.9
  58. epoch_size = 2
  59. dataset = Dataset(_x, _b)
  60. opt = Momentum(net.trainable_params(), learning_rate, momentum)
  61. model = Model(net, optimizer=opt)
  62. model.train(epoch_size, dataset, dataset_sink_mode=False)
  63. context.reset_auto_parallel_context()
  64. def test_select_data_parallel():
  65. context.set_auto_parallel_context(
  66. parallel_mode="semi_auto_parallel", device_num=8, global_rank=0)
  67. strategy1 = ((8, 1, 1), (8, 1, 1))
  68. strategy2 = ((8, 1, 1), (8, 1, 1), (8, 1, 1))
  69. net = Net(_w1, _w2, strategy1, strategy2)
  70. compile_net(net)
  71. def test_select_model_parallel():
  72. context.set_auto_parallel_context(
  73. parallel_mode="semi_auto_parallel", device_num=8, global_rank=0)
  74. strategy1 = ((2, 2, 2), (2, 2, 2))
  75. strategy2 = ((2, 2, 2), (2, 2, 2), (2, 2, 2))
  76. net = Net(_w1, _w2, strategy1, strategy2)
  77. compile_net(net)
  78. def test_select_auto_parallel():
  79. context.set_auto_parallel_context(
  80. parallel_mode="auto_parallel", device_num=8, global_rank=0)
  81. net = Net(_w1, _w2)
  82. compile_net(net)
  83. def test_select_strategy_error():
  84. context.set_auto_parallel_context(
  85. parallel_mode="semi_auto_parallel", device_num=8, global_rank=0)
  86. strategy1 = ((2, 2, 2), (2, 2, 2))
  87. strategy2 = ((8, 1, 1), (2, 2, 2), (2, 2, 2))
  88. net = Net(_w1, _w2, strategy1, strategy2)
  89. with pytest.raises(RuntimeError):
  90. compile_net(net)