-
Notifications
You must be signed in to change notification settings - Fork 645
/
Copy pathnet.py
357 lines (314 loc) · 13.4 KB
/
net.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
# Copyright (c) 2020 PaddlePaddle Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import paddle
import paddle.nn as nn
import paddle.nn.functional as F
import math
import numpy as np
class ExpertLayer(nn.Layer):
def __init__(self, output_size=8, use_bn=True):
super(ExpertLayer, self).__init__()
self.use_bn = use_bn
# out: 32 * 32 (36 - 5 + 1 = 32)
self.conv1 = paddle.nn.Conv2D(
in_channels=1,
out_channels=10,
kernel_size=5,
weight_attr=paddle.ParamAttr(
initializer=paddle.nn.initializer.XavierNormal()))
self.bn1 = paddle.nn.BatchNorm2D(num_features=10)
# out: 16 * 16 (32/2)
self.max_pool1 = paddle.nn.MaxPool2D(kernel_size=2)
# out: 12 * 12 (16 - 5 + 1)
self.conv2 = paddle.nn.Conv2D(
in_channels=10,
out_channels=20,
kernel_size=5,
weight_attr=paddle.ParamAttr(
initializer=paddle.nn.initializer.XavierNormal()))
self.bn2 = paddle.nn.BatchNorm2D(num_features=20)
# out: 6 * 6 (12/2)
self.max_pool2 = paddle.nn.MaxPool2D(kernel_size=2)
self.linear1 = paddle.nn.Linear(
in_features=20 * 6 * 6,
out_features=50,
weight_attr=paddle.ParamAttr(
initializer=paddle.nn.initializer.TruncatedNormal(
std=1.0 / math.sqrt(720))))
self.linear2 = paddle.nn.Linear(
in_features=50,
out_features=50,
weight_attr=paddle.ParamAttr(
initializer=paddle.nn.initializer.TruncatedNormal(
std=1.0 / math.sqrt(50))))
self.linear3 = paddle.nn.Linear(
in_features=50,
out_features=output_size,
weight_attr=paddle.ParamAttr(
initializer=paddle.nn.initializer.TruncatedNormal(
std=1.0 / math.sqrt(50))))
def forward(self, x):
x = self.conv1(x)
if self.use_bn:
x = self.bn1(x)
x = F.relu(x)
x = self.max_pool1(x)
x = self.conv2(x)
if self.use_bn:
x = self.bn2(x)
x = F.relu(x)
x = self.max_pool2(x)
x = paddle.flatten(x, start_axis=1, stop_axis=-1)
x = self.linear1(x)
x = F.relu(x)
# x = self.linear2(x)
# x = F.relu(x)
x = self.linear3(x)
return x
class TowerLayer(nn.Layer):
def __init__(self, input_size, output_size=10):
super(TowerLayer, self).__init__()
self.linear1 = paddle.nn.Linear(
in_features=input_size,
out_features=50,
weight_attr=paddle.ParamAttr(
initializer=paddle.nn.initializer.TruncatedNormal(
std=1.0 / math.sqrt(input_size))))
self.linear2 = paddle.nn.Linear(
in_features=50,
out_features=50,
weight_attr=paddle.ParamAttr(
initializer=paddle.nn.initializer.TruncatedNormal(
std=1.0 / math.sqrt(input_size))))
self.linear3 = paddle.nn.Linear(
in_features=50,
out_features=output_size,
weight_attr=paddle.ParamAttr(
initializer=paddle.nn.initializer.TruncatedNormal(
std=1.0 / math.sqrt(input_size))))
def forward(self, x):
x = self.linear1(x)
x = F.relu(x)
x = self.linear2(x)
x = F.relu(x)
x = self.linear3(x)
return x
class SmoothStep(nn.Layer):
def __init__(self, gamma=1.0):
super(SmoothStep, self).__init__()
self._lower_bound = -gamma / 2.0
self._upper_bound = gamma / 2.0
self._a3 = -2 / (gamma**3)
self._a1 = 3 / (2 * gamma)
self._a0 = 0.5
def forward(self, inputs):
return paddle.where(
condition=inputs <= self._lower_bound,
x=paddle.zeros_like(inputs),
y=paddle.where(
condition=inputs >= self._upper_bound,
x=paddle.ones_like(inputs),
y=self._a3 * (inputs**3) + self._a1 * inputs + self._a0))
class DSelectkGate(nn.Layer):
def __init__(self,
num_nonzeros,
expert_num,
input_shape=None,
gamma=1.0,
entropy_reg=None,
z_initializer=None,
w_initializer=None):
super(DSelectkGate, self).__init__()
self._num_nonzeros = num_nonzeros
self._smooth_step = SmoothStep(gamma)
self._entropy_reg = entropy_reg
self._z_initializer = z_initializer or paddle.nn.initializer.Uniform(
-gamma / 100.0, gamma / 100.0)
self._w_initializer = w_initializer or paddle.nn.initializer.Uniform()
self._expert_num = expert_num
self._num_binary = math.ceil(math.log2(expert_num))
self._power_of_2 = (expert_num == 2**self._num_binary)
if input_shape is None:
z_logits = self.create_parameter(
shape=[self._num_nonzeros, 1, self._num_binary],
attr=paddle.ParamAttr(initializer=self._z_initializer))
self._z_logits = z_logits
self.add_parameter("z_logits", z_logits)
w_logits = self.create_parameter(
shape=[self._num_nonzeros, 1],
attr=paddle.ParamAttr(initializer=self._w_initializer))
self._w_logits = w_logits
self.add_parameter("w_logits", w_logits)
else:
self._z_logits = paddle.nn.Linear(
in_features=input_shape,
out_features=self._num_nonzeros * self._num_binary,
weight_attr=paddle.ParamAttr(initializer=self._z_initializer),
bias_attr=paddle.ParamAttr(initializer=self._z_initializer))
self._w_logits = paddle.nn.Linear(
in_features=input_shape,
out_features=self._num_nonzeros,
weight_attr=paddle.ParamAttr(initializer=self._w_initializer),
bias_attr=paddle.ParamAttr(initializer=self._w_initializer))
self._binary_codes = paddle.unsqueeze(
self.dec2bin(
paddle.arange(
start=0, end=expert_num, dtype=paddle.int64),
self._num_binary),
axis=0)
def dec2bin(self, x, bits):
mask = paddle.arange(bits - 1, -1, -1, dtype=paddle.float32)
mask = paddle.cast(2**mask, dtype=paddle.int64)
return paddle.not_equal(
x.unsqueeze(-1).bitwise_and(mask),
paddle.full(
shape=[1], fill_value=0, dtype=paddle.int64))
def forward(self, inputs, training=False):
if isinstance(inputs, tuple):
experts, routing_inputs = inputs
else:
experts, routing_inputs = inputs, None
if routing_inputs is None:
# static gating
expert_weights, selector_outputs = self._compute_expert_weights()
output = paddle.add_n(inputs=[
expert_weights[i] * experts[i] for i in range(len(experts))
])
else:
# per-example gating
expert_weights, selector_outputs = self._compute_example_conditioned_expert_weights(
routing_inputs)
output = paddle.add_n(inputs=[
paddle.reshape(expert_weights[:, i], [-1, 1]) * experts[i]
for i in range(len(experts))
])
return output
def _compute_expert_weights(self):
"""Computes the weight vector for the experts.
Args: None.
Returns:
A tuple: (expert_weights, selector_outputs).
expert_weights is the final weight vector of the experts.
selector_outputs is a (num_nonzero, num_experts)-matrix whose i-th row
represents the outputs of the i-th single-expert selector.
"""
# Shape = (num_nonzero, 1, num_binary)
smooth_step_activations = self._smooth_step(self._z_logits)
# Shape = (num_nonzero, num_experts)
selector_outputs = paddle.prod(
paddle.where(self._binary_codes, smooth_step_activations,
1 - smooth_step_activations),
axis=2)
# Weights for the single-expert selectors: shape = (num_nonzero, 1)
selector_weights = F.softmax(self._w_logits, axis=0)
expert_weights = paddle.sum(selector_weights * selector_outputs,
axis=0)
return expert_weights, selector_outputs
def _compute_example_conditioned_expert_weights(self, routing_inputs):
"""Computes the example-conditioned weights for the experts.
Args:
routing_inputs: a tensor of shape=(batch_size, num_features) containing
the input examples.
Returns:
A tuple: (expert_weights, selector_outputs).
expert_weights is a tensor with shape=(batch_size, num_experts),
containing the expert weights for each example in routing_inputs.
selector_outputs is a tensor with
shape=(batch_size, num_nonzero, num_experts), which contains the outputs
of the single-expert selectors for all the examples in routing_inputs.
"""
sample_logits = paddle.reshape(
self._z_logits(routing_inputs),
[-1, self._num_nonzeros, 1, self._num_binary])
smooth_step_activations = self._smooth_step(sample_logits)
# Shape = (batch_size, num_nonzeros, num_experts).
selector_outputs = paddle.prod(
paddle.where(
paddle.unsqueeze(self._binary_codes, 0),
smooth_step_activations, 1 - smooth_step_activations),
axis=3)
# Weights for the single-expert selectors
# Shape = (batch_size, num_nonzeros, 1)
selector_weights = paddle.unsqueeze(self._w_logits(routing_inputs), 2)
selector_weights = F.softmax(selector_weights, axis=1)
# Sum over the signle-expert selectors. Shape = (batch_size, num_experts).
expert_weights = paddle.sum(selector_weights * selector_outputs,
axis=1)
return expert_weights, selector_outputs
class MoELayer(nn.Layer):
def __init__(self,
feature_size,
expert_num,
expert_size,
tower_size,
gate_num,
topk=2):
super(MoELayer, self).__init__()
self.expert_num = expert_num
self.expert_size = expert_size
self.tower_size = tower_size
self.gate_num = gate_num
self.num_nonzeros = topk
self._dselect_k = DSelectkGate(
expert_num=expert_num, num_nonzeros=topk)
self._param_expert = []
for i in range(0, self.expert_num):
cnn = self.add_sublayer(
name="expert_" + str(i),
sublayer=ExpertLayer(output_size=expert_size))
self._param_expert.append(cnn)
self._param_gate = []
self._param_tower_out = []
for i in range(0, self.gate_num):
cnn = self.add_sublayer(
name="gate_" + str(i),
sublayer=ExpertLayer(output_size=expert_num))
self._param_gate.append(cnn)
tower = self.add_sublayer(
name='tower_out_' + str(i),
sublayer=TowerLayer(
input_size=50, output_size=10))
self._param_tower_out.append(tower)
def forward(self, input_data):
expert_outputs = []
for i in range(0, self.expert_num):
linear_out = self._param_expert[i](input_data)
expert_output = F.relu(linear_out)
expert_outputs.append(expert_output)
# # MMoE
# # (512, expert_size * 8)
# expert_concat = paddle.concat(x=expert_outputs, axis=1)
# expert_concat = paddle.reshape(
# expert_concat, [-1, self.expert_num, self.expert_size])
#
# output_layers = []
# for i in range(0, self.gate_num):
# cur_gate_linear = self._param_gate[i](input_data)
# cur_gate = F.softmax(cur_gate_linear)
# cur_gate = paddle.reshape(cur_gate, [-1, self.expert_num, 1])
# cur_gate_expert = paddle.multiply(x=expert_concat, y=cur_gate)
# cur_gate_expert = paddle.sum(x=cur_gate_expert, axis=1)
# out = self._param_tower_out[i](cur_gate_expert)
# out = F.softmax(out)
# out = paddle.clip(out, min=1e-15, max=1.0 - 1e-15)
# output_layers.append(out)
# DSelect-K
gate_output = self._dselect_k(expert_outputs)
output_layers = []
for i in range(0, self.gate_num):
out = self._param_tower_out[i](gate_output)
out = F.softmax(out)
out = paddle.clip(out, min=1e-15, max=1.0 - 1e-15)
output_layers.append(out)
return output_layers