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_dropout.py 3.2 kB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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. import numpy as np
  15. import mindspore as ms
  16. from mindspore import context, Tensor, Parameter
  17. from mindspore.common.api import _cell_graph_executor
  18. from mindspore.nn import Cell, TrainOneStepCell, Momentum
  19. from mindspore.ops import operations as P
  20. class Net(Cell):
  21. def __init__(self, mul_weight, strategy1=None, strategy2=None):
  22. super().__init__()
  23. self.mul = P.Mul().shard(strategy1)
  24. self.dropout1 = P.Dropout(keep_prob=0.5).shard(strategy2)
  25. self.relu = P.ReLU().shard(strategy2)
  26. self.dropout2 = P.Dropout(keep_prob=0.5).shard(strategy2)
  27. self.relu2 = P.ReLU().shard(strategy2)
  28. self.mul_weight = Parameter(mul_weight, "w1")
  29. def construct(self, x, b):
  30. out = self.mul(x, self.mul_weight)
  31. out, _ = self.dropout1(out)
  32. out = self.relu(out)
  33. out, _ = self.dropout2(out)
  34. out = self.relu2(out)
  35. return out
  36. _x = Tensor(np.ones([128, 64]), dtype=ms.float32)
  37. _w1 = Tensor(np.ones([128, 64]), dtype=ms.float32)
  38. _b = Tensor(np.ones([128, 64]), dtype=ms.float32)
  39. def compile_net(net):
  40. optimizer = Momentum(net.trainable_params(), learning_rate=0.1, momentum=0.9)
  41. train_net = TrainOneStepCell(net, optimizer)
  42. train_net.set_auto_parallel()
  43. train_net.set_train()
  44. _cell_graph_executor.compile(train_net, _x, _b)
  45. context.reset_auto_parallel_context()
  46. def test_dropout_data_parallel():
  47. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel", device_num=16, global_rank=0)
  48. strategy1 = ((16, 1), (16, 1))
  49. strategy2 = ((16, 1),)
  50. net = Net(_w1, strategy1, strategy2)
  51. compile_net(net)
  52. def test_dropout_model_parallel():
  53. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel", device_num=16, global_rank=0)
  54. strategy1 = ((1, 16), (1, 16))
  55. strategy2 = ((1, 16),)
  56. net = Net(_w1, strategy1, strategy2)
  57. compile_net(net)
  58. def test_dropout_mixed_parallel():
  59. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel", device_num=16, global_rank=0)
  60. strategy1 = ((4, 4), (4, 4))
  61. strategy2 = ((4, 4),)
  62. net = Net(_w1, strategy1, strategy2)
  63. compile_net(net)
  64. def test_dropout_auto_parallel():
  65. context.set_auto_parallel_context(parallel_mode="auto_parallel", device_num=16, global_rank=0)
  66. net = Net(_w1)
  67. compile_net(net)
  68. def test_dropout_repeat_calc():
  69. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel", device_num=16, global_rank=0)
  70. strategy1 = ((4, 4), (4, 4))
  71. strategy2 = ((2, 4),)
  72. net = Net(_w1, strategy1, strategy2)
  73. compile_net(net)