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