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