test_context.py 5.2 KB
Newer Older
Z
zhunaipan 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
# Copyright 2020 Huawei Technologies Co., Ltd
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ============================================================================
""" test_context """
16
import os
Z
zhunaipan 已提交
17 18
import pytest
from mindspore import context
19 20


Z
zhunaipan 已提交
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
# pylint: disable=W0212
# W0212: protected-access


def setup_module(module):
    context.set_context(mode=context.PYNATIVE_MODE)


def test_contex_create_context():
    """ test_contex_create_context """
    context.set_context(mode=context.PYNATIVE_MODE)
    if context._k_context is None:
        ctx = context._context()
        assert ctx is not None
    context._k_context = None


def test_switch_mode():
    """ test_switch_mode """
    context.set_context(mode=context.GRAPH_MODE)
    assert context.get_context("mode") == context.GRAPH_MODE
    context.set_context(mode=context.PYNATIVE_MODE)
    assert context.get_context("mode") == context.PYNATIVE_MODE


def test_set_device_id():
    """ test_set_device_id """
    with pytest.raises(TypeError):
        context.set_context(device_id="cpu")
    assert context.get_context("device_id") == 0
    context.set_context(device_id=1)
    assert context.get_context("device_id") == 1


def test_device_target():
    """ test_device_target """
    with pytest.raises(TypeError):
        context.set_context(device_target=123)
    context.set_context(device_target="GPU")
    assert context.get_context("device_target") == "GPU"
    context.set_context(device_target="Ascend")
    assert context.get_context("device_target") == "Ascend"
    assert context.get_context("device_id") == 1


def test_dump_target():
    """ test_dump_target """
    with pytest.raises(TypeError):
        context.set_context(save_dump_path=1)
    context.set_context(enable_dump=False)
P
panyifeng 已提交
71
    assert not context.get_context("enable_dump")
Z
zhunaipan 已提交
72
    context.set_context(enable_dump=True)
P
panyifeng 已提交
73
    assert context.get_context("enable_dump")
Z
zhunaipan 已提交
74 75 76
    assert context.get_context("save_dump_path") == "."


77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104
def test_enable_profiling():
    """ test_profiling_mode """
    with pytest.raises(TypeError):
        context.set_context(enable_profiling=1)
    with pytest.raises(TypeError):
        context.set_context(enable_profiling="1")
    context.set_context(enable_profiling=True)
    assert context.get_context("enable_profiling") is True
    context.set_context(enable_profiling=False)
    assert context.get_context("enable_profiling") is False


def test_profiling_options():
    """ test_profiling_options """
    with pytest.raises(TypeError):
        context.set_context(profiling_options=True)
    with pytest.raises(TypeError):
        context.set_context(profiling_options=1)
    with pytest.raises(ValueError):
        context.set_context(profiling_options="training_")
    with pytest.raises(ValueError):
        context.set_context(profiling_options="training_trace:op_trace")
    context.set_context(profiling_options="training_trace")
    assert context.get_context("profiling_options") == "training_trace"
    context.set_context(profiling_options="training_trace:task_trace")
    assert context.get_context("profiling_options") == "training_trace:task_trace"


J
jinyaohui 已提交
105 106 107 108 109 110 111 112 113 114 115 116 117 118 119
def test_variable_memory_max_size():
    """test_variable_memory_max_size"""
    with pytest.raises(TypeError):
        context.set_context(variable_memory_max_size=True)
    with pytest.raises(TypeError):
        context.set_context(variable_memory_max_size=1)
    with pytest.raises(ValueError):
        context.set_context(variable_memory_max_size="")
    with pytest.raises(ValueError):
        context.set_context(variable_memory_max_size="1G")
    with pytest.raises(ValueError):
        context.set_context(variable_memory_max_size="31GB")
    context.set_context(variable_memory_max_size="3GB")


Z
zhunaipan 已提交
120 121 122
def test_set_context():
    """ test_set_context """
    context.set_context(mode=context.GRAPH_MODE, device_target="Ascend",
123
                        device_id=0, save_graphs=True, save_graphs_path="mindspore_ir_path")
Z
zhunaipan 已提交
124 125 126
    assert context.get_context("device_id") == 0
    assert context.get_context("device_target") == "Ascend"
    assert context.get_context("save_graphs")
127 128
    assert os.path.exists("mindspore_ir_path")
    assert context.get_context("save_graphs_path").find("mindspore_ir_path") > 0
Z
zhunaipan 已提交
129 130 131 132 133 134 135 136
    assert context.get_context("mode") == context.GRAPH_MODE

    context.set_context(mode=context.PYNATIVE_MODE)
    assert context.get_context("mode") == context.PYNATIVE_MODE
    assert context.get_context("device_target") == "Ascend"

    with pytest.raises(ValueError):
        context.set_context(modex="ge")
137 138 139 140 141 142 143 144 145 146 147 148


def teardown_module():
    dirs = ['mindspore_ir_path']
    for item in dirs:
        item_name = './' + item
        if not os.path.exists(item_name):
            continue
        if os.path.isdir(item_name):
            os.rmdir(item_name)
        elif os.path.isfile(item_name):
            os.remove(item_name)