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

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