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

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