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_relu6_op.py 2.9 kB

5 years ago
5 years ago
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. # Copyright 2020-2021 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 NetReLU6(nn.Cell):
  23. def __init__(self):
  24. super(NetReLU6, self).__init__()
  25. self.relu6 = P.ReLU6()
  26. def construct(self, x):
  27. return self.relu6(x)
  28. class NetRelu6Dynamic(nn.Cell):
  29. def __init__(self):
  30. super(NetRelu6Dynamic, self).__init__()
  31. self.test_dynamic = inner.GpuConvertToDynamicShape()
  32. self.relu6 = P.ReLU6()
  33. def construct(self, x):
  34. x = self.test_dynamic(x)
  35. return self.relu6(x)
  36. @pytest.mark.level0
  37. @pytest.mark.platform_x86_gpu_training
  38. @pytest.mark.env_onecard
  39. def test_relu6():
  40. x = Tensor(np.array([[[[-1, 1, 10],
  41. [5.9, 6.1, 6],
  42. [10, 1, -1]]]]).astype(np.float32))
  43. expect = np.array([[[[0, 1, 6,],
  44. [5.9, 6, 6,],
  45. [6, 1, 0.]]]]).astype(np.float32)
  46. context.set_context(mode=context.PYNATIVE_MODE, device_target="GPU")
  47. relu6 = NetReLU6()
  48. output = relu6(x)
  49. assert (output.asnumpy() == expect).all()
  50. context.set_context(mode=context.GRAPH_MODE, device_target="GPU")
  51. relu6 = NetReLU6()
  52. output = relu6(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_relu6_dynamic():
  58. x1 = Tensor(np.array([[-1.0, 4.0, -8.0], [2.0, -5.0, 9.0]]).astype(np.float32))
  59. expect1 = np.array([[0, 4, 0,],
  60. [2, 0, 6,]]).astype(np.float32)
  61. x2 = Tensor(np.array([[[[-1, 1, 10],
  62. [5.9, 6.1, 6],
  63. [10, 1, -1]]]]).astype(np.float32))
  64. expect2 = np.array([[[[0, 1, 6,],
  65. [5.9, 6, 6,],
  66. [6, 1, 0.]]]]).astype(np.float32)
  67. context.set_context(mode=context.GRAPH_MODE, device_target="GPU")
  68. relu6 = NetRelu6Dynamic()
  69. output1 = relu6(x1)
  70. assert (output1.asnumpy() == expect1).all()
  71. output2 = relu6(x2)
  72. assert (output2.asnumpy() == expect2).all()