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_layer_norm.py 4.3 kB

5 years ago
5 years ago
5 years ago
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. # ============================================================================
  15. import numpy as np
  16. import pytest
  17. import mindspore as ms
  18. from mindspore import context, Tensor, Parameter
  19. from mindspore.common.api import _executor
  20. from mindspore.common.initializer import initializer
  21. from mindspore.nn import Cell, TrainOneStepCell, Momentum
  22. from mindspore.ops import operations as P
  23. class Net(Cell):
  24. def __init__(self, mul_weight, strategy1=None, strategy2=None, strategy3=None):
  25. super().__init__()
  26. self.begin_norm_axis = 2
  27. self.begin_params_axis = 1
  28. self.mul = P.Mul().set_strategy(strategy1)
  29. self.layer_norm = P.LayerNorm(self.begin_norm_axis, self.begin_params_axis).set_strategy(strategy2)
  30. self.mul2 = P.Mul().set_strategy(strategy3)
  31. self.mul_weight = Parameter(mul_weight, "w1")
  32. self.normalized_shape = [64, 32, 16]
  33. self.gamma = Parameter(initializer('ones', self.normalized_shape), name="gamma")
  34. self.beta = Parameter(initializer('zeros', self.normalized_shape), name="beta")
  35. def construct(self, x, b):
  36. out = self.mul(x, self.mul_weight)
  37. out, _, _ = self.layer_norm(out, self.gamma, self.beta)
  38. out = self.mul2(out, b)
  39. return out
  40. _x = Tensor(np.ones([128, 64, 32, 16]), dtype=ms.float32)
  41. _w = Tensor(np.ones([128, 64, 32, 16]), dtype=ms.float32)
  42. _b = Tensor(np.ones([128, 64, 32, 16]), dtype=ms.float32)
  43. def compile_net(net):
  44. optimizer = Momentum(net.trainable_params(), learning_rate=0.1, momentum=0.9)
  45. train_net = TrainOneStepCell(net, optimizer)
  46. train_net.set_auto_parallel()
  47. _executor.compile(train_net, _x, _b)
  48. context.reset_auto_parallel_context()
  49. def test_layer_norm_data_parallel():
  50. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel", device_num=16, global_rank=0)
  51. strategy1 = ((16, 1, 1, 1), (16, 1, 1, 1))
  52. strategy2 = ((16, 1, 1, 1), (1, 1, 1), (1, 1, 1))
  53. strategy3 = ((16, 1, 1, 1), (16, 1, 1, 1))
  54. net = Net(_w, strategy1, strategy2, strategy3)
  55. compile_net(net)
  56. def test_layer_norm_model_parallel():
  57. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel", device_num=16, global_rank=0)
  58. strategy1 = ((1, 16, 1, 1), (1, 16, 1, 1))
  59. strategy2 = ((1, 16, 1, 1), (16, 1, 1), (16, 1, 1))
  60. strategy3 = ((1, 16, 1, 1), (1, 16, 1, 1))
  61. net = Net(_w, strategy1, strategy2, strategy3)
  62. compile_net(net)
  63. def test_layer_norm_hybrid_parallel():
  64. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel", device_num=16, global_rank=0)
  65. strategy1 = ((2, 8, 1, 1), (2, 8, 1, 1))
  66. strategy2 = ((2, 8, 1, 1), (8, 1, 1), (8, 1, 1))
  67. strategy3 = ((2, 8, 1, 1), (2, 8, 1, 1))
  68. net = Net(_w, strategy1, strategy2, strategy3)
  69. compile_net(net)
  70. def test_layer_norm_auto_parallel():
  71. context.set_auto_parallel_context(parallel_mode="auto_parallel", device_num=16, global_rank=0)
  72. net = Net(_w)
  73. compile_net(net)
  74. def test_layer_norm_repeat_calc():
  75. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel", device_num=16, global_rank=0)
  76. strategy1 = ((2, 2, 4, 1), (2, 2, 4, 1))
  77. strategy2 = ((2, 2, 1, 1), (2, 1, 1), (2, 1, 1))
  78. strategy3 = ((2, 2, 4, 1), (2, 2, 4, 1))
  79. net = Net(_w, strategy1, strategy2, strategy3)
  80. compile_net(net)
  81. def test_layer_norm_wrong_strategy():
  82. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel", device_num=16, global_rank=0)
  83. strategy1 = ((2, 2, 4, 1), (2, 2, 4, 1))
  84. strategy2 = ((1, 2, 1, 2), (2, 1, 2), (2, 1, 2))
  85. strategy3 = ((2, 2, 4, 1), (2, 2, 4, 1))
  86. net = Net(_w, strategy1, strategy2, strategy3)
  87. with pytest.raises(RuntimeError):
  88. compile_net(net)