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

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