-
Notifications
You must be signed in to change notification settings - Fork 1
/
scenario_encoder.py
429 lines (364 loc) 路 14.2 KB
/
scenario_encoder.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
import torch
import pytorch_lightning as pl
from torch import nn, Tensor
from local_attention import LocalAttention
class REDEncoder(pl.LightningModule):
"""Road Environment Description (RED) Encoder"""
def __init__(
self,
size_encoder_vocab: int = 11,
dim_encoder_semantic_embedding: int = 4,
num_encoder_layers: int = 6,
size_decoder_vocab: int = 100,
num_decoder_layers: int = 6,
dim_model: int = 512,
dim_heads_encoder: int = 64,
dim_attn_window_encoder: int = 64,
num_heads_decoder: int = 8,
dim_feedforward: int = 2048,
dropout: float = 0.1,
max_dist: float = 50.0,
z_dim: int = 512,
batch_size: int = 8,
max_train_epochs: int = 200,
learning_rate=1e-4,
lambda_coeff=5e-3,
):
super().__init__()
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
self.encoder_semantic_embedding = nn.Embedding(
num_embeddings=size_encoder_vocab,
embedding_dim=dim_encoder_semantic_embedding,
padding_idx=-1, # For [pad] token
).to(device)
self.to_dim_model = nn.Linear(
in_features=dim_encoder_semantic_embedding + 2, # For position as (x, y)
out_features=dim_model,
)
self.max_dist = max_dist
self.encoder = LocalTransformerEncoder(
num_layers=num_encoder_layers,
dim_model=dim_model,
dim_heads=dim_heads_encoder,
dim_attn_window=dim_attn_window_encoder,
dim_feedforward=dim_feedforward,
dropout=dropout,
)
self.range_decoder_embedding = torch.arange(size_decoder_vocab).expand(
batch_size, size_decoder_vocab
)
self.decoder_semantic_embedding = nn.Embedding(
num_embeddings=size_decoder_vocab,
embedding_dim=dim_model - 10, # For learned pos. embedding
)
self.decoder_pos_embedding = nn.Embedding(
num_embeddings=size_decoder_vocab,
embedding_dim=10,
)
self.decoder = ParallelTransformerDecoder(
num_layers=num_decoder_layers,
dim_model=dim_model,
num_heads=num_heads_decoder,
dim_feedforward=dim_feedforward,
dropout=dropout,
)
self.projection_head = nn.Sequential(
nn.Linear(
in_features=size_decoder_vocab * 2, out_features=4096
), # Mean, var per token
nn.BatchNorm1d(4096),
nn.ReLU(),
nn.Linear(in_features=4096, out_features=z_dim),
)
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
self.loss_fn = BarlowTwinsLoss(
batch_size=batch_size, lambda_coeff=lambda_coeff, z_dim=z_dim
).to(device)
self.max_epochs = max_train_epochs
self.learning_rate = learning_rate
def forward(
self, idxs_src_tokens: Tensor, pos_src_tokens: Tensor, src_mask: Tensor
) -> Tensor:
pos_src_tokens /= self.max_dist
src = torch.concat(
(self.encoder_semantic_embedding(idxs_src_tokens), pos_src_tokens), dim=2
) # Concat in feature dim
src = self.to_dim_model(src)
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
self.range_decoder_embedding = self.range_decoder_embedding.to(device)
tgt = torch.concat(
(
self.decoder_semantic_embedding(self.range_decoder_embedding),
self.decoder_pos_embedding(self.range_decoder_embedding),
),
dim=2,
)
return self.decoder(tgt, self.encoder(src, src_mask), src_mask)
def shared_step(self, batch):
road_env_tokens_a = self.forward(
idxs_src_tokens=batch["sample_a"]["idx_src_tokens"],
pos_src_tokens=batch["sample_a"]["pos_src_tokens"],
src_mask=batch["src_attn_mask"],
)
road_env_tokens_b = self.forward(
idxs_src_tokens=batch["sample_b"]["idx_src_tokens"],
pos_src_tokens=batch["sample_b"]["pos_src_tokens"],
src_mask=batch["src_attn_mask"],
)
z_a = self.projection_head(
torch.concat(
(road_env_tokens_a.mean(dim=2), road_env_tokens_a.var(dim=2)), dim=1
)
)
z_b = self.projection_head(
torch.concat(
(road_env_tokens_b.mean(dim=2), road_env_tokens_b.var(dim=2)), dim=1
)
)
return self.loss_fn(z_a, z_b)
def training_step(self, batch, batch_idx):
loss = self.shared_step(batch)
self.log("train_loss", loss, on_step=True, on_epoch=False, sync_dist=True)
return loss
def validation_step(self, batch, batch_idx):
loss = self.shared_step(batch)
self.log("val_loss", loss, on_step=False, on_epoch=True, sync_dist=True)
def configure_optimizers(self):
optimizer = torch.optim.AdamW(self.parameters(), lr=self.learning_rate)
return {
"optimizer": optimizer,
"lr_scheduler": {
"scheduler": torch.optim.lr_scheduler.CosineAnnealingLR(
optimizer,
T_max=self.max_epochs,
eta_min=1e-6,
),
"interval": "epoch",
"frequency": 1,
"name": "lr",
},
}
class ParallelTransformerDecoderLayer(nn.Module):
def __init__(
self,
dim_model: int = 512,
num_heads: int = 8,
dim_feedforward: int = 2048,
dropout: float = 0.1,
):
super().__init__()
self.num_heads = num_heads
self.self_attn = Residual(
nn.MultiheadAttention(
embed_dim=dim_model,
num_heads=num_heads,
batch_first=True,
),
dimension=dim_model,
dropout=dropout,
)
self.cross_attn = Residual(
nn.MultiheadAttention(
embed_dim=dim_model,
num_heads=num_heads,
batch_first=True,
),
dimension=dim_model,
dropout=dropout,
)
self.feed_forward = Residual(
feed_forward(dim_model, dim_feedforward),
dimension=dim_model,
dropout=dropout,
)
def forward(self, tgt: Tensor, memory: Tensor, mem_mask: Tensor) -> Tensor:
tgt = self.self_attn(tgt, tgt, tgt, need_weights=False)
batch_size, tgt_len = tgt.size(dim=0), tgt.size(dim=1)
mem_mask = mem_mask[:, None, :].expand(batch_size, tgt_len, -1)
mem_mask = mem_mask.repeat(1, self.num_heads, 1)
mem_mask = mem_mask.view(batch_size * self.num_heads, tgt_len, -1)
tgt = self.cross_attn(
tgt, memory, memory, attn_mask=mem_mask, need_weights=False
)
return self.feed_forward(tgt)
class Residual(nn.Module):
def __init__(self, sublayer: nn.Module, dimension: int, dropout: float = 0.1):
super().__init__()
self.sublayer = sublayer
self.norm = nn.LayerNorm(dimension)
self.dropout = nn.Dropout(dropout)
def forward(self, *tensors: Tensor, **kwargs: dict) -> Tensor:
# Assume that the "query" tensor is given first, so we can compute the
# residual. This matches the signature of 'MultiHeadAttention'.
output_sublayer = self.sublayer(*tensors, **kwargs)
# nn.MultiheadAttention always returns a tuple (out, attn_weights or None)
if isinstance(output_sublayer, tuple):
output_sublayer = output_sublayer[0]
return self.norm(tensors[0] + self.dropout(output_sublayer))
class ParallelTransformerDecoder(nn.Module):
def __init__(
self,
num_layers: int = 6,
dim_model: int = 512,
num_heads: int = 8,
dim_feedforward: int = 2048,
dropout: float = 0.1,
add_pos_encoding: bool = False,
):
super().__init__()
self.add_pos_encoding = add_pos_encoding
self.layers = nn.ModuleList(
[
ParallelTransformerDecoderLayer(
dim_model, num_heads, dim_feedforward, dropout
)
for _ in range(num_layers)
]
)
self.linear = nn.Linear(dim_model, dim_model)
def forward(self, tgt: Tensor, memory: Tensor, mem_mask: Tensor) -> Tensor:
if self.add_pos_encoding:
seq_len, dimension = tgt.size(1), tgt.size(2)
tgt += position_encoding(seq_len, dimension)
for layer in self.layers:
tgt = layer(tgt, memory, mem_mask)
return self.linear(tgt)
def position_encoding(
seq_len: int,
dim_model: int,
device: torch.device = torch.device("cpu"),
) -> Tensor:
pos = torch.arange(seq_len, dtype=torch.float, device=device).reshape(1, -1, 1)
dim = torch.arange(dim_model, dtype=torch.float, device=device).reshape(1, 1, -1)
phase = pos / (1e4 ** (dim / dim_model))
return torch.where(dim.long() % 2 == 0, torch.sin(phase), torch.cos(phase))
def feed_forward(dim_input: int = 512, dim_feedforward: int = 2048) -> nn.Module:
return nn.Sequential(
nn.Linear(dim_input, dim_feedforward),
nn.ReLU(),
nn.Linear(dim_feedforward, dim_input),
)
class LocalTransformerEncoderLayer(nn.Module):
def __init__(
self,
dim_model: int = 512,
dim_heads: int = 64,
dim_attn_window: int = 64,
dim_feedforward: int = 2048,
dropout: float = 0.1,
):
super().__init__()
self.attention = Residual(
LocalMultiheadAttention(
dim_in=dim_model,
dim_q=dim_model,
dim_k=dim_model,
dim_heads=dim_heads,
dim_attn_window=dim_attn_window,
),
dimension=dim_model,
dropout=dropout,
)
self.feed_forward = Residual(
feed_forward(dim_model, dim_feedforward),
dimension=dim_model,
dropout=dropout,
)
def forward(self, src: Tensor, mask: Tensor) -> Tensor:
src = self.attention(src, src, src, mask)
return self.feed_forward(src)
class LocalTransformerEncoder(nn.Module):
def __init__(
self,
num_layers: int = 6,
dim_model: int = 512,
dim_heads: int = 64,
dim_attn_window: int = 64,
dim_feedforward: int = 2048,
dropout: float = 0.1,
add_pos_encoding: bool = False,
):
super().__init__()
self.add_pos_encoding = add_pos_encoding
self.layers = nn.ModuleList(
[
LocalTransformerEncoderLayer(
dim_model, dim_heads, dim_attn_window, dim_feedforward, dropout
)
for _ in range(num_layers)
]
)
def forward(self, src: Tensor, mask: Tensor) -> Tensor:
if self.add_pos_encoding:
seq_len, dimension = src.size(1), src.size(2)
src += position_encoding(seq_len, dimension)
for layer in self.layers:
src = layer(src, mask)
return src
class LocalMultiheadAttention(nn.Module):
def __init__(
self, dim_in: int, dim_q: int, dim_k: int, dim_heads: int, dim_attn_window: int
):
super().__init__()
self.to_q = nn.Linear(dim_in, dim_q)
self.to_k = nn.Linear(dim_in, dim_k)
self.to_v = nn.Linear(dim_in, dim_k)
self.attn = LocalAttention(
dim=dim_heads,
window_size=dim_attn_window,
autopad=True,
use_rotary_pos_emb=False,
)
def forward(self, queries, keys, values, mask):
q = self.to_q(queries)
k = self.to_k(keys)
v = self.to_v(values)
return self.attn(q, k, v, mask=mask)
class BarlowTwinsLoss(nn.Module):
"""Src: https://lightning.ai/docs/pytorch/stable/notebooks/lightning_examples/barlow-twins.html"""
def __init__(self, batch_size, lambda_coeff=5e-3, z_dim=128):
super().__init__()
self.z_dim = z_dim
self.batch_size = batch_size
self.lambda_coeff = lambda_coeff
def off_diagonal_ele(self, x):
# taken from: https://github.com/facebookresearch/barlowtwins/blob/main/main.py
# return a flattened view of the off-diagonal elements of a square matrix
n, m = x.shape
assert n == m
return x.flatten()[:-1].view(n - 1, n + 1)[:, 1:].flatten()
def forward(self, z1, z2):
# N x D, where N is the batch size and D is output dim of projection head
z1_norm = (z1 - torch.mean(z1, dim=0)) / torch.std(z1, dim=0)
z2_norm = (z2 - torch.mean(z2, dim=0)) / torch.std(z2, dim=0)
cross_corr = torch.matmul(z1_norm.T, z2_norm) / self.batch_size
on_diag = torch.diagonal(cross_corr).add_(-1).pow_(2).sum()
off_diag = self.off_diagonal_ele(cross_corr).pow_(2).sum()
return on_diag + self.lambda_coeff * off_diag
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
# Assuming these parameters for the dummy data
batch_size = 8 # as defined in REDEncoder
seq_len = 10 # arbitrary sequence length
size_encoder_vocab = 11 # as defined in REDEncoder
dim_pos = 2 # for (x, y) coordinates
# Create dummy data
idxs_src_tokens = torch.randint(size_encoder_vocab, (batch_size, seq_len))
pos_src_tokens = (
torch.rand(batch_size, seq_len, dim_pos) * 100
) # assuming positional values
src_mask = torch.ones(batch_size, seq_len) # assuming all tokens are valid
# Normalize positional information as per the REDEncoder forward method
pos_src_tokens /= 50.0 # Using the max_dist value from the REDEncoder
idxs_src_tokens = idxs_src_tokens.to(device)
pos_src_tokens = pos_src_tokens.to(device)
src_mask = src_mask.bool().to(device)
# Create an instance of the model
red_encoder = REDEncoder().to(device)
print(idxs_src_tokens.device)
print(pos_src_tokens.device)
print(src_mask.device)
print(red_encoder.device)
# Call the encoder with dummy data
output = red_encoder(idxs_src_tokens, pos_src_tokens, src_mask)
# Output shape will depend on the model's internal configurations
print("Output shape:", output.shape)