-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathlayers.py
442 lines (377 loc) · 13.4 KB
/
layers.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
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
# Copyright 2024 DeepMind Technologies Limited
#
# 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.
# ==============================================================================
# pylint: disable=redefined-outer-name,missing-module-docstring,g-importing-member,missing-function-docstring,g-bare-generic,g-doc-args,missing-class-docstring
import string
from typing import Optional, Tuple
import flax.linen as nn
from flax.linen import initializers
from flax.typing import Dtype
from flax.typing import Initializer
import jax
import jax.numpy as jnp
import numpy as np
default_kernel_init = initializers.lecun_normal()
class LayerNorm(nn.Module):
"""Layer norm used in Transformer layers."""
dim: int = 1
epsilon: float = 1e-6
use_scale: bool = True
use_bias: bool = True
def setup(self) -> None:
if self.use_scale:
self.scale = self.param('scale', jax.nn.initializers.ones, (self.dim,))
if self.use_bias:
self.bias = self.param('bias', jax.nn.initializers.zeros, (self.dim,))
def __call__(self, x: jnp.ndarray) -> jnp.ndarray:
mean = jnp.mean(x, axis=[-1], keepdims=True)
var = jnp.mean(jnp.square(x - mean), axis=[-1], keepdims=True)
normed_x = (x - mean) * jax.lax.rsqrt(var + self.epsilon)
if self.use_scale:
normed_x = normed_x * (1 + self.scale)
if self.use_bias:
normed_x = normed_x + self.bias
return normed_x
class Weight(nn.Module):
input_dim: int = 0
hidden_dim: int = 0
kernel_init: Initializer = default_kernel_init
param_dtype: Dtype = jnp.float32
def setup(self) -> None:
self.w = self.param('w',
self.kernel_init,
(self.input_dim, self.hidden_dim),
self.param_dtype
)
def __call__(self, x: jnp.ndarray) -> jnp.ndarray:
return jnp.matmul(x, self.w)
class Bias(nn.Module):
hidden_dim: int = 0
param_dtype: Dtype = jnp.float32
def setup(self) -> None:
self.b = self.param('b',
jax.nn.initializers.zeros,
(self.hidden_dim,),
self.param_dtype
)
def __call__(self, x: jnp.ndarray) -> jnp.ndarray:
return x + self.b
class FFN(nn.Module):
"""Feed-forward network."""
input_dim: int = 0
output_dim: int = 0
use_bias: bool = True
use_relu: bool = True
param_dtype: Dtype = jnp.float32
kernel_init: Initializer = default_kernel_init
def setup(self) -> None:
self.linear = Weight(self.input_dim, self.output_dim)
if self.use_bias:
self.bias = Bias(self.output_dim)
def __call__(self, x: jnp.ndarray) -> jnp.ndarray:
x = self.linear(x)
if self.use_bias:
x = self.bias(x)
if self.use_relu:
x = jax.nn.relu(x)
return x
class TransformerFFN(nn.Module):
"""Feed-forward network used in Transformer layers with residual connection."""
input_dim: int = 0
output_dim: int = 0
hidden_dim: int = 0
use_bias: bool = True
add_skip_connection: bool = True
param_dtype: Dtype = jnp.float32
kernel_init: Initializer = default_kernel_init
def setup(self) -> None:
output_dim = self.output_dim
if output_dim == 0:
output_dim = self.input_dim
self.ln = LayerNorm(dim=self.input_dim, name='layer_norm')
self.ffn1 = FFN(input_dim=self.input_dim,
output_dim=self.hidden_dim,
use_bias=self.use_bias,
name='ffn_layer1')
self.ffn2 = FFN(input_dim=self.hidden_dim,
output_dim=output_dim,
use_bias=self.use_bias,
use_relu=False,
name='ffn_layer2')
def __call__(self, x: jnp.ndarray) -> jnp.ndarray:
# pdb.set_trace()
residual = x
x = self.ln(x)
x = self.ffn1(x)
x = self.ffn2(x)
if self.add_skip_connection:
x = x + residual
return x
class AttentionProjection(nn.Module):
"""Projection (e.g., k) used in self-attention.
output_proj: Whether it is out projection or not. If False, we use
"...D,DNH->...NH" for query,key,value projection. Otherwise we use
"...NH,DNH->...D" for output projection.
"""
input_dim: int = 0
num_heads: int = 0
dim_per_head: int = 0
use_bias: bool = True
output_proj: bool = False
param_dtype: Dtype = jnp.float32
def setup(self) -> None:
hd_shape = [self.num_heads, self.dim_per_head]
pc_shape = [self.input_dim] + hd_shape
if self.output_proj:
fan_in_axes, fan_out_axes = [-1], [-2, -3]
else:
fan_in_axes, fan_out_axes = [-3], [-1, -2]
self.w = self.param(
'w',
jax.nn.initializers.lecun_normal(fan_in_axes, fan_out_axes),
pc_shape,
)
if self.use_bias:
if self.output_proj:
self.b = self.param(
'b',
jax.nn.initializers.zeros,
(self.input_dim,),
dtype=self.param_dtype,
)
else:
self.b = self.param(
'b', jax.nn.initializers.zeros, hd_shape, dtype=self.param_dtype
)
def __call__(self, x: jnp.ndarray) -> jnp.ndarray:
eqn_sym = ''.join(sorted(set(string.ascii_uppercase) - set('DHN')))
shape = x.shape
rank = len(shape)
if self.output_proj:
assert shape[-2:] == (self.num_heads, self.dim_per_head)
batch_eqn = eqn_sym[: (rank - 2)]
eqn = f'{batch_eqn}NH,DNH->{batch_eqn}D'
else:
assert (
shape[-1] == self.input_dim
), f'Expecting shape[-1] == p.input_dim, {shape[-1]} != {self.input_dim}'
batch_eqn = eqn_sym[: (rank - 1)] if rank else '...'
eqn = f'{batch_eqn}D,DNH->{batch_eqn}NH'
ret = jnp.einsum(eqn, x, self.w)
if self.use_bias:
ret += self.b
return ret
class PerDimScale(nn.Module):
dim: int = 0
param_dtype: Dtype = jnp.float32
def setup(self) -> None:
self.per_dim_scale = self.param(
'per_dim_scale',
jax.nn.initializers.ones,
(self.dim,),
dtype=self.param_dtype,
)
def __call__(self, x: jnp.ndarray) -> jnp.ndarray:
assert x.shape[-1] == self.dim
r_softplus_0 = 1.442695041
scale = jnp.array(r_softplus_0 / np.sqrt(self.dim), dtype=x.dtype)
scale *= jax.nn.softplus(self.per_dim_scale)
return x * scale
class DotProductAttention(nn.Module):
"""Self-attention used in Transformer layers."""
input_dim: int = 0
hidden_dim: int = 0
num_heads: int = 1
use_bias: bool = True
dim_per_head: int = 0
use_per_dim_scale: bool = False
def setup(self) -> None:
assert self.input_dim, 'input_dim is {}'.format(self.input_dim)
assert self.hidden_dim, 'hidden_dim is {}'.format(self.hidden_dim)
dim_per_head = self.dim_per_head
if dim_per_head == 0:
dim_per_head = self.hidden_dim // self.num_heads
assert (
dim_per_head * self.num_heads == self.hidden_dim
), f'{dim_per_head} * {self.num_heads} != {self.hidden_dim}'
self.key = AttentionProjection(input_dim=self.input_dim,
num_heads=self.num_heads,
dim_per_head=dim_per_head,
use_bias=self.use_bias)
self.query = AttentionProjection(input_dim=self.input_dim,
num_heads=self.num_heads,
dim_per_head=dim_per_head,
use_bias=self.use_bias)
self.value = AttentionProjection(input_dim=self.input_dim,
num_heads=self.num_heads,
dim_per_head=dim_per_head,
use_bias=self.use_bias)
if self.use_per_dim_scale:
self.per_dim_scale = PerDimScale(dim=dim_per_head)
self.post = AttentionProjection(self.input_dim,
self.num_heads,
dim_per_head,
self.use_bias,
output_proj=True)
def _dot_atten(self, query: jnp.ndarray, key: jnp.ndarray, value: jnp.ndarray
) -> Tuple[jnp.ndarray, jnp.ndarray]:
"""Dot-product attention."""
# query_vec: [B, T, D].
# key_vec: [B, S, D].
# value_vec: [B, S, D].
# assert query.shape[:-1] == key.shape[:-1] == value.shape[:-1]
if self.use_per_dim_scale:
query = self.per_dim_scale(query)
else:
dim_per_head = self.hidden_dim // self.num_heads
query *= dim_per_head**-0.5
logits = jnp.einsum('BTNH,BSNH->BNTS', query, key)
# cap logits
cap = jnp.array(50.0, dtype=logits.dtype)
logits = cap * jnp.tanh(logits / cap)
probs = jax.nn.softmax(logits, axis=-1).astype(key.dtype)
encoded = jnp.einsum('BNTS,BSNH->BTNH', probs, value)
return encoded, probs
def __call__(self,
q_vector: jnp.ndarray,
k_vector: jnp.ndarray,
v_vector: jnp.ndarray,
atten_mask: None = None) -> Tuple[jnp.ndarray, jnp.ndarray]:
query_proj = self.query(q_vector)
key_proj = self.key(k_vector)
value_proj = self.value(v_vector)
encoded, atten_probs = self._dot_atten(query_proj, key_proj, value_proj)
encoded = self.post(encoded)
return encoded, atten_probs
class Transformer(nn.Module):
"""Transformer layer used in multimodal encoder."""
num_heads: int
# ff_layer, layer_norm, self_attention
input_dim: int = 0
hidden_dim: int = 0
output_dim: int = 0
use_bias: bool = True
add_skip_connection: bool = True
use_per_dim_scale: bool = False
def setup(self) -> None:
output_dim = self.output_dim
if output_dim == 0:
output_dim = self.input_dim
self.ff_layer = TransformerFFN(
self.input_dim,
output_dim,
self.hidden_dim,
self.use_bias,
self.add_skip_connection,
)
attn_hidden_dim = self.input_dim
self.self_attention = DotProductAttention(
input_dim=self.input_dim,
hidden_dim=attn_hidden_dim,
num_heads=self.num_heads,
use_bias=self.use_bias,
use_per_dim_scale=self.use_per_dim_scale,
)
self.layer_norm = LayerNorm(dim=self.input_dim, name='layer_norm')
def __call__(
self, x: jnp.ndarray, attn_mask=None
) -> Tuple[jnp.ndarray, jnp.ndarray]:
x_normalized = self.layer_norm(x)
atten_output, atten_probs = self.self_attention(
x_normalized,
x_normalized,
x_normalized,
)
if self.add_skip_connection:
atten_output += x
output = self.ff_layer(atten_output)
return output, atten_probs
class StackedTransformer(nn.Module):
num_layers: int
num_heads: int
input_dim: int
hidden_dim: int
use_bias: bool = True
add_skip_connection: bool = True
use_per_dim_scale: bool = False
def setup(self) -> None:
assert self.num_layers > 0
assert self.input_dim > 0
assert self.hidden_dim > 0
assert self.num_heads > 0
output_dim = self.input_dim
self.layers = [
Transformer(num_heads=self.num_heads,
input_dim=self.input_dim,
hidden_dim=self.hidden_dim,
output_dim=output_dim,
use_bias=self.use_bias,
add_skip_connection=self.add_skip_connection,
use_per_dim_scale=self.use_per_dim_scale,
name=f'x_layers_{i}')
for i in range(self.num_layers)
]
def __call__(self, x: jnp.ndarray, attn_mask=None) -> jnp.ndarray:
for layer in self.layers:
x, _ = layer(x, attn_mask)
return x
class AttenTokenPoolingLayer(nn.Module):
input_dim: int = 0
query_dim: Optional[int] = None
hidden_dim: int = 0
num_heads: int = 1
num_query_tokens: int = 1
use_bias: bool = True
use_per_dim_scale: bool = True
param_dtype: Dtype = jnp.float32
kernel_init: Initializer = default_kernel_init
def setup(self):
# Create sub-modules
assert self.input_dim > 0, 'input_dim must be positive'
query_dim = self.query_dim or self.input_dim
ff_hidden_dim = (
self.hidden_dim if self.hidden_dim > 0 else 4 * self.input_dim
)
self.pool_attn = DotProductAttention(
input_dim=self.input_dim,
hidden_dim=ff_hidden_dim,
num_heads=self.num_heads,
use_bias=self.use_bias,
use_per_dim_scale=self.use_per_dim_scale,
name='pool_attn',
)
self.pool_attn_ln = LayerNorm(
dim=query_dim, epsilon=1e-6, name='pool_attn_ln'
)
self.pooling_attn_query = self.param(
'pooling_attn_query',
self.kernel_init,
(self.num_query_tokens, query_dim),
dtype=self.param_dtype,
)
def __call__(self, embeds: jnp.ndarray) -> jnp.ndarray:
"""Pooling layer.
Args:
embeds: (batch_size, seq_len, input_dim)
Returns:
pooled_output: (batch_size, query_dim)
"""
batch_size, _ = embeds.shape[:2]
query = jnp.tile(
self.pooling_attn_query[jnp.newaxis, :, :], [batch_size, 1, 1]
)
key = embeds
pooled_output, _ = self.pool_attn(query, key, embeds)
pooled_output = self.pool_attn_ln(pooled_output)
return pooled_output