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
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import math
|
| 2 |
+
import logging
|
| 3 |
+
import torch
|
| 4 |
+
import torch.nn as nn
|
| 5 |
+
from torch.nn import functional as F
|
| 6 |
+
logger = logging.getLogger(__name__)
|
| 7 |
+
|
| 8 |
+
|
| 9 |
+
class RWKV_TimeMix(nn.Module):
|
| 10 |
+
def __init__(self, config, layer_id):
|
| 11 |
+
super().__init__()
|
| 12 |
+
assert config.n_attn % config.n_head == 0
|
| 13 |
+
self.layer_id = layer_id
|
| 14 |
+
self.ctx_len = config.ctx_len
|
| 15 |
+
self.n_head = config.n_head
|
| 16 |
+
self.head_size = config.n_attn // config.n_head
|
| 17 |
+
|
| 18 |
+
self.time_ww = nn.Parameter(
|
| 19 |
+
torch.ones(config.n_head, config.ctx_len, config.ctx_len))
|
| 20 |
+
self.time_gamma = nn.Parameter(torch.ones(config.ctx_len, 1))
|
| 21 |
+
|
| 22 |
+
self.time_shift = nn.ZeroPad2d((0, 0, 1, -1))
|
| 23 |
+
|
| 24 |
+
self.key = nn.Linear(config.n_embd, config.n_attn)
|
| 25 |
+
self.value = nn.Linear(config.n_embd, config.n_attn)
|
| 26 |
+
self.receptance = nn.Linear(config.n_embd, config.n_attn)
|
| 27 |
+
|
| 28 |
+
self.output = nn.Linear(config.n_attn, config.n_embd)
|
| 29 |
+
|
| 30 |
+
self.key.scale_init = 0
|
| 31 |
+
self.receptance.scale_init = 0
|
| 32 |
+
self.output.scale_init = 0
|
| 33 |
+
|
| 34 |
+
def forward(self, x):
|
| 35 |
+
B, T, C = x.size()
|
| 36 |
+
|
| 37 |
+
x = torch.cat(
|
| 38 |
+
[self.time_shift(x[:, :, :C//2]), x[:, :, C//2:]], dim=-1)
|
| 39 |
+
|
| 40 |
+
k = self.key(x)
|
| 41 |
+
v = self.value(x)
|
| 42 |
+
r = self.receptance(x)
|
| 43 |
+
|
| 44 |
+
k = torch.clamp(k, max=30, min=-60)
|
| 45 |
+
k = torch.exp(k)
|
| 46 |
+
sum_k = torch.cumsum(k, dim=1)
|
| 47 |
+
|
| 48 |
+
kv = (k * v).view(B, T, self.n_head, self.head_size)
|
| 49 |
+
|
| 50 |
+
wkv = (torch.einsum('htu,buhc->bthc', self.time_ww[:,:T,:T], kv)
|
| 51 |
+
).contiguous().view(B, T, -1)
|
| 52 |
+
|
| 53 |
+
rwkv = torch.sigmoid(r) * wkv / sum_k
|
| 54 |
+
|
| 55 |
+
rwkv = self.output(rwkv)
|
| 56 |
+
return rwkv * self.time_gamma[:T, :]
|
| 57 |
+
|
| 58 |
+
class RWKV_ChannelMix(nn.Module):
|
| 59 |
+
def __init__(self, config, layer_id):
|
| 60 |
+
super().__init__()
|
| 61 |
+
self.layer_id = layer_id
|
| 62 |
+
self.time_shift = nn.ZeroPad2d((0, 0, 1, -1))
|
| 63 |
+
|
| 64 |
+
hidden_sz = 5 * config.n_ffn // 2
|
| 65 |
+
self.key = nn.Linear(config.n_embd, hidden_sz)
|
| 66 |
+
self.value = nn.Linear(config.n_embd, hidden_sz)
|
| 67 |
+
self.weight = nn.Linear(hidden_sz, config.n_embd)
|
| 68 |
+
self.receptance = nn.Linear(config.n_embd, config.n_embd)
|
| 69 |
+
|
| 70 |
+
self.receptance.scale_init = 0
|
| 71 |
+
self.weight.scale_init = 0
|
| 72 |
+
|
| 73 |
+
def forward(self, x):
|
| 74 |
+
B, T, C = x.size()
|
| 75 |
+
|
| 76 |
+
x = torch.cat(
|
| 77 |
+
[self.time_shift(x[:, :, :C//2]), x[:, :, C//2:]], dim=-1)
|
| 78 |
+
k = self.key(x)
|
| 79 |
+
v = self.value(x)
|
| 80 |
+
r = self.receptance(x)
|
| 81 |
+
|
| 82 |
+
wkv = self.weight(F.mish(k) * v)
|
| 83 |
+
|
| 84 |
+
rwkv = torch.sigmoid(r) * wkv
|
| 85 |
+
|
| 86 |
+
return rwkv
|
| 87 |
+
|
| 88 |
+
|
| 89 |
+
class GPTConfig:
|
| 90 |
+
def __init__(self, vocab_size, ctx_len, **kwargs):
|
| 91 |
+
self.vocab_size = vocab_size
|
| 92 |
+
self.ctx_len = ctx_len
|
| 93 |
+
for k, v in kwargs.items():
|
| 94 |
+
setattr(self, k, v)
|
| 95 |
+
|
| 96 |
+
|
| 97 |
+
class Block(nn.Module):
|
| 98 |
+
def __init__(self, config, layer_id):
|
| 99 |
+
super().__init__()
|
| 100 |
+
self.config = config
|
| 101 |
+
|
| 102 |
+
self.ln1 = nn.LayerNorm(config.n_embd)
|
| 103 |
+
self.ln2 = nn.LayerNorm(config.n_embd)
|
| 104 |
+
|
| 105 |
+
self.attn = RWKV_TimeMix(config, layer_id)
|
| 106 |
+
self.mlp = RWKV_ChannelMix(config, layer_id)
|
| 107 |
+
|
| 108 |
+
def forward(self, x):
|
| 109 |
+
|
| 110 |
+
x = x + self.attn(self.ln1(x))
|
| 111 |
+
x = x + self.mlp(self.ln2(x))
|
| 112 |
+
|
| 113 |
+
return x
|
| 114 |
+
|
| 115 |
+
|
| 116 |
+
class GPT(nn.Module):
|
| 117 |
+
def __init__(self, config):
|
| 118 |
+
super().__init__()
|
| 119 |
+
self.config = config
|
| 120 |
+
|
| 121 |
+
self.tok_emb = nn.Embedding(config.vocab_size, config.n_embd)
|
| 122 |
+
|
| 123 |
+
self.blocks = nn.Sequential(*[Block(config, i)
|
| 124 |
+
for i in range(config.n_layer)])
|
| 125 |
+
|
| 126 |
+
self.ln_f = nn.LayerNorm(config.n_embd)
|
| 127 |
+
self.time_out = nn.Parameter(torch.ones(1, config.ctx_len, 1))
|
| 128 |
+
self.head = nn.Linear(config.n_embd, config.vocab_size, bias=False)
|
| 129 |
+
|
| 130 |
+
self.head_q = nn.Linear(config.n_embd, 256)
|
| 131 |
+
self.head_k = nn.Linear(config.n_embd, 256)
|
| 132 |
+
self.register_buffer("copy_mask", torch.tril(torch.ones(config.ctx_len, config.ctx_len)))
|
| 133 |
+
|
| 134 |
+
self.ctx_len = config.ctx_len
|
| 135 |
+
|
| 136 |
+
logger.info("number of parameters: %e", sum(p.numel()
|
| 137 |
+
for p in self.parameters()))
|
| 138 |
+
|
| 139 |
+
def get_ctx_len(self):
|
| 140 |
+
return self.ctx_len
|
| 141 |
+
|
| 142 |
+
def forward(self, idx, targets=None):
|
| 143 |
+
B, T = idx.size()
|
| 144 |
+
assert T <= self.ctx_len, "Cannot forward, because len(input) > model ctx_len."
|
| 145 |
+
|
| 146 |
+
x = self.tok_emb(idx)
|
| 147 |
+
|
| 148 |
+
x = self.blocks(x)
|
| 149 |
+
|
| 150 |
+
x = self.ln_f(x)
|
| 151 |
+
q = self.head_q(x)[:,:T,:]
|
| 152 |
+
k = self.head_k(x)[:,:T,:]
|
| 153 |
+
c = (q @ k.transpose(-2, -1)) * (1.0 / 256)
|
| 154 |
+
c = c.masked_fill(self.copy_mask[:T,:T] == 0, 0)
|
| 155 |
+
c = c @ F.one_hot(idx, num_classes = self.config.vocab_size).float()
|
| 156 |
+
x = x * self.time_out[:, :T, :]
|
| 157 |
+
x = self.head(x) + c
|
| 158 |
+
|
| 159 |
+
loss = None
|
| 160 |
+
if targets is not None:
|
| 161 |
+
loss = F.cross_entropy(x.view(-1, x.size(-1)), targets.view(-1))
|
| 162 |
+
|
| 163 |
+
return x, loss
|