updater.py 4.61 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
22
23
# 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 json
import os
24
from .rest_utils import rest_put, rest_get, check_rest_server_quick, check_response
Deshui Yu's avatar
Deshui Yu committed
25
26
27
from .url_utils import experiment_url
from .config_utils import Config
from .common_utils import get_json_content
28
from .nnictl_utils import check_experiment_id, get_experiment_port, get_config_filename
29
from .launcher_utils import parse_time
30
from .constants import REST_TIME_OUT
Deshui Yu's avatar
Deshui Yu committed
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

def validate_digit(value, start, end):
    '''validate if a digit is valid'''
    if not str(value).isdigit() or int(value) < start or int(value) > end:
        raise ValueError('%s must be a digit from %s to %s' % (value, start, end))

def validate_file(path):
    '''validate if a file exist'''
    if not os.path.exists(path):
        raise FileNotFoundError('%s is not a valid file path' % path)

def load_search_space(path):
    '''load search space content'''
    content = json.dumps(get_json_content(path))
    if not content:
        raise ValueError('searchSpace file should not be empty')
    return content

def get_query_type(key):
    '''get update query type'''
    if key == 'trialConcurrency':
        return '?update_type=TRIAL_CONCURRENCY'
    if key == 'maxExecDuration':
        return '?update_type=MAX_EXEC_DURATION'
    if key == 'searchSpace':
        return '?update_type=SEARCH_SPACE'
QuanluZhang's avatar
QuanluZhang committed
57
58
    if key == 'maxTrialNum':
        return '?update_type=MAX_TRIAL_NUM'
Deshui Yu's avatar
Deshui Yu committed
59

goooxu's avatar
goooxu committed
60
def update_experiment_profile(args, key, value):
Deshui Yu's avatar
Deshui Yu committed
61
    '''call restful server to update experiment profile'''
62
    nni_config = Config(get_config_filename(args))
Deshui Yu's avatar
Deshui Yu committed
63
    rest_port = nni_config.get_config('restServerPort')
64
65
    running, _ = check_rest_server_quick(rest_port)
    if running:
66
        response = rest_get(experiment_url(rest_port), REST_TIME_OUT)
67
        if response and check_response(response):
Deshui Yu's avatar
Deshui Yu committed
68
69
            experiment_profile = json.loads(response.text)
            experiment_profile['params'][key] = value
70
            response = rest_put(experiment_url(rest_port)+get_query_type(key), json.dumps(experiment_profile), REST_TIME_OUT)
71
            if response and check_response(response):
Deshui Yu's avatar
Deshui Yu committed
72
73
74
75
76
77
78
79
                return response
    else:
        print('ERROR: restful server is not running...')
    return None

def update_searchspace(args):
    validate_file(args.filename)
    content = load_search_space(args.filename)
80
81
82
83
84
85
    args.port = get_experiment_port(args)
    if args.port is not None:
        if update_experiment_profile(args, 'searchSpace', content):
            print('INFO: update %s success!' % 'searchSpace')
        else:
            print('ERROR: update %s failed!' % 'searchSpace')
Deshui Yu's avatar
Deshui Yu committed
86
87
88

def update_concurrency(args):
    validate_digit(args.value, 1, 1000)
89
90
91
92
93
94
    args.port = get_experiment_port(args)
    if args.port is not None:
        if update_experiment_profile(args, 'trialConcurrency', int(args.value)):
            print('INFO: update %s success!' % 'concurrency')
        else:
            print('ERROR: update %s failed!' % 'concurrency')
Deshui Yu's avatar
Deshui Yu committed
95
96

def update_duration(args):
97
98
    #parse time, change time unit to seconds 
    args.value = parse_time(args.value)
99
100
101
102
103
104
    args.port = get_experiment_port(args)
    if args.port is not None:
        if update_experiment_profile(args, 'maxExecDuration', int(args.value)):
            print('INFO: update %s success!' % 'duration')
        else:
            print('ERROR: update %s failed!' % 'duration')
Deshui Yu's avatar
Deshui Yu committed
105

QuanluZhang's avatar
QuanluZhang committed
106
107
def update_trialnum(args):
    validate_digit(args.value, 1, 999999999)
108
109
110
111
    if update_experiment_profile(args, 'maxTrialNum', int(args.value)):
        print('INFO: update %s success!' % 'trialnum')
    else:
        print('ERROR: update %s failed!' % 'trialnum')