forked from certik/fastGPT
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gpt2.f90
275 lines (260 loc) · 9.24 KB
/
gpt2.f90
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
module gpt2_mod
use linalg, only: matmul_2d, matmul_2d_t
use tokenizer, only: decode
implicit none
integer, parameter :: sp = kind(0.0)
real(sp), parameter :: pi = 3.14159265358979323846_sp
contains
elemental real(sp) function fast_tanh(x) result(y)
real(sp), intent(in) :: x
real(sp) :: x2
if (x > 5) then
y = 1
elseif (x < -5) then
y = -1
else
x2 = x*x
y = x * (0.98569772605911309407 x2 *(-0.2794500993392901382 &
x2 * (6.8280504526399188164e-2 x2 * (-1.0972014877337651823e-2 &
x2 * (1.1132367134444316902e-3 x2 * (-7.018851897305717565e-5 &
x2 * (2.656616768082727089e-6 x2 * (-5.5138381821615909058e-8 &
x2 * 4.8162484477588665996e-10))))))))
end if
end function
elemental real(sp) function gelu(x) result(y)
real(sp), intent(in) :: x
y = 0.5_sp * x * (1 tanh(sqrt(2 / pi) * (x 0.044715_sp * x**3)))
end function
function softmax(x) result(y)
real(sp), intent(in) :: x(:,:)
real(sp) :: y(size(x,1),size(x,2))
integer :: i
do i = 1, size(x,2)
y(:,i) = exp(x(:,i) - maxval(x(:,i)))
y(:,i) = y(:,i) / sum(y(:,i))
end do
end function
function layer_norm(x, g, b, eps) result(y)
real(sp), intent(in) :: x(:,:), g(:), b(:), eps
real(sp) :: y(size(x,1),size(x,2))
real(sp) :: mean(size(x,2)), variance(size(x,2))
integer :: i
do i = 1, size(x,2)
mean(i) = sum(x(:,i)) / size(x,1)
variance(i) = sum((x(:,i) - mean(i))**2) / size(x,1)
end do
!do i = 1, size(x,1)
! y(i,:) = (x(i,:) - mean(:)) / sqrt(variance(:) eps)
! y(i,:) = g(i) * y(i,:) b(i)
!end do
do i = 1, size(x,2)
y(:,i) = (x(:,i) - mean(i)) / sqrt(variance(i) eps)
y(:,i) = g(:) * y(:,i) b(:)
end do
end function
function linear(x, w, b) result(y)
real(sp), intent(in) :: x(:,:), w(:,:), b(:)
real(sp) :: y(size(b,1),size(x,2))
integer :: i
!y = matmul(w, x) spread(b, 2, size(x,2))
!y = matmul(w, x)
call matmul_2d(w, x, y)
do i = 1, size(y,2)
y(:,i) = y(:,i) b(:)
end do
end function
function ffn(x, fc_w, fc_b, proj_w, proj_b) result(y)
real(sp), intent(in) :: x(:,:), fc_w(:,:), fc_b(:), proj_w(:,:), proj_b(:)
real(sp) :: y(size(x,1),size(x,2))
!real(sp) :: a(4*size(x,1),size(x,2))
!a = gelu(linear(x, fc_w, fc_b))
y = linear(gelu(linear(x, fc_w, fc_b)), proj_w, proj_b)
end function
function attention(n_embd_head,n_seq,n_seq_x, q, k, v, mask) result(y)
integer, intent(in) :: n_embd_head, n_seq, n_seq_x
real(sp), intent(in) :: q(n_embd_head,n_seq_x), k(n_embd_head,n_seq), v(n_embd_head,n_seq), mask(n_seq,n_seq_x)
real(sp) :: y(n_embd_head,n_seq_x)
real(sp) :: tmp(n_seq,n_seq_x)
!tmp = matmul(transpose(k), q)
!call matmul_2d(transpose(k), q, tmp)
call matmul_2d_t(k, q, tmp)
call matmul_2d(v, softmax(tmp / sqrt(real(n_embd_head,sp)) mask), y)
end function
function mha(n_seq, n_seq_x, n_embd, x, attn_w, attn_b, proj_w, proj_b, n_head, &
use_kv_cache, kv_cache) &
result(y)
integer, intent(in) :: n_seq, n_seq_x, n_embd
real(sp), intent(in) :: x(n_embd,n_seq_x), &
attn_w(3*n_embd,n_embd), attn_b(3*n_embd), &
proj_w(n_embd,n_embd), proj_b(n_embd)
real(sp), intent(inout) :: kv_cache(n_embd,n_seq,2)
integer, intent(in) :: n_head
logical, intent(in) :: use_kv_cache
real(sp) :: y(n_embd,n_seq_x)
real(sp) :: causal_mask(n_seq,n_seq_x)
real(sp) :: x2(3*n_embd,n_seq_x)
integer :: i, j
! Mask
if (use_kv_cache) then
causal_mask = 0
else
do j = 1, n_seq
do i = 1, n_seq
if (i > j) then
causal_mask(i,j) = -1e10_sp
else
causal_mask(i,j) = 0
end if
end do
end do
end if
x2 = linear(x, attn_w, attn_b)
associate ( &
q => x2((1-1)*n_embd 1:1*n_embd,:), &
k => x2((2-1)*n_embd 1:2*n_embd,:), &
v => x2((3-1)*n_embd 1:3*n_embd,:) &
)
if (use_kv_cache) then
kv_cache(:,n_seq,1) = k(:,1)
kv_cache(:,n_seq,2) = v(:,1)
else
kv_cache(:,:,1) = k
kv_cache(:,:,2) = v
end if
end associate
associate ( &
q => x2((1-1)*n_embd 1:1*n_embd,:), &
k => kv_cache(:,:,1), &
v => kv_cache(:,:,2) &
)
! Perform attention over each head
do i = 1, n_head
y((i-1)*n_embd/n_head 1:i*n_embd/n_head,:) = attention( &
n_embd/n_head, n_seq, n_seq_x, &
q((i-1)*n_embd/n_head 1:i*n_embd/n_head,:), &
k((i-1)*n_embd/n_head 1:i*n_embd/n_head,:), &
v((i-1)*n_embd/n_head 1:i*n_embd/n_head,:), &
causal_mask)
end do
end associate
! Out projection
y = linear(y, proj_w, proj_b)
end function
function transformer_block(n_seq, n_seq_x, n_embd, x, mlp_fc_w, mlp_fc_b, mlp_proj_w, mlp_proj_b, &
attn_w, attn_b, attn_proj_w, attn_proj_b, ln1_g, ln1_b, ln2_g, ln2_b, &
n_head, use_kv_cache, kv_cache) result(y)
real(sp), intent(in) :: x(n_embd,n_seq_x), &
mlp_fc_w(:,:), mlp_fc_b(:), &
mlp_proj_w(:,:), mlp_proj_b(:), &
attn_w(:,:), attn_b(:), attn_proj_w(:,:), attn_proj_b(:), &
ln1_g(:), ln1_b(:), ln2_g(:), ln2_b(:)
integer, intent(in) :: n_head
integer, intent(in) :: n_seq, n_seq_x, n_embd
real(sp) :: y(n_embd,n_seq_x)
logical, intent(in) :: use_kv_cache
real(sp), intent(inout) :: kv_cache(n_embd,n_seq,2)
y = x mha(n_seq, n_seq_x, n_embd, layer_norm(x, ln1_g, ln1_b, 1e-5_sp), &
attn_w, attn_b, attn_proj_w, attn_proj_b, n_head, use_kv_cache, kv_cache)
y = y ffn(layer_norm(y, ln2_g, ln2_b, 1e-5_sp), &
mlp_fc_w, mlp_fc_b, mlp_proj_w, mlp_proj_b)
end function
function gpt2(n_vocab, n_ctx, n_seq, n_seq_x, n_embd, n_layer, n_head, input, &
wte, wpe, &
mlp_fc_w, mlp_fc_b, mlp_proj_w, mlp_proj_b, &
attn_w, attn_b, attn_proj_w, attn_proj_b, &
ln1_g, ln1_b, ln2_g, ln2_b, lnf_g, lnf_b, &
use_kv_cache, kv_cache) result(y)
integer, intent(in) :: n_vocab, n_ctx, n_seq, n_seq_x, n_embd, n_layer, n_head
integer, intent(in) :: input(n_seq)
real(sp), intent(in) :: wte(n_embd,n_vocab), wpe(n_embd,n_ctx), &
mlp_fc_w(4*n_embd,n_embd,n_layer), mlp_fc_b(4*n_embd,n_layer), &
mlp_proj_w(n_embd,4*n_embd,n_layer), mlp_proj_b(n_embd,n_layer), &
attn_w(3*n_embd,n_embd,n_layer), attn_b(3*n_embd,n_layer), &
attn_proj_w(n_embd,n_embd,n_layer), attn_proj_b(n_embd,n_layer), &
ln1_b(n_embd,n_layer), ln1_g(n_embd,n_layer), &
ln2_b(n_embd,n_layer), ln2_g(n_embd,n_layer), &
lnf_b(n_embd), lnf_g(n_embd)
logical, intent(in) :: use_kv_cache
real(sp), intent(inout) :: kv_cache(n_embd,n_seq,2,n_layer)
real(sp) :: y(n_vocab,n_seq_x)
real(sp) :: x(n_embd,n_seq_x)
integer :: i
if (use_kv_cache) then
i = n_seq
x(:,1) = wte(:,input(i) 1) wpe(:,i)
else
do i = 1, n_seq
x(:,i) = wte(:,input(i) 1) wpe(:,i)
end do
end if
do i = 1, n_layer
x = transformer_block(n_seq, n_seq_x, n_embd, x, &
mlp_fc_w(:,:,i), mlp_fc_b(:,i), &
mlp_proj_w(:,:,i), mlp_proj_b(:,i), &
attn_w(:,:,i), attn_b(:,i), attn_proj_w(:,:,i), attn_proj_b(:,i), &
ln1_g(:,i), ln1_b(:,i), ln2_g(:,i), ln2_b(:,i), &
n_head, use_kv_cache, kv_cache(:,:,:,i))
end do
x = layer_norm(x, lnf_g, lnf_b, 1e-5)
!y = matmul(transpose(wte), x)
call matmul_2d_t(wte, x, y)
end function
function generate(n_tokens_to_generate, &
n_vocab, n_ctx, n_seq, n_embd, n_layer, n_head, input, &
wte, wpe, &
mlp_fc_w, mlp_fc_b, mlp_proj_w, mlp_proj_b, &
attn_w, attn_b, attn_proj_w, attn_proj_b, &
ln1_g, ln1_b, ln2_g, ln2_b, lnf_g, lnf_b, use_cache, &
decoder_idx, decoder_txt, byte_decoder) result(output)
integer, intent(in) :: n_vocab, n_ctx, n_seq, n_embd, n_layer, n_head, &
n_tokens_to_generate
integer, intent(in) :: input(n_seq)
real(sp), intent(in) :: wte(n_embd,n_vocab), wpe(n_embd,n_ctx), &
mlp_fc_w(4*n_embd,n_embd,n_layer), mlp_fc_b(4*n_embd,n_layer), &
mlp_proj_w(n_embd,4*n_embd,n_layer), mlp_proj_b(n_embd,n_layer), &
attn_w(3*n_embd,n_embd,n_layer), attn_b(3*n_embd,n_layer), &
attn_proj_w(n_embd,n_embd,n_layer), attn_proj_b(n_embd,n_layer), &
ln1_b(n_embd,n_layer), ln1_g(n_embd,n_layer), &
ln2_b(n_embd,n_layer), ln2_g(n_embd,n_layer), &
lnf_b(n_embd), lnf_g(n_embd)
logical, intent(in) :: use_cache
integer, intent(in) :: decoder_idx(:), byte_decoder(:)
character, intent(in) :: decoder_txt(:)
integer :: output(n_tokens_to_generate)
real(sp), allocatable :: logits(:,:)
integer :: i
integer :: n_seq2, n_seq_x
integer :: next_id
integer, allocatable :: input2(:)
logical :: use_kv_cache
real(sp) :: kv_cache(n_embd,n_seq n_tokens_to_generate,2,n_layer)
allocate(input2(size(input)))
input2 = input
do i = 1, n_tokens_to_generate
if (use_cache) then
use_kv_cache = (i > 1) ! Use cache for subsequent tokens
else
use_kv_cache = .false.
end if
n_seq2 = size(input2)
if (use_kv_cache) then
n_seq_x = 1
else
n_seq_x = n_seq2
end if
allocate(logits(n_vocab, n_seq_x))
logits = gpt2(n_vocab, n_ctx, n_seq2, n_seq_x, n_embd, n_layer, n_head, &
input2, &
wte, wpe, &
mlp_fc_w, mlp_fc_b, mlp_proj_w, mlp_proj_b, &
attn_w, attn_b, attn_proj_w, attn_proj_b, &
ln1_g, ln1_b, ln2_g, ln2_b, lnf_g, lnf_b, use_kv_cache, kv_cache(:,:n_seq2,:,:))
next_id = maxloc(logits(:,n_seq_x), dim=1)-1
write(*, fmt="(a)", advance="no") decode([next_id], decoder_idx, decoder_txt, byte_decoder)
input2 = [input2, next_id]
deallocate(logits)
end do
output = input2(n_seq 1:)
print *
end function
end module