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_unique.py 2.9 kB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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. from mindspore import ops
  19. import mindspore.nn as nn
  20. from mindspore import Tensor
  21. import mindspore.common.dtype as mstype
  22. from mindspore.ops import operations as P
  23. context.set_context(mode=context.GRAPH_MODE, device_target="Ascend")
  24. class Net(nn.Cell):
  25. def __init__(self):
  26. super(Net, self).__init__()
  27. self.unique = P.Unique()
  28. def construct(self, x):
  29. return self.unique(x)
  30. class NetFunc(nn.Cell):
  31. def __init__(self):
  32. super(NetFunc, self).__init__()
  33. self.unique = ops.unique
  34. def construct(self, x):
  35. return self.unique(x)
  36. @pytest.mark.level0
  37. @pytest.mark.platform_arm_ascend_training
  38. @pytest.mark.platform_x86_ascend_training
  39. @pytest.mark.env_onecard
  40. def test_unqiue():
  41. x = Tensor(np.array([1, 1, 2, 2, 3, 3]), mstype.int32)
  42. unique = Net()
  43. output = unique(x)
  44. expect1 = np.array([1, 2, 3])
  45. expect2 = np.array([0, 0, 1, 1, 2, 2])
  46. assert (output[0].asnumpy() == expect1).all()
  47. assert (output[1].asnumpy() == expect2).all()
  48. @pytest.mark.level0
  49. @pytest.mark.platform_arm_ascend_training
  50. @pytest.mark.platform_x86_ascend_training
  51. @pytest.mark.env_onecard
  52. def test_unqiue_func_1d():
  53. """
  54. Feature: Test unique function
  55. Description: Input 1D Tensor
  56. Expectation: Successful execution.
  57. """
  58. x = Tensor(np.array([1, 1, 2, 2, 3, 3]), mstype.int32)
  59. unique = NetFunc()
  60. output = unique(x)
  61. expect1 = np.array([1, 2, 3])
  62. expect2 = np.array([0, 0, 1, 1, 2, 2])
  63. assert (output[0].asnumpy() == expect1).all()
  64. assert (output[1].asnumpy() == expect2).all()
  65. @pytest.mark.level0
  66. @pytest.mark.platform_arm_ascend_training
  67. @pytest.mark.platform_x86_ascend_training
  68. @pytest.mark.env_onecard
  69. def test_unqiue_func_2d():
  70. """
  71. Feature: Test unique function
  72. Description: Input 2D Tensor
  73. Expectation: Successful execution.
  74. """
  75. x = Tensor(np.array([[1, 1, 2], [2, 3, 3]]), mstype.int32)
  76. unique = NetFunc()
  77. output = unique(x)
  78. expect1 = np.array([1, 2, 3])
  79. expect2 = np.array([[0, 0, 1], [1, 2, 2]])
  80. assert (output[0].asnumpy() == expect1).all()
  81. assert (output[1].asnumpy() == expect2).all()