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_ascend_control_sink.py 9.9 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  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_ascend_control_sink """
  16. import pytest
  17. import numpy as np
  18. import mindspore.context as context
  19. import mindspore.nn as nn
  20. from mindspore.ops import operations as op
  21. from mindspore.common import dtype as mstype
  22. from mindspore.common.tensor import Tensor
  23. from mindspore.common.parameter import Parameter
  24. from mindspore.common.initializer import initializer
  25. class ControlSimpleIf(nn.Cell):
  26. def __init__(self):
  27. super().__init__()
  28. self.addn = op.AddN()
  29. def construct(self, x, y, z, input1, input2):
  30. addn1 = self.addn([input1, input1, input1])
  31. addn2 = self.addn([input2, input2, input2])
  32. addn11 = self.addn([addn1, addn1, addn1])
  33. addn22 = self.addn([addn2, addn2, addn2])
  34. cond1 = x > y
  35. cond2 = y > z
  36. # dodge pylint
  37. if cond1 and cond2:
  38. out = self.addn([addn11, addn11])
  39. else:
  40. out = self.addn([addn22, addn22])
  41. out_me = self.addn([out, input1])
  42. return out_me
  43. class ControlSimpleIfWithAssign(nn.Cell):
  44. def __init__(self, input_shape):
  45. super().__init__()
  46. self.addn = op.AddN()
  47. self.assign = op.Assign()
  48. self.input_data = Parameter(initializer(1, input_shape, mstype.float32), name="var")
  49. def construct(self, x, y, input_data):
  50. if x > y:
  51. out = self.addn([input_data, input_data, input_data])
  52. else:
  53. out = self.assign(self.input_data, input_data)
  54. return out
  55. class ControlIfinIf(nn.Cell):
  56. """pass"""
  57. def construct(self, x, y):
  58. if x > y:
  59. x = x + 1
  60. if y < 0:
  61. y = y + 1
  62. else:
  63. y = y + 2
  64. else:
  65. x = x + 2
  66. x = x + y
  67. return x
  68. class ControlIfbyIfbyIf(nn.Cell):
  69. def __init__(self):
  70. super().__init__()
  71. self.addn = op.AddN()
  72. def construct(self, x, y, cond1, cond2, input_data):
  73. tri_in = self.addn([input_data, input_data, input_data])
  74. if x > y:
  75. addn_1 = self.addn([tri_in, tri_in])
  76. else:
  77. addn_1 = self.addn([tri_in, tri_in, tri_in])
  78. if cond1:
  79. addn_2 = self.addn([addn_1, addn_1])
  80. else:
  81. addn_2 = self.addn([addn_1, addn_1, addn_1])
  82. if cond2:
  83. out = self.addn([addn_2, addn_2, addn_2])
  84. else:
  85. out = self.addn([addn_2, addn_2])
  86. return out
  87. class ControlSimpleWhile(nn.Cell):
  88. def __init__(self):
  89. super().__init__()
  90. self.addn = op.AddN()
  91. def construct(self, x, y, input_data):
  92. out = input_data
  93. while x:
  94. out = self.addn([input_data, input_data, input_data])
  95. x = y
  96. return out
  97. class ControlMixedWhileIf(nn.Cell):
  98. def __init__(self):
  99. super().__init__()
  100. self.assign = op.Assign()
  101. self.var = Parameter(initializer(1, (1), mstype.float32), name="var")
  102. def construct(self, x, y, z, c2, c4):
  103. out = c4
  104. self.assign(self.var, c4)
  105. while x < c2:
  106. y = c4
  107. self.assign(self.var, c4)
  108. while y < c2 and x < c2:
  109. if 2 * y < c2:
  110. y = y + 2
  111. else:
  112. y = y + 1
  113. out = out + y
  114. z = c4
  115. self.assign(self.var, c4)
  116. while z < c2:
  117. z = z + 1
  118. out = out + z
  119. x = x + 1
  120. out = out + x
  121. while x < 2 * c2:
  122. y = c4
  123. self.assign(self.var, c4)
  124. x = x + 1
  125. while y < c2:
  126. z = c4
  127. self.assign(self.var, c4)
  128. while z < c2:
  129. z = z + 1
  130. if x < c2:
  131. y = y - 1
  132. else:
  133. y = y + 1
  134. out = out + z
  135. out = out + y
  136. out = out + x
  137. return out
  138. class AndOperation(nn.Cell):
  139. def __init__(self):
  140. super().__init__()
  141. self.reduce_sum = op.ReduceSum()
  142. def construct(self, x, y):
  143. x_sum = self.reduce_sum(x)
  144. y_sum = self.reduce_sum(y)
  145. out = x_sum and y_sum
  146. return out
  147. class OrOperation(nn.Cell):
  148. def __init__(self):
  149. super().__init__()
  150. self.reduce_sum = op.ReduceSum()
  151. def construct(self, x, y):
  152. x_sum = self.reduce_sum(x)
  153. y_sum = self.reduce_sum(y)
  154. out = x_sum or y_sum
  155. return out
  156. class NotOperation(nn.Cell):
  157. def __init__(self):
  158. super().__init__()
  159. self.reduce_sum = op.ReduceSum()
  160. def construct(self, x):
  161. x_sum = self.reduce_sum(x)
  162. return not x_sum
  163. @pytest.mark.level0
  164. @pytest.mark.platform_arm_ascend_training
  165. @pytest.mark.platform_x86_ascend_training
  166. @pytest.mark.env_onecard
  167. def test_simple_if():
  168. context.set_context(mode=context.GRAPH_MODE, device_target="Ascend")
  169. x = np.array(3).astype(np.float32)
  170. y = np.array(2).astype(np.float32)
  171. z = np.array(3).astype(np.float32)
  172. input_shape = (127, 7, 53, 31)
  173. input1 = np.random.randn(*input_shape).astype(np.float32)
  174. input2 = np.random.randn(*input_shape).astype(np.float32)
  175. net = ControlSimpleIf()
  176. output = net(Tensor(x), Tensor(y), Tensor(z), Tensor(input1), Tensor(input2))
  177. expect = input2 * 3 * 3 * 2 + input1
  178. assert np.allclose(expect, output.asnumpy(), 0.0001, 0.0001)
  179. @pytest.mark.level0
  180. @pytest.mark.platform_arm_ascend_training
  181. @pytest.mark.platform_x86_ascend_training
  182. @pytest.mark.env_onecard
  183. def test_simple_if_with_assign():
  184. context.set_context(mode=context.GRAPH_MODE, device_target="Ascend")
  185. x = np.array(0).astype(np.float32)
  186. y = np.array(1).astype(np.float32)
  187. input_shape = (127, 7, 53, 31)
  188. input_data = np.random.randn(*input_shape).astype(np.float32)
  189. net = ControlSimpleIfWithAssign(input_shape)
  190. output = net(Tensor(x), Tensor(y), Tensor(input_data))
  191. expect = input_data
  192. assert np.allclose(expect, output.asnumpy(), 0.0001, 0.0001)
  193. @pytest.mark.level0
  194. @pytest.mark.platform_arm_ascend_training
  195. @pytest.mark.platform_x86_ascend_training
  196. @pytest.mark.env_onecard
  197. def test_if_in_if():
  198. context.set_context(mode=context.GRAPH_MODE, device_target="Ascend")
  199. x = np.array(2.345678).astype(np.float32)
  200. y = np.array(1.234567).astype(np.float32)
  201. net = ControlIfinIf()
  202. output = net(Tensor(x), Tensor(y))
  203. expect = x + y + 3
  204. assert np.allclose(expect, output.asnumpy(), 0.0001, 0.0001)
  205. @pytest.mark.level0
  206. @pytest.mark.platform_arm_ascend_training
  207. @pytest.mark.platform_x86_ascend_training
  208. @pytest.mark.env_onecard
  209. def test_if_by_if_by_if():
  210. context.set_context(mode=context.GRAPH_MODE, device_target="Ascend")
  211. x = np.array(2.345678).astype(np.float32)
  212. y = np.array(1.234567).astype(np.float32)
  213. cond1 = np.array(True).astype(np.bool)
  214. cond2 = np.array(False).astype(np.bool)
  215. input_shape = (127, 7, 53, 31)
  216. input_data = np.random.randn(*input_shape).astype(np.float32)
  217. net = ControlIfbyIfbyIf()
  218. output = net(Tensor(x), Tensor(y), Tensor(cond1), Tensor(cond2), Tensor(input_data))
  219. expect = input_data * 3 * 2 * 2 * 2
  220. assert np.allclose(expect, output.asnumpy(), 0.0001, 0.0001)
  221. @pytest.mark.level0
  222. @pytest.mark.platform_arm_ascend_training
  223. @pytest.mark.platform_x86_ascend_training
  224. @pytest.mark.env_onecard
  225. def test_simple_while():
  226. context.set_context(mode=context.GRAPH_MODE, device_target="Ascend")
  227. x = np.array(True).astype(np.bool)
  228. y = np.array(False).astype(np.bool)
  229. input_shape = (127, 7, 53, 31)
  230. input_data = np.random.randn(*input_shape).astype(np.float32)
  231. net = ControlSimpleWhile()
  232. output = net(Tensor(x), Tensor(y), Tensor(input_data))
  233. expect = input_data * 3
  234. assert np.allclose(expect, output.asnumpy(), 0.0001, 0.0001)
  235. @pytest.mark.level0
  236. @pytest.mark.platform_arm_ascend_training
  237. @pytest.mark.platform_x86_ascend_training
  238. @pytest.mark.env_onecard
  239. def test_mixed_while_if():
  240. context.set_context(mode=context.GRAPH_MODE, device_target="Ascend")
  241. x = np.array(2).astype(np.int32)
  242. y = np.array(14).astype(np.int32)
  243. z = np.array(1).astype(np.int32)
  244. c2 = Tensor([14], mstype.int32)
  245. c4 = Tensor([0], mstype.int32)
  246. net = ControlMixedWhileIf()
  247. output = net(Tensor(x), Tensor(y), Tensor(z), c2, c4)
  248. expect = np.array(3318).astype(np.int32)
  249. assert np.allclose(expect, output.asnumpy(), 0.0001, 0.0001)
  250. @pytest.mark.level0
  251. @pytest.mark.platform_arm_ascend_training
  252. @pytest.mark.platform_x86_ascend_training
  253. @pytest.mark.env_onecard
  254. def test_and_or_operation():
  255. context.set_context(mode=context.GRAPH_MODE, device_target="Ascend")
  256. x = np.array([0, 1]).astype(np.float32)
  257. y = np.array([0, 0]).astype(np.float32)
  258. net = AndOperation()
  259. output = net(Tensor(x), Tensor(y))
  260. expect = np.sum(x) and np.sum(y)
  261. assert np.allclose(expect, output.asnumpy(), 0.0001, 0.0001)
  262. net = OrOperation()
  263. output = net(Tensor(x), Tensor(y))
  264. expect = np.sum(x) or np.sum(y)
  265. assert np.allclose(expect, output.asnumpy(), 0.0001, 0.0001)
  266. net = NotOperation()
  267. output = net(Tensor(x))
  268. expect = not np.sum(x)
  269. assert np.allclose(expect, output.asnumpy(), 0.0001, 0.0001)