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

6 years ago
6 years ago
6 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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 json
  19. import pytest
  20. from mindspore import context
  21. from mindspore._c_expression import security
  22. from tests.security_utils import security_off_wrap
  23. # pylint: disable=W0212
  24. # W0212: protected-access
  25. def setup_module(module):
  26. context.set_context(mode=context.PYNATIVE_MODE)
  27. def test_contex_create_context():
  28. """ test_contex_create_context """
  29. context.set_context(mode=context.PYNATIVE_MODE)
  30. if context._k_context is None:
  31. ctx = context._context()
  32. assert ctx is not None
  33. context._k_context = None
  34. def test_set_save_graphs_in_security():
  35. """ test set save_graphs in the security mode"""
  36. if security.enable_security():
  37. with pytest.raises(ValueError) as err:
  38. context.set_context(save_graphs=True)
  39. assert "not supported" in str(err.value)
  40. def test_set_save_graphs_path_in_security():
  41. """ test set save_graphs_path in the security mode"""
  42. if security.enable_security():
  43. with pytest.raises(ValueError) as err:
  44. context.set_context(save_graphs_path="ir_files")
  45. assert "not supported" in str(err.value)
  46. def test_switch_mode():
  47. """ test_switch_mode """
  48. context.set_context(mode=context.GRAPH_MODE)
  49. assert context.get_context("mode") == context.GRAPH_MODE
  50. context.set_context(mode=context.PYNATIVE_MODE)
  51. assert context.get_context("mode") == context.PYNATIVE_MODE
  52. def test_set_device_id():
  53. """ test_set_device_id """
  54. with pytest.raises(TypeError):
  55. context.set_context(device_id=1)
  56. context.set_context(device_id="cpu")
  57. assert context.get_context("device_id") == 1
  58. def test_device_target():
  59. """ test_device_target """
  60. with pytest.raises(TypeError):
  61. context.set_context(device_target=123)
  62. context.set_context(device_target="GPU")
  63. assert context.get_context("device_target") == "GPU"
  64. context.set_context(device_target="Ascend")
  65. assert context.get_context("device_target") == "Ascend"
  66. assert context.get_context("device_id") == 1
  67. def test_dump_target():
  68. """ test_dump_target """
  69. with pytest.raises(TypeError):
  70. context.set_context(save_dump_path=1)
  71. context.set_context(enable_dump=False)
  72. assert not context.get_context("enable_dump")
  73. context.set_context(enable_dump=True)
  74. assert context.get_context("enable_dump")
  75. assert context.get_context("save_dump_path") == "."
  76. def test_enable_profiling():
  77. """ test_profiling_mode """
  78. with pytest.raises(TypeError):
  79. context.set_context(enable_profiling=1)
  80. with pytest.raises(TypeError):
  81. context.set_context(enable_profiling="1")
  82. context.set_context(enable_profiling=True)
  83. assert context.get_context("enable_profiling") is True
  84. context.set_context(enable_profiling=False)
  85. assert context.get_context("enable_profiling") is False
  86. def test_profiling_options():
  87. """ test_profiling_options """
  88. with pytest.raises(TypeError):
  89. context.set_context(profiling_options=True)
  90. with pytest.raises(TypeError):
  91. context.set_context(profiling_options=1)
  92. profiling_options = {
  93. "output": "",
  94. "fp_point": "",
  95. "bp_point": "",
  96. "training_trace": "on",
  97. "task_trace": "on",
  98. "aic_metrics": "PipeUtilization",
  99. "aicpu": "on"
  100. }
  101. profiling_options = json.dumps(profiling_options)
  102. context.set_context(profiling_options=profiling_options)
  103. assert context.get_context("profiling_options") == profiling_options
  104. def test_variable_memory_max_size():
  105. """test_variable_memory_max_size"""
  106. with pytest.raises(TypeError):
  107. context.set_context(variable_memory_max_size=True)
  108. with pytest.raises(TypeError):
  109. context.set_context(variable_memory_max_size=1)
  110. context.set_context(variable_memory_max_size="1G")
  111. context.set_context.__wrapped__(variable_memory_max_size="3GB")
  112. def test_max_device_memory_size():
  113. """test_max_device_memory_size"""
  114. with pytest.raises(TypeError):
  115. context.set_context(max_device_memory=True)
  116. with pytest.raises(TypeError):
  117. context.set_context(max_device_memory=1)
  118. context.set_context(max_device_memory="3.5G")
  119. context.set_context.__wrapped__(max_device_memory="3GB")
  120. def test_print_file_path():
  121. """test_print_file_path"""
  122. with pytest.raises(IOError):
  123. context.set_context(print_file_path="./")
  124. @security_off_wrap
  125. def test_set_context():
  126. """ test_set_context """
  127. context.set_context.__wrapped__(device_id=0)
  128. context.set_context(mode=context.GRAPH_MODE, device_target="Ascend",
  129. save_graphs=True, save_graphs_path="mindspore_ir_path")
  130. assert context.get_context("device_id") == 0
  131. assert context.get_context("device_target") == "Ascend"
  132. assert context.get_context("save_graphs")
  133. assert os.path.exists("mindspore_ir_path")
  134. assert context.get_context("save_graphs_path").find("mindspore_ir_path") > 0
  135. assert context.get_context("mode") == context.GRAPH_MODE
  136. context.set_context(mode=context.PYNATIVE_MODE)
  137. assert context.get_context("mode") == context.PYNATIVE_MODE
  138. assert context.get_context("device_target") == "Ascend"
  139. with pytest.raises(ValueError):
  140. context.set_context(modex="ge")
  141. def teardown_module():
  142. dirs = ['mindspore_ir_path']
  143. for item in dirs:
  144. item_name = './' + item
  145. if not os.path.exists(item_name):
  146. continue
  147. if os.path.isdir(item_name):
  148. shutil.rmtree(item_name)
  149. elif os.path.isfile(item_name):
  150. os.remove(item_name)