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
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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 shutil
  18. import pytest
  19. import mindspore.nn as nn
  20. from mindspore import context, ms_function
  21. from mindspore._checkparam import args_type_check
  22. from mindspore.common.initializer import initializer
  23. from mindspore.common.parameter import Parameter
  24. from mindspore.ops import operations as P
  25. def setup_module():
  26. context.set_context(mode=context.PYNATIVE_MODE)
  27. class Net(nn.Cell):
  28. """ Net definition """
  29. def __init__(self):
  30. super(Net, self).__init__()
  31. self.add = P.Add()
  32. self.x = Parameter(initializer('normal', [1, 3, 3, 4]), name='x')
  33. self.y = Parameter(initializer('normal', [1, 3, 3, 4]), name='y')
  34. @ms_function
  35. def construct(self):
  36. return self.add(self.x, self.y)
  37. def test_vm_backend():
  38. """ test_vm_backend """
  39. context.set_context(mode=context.PYNATIVE_MODE)
  40. add = Net()
  41. output = add()
  42. assert output.asnumpy().shape == (1, 3, 3, 4)
  43. def test_vm_set_context():
  44. """ test_vm_set_context """
  45. context.set_context(save_graphs=True, save_graphs_path="mindspore_ir_path", mode=context.GRAPH_MODE)
  46. assert context.get_context("save_graphs")
  47. assert context.get_context("mode") == context.GRAPH_MODE
  48. assert os.path.exists("mindspore_ir_path")
  49. assert context.get_context("save_graphs_path").find("mindspore_ir_path") > 0
  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))
  66. def teardown_module():
  67. dirs = ['mindspore_ir_path']
  68. for item in dirs:
  69. item_name = './' + item
  70. if not os.path.exists(item_name):
  71. continue
  72. if os.path.isdir(item_name):
  73. shutil.rmtree(item_name)
  74. elif os.path.isfile(item_name):
  75. os.remove(item_name)