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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  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 numpy ops """
  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 functional as F
  21. context.set_context(mode=context.GRAPH_MODE)
  22. # `add_func` is defined in current file.
  23. def add_func(x, y):
  24. return x + y
  25. @ms_function
  26. def do_increment(i):
  27. add_1 = F.partial(add_func, 1)
  28. return add_1(i)
  29. def test_increment():
  30. a = do_increment(9)
  31. assert a == 10
  32. @ms_function
  33. def np_fallback_func():
  34. array_x = [2, 3, 4, 5]
  35. np_x = np.array(array_x).astype(np.float32)
  36. me_x = Tensor(np_x)
  37. me_x = me_x + me_x
  38. return me_x
  39. @pytest.mark.skip(reason='Graph fallback feature is not supported yet')
  40. def test_np_fallback_func():
  41. print(np_fallback_func())
  42. class Net(nn.Cell):
  43. def __init__(self):
  44. super(Net, self).__init__()
  45. self.x = Tensor([2, 3, 4])
  46. def construct(self):
  47. x_len = len(self.x)
  48. for i in range(x_len):
  49. print(i)
  50. return x_len
  51. def test_builtins_len():
  52. net = Net()
  53. net()