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.0 kB

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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 pytest
  16. from mindspore.ops import operations as P
  17. from mindspore.nn import Cell
  18. from mindspore.common.tensor import Tensor
  19. import mindspore.context as context
  20. import numpy as np
  21. class NetAnd(Cell):
  22. def __init__(self):
  23. super(NetAnd, self).__init__()
  24. self.logicaland = P.LogicalAnd()
  25. def construct(self, x, y):
  26. return self.logicaland(x, y)
  27. class NetOr(Cell):
  28. def __init__(self):
  29. super(NetOr, self).__init__()
  30. self.logicalor = P.LogicalOr()
  31. def construct(self, x, y):
  32. return self.logicalor(x, y)
  33. class NetNot(Cell):
  34. def __init__(self):
  35. super(NetNot, self).__init__()
  36. self.logicalnot = P.LogicalNot()
  37. def construct(self, x):
  38. return self.logicalnot(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))