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_scatter_nd.py 3.6 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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 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. class Net(nn.Cell):
  22. def __init__(self, _shape):
  23. super(Net, self).__init__()
  24. self.shape = _shape
  25. self.scatternd = P.ScatterNd()
  26. def construct(self, indices, update):
  27. return self.scatternd(indices, update, self.shape)
  28. def scatternd_net(indices, update, _shape, expect):
  29. scatternd = Net(_shape)
  30. output = scatternd(Tensor(indices), Tensor(update))
  31. error = np.ones(shape=output.asnumpy().shape) * 1.0e-6
  32. diff = output.asnumpy() - expect
  33. assert np.all(diff < error)
  34. assert np.all(-diff < error)
  35. def scatternd_positive(nptype):
  36. context.set_context(mode=context.GRAPH_MODE, device_target="GPU")
  37. arr_indices = np.array([[0, 1], [1, 1], [0, 1], [0, 1], [0, 1]]).astype(np.int32)
  38. arr_update = np.array([3.2, 1.1, 5.3, -2.2, -1.0]).astype(nptype)
  39. shape = (2, 2)
  40. expect = np.array([[0., 5.3],
  41. [0., 1.1]]).astype(nptype)
  42. scatternd_net(arr_indices, arr_update, shape, expect)
  43. arr_indices = np.array([[0, 1], [1, 1], [0, 1], [0, 1], [0, 1]]).astype(np.int64)
  44. arr_update = np.array([3.2, 1.1, 5.3, -2.2, -1.0]).astype(nptype)
  45. shape = (2, 2)
  46. expect = np.array([[0., 5.3],
  47. [0., 1.1]]).astype(nptype)
  48. scatternd_net(arr_indices, arr_update, shape, expect)
  49. def scatternd_negative(nptype):
  50. context.set_context(mode=context.GRAPH_MODE, device_target="GPU")
  51. arr_indices = np.array([[1, 0], [1, 1], [1, 0], [1, 0], [1, 0]]).astype(np.int32)
  52. arr_update = np.array([-13.4, -3.1, 5.1, -12.1, -1.0]).astype(nptype)
  53. shape = (2, 2)
  54. expect = np.array([[0., 0.],
  55. [-21.4, -3.1]]).astype(nptype)
  56. scatternd_net(arr_indices, arr_update, shape, expect)
  57. arr_indices = np.array([[1, 0], [1, 1], [1, 0], [1, 0], [1, 0]]).astype(np.int64)
  58. arr_update = np.array([-13.4, -3.1, 5.1, -12.1, -1.0]).astype(nptype)
  59. shape = (2, 2)
  60. expect = np.array([[0., 0.],
  61. [-21.4, -3.1]]).astype(nptype)
  62. scatternd_net(arr_indices, arr_update, shape, expect)
  63. @pytest.mark.level0
  64. @pytest.mark.platform_x86_gpu_traning
  65. @pytest.mark.env_onecard
  66. def test_scatternd_float32():
  67. scatternd_positive(np.float32)
  68. scatternd_negative(np.float32)
  69. @pytest.mark.level0
  70. @pytest.mark.platform_x86_gpu_traning
  71. @pytest.mark.env_onecard
  72. def test_scatternd_float16():
  73. scatternd_positive(np.float16)
  74. scatternd_negative(np.float16)
  75. @pytest.mark.level0
  76. @pytest.mark.platform_x86_gpu_traning
  77. @pytest.mark.env_onecard
  78. def test_scatternd_int16():
  79. scatternd_positive(np.int16)
  80. scatternd_negative(np.int16)
  81. @pytest.mark.level0
  82. @pytest.mark.platform_x86_gpu_traning
  83. @pytest.mark.env_onecard
  84. def test_scatternd_uint8():
  85. scatternd_positive(np.uint8)