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_partial.py 6.0 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  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 partial"""
  16. from functools import partial
  17. import numpy as np
  18. import pytest
  19. from mindspore import nn, Tensor, context
  20. context.set_context(mode=context.GRAPH_MODE)
  21. def test_partial_pos_arg():
  22. class Net(nn.Cell):
  23. def __init__(self):
  24. super(Net, self).__init__()
  25. def show(self, x, y, z):
  26. return x, y, z
  27. def construct(self, x, y, z):
  28. f = partial(self.show, x)
  29. ret = f(y, z)
  30. return ret
  31. x = Tensor(np.arange(3).reshape((3,)).astype(np.float32))
  32. y = Tensor(np.arange(3 * 4).reshape((3, 4)).astype(np.float32))
  33. z = Tensor(np.arange(3 * 4 * 5).reshape((3, 4, 5)).astype(np.float32))
  34. net = Net()
  35. net(x, y, z)
  36. def test_partial_key_ward_arg():
  37. class Net(nn.Cell):
  38. def __init__(self):
  39. super(Net, self).__init__()
  40. def show(self, x, y, z):
  41. return x, y, z
  42. def construct(self, x, y, z):
  43. f = partial(self.show, x=x)
  44. ret = f(y=y, z=z)
  45. return ret
  46. x = Tensor(np.arange(3).reshape((3,)).astype(np.float32))
  47. y = Tensor(np.arange(3 * 4).reshape((3, 4)).astype(np.float32))
  48. z = Tensor(np.arange(3 * 4 * 5).reshape((3, 4, 5)).astype(np.float32))
  49. net = Net()
  50. net(x, y, z)
  51. def test_partial_key_ward_arg_update():
  52. class Net(nn.Cell):
  53. def __init__(self):
  54. super(Net, self).__init__()
  55. def show(self, x, y, z):
  56. return x, y, z
  57. def construct(self, x, y, z):
  58. f = partial(self.show, x=x, y=y)
  59. ret = f(y=y, z=z)
  60. return ret
  61. x = Tensor(np.arange(3).reshape((3,)).astype(np.float32))
  62. y = Tensor(np.arange(3 * 4).reshape((3, 4)).astype(np.float32))
  63. z = Tensor(np.arange(3 * 4 * 5).reshape((3, 4, 5)).astype(np.float32))
  64. net = Net()
  65. net(x, y, z)
  66. def test_partial_key_ward_arg_and_pos_arg():
  67. class Net(nn.Cell):
  68. def __init__(self):
  69. super(Net, self).__init__()
  70. def show(self, x, y, z):
  71. return x, y, z
  72. def construct(self, x, y, z):
  73. f = partial(self.show, y=y)
  74. ret = f(2, z=z)
  75. return ret
  76. x = Tensor(np.arange(3).reshape((3,)).astype(np.float32))
  77. y = Tensor(np.arange(3 * 4).reshape((3, 4)).astype(np.float32))
  78. z = Tensor(np.arange(3 * 4 * 5).reshape((3, 4, 5)).astype(np.float32))
  79. net = Net()
  80. net(x, y, z)
  81. def test_partial_pos_arg_const():
  82. class Net(nn.Cell):
  83. def __init__(self):
  84. super(Net, self).__init__()
  85. def show(self, x, y, z):
  86. return x, y, z
  87. def construct(self):
  88. f = partial(self.show, 1)
  89. ret = f(2, 3)
  90. return ret
  91. net = Net()
  92. assert net() == (1, 2, 3)
  93. def test_partial_key_ward_arg_const():
  94. class Net(nn.Cell):
  95. def __init__(self):
  96. super(Net, self).__init__()
  97. def show(self, x, y, z):
  98. return x, y, z
  99. def construct(self):
  100. f = partial(self.show, x=1)
  101. ret = f(y=2, z=3)
  102. return ret
  103. net = Net()
  104. assert net() == (1, 2, 3)
  105. def test_partial_key_ward_arg_update_const():
  106. class Net(nn.Cell):
  107. def __init__(self):
  108. super(Net, self).__init__()
  109. def show(self, x, y, z):
  110. return x, y, z
  111. def construct(self):
  112. f = partial(self.show, x=1, y=2)
  113. ret = f(y=3, z=4)
  114. return ret
  115. net = Net()
  116. assert net() == (1, 3, 4)
  117. def test_partial_key_ward_arg_and_pos_arg_const():
  118. class Net(nn.Cell):
  119. def __init__(self):
  120. super(Net, self).__init__()
  121. def show(self, x, y, z):
  122. return x, y, z
  123. def construct(self):
  124. f = partial(self.show, y=2)
  125. ret = f(1, z=3)
  126. return ret
  127. net = Net()
  128. assert net() == (1, 2, 3)
  129. def test_partial_key_ward_arg_and_pos_arg_const_multi_assign_x():
  130. class Net(nn.Cell):
  131. def __init__(self):
  132. super(Net, self).__init__()
  133. def show(self, x, y, z):
  134. return x, y, z
  135. def construct(self):
  136. f = partial(self.show, x=1)
  137. ret = f(1, 2, 3)
  138. return ret
  139. net = Net()
  140. with pytest.raises(TypeError) as ex:
  141. net()
  142. assert "Multiply values for specific argument: x" in str(ex.value)
  143. def test_partial_key_ward_arg_and_pos_arg_const_multi_assign_y():
  144. class Net(nn.Cell):
  145. def __init__(self):
  146. super(Net, self).__init__()
  147. def show(self, x, y, z):
  148. return x, y, z
  149. def construct(self):
  150. f = partial(self.show, y=2)
  151. ret = f(1, 2, z=3)
  152. return ret
  153. net = Net()
  154. with pytest.raises(TypeError) as ex:
  155. net()
  156. assert "Multiply values for specific argument: y" in str(ex.value)
  157. def test_partial_key_ward_arg_and_pos_arg_const_multi_assign_z():
  158. class Net(nn.Cell):
  159. def __init__(self):
  160. super(Net, self).__init__()
  161. def show(self, x, y, z):
  162. return x, y, z
  163. def construct(self):
  164. f = partial(self.show, z=1)
  165. ret = f(1, 2, 3)
  166. return ret
  167. net = Net()
  168. with pytest.raises(TypeError) as ex:
  169. net()
  170. assert "Multiply values for specific argument: z" in str(ex.value)