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_logical_op.py 3.1 kB

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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.common.tensor import Tensor
  19. from mindspore.nn import Cell
  20. from mindspore.ops import operations as P
  21. class NetAnd(Cell):
  22. def __init__(self):
  23. super(NetAnd, self).__init__()
  24. self.logicaland = P.LogicalAnd()
  25. def construct(self, input_x, input_y):
  26. return self.logicaland(input_x, input_y)
  27. class NetOr(Cell):
  28. def __init__(self):
  29. super(NetOr, self).__init__()
  30. self.logicalor = P.LogicalOr()
  31. def construct(self, input_x, input_y):
  32. return self.logicalor(input_x, input_y)
  33. class NetNot(Cell):
  34. def __init__(self):
  35. super(NetNot, self).__init__()
  36. self.logicalnot = P.LogicalNot()
  37. def construct(self, input_x):
  38. return self.logicalnot(input_x)
  39. x = np.array([True, False, False]).astype(np.bool)
  40. y = np.array([False]).astype(np.bool)
  41. @pytest.mark.level0
  42. @pytest.mark.platform_x86_gpu_training
  43. @pytest.mark.env_onecard
  44. def test_logicaland():
  45. context.set_context(mode=context.PYNATIVE_MODE, device_target="GPU")
  46. logicaland = NetAnd()
  47. output = logicaland(Tensor(x), Tensor(y))
  48. assert np.all(output.asnumpy() == np.logical_and(x, y))
  49. context.set_context(mode=context.GRAPH_MODE, device_target="GPU")
  50. logicaland = NetAnd()
  51. output = logicaland(Tensor(x), Tensor(y))
  52. assert np.all(output.asnumpy() == np.logical_and(x, y))
  53. @pytest.mark.level0
  54. @pytest.mark.platform_x86_gpu_training
  55. @pytest.mark.env_onecard
  56. def test_logicalor():
  57. context.set_context(mode=context.PYNATIVE_MODE, device_target="GPU")
  58. logicalor = NetOr()
  59. output = logicalor(Tensor(x), Tensor(y))
  60. assert np.all(output.asnumpy() == np.logical_or(x, y))
  61. context.set_context(mode=context.GRAPH_MODE, device_target="GPU")
  62. logicalor = NetOr()
  63. output = logicalor(Tensor(x), Tensor(y))
  64. assert np.all(output.asnumpy() == np.logical_or(x, y))
  65. @pytest.mark.level0
  66. @pytest.mark.platform_x86_gpu_training
  67. @pytest.mark.env_onecard
  68. def test_logicalnot():
  69. context.set_context(mode=context.PYNATIVE_MODE, device_target="GPU")
  70. logicalnot = NetNot()
  71. output = logicalnot(Tensor(x))
  72. assert np.all(output.asnumpy() == np.logical_not(x))
  73. context.set_context(mode=context.GRAPH_MODE, device_target="GPU")
  74. logicalnot = NetNot()
  75. output = logicalnot(Tensor(x))
  76. assert np.all(output.asnumpy() == np.logical_not(x))