validation.py 4.7 KB
Newer Older
X
test  
xjqbest 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
# Copyright (c) 2020 PaddlePaddle Authors. All Rights Reserved.
#
# 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.

from paddlerec.core.utils import envs


class ValueFormat:
X
xjqbest 已提交
19 20
    def __init__(self, value_type, value, value_handler):
        self.value_type = value_type
X
test  
xjqbest 已提交
21 22 23 24 25 26 27 28 29 30 31 32
        self.value = value
        self.value_handler = value_handler

    def is_valid(self, name, value):
        ret = self.is_type_valid(name, value)
        if not ret:
            return ret

        ret = self.is_value_valid(name, value)
        return ret

    def is_type_valid(self, name, value):
X
xjqbest 已提交
33
        if self.value_type == "int":
X
test  
xjqbest 已提交
34 35
            if not isinstance(value, int):
                print("\nattr {} should be int, but {} now\n".format(
X
xjqbest 已提交
36
                    name, self.value_type))
X
test  
xjqbest 已提交
37 38 39
                return False
            return True

X
xjqbest 已提交
40
        elif self.value_type == "str":
X
test  
xjqbest 已提交
41 42
            if not isinstance(value, str):
                print("\nattr {} should be str, but {} now\n".format(
X
xjqbest 已提交
43
                    name, self.value_type))
X
test  
xjqbest 已提交
44 45 46
                return False
            return True

X
xjqbest 已提交
47
        elif self.value_type == "strs":
X
test  
xjqbest 已提交
48 49
            if not isinstance(value, list):
                print("\nattr {} should be list(str), but {} now\n".format(
X
xjqbest 已提交
50
                    name, self.value_type))
X
test  
xjqbest 已提交
51 52 53 54 55 56 57 58
                return False
            for v in value:
                if not isinstance(v, str):
                    print("\nattr {} should be list(str), but list({}) now\n".
                          format(name, type(v)))
                    return False
            return True

X
xjqbest 已提交
59
        elif self.value_type == "ints":
X
test  
xjqbest 已提交
60 61
            if not isinstance(value, list):
                print("\nattr {} should be list(int), but {} now\n".format(
X
xjqbest 已提交
62
                    name, self.value_type))
X
test  
xjqbest 已提交
63 64 65 66 67 68 69 70 71 72 73 74 75 76 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 105 106 107 108 109 110 111 112 113 114
                return False
            for v in value:
                if not isinstance(v, int):
                    print("\nattr {} should be list(int), but list({}) now\n".
                          format(name, type(v)))
                    return False
            return True

        else:
            print("\nattr {}'s type is {}, can not be supported now\n".format(
                name, type(value)))
            return False

    def is_value_valid(self, name, value):
        ret = self.value_handler(value)
        return ret


def in_value_handler(name, value, values):
    if value not in values:
        print("\nattr {}'s value is {}, but {} is expected\n".format(
            name, value, values))
        return False
    return True


def eq_value_handler(name, value, values):
    if value != values:
        print("\nattr {}'s value is {}, but == {} is expected\n".format(
            name, value, values))
        return False
    return True


def ge_value_handler(name, value, values):
    if value < values:
        print("\nattr {}'s value is {}, but >= {} is expected\n".format(
            name, value, values))
        return False
    return True


def le_value_handler(name, value, values):
    if value > values:
        print("\nattr {}'s value is {}, but <= {} is expected\n".format(
            name, value, values))
        return False
    return True


def register():
    validations = {}
X
xjqbest 已提交
115 116 117 118 119 120 121
    validations["train.workspace"] = ValueFormat("str", None, eq_value_handler)
    validations["train.device"] = ValueFormat("str", ["cpu", "gpu"],
                                              in_value_handler)
    validations["train.epochs"] = ValueFormat("int", 1, ge_value_handler)
    validations["train.engine"] = ValueFormat(
        "str", ["single", "local_cluster", "cluster"], in_value_handler)

Y
fix bug  
yaoxuefeng 已提交
122
    requires = ["workspace", "dataset", "mode", "runner", "phase"]
X
test  
xjqbest 已提交
123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148
    return validations, requires


def yaml_validation(config):
    all_checkers, require_checkers = register()

    _config = envs.load_yaml(config)
    flattens = envs.flatten_environs(_config)

    for required in require_checkers:
        if required not in flattens.keys():
            print("\ncan not find {} in yaml, which is required\n".format(
                required))
            return False

    for name, flatten in flattens.items():
        checker = all_checkers.get(name, None)

        if not checker:
            continue

        ret = checker.is_valid(name, flattens)
        if not ret:
            return False

    return True