smartparam.py 6.51 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
# 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 random
23
import numpy as np
Deshui Yu's avatar
Deshui Yu committed
24

chicm-ms's avatar
chicm-ms committed
25
from .env_vars import trial_env_vars
Deshui Yu's avatar
Deshui Yu committed
26
from . import trial
27
from .nas_utils import classic_mode, enas_mode, oneshot_mode, darts_mode
Deshui Yu's avatar
Deshui Yu committed
28
29
30
31
32
33
34
35
36
37
38
39
40


__all__ = [
    'choice',
    'randint',
    'uniform',
    'quniform',
    'loguniform',
    'qloguniform',
    'normal',
    'qnormal',
    'lognormal',
    'qlognormal',
41
42
    'function_choice',
    'mutable_layer'
Deshui Yu's avatar
Deshui Yu committed
43
44
45
46
47
]


# pylint: disable=unused-argument

chicm-ms's avatar
chicm-ms committed
48
if trial_env_vars.NNI_PLATFORM is None:
Deshui Yu's avatar
Deshui Yu committed
49
50
51
52
53
54
55
56
57
58
    def choice(*options, name=None):
        return random.choice(options)

    def randint(upper, name=None):
        return random.randrange(upper)

    def uniform(low, high, name=None):
        return random.uniform(low, high)

    def quniform(low, high, q, name=None):
59
        assert high > low, 'Upper bound must be larger than lower bound'
60
        return np.clip(round(random.uniform(low, high) / q) * q, low, high)
Deshui Yu's avatar
Deshui Yu committed
61
62

    def loguniform(low, high, name=None):
63
64
        assert low > 0, 'Lower bound must be positive'
        return np.exp(random.uniform(np.log(low), np.log(high)))
Deshui Yu's avatar
Deshui Yu committed
65
66

    def qloguniform(low, high, q, name=None):
67
        return np.clip(round(loguniform(low, high) / q) * q, low, high)
Deshui Yu's avatar
Deshui Yu committed
68
69
70
71
72
73
74
75

    def normal(mu, sigma, name=None):
        return random.gauss(mu, sigma)

    def qnormal(mu, sigma, q, name=None):
        return round(random.gauss(mu, sigma) / q) * q

    def lognormal(mu, sigma, name=None):
76
        return np.exp(random.gauss(mu, sigma))
Deshui Yu's avatar
Deshui Yu committed
77
78
79
80
81
82
83

    def qlognormal(mu, sigma, q, name=None):
        return round(lognormal(mu, sigma) / q) * q

    def function_choice(*funcs, name=None):
        return random.choice(funcs)()

84
85
86
    def mutable_layer():
        raise RuntimeError('Cannot call nni.mutable_layer in this mode')

Deshui Yu's avatar
Deshui Yu committed
87
88
else:

Zejun Lin's avatar
Zejun Lin committed
89
90
    def choice(options, name=None, key=None):
        return options[_get_param(key)]
Deshui Yu's avatar
Deshui Yu committed
91

Zejun Lin's avatar
Zejun Lin committed
92
93
    def randint(upper, name=None, key=None):
        return _get_param(key)
Deshui Yu's avatar
Deshui Yu committed
94

Zejun Lin's avatar
Zejun Lin committed
95
96
    def uniform(low, high, name=None, key=None):
        return _get_param(key)
Deshui Yu's avatar
Deshui Yu committed
97

Zejun Lin's avatar
Zejun Lin committed
98
99
    def quniform(low, high, q, name=None, key=None):
        return _get_param(key)
Deshui Yu's avatar
Deshui Yu committed
100

Zejun Lin's avatar
Zejun Lin committed
101
102
    def loguniform(low, high, name=None, key=None):
        return _get_param(key)
Deshui Yu's avatar
Deshui Yu committed
103

Zejun Lin's avatar
Zejun Lin committed
104
105
    def qloguniform(low, high, q, name=None, key=None):
        return _get_param(key)
Deshui Yu's avatar
Deshui Yu committed
106

Zejun Lin's avatar
Zejun Lin committed
107
108
    def normal(mu, sigma, name=None, key=None):
        return _get_param(key)
Deshui Yu's avatar
Deshui Yu committed
109

Zejun Lin's avatar
Zejun Lin committed
110
111
    def qnormal(mu, sigma, q, name=None, key=None):
        return _get_param(key)
Deshui Yu's avatar
Deshui Yu committed
112

Zejun Lin's avatar
Zejun Lin committed
113
114
    def lognormal(mu, sigma, name=None, key=None):
        return _get_param(key)
Deshui Yu's avatar
Deshui Yu committed
115

Zejun Lin's avatar
Zejun Lin committed
116
117
118
119
120
121
    def qlognormal(mu, sigma, q, name=None, key=None):
        return _get_param(key)

    def function_choice(funcs, name=None, key=None):
        return funcs[_get_param(key)]()

122
123
124
125
126
127
128
    def mutable_layer(
            mutable_id,
            mutable_layer_id,
            funcs,
            funcs_args,
            fixed_inputs,
            optional_inputs,
129
130
131
            optional_input_size,
            mode='classic_mode',
            tf=None):
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
        '''execute the chosen function and inputs.
        Below is an example of chosen function and inputs:
        {
            "mutable_id": {
                "mutable_layer_id": {
                    "chosen_layer": "pool",
                    "chosen_inputs": ["out1", "out3"]
                }
            }
        }
        Parameters:
        ---------------
        mutable_id: the name of this mutable_layer block (which could have multiple mutable layers)
        mutable_layer_id: the name of a mutable layer in this block
        funcs: dict of function calls
        funcs_args:
        fixed_inputs:
        optional_inputs: dict of optional inputs
        optional_input_size: number of candidate inputs to be chosen
151
        tf: tensorflow module
152
        '''
153
154
155
156
157
158
159
160
        if mode == 'classic_mode':
            return classic_mode(mutable_id,
                            mutable_layer_id,
                            funcs,
                            funcs_args,
                            fixed_inputs,
                            optional_inputs,
                            optional_input_size)
161
162
        assert tf is not None, 'Internal Error: Tensorflow should not be None in modes other than classic_mode'
        if mode == 'enas_mode':
163
164
165
166
167
168
169
170
            return enas_mode(mutable_id,
                            mutable_layer_id,
                            funcs,
                            funcs_args,
                            fixed_inputs,
                            optional_inputs,
                            optional_input_size,
                            tf)
171
        if mode == 'oneshot_mode':
172
173
174
175
176
177
178
179
            return oneshot_mode(mutable_id,
                            mutable_layer_id,
                            funcs,
                            funcs_args,
                            fixed_inputs,
                            optional_inputs,
                            optional_input_size,
                            tf)
180
181
182
183
184
185
186
187
188
189
        if mode == 'darts_mode':
            return darts_mode(mutable_id,
                            mutable_layer_id,
                            funcs,
                            funcs_args,
                            fixed_inputs,
                            optional_inputs,
                            optional_input_size,
                            tf)
        raise RuntimeError('Unrecognized mode: %s' % mode)
190

Zejun Lin's avatar
Zejun Lin committed
191
    def _get_param(key):
192
193
        if trial._params is None:
            trial.get_next_parameter()
chicm-ms's avatar
chicm-ms committed
194
        return trial.get_current_parameter(key)