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_parameter.py 14 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387
  1. # Copyright 2022 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. import pytest
  16. import mindspore as ms
  17. from mindspore.nn import Cell
  18. from mindspore.common.parameter import Parameter
  19. from mindspore.common import ParameterTuple
  20. from mindspore import Tensor, context
  21. context.set_context(mode=context.GRAPH_MODE)
  22. @pytest.mark.level1
  23. @pytest.mark.platform_arm_ascend_training
  24. @pytest.mark.platform_x86_ascend_training
  25. @pytest.mark.env_onecard
  26. def test_parameter_1_1():
  27. """
  28. Feature: Check the names of parameters and the names of inputs of construct.
  29. Description: If the name of the input of construct is same as the parameters, add suffix to the name of the input.
  30. Expectation: No exception.
  31. """
  32. class ParamNet(Cell):
  33. def __init__(self):
  34. super(ParamNet, self).__init__()
  35. self.param_a = Parameter(Tensor([1], ms.float32), name="name_a")
  36. self.param_b = Parameter(Tensor([2], ms.float32), name="name_b")
  37. def construct(self, name_a):
  38. return self.param_a + self.param_b - name_a
  39. net = ParamNet()
  40. res = net(Tensor([3], ms.float32))
  41. assert res == 0
  42. @pytest.mark.level1
  43. @pytest.mark.platform_arm_ascend_training
  44. @pytest.mark.platform_x86_ascend_training
  45. @pytest.mark.env_onecard
  46. def test_parameter_1_2():
  47. """
  48. Feature: Check the names of parameters and the names of inputs of construct.
  49. Description: If the name of the input of construct is same as the parameters, add suffix to the name of the input.
  50. Expectation: No exception.
  51. """
  52. class ParamNet(Cell):
  53. def __init__(self):
  54. super(ParamNet, self).__init__()
  55. self.param_a = Parameter(Tensor([1], ms.float32), name="name_a")
  56. self.param_b = ParameterTuple((Parameter(Tensor([2], ms.float32), name="name_b"), self.param_a))
  57. def construct(self, name_b):
  58. return self.param_a + self.param_b[0] - name_b
  59. net = ParamNet()
  60. res = net(Tensor([3], ms.float32))
  61. assert res == 0
  62. @pytest.mark.level1
  63. @pytest.mark.platform_arm_ascend_training
  64. @pytest.mark.platform_x86_ascend_training
  65. @pytest.mark.env_onecard
  66. def test_parameter_2_1():
  67. """
  68. Feature: Check the names of parameters.
  69. Description: If parameters in init have same name, an exception will be thrown.
  70. Expectation: No exception.
  71. """
  72. class ParamNet(Cell):
  73. def __init__(self):
  74. super(ParamNet, self).__init__()
  75. self.param_a = Parameter(Tensor([1], ms.float32), name="name_a")
  76. self.param_b = Parameter(Tensor([2], ms.float32), name="name_a")
  77. def construct(self):
  78. return self.param_a + self.param_b
  79. with pytest.raises(ValueError, match="its name 'name_a' already exists."):
  80. net = ParamNet()
  81. res = net()
  82. assert res == 3
  83. @pytest.mark.level1
  84. @pytest.mark.platform_arm_ascend_training
  85. @pytest.mark.platform_x86_ascend_training
  86. @pytest.mark.env_onecard
  87. def test_parameter_2_2():
  88. """
  89. Feature: Check the names of parameters.
  90. Description: Check the name of parameter in init.
  91. Expectation: No exception.
  92. """
  93. class ParamNet(Cell):
  94. def __init__(self):
  95. super(ParamNet, self).__init__()
  96. self.param_a = Parameter(Tensor([1], ms.float32), name="name_a")
  97. self.res1 = ParameterTuple((Parameter(Tensor([2], ms.float32)), self.param_a))
  98. self.param_a = Parameter(Tensor([3], ms.float32), name="name_a")
  99. self.res2 = self.res1[0] + self.param_a
  100. def construct(self):
  101. return self.param_a + self.res1[0] + self.res2
  102. with pytest.raises(ValueError, match="its name 'name_a' already exists."):
  103. net = ParamNet()
  104. res = net()
  105. assert res == 10
  106. @pytest.mark.level1
  107. @pytest.mark.platform_arm_ascend_training
  108. @pytest.mark.platform_x86_ascend_training
  109. @pytest.mark.env_onecard
  110. def test_parameter_3():
  111. """
  112. Feature: Check the names of parameters.
  113. Description: Check the name of parameter in init.
  114. Expectation: No exception.
  115. """
  116. class ParamNet(Cell):
  117. def __init__(self):
  118. super(ParamNet, self).__init__()
  119. self.param_a = Parameter(Tensor([1], ms.float32))
  120. self.param_b = Parameter(Tensor([2], ms.float32))
  121. def construct(self):
  122. return self.param_a + self.param_b
  123. net = ParamNet()
  124. res = net()
  125. assert res == 3
  126. @pytest.mark.level1
  127. @pytest.mark.platform_arm_ascend_training
  128. @pytest.mark.platform_x86_ascend_training
  129. @pytest.mark.env_onecard
  130. def test_parameter_4():
  131. """
  132. Feature: Check the names of parameters.
  133. Description: Check the name of parameter in init.
  134. Expectation: No exception.
  135. """
  136. class ParamNet(Cell):
  137. def __init__(self):
  138. super(ParamNet, self).__init__()
  139. self.res1 = ParameterTuple((Parameter(Tensor([2], ms.float32), name="name_a"),
  140. Parameter(Tensor([4], ms.float32), name="name_a")))
  141. def construct(self):
  142. return self.res1[0] + self.res1[1]
  143. with pytest.raises(ValueError, match="its name 'name_a' already exists."):
  144. net = ParamNet()
  145. res = net()
  146. assert res == 6
  147. @pytest.mark.level1
  148. @pytest.mark.platform_arm_ascend_training
  149. @pytest.mark.platform_x86_ascend_training
  150. @pytest.mark.env_onecard
  151. def test_parameter_5_1():
  152. """
  153. Feature: Check the names of parameters.
  154. Description: Check the name of parameter in init.
  155. Expectation: No exception.
  156. """
  157. class ParamNet(Cell):
  158. def __init__(self):
  159. super(ParamNet, self).__init__()
  160. self.res1 = ParameterTuple((Parameter(Tensor([2], ms.float32)), Parameter(Tensor([4], ms.float32))))
  161. def construct(self):
  162. return self.res1[0] + self.res1[1]
  163. with pytest.raises(ValueError, match="its name 'Parameter' already exists."):
  164. net = ParamNet()
  165. res = net()
  166. assert res == 6
  167. @pytest.mark.level1
  168. @pytest.mark.platform_arm_ascend_training
  169. @pytest.mark.platform_x86_ascend_training
  170. @pytest.mark.env_onecard
  171. def test_parameter_5_2():
  172. """
  173. Feature: Check the names of parameters.
  174. Description: Check the name of parameter in init.
  175. Expectation: No exception.
  176. """
  177. class ParamNet(Cell):
  178. def __init__(self):
  179. super(ParamNet, self).__init__()
  180. self.param_a = Parameter(Tensor([1], ms.float32), name="name_a")
  181. self.res1 = ParameterTuple((Parameter(Tensor([2], ms.float32)), self.param_a))
  182. self.param_a = Parameter(Tensor([3], ms.float32), name="name_b")
  183. self.res2 = self.res1[0] + self.param_a
  184. def construct(self):
  185. return self.param_a + self.res1[0] + self.res2
  186. net = ParamNet()
  187. res = net()
  188. assert res == 10
  189. @pytest.mark.level1
  190. @pytest.mark.platform_arm_ascend_training
  191. @pytest.mark.platform_x86_ascend_training
  192. @pytest.mark.env_onecard
  193. def test_parameter_list_tuple_no_name():
  194. """
  195. Feature: Check the names of parameters.
  196. Description: Check the name of parameter in init.
  197. Expectation: No exception.
  198. """
  199. class ParamNet(Cell):
  200. def __init__(self):
  201. super(ParamNet, self).__init__()
  202. self.param_tuple = (Parameter(Tensor([5], ms.float32)), Parameter(Tensor([6], ms.float32)))
  203. self.param_list = [Parameter(Tensor([7], ms.float32)), Parameter(Tensor([8], ms.float32))]
  204. def construct(self):
  205. return self.param_tuple[0] + self.param_tuple[1] + self.param_list[0] + self.param_list[1]
  206. net = ParamNet()
  207. res = net()
  208. assert res == 26
  209. @pytest.mark.level1
  210. @pytest.mark.platform_arm_ascend_training
  211. @pytest.mark.platform_x86_ascend_training
  212. @pytest.mark.env_onecard
  213. def test_parameter_in_tuple():
  214. """
  215. Feature: Check the names of parameters.
  216. Description: Check the name of parameter in init.
  217. Expectation: No exception.
  218. """
  219. class ParamNet(Cell):
  220. def __init__(self):
  221. super(ParamNet, self).__init__()
  222. self.param_a = Parameter(Tensor([1], ms.float32), name="name_a")
  223. self.param_b = Parameter(Tensor([2], ms.float32), name="name_b")
  224. self.param_tuple = ParameterTuple((self.param_a, self.param_b))
  225. def construct(self):
  226. return self.param_a + self.param_b + self.param_tuple[0] + self.param_tuple[1]
  227. net = ParamNet()
  228. res = net()
  229. assert res == 6
  230. @pytest.mark.level1
  231. @pytest.mark.platform_arm_ascend_training
  232. @pytest.mark.platform_x86_ascend_training
  233. @pytest.mark.env_onecard
  234. def test_parameter_parameter_tuple_1():
  235. """
  236. Feature: Check the names of parameters.
  237. Description: Check the name of parameter in init.
  238. Expectation: No exception.
  239. """
  240. class ParamNet(Cell):
  241. def __init__(self):
  242. super(ParamNet, self).__init__()
  243. self.param_a = Parameter(Tensor([1], ms.float32), name="name_a")
  244. self.param_tuple = ParameterTuple((Parameter(Tensor([5], ms.float32), name="name_a"),
  245. Parameter(Tensor([5], ms.float32), name="name_b")))
  246. def construct(self):
  247. return self.param_a + self.param_tuple[0] + self.param_tuple[1]
  248. with pytest.raises(ValueError, match="its name 'name_a' already exists."):
  249. net = ParamNet()
  250. res = net()
  251. assert res == 11
  252. @pytest.mark.level1
  253. @pytest.mark.platform_arm_ascend_training
  254. @pytest.mark.platform_x86_ascend_training
  255. @pytest.mark.env_onecard
  256. def test_parameter_parameter_tuple_2():
  257. """
  258. Feature: Check the names of parameters.
  259. Description: Check the name of parameter in init.
  260. Expectation: No exception.
  261. """
  262. class ParamNet(Cell):
  263. def __init__(self):
  264. super(ParamNet, self).__init__()
  265. self.param_a = Parameter(Tensor([1], ms.float32), name="name_a")
  266. self.param_tuple = ParameterTuple((self.param_a, self.param_a, self.param_a))
  267. def construct(self):
  268. return self.param_a + self.param_tuple[0] + self.param_tuple[1] + self.param_tuple[2]
  269. net = ParamNet()
  270. res = net()
  271. assert res == 4
  272. @pytest.mark.level1
  273. @pytest.mark.platform_arm_ascend_training
  274. @pytest.mark.platform_x86_gpu_training
  275. @pytest.mark.env_onecard
  276. def test_parameter():
  277. """
  278. Feature: Check the names of parameters.
  279. Description: If parameter in list or tuple is not given a name, will give it a unique name.
  280. Expectation: No exception.
  281. """
  282. class ParamNet(Cell):
  283. def __init__(self):
  284. super(ParamNet, self).__init__()
  285. self.param_a = Parameter(Tensor([1], ms.float32), name="name_a")
  286. self.param_b = Parameter(Tensor([2], ms.float32), name="name_b")
  287. self.param_c = Parameter(Tensor([3], ms.float32))
  288. self.param_d = Parameter(Tensor([4], ms.float32))
  289. self.param_tuple = (Parameter(Tensor([5], ms.float32)),
  290. Parameter(Tensor([6], ms.float32)))
  291. self.param_list = [Parameter(Tensor([5], ms.float32)),
  292. Parameter(Tensor([6], ms.float32))]
  293. def construct(self, x):
  294. res1 = self.param_a + self.param_b + self.param_c + self.param_d
  295. res1 = res1 - self.param_list[0] + self.param_list[1] + x
  296. res2 = self.param_list[0] + self.param_list[1]
  297. return res1, res2
  298. net = ParamNet()
  299. x = Tensor([10], ms.float32)
  300. output1, output2 = net(x)
  301. output1_expect = Tensor(21, ms.float32)
  302. output2_expect = Tensor(11, ms.float32)
  303. assert output1 == output1_expect
  304. assert output2 == output2_expect
  305. @pytest.mark.level1
  306. @pytest.mark.platform_arm_ascend_training
  307. @pytest.mark.platform_x86_gpu_training
  308. @pytest.mark.env_onecard
  309. def test_parameter_same_name_between_tuple_or_list():
  310. """
  311. Feature: Check the names of parameters between tuple or list.
  312. Description: If the same name exists between tuple and list, an exception will be thrown.
  313. Expectation: Get the expected exception report.
  314. """
  315. class ParamNet(Cell):
  316. def __init__(self):
  317. super(ParamNet, self).__init__()
  318. self.param_tuple = (Parameter(Tensor([1], ms.float32), name="name_a"),
  319. Parameter(Tensor([2], ms.float32)))
  320. self.param_list = [Parameter(Tensor([3], ms.float32), name="name_a"),
  321. Parameter(Tensor([4], ms.float32))]
  322. def construct(self, x):
  323. res = self.param_tuple[0] + self.param_tuple[1] + self.param_list[0] + self.param_listp[1] + x
  324. return res
  325. with pytest.raises(ValueError, match="its name 'name_a' already exists."):
  326. net = ParamNet()
  327. x = Tensor([10], ms.float32)
  328. output = net(x)
  329. output_expect = Tensor(20, ms.float32)
  330. assert output == output_expect