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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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 os
  17. import numpy as np
  18. import pytest
  19. from mindspore.ops import operations as P
  20. import mindspore.nn as nn
  21. from mindspore import context
  22. from mindspore.common.initializer import initializer
  23. from mindspore.common.parameter import Parameter
  24. from mindspore._extends.pynative_helper import args_type_check
  25. from mindspore.common.tensor import Tensor
  26. from mindspore.common.api import ms_function
  27. def setup_module(module):
  28. context.set_context(mode=context.PYNATIVE_MODE)
  29. class Net(nn.Cell):
  30. """ Net definition """
  31. def __init__(self):
  32. super(Net, self).__init__()
  33. self.add = P.TensorAdd()
  34. self.x = Parameter(initializer('normal', [1, 3, 3, 4]), name='x')
  35. self.y = Parameter(initializer('normal', [1, 3, 3, 4]), name='y')
  36. @ms_function
  37. def construct(self):
  38. return self.add(self.x, self.y)
  39. def test_vm_backend():
  40. """ test_vm_backend """
  41. context.set_context(mode=context.PYNATIVE_MODE)
  42. add = Net()
  43. output = add()
  44. assert output.asnumpy().shape == (1, 3, 3, 4)
  45. def test_vm_set_context():
  46. """ test_vm_set_context """
  47. context.set_context(save_graphs=True, save_graphs_path="mindspore_ir_path", mode=context.GRAPH_MODE)
  48. assert context.get_context("save_graphs")
  49. assert context.get_context("mode") == context.GRAPH_MODE
  50. assert os.path.exists("mindspore_ir_path")
  51. assert context.get_context("save_graphs_path").find("mindspore_ir_path") > 0
  52. context.set_context(mode=context.PYNATIVE_MODE)
  53. @args_type_check(v_str=str, v_int=int, v_tuple=tuple)
  54. def check_input(v_str, v_int, v_tuple):
  55. """ check_input """
  56. print("v_str:", v_str)
  57. print("v_int:", v_int)
  58. print("v_tuple:", v_tuple)
  59. def test_args_type_check():
  60. """ test_args_type_check """
  61. with pytest.raises(TypeError):
  62. check_input(100, 100, (10, 10))
  63. with pytest.raises(TypeError):
  64. check_input("name", "age", (10, 10))
  65. with pytest.raises(TypeError):
  66. check_input("name", 100, "age")
  67. check_input("name", 100, (10, 10))
  68. def teardown_module():
  69. dirs = ['mindspore_ir_path']
  70. for item in dirs:
  71. item_name = './' + item
  72. if not os.path.exists(item_name):
  73. continue
  74. if os.path.isdir(item_name):
  75. os.rmdir(item_name)
  76. elif os.path.isfile(item_name):
  77. os.remove(item_name)