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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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 context.get_context("enable_dump") == False
  58. context.set_context(enable_dump=True)
  59. assert context.get_context("enable_dump") == True
  60. assert context.get_context("save_dump_path") == "."
  61. def test_set_context():
  62. """ test_set_context """
  63. context.set_context(mode=context.GRAPH_MODE, device_target="Ascend",
  64. device_id=0, save_graphs=True, save_graphs_path="mindspore_ir_path")
  65. assert context.get_context("device_id") == 0
  66. assert context.get_context("device_target") == "Ascend"
  67. assert context.get_context("save_graphs")
  68. assert os.path.exists("mindspore_ir_path")
  69. assert context.get_context("save_graphs_path").find("mindspore_ir_path") > 0
  70. assert context.get_context("mode") == context.GRAPH_MODE
  71. context.set_context(mode=context.PYNATIVE_MODE)
  72. assert context.get_context("mode") == context.PYNATIVE_MODE
  73. assert context.get_context("device_target") == "Ascend"
  74. with pytest.raises(ValueError):
  75. context.set_context(modex="ge")
  76. def teardown_module():
  77. dirs = ['mindspore_ir_path']
  78. for item in dirs:
  79. item_name = './' + item
  80. if not os.path.exists(item_name):
  81. continue
  82. if os.path.isdir(item_name):
  83. os.rmdir(item_name)
  84. elif os.path.isfile(item_name):
  85. os.remove(item_name)