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_do_mask.py 3.4 kB

5 years ago
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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 mindspore as ms
  16. from mindspore import context, Tensor, Parameter
  17. from mindspore.common.api import _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.mul2 = P.Mul().shard(strategy1)
  25. self.dropout_do_mask = P.DropoutDoMask().shard(strategy2)
  26. self.dropout_gen_mask = P.DropoutGenMask()
  27. self.get_shape = P.Shape()
  28. self.cast = P.Cast()
  29. self.mul_weight = Parameter(mul_weight, "w1")
  30. self.mul_weight2 = Parameter(mul_weight, "w2")
  31. self.keep_prob = Tensor(0.9)
  32. def construct(self, x, b):
  33. out = self.mul(x, self.mul_weight)
  34. shape = self.get_shape(out)
  35. dtype = P.DType()(out)
  36. keep_prob = self.cast(self.keep_prob, dtype)
  37. mask = self.dropout_gen_mask(shape, keep_prob)
  38. out = self.dropout_do_mask(out, mask, keep_prob)
  39. out = self.mul2(out, self.mul_weight2)
  40. return out
  41. _x = Tensor(np.ones([128, 64]), dtype=ms.float32)
  42. _w1 = Tensor(np.ones([128, 64]), dtype=ms.float32)
  43. _b = Tensor(np.ones([128, 64]), 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. _executor.compile(train_net, _x, _b)
  49. context.reset_auto_parallel_context()
  50. def test_dropout_do_mask_data_parallel():
  51. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel", device_num=16, global_rank=0)
  52. strategy1 = ((16, 1), (16, 1))
  53. strategy2 = ((16, 1),)
  54. net = Net(_w1, strategy1, strategy2)
  55. compile_net(net)
  56. def test_dropout_do_mask_model_parallel():
  57. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel", device_num=16, global_rank=0)
  58. strategy1 = ((1, 16), (1, 16))
  59. strategy2 = ((1, 16),)
  60. net = Net(_w1, strategy1, strategy2)
  61. compile_net(net)
  62. def test_dropout_do_mask_hybrid_parallel():
  63. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel", device_num=16, global_rank=0)
  64. strategy1 = ((4, 4), (4, 4))
  65. strategy2 = ((4, 4),)
  66. net = Net(_w1, strategy1, strategy2)
  67. compile_net(net)
  68. def test_dropout_do_mask_auto_parallel():
  69. context.set_auto_parallel_context(parallel_mode="auto_parallel", device_num=16, global_rank=0)
  70. net = Net(_w1)
  71. compile_net(net)
  72. def test_dropout_do_mask_repeat_calc():
  73. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel", device_num=16, global_rank=0)
  74. strategy1 = ((4, 4), (4, 4))
  75. strategy2 = ((2, 4),)
  76. net = Net(_w1, strategy1, strategy2)
  77. compile_net(net)