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.8 kB

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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. import mindspore.nn as nn
  19. from mindspore import Tensor
  20. from mindspore.ops import operations as P
  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())
  70. @pytest.mark.level0
  71. @pytest.mark.platform_x86_gpu_training
  72. @pytest.mark.env_onecard
  73. def test_gelu_4d_fp16():
  74. x_np = np.random.random((32, 3, 224, 224)).astype(np.float16)
  75. y_np = GeluCompute(x_np)
  76. x_ms = Tensor(x_np)
  77. net = GeluNet()
  78. y_ms = net(x_ms)
  79. assert np.allclose(y_np, y_ms.asnumpy(), rtol=1e-3)