launcher_utils.py 8.2 KB
Newer Older
Deshui Yu's avatar
Deshui Yu committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# Copyright (c) Microsoft Corporation
# All rights reserved.
#
# MIT License
#
# Permission is hereby granted, free of charge,
# to any person obtaining a copy of this software and associated
# documentation files (the "Software"), to deal in the Software without restriction,
# including without limitation the rights to use, copy, modify, merge, publish,
# distribute, sublicense, and/or sell copies of the Software, and
# to permit persons to whom the Software is furnished to do so, subject to the following conditions:
# The above copyright notice and this permission notice shall be included
# in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED *AS IS*, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
# BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
# DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

import os
22
import json
23
from .config_schema import LOCAL_CONFIG_SCHEMA, REMOTE_CONFIG_SCHEMA, PAI_CONFIG_SCHEMA, KUBEFLOW_CONFIG_SCHEMA
24
from .common_utils import get_json_content, print_error
25
26
27
28
29
30
31
32
33
34

def expand_path(experiment_config, key):
    '''Change '~' to user home directory'''
    if experiment_config.get(key):
        experiment_config[key] = os.path.expanduser(experiment_config[key])

def parse_relative_path(root_path, experiment_config, key):
    '''Change relative path to absolute path'''
    if experiment_config.get(key) and not os.path.isabs(experiment_config.get(key)):
        experiment_config[key] = os.path.join(root_path, experiment_config.get(key))
Deshui Yu's avatar
Deshui Yu committed
35

36
def parse_time(experiment_config):
Deshui Yu's avatar
Deshui Yu committed
37
    '''Parse time format'''
38
    unit = experiment_config['maxExecDuration'][-1]
Deshui Yu's avatar
Deshui Yu committed
39
    if unit not in ['s', 'm', 'h', 'd']:
40
41
        print_error('the unit of time could only from {s, m, h, d}')
        exit(1)
42
    time = experiment_config['maxExecDuration'][:-1]
Deshui Yu's avatar
Deshui Yu committed
43
    if not time.isdigit():
44
45
        print_error('time format error!')
        exit(1)
Deshui Yu's avatar
Deshui Yu committed
46
    parse_dict = {'s':1, 'm':60, 'h':3600, 'd':86400}
47
    experiment_config['maxExecDuration'] = int(time) * parse_dict[unit]
Deshui Yu's avatar
Deshui Yu committed
48

49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
def parse_path(experiment_config, config_path):
    '''Parse path in config file'''
    expand_path(experiment_config, 'searchSpacePath')
    if experiment_config.get('trial'):
        expand_path(experiment_config['trial'], 'codeDir')
    if experiment_config.get('tuner'):
        expand_path(experiment_config['tuner'], 'codeDir')
    if experiment_config.get('assessor'):
        expand_path(experiment_config['assessor'], 'codeDir')
    
    #if users use relative path, convert it to absolute path
    root_path = os.path.dirname(config_path)
    if experiment_config.get('searchSpacePath'):
        parse_relative_path(root_path, experiment_config, 'searchSpacePath')
    if experiment_config.get('trial'):
        parse_relative_path(root_path, experiment_config['trial'], 'codeDir')
    if experiment_config.get('tuner'):
        parse_relative_path(root_path, experiment_config['tuner'], 'codeDir')
    if experiment_config.get('assessor'):
        parse_relative_path(root_path, experiment_config['assessor'], 'codeDir')

def validate_search_space_content(experiment_config):
    '''Validate searchspace content, 
       if the searchspace file is not json format or its values does not contain _type and _value which must be specified, 
       it will not be a valid searchspace file'''
    try:
        search_space_content = json.load(open(experiment_config.get('searchSpacePath'), 'r'))
        for value in search_space_content.values():
            if not value.get('_type') or not value.get('_value'):
78
79
                print_error('please use _type and _value to specify searchspace!')
                exit(1)
80
81
82
    except:
        raise Exception('searchspace file is not a valid json format!')

Deshui Yu's avatar
Deshui Yu committed
83
84
def validate_common_content(experiment_config):
    '''Validate whether the common values in experiment_config is valid'''
85
    if not experiment_config.get('trainingServicePlatform') or \
86
        experiment_config.get('trainingServicePlatform') not in ['local', 'remote', 'pai', 'kubeflow']:
87
        print_error('Please set correct trainingServicePlatform!')
88
        exit(1)
89
90
91
    schema_dict = {
            'local': LOCAL_CONFIG_SCHEMA,
            'remote': REMOTE_CONFIG_SCHEMA,
92
93
            'pai': PAI_CONFIG_SCHEMA,
            'kubeflow': KUBEFLOW_CONFIG_SCHEMA
94
        }
95
    try:
96
        schema_dict.get(experiment_config['trainingServicePlatform']).validate(experiment_config)
97
98
99
100
101
        #set default value
        if experiment_config.get('maxExecDuration') is None:
            experiment_config['maxExecDuration'] = '999d'
        if experiment_config.get('maxTrialNum') is None:
            experiment_config['maxTrialNum'] = 99999
SparkSnail's avatar
SparkSnail committed
102
103
104
105
106
        if experiment_config['trainingServicePlatform'] == 'remote':
            for index in range(len(experiment_config['machineList'])):
                if experiment_config['machineList'][index].get('port') is None:
                    experiment_config['machineList'][index]['port'] = 22
                
107
    except Exception as exception:
108
109
        print_error('Your config file is not correct, please check your config file content!\n%s' % exception)
        exit(1)
Deshui Yu's avatar
Deshui Yu committed
110

111
def parse_tuner_content(experiment_config):
Deshui Yu's avatar
Deshui Yu committed
112
    '''Validate whether tuner in experiment_config is valid'''
QuanluZhang's avatar
QuanluZhang committed
113
114
115
116
117
118
119
120
    if experiment_config['tuner'].get('builtinTunerName'):
        experiment_config['tuner']['className'] = experiment_config['tuner']['builtinTunerName']
    elif experiment_config['tuner'].get('codeDir') and \
        experiment_config['tuner'].get('classFileName') and \
        experiment_config['tuner'].get('className'):
        if not os.path.exists(os.path.join(
                experiment_config['tuner']['codeDir'],
                experiment_config['tuner']['classFileName'])):
121
122
            print_error('Tuner file directory is not valid!')
            exit(1)
Deshui Yu's avatar
Deshui Yu committed
123
    else:
124
        raise ValueError('Tuner format is not valid!')
Deshui Yu's avatar
Deshui Yu committed
125

126
def parse_assessor_content(experiment_config):
Deshui Yu's avatar
Deshui Yu committed
127
    '''Validate whether assessor in experiment_config is valid'''
128
    if experiment_config.get('assessor'):
QuanluZhang's avatar
QuanluZhang committed
129
130
131
132
133
134
135
136
        if experiment_config['assessor'].get('builtinAssessorName'):
            experiment_config['assessor']['className'] = experiment_config['assessor']['builtinAssessorName']
        elif experiment_config['assessor'].get('codeDir') and \
            experiment_config['assessor'].get('classFileName') and \
            experiment_config['assessor'].get('className'):
            if not os.path.exists(os.path.join(
                    experiment_config['assessor']['codeDir'],
                    experiment_config['assessor']['classFileName'])):
137
138
                print_error('Assessor file directory is not valid!')
                exit(1)
139
        else:
140
141
            print_error('Assessor format is not valid!')
            exit(1)
Deshui Yu's avatar
Deshui Yu committed
142
143
144
145
146

def validate_annotation_content(experiment_config):
    '''Valid whether useAnnotation and searchSpacePath is coexist'''
    if experiment_config.get('useAnnotation'):
        if experiment_config.get('searchSpacePath'):
147
148
            print_error('If you set useAnnotation=true, please leave searchSpacePath empty')
            exit(1)
Deshui Yu's avatar
Deshui Yu committed
149
150
    else:
        # validate searchSpaceFile
151
        if experiment_config['tuner'].get('tunerName') and experiment_config['tuner'].get('optimizationMode'):
152
            if experiment_config.get('searchSpacePath') is None:
153
154
                print_error('Please set searchSpace!')
                exit(1)
155
            validate_search_space_content(experiment_config)
Deshui Yu's avatar
Deshui Yu committed
156

157
158
159
def validate_machine_list(experiment_config):
    '''Validate machine list'''
    if experiment_config.get('trainingServicePlatform') == 'remote' and experiment_config.get('machineList') is None:
160
161
        print_error('Please set machineList!')
        exit(1)
Deshui Yu's avatar
Deshui Yu committed
162

163
def validate_all_content(experiment_config, config_path):
Deshui Yu's avatar
Deshui Yu committed
164
    '''Validate whether experiment_config is valid'''
165
    parse_path(experiment_config, config_path)
Deshui Yu's avatar
Deshui Yu committed
166
    validate_common_content(experiment_config)
167
168
169
    parse_time(experiment_config)
    parse_tuner_content(experiment_config)
    parse_assessor_content(experiment_config)
170
    validate_annotation_content(experiment_config)