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_parameter_multi_users.py 3.7 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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. import numpy as np
  15. import pytest
  16. import mindspore as ms
  17. from mindspore import context, Tensor, Parameter
  18. from mindspore.common.api import _cell_graph_executor
  19. from mindspore.nn import Cell, TrainOneStepCell, Momentum
  20. from mindspore.ops import operations as P
  21. class Net(Cell):
  22. def __init__(self, mul_weight, strategy1=None, strategy2=None):
  23. super().__init__()
  24. self.mul = P.Mul().shard(strategy1)
  25. self.mul2 = P.Mul().shard(strategy2)
  26. self.mul_weight = Parameter(mul_weight, "w1")
  27. def construct(self, x, b):
  28. out = self.mul(x, self.mul_weight)
  29. out = self.mul2(out, self.mul_weight)
  30. return out
  31. class Net2(Cell):
  32. def __init__(self, mul_weight, strategy1=None, strategy2=None):
  33. super().__init__()
  34. self.mul = P.Mul().shard(strategy1)
  35. self.mul2 = P.Mul().shard(strategy2)
  36. self.mul_weight = Parameter(mul_weight, "w1")
  37. def construct(self, x, b):
  38. out = self.mul(x, self.mul_weight)
  39. out = self.mul2(x, out)
  40. return out
  41. class Net3(Cell):
  42. def __init__(self, mul_weight, strategy1=None, strategy2=None):
  43. super().__init__()
  44. self.mul = P.MatMul().shard(strategy1)
  45. self.mul2 = P.MatMul().shard(strategy2)
  46. self.mul_weight = Parameter(mul_weight, "w1")
  47. def construct(self, x, b):
  48. out = self.mul(x, self.mul_weight)
  49. out = self.mul2(out, self.mul_weight)
  50. return out
  51. _x = Tensor(np.ones([16, 16]), dtype=ms.float32)
  52. _w = Tensor(np.ones([16, 16]), dtype=ms.float32)
  53. _b = Tensor(np.ones([16, 16]), dtype=ms.float32)
  54. def compile_net(net):
  55. optimizer = Momentum(net.trainable_params(), learning_rate=0.1, momentum=0.9)
  56. train_net = TrainOneStepCell(net, optimizer)
  57. train_net.set_auto_parallel()
  58. train_net.set_train()
  59. _cell_graph_executor.compile(train_net, _x, _b)
  60. context.reset_auto_parallel_context()
  61. def test_parameter_same_split():
  62. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel", device_num=16, global_rank=0)
  63. strategy1 = ((16, 1), (16, 1))
  64. strategy2 = ((16, 1), (16, 1))
  65. net = Net(_w, strategy1, strategy2)
  66. compile_net(net)
  67. def test_parameter_different_split():
  68. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel", device_num=16, global_rank=0)
  69. strategy1 = ((16, 1), (16, 1))
  70. strategy2 = ((4, 4), (4, 4))
  71. net = Net(_w, strategy1, strategy2)
  72. with pytest.raises(RuntimeError):
  73. compile_net(net)
  74. def test_input_same_split():
  75. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel", device_num=16, global_rank=0)
  76. strategy1 = ((16, 1), (16, 1))
  77. strategy2 = ((16, 1), (16, 1))
  78. net = Net(_w, strategy1, strategy2)
  79. compile_net(net)
  80. def test_parameter_different_group():
  81. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel", device_num=16, global_rank=0)
  82. strategy1 = ((1, 2), (2, 1))
  83. strategy2 = ((8, 2), (2, 1))
  84. net = Net3(_w, strategy1, strategy2)
  85. with pytest.raises(RuntimeError):
  86. compile_net(net)