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.8 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  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 = self.assign(self.var, c4)
  104. while x < c2:
  105. y = self.assign(self.var, c4)
  106. while y < c2 and x < c2:
  107. if 2 * y < c2:
  108. y = y + 2
  109. else:
  110. y = y + 1
  111. out = out + y
  112. z = self.assign(self.var, c4)
  113. while z < c2:
  114. z = z + 1
  115. out = out + z
  116. x = x + 1
  117. out = out + x
  118. while x < 2 * c2:
  119. y = self.assign(self.var, c4)
  120. x = x + 1
  121. while y < c2:
  122. z = self.assign(self.var, c4)
  123. while z < c2:
  124. z = z + 1
  125. if x < c2:
  126. y = y - 1
  127. else:
  128. y = y + 1
  129. out = out + z
  130. out = out + y
  131. out = out + x
  132. return out
  133. class AndOperation(nn.Cell):
  134. def __init__(self):
  135. super().__init__()
  136. self.reduce_sum = op.ReduceSum()
  137. def construct(self, x, y):
  138. x_sum = self.reduce_sum(x)
  139. y_sum = self.reduce_sum(y)
  140. out = x_sum and y_sum
  141. return out
  142. class OrOperation(nn.Cell):
  143. def __init__(self):
  144. super().__init__()
  145. self.reduce_sum = op.ReduceSum()
  146. def construct(self, x, y):
  147. x_sum = self.reduce_sum(x)
  148. y_sum = self.reduce_sum(y)
  149. out = x_sum or y_sum
  150. return out
  151. class NotOperation(nn.Cell):
  152. def __init__(self):
  153. super().__init__()
  154. self.reduce_sum = op.ReduceSum()
  155. def construct(self, x):
  156. x_sum = self.reduce_sum(x)
  157. return not x_sum
  158. @pytest.mark.level0
  159. @pytest.mark.platform_arm_ascend_training
  160. @pytest.mark.platform_x86_ascend_training
  161. @pytest.mark.env_onecard
  162. def test_simple_if():
  163. context.set_context(mode=context.GRAPH_MODE, device_target="Ascend")
  164. x = np.array(3).astype(np.float32)
  165. y = np.array(2).astype(np.float32)
  166. z = np.array(3).astype(np.float32)
  167. input_shape = (127, 7, 53, 31)
  168. input1 = np.random.randn(*input_shape).astype(np.float32)
  169. input2 = np.random.randn(*input_shape).astype(np.float32)
  170. net = ControlSimpleIf()
  171. output = net(Tensor(x), Tensor(y), Tensor(z), Tensor(input1), Tensor(input2))
  172. expect = input2 * 3 * 3 * 2 + input1
  173. assert np.allclose(expect, output.asnumpy(), 0.0001, 0.0001)
  174. @pytest.mark.level0
  175. @pytest.mark.platform_arm_ascend_training
  176. @pytest.mark.platform_x86_ascend_training
  177. @pytest.mark.env_onecard
  178. def test_simple_if_with_assign():
  179. context.set_context(mode=context.GRAPH_MODE, device_target="Ascend")
  180. x = np.array(0).astype(np.float32)
  181. y = np.array(1).astype(np.float32)
  182. input_shape = (127, 7, 53, 31)
  183. input_data = np.random.randn(*input_shape).astype(np.float32)
  184. net = ControlSimpleIfWithAssign(input_shape)
  185. output = net(Tensor(x), Tensor(y), Tensor(input_data))
  186. expect = input_data
  187. assert np.allclose(expect, output.asnumpy(), 0.0001, 0.0001)
  188. @pytest.mark.level0
  189. @pytest.mark.platform_arm_ascend_training
  190. @pytest.mark.platform_x86_ascend_training
  191. @pytest.mark.env_onecard
  192. def test_if_in_if():
  193. context.set_context(mode=context.GRAPH_MODE, device_target="Ascend")
  194. x = np.array(2.345678).astype(np.float32)
  195. y = np.array(1.234567).astype(np.float32)
  196. net = ControlIfinIf()
  197. output = net(Tensor(x), Tensor(y))
  198. expect = x + y + 3
  199. assert np.allclose(expect, output.asnumpy(), 0.0001, 0.0001)
  200. @pytest.mark.level0
  201. @pytest.mark.platform_arm_ascend_training
  202. @pytest.mark.platform_x86_ascend_training
  203. @pytest.mark.env_onecard
  204. def test_if_by_if_by_if():
  205. context.set_context(mode=context.GRAPH_MODE, device_target="Ascend")
  206. x = np.array(2.345678).astype(np.float32)
  207. y = np.array(1.234567).astype(np.float32)
  208. cond1 = np.array(True).astype(np.bool)
  209. cond2 = np.array(False).astype(np.bool)
  210. input_shape = (127, 7, 53, 31)
  211. input_data = np.random.randn(*input_shape).astype(np.float32)
  212. net = ControlIfbyIfbyIf()
  213. output = net(Tensor(x), Tensor(y), Tensor(cond1), Tensor(cond2), Tensor(input_data))
  214. expect = input_data * 3 * 2 * 2 * 2
  215. assert np.allclose(expect, output.asnumpy(), 0.0001, 0.0001)
  216. @pytest.mark.level0
  217. @pytest.mark.platform_arm_ascend_training
  218. @pytest.mark.platform_x86_ascend_training
  219. @pytest.mark.env_onecard
  220. def test_simple_while():
  221. context.set_context(mode=context.GRAPH_MODE, device_target="Ascend")
  222. x = np.array(True).astype(np.bool)
  223. y = np.array(False).astype(np.bool)
  224. input_shape = (127, 7, 53, 31)
  225. input_data = np.random.randn(*input_shape).astype(np.float32)
  226. net = ControlSimpleWhile()
  227. output = net(Tensor(x), Tensor(y), Tensor(input_data))
  228. expect = input_data * 3
  229. assert np.allclose(expect, output.asnumpy(), 0.0001, 0.0001)
  230. @pytest.mark.level0
  231. @pytest.mark.platform_arm_ascend_training
  232. @pytest.mark.platform_x86_ascend_training
  233. @pytest.mark.env_onecard
  234. def test_mixed_while_if():
  235. context.set_context(mode=context.GRAPH_MODE, device_target="Ascend")
  236. x = np.array(2).astype(np.int32)
  237. y = np.array(14).astype(np.int32)
  238. z = np.array(1).astype(np.int32)
  239. c2 = Tensor([14], mstype.int32)
  240. c4 = Tensor([0], mstype.int32)
  241. net = ControlMixedWhileIf()
  242. output = net(Tensor(x), Tensor(y), Tensor(z), c2, c4)
  243. expect = np.array(3318).astype(np.int32)
  244. assert np.allclose(expect, output.asnumpy(), 0.0001, 0.0001)
  245. @pytest.mark.level0
  246. @pytest.mark.platform_arm_ascend_training
  247. @pytest.mark.platform_x86_ascend_training
  248. @pytest.mark.env_onecard
  249. def test_and_or_operation():
  250. context.set_context(mode=context.GRAPH_MODE, device_target="Ascend")
  251. x = np.array([0, 1]).astype(np.float32)
  252. y = np.array([0, 0]).astype(np.float32)
  253. net = AndOperation()
  254. output = net(Tensor(x), Tensor(y))
  255. expect = np.sum(x) and np.sum(y)
  256. assert np.allclose(expect, output.asnumpy(), 0.0001, 0.0001)
  257. net = OrOperation()
  258. output = net(Tensor(x), Tensor(y))
  259. expect = np.sum(x) or np.sum(y)
  260. assert np.allclose(expect, output.asnumpy(), 0.0001, 0.0001)
  261. net = NotOperation()
  262. output = net(Tensor(x))
  263. expect = not np.sum(x)
  264. assert np.allclose(expect, output.asnumpy(), 0.0001, 0.0001)