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 super""" 16import numpy as np 17 18import mindspore.nn as nn 19from mindspore import Tensor 20from mindspore import context 21 22context.set_context(mode=context.GRAPH_MODE) 23 24 25class FatherNet(nn.Cell): 26 def __init__(self, x): 27 super(FatherNet, self).__init__(x) 28 self.x = x 29 30 def construct(self, x, y): 31 return self.x * x 32 33 def test_father(self, x): 34 return self.x + x 35 36 37class MatherNet(nn.Cell): 38 def __init__(self, y): 39 super(MatherNet, self).__init__() 40 self.y = y 41 42 def construct(self, x, y): 43 return self.y * y 44 45 def test_mather(self, y): 46 return self.y + y 47 48 49class SingleSubNet(FatherNet): 50 def __init__(self, x, z): 51 super(SingleSubNet, self).__init__(x) 52 self.z = z 53 54 def construct(self, x, y): 55 ret_father_construct = super().construct(x, y) 56 ret_father_test = super(SingleSubNet, self).test_father(x) 57 ret_father_x = super(SingleSubNet, self).x 58 ret_sub_z = self.z 59 60 return ret_father_construct, ret_father_test, ret_father_x, ret_sub_z 61 62 63class MulSubNet(FatherNet, MatherNet): 64 def __init__(self, x, y, z): 65 super(MulSubNet, self).__init__(x) 66 super(FatherNet, self).__init__(y) 67 self.z = z 68 69 def construct(self, x, y): 70 ret_father_construct = super().construct(x, y) 71 ret_father_test = super(MulSubNet, self).test_father(x) 72 ret_father_x = super(MulSubNet, self).x 73 ret_mather_construct = super(FatherNet, self).construct(x, y) 74 ret_mather_test = super(FatherNet, self).test_mather(y) 75 ret_mather_y = super(FatherNet, self).y 76 ret_sub_z = self.z 77 78 return ret_father_construct, ret_father_test, ret_father_x, \ 79 ret_mather_construct, ret_mather_test, ret_mather_y, ret_sub_z 80 81 82class Net(nn.Cell): 83 def __init__(self, x): 84 super(Net, self).__init__() 85 self.x = x 86 87 def construct(self, x, y): 88 ret = super(Net, self).construct(x, y) 89 return ret 90 91 92def test_single_super(): 93 single_net = SingleSubNet(2, 3) 94 x = Tensor(np.ones([1, 2, 3], np.int32)) 95 y = Tensor(np.ones([1, 2, 3], np.int32)) 96 single_net(x, y) 97 98 99def test_mul_super(): 100 mul_net = MulSubNet(2, 3, 4) 101 x = Tensor(np.ones([1, 2, 3], np.int32)) 102 y = Tensor(np.ones([1, 2, 3], np.int32)) 103 mul_net(x, y) 104 105 106def test_super_cell(): 107 net = Net(2) 108 x = Tensor(np.ones([1, 2, 3], np.int32)) 109 y = Tensor(np.ones([1, 2, 3], np.int32)) 110 assert net(x, y) is None 111 112 113def test_single_super_in(): 114 class FatherNetIn(nn.Cell): 115 def __init__(self, x): 116 super(FatherNetIn, self).__init__(x) 117 self.x = x 118 119 def construct(self, x, y): 120 return self.x * x 121 122 def test_father(self, x): 123 return self.x + x 124 125 class SingleSubNetIN(FatherNetIn): 126 def __init__(self, x, z): 127 super(SingleSubNetIN, self).__init__(x) 128 self.z = z 129 130 def construct(self, x, y): 131 ret_father_construct = super().construct(x, y) 132 ret_father_test = super(SingleSubNetIN, self).test_father(x) 133 ret_father_x = super(SingleSubNetIN, self).x 134 ret_sub_z = self.z 135 136 return ret_father_construct, ret_father_test, ret_father_x, ret_sub_z 137 138 single_net_in = SingleSubNetIN(2, 3) 139 x = Tensor(np.ones([1, 2, 3], np.int32)) 140 y = Tensor(np.ones([1, 2, 3], np.int32)) 141 single_net_in(x, y) 142