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 10 kB

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