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_graph_fallback.py 5.7 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. # Copyright 2021 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 graph fallback """
  16. import pytest
  17. import numpy as np
  18. import mindspore.nn as nn
  19. from mindspore import Tensor, ms_function, context
  20. from mindspore.ops import operations as P
  21. from mindspore.ops import functional as F
  22. import mindspore.common.dtype as mstype
  23. import mindspore.common._monad as monad
  24. context.set_context(mode=context.GRAPH_MODE)
  25. # `add_func` is defined in current file.
  26. def add_func(x, y):
  27. return x + y
  28. @ms_function
  29. def do_increment(i):
  30. add_1 = F.partial(add_func, 1)
  31. return add_1(i)
  32. def test_increment():
  33. a = do_increment(9)
  34. assert a == 10
  35. @ms_function
  36. def use_monad(x, y):
  37. res = P.Mul()(x, y)
  38. res = F.depend(res, monad.U)
  39. return res
  40. def test_use_monad():
  41. x = Tensor(1.0, mstype.float32)
  42. y = Tensor(1.0, mstype.float32)
  43. print(use_monad(x, y))
  44. @ms_function
  45. def use_tensor_with_mstype():
  46. me_x = Tensor(1, mstype.int32)
  47. return me_x
  48. def test_tensor_with_mstype():
  49. """
  50. Feature: JIT Fallback
  51. Description: Test tensor with mstype in graph mode.
  52. Expectation: No exception.
  53. """
  54. print(use_tensor_with_mstype())
  55. @ms_function
  56. def use_tuple_of_tensor():
  57. me_x = (Tensor(1), Tensor(1))
  58. return me_x
  59. def test_tuple_of_tensor():
  60. """
  61. Feature: JIT Fallback
  62. Description: Test tuple of tensor in graph mode.
  63. Expectation: No exception.
  64. """
  65. print(use_tuple_of_tensor())
  66. @ms_function
  67. def use_list_of_tensor():
  68. me_x = [Tensor(1), Tensor(1)]
  69. return me_x
  70. def test_list_of_tensor():
  71. """
  72. Feature: JIT Fallback
  73. Description: Test list of tensor in graph mode.
  74. Expectation: No exception.
  75. """
  76. print(use_list_of_tensor())
  77. class Net(nn.Cell):
  78. def __init__(self):
  79. super(Net, self).__init__()
  80. self.x = Tensor([2, 3, 4])
  81. def construct(self):
  82. x_len = len(self.x)
  83. for i in range(x_len):
  84. print(i)
  85. return x_len
  86. def test_builtins_len():
  87. net = Net()
  88. net()
  89. @ms_function
  90. def np_fallback_func():
  91. array_x = tuple([2, 3, 4, 5])
  92. np_x = np.array(array_x).astype(np.float32)
  93. me_x = Tensor(np_x)
  94. me_x = me_x + me_x
  95. return me_x
  96. def test_np_fallback_func():
  97. print(np_fallback_func())
  98. # Test `return` interpret node.
  99. @ms_function
  100. def div_mod_func1():
  101. x = 8
  102. y = 3
  103. a = divmod(x, y)
  104. return Tensor(a)
  105. def test_div_mod_func1():
  106. print(div_mod_func1()) # (2, 2)
  107. # Test interpret node with parameters as input.
  108. @ms_function
  109. def div_mod_func2(x, y):
  110. a = divmod(x, y)
  111. return Tensor(a)
  112. def test_div_mod_func2_scalar():
  113. """
  114. Feature: JIT Fallback
  115. Description: Test divmod in graph.
  116. Expectation: No exception.
  117. """
  118. print(div_mod_func2(8, 3)) # (2, 2)
  119. @pytest.mark.skip(reason='Not support in graph jit fallback feature yet')
  120. def test_div_mod_func2_tensor():
  121. """
  122. Feature: JIT Fallback
  123. Description: Test divmod with Tensor input in graph. We'll support it in Tensor Input Fallback solution.
  124. Expectation: Not supported exception.
  125. """
  126. with pytest.raises(RuntimeError) as err:
  127. print(div_mod_func2(Tensor(8), Tensor(3)))
  128. assert "Not support Tensor or variable type as input during running JIT Fallback, but got" in str(err.value)
  129. @ms_function
  130. def select_func(cond, x, y):
  131. if isinstance(cond, (tuple, list)):
  132. output = y
  133. elif isinstance(cond, Tensor):
  134. output = F.select(cond, x, y)
  135. else:
  136. output = x
  137. return output
  138. def test_select_func():
  139. cond = Tensor([True, False])
  140. x = Tensor([2, 3], mstype.float32)
  141. y = Tensor([1, 2], mstype.float32)
  142. print(select_func(cond, x, y))
  143. @ms_function
  144. def select_func2(cond, x, y):
  145. if isinstance(cond, (tuple, list)):
  146. output = y
  147. if isinstance(cond, Tensor):
  148. output = F.select(cond, x, y)
  149. else:
  150. output = x
  151. return output
  152. def test_select_func2():
  153. cond = Tensor([True, False])
  154. x = Tensor([2, 3], mstype.float32)
  155. y = Tensor([1, 2], mstype.float32)
  156. print(select_func2(cond, x, y))
  157. @ms_function
  158. def slice_func(a, b):
  159. a[1:3, ::] = b
  160. return a
  161. def test_slice_func():
  162. a = Tensor(np.arange(60).reshape(3, 4, 5), dtype=mstype.float32)
  163. b = Tensor([1], dtype=mstype.float32)
  164. print(slice_func(a, b))
  165. # EvalCNode: This may be not defined, or it can't be a operator.
  166. @pytest.mark.skip(reason='Not support graph fallback feature yet')
  167. def test_np_tensor_add():
  168. """
  169. Feature: Fallback feature
  170. Description: support Tensor add.
  171. Expectation: No exception.
  172. """
  173. @ms_function
  174. def np_tensor_add():
  175. a = Tensor(np.array(4))
  176. b = Tensor(np.array(5))
  177. tensor_list = [a, b]
  178. for tensor in tensor_list:
  179. print(tensor)
  180. x = 6
  181. np_x = np.array(x)
  182. c = Tensor(np_x)
  183. d = tensor_list[-1] + c
  184. tensor_list.append(d)
  185. return tensor_list
  186. tensor_list = np_tensor_add()
  187. print("tensor_list:", tensor_list)
  188. assert tensor_list[-1] == 11