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_equal_op.py 4.5 kB

5 years ago
5 years ago
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. # Copyright 2019 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.common.tensor import Tensor
  19. from mindspore.nn import Cell
  20. from mindspore.ops import operations as P
  21. class NetEqual(Cell):
  22. def __init__(self):
  23. super(NetEqual, self).__init__()
  24. self.Equal = P.Equal()
  25. def construct(self, x, y):
  26. return self.Equal(x, y)
  27. class NetNotEqual(Cell):
  28. def __init__(self):
  29. super(NetNotEqual, self).__init__()
  30. self.NotEqual = P.NotEqual()
  31. def construct(self, x, y):
  32. return self.NotEqual(x, y)
  33. class NetGreaterEqual(Cell):
  34. def __init__(self):
  35. super(NetGreaterEqual, self).__init__()
  36. self.GreaterEqual = P.GreaterEqual()
  37. def construct(self, x, y):
  38. return self.GreaterEqual(x, y)
  39. @pytest.mark.level0
  40. @pytest.mark.platform_x86_gpu_training
  41. @pytest.mark.env_onecard
  42. def test_equal():
  43. x0_np = np.arange(24).reshape((4, 3, 2)).astype(np.float32)
  44. x0 = Tensor(x0_np)
  45. y0_np = np.arange(24).reshape((4, 3, 2)).astype(np.float32)
  46. y0 = Tensor(y0_np)
  47. expect0 = np.equal(x0_np, y0_np)
  48. x1_np = np.array([0, 1, 3]).astype(np.float32)
  49. x1 = Tensor(x1_np)
  50. y1_np = np.array([0, 1, -3]).astype(np.float32)
  51. y1 = Tensor(y1_np)
  52. expect1 = np.equal(x1_np, y1_np)
  53. x2_np = np.array([0, 1, 3]).astype(np.int32)
  54. x2 = Tensor(x2_np)
  55. y2_np = np.array([0, 1, -3]).astype(np.int32)
  56. y2 = Tensor(y2_np)
  57. expect2 = np.equal(x2_np, y2_np)
  58. context.set_context(mode=context.PYNATIVE_MODE, device_target="GPU")
  59. equal = NetEqual()
  60. output0 = equal(x0, y0)
  61. assert np.all(output0.asnumpy() == expect0)
  62. assert output0.shape == expect0.shape
  63. output1 = equal(x1, y1)
  64. assert np.all(output1.asnumpy() == expect1)
  65. assert output1.shape == expect1.shape
  66. output2 = equal(x2, y2)
  67. assert np.all(output2.asnumpy() == expect2)
  68. assert output2.shape == expect2.shape
  69. context.set_context(mode=context.GRAPH_MODE, device_target="GPU")
  70. equal = NetEqual()
  71. output0 = equal(x0, y0)
  72. assert np.all(output0.asnumpy() == expect0)
  73. assert output0.shape == expect0.shape
  74. output1 = equal(x1, y1)
  75. assert np.all(output1.asnumpy() == expect1)
  76. assert output1.shape == expect1.shape
  77. output2 = equal(x2, y2)
  78. assert np.all(output2.asnumpy() == expect2)
  79. assert output2.shape == expect2.shape
  80. @pytest.mark.level0
  81. @pytest.mark.platform_x86_gpu_training
  82. @pytest.mark.env_onecard
  83. def test_notequal():
  84. x0 = Tensor(np.array([[1.2, 1], [1, 0]]).astype(np.float32))
  85. y0 = Tensor(np.array([[1, 2]]).astype(np.float32))
  86. expect0 = np.array([[True, True], [False, True]])
  87. context.set_context(mode=context.PYNATIVE_MODE, device_target="GPU")
  88. notequal = NetNotEqual()
  89. output0 = notequal(x0, y0)
  90. assert np.all(output0.asnumpy() == expect0)
  91. assert output0.shape == expect0.shape
  92. context.set_context(mode=context.GRAPH_MODE, device_target="GPU")
  93. notequal = NetNotEqual()
  94. output0 = notequal(x0, y0)
  95. assert np.all(output0.asnumpy() == expect0)
  96. assert output0.shape == expect0.shape
  97. @pytest.mark.level0
  98. @pytest.mark.platform_x86_gpu_training
  99. @pytest.mark.env_onecard
  100. def test_greaterqual():
  101. x0 = Tensor(np.array([[1.2, 1], [1, 0]]).astype(np.float32))
  102. y0 = Tensor(np.array([[1, 2]]).astype(np.float32))
  103. expect0 = np.array([[True, False], [True, False]])
  104. context.set_context(mode=context.PYNATIVE_MODE, device_target="GPU")
  105. gequal = NetGreaterEqual()
  106. output0 = gequal(x0, y0)
  107. assert np.all(output0.asnumpy() == expect0)
  108. assert output0.shape == expect0.shape
  109. context.set_context(mode=context.GRAPH_MODE, device_target="GPU")
  110. gequal = NetGreaterEqual()
  111. output0 = gequal(x0, y0)
  112. assert np.all(output0.asnumpy() == expect0)
  113. assert output0.shape == expect0.shape