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_backend.py 2.6 kB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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_backend """
  16. import numpy as np
  17. import pytest
  18. from mindspore.ops import operations as P
  19. import mindspore.nn as nn
  20. from mindspore import context
  21. from mindspore.common.initializer import initializer
  22. from mindspore.common.parameter import Parameter
  23. from mindspore._extends.pynative_helper import args_type_check
  24. from mindspore.common.tensor import Tensor
  25. from mindspore.common.api import ms_function
  26. def setup_module(module):
  27. context.set_context(mode=context.PYNATIVE_MODE)
  28. class Net(nn.Cell):
  29. """ Net definition """
  30. def __init__(self):
  31. super(Net, self).__init__()
  32. self.add = P.TensorAdd()
  33. self.x = Parameter(initializer('normal', [1, 3, 3, 4]), name='x')
  34. self.y = Parameter(initializer('normal', [1, 3, 3, 4]), name='y')
  35. @ms_function
  36. def construct(self):
  37. return self.add(self.x, self.y)
  38. def test_vm_backend():
  39. """ test_vm_backend """
  40. context.set_context(mode=context.PYNATIVE_MODE)
  41. add = Net()
  42. output = add()
  43. assert output.asnumpy().shape == (1, 3, 3, 4)
  44. def test_vm_set_context():
  45. """ test_vm_set_context """
  46. context.set_context(save_graphs=True, save_graphs_path="/home/mindspore", mode=context.GRAPH_MODE)
  47. assert context.get_context("save_graphs")
  48. assert context.get_context("mode") == context.GRAPH_MODE
  49. assert context.get_context("save_graphs_path") == "/home/mindspore"
  50. context.set_context(mode=context.PYNATIVE_MODE)
  51. @args_type_check(v_str=str, v_int=int, v_tuple=tuple)
  52. def check_input(v_str, v_int, v_tuple):
  53. """ check_input """
  54. print("v_str:", v_str)
  55. print("v_int:", v_int)
  56. print("v_tuple:", v_tuple)
  57. def test_args_type_check():
  58. """ test_args_type_check """
  59. with pytest.raises(TypeError):
  60. check_input(100, 100, (10, 10))
  61. with pytest.raises(TypeError):
  62. check_input("name", "age", (10, 10))
  63. with pytest.raises(TypeError):
  64. check_input("name", 100, "age")
  65. check_input("name", 100, (10, 10))