smartparam.py 5.15 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 random

chicm-ms's avatar
chicm-ms committed
24
from .env_vars import trial_env_vars
Deshui Yu's avatar
Deshui Yu committed
25
26
27
28
29
30
31
32
33
34
35
36
37
38
from . import trial


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


# pylint: disable=unused-argument

chicm-ms's avatar
chicm-ms committed
46
if trial_env_vars.NNI_PLATFORM is None:
Deshui Yu's avatar
Deshui Yu committed
47
48
49
50
51
52
53
54
55
56
    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):
57
        assert high > low, 'Upper bound must be larger than lower bound'
Deshui Yu's avatar
Deshui Yu committed
58
59
60
        return round(random.uniform(low, high) / q) * q

    def loguniform(low, high, name=None):
61
62
        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
63
64
65
66
67
68
69
70
71
72
73

    def qloguniform(low, high, q, name=None):
        return round(loguniform(low, high) / q) * q

    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):
74
        return np.exp(random.gauss(mu, sigma))
Deshui Yu's avatar
Deshui Yu committed
75
76
77
78
79
80
81

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

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

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

Deshui Yu's avatar
Deshui Yu committed
85
86
else:

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

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

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

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

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

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

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

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

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

Zejun Lin's avatar
Zejun Lin committed
114
115
116
117
118
119
    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)]()

120
121
122
123
124
125
126
    def mutable_layer(
            mutable_id,
            mutable_layer_id,
            funcs,
            funcs_args,
            fixed_inputs,
            optional_inputs,
127
            optional_input_size):
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
        '''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
        '''
        mutable_block = _get_param(mutable_id)
        chosen_layer = mutable_block[mutable_layer_id]["chosen_layer"]
        chosen_inputs = mutable_block[mutable_layer_id]["chosen_inputs"]
        real_chosen_inputs = [optional_inputs[input_name] for input_name in chosen_inputs]
Zejun Lin's avatar
Zejun Lin committed
152
        layer_out = funcs[chosen_layer]([fixed_inputs, real_chosen_inputs], **funcs_args[chosen_layer])
153
154
155
        
        return layer_out

Zejun Lin's avatar
Zejun Lin committed
156
    def _get_param(key):
157
158
        if trial._params is None:
            trial.get_next_parameter()
chicm-ms's avatar
chicm-ms committed
159
        return trial.get_current_parameter(key)