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_cpu.py 3.5 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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="CPU")
  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. class UniqueSquare(nn.Cell):
  37. def __init__(self):
  38. super(UniqueSquare, self).__init__()
  39. self.unique = P.Unique()
  40. self.square = P.Square()
  41. def construct(self, x):
  42. x, _ = self.unique(x)
  43. return self.square(x)
  44. @pytest.mark.level0
  45. @pytest.mark.platform_arm_ascend_training
  46. @pytest.mark.platform_x86_ascend_training
  47. @pytest.mark.env_onecard
  48. def test_unique_cpu():
  49. x = Tensor(np.array([1, 1, 2, 2, 3, 3]), mstype.int32)
  50. unique = Net()
  51. output = unique(x)
  52. expect1 = np.array([1, 2, 3])
  53. expect2 = np.array([0, 0, 1, 1, 2, 2])
  54. assert (output[0].asnumpy() == expect1).all()
  55. assert (output[1].asnumpy() == expect2).all()
  56. @pytest.mark.level0
  57. @pytest.mark.platform_arm_ascend_training
  58. @pytest.mark.platform_x86_ascend_training
  59. @pytest.mark.env_onecard
  60. def test_unique_square():
  61. x = Tensor(np.array([1, 1, 2, 2, 3, 3]), mstype.int32)
  62. net = UniqueSquare()
  63. output = net(x)
  64. expect1 = np.array([1, 4, 9])
  65. assert (output.asnumpy() == expect1).all()
  66. @pytest.mark.level0
  67. @pytest.mark.platform_arm_ascend_training
  68. @pytest.mark.platform_x86_ascend_training
  69. @pytest.mark.env_onecard
  70. def test_unqiue_func_1d():
  71. """
  72. Feature: Test unique function
  73. Description: Input 1D Tensor
  74. Expectation: Successful execution.
  75. """
  76. x = Tensor(np.array([1, 1, 2, 2, 3, 3]), mstype.int32)
  77. unique = NetFunc()
  78. output = unique(x)
  79. expect1 = np.array([1, 2, 3])
  80. expect2 = np.array([0, 0, 1, 1, 2, 2])
  81. assert (output[0].asnumpy() == expect1).all()
  82. assert (output[1].asnumpy() == expect2).all()
  83. @pytest.mark.level0
  84. @pytest.mark.platform_arm_ascend_training
  85. @pytest.mark.platform_x86_ascend_training
  86. @pytest.mark.env_onecard
  87. def test_unqiue_func_2d():
  88. """
  89. Feature: Test unique function
  90. Description: Input 2D Tensor
  91. Expectation: Successful execution.
  92. """
  93. x = Tensor(np.array([[1, 1, 2], [2, 3, 3]]), mstype.int32)
  94. unique = NetFunc()
  95. output = unique(x)
  96. expect1 = np.array([1, 2, 3])
  97. expect2 = np.array([[0, 0, 1], [1, 2, 2]])
  98. assert (output[0].asnumpy() == expect1).all()
  99. assert (output[1].asnumpy() == expect2).all()