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_layernorm.py 2.7 kB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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.context as context
  18. from mindspore import Tensor
  19. from mindspore.nn import Cell
  20. import mindspore.ops.operations as P
  21. context.set_context(mode=context.GRAPH_MODE, enable_graph_kernel=True, device_target="GPU")
  22. class Net(Cell):
  23. def __init__(self):
  24. super(Net, self).__init__()
  25. self.layernorm = P.LayerNorm(1, 1)
  26. def construct(self, x, y, z):
  27. return self.layernorm(x, y, z)
  28. @pytest.mark.level0
  29. @pytest.mark.platform_x86_gpu_training
  30. @pytest.mark.env_onecard
  31. def test_basic():
  32. input_x = np.random.normal(0, 1, [2, 3, 4, 3]).astype(np.float32)
  33. gamma = np.random.normal(0, 1, [3, 4, 3]).astype(np.float32)
  34. beta = np.random.normal(0, 1, [3, 4, 3]).astype(np.float32)
  35. shape_x = [2, 3, 4, 3]
  36. begin_norm_axis = 1
  37. in_rank = len(shape_x)
  38. if begin_norm_axis < 0:
  39. norm_axis = begin_norm_axis + in_rank
  40. else:
  41. norm_axis = begin_norm_axis
  42. norm_axes = tuple(range(norm_axis, in_rank))
  43. mean = np.mean(input_x, axis=norm_axes, keepdims=True)
  44. mean_b = np.broadcast_to(mean, shape_x)
  45. diff = input_x - mean_b
  46. square = np.square(diff)
  47. smean = np.mean(square, axis=norm_axes, keepdims=True)
  48. smean_b = np.broadcast_to(smean, shape_x)
  49. meps = smean_b + 1e-5
  50. logs = np.log(meps)
  51. mul = logs * (-0.5)
  52. rsqrt = np.exp(mul)
  53. out = diff * rsqrt
  54. bn = out * gamma + beta
  55. expect = (bn, mean, smean)
  56. net = Net()
  57. net_result = net(Tensor(input_x), Tensor(gamma), Tensor(beta))
  58. if isinstance(net_result, tuple) and len(net_result) == 3:
  59. result = (net_result[0].asnumpy(), net_result[1].asnumpy(), net_result[2].asnumpy())
  60. res0 = np.allclose(expect[0], result[0], rtol=1.e-4, atol=1.e-4, equal_nan=True)
  61. assert res0
  62. res1 = np.allclose(expect[1], result[1], rtol=1.e-4, atol=1.e-7, equal_nan=True)
  63. assert res1
  64. res2 = np.allclose(expect[2], result[2], rtol=1.e-4, atol=1.e-7, equal_nan=True)
  65. assert res2
  66. else:
  67. assert False