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_gather_op.py 3.7 kB

5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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. from mindspore.common.api import ms_function
  20. import numpy as np
  21. import mindspore.context as context
  22. from mindspore.common import dtype as mstype
  23. context.set_context(mode=context.GRAPH_MODE, device_target='CPU')
  24. class NetGatherV2_axis0(nn.Cell):
  25. def __init__(self):
  26. super(NetGatherV2_axis0, self).__init__()
  27. self.gatherv2 = P.GatherV2()
  28. def construct(self, params, indices):
  29. return self.gatherv2(params, indices, 0)
  30. @pytest.mark.level0
  31. @pytest.mark.platform_x86_cpu
  32. @pytest.mark.env_onecard
  33. def test_gatherv2_axis0():
  34. x = Tensor(np.arange(3 * 2 * 2).reshape(3, 2, 2), mstype.float32)
  35. indices = Tensor(np.array([1, 2]), mstype.int32)
  36. gatherv2 = NetGatherV2_axis0()
  37. ms_output = gatherv2(x, indices)
  38. print("output:\n", ms_output)
  39. expect = np.array([[[4., 5.],
  40. [6., 7.]],
  41. [[8., 9.],
  42. [10., 11.]]])
  43. error = np.ones(shape=ms_output.asnumpy().shape) * 1.0e-6
  44. diff = ms_output.asnumpy() - expect
  45. assert np.all(diff < error)
  46. assert np.all(-diff < error)
  47. class NetGatherV2_axis1(nn.Cell):
  48. def __init__(self):
  49. super(NetGatherV2_axis1, self).__init__()
  50. self.gatherv2 = P.GatherV2()
  51. def construct(self, params, indices):
  52. return self.gatherv2(params, indices, 1)
  53. @pytest.mark.level0
  54. @pytest.mark.platform_x86_cpu
  55. @pytest.mark.env_onecard
  56. def test_gatherv2_axis1():
  57. x = Tensor(np.arange(2 * 3 * 2).reshape(2, 3, 2), mstype.float32)
  58. indices = Tensor(np.array([1, 2]), mstype.int32)
  59. gatherv2 = NetGatherV2_axis1()
  60. ms_output = gatherv2(x, indices)
  61. print("output:\n", ms_output)
  62. expect = np.array([[[2., 3.],
  63. [4., 5.]],
  64. [[8., 9.],
  65. [10., 11.]]])
  66. error = np.ones(shape=ms_output.asnumpy().shape) * 1.0e-6
  67. diff = ms_output.asnumpy() - expect
  68. assert np.all(diff < error)
  69. assert np.all(-diff < error)
  70. class NetGatherV2_axisN1(nn.Cell):
  71. def __init__(self):
  72. super(NetGatherV2_axisN1, self).__init__()
  73. self.gatherv2 = P.GatherV2()
  74. def construct(self, params, indices):
  75. return self.gatherv2(params, indices, -1)
  76. @pytest.mark.level0
  77. @pytest.mark.platform_x86_cpu
  78. @pytest.mark.env_onecard
  79. def test_gatherv2_axisN1():
  80. x = Tensor(np.arange(2 * 2 * 3).reshape(2, 2, 3), mstype.float32)
  81. indices = Tensor(np.array([1, 2]), mstype.int32)
  82. gatherv2 = NetGatherV2_axisN1()
  83. ms_output = gatherv2(x, indices)
  84. print("output:\n", ms_output)
  85. expect = np.array([[[1., 2.],
  86. [4., 5.]],
  87. [[7., 8.],
  88. [10.,11.]]])
  89. error = np.ones(shape=ms_output.asnumpy().shape) * 1.0e-6
  90. diff = ms_output.asnumpy() - expect
  91. assert np.all(diff < error)
  92. assert np.all(-diff < error)
  93. if __name__ == '__main__':
  94. test_gatherv2_axis0()
  95. test_gatherv2_axis1()
  96. test_gatherv2_axisN1()