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.2 kB

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