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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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 pytest
  16. from mindspore import Tensor
  17. import mindspore.nn as nn
  18. import numpy as np
  19. import mindspore.context as context
  20. class Net_Pool(nn.Cell):
  21. def __init__(self):
  22. super(Net_Pool, self).__init__()
  23. self.maxpool_fun = nn.MaxPool2d(kernel_size=2, stride=2, pad_mode="VALID")
  24. def construct(self, x):
  25. return self.maxpool_fun(x)
  26. class Net_Pool2(nn.Cell):
  27. def __init__(self):
  28. super(Net_Pool2, self).__init__()
  29. self.maxpool_fun = nn.MaxPool2d(kernel_size=3, stride=2, pad_mode="SAME")
  30. def construct(self, x):
  31. return self.maxpool_fun(x)
  32. @pytest.mark.level0
  33. @pytest.mark.platform_x86_gpu_training
  34. @pytest.mark.env_onecard
  35. def test_maxpool2d():
  36. x = Tensor(np.array([[[
  37. [0, 1, 2, 3, -4, -5],
  38. [6, 7, 8, 9, -10, -11],
  39. [12, 13, 14, -15, -16, -17],
  40. [18, 19, 20, 21, 22, 23],
  41. [24, 25, 26, 27, 28, 29],
  42. [30, 31, 32, 33, 34, 35]
  43. ]]]).astype(np.float32))
  44. expect_result = (np.array([[[
  45. [7, 9, -4],
  46. [19, 21, 23],
  47. [31, 33, 35]
  48. ]]]))
  49. expect_result2 = (np.array([[[
  50. [14, 14, -4],
  51. [26, 28, 29],
  52. [32, 34, 35]
  53. ]]]))
  54. context.set_context(mode=context.PYNATIVE_MODE, device_target="GPU")
  55. maxpool2d = Net_Pool()
  56. maxpool2d2 = Net_Pool2()
  57. output2 = maxpool2d2(x)
  58. output = maxpool2d(x)
  59. assert (output.asnumpy() == expect_result).all()
  60. assert (output2.asnumpy() == expect_result2).all()
  61. context.set_context(mode=context.GRAPH_MODE, device_target="GPU")
  62. maxpool2d = Net_Pool()
  63. maxpool2d2 = Net_Pool2()
  64. output2 = maxpool2d2(x)
  65. output = maxpool2d(x)
  66. assert (output.asnumpy() == expect_result).all()
  67. assert (output2.asnumpy() == expect_result2).all()