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_control_ops.py 12 kB

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446
  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 control ops """
  16. import numpy as np
  17. import pytest
  18. import mindspore as ms
  19. from mindspore import Tensor
  20. from mindspore import context
  21. from mindspore import nn
  22. from mindspore.common.parameter import Parameter, ParameterTuple
  23. from mindspore.ops import composite as C
  24. from mindspore.ops import functional as F
  25. from mindspore.ops import operations as P
  26. context.set_context(mode=context.GRAPH_MODE)
  27. def cond_data_test(x_init, y_init):
  28. class Net(nn.Cell):
  29. def __init__(self):
  30. """"""
  31. super(Net, self).__init__()
  32. self.square = P.Square()
  33. self.add = P.TensorAdd()
  34. self.value = Tensor(3, dtype=ms.float32)
  35. self.switch = P.GeSwitch()
  36. self.merge = P.Merge()
  37. self.less = P.Less()
  38. def construct(self, x, y):
  39. cond = self.less(x, y)
  40. st1, sf1 = self.switch(x, cond)
  41. st2, sf2 = self.switch(y, cond)
  42. add_ret = self.add(st1, st2)
  43. st3, sf3 = self.switch(self.value, cond)
  44. sq_ret = self.square(sf3)
  45. ret = self.merge((add_ret, sq_ret))
  46. return ret[0]
  47. x = Tensor(x_init, dtype=ms.float32)
  48. y = Tensor(y_init, dtype=ms.float32)
  49. net = Net()
  50. output = net(x, y)
  51. return output
  52. def test_cond_data_true():
  53. output = cond_data_test(3, 8)
  54. print("test_cond_data_true:", output)
  55. def test_cond_data_false():
  56. output = cond_data_test(8, 3)
  57. print("test_cond_data_false:", output)
  58. def if_compile_test(x_init, y_init):
  59. class Net(nn.Cell):
  60. def __init__(self):
  61. """"""
  62. super(Net, self).__init__()
  63. self.square = P.Square()
  64. self.add = P.TensorAdd()
  65. self.value = Tensor(3, dtype=ms.float32)
  66. self.switch = P.GeSwitch()
  67. self.merge = P.Merge()
  68. self.less = P.Less()
  69. def construct(self, x, y):
  70. cond = self.less(x, y)
  71. ret = self.value
  72. if cond:
  73. ret = self.add(x, ret)
  74. ret = self.add(y, ret)
  75. else:
  76. ret = self.square(self.value)
  77. return ret
  78. x = Tensor(x_init, dtype=ms.float32)
  79. y = Tensor(y_init, dtype=ms.float32)
  80. net = Net()
  81. output = net(x, y)
  82. return output
  83. def test_if_none():
  84. class Net(nn.Cell):
  85. def __init__(self, z: None):
  86. """"""
  87. super(Net, self).__init__()
  88. self.z = z
  89. def construct(self, x, y):
  90. if self.z:
  91. ret = x
  92. else:
  93. ret = y
  94. return ret
  95. x = Tensor(np.ones([6, 8, 10], np.int32))
  96. y = Tensor(np.zeros([3, 4, 5], np.int32))
  97. z = None
  98. net = Net(z)
  99. assert net(x, y) == y
  100. def test_if_str_is_not_none_right():
  101. class Net(nn.Cell):
  102. def __init__(self, z: str):
  103. """"""
  104. super(Net, self).__init__()
  105. self.z = z
  106. def construct(self, x, y):
  107. if self.z == None:
  108. ret = x
  109. else:
  110. ret = y
  111. return ret
  112. x = Tensor(np.ones([6, 8, 10], np.int32))
  113. y = Tensor(np.zeros([3, 4, 5], np.int32))
  114. z = "ok"
  115. net = Net(z)
  116. assert net(x, y) == y
  117. def test_if_str_is_not_none_left():
  118. class Net(nn.Cell):
  119. def __init__(self, z: str):
  120. """"""
  121. super(Net, self).__init__()
  122. self.z = z
  123. def construct(self, x, y):
  124. if None == self.z:
  125. ret = x
  126. else:
  127. ret = y
  128. return ret
  129. x = Tensor(np.ones([6, 8, 10], np.int32))
  130. y = Tensor(np.zeros([3, 4, 5], np.int32))
  131. z = "ok"
  132. net = Net(z)
  133. assert net(x, y) == y
  134. def test_if_none_equal_none():
  135. class Net(nn.Cell):
  136. def __init__(self, z: None):
  137. """"""
  138. super(Net, self).__init__()
  139. self.z = z
  140. def construct(self, x, y):
  141. if self.z == None:
  142. ret = x
  143. else:
  144. ret = y
  145. return ret
  146. x = Tensor(np.ones([6, 8, 10], np.int32))
  147. y = Tensor(np.zeros([3, 4, 5], np.int32))
  148. z = None
  149. net = Net(z)
  150. assert net(x, y) == x
  151. def test_if_str_is_null():
  152. class Net(nn.Cell):
  153. def __init__(self, z: str):
  154. """"""
  155. super(Net, self).__init__()
  156. self.z = z
  157. def construct(self, x, y):
  158. if self.z:
  159. ret = x
  160. else:
  161. ret = y
  162. return ret
  163. x = Tensor(np.ones([6, 8, 10], np.int32))
  164. y = Tensor(np.zeros([3, 4, 5], np.int32))
  165. z = ""
  166. net = Net(z)
  167. assert net(x, y) == y
  168. def test_if_str_is_true():
  169. class Net(nn.Cell):
  170. def __init__(self, z: str):
  171. """"""
  172. super(Net, self).__init__()
  173. self.z = z
  174. def construct(self, x, y):
  175. if self.z:
  176. ret = x
  177. else:
  178. ret = y
  179. return ret
  180. x = Tensor(np.ones([6, 9, 10], np.int32))
  181. y = Tensor(np.zeros([3, 4, 5], np.int32))
  182. z = "ok"
  183. net = Net(z)
  184. assert net(x, y) == x
  185. def test_if_str_equal():
  186. class Net(nn.Cell):
  187. def __init__(self, z: str):
  188. """"""
  189. super(Net, self).__init__()
  190. self.z = z
  191. def construct(self, x, y):
  192. if self.z == "ok":
  193. ret = x
  194. else:
  195. ret = y
  196. return ret
  197. x = Tensor(np.ones([6, 8, 10], np.int32))
  198. y = Tensor(np.zeros([3, 4, 5], np.int32))
  199. z = "ok"
  200. net = Net(z)
  201. assert net(x, y) == x
  202. def test_if_tuple_is_null():
  203. class Net(nn.Cell):
  204. def __init__(self, z: tuple):
  205. """"""
  206. super(Net, self).__init__()
  207. self.z = z
  208. def construct(self, x, y):
  209. if self.z:
  210. ret = x
  211. else:
  212. ret = y
  213. return ret
  214. x = Tensor(np.ones([6, 8, 10], np.int32))
  215. y = Tensor(np.zeros([3, 4, 5], np.int32))
  216. z = ()
  217. net = Net(z)
  218. assert net(x, y) == y
  219. def test_if_tuple_is_not_null():
  220. class Net(nn.Cell):
  221. def __init__(self, z: tuple):
  222. """"""
  223. super(Net, self).__init__()
  224. self.z = z
  225. def construct(self, x, y):
  226. if self.z:
  227. ret = x
  228. else:
  229. ret = y
  230. return ret
  231. x = Tensor(np.ones([6, 8, 10], np.int32))
  232. y = Tensor(np.zeros([3, 4, 5], np.int32))
  233. z = (1, 2, 3)
  234. net = Net(z)
  235. assert net(x, y) == x
  236. def test_if_dict_is_null():
  237. class Net(nn.Cell):
  238. def __init__(self, z: dict):
  239. """"""
  240. super(Net, self).__init__()
  241. self.z = z
  242. def construct(self, x, y):
  243. if self.z:
  244. ret = x
  245. else:
  246. ret = y
  247. return ret
  248. x = Tensor(np.ones([6, 8, 10], np.int32))
  249. y = Tensor(np.zeros([3, 4, 5], np.int32))
  250. z = {}
  251. net = Net(z)
  252. assert net(x, y) == y
  253. def test_if_dict_is_not_null():
  254. class Net(nn.Cell):
  255. def __init__(self, z: dict):
  256. """"""
  257. super(Net, self).__init__()
  258. self.z = z
  259. def construct(self, x, y):
  260. if self.z:
  261. ret = x
  262. else:
  263. ret = y
  264. return ret
  265. x = Tensor(np.ones([6, 8, 10], np.int32))
  266. y = Tensor(np.zeros([3, 4, 5], np.int32))
  267. z = {"one": 1, "two": 2}
  268. net = Net(z)
  269. assert net(x, y) == x
  270. def test_if_else_assign():
  271. class Net(nn.Cell):
  272. def __init__(self, m: list):
  273. """"""
  274. super(Net, self).__init__()
  275. self.m = m
  276. self.n = [4, 5, 6]
  277. def construct(self, x, y):
  278. exp_1 = self.m if self.m else self.n
  279. exp_2 = self.m if exp_1 == self.n else self.n
  280. if exp_2 == self.m:
  281. if self.m:
  282. ret = x
  283. else:
  284. ret = y
  285. else:
  286. if self.m:
  287. ret = x
  288. else:
  289. ret = y
  290. return ret
  291. x = Tensor(np.ones([6, 8, 10], np.int32))
  292. y = Tensor(np.zeros([3, 4, 5], np.int32))
  293. z = [1, 2]
  294. net = Net(z)
  295. assert net(x, y) == x
  296. def test_if_compile_true():
  297. output = if_compile_test(3, 8)
  298. print("test_if_compile_true:", output)
  299. def test_if_compile_false():
  300. output = if_compile_test(8, 3)
  301. print("test_if_compile_false:", output)
  302. def test_switch_layer():
  303. class Layer1(nn.Cell):
  304. def __init__(self):
  305. super(Layer1, self).__init__()
  306. self.z1 = Parameter(Tensor(np.full([128, 96], 0.6, dtype=np.float32)), name='z1')
  307. def construct(self, x):
  308. return x * self.z1
  309. class Layer2(nn.Cell):
  310. def __init__(self):
  311. super(Layer2, self).__init__()
  312. self.z2 = Parameter(Tensor(np.full([128, 96], 0.6, dtype=np.float32)), name='z2')
  313. def construct(self, x):
  314. return x * self.z2
  315. class SwitchLayerCell(nn.Cell):
  316. def __init__(self):
  317. super(SwitchLayerCell, self).__init__()
  318. self.layers = (Layer1(), Layer2())
  319. self.z3 = Parameter(Tensor(np.full([128, 96], 0.6, dtype=np.float32)), name='z3')
  320. def construct(self, index, x):
  321. ret = F.switch_layer(index, self.layers)(x) * self.z3
  322. return ret
  323. index = Tensor(0)
  324. net = SwitchLayerCell()
  325. net(index, Tensor(np.full([128, 96], 0.6, dtype=np.float32)))
  326. C.grad_by_list(net, ParameterTuple(net.trainable_params()))(index,
  327. Tensor(np.full([128, 96], 0.6, dtype=np.float32)))
  328. C.grad_all(net)(index, Tensor(np.full([128, 96], 0.6, dtype=np.float32)))
  329. def test_index_to_switch_layer():
  330. class Layer1(nn.Cell):
  331. def __init__(self):
  332. super(Layer1, self).__init__()
  333. self.z1 = Parameter(Tensor(np.full([128, 96], 0.6, dtype=np.float32)), name='z1')
  334. def construct(self, x):
  335. return x * self.z1
  336. class Layer2(nn.Cell):
  337. def __init__(self):
  338. super(Layer2, self).__init__()
  339. self.z2 = Parameter(Tensor(np.full([128, 96], 0.6, dtype=np.float32)), name='z2')
  340. def construct(self, x):
  341. return x * self.z2
  342. class SwitchLayerCell(nn.Cell):
  343. def __init__(self):
  344. super(SwitchLayerCell, self).__init__()
  345. self.layers = (Layer1(), Layer2())
  346. self.z3 = Parameter(Tensor(np.full([128, 96], 0.6, dtype=np.float32)), name='z3')
  347. def construct(self, index, x):
  348. ret = self.layers[index](x) * self.z3
  349. return ret
  350. index = Tensor(0)
  351. net = SwitchLayerCell()
  352. net(index, Tensor(np.full([128, 96], 0.6, dtype=np.float32)))
  353. C.grad_by_list(net, ParameterTuple(net.trainable_params()))(index,
  354. Tensor(np.full([128, 96], 0.6, dtype=np.float32)))
  355. C.grad_all(net)(index, Tensor(np.full([128, 96], 0.6, dtype=np.float32)))
  356. def test_control_depend_check():
  357. with pytest.raises(TypeError) as e:
  358. depend = P.ControlDepend(0.0)
  359. with pytest.raises(ValueError) as e:
  360. depend = P.ControlDepend(2)
  361. with pytest.raises(TypeError) as e:
  362. depend = P.ControlDepend((2,))