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

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