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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  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_tuple_of_tensor():
  46. me_x = (Tensor(1), Tensor(1))
  47. return me_x
  48. def test_tuple_of_tensor():
  49. """
  50. Feature: JIT Fallback
  51. Description: Test tuple of tensor in graph mode.
  52. Expectation: No exception.
  53. """
  54. print(use_tuple_of_tensor())
  55. @ms_function
  56. def use_list_of_tensor():
  57. me_x = [Tensor(1), Tensor(1)]
  58. return me_x
  59. def test_list_of_tensor():
  60. """
  61. Feature: JIT Fallback
  62. Description: Test list of tensor in graph mode.
  63. Expectation: No exception.
  64. """
  65. print(use_list_of_tensor())
  66. class Net(nn.Cell):
  67. def __init__(self):
  68. super(Net, self).__init__()
  69. self.x = Tensor([2, 3, 4])
  70. def construct(self):
  71. x_len = len(self.x)
  72. for i in range(x_len):
  73. print(i)
  74. return x_len
  75. def test_builtins_len():
  76. net = Net()
  77. net()
  78. @ms_function
  79. def np_fallback_func():
  80. array_x = tuple([2, 3, 4, 5])
  81. np_x = np.array(array_x).astype(np.float32)
  82. me_x = Tensor(np_x)
  83. me_x = me_x + me_x
  84. return me_x
  85. def test_np_fallback_func():
  86. print(np_fallback_func())
  87. # Test `return` interpret node.
  88. @ms_function
  89. def div_mod_func1():
  90. x = 8
  91. y = 3
  92. a = divmod(x, y)
  93. return Tensor(a)
  94. def test_div_mod_func1():
  95. print(div_mod_func1()) # (2, 2)
  96. # Test interpret node with parameters as input.
  97. @ms_function
  98. def div_mod_func2(x, y):
  99. a = divmod(x, y)
  100. return Tensor(a)
  101. def test_div_mod_func2_scalar():
  102. """
  103. Feature: JIT Fallback
  104. Description: Test divmod in graph.
  105. Expectation: No exception.
  106. """
  107. print(div_mod_func2(8, 3)) # (2, 2)
  108. @pytest.mark.skip(reason='Not support in graph jit fallback feature yet')
  109. def test_div_mod_func2_tensor():
  110. """
  111. Feature: JIT Fallback
  112. Description: Test divmod with Tensor input in graph. We'll support it in Tensor Input Fallback solution.
  113. Expectation: Not supported exception.
  114. """
  115. with pytest.raises(RuntimeError) as err:
  116. print(div_mod_func2(Tensor(8), Tensor(3)))
  117. assert "Not support Tensor or variable type as input during running JIT Fallback, but got" in str(err.value)
  118. @ms_function
  119. def select_func(cond, x, y):
  120. if isinstance(cond, (tuple, list)):
  121. output = y
  122. elif isinstance(cond, Tensor):
  123. output = F.select(cond, x, y)
  124. else:
  125. output = x
  126. return output
  127. def test_select_func():
  128. cond = Tensor([True, False])
  129. x = Tensor([2, 3], mstype.float32)
  130. y = Tensor([1, 2], mstype.float32)
  131. print(select_func(cond, x, y))
  132. @ms_function
  133. def select_func2(cond, x, y):
  134. if isinstance(cond, (tuple, list)):
  135. output = y
  136. if isinstance(cond, Tensor):
  137. output = F.select(cond, x, y)
  138. else:
  139. output = x
  140. return output
  141. def test_select_func2():
  142. cond = Tensor([True, False])
  143. x = Tensor([2, 3], mstype.float32)
  144. y = Tensor([1, 2], mstype.float32)
  145. print(select_func2(cond, x, y))
  146. @ms_function
  147. def slice_func(a, b):
  148. a[1:3, ::] = b
  149. return a
  150. def test_slice_func():
  151. a = Tensor(np.arange(60).reshape(3, 4, 5), dtype=mstype.float32)
  152. b = Tensor([1], dtype=mstype.float32)
  153. print(slice_func(a, b))
  154. def test_context():
  155. """
  156. Feature: JIT Fallback
  157. Description: Test context in graph.
  158. Expectation: No exception.
  159. """
  160. class ContextNet(nn.Cell):
  161. def __init__(self):
  162. super(ContextNet, self).__init__()
  163. self.mode = context.get_context("mode")
  164. def construct(self):
  165. out = 1
  166. if self.mode == context.GRAPH_MODE:
  167. out = 2
  168. return out
  169. net = ContextNet()
  170. out = net()
  171. print(out)