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

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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 pytest
  17. from mindspore import context
  18. # pylint: disable=W0212
  19. # W0212: protected-access
  20. def setup_module(module):
  21. context.set_context(mode=context.PYNATIVE_MODE)
  22. def test_contex_create_context():
  23. """ test_contex_create_context """
  24. context.set_context(mode=context.PYNATIVE_MODE)
  25. if context._k_context is None:
  26. ctx = context._context()
  27. assert ctx is not None
  28. context._k_context = None
  29. def test_switch_mode():
  30. """ test_switch_mode """
  31. context.set_context(mode=context.GRAPH_MODE)
  32. assert context.get_context("mode") == context.GRAPH_MODE
  33. context.set_context(mode=context.PYNATIVE_MODE)
  34. assert context.get_context("mode") == context.PYNATIVE_MODE
  35. def test_set_device_id():
  36. """ test_set_device_id """
  37. with pytest.raises(TypeError):
  38. context.set_context(device_id="cpu")
  39. assert context.get_context("device_id") == 0
  40. context.set_context(device_id=1)
  41. assert context.get_context("device_id") == 1
  42. def test_device_target():
  43. """ test_device_target """
  44. with pytest.raises(TypeError):
  45. context.set_context(device_target=123)
  46. context.set_context(device_target="GPU")
  47. assert context.get_context("device_target") == "GPU"
  48. context.set_context(device_target="Ascend")
  49. assert context.get_context("device_target") == "Ascend"
  50. assert context.get_context("device_id") == 1
  51. def test_dump_target():
  52. """ test_dump_target """
  53. with pytest.raises(TypeError):
  54. context.set_context(save_dump_path=1)
  55. context.set_context(enable_dump=False)
  56. assert context.get_context("enable_dump") == False
  57. context.set_context(enable_dump=True)
  58. assert context.get_context("enable_dump") == True
  59. assert context.get_context("save_dump_path") == "."
  60. def test_set_context():
  61. """ test_set_context """
  62. context.set_context(mode=context.GRAPH_MODE, device_target="Ascend",
  63. device_id=0, save_graphs=True, save_graphs_path="/mindspore")
  64. assert context.get_context("device_id") == 0
  65. assert context.get_context("device_target") == "Ascend"
  66. assert context.get_context("save_graphs")
  67. assert context.get_context("save_graphs_path") == "/mindspore"
  68. assert context.get_context("mode") == context.GRAPH_MODE
  69. context.set_context(mode=context.PYNATIVE_MODE)
  70. assert context.get_context("mode") == context.PYNATIVE_MODE
  71. assert context.get_context("device_target") == "Ascend"
  72. with pytest.raises(ValueError):
  73. context.set_context(modex="ge")