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

5 years ago
5 years ago
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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. # 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=1)
  42. context.set_context(device_id="cpu")
  43. assert context.get_context("device_id") == 1
  44. def test_device_target():
  45. """ test_device_target """
  46. with pytest.raises(TypeError):
  47. context.set_context(device_target=123)
  48. context.set_context(device_target="GPU")
  49. assert context.get_context("device_target") == "GPU"
  50. context.set_context(device_target="Ascend")
  51. assert context.get_context("device_target") == "Ascend"
  52. assert context.get_context("device_id") == 1
  53. def test_dump_target():
  54. """ test_dump_target """
  55. with pytest.raises(TypeError):
  56. context.set_context(save_dump_path=1)
  57. context.set_context(enable_dump=False)
  58. assert not context.get_context("enable_dump")
  59. context.set_context(enable_dump=True)
  60. assert context.get_context("enable_dump")
  61. assert context.get_context("save_dump_path") == "."
  62. def test_enable_profiling():
  63. """ test_profiling_mode """
  64. with pytest.raises(TypeError):
  65. context.set_context(enable_profiling=1)
  66. with pytest.raises(TypeError):
  67. context.set_context(enable_profiling="1")
  68. context.set_context(enable_profiling=True)
  69. assert context.get_context("enable_profiling") is True
  70. context.set_context(enable_profiling=False)
  71. assert context.get_context("enable_profiling") is False
  72. def test_profiling_options():
  73. """ test_profiling_options """
  74. with pytest.raises(TypeError):
  75. context.set_context(profiling_options=True)
  76. with pytest.raises(TypeError):
  77. context.set_context(profiling_options=1)
  78. profiling_options = {
  79. "output": "",
  80. "fp_point": "",
  81. "bp_point": "",
  82. "training_trace": "on",
  83. "task_trace": "on",
  84. "aic_metrics": "PipeUtilization",
  85. "aicpu": "on"
  86. }
  87. profiling_options = json.dumps(profiling_options)
  88. context.set_context(profiling_options=profiling_options)
  89. assert context.get_context("profiling_options") == profiling_options
  90. def test_variable_memory_max_size():
  91. """test_variable_memory_max_size"""
  92. with pytest.raises(TypeError):
  93. context.set_context(variable_memory_max_size=True)
  94. with pytest.raises(TypeError):
  95. context.set_context(variable_memory_max_size=1)
  96. context.set_context(variable_memory_max_size="1G")
  97. context.set_context.__wrapped__(variable_memory_max_size="3GB")
  98. def test_max_device_memory_size():
  99. """test_max_device_memory_size"""
  100. with pytest.raises(TypeError):
  101. context.set_context(max_device_memory=True)
  102. with pytest.raises(TypeError):
  103. context.set_context(max_device_memory=1)
  104. context.set_context(max_device_memory="3.5G")
  105. context.set_context.__wrapped__(max_device_memory="3GB")
  106. def test_print_file_path():
  107. """test_print_file_path"""
  108. with pytest.raises(IOError):
  109. context.set_context(print_file_path="./")
  110. def test_set_context():
  111. """ test_set_context """
  112. context.set_context.__wrapped__(device_id=0)
  113. context.set_context(mode=context.GRAPH_MODE, device_target="Ascend",
  114. save_graphs=True, save_graphs_path="mindspore_ir_path")
  115. assert context.get_context("device_id") == 0
  116. assert context.get_context("device_target") == "Ascend"
  117. assert context.get_context("save_graphs")
  118. assert os.path.exists("mindspore_ir_path")
  119. assert context.get_context("save_graphs_path").find("mindspore_ir_path") > 0
  120. assert context.get_context("mode") == context.GRAPH_MODE
  121. context.set_context(mode=context.PYNATIVE_MODE)
  122. assert context.get_context("mode") == context.PYNATIVE_MODE
  123. assert context.get_context("device_target") == "Ascend"
  124. with pytest.raises(ValueError):
  125. context.set_context(modex="ge")
  126. def teardown_module():
  127. dirs = ['mindspore_ir_path']
  128. for item in dirs:
  129. item_name = './' + item
  130. if not os.path.exists(item_name):
  131. continue
  132. if os.path.isdir(item_name):
  133. shutil.rmtree(item_name)
  134. elif os.path.isfile(item_name):
  135. os.remove(item_name)