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_gelu_op.py 2.4 kB

5 years ago
5 years ago
5 years ago
5 years ago
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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 pytest
  16. from mindspore import Tensor
  17. from mindspore.ops import operations as P
  18. import mindspore.nn as nn
  19. import numpy as np
  20. import mindspore.context as context
  21. context.set_context(mode=context.GRAPH_MODE, device_target="GPU")
  22. class GeluNet(nn.Cell):
  23. def __init__(self):
  24. super(GeluNet, self).__init__()
  25. self.gelu = P.Gelu()
  26. def construct(self, x):
  27. return self.gelu(x)
  28. def GeluCompute(x):
  29. return 0.5 * x * (1.0 + np.tanh(np.sqrt(2 / np.pi) * (x + 0.044715 * x * x * x)))
  30. @pytest.mark.level0
  31. @pytest.mark.platform_x86_gpu_training
  32. @pytest.mark.env_onecard
  33. def test_gelu_1d():
  34. x_np = np.random.random((50,)).astype(np.float32)
  35. y_np = GeluCompute(x_np)
  36. x_ms = Tensor(x_np)
  37. net = GeluNet()
  38. y_ms = net(x_ms)
  39. assert np.allclose(y_np, y_ms.asnumpy())
  40. @pytest.mark.level0
  41. @pytest.mark.platform_x86_gpu_training
  42. @pytest.mark.env_onecard
  43. def test_gelu_2d():
  44. x_np = np.random.random((50, 40)).astype(np.float32)
  45. y_np = GeluCompute(x_np)
  46. x_ms = Tensor(x_np)
  47. net = GeluNet()
  48. y_ms = net(x_ms)
  49. assert np.allclose(y_np, y_ms.asnumpy())
  50. @pytest.mark.level0
  51. @pytest.mark.platform_x86_gpu_training
  52. @pytest.mark.env_onecard
  53. def test_gelu_4d():
  54. x_np = np.random.random((32, 3, 224, 224)).astype(np.float32)
  55. y_np = GeluCompute(x_np)
  56. x_ms = Tensor(x_np)
  57. net = GeluNet()
  58. y_ms = net(x_ms)
  59. assert np.allclose(y_np, y_ms.asnumpy())
  60. @pytest.mark.level0
  61. @pytest.mark.platform_x86_gpu_training
  62. @pytest.mark.env_onecard
  63. def test_gelu_neg():
  64. x_np = np.random.random((32, 3, 224, 224)).astype(np.float32) * -1
  65. y_np = GeluCompute(x_np)
  66. x_ms = Tensor(x_np)
  67. net = GeluNet()
  68. y_ms = net(x_ms)
  69. assert np.allclose(y_np, y_ms.asnumpy())