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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  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. def __init__(self):
  57. super().__init__()
  58. def construct(self, x, y):
  59. if x > y:
  60. x = x + 1
  61. if y < 0:
  62. y = y + 1
  63. else:
  64. y = y + 2
  65. else:
  66. x = x + 2
  67. x = x + y
  68. return x
  69. class ControlIfbyIfbyIf(nn.Cell):
  70. def __init__(self):
  71. super().__init__()
  72. self.addn = op.AddN()
  73. def construct(self, x, y, cond1, cond2, input_data):
  74. tri_in = self.addn([input_data, input_data, input_data])
  75. if x > y:
  76. addn_1 = self.addn([tri_in, tri_in])
  77. else:
  78. addn_1 = self.addn([tri_in, tri_in, tri_in])
  79. if cond1:
  80. addn_2 = self.addn([addn_1, addn_1])
  81. else:
  82. addn_2 = self.addn([addn_1, addn_1, addn_1])
  83. if cond2:
  84. out = self.addn([addn_2, addn_2, addn_2])
  85. else:
  86. out = self.addn([addn_2, addn_2])
  87. return out
  88. class ControlSimpleWhile(nn.Cell):
  89. def __init__(self):
  90. super().__init__()
  91. self.addn = op.AddN()
  92. def construct(self, x, y, input_data):
  93. out = input_data
  94. while x:
  95. out = self.addn([input_data, input_data, input_data])
  96. x = y
  97. return out
  98. class ControlMixedWhileIf(nn.Cell):
  99. def __init__(self):
  100. super().__init__()
  101. self.assign = op.Assign()
  102. self.var = Parameter(initializer(1, (1), mstype.float32), name="var")
  103. def construct(self, x, y, z, c2, c4):
  104. out = self.assign(self.var, c4)
  105. while x < c2:
  106. y = self.assign(self.var, c4)
  107. while y < c2 and x < c2:
  108. if 2 * y < c2:
  109. y = y + 2
  110. else:
  111. y = y + 1
  112. out = out + y
  113. z = self.assign(self.var, c4)
  114. while z < c2:
  115. z = z + 1
  116. out = out + z
  117. x = x + 1
  118. out = out + x
  119. while x < 2 * c2:
  120. y = self.assign(self.var, c4)
  121. x = x + 1
  122. while y < c2:
  123. z = self.assign(self.var, c4)
  124. while z < c2:
  125. z = z + 1
  126. if x < c2:
  127. y = y - 1
  128. else:
  129. y = y + 1
  130. out = out + z
  131. out = out + y
  132. out = out + x
  133. return out
  134. @pytest.mark.level0
  135. @pytest.mark.platform_arm_ascend_training
  136. @pytest.mark.platform_x86_ascend_training
  137. @pytest.mark.env_onecard
  138. def test_simple_if():
  139. context.set_context(mode=context.GRAPH_MODE, device_target="Ascend")
  140. x = np.array(3).astype(np.float32)
  141. y = np.array(2).astype(np.float32)
  142. z = np.array(3).astype(np.float32)
  143. input_shape = (127, 7, 53, 31)
  144. input1 = np.random.randn(*input_shape).astype(np.float32)
  145. input2 = np.random.randn(*input_shape).astype(np.float32)
  146. net = ControlSimpleIf()
  147. output = net(Tensor(x), Tensor(y), Tensor(z), Tensor(input1), Tensor(input2))
  148. expect = input2 * 3 * 3 * 2 + input1
  149. assert np.allclose(expect, output.asnumpy(), 0.0001, 0.0001)
  150. @pytest.mark.level0
  151. @pytest.mark.platform_arm_ascend_training
  152. @pytest.mark.platform_x86_ascend_training
  153. @pytest.mark.env_onecard
  154. def test_simple_if_with_assign():
  155. context.set_context(mode=context.GRAPH_MODE, device_target="Ascend")
  156. x = np.array(0).astype(np.float32)
  157. y = np.array(1).astype(np.float32)
  158. input_shape = (127, 7, 53, 31)
  159. input_data = np.random.randn(*input_shape).astype(np.float32)
  160. net = ControlSimpleIfWithAssign(input_shape)
  161. output = net(Tensor(x), Tensor(y), Tensor(input_data))
  162. expect = input_data
  163. assert np.allclose(expect, output.asnumpy(), 0.0001, 0.0001)
  164. @pytest.mark.level0
  165. @pytest.mark.platform_arm_ascend_training
  166. @pytest.mark.platform_x86_ascend_training
  167. @pytest.mark.env_onecard
  168. def test_if_in_if():
  169. context.set_context(mode=context.GRAPH_MODE, device_target="Ascend")
  170. x = np.array(2.345678).astype(np.float32)
  171. y = np.array(1.234567).astype(np.float32)
  172. net = ControlIfinIf()
  173. output = net(Tensor(x), Tensor(y))
  174. expect = x + y + 3
  175. assert np.allclose(expect, output.asnumpy(), 0.0001, 0.0001)
  176. @pytest.mark.level0
  177. @pytest.mark.platform_arm_ascend_training
  178. @pytest.mark.platform_x86_ascend_training
  179. @pytest.mark.env_onecard
  180. def test_if_by_if_by_if():
  181. context.set_context(mode=context.GRAPH_MODE, device_target="Ascend")
  182. x = np.array(2.345678).astype(np.float32)
  183. y = np.array(1.234567).astype(np.float32)
  184. cond1 = np.array(True).astype(np.bool)
  185. cond2 = np.array(False).astype(np.bool)
  186. input_shape = (127, 7, 53, 31)
  187. input_data = np.random.randn(*input_shape).astype(np.float32)
  188. net = ControlIfbyIfbyIf()
  189. output = net(Tensor(x), Tensor(y), Tensor(cond1), Tensor(cond2), Tensor(input_data))
  190. expect = input_data * 3 * 2 * 2 * 2
  191. assert np.allclose(expect, output.asnumpy(), 0.0001, 0.0001)
  192. @pytest.mark.level0
  193. @pytest.mark.platform_arm_ascend_training
  194. @pytest.mark.platform_x86_ascend_training
  195. @pytest.mark.env_onecard
  196. def test_simple_while():
  197. context.set_context(mode=context.GRAPH_MODE, device_target="Ascend")
  198. x = np.array(True).astype(np.bool)
  199. y = np.array(False).astype(np.bool)
  200. input_shape = (127, 7, 53, 31)
  201. input_data = np.random.randn(*input_shape).astype(np.float32)
  202. net = ControlSimpleWhile()
  203. output = net(Tensor(x), Tensor(y), Tensor(input_data))
  204. expect = input_data * 3
  205. assert np.allclose(expect, output.asnumpy(), 0.0001, 0.0001)
  206. @pytest.mark.level0
  207. @pytest.mark.platform_arm_ascend_training
  208. @pytest.mark.platform_x86_ascend_training
  209. @pytest.mark.env_onecard
  210. def test_mixed_while_if():
  211. context.set_context(mode=context.GRAPH_MODE, device_target="Ascend")
  212. x = np.array(2).astype(np.int32)
  213. y = np.array(14).astype(np.int32)
  214. z = np.array(1).astype(np.int32)
  215. c2 = Tensor([14], mstype.int32)
  216. c4 = Tensor([0], mstype.int32)
  217. net = ControlMixedWhileIf()
  218. output = net(Tensor(x), Tensor(y), Tensor(z), c2, c4)
  219. expect = np.array(3318).astype(np.int32)
  220. assert np.allclose(expect, output.asnumpy(), 0.0001, 0.0001)