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.3 kB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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 _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. _x = Tensor(np.ones([128, 64, 32]), dtype=ms.float32)
  42. _w = Tensor(np.ones([128, 64, 32]), dtype=ms.float32)
  43. _b = Tensor(np.ones([128, 64, 32]), dtype=ms.float32)
  44. def compile_net(net):
  45. optimizer = Momentum(net.trainable_params(), learning_rate=0.1, momentum=0.9)
  46. train_net = TrainOneStepCell(net, optimizer)
  47. train_net.set_auto_parallel()
  48. train_net.set_train()
  49. _executor.compile(train_net, _x, _b)
  50. context.reset_auto_parallel_context()
  51. def test_parameter_same_split():
  52. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel", device_num=16, global_rank=0)
  53. strategy1 = ((16, 1, 1), (16, 1, 1))
  54. strategy2 = ((16, 1, 1), (16, 1, 1))
  55. net = Net(_w, strategy1, strategy2)
  56. compile_net(net)
  57. def test_parameter_different_split():
  58. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel", device_num=16, global_rank=0)
  59. strategy1 = ((16, 1, 1), (16, 1, 1))
  60. strategy2 = ((4, 4, 1), (4, 4, 1))
  61. net = Net(_w, strategy1, strategy2)
  62. with pytest.raises(RuntimeError):
  63. compile_net(net)
  64. def test_input_same_split():
  65. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel", device_num=16, global_rank=0)
  66. strategy1 = ((16, 1, 1), (16, 1, 1))
  67. strategy2 = ((16, 1, 1), (16, 1, 1))
  68. net = Net(_w, strategy1, strategy2)
  69. compile_net(net)
  70. def test_input_different_split():
  71. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel", device_num=16, global_rank=0)
  72. strategy1 = ((16, 1, 1), (16, 1, 1))
  73. strategy2 = ((4, 4, 1), (4, 4, 1))
  74. net = Net2(_w, strategy1, strategy2)
  75. with pytest.raises(RuntimeError):
  76. compile_net(net)