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