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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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 ControlMixedWhileIf(nn.Cell):
  89. def __init__(self):
  90. super().__init__()
  91. def construct(self, x, y):
  92. y = y + 4
  93. while x < y:
  94. if 2 * x < y:
  95. x = x + 1
  96. else:
  97. x = x + 2
  98. x = x + 3
  99. return x
  100. @pytest.mark.level0
  101. @pytest.mark.platform_arm_ascend_training
  102. @pytest.mark.platform_x86_ascend_training
  103. @pytest.mark.env_onecard
  104. def test_simple_if():
  105. context.set_context(mode=context.GRAPH_MODE, device_target="Ascend")
  106. x = np.array(3).astype(np.float32)
  107. y = np.array(2).astype(np.float32)
  108. z = np.array(3).astype(np.float32)
  109. input_shape = (127, 7, 53, 31)
  110. input1 = np.random.randn(*input_shape).astype(np.float32)
  111. input2 = np.random.randn(*input_shape).astype(np.float32)
  112. net = ControlSimpleIf()
  113. output = net(Tensor(x), Tensor(y), Tensor(z), Tensor(input1), Tensor(input2))
  114. expect = input2 * 3 * 3 * 2 + input1
  115. assert np.allclose(expect, output.asnumpy(), 0.0001, 0.0001)
  116. @pytest.mark.level0
  117. @pytest.mark.platform_arm_ascend_training
  118. @pytest.mark.platform_x86_ascend_training
  119. @pytest.mark.env_onecard
  120. def test_simple_if_with_assign():
  121. context.set_context(mode=context.GRAPH_MODE, device_target="Ascend")
  122. x = np.array(0).astype(np.float32)
  123. y = np.array(1).astype(np.float32)
  124. input_shape = (127, 7, 53, 31)
  125. input_data = np.random.randn(*input_shape).astype(np.float32)
  126. net = ControlSimpleIfWithAssign(input_shape)
  127. output = net(Tensor(x), Tensor(y), Tensor(input_data))
  128. expect = input_data
  129. assert np.allclose(expect, output.asnumpy(), 0.0001, 0.0001)
  130. @pytest.mark.level0
  131. @pytest.mark.platform_arm_ascend_training
  132. @pytest.mark.platform_x86_ascend_training
  133. @pytest.mark.env_onecard
  134. def test_if_in_if():
  135. context.set_context(mode=context.GRAPH_MODE, device_target="Ascend")
  136. x = np.array(2.345678).astype(np.float32)
  137. y = np.array(1.234567).astype(np.float32)
  138. net = ControlIfinIf()
  139. output = net(Tensor(x), Tensor(y))
  140. expect = x + y + 3
  141. assert np.allclose(expect, output.asnumpy(), 0.0001, 0.0001)
  142. @pytest.mark.level0
  143. @pytest.mark.platform_arm_ascend_training
  144. @pytest.mark.platform_x86_ascend_training
  145. @pytest.mark.env_onecard
  146. def test_if_by_if_by_if():
  147. context.set_context(mode=context.GRAPH_MODE, device_target="Ascend")
  148. x = np.array(2.345678).astype(np.float32)
  149. y = np.array(1.234567).astype(np.float32)
  150. cond1 = np.array(True).astype(np.bool)
  151. cond2 = np.array(False).astype(np.bool)
  152. input_shape = (127, 7, 53, 31)
  153. input_data = np.random.randn(*input_shape).astype(np.float32)
  154. net = ControlIfbyIfbyIf()
  155. output = net(Tensor(x), Tensor(y), Tensor(cond1), Tensor(cond2), Tensor(input_data))
  156. expect = input_data * 3 * 2 * 2 * 2
  157. assert np.allclose(expect, output.asnumpy(), 0.0001, 0.0001)
  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_mixed_while_if():
  163. context.set_context(mode=context.GRAPH_MODE, device_target="Ascend")
  164. x = np.array(2).astype(np.int32)
  165. y = np.array(14).astype(np.int32)
  166. net = ControlMixedWhileIf()
  167. output = net(Tensor(x), Tensor(y))
  168. expect = np.array(22).astype(np.int32)
  169. assert np.allclose(expect, output.asnumpy(), 0.0001, 0.0001)