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_context.py 4.8 kB

5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. # Copyright 2020-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_context """
  16. import os
  17. import shutil
  18. import pytest
  19. from mindspore import context
  20. from mindspore._c_expression import security
  21. from tests.security_utils import security_off_wrap
  22. # pylint: disable=W0212
  23. # W0212: protected-access
  24. def setup_module(module):
  25. context.set_context(mode=context.PYNATIVE_MODE)
  26. def test_contex_create_context():
  27. """ test_contex_create_context """
  28. context.set_context(mode=context.PYNATIVE_MODE)
  29. if context._k_context is None:
  30. ctx = context._context()
  31. assert ctx is not None
  32. context._k_context = None
  33. def test_set_save_graphs_in_security():
  34. """ test set save_graphs in the security mode"""
  35. if security.enable_security():
  36. with pytest.raises(ValueError) as err:
  37. context.set_context(save_graphs=True)
  38. assert "not supported" in str(err.value)
  39. def test_set_save_graphs_path_in_security():
  40. """ test set save_graphs_path in the security mode"""
  41. if security.enable_security():
  42. with pytest.raises(ValueError) as err:
  43. context.set_context(save_graphs_path="ir_files")
  44. assert "not supported" in str(err.value)
  45. def test_switch_mode():
  46. """ test_switch_mode """
  47. context.set_context(mode=context.GRAPH_MODE)
  48. assert context.get_context("mode") == context.GRAPH_MODE
  49. context.set_context(mode=context.PYNATIVE_MODE)
  50. assert context.get_context("mode") == context.PYNATIVE_MODE
  51. def test_set_device_id():
  52. """ test_set_device_id """
  53. with pytest.raises(TypeError):
  54. context.set_context(device_id=1)
  55. context.set_context(device_id="cpu")
  56. assert context.get_context("device_id") == 1
  57. def test_device_target():
  58. """ test_device_target """
  59. with pytest.raises(TypeError):
  60. context.set_context(device_target=123)
  61. context.set_context(device_target="GPU")
  62. assert context.get_context("device_target") == "GPU"
  63. context.set_context(device_target="Ascend")
  64. assert context.get_context("device_target") == "Ascend"
  65. def test_variable_memory_max_size():
  66. """test_variable_memory_max_size"""
  67. with pytest.raises(TypeError):
  68. context.set_context(variable_memory_max_size=True)
  69. with pytest.raises(TypeError):
  70. context.set_context(variable_memory_max_size=1)
  71. context.set_context(variable_memory_max_size="1G")
  72. context.set_context.__wrapped__(variable_memory_max_size="3GB")
  73. def test_max_device_memory_size():
  74. """test_max_device_memory_size"""
  75. with pytest.raises(TypeError):
  76. context.set_context(max_device_memory=True)
  77. with pytest.raises(TypeError):
  78. context.set_context(max_device_memory=1)
  79. context.set_context(max_device_memory="3.5G")
  80. context.set_context.__wrapped__(max_device_memory="3GB")
  81. def test_print_file_path():
  82. """test_print_file_path"""
  83. with pytest.raises(IOError):
  84. context.set_context(device_target="Ascend")
  85. context.set_context(print_file_path="./")
  86. @security_off_wrap
  87. def test_set_context():
  88. """ test_set_context """
  89. context.set_context.__wrapped__(device_id=0)
  90. context.set_context(mode=context.GRAPH_MODE, device_target="Ascend",
  91. save_graphs=True, save_graphs_path="mindspore_ir_path")
  92. assert context.get_context("device_id") == 0
  93. assert context.get_context("device_target") == "Ascend"
  94. assert context.get_context("save_graphs")
  95. assert os.path.exists("mindspore_ir_path")
  96. assert context.get_context("save_graphs_path").find("mindspore_ir_path") > 0
  97. assert context.get_context("mode") == context.GRAPH_MODE
  98. context.set_context(mode=context.PYNATIVE_MODE)
  99. assert context.get_context("mode") == context.PYNATIVE_MODE
  100. assert context.get_context("device_target") == "Ascend"
  101. with pytest.raises(ValueError):
  102. context.set_context(modex="ge")
  103. def teardown_module():
  104. dirs = ['mindspore_ir_path']
  105. for item in dirs:
  106. item_name = './' + item
  107. if not os.path.exists(item_name):
  108. continue
  109. if os.path.isdir(item_name):
  110. shutil.rmtree(item_name)
  111. elif os.path.isfile(item_name):
  112. os.remove(item_name)