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_multigraph_sink.py 4.0 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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_multigraph_sink """
  16. import pytest
  17. import numpy as np
  18. import mindspore.nn as nn
  19. import mindspore.context as context
  20. from mindspore.common.tensor import Tensor
  21. from mindspore.common import dtype as mstype
  22. from mindspore.common import ms_function
  23. from mindspore.ops import operations as P
  24. def setup_module(module):
  25. context.set_context(mode = context.PYNATIVE_MODE, save_graphs = True, device_target = "Ascend")
  26. context.set_context(enable_task_sink = True, device_id = 0)
  27. c1 = Tensor([2], mstype.int32)
  28. c2 = Tensor([14], mstype.int32)
  29. c3 = Tensor([1], mstype.int32)
  30. c4 = Tensor([0], mstype.int32)
  31. c5 = Tensor([14], mstype.int32)
  32. @ms_function
  33. def simple_if(x, y, z):
  34. if x < y:
  35. x = x + 1
  36. else:
  37. x = x + 2
  38. x = x + 3
  39. return x
  40. @ms_function
  41. def if_by_if(x, y, z):
  42. if x < y:
  43. x = x + 1
  44. if y > x:
  45. x = x + 2
  46. x = x + 3
  47. return x
  48. @ms_function
  49. def if_in_if(x, y, z):
  50. out = c4
  51. if x < y:
  52. z = c4 + c4
  53. if z < y:
  54. z = z + 2
  55. out = out + z
  56. x = x + 3
  57. out = out + x
  58. return out
  59. @ms_function
  60. def simple_while(x, y, z):
  61. y = y + 4
  62. while x < y:
  63. x = x + 1
  64. x = x + 3
  65. return x
  66. @ms_function
  67. def while_by_while(x, y, z):
  68. while x < y:
  69. x = x + 1
  70. while z < c5:
  71. z = z + 1
  72. x = x + 1
  73. x = x + 1
  74. return x
  75. @ms_function
  76. def while_in_while(x, y, z):
  77. out = c4
  78. while x < y:
  79. z = c4 + c4
  80. while z < y:
  81. z = z + 1
  82. out = out + z
  83. x = x + 1
  84. out = out + x
  85. return out
  86. @ms_function
  87. def while_by_while_in_while(x, y, z):
  88. out = c4
  89. while x < c2:
  90. y = c4 + c4
  91. while y < c2:
  92. y = y + 1
  93. out = out + y
  94. z = c4 + c4
  95. while z < c2:
  96. z = z + 1
  97. out = out + z
  98. x = x + 1
  99. out = out + x
  100. return out
  101. @ms_function
  102. def while_in_while_in_while(x, y, z):
  103. out = c4
  104. while x < c2:
  105. y = c4 + c4
  106. while y < c2:
  107. y = y + 1
  108. z = c4 + c4
  109. while z < c2:
  110. z = z + 1
  111. out = out + z
  112. out = out + y
  113. x = x + 1
  114. out = out + x
  115. return out
  116. def test_simple_if():
  117. output = simple_if(c1, c2, c3)
  118. expect = Tensor([6], mstype.int32)
  119. assert output == expect
  120. def test_if_by_if():
  121. output = if_by_if(c1, c2, c3)
  122. expect = Tensor([8], mstype.int32)
  123. assert output == expect
  124. def test_if_in_if():
  125. output = if_in_if(c1, c2, c3)
  126. expect = Tensor([7], mstype.int32)
  127. assert output == expect
  128. def test_simple_while():
  129. output = simple_while(c1, c2, c3)
  130. expect = Tensor([21], mstype.int32)
  131. assert output == expect
  132. def test_while_by_while():
  133. output = while_by_while(c1, c2, c3)
  134. expect = Tensor([28], mstype.int32)
  135. assert output == expect
  136. def test_while_in_while():
  137. output = while_in_while(c1, c2, c3)
  138. expect = Tensor([1274], mstype.int32)
  139. assert output == expect
  140. def test_while_by_while_in_while():
  141. output = while_by_while_in_while(c1, c2, c3)
  142. expect = Tensor([350], mstype.int32)
  143. assert output == expect
  144. def test_while_in_while_in_while():
  145. output = while_in_while_in_while(c1, c2, c3)
  146. expect = Tensor([2534], mstype.int32)
  147. assert output == expect