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_super.py 4.1 kB

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