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_python_operators.py 4.3 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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. """ test control ops """
  16. import functools
  17. import numpy as np
  18. import mindspore as ms
  19. from mindspore import nn
  20. from mindspore import Tensor
  21. from mindspore import context
  22. from mindspore.ops import operations as P
  23. from mindspore.common import dtype as mstype
  24. from ....mindspore_test_framework.mindspore_test import mindspore_test
  25. from ....mindspore_test_framework.pipeline.forward.compile_forward \
  26. import pipeline_for_compile_forward_ge_graph_for_case_by_case_config
  27. context.set_context(mode=context.GRAPH_MODE)
  28. class ComparisonOpsNet(nn.Cell):
  29. def __init__(self):
  30. super(ComparisonOpsNet, self).__init__()
  31. def construct(self, x, y):
  32. a = x <= y
  33. b = x <= 1.0
  34. c = y >= 1.0
  35. d = y >= x
  36. e = x < y
  37. f = x < 1.0
  38. g = 1.0 > y
  39. h = y > x
  40. i = y == 3.0
  41. j = x != 4
  42. k = + x
  43. l = + 1.0
  44. m = k != l
  45. return a or b or c or d or e or f or g or h or i or j or m
  46. class MathOpsNet(nn.Cell):
  47. def __init__(self):
  48. super(MathOpsNet, self).__init__()
  49. self.relu = P.ReLU()
  50. def construct(self, x, y):
  51. x = x - (-1)
  52. return self.relu(x)
  53. class ScalarCompareNet(nn.Cell):
  54. def __init__(self):
  55. super(ScalarCompareNet, self).__init__()
  56. self.relu = P.ReLU()
  57. def construct(self, x, y):
  58. t = 0
  59. if 3 > 3.2:
  60. t = x + y
  61. else:
  62. t = x - y
  63. if 3.1 <= 5:
  64. t = t - x
  65. else:
  66. t = t + x
  67. a = 32.0 * 12
  68. b = 12/3.0
  69. if a > b:
  70. t = t * x
  71. else:
  72. t = t / x
  73. return t
  74. class LogicalNumberOpsNet(nn.Cell):
  75. def __init__(self):
  76. super(LogicalNumberOpsNet, self).__init__()
  77. self.cond = True
  78. self.one = 0
  79. self.zero = 0.0
  80. def construct(self, x, y):
  81. if self.cond and self.one or self.zero and not self.one:
  82. return x + y
  83. return x - y
  84. class LogicalTensorOpsNet(nn.Cell):
  85. def __init__(self):
  86. """"""
  87. super(LogicalTensorOpsNet, self).__init__()
  88. self.const_true = Tensor(True, dtype=mstype.bool_)
  89. def construct(self, x, y):
  90. ret = x and y and (y or self.const_true) and (not self.const_true)
  91. return ret
  92. test_case_ops = [
  93. ('CompareOpsNet', {
  94. 'block': ComparisonOpsNet(),
  95. 'desc_inputs': [Tensor(np.ones([6, 9, 10]), dtype=mstype.float32),
  96. Tensor(np.zeros([6, 9, 10]), dtype=mstype.float32)]}),
  97. ('MathOpsNet', {
  98. 'block': MathOpsNet(),
  99. 'desc_inputs': [Tensor(np.ones([6, 9, 10]), dtype=mstype.float32),
  100. Tensor(np.zeros([6, 9, 10]), dtype=mstype.float32)]}),
  101. ('ScalarCompareNet', {
  102. 'block': ScalarCompareNet(),
  103. 'desc_inputs': [Tensor(np.ones([6, 9, 10]), dtype=mstype.float32),
  104. Tensor(np.zeros([6, 9, 10]), dtype=mstype.float32)]}),
  105. ('LogicalNumberOps', {
  106. 'block': LogicalNumberOpsNet(),
  107. 'desc_inputs': [Tensor(np.ones([6, 9, 10]), dtype=mstype.float32),
  108. Tensor(np.zeros([6, 9, 10]), dtype=mstype.float32)]}),
  109. ('LogicalTensorOps', {
  110. 'block': LogicalTensorOpsNet(),
  111. 'desc_inputs': [Tensor(np.ones([6, 9, 10]).astype(np.bool_), dtype=mstype.bool_),
  112. Tensor(np.zeros([6, 9, 10]).astype(np.bool_), dtype=mstype.bool_)]}),
  113. ]
  114. test_case_lists = [test_case_ops]
  115. test_exec_case = functools.reduce(lambda x, y: x + y, test_case_lists)
  116. @mindspore_test(pipeline_for_compile_forward_ge_graph_for_case_by_case_config)
  117. def test_compile():
  118. return test_exec_case