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 2.9 kB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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. def scatternd_negative(nptype):
  44. context.set_context(mode=context.GRAPH_MODE, device_target="GPU")
  45. arr_indices = np.array([[1, 0], [1, 1], [1, 0], [1, 0], [1, 0]]).astype(np.int32)
  46. arr_update = np.array([-13.4, -3.1, 5.1, -12.1, -1.0]).astype(nptype)
  47. shape = (2, 2)
  48. expect = np.array([[0., 0.],
  49. [-21.4, -3.1]]).astype(nptype)
  50. scatternd_net(arr_indices, arr_update, shape, expect)
  51. @pytest.mark.level0
  52. @pytest.mark.platform_x86_gpu_traning
  53. @pytest.mark.env_onecard
  54. def test_scatternd_float32():
  55. scatternd_positive(np.float32)
  56. scatternd_negative(np.float32)
  57. @pytest.mark.level0
  58. @pytest.mark.platform_x86_gpu_traning
  59. @pytest.mark.env_onecard
  60. def test_scatternd_float16():
  61. scatternd_positive(np.float16)
  62. scatternd_negative(np.float16)
  63. @pytest.mark.level0
  64. @pytest.mark.platform_x86_gpu_traning
  65. @pytest.mark.env_onecard
  66. def test_scatternd_int16():
  67. scatternd_positive(np.int16)
  68. scatternd_negative(np.int16)
  69. @pytest.mark.level0
  70. @pytest.mark.platform_x86_gpu_traning
  71. @pytest.mark.env_onecard
  72. def test_scatternd_uint8():
  73. scatternd_positive(np.uint8)