"""
This code is based on huggingface,
https://github.com/huggingface/pytorch-openai-transformer-lm/blob/master/model_pytorch.py
MIT License
Copyright (c) 2018 OpenAI
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OFS CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
"""
# Arxiv Link https://arxiv.org/pdf/1907.00235.pdf
import numpy as np
import torch
import torch.nn as nn
import math
# from torch.distributions.normal import Normal
import copy
from torch.nn.parameter import Parameter
from typing import Dict
from flood_forecast.transformer_xl.lower_upper_config import activation_dict
[docs]def gelu(x):
return 0.5 * x * (1 + torch.tanh(math.sqrt(2 / math.pi) * (x + 0.044715 * torch.pow(x, 3))))
[docs]def swish(x):
return x * torch.sigmoid(x)
ACT_FNS = {
'relu': nn.ReLU(),
'swish': swish,
'gelu': gelu
}
[docs]class Attention(nn.Module):
[docs] def __init__(self, n_head, n_embd, win_len, scale, q_len, sub_len, sparse=None, attn_pdrop=0.1, resid_pdrop=0.1):
super(Attention, self).__init__()
if(sparse):
print('Activate log sparse!')
mask = self.log_mask(win_len, sub_len)
else:
mask = torch.tril(torch.ones(win_len, win_len)).view(1, 1, win_len, win_len)
self.register_buffer('mask_tri', mask)
self.n_head = n_head
self.split_size = n_embd * self.n_head
self.scale = scale
self.q_len = q_len
self.query_key = nn.Conv1d(n_embd, n_embd * n_head * 2, self.q_len)
self.value = Conv1D(n_embd * n_head, 1, n_embd)
self.c_proj = Conv1D(n_embd, 1, n_embd * self.n_head)
self.attn_dropout = nn.Dropout(attn_pdrop)
self.resid_dropout = nn.Dropout(resid_pdrop)
[docs] def log_mask(self, win_len, sub_len):
mask = torch.zeros((win_len, win_len), dtype=torch.float)
for i in range(win_len):
mask[i] = self.row_mask(i, sub_len, win_len)
return mask.view(1, 1, mask.size(0), mask.size(1))
[docs] def row_mask(self, index, sub_len, win_len):
"""
Remark:
1 . Currently, dense matrices with sparse multiplication are not supported by Pytorch. Efficient implementation
should deal with CUDA kernel, which we haven't implemented yet.
2 . Our default setting here use Local attention and Restart attention.
3 . For index-th row, if its past is smaller than the number of cells the last
cell can attend, we can allow current cell to attend all past cells to fully
utilize parallel computing in dense matrices with sparse multiplication."""
log_l = math.ceil(np.log2(sub_len))
mask = torch.zeros((win_len), dtype=torch.float)
if((win_len // sub_len) * 2 * (log_l) > index):
mask[:(index + 1)] = 1
else:
while(index >= 0):
if((index - log_l + 1) < 0):
mask[:index] = 1
break
mask[index - log_l + 1:(index + 1)] = 1 # Local attention
for i in range(0, log_l):
new_index = index - log_l + 1 - 2**i
if((index - new_index) <= sub_len and new_index >= 0):
mask[new_index] = 1
index -= sub_len
return mask
[docs] def attn(self, query: torch.Tensor, key, value: torch.Tensor, activation="Softmax"):
activation = activation_dict[activation](dim=-1)
pre_att = torch.matmul(query, key)
if self.scale:
pre_att = pre_att / math.sqrt(value.size(-1))
mask = self.mask_tri[:, :, :pre_att.size(-2), :pre_att.size(-1)]
pre_att = pre_att * mask + -1e9 * (1 - mask)
pre_att = activation(pre_att)
pre_att = self.attn_dropout(pre_att)
attn = torch.matmul(pre_att, value)
return attn
[docs] def merge_heads(self, x):
x = x.permute(0, 2, 1, 3).contiguous()
new_x_shape = x.size()[:-2] + (x.size(-2) * x.size(-1),)
return x.view(*new_x_shape)
[docs] def split_heads(self, x, k=False):
new_x_shape = x.size()[:-1] + (self.n_head, x.size(-1) // self.n_head)
x = x.view(*new_x_shape)
if k:
return x.permute(0, 2, 3, 1)
else:
return x.permute(0, 2, 1, 3)
[docs] def forward(self, x):
value = self.value(x)
qk_x = nn.functional.pad(x.permute(0, 2, 1), pad=(self.q_len - 1, 0))
query_key = self.query_key(qk_x).permute(0, 2, 1)
query, key = query_key.split(self.split_size, dim=2)
query = self.split_heads(query)
key = self.split_heads(key, k=True)
value = self.split_heads(value)
attn = self.attn(query, key, value)
attn = self.merge_heads(attn)
attn = self.c_proj(attn)
attn = self.resid_dropout(attn)
return attn
[docs]class Conv1D(nn.Module):
[docs] def __init__(self, out_dim, rf, in_dim):
super(Conv1D, self).__init__()
self.rf = rf
self.out_dim = out_dim
if rf == 1:
w = torch.empty(in_dim, out_dim)
nn.init.normal_(w, std=0.02)
self.w = Parameter(w)
self.b = Parameter(torch.zeros(out_dim))
else:
raise NotImplementedError
[docs] def forward(self, x):
if self.rf == 1:
size_out = x.size()[:-1] + (self.out_dim,)
x = torch.addmm(self.b, x.view(-1, x.size(-1)), self.w)
x = x.view(*size_out)
else:
raise NotImplementedError
return x
[docs]class LayerNorm(nn.Module):
"Construct a layernorm module in the OpenAI style (epsilon inside the square root)."
[docs] def __init__(self, n_embd, e=1e-5):
super(LayerNorm, self).__init__()
self.g = nn.Parameter(torch.ones(n_embd))
self.b = nn.Parameter(torch.zeros(n_embd))
self.e = e
[docs] def forward(self, x):
mu = x.mean(-1, keepdim=True)
sigma = (x - mu).pow(2).mean(-1, keepdim=True)
x = (x - mu) / torch.sqrt(sigma + self.e)
return self.g * x + self.b
[docs]class MLP(nn.Module):
[docs] def __init__(self, n_state, n_embd, acf='relu'):
super(MLP, self).__init__()
n_embd = n_embd
self.c_fc = Conv1D(n_state, 1, n_embd)
self.c_proj = Conv1D(n_embd, 1, n_state)
self.act = ACT_FNS[acf]
self.dropout = nn.Dropout(0.1)
[docs] def forward(self, x):
hidden1 = self.act(self.c_fc(x))
hidden2 = self.c_proj(hidden1)
return self.dropout(hidden2)
[docs]class Block(nn.Module):
[docs] def __init__(self, n_head, win_len, n_embd, scale, q_len, sub_len, additional_params: Dict):
super(Block, self).__init__()
n_embd = n_embd
self.attn = Attention(n_head, n_embd, win_len, scale, q_len, sub_len, **additional_params)
self.ln_1 = LayerNorm(n_embd)
self.mlp = MLP(4 * n_embd, n_embd)
self.ln_2 = LayerNorm(n_embd)
[docs] def forward(self, x):
attn = self.attn(x)
ln1 = self.ln_1(x + attn)
mlp = self.mlp(ln1)
hidden = self.ln_2(ln1 + mlp)
return hidden