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
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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 pytest
  19. from mindspore import context
  20. # pylint: disable=W0212
  21. # W0212: protected-access
  22. def setup_module(module):
  23. context.set_context(mode=context.PYNATIVE_MODE)
  24. def test_contex_create_context():
  25. """ test_contex_create_context """
  26. context.set_context(mode=context.PYNATIVE_MODE)
  27. if context._k_context is None:
  28. ctx = context._context()
  29. assert ctx is not None
  30. context._k_context = None
  31. def test_switch_mode():
  32. """ test_switch_mode """
  33. context.set_context(mode=context.GRAPH_MODE)
  34. assert context.get_context("mode") == context.GRAPH_MODE
  35. context.set_context(mode=context.PYNATIVE_MODE)
  36. assert context.get_context("mode") == context.PYNATIVE_MODE
  37. def test_set_device_id():
  38. """ test_set_device_id """
  39. with pytest.raises(TypeError):
  40. context.set_context(device_id="cpu")
  41. assert context.get_context("device_id") == 0
  42. context.set_context(device_id=1)
  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. with pytest.raises(ValueError):
  79. context.set_context(profiling_options="training_")
  80. with pytest.raises(ValueError):
  81. context.set_context(profiling_options="training_trace:op_trace")
  82. context.set_context(profiling_options="training_trace")
  83. assert context.get_context("profiling_options") == "training_trace"
  84. context.set_context(profiling_options="training_trace:task_trace")
  85. assert context.get_context("profiling_options") == "training_trace:task_trace"
  86. def test_variable_memory_max_size():
  87. """test_variable_memory_max_size"""
  88. with pytest.raises(TypeError):
  89. context.set_context(variable_memory_max_size=True)
  90. with pytest.raises(TypeError):
  91. context.set_context(variable_memory_max_size=1)
  92. with pytest.raises(ValueError):
  93. context.set_context(variable_memory_max_size="")
  94. with pytest.raises(ValueError):
  95. context.set_context(variable_memory_max_size="1G")
  96. with pytest.raises(ValueError):
  97. context.set_context(variable_memory_max_size="31GB")
  98. context.set_context(variable_memory_max_size="3GB")
  99. def test_print_file_path():
  100. """test_print_file_path"""
  101. with pytest.raises(IOError):
  102. context.set_context(print_file_path="./")
  103. def test_set_context():
  104. """ test_set_context """
  105. context.set_context(mode=context.GRAPH_MODE, device_target="Ascend",
  106. device_id=0, save_graphs=True, save_graphs_path="mindspore_ir_path")
  107. assert context.get_context("device_id") == 0
  108. assert context.get_context("device_target") == "Ascend"
  109. assert context.get_context("save_graphs")
  110. assert os.path.exists("mindspore_ir_path")
  111. assert context.get_context("save_graphs_path").find("mindspore_ir_path") > 0
  112. assert context.get_context("mode") == context.GRAPH_MODE
  113. context.set_context(mode=context.PYNATIVE_MODE)
  114. assert context.get_context("mode") == context.PYNATIVE_MODE
  115. assert context.get_context("device_target") == "Ascend"
  116. with pytest.raises(ValueError):
  117. context.set_context(modex="ge")
  118. def teardown_module():
  119. dirs = ['mindspore_ir_path']
  120. for item in dirs:
  121. item_name = './' + item
  122. if not os.path.exists(item_name):
  123. continue
  124. if os.path.isdir(item_name):
  125. shutil.rmtree(item_name)
  126. elif os.path.isfile(item_name):
  127. os.remove(item_name)