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_relu_op.py 4.1 kB

5 years ago
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. # Copyright 2019 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. from mindspore.ops.operations import _inner_ops as inner
  22. class NetRelu(nn.Cell):
  23. def __init__(self):
  24. super(NetRelu, self).__init__()
  25. self.relu = P.ReLU()
  26. def construct(self, x):
  27. return self.relu(x)
  28. class NetReluDynamic(nn.Cell):
  29. def __init__(self):
  30. super(NetReluDynamic, self).__init__()
  31. self.conv = inner.GpuConvertToDynamicShape()
  32. self.relu = P.ReLU()
  33. def construct(self, x):
  34. x_conv = self.conv(x)
  35. return self.relu(x_conv)
  36. @pytest.mark.level0
  37. @pytest.mark.platform_x86_gpu_training
  38. @pytest.mark.env_onecard
  39. def test_relu_float32():
  40. x = Tensor(np.array([[[[-1, 1, 10],
  41. [1, -1, 1],
  42. [10, 1, -1]]]]).astype(np.float32))
  43. expect = np.array([[[[0, 1, 10,],
  44. [1, 0, 1,],
  45. [10, 1, 0.]]]]).astype(np.float32)
  46. context.set_context(mode=context.PYNATIVE_MODE, device_target="GPU")
  47. relu = NetRelu()
  48. output = relu(x)
  49. assert (output.asnumpy() == expect).all()
  50. context.set_context(mode=context.GRAPH_MODE, device_target="GPU")
  51. relu = NetRelu()
  52. output = relu(x)
  53. assert (output.asnumpy() == expect).all()
  54. @pytest.mark.level0
  55. @pytest.mark.platform_x86_gpu_training
  56. @pytest.mark.env_onecard
  57. def test_relu_int32():
  58. x = Tensor(np.array([[[[-1, 1, 10],
  59. [1, -1, 1],
  60. [10, 1, -1]]]]).astype(np.int32))
  61. expect = np.array([[[[0, 1, 10,],
  62. [1, 0, 1,],
  63. [10, 1, 0.]]]]).astype(np.int32)
  64. context.set_context(mode=context.PYNATIVE_MODE, device_target="GPU")
  65. relu = NetRelu()
  66. output = relu(x)
  67. assert (output.asnumpy() == expect).all()
  68. context.set_context(mode=context.GRAPH_MODE, device_target="GPU")
  69. relu = NetRelu()
  70. output = relu(x)
  71. assert (output.asnumpy() == expect).all()
  72. @pytest.mark.level0
  73. @pytest.mark.platform_x86_gpu_training
  74. @pytest.mark.env_onecard
  75. def test_relu_int64():
  76. x = Tensor(np.array([[[[-1, 1, 10],
  77. [1, -1, 1],
  78. [10, 1, -1]]]]).astype(np.int64))
  79. expect = np.array([[[[0, 1, 10,],
  80. [1, 0, 1,],
  81. [10, 1, 0.]]]]).astype(np.int64)
  82. context.set_context(mode=context.PYNATIVE_MODE, device_target="GPU")
  83. relu = NetRelu()
  84. output = relu(x)
  85. print(output.asnumpy(), expect)
  86. assert (output.asnumpy() == expect).all()
  87. context.set_context(mode=context.GRAPH_MODE, device_target="GPU")
  88. relu = NetRelu()
  89. output = relu(x)
  90. assert (output.asnumpy() == expect).all()
  91. @pytest.mark.level0
  92. @pytest.mark.platform_x86_gpu_training
  93. @pytest.mark.env_onecard
  94. def test_relu_int64_dynamic_shape():
  95. x = Tensor(np.array([[[[-1, 1, 10],
  96. [1, -1, 1],
  97. [10, 1, -1]]]]).astype(np.int64))
  98. expect = np.array([[[[0, 1, 10,],
  99. [1, 0, 1,],
  100. [10, 1, 0.]]]]).astype(np.int64)
  101. context.set_context(mode=context.GRAPH_MODE, device_target="GPU")
  102. relu_dynamic = NetReluDynamic()
  103. output = relu_dynamic(x)
  104. assert (output.asnumpy() == expect).all()