-
Notifications
You must be signed in to change notification settings - Fork 6
/
model.py
168 lines (136 loc) · 4.97 KB
/
model.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
"""
This code is adapted from
https://github.com/google-research/big_vision/blob/main/big_vision/models/vit.py
"""
from typing import Optional, Any, Sequence
import flax.linen as nn
import jax.numpy as jnp
import numpy as np
from inner_loop import *
def posemb_sincos_2d(h, w, width, temperature=10_000., dtype=jnp.float32):
y, x = jnp.mgrid[:h, :w]
assert width % 4 == 0, "Width must be mult of 4 for sincos posemb"
omega = jnp.arange(width // 4) / (width // 4 - 1)
omega = 1. / (temperature**omega)
y = jnp.einsum("m,d->md", y.flatten(), omega)
x = jnp.einsum("m,d->md", x.flatten(), omega)
pe = jnp.concatenate([jnp.sin(x), jnp.cos(x), jnp.sin(y), jnp.cos(y)], axis=1)
return jnp.asarray(pe, dtype)[None, :, :]
def get_posemb(self, typ, seqshape, width, name, dtype=jnp.float32):
if typ == "learn":
return self.param(name, nn.initializers.normal(stddev=1/np.sqrt(width)),
(1, np.prod(seqshape), width), dtype)
elif typ == "sincos2d":
return posemb_sincos_2d(*seqshape, width, dtype=dtype)
else:
raise ValueError("Unknown posemb type: %s" % typ)
class MlpBlock(nn.Module):
"""Transformer MLP / feed-forward block."""
mlp_dim: Optional[int] = None # Defaults to 4x input dim
@nn.compact
def __call__(self, x):
"""Apply Transformer MlpBlock module."""
inits = dict(
kernel_init=nn.initializers.xavier_uniform(),
bias_init=nn.initializers.normal(stddev=1e-6),
)
n, l, d = x.shape # pylint: disable=unused-variable
x = nn.Dense(self.mlp_dim or 4 * d, **inits)(x)
x = nn.gelu(x)
x = nn.Dense(d, **inits)(x)
return x
class Encoder1DBlock(nn.Module):
"""Single transformer encoder block (TTT Layer + MLP)."""
width: int
mlp_dim: Optional[int] = None # Defaults to 4x input dim
num_heads: int = 6
config: Any = None
@nn.compact
def __call__(self, x):
B, N, d = x.shape # pylint: disable=unused-variable
y = nn.LayerNorm()(x)
if self.config.layer_type == "self_attention":
y = SelfAttention(
num_heads=self.num_heads,
kernel_init=nn.initializers.xavier_uniform(),
deterministic=True,
)(y, y)
inner_loss_tuple = (jnp.inf, jnp.inf) # inner loss only applies to TTT Layers
elif self.config.layer_type == "linear_attention":
y = LinearAttention(
width=self.width,
num_heads=self.num_heads,
config=self.config.linear_attention,
)(y)
inner_loss_tuple = (jnp.inf, jnp.inf) # inner loss only applies to TTT Layers
elif self.config.layer_type == "TTT":
y, inner_loss_tuple = TTTLayer(width=self.width,
num_heads=self.num_heads,
config=self.config.TTT)(y)
else:
raise NotImplementedError("Layer Type %s Not Implemented." % (self.config.layer_type))
x = x + y
y = nn.LayerNorm()(x)
y = MlpBlock(mlp_dim=self.mlp_dim)(y)
x = x + y
return x, inner_loss_tuple
class Encoder(nn.Module):
"""Transformer Model Encoder for sequence to sequence translation."""
width: int
depth: int
mlp_dim: Optional[int] = None # Defaults to 4x input dim
num_heads: int = 12
config: Any = None
@nn.compact
def __call__(self, x):
inner_loss_tuple_layers = ()
# Input Encoder
for lyr in range(self.depth):
block = Encoder1DBlock(
name=f"encoderblock_{lyr}",
width=self.width, mlp_dim=self.mlp_dim, num_heads=self.num_heads,
config=self.config)
x, inner_loss_tuple = block(x)
inner_loss_tuple_layers += (inner_loss_tuple,)
return nn.LayerNorm(name="encoder_norm")(x), inner_loss_tuple_layers
class Model(nn.Module):
width: int
depth: int
mlp_dim: int
num_heads: int
num_classes: int = 1000
patch_size: Sequence[int] = (16, 16)
posemb: str = "sincos2d"
head_zeroinit: bool = True
config: Any = None
def setup(self) -> None:
self.word_embeddings = nn.Conv(
features=self.width,
kernel_size=self.patch_size,
strides=self.patch_size,
padding="VALID",
param_dtype=jnp.float32,
name="embedding")
self.pos_emb = get_posemb(
self, self.posemb, (224 // self.patch_size[0], 224 // self.patch_size[1]),
self.width, "pos_embedding", jnp.float32)
self.encoder = Encoder(
width=self.width,
depth=self.depth,
mlp_dim=self.mlp_dim,
num_heads=self.num_heads,
config=self.config,
name="Transformer")
self.pre_logit = nn.Dense(self.width, name="pre_logits")
kw = {"kernel_init": nn.initializers.zeros} if self.head_zeroinit else {}
self.head = nn.Dense(self.num_classes, name="head", **kw)
def __call__(self, image):
B, H, W, C = image.shape
tok_emb = self.word_embeddings(image)
tok_emb = tok_emb.reshape(B, -1, self.width)
x = tok_emb + self.pos_emb
x, inner_loss_tuple_layers = self.encoder(x)
x = jnp.mean(x, axis=1)
x = nn.tanh(self.pre_logit(x))
x = self.head(x)
return x, inner_loss_tuple_layers