"vscode:/vscode.git/clone" did not exist on "056c9d8561ff2f2bacf7508c124443f17489008f"
local.py 3.52 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
chicm-ms's avatar
chicm-ms committed
22
23
24
import json
import time
import json_tricks
25
import subprocess
Deshui Yu's avatar
Deshui Yu committed
26

chicm-ms's avatar
chicm-ms committed
27
from ..common import init_logger, env_args
Deshui Yu's avatar
Deshui Yu committed
28

fishyds's avatar
fishyds committed
29
30
31
32
33
34
35
36
_sysdir = os.environ['NNI_SYS_DIR']
if not os.path.exists(os.path.join(_sysdir, '.nni')):
    os.makedirs(os.path.join(_sysdir, '.nni'))
_metric_file = open(os.path.join(_sysdir, '.nni', 'metrics'), 'wb')

_outputdir = os.environ['NNI_OUTPUT_DIR']
if not os.path.exists(_outputdir):
    os.makedirs(_outputdir)
37
38

_nni_platform = os.environ['NNI_PLATFORM']
39
if _nni_platform not in ['pai', 'kubeflow', 'frameworkcontroller']:
40
41
   _log_file_path = os.path.join(_outputdir, 'trial.log')
   init_logger(_log_file_path)
Deshui Yu's avatar
Deshui Yu committed
42

43
44
_multiphase = os.environ.get('MULTI_PHASE')

chicm-ms's avatar
chicm-ms committed
45
46
47
48
49
50
51
52
53
54
_param_index = 0

def request_next_parameter():
    metric = json_tricks.dumps({
        'trial_job_id': env_args.trial_job_id,
        'type': 'REQUEST_PARAMETER',
        'sequence': 0,
        'parameter_index': _param_index
    })
    send_metric(metric)
Deshui Yu's avatar
Deshui Yu committed
55

chicm-ms's avatar
chicm-ms committed
56
def get_next_parameter():
chicm-ms's avatar
chicm-ms committed
57
    global _param_index
58
59
60
61
    params_file_name = ''
    if _multiphase and (_multiphase == 'true' or _multiphase == 'True'):
        params_file_name = ('parameter_{}.cfg'.format(_param_index), 'parameter.cfg')[_param_index == 0]
    else:
chicm-ms's avatar
chicm-ms committed
62
63
64
65
66
67
        if _param_index > 0:
            return None
        elif _param_index == 0:
            params_file_name = 'parameter.cfg'
        else:
            raise AssertionError('_param_index value ({}) should >=0'.format(_param_index))
68
69
    
    params_filepath = os.path.join(_sysdir, params_file_name)
chicm-ms's avatar
chicm-ms committed
70
71
    if not os.path.isfile(params_filepath):
        request_next_parameter()
chicm-ms's avatar
chicm-ms committed
72
    while not (os.path.isfile(params_filepath) and os.path.getsize(params_filepath) > 0):
chicm-ms's avatar
chicm-ms committed
73
74
75
76
77
        time.sleep(3)
    params_file = open(params_filepath, 'r')
    params = json.load(params_file)
    _param_index += 1
    return params
Deshui Yu's avatar
Deshui Yu committed
78
79

def send_metric(string):
80
    if _nni_platform in ['pai', 'kubeflow', 'frameworkcontroller']:
81
82
83
84
85
86
87
88
89
        data = (string).encode('utf8')
        assert len(data) < 1000000, 'Metric too long'    
        print('NNISDK_ME%s' % (data))
    else:
        data = (string + '\n').encode('utf8')
        assert len(data) < 1000000, 'Metric too long'    
        _metric_file.write(b'ME%06d%b' % (len(data), data))
        _metric_file.flush()
        subprocess.run(['touch', _metric_file.name], check = True)
90
91

def get_sequence_id():
92
    return os.environ['NNI_TRIAL_SEQ_ID']