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_array_ops_check.py 5.8 kB

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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 ops """
  16. import numpy as np
  17. import mindspore.nn as nn
  18. from mindspore import Tensor
  19. from mindspore.common import dtype as mstype
  20. from mindspore.ops import operations as P
  21. from ....mindspore_test_framework.mindspore_test import mindspore_test
  22. from ....mindspore_test_framework.pipeline.forward.compile_forward \
  23. import pipeline_for_compile_forward_ge_graph_for_case_by_case_config_exception
  24. class ExpandDimsNet(nn.Cell):
  25. def __init__(self, axis):
  26. super(ExpandDimsNet, self).__init__()
  27. self.axis = axis
  28. self.op = P.ExpandDims()
  29. def construct(self, x):
  30. return self.op(x, self.axis)
  31. class IsInstanceNet(nn.Cell):
  32. def __init__(self, inst):
  33. super(IsInstanceNet, self).__init__()
  34. self.inst = inst
  35. self.op = P.IsInstance()
  36. def construct(self, t):
  37. return self.op(self.inst, t)
  38. class ReshapeNet(nn.Cell):
  39. def __init__(self, shape):
  40. super(ReshapeNet, self).__init__()
  41. self.shape = shape
  42. self.op = P.Reshape()
  43. def construct(self, x):
  44. return self.op(x, self.shape)
  45. raise_set = [
  46. # input is scala, not Tensor
  47. ('ExpandDims0', {
  48. 'block': (P.ExpandDims(), {'exception': TypeError, 'error_keywords': ['ExpandDims']}),
  49. 'desc_inputs': [5.0, 1],
  50. 'skip': ['backward']}),
  51. # axis is as a parameter
  52. ('ExpandDims1', {
  53. 'block': (P.ExpandDims(), {'exception': TypeError, 'error_keywords': ['ExpandDims']}),
  54. 'desc_inputs': [Tensor(np.ones([3, 4]).astype(np.float32)), 1],
  55. 'skip': ['backward']}),
  56. # axis as an attribute, but less then lower limit
  57. ('ExpandDims2', {
  58. 'block': (ExpandDimsNet(-4), {'exception': ValueError, 'error_keywords': ['ExpandDims']}),
  59. 'desc_inputs': [Tensor(np.ones([3, 4]).astype(np.float32))],
  60. 'skip': ['backward']}),
  61. # axis as an attribute, but greater then upper limit
  62. ('ExpandDims3', {
  63. 'block': (ExpandDimsNet(3), {'exception': ValueError, 'error_keywords': ['ExpandDims']}),
  64. 'desc_inputs': [Tensor(np.ones([3, 4]).astype(np.float32))],
  65. 'skip': ['backward']}),
  66. # input is scala, not Tensor
  67. ('DType0', {
  68. 'block': (P.DType(), {'exception': TypeError, 'error_keywords': ['DType']}),
  69. 'desc_inputs': [5.0],
  70. 'skip': ['backward']}),
  71. # input x scala, not Tensor
  72. ('SameTypeShape0', {
  73. 'block': (P.SameTypeShape(), {'exception': TypeError, 'error_keywords': ['SameTypeShape']}),
  74. 'desc_inputs': [5.0, Tensor(np.ones([3, 4]).astype(np.float32))],
  75. 'skip': ['backward']}),
  76. # input y scala, not Tensor
  77. ('SameTypeShape1', {
  78. 'block': (P.SameTypeShape(), {'exception': TypeError, 'error_keywords': ['SameTypeShape']}),
  79. 'desc_inputs': [Tensor(np.ones([3, 4]).astype(np.float32)), 5.0],
  80. 'skip': ['backward']}),
  81. # type of x and y not match
  82. ('SameTypeShape2', {
  83. 'block': (P.SameTypeShape(), {'exception': TypeError, 'error_keywords': ['SameTypeShape']}),
  84. 'desc_inputs': [Tensor(np.ones([3, 4]).astype(np.float32)), Tensor(np.ones([3, 4]).astype(np.int32))],
  85. 'skip': ['backward']}),
  86. # shape of x and y not match
  87. ('SameTypeShape3', {
  88. 'block': (P.SameTypeShape(), {'exception': ValueError, 'error_keywords': ['SameTypeShape']}),
  89. 'desc_inputs': [Tensor(np.ones([3, 4]).astype(np.float32)), Tensor(np.ones([3, 3]).astype(np.float32))],
  90. 'skip': ['backward']}),
  91. # sub_type is None
  92. ('IsSubClass0', {
  93. 'block': (P.IsSubClass(), {'exception': TypeError, 'error_keywords': ['IsSubClass']}),
  94. 'desc_inputs': [None, mstype.number],
  95. 'skip': ['backward']}),
  96. # type_ is None
  97. ('IsSubClass1', {
  98. 'block': (P.IsSubClass(), {'exception': TypeError, 'error_keywords': ['IsSubClass']}),
  99. 'desc_inputs': [mstype.number, None],
  100. 'skip': ['backward']}),
  101. # inst is var
  102. ('IsInstance0', {
  103. 'block': (P.IsInstance(), {'exception': ValueError, 'error_keywords': ['IsInstance']}),
  104. 'desc_inputs': [5.0, mstype.number],
  105. 'skip': ['backward']}),
  106. # t is not mstype.Type
  107. ('IsInstance1', {
  108. 'block': (IsInstanceNet(5.0), {'exception': TypeError, 'error_keywords': ['IsInstance']}),
  109. 'desc_inputs': [None],
  110. 'skip': ['backward']}),
  111. # input x is scalar, not Tensor
  112. ('Reshape0', {
  113. 'block': (P.Reshape(), {'exception': TypeError, 'error_keywords': ['Reshape']}),
  114. 'desc_inputs': [5.0, (1, 2)],
  115. 'skip': ['backward']}),
  116. # input shape is var
  117. ('Reshape1', {
  118. 'block': (P.Reshape(), {'exception': TypeError, 'error_keywords': ['Reshape']}),
  119. 'desc_inputs': [Tensor(np.ones([3, 4]).astype(np.float32)), (2, 3, 2)],
  120. 'skip': ['backward']}),
  121. # element of shape is not int
  122. ('Reshape3', {
  123. 'block': (ReshapeNet((2, 3.0, 2)), {'exception': TypeError, 'error_keywords': ['Reshape']}),
  124. 'desc_inputs': [Tensor(np.ones([3, 4]).astype(np.float32))],
  125. 'skip': ['backward']}),
  126. ]
  127. @mindspore_test(pipeline_for_compile_forward_ge_graph_for_case_by_case_config_exception)
  128. def test_check_exception():
  129. return raise_set