2022-07-02 13:06:22 +00:00
|
|
|
from typing import Tuple, List
|
2022-06-27 15:57:56 +00:00
|
|
|
import torch
|
|
|
|
from torch import LongTensor, nn, FloatTensor, BoolTensor
|
2022-06-29 01:28:36 +00:00
|
|
|
torch.set_grad_enabled(False)
|
2022-06-27 15:57:56 +00:00
|
|
|
|
2022-07-01 23:44:24 +00:00
|
|
|
from .dalle_bart_encoder import GLU, AttentionBase
|
2022-06-27 15:57:56 +00:00
|
|
|
|
2022-07-04 20:06:49 +00:00
|
|
|
IMAGE_TOKEN_COUNT = 256
|
|
|
|
BLANK_TOKEN = 6965
|
|
|
|
|
2022-06-27 15:57:56 +00:00
|
|
|
|
2022-07-01 23:44:24 +00:00
|
|
|
class DecoderCrossAttention(AttentionBase):
|
2022-06-27 15:57:56 +00:00
|
|
|
def forward(
|
|
|
|
self,
|
|
|
|
decoder_state: FloatTensor,
|
|
|
|
encoder_state: FloatTensor,
|
|
|
|
attention_mask: BoolTensor
|
|
|
|
) -> FloatTensor:
|
|
|
|
keys = self.k_proj.forward(encoder_state)
|
|
|
|
values = self.v_proj.forward(encoder_state)
|
|
|
|
queries = self.q_proj.forward(decoder_state)
|
|
|
|
return super().forward(keys, values, queries, attention_mask)
|
|
|
|
|
|
|
|
|
2022-07-01 23:44:24 +00:00
|
|
|
class DecoderSelfAttention(AttentionBase):
|
2022-07-04 20:06:49 +00:00
|
|
|
def __init__(self, head_count: int, embed_count: int):
|
2022-07-04 12:05:55 +00:00
|
|
|
super().__init__(head_count, embed_count)
|
2022-07-04 20:06:49 +00:00
|
|
|
token_indices = torch.arange(IMAGE_TOKEN_COUNT)
|
2022-07-04 12:05:55 +00:00
|
|
|
if torch.cuda.is_available(): token_indices = token_indices.cuda()
|
|
|
|
self.token_indices = token_indices
|
|
|
|
|
2022-06-29 13:42:12 +00:00
|
|
|
def forward(
|
|
|
|
self,
|
2022-06-27 15:57:56 +00:00
|
|
|
decoder_state: FloatTensor,
|
2022-06-29 18:56:28 +00:00
|
|
|
attention_state: FloatTensor,
|
2022-07-04 12:05:55 +00:00
|
|
|
token_index: LongTensor
|
2022-07-02 13:06:22 +00:00
|
|
|
) -> Tuple[FloatTensor, FloatTensor]:
|
2022-06-29 17:48:12 +00:00
|
|
|
keys = self.k_proj.forward(decoder_state)
|
|
|
|
values = self.v_proj.forward(decoder_state)
|
|
|
|
queries = self.q_proj.forward(decoder_state)
|
2022-07-04 12:05:55 +00:00
|
|
|
attn_mask = self.token_indices < token_index + 1
|
|
|
|
attn_mask = attn_mask[None][[0] * decoder_state.shape[0]]
|
2022-07-07 12:21:20 +00:00
|
|
|
attn_state_new = torch.cat([keys, values]).to(attention_state.dtype)
|
|
|
|
attention_state[:, token_index] = attn_state_new
|
2022-06-29 18:56:28 +00:00
|
|
|
batch_count = decoder_state.shape[0]
|
2022-06-30 11:41:31 +00:00
|
|
|
keys = attention_state[:batch_count]
|
|
|
|
values = attention_state[batch_count:]
|
2022-07-04 12:05:55 +00:00
|
|
|
decoder_state = super().forward(keys, values, queries, attn_mask)
|
2022-06-29 18:56:28 +00:00
|
|
|
return decoder_state, attention_state
|
2022-06-27 15:57:56 +00:00
|
|
|
|
|
|
|
|
2022-07-01 23:44:24 +00:00
|
|
|
class DecoderLayer(nn.Module):
|
2022-06-29 13:42:12 +00:00
|
|
|
def __init__(
|
|
|
|
self,
|
2022-06-27 15:57:56 +00:00
|
|
|
head_count: int,
|
|
|
|
embed_count: int,
|
|
|
|
glu_embed_count: int
|
|
|
|
):
|
|
|
|
super().__init__()
|
|
|
|
self.pre_self_attn_layer_norm = nn.LayerNorm(embed_count)
|
2022-07-04 20:06:49 +00:00
|
|
|
self.self_attn = DecoderSelfAttention(head_count, embed_count)
|
2022-06-27 15:57:56 +00:00
|
|
|
self.self_attn_layer_norm = nn.LayerNorm(embed_count)
|
|
|
|
self.pre_encoder_attn_layer_norm = nn.LayerNorm(embed_count)
|
2022-07-01 23:44:24 +00:00
|
|
|
self.encoder_attn = DecoderCrossAttention(head_count, embed_count)
|
2022-06-27 15:57:56 +00:00
|
|
|
self.encoder_attn_layer_norm = nn.LayerNorm(embed_count)
|
2022-07-01 23:44:24 +00:00
|
|
|
self.glu = GLU(embed_count, glu_embed_count)
|
2022-06-27 15:57:56 +00:00
|
|
|
|
2022-06-29 01:28:36 +00:00
|
|
|
|
2022-06-29 13:42:12 +00:00
|
|
|
def forward(
|
|
|
|
self,
|
2022-06-27 15:57:56 +00:00
|
|
|
decoder_state: FloatTensor,
|
|
|
|
encoder_state: FloatTensor,
|
2022-06-29 18:56:28 +00:00
|
|
|
attention_state: FloatTensor,
|
2022-06-27 15:57:56 +00:00
|
|
|
attention_mask: BoolTensor,
|
|
|
|
token_index: LongTensor
|
2022-07-02 13:06:22 +00:00
|
|
|
) -> Tuple[FloatTensor, FloatTensor]:
|
2022-06-27 15:57:56 +00:00
|
|
|
# Self Attention
|
|
|
|
residual = decoder_state
|
|
|
|
decoder_state = self.pre_self_attn_layer_norm.forward(decoder_state)
|
2022-06-29 18:56:28 +00:00
|
|
|
decoder_state, attention_state = self.self_attn.forward(
|
2022-06-27 15:57:56 +00:00
|
|
|
decoder_state,
|
2022-06-29 18:56:28 +00:00
|
|
|
attention_state,
|
2022-07-04 12:05:55 +00:00
|
|
|
token_index
|
2022-06-27 15:57:56 +00:00
|
|
|
)
|
|
|
|
decoder_state = self.self_attn_layer_norm.forward(decoder_state)
|
|
|
|
decoder_state = residual + decoder_state
|
|
|
|
|
|
|
|
# Cross Attention
|
|
|
|
residual = decoder_state
|
|
|
|
decoder_state = self.pre_encoder_attn_layer_norm.forward(decoder_state)
|
|
|
|
decoder_state = self.encoder_attn.forward(
|
|
|
|
decoder_state,
|
|
|
|
encoder_state,
|
|
|
|
attention_mask
|
|
|
|
)
|
|
|
|
decoder_state = self.encoder_attn_layer_norm.forward(decoder_state)
|
|
|
|
decoder_state = residual + decoder_state
|
|
|
|
|
|
|
|
# Feed forward
|
|
|
|
residual = decoder_state
|
|
|
|
decoder_state = self.glu.forward(decoder_state)
|
|
|
|
decoder_state = residual + decoder_state
|
|
|
|
|
2022-06-29 18:56:28 +00:00
|
|
|
return decoder_state, attention_state
|
2022-06-27 15:57:56 +00:00
|
|
|
|
|
|
|
|
2022-07-01 23:44:24 +00:00
|
|
|
class DalleBartDecoder(nn.Module):
|
2022-06-29 13:42:12 +00:00
|
|
|
def __init__(
|
|
|
|
self,
|
2022-07-01 16:03:37 +00:00
|
|
|
image_vocab_count: int,
|
2022-06-27 15:57:56 +00:00
|
|
|
embed_count: int,
|
|
|
|
attention_head_count: int,
|
|
|
|
glu_embed_count: int,
|
|
|
|
layer_count: int,
|
2022-07-01 16:03:37 +00:00
|
|
|
start_token: int
|
2022-06-27 15:57:56 +00:00
|
|
|
):
|
|
|
|
super().__init__()
|
|
|
|
self.layer_count = layer_count
|
2022-07-02 12:45:49 +00:00
|
|
|
self.embed_count = embed_count
|
2022-07-01 16:03:37 +00:00
|
|
|
self.embed_tokens = nn.Embedding(image_vocab_count + 1, embed_count)
|
2022-07-04 20:06:49 +00:00
|
|
|
self.embed_positions = nn.Embedding(IMAGE_TOKEN_COUNT, embed_count)
|
2022-07-02 13:06:22 +00:00
|
|
|
self.layers: List[DecoderLayer] = nn.ModuleList([
|
2022-07-01 23:44:24 +00:00
|
|
|
DecoderLayer(
|
2022-06-27 15:57:56 +00:00
|
|
|
attention_head_count,
|
|
|
|
embed_count,
|
|
|
|
glu_embed_count
|
|
|
|
)
|
|
|
|
for _ in range(layer_count)
|
|
|
|
])
|
|
|
|
self.layernorm_embedding = nn.LayerNorm(embed_count)
|
|
|
|
self.final_ln = nn.LayerNorm(embed_count)
|
2022-07-01 16:03:37 +00:00
|
|
|
self.lm_head = nn.Linear(embed_count, image_vocab_count + 1, bias=False)
|
2022-06-29 01:28:36 +00:00
|
|
|
self.zero_prob = torch.zeros([1])
|
2022-07-04 20:06:49 +00:00
|
|
|
self.token_indices = torch.arange(IMAGE_TOKEN_COUNT)
|
2022-06-29 01:28:36 +00:00
|
|
|
self.start_token = torch.tensor([start_token]).to(torch.long)
|
|
|
|
if torch.cuda.is_available():
|
|
|
|
self.zero_prob = self.zero_prob.cuda()
|
|
|
|
self.token_indices = self.token_indices.cuda()
|
|
|
|
self.start_token = self.start_token.cuda()
|
2022-06-27 15:57:56 +00:00
|
|
|
|
|
|
|
|
2022-06-29 13:42:12 +00:00
|
|
|
def decode_step(
|
|
|
|
self,
|
2022-07-05 21:23:05 +00:00
|
|
|
log2_k: int,
|
2022-07-05 01:30:27 +00:00
|
|
|
log2_supercondition_factor: int,
|
2022-07-02 12:45:49 +00:00
|
|
|
attention_mask: BoolTensor,
|
2022-06-27 15:57:56 +00:00
|
|
|
encoder_state: FloatTensor,
|
2022-06-29 18:56:28 +00:00
|
|
|
attention_state: FloatTensor,
|
2022-07-02 12:45:49 +00:00
|
|
|
prev_tokens: LongTensor,
|
2022-06-30 13:04:11 +00:00
|
|
|
token_index: LongTensor
|
2022-07-04 13:14:37 +00:00
|
|
|
) -> Tuple[FloatTensor, FloatTensor]:
|
2022-07-02 12:45:49 +00:00
|
|
|
image_count = encoder_state.shape[0] // 2
|
|
|
|
token_index_batched = token_index[[0] * image_count * 2]
|
|
|
|
prev_tokens = prev_tokens[list(range(image_count)) * 2]
|
|
|
|
decoder_state = self.embed_tokens.forward(prev_tokens)
|
2022-06-30 13:04:11 +00:00
|
|
|
decoder_state += self.embed_positions.forward(token_index_batched)
|
2022-06-27 15:57:56 +00:00
|
|
|
decoder_state = self.layernorm_embedding.forward(decoder_state)
|
2022-06-27 17:19:03 +00:00
|
|
|
decoder_state = decoder_state[:, None]
|
2022-06-29 18:56:28 +00:00
|
|
|
for i in range(self.layer_count):
|
2022-07-04 13:14:37 +00:00
|
|
|
decoder_state, attention_state[i] = self.layers[i].forward(
|
2022-06-27 15:57:56 +00:00
|
|
|
decoder_state,
|
|
|
|
encoder_state,
|
2022-06-29 18:56:28 +00:00
|
|
|
attention_state[i],
|
2022-06-27 15:57:56 +00:00
|
|
|
attention_mask,
|
2022-06-30 13:04:11 +00:00
|
|
|
token_index
|
2022-06-27 15:57:56 +00:00
|
|
|
)
|
2022-06-27 17:19:03 +00:00
|
|
|
decoder_state = self.final_ln(decoder_state)
|
|
|
|
logits = self.lm_head(decoder_state)
|
2022-07-05 03:29:48 +00:00
|
|
|
a = 2 ** log2_supercondition_factor
|
2022-07-02 12:45:49 +00:00
|
|
|
logits: FloatTensor = (
|
|
|
|
logits[:image_count, -1] * (1 - a) +
|
|
|
|
logits[image_count:, -1] * a
|
|
|
|
)
|
2022-06-27 15:57:56 +00:00
|
|
|
|
2022-07-05 21:23:05 +00:00
|
|
|
top_logits, _ = logits.topk(2 ** log2_k, dim=-1)
|
2022-06-27 15:57:56 +00:00
|
|
|
probs = torch.where(
|
2022-07-02 12:45:49 +00:00
|
|
|
logits < top_logits[:, [-1]],
|
2022-06-29 01:28:36 +00:00
|
|
|
self.zero_prob,
|
2022-07-02 12:45:49 +00:00
|
|
|
torch.exp(logits - top_logits[:, [0]])
|
2022-06-27 15:57:56 +00:00
|
|
|
)
|
2022-07-04 13:14:37 +00:00
|
|
|
return probs, attention_state
|
2022-07-04 20:06:49 +00:00
|
|
|
|
2022-07-04 13:58:00 +00:00
|
|
|
|
|
|
|
def decode_row(
|
|
|
|
self,
|
|
|
|
row_index: int,
|
2022-07-05 21:23:05 +00:00
|
|
|
log2_k: int,
|
2022-07-05 01:30:27 +00:00
|
|
|
log2_supercondition_factor: int,
|
2022-07-04 13:58:00 +00:00
|
|
|
encoder_state: FloatTensor,
|
2022-07-04 20:06:49 +00:00
|
|
|
attention_mask: BoolTensor,
|
2022-07-04 13:58:00 +00:00
|
|
|
attention_state: FloatTensor,
|
|
|
|
image_tokens_sequence: LongTensor
|
|
|
|
) -> Tuple[FloatTensor, LongTensor]:
|
|
|
|
for col_index in range(16):
|
|
|
|
i = 16 * row_index + col_index
|
|
|
|
probs, attention_state = self.decode_step(
|
2022-07-05 21:23:05 +00:00
|
|
|
log2_k = log2_k,
|
2022-07-05 01:30:27 +00:00
|
|
|
log2_supercondition_factor = log2_supercondition_factor,
|
2022-07-04 13:58:00 +00:00
|
|
|
attention_mask = attention_mask,
|
|
|
|
encoder_state = encoder_state,
|
|
|
|
attention_state = attention_state,
|
|
|
|
prev_tokens = image_tokens_sequence[:, i],
|
|
|
|
token_index = self.token_indices[[i]]
|
|
|
|
)
|
|
|
|
image_tokens_sequence[:, i + 1] = torch.multinomial(probs, 1)[:, 0]
|
|
|
|
|
|
|
|
return attention_state, image_tokens_sequence
|
2022-06-27 15:57:56 +00:00
|
|
|
|
2022-07-04 20:06:49 +00:00
|
|
|
|
|
|
|
def decode_initial(
|
2022-06-29 13:42:12 +00:00
|
|
|
self,
|
2022-07-04 20:06:49 +00:00
|
|
|
seed: int,
|
2022-07-02 12:45:49 +00:00
|
|
|
image_count: int,
|
2022-06-27 15:57:56 +00:00
|
|
|
text_tokens: LongTensor,
|
|
|
|
encoder_state: FloatTensor
|
2022-07-04 20:06:49 +00:00
|
|
|
) -> Tuple[FloatTensor, FloatTensor, FloatTensor, LongTensor]:
|
2022-07-02 12:45:49 +00:00
|
|
|
expanded_indices = [0] * image_count + [1] * image_count
|
|
|
|
text_tokens = text_tokens[expanded_indices]
|
|
|
|
encoder_state = encoder_state[expanded_indices]
|
|
|
|
attention_mask = text_tokens.not_equal(1)
|
2022-06-27 15:57:56 +00:00
|
|
|
|
2022-07-02 12:45:49 +00:00
|
|
|
attention_state_shape = (
|
|
|
|
self.layer_count,
|
|
|
|
image_count * 4,
|
2022-07-04 20:06:49 +00:00
|
|
|
IMAGE_TOKEN_COUNT,
|
2022-07-02 12:45:49 +00:00
|
|
|
self.embed_count
|
|
|
|
)
|
|
|
|
attention_state = torch.zeros(attention_state_shape)
|
2022-07-04 13:14:37 +00:00
|
|
|
image_tokens_sequence = torch.full(
|
2022-07-04 20:06:49 +00:00
|
|
|
(image_count, IMAGE_TOKEN_COUNT + 1),
|
|
|
|
BLANK_TOKEN,
|
2022-07-04 13:14:37 +00:00
|
|
|
dtype=torch.long
|
|
|
|
)
|
|
|
|
if torch.cuda.is_available():
|
|
|
|
attention_state = attention_state.cuda()
|
|
|
|
image_tokens_sequence = image_tokens_sequence.cuda()
|
2022-07-02 12:45:49 +00:00
|
|
|
|
2022-07-04 13:58:00 +00:00
|
|
|
image_tokens_sequence[:, 0] = self.start_token[0]
|
2022-06-27 15:57:56 +00:00
|
|
|
|
2022-07-04 20:06:49 +00:00
|
|
|
if seed > 0: torch.manual_seed(seed)
|
|
|
|
|
|
|
|
return encoder_state, attention_mask, attention_state, image_tokens_sequence
|