-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathmodel.py
606 lines (511 loc) · 26.4 KB
/
model.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
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
########################################################################################################
# The RWKV Language Model - https://github.com/BlinkDL/RWKV-LM
########################################################################################################
import os, math, gc, importlib
import torch
# torch._C._jit_set_profiling_executor(True)
# torch._C._jit_set_profiling_mode(True)
import torch.nn as nn
from torch.nn import functional as F
import pytorch_lightning as pl
from pytorch_lightning.utilities import rank_zero_info, rank_zero_only
from pytorch_lightning.strategies import DeepSpeedStrategy
from transformers import CLIPVisionModel
if importlib.util.find_spec('deepspeed'):
import deepspeed
from deepspeed.ops.adam import DeepSpeedCPUAdam, FusedAdam
# from deepspeed.runtime.fp16.onebit.zoadam import ZeroOneAdam
from .dataset import IGNORE_INDEX, IMAGE_TOKEN_INDEX
def __nop(ob):
return ob
MyModule = nn.Module
MyFunction = __nop
if os.environ["RWKV_JIT_ON"] == "1":
MyModule = torch.jit.ScriptModule
MyFunction = torch.jit.script_method
########################################################################################################
# CUDA Kernel
########################################################################################################
from torch.utils.cpp_extension import load
HEAD_SIZE = int(os.environ["RWKV_HEAD_SIZE_A"])
wkv6_cuda = load(name="wkv6", sources=["cuda/wkv6_op.cpp", f"cuda/wkv6_cuda.cu"],
verbose=True, extra_cuda_cflags=["-res-usage", "--use_fast_math", "-O3", "-Xptxas -O3", "--extra-device-vectorization", f"-D_N_={HEAD_SIZE}", f"-D_T_={int(os.environ['RWKV_CTXLEN'])}"])
class WKV_6(torch.autograd.Function):
@staticmethod
def forward(ctx, B, T, C, H, r, k, v, w, u):
with torch.no_grad():
assert r.dtype == torch.bfloat16
assert k.dtype == torch.bfloat16
assert v.dtype == torch.bfloat16
assert w.dtype == torch.bfloat16
assert u.dtype == torch.bfloat16
assert HEAD_SIZE == C // H
ctx.B = B
ctx.T = T
ctx.C = C
ctx.H = H
assert r.is_contiguous()
assert k.is_contiguous()
assert v.is_contiguous()
assert w.is_contiguous()
assert u.is_contiguous()
ew = (-torch.exp(w.float())).contiguous()
ctx.save_for_backward(r, k, v, ew, u)
y = torch.empty((B, T, C), device=r.device, dtype=torch.bfloat16, memory_format=torch.contiguous_format)#.uniform_(-100, 100)
wkv6_cuda.forward(B, T, C, H, r, k, v, ew, u, y)
return y
@staticmethod
def backward(ctx, gy):
with torch.no_grad():
assert gy.dtype == torch.bfloat16
B = ctx.B
T = ctx.T
C = ctx.C
H = ctx.H
assert gy.is_contiguous()
r, k, v, ew, u = ctx.saved_tensors
gr = torch.empty((B, T, C), device=gy.device, requires_grad=False, dtype=torch.bfloat16, memory_format=torch.contiguous_format)#.uniform_(-100, 100)
gk = torch.empty((B, T, C), device=gy.device, requires_grad=False, dtype=torch.bfloat16, memory_format=torch.contiguous_format)#.uniform_(-100, 100)
gv = torch.empty((B, T, C), device=gy.device, requires_grad=False, dtype=torch.bfloat16, memory_format=torch.contiguous_format)#.uniform_(-100, 100)
gw = torch.empty((B, T, C), device=gy.device, requires_grad=False, dtype=torch.bfloat16, memory_format=torch.contiguous_format)#.uniform_(-100, 100)
gu = torch.empty((B, C), device=gy.device, requires_grad=False, dtype=torch.bfloat16, memory_format=torch.contiguous_format)#.uniform_(-100, 100)
wkv6_cuda.backward(B, T, C, H, r, k, v, ew, u, gy, gr, gk, gv, gw, gu)
gu = torch.sum(gu, 0).view(H, C//H)
return (None, None, None, None, gr, gk, gv, gw, gu)
def RUN_CUDA_RWKV6(B, T, C, H, r, k, v, w, u):
return WKV_6.apply(B, T, C, H, r, k, v, w, u)
########################################################################################################
class RWKV_Tmix_x060(MyModule):
def __init__(self, args, layer_id):
super().__init__()
self.args = args
self.layer_id = layer_id
self.head_size = args.head_size_a
self.n_head = args.dim_att // self.head_size
assert args.dim_att % self.n_head == 0
with torch.no_grad():
ratio_0_to_1 = layer_id / (args.n_layer - 1) # 0 to 1
ratio_1_to_almost0 = 1.0 - (layer_id / args.n_layer) # 1 to ~0
ddd = torch.ones(1, 1, args.n_embd)
for i in range(args.n_embd):
ddd[0, 0, i] = i / args.n_embd
# fancy time_mix
self.time_maa_x = nn.Parameter(1.0 - torch.pow(ddd, ratio_1_to_almost0))
self.time_maa_w = nn.Parameter(1.0 - torch.pow(ddd, ratio_1_to_almost0))
self.time_maa_k = nn.Parameter(1.0 - torch.pow(ddd, ratio_1_to_almost0))
self.time_maa_v = nn.Parameter(1.0 - (torch.pow(ddd, ratio_1_to_almost0) + 0.3 * ratio_0_to_1))
self.time_maa_r = nn.Parameter(1.0 - torch.pow(ddd, 0.5 * ratio_1_to_almost0))
self.time_maa_g = nn.Parameter(1.0 - torch.pow(ddd, 0.5 * ratio_1_to_almost0))
D_MIX_LORA = 32 # generate TIME_MIX for w,k,v,r,g
if args.n_embd >= 4096:
D_MIX_LORA = 64
self.time_maa_w1 = nn.Parameter(torch.zeros(args.n_embd, D_MIX_LORA*5))
self.time_maa_w2 = nn.Parameter(torch.zeros(5, D_MIX_LORA, args.n_embd).uniform_(-0.01, 0.01))
# fancy time_decay
decay_speed = torch.ones(args.dim_att)
for n in range(args.dim_att):
decay_speed[n] = -6 + 5 * (n / (args.dim_att - 1)) ** (0.7 + 1.3 * ratio_0_to_1)
self.time_decay = nn.Parameter(decay_speed.reshape(1,1,args.dim_att))
D_DECAY_LORA = 64
if args.n_embd >= 4096:
D_DECAY_LORA = 128
self.time_decay_w1 = nn.Parameter(torch.zeros(args.n_embd, D_DECAY_LORA))
self.time_decay_w2 = nn.Parameter(torch.zeros(D_DECAY_LORA, args.dim_att).uniform_(-0.01, 0.01))
tmp = torch.zeros(args.dim_att)
for n in range(args.dim_att):
zigzag = ((n + 1) % 3 - 1) * 0.1
tmp[n] = ratio_0_to_1 * (1 - (n / (args.dim_att - 1))) + zigzag
self.time_faaaa = nn.Parameter(tmp.reshape(self.n_head, self.head_size))
self.time_shift = nn.ZeroPad2d((0, 0, 1, -1))
self.receptance = nn.Linear(args.n_embd, args.dim_att, bias=False)
self.key = nn.Linear(args.n_embd, args.dim_att, bias=False)
self.value = nn.Linear(args.n_embd, args.dim_att, bias=False)
self.output = nn.Linear(args.dim_att, args.n_embd, bias=False)
self.gate = nn.Linear(args.n_embd, args.dim_att, bias=False)
self.ln_x = nn.GroupNorm(self.n_head, args.dim_att, eps=(1e-5)*(args.head_size_divisor**2))
@MyFunction
def jit_func(self, x):
B, T, C = x.size()
xx = self.time_shift(x) - x
xxx = x + xx * self.time_maa_x
xxx = torch.tanh(xxx @ self.time_maa_w1).view(B*T, 5, -1).transpose(0, 1)
xxx = torch.bmm(xxx, self.time_maa_w2).view(5, B, T, -1)
mw, mk, mv, mr, mg = xxx.unbind(dim=0)
xw = x + xx * (self.time_maa_w + mw)
xk = x + xx * (self.time_maa_k + mk)
xv = x + xx * (self.time_maa_v + mv)
xr = x + xx * (self.time_maa_r + mr)
xg = x + xx * (self.time_maa_g + mg)
r = self.receptance(xr)
k = self.key(xk)
v = self.value(xv)
g = F.silu(self.gate(xg))
ww = torch.tanh(xw @ self.time_decay_w1) @ self.time_decay_w2
w = self.time_decay + ww
return r, k, v, g, w
@MyFunction
def jit_func_2(self, x, g):
B, T, C = x.size()
x = x.view(B * T, C)
x = self.ln_x(x).view(B, T, C)
x = self.output(x * g)
return x
def forward(self, x):
B, T, C = x.size()
H = self.n_head
r, k, v, g, w = self.jit_func(x)
x = RUN_CUDA_RWKV6(B, T, C, H, r, k, v, w, u=self.time_faaaa)
return self.jit_func_2(x, g)
########################################################################################################
class RWKV_CMix_x060(MyModule):
def __init__(self, args, layer_id):
super().__init__()
self.args = args
self.layer_id = layer_id
self.time_shift = nn.ZeroPad2d((0, 0, 1, -1))
with torch.no_grad(): # fancy init of time_mix
ratio_1_to_almost0 = 1.0 - (layer_id / args.n_layer) # 1 to ~0
ddd = torch.ones(1, 1, args.n_embd)
for i in range(args.n_embd):
ddd[0, 0, i] = i / args.n_embd
self.time_maa_k = nn.Parameter(1.0 - torch.pow(ddd, ratio_1_to_almost0))
self.time_maa_r = nn.Parameter(1.0 - torch.pow(ddd, ratio_1_to_almost0))
self.key = nn.Linear(args.n_embd, args.dim_ffn, bias=False)
self.receptance = nn.Linear(args.n_embd, args.n_embd, bias=False)
self.value = nn.Linear(args.dim_ffn, args.n_embd, bias=False)
@MyFunction
def forward(self, x):
xx = self.time_shift(x) - x
xk = x + xx * self.time_maa_k
xr = x + xx * self.time_maa_r
k = self.key(xk)
k = torch.relu(k) ** 2
kv = self.value(k)
return torch.sigmoid(self.receptance(xr)) * kv
########################################################################################################
# The RWKV Model with our blocks
########################################################################################################
class Block(nn.Module):
def __init__(self, args, layer_id):
super().__init__()
self.args = args
self.layer_id = layer_id
self.ln1 = nn.LayerNorm(args.n_embd)
self.ln2 = nn.LayerNorm(args.n_embd)
if self.layer_id == 0:
self.ln0 = nn.LayerNorm(args.n_embd)
self.att = RWKV_Tmix_x060(args, layer_id)
self.ffn = RWKV_CMix_x060(args, layer_id)
if args.dropout > 0:
self.drop0 = nn.Dropout(p = args.dropout)
self.drop1 = nn.Dropout(p = args.dropout)
def forward(self, x):
if self.layer_id == 0:
x = self.ln0(x)
x = x + self.att(self.ln1(x))
x = x + self.ffn(self.ln2(x))
return x
class L2Wrap(torch.autograd.Function):
@staticmethod
def forward(ctx, loss, y):
ctx.save_for_backward(y)
return loss
@staticmethod
def backward(ctx, grad_output):
y = ctx.saved_tensors[0]
# to encourage the logits to be close to 0
factor = 1e-4 / (y.shape[0] * y.shape[1])
maxx, ids = torch.max(y, -1, keepdim=True)
gy = torch.zeros_like(y)
gy.scatter_(-1, ids, maxx * factor)
return (grad_output, gy)
class RWKV(pl.LightningModule):
def __init__(self, args):
super().__init__()
self.args = args
self.emb = nn.Embedding(args.vocab_size, args.n_embd)
self.blocks = nn.ModuleList([Block(args, i) for i in range(args.n_layer)])
self.ln_out = nn.LayerNorm(args.n_embd)
self.head = nn.Linear(args.n_embd, args.vocab_size, bias=False)
if args.dropout > 0:
self.drop0 = nn.Dropout(p = args.dropout)
def configure_optimizers(self):
trainable_params = [p for p in self.parameters() if p.requires_grad]
optim_groups = [{"params": trainable_params, "weight_decay": self.args.weight_decay}]
if self.deepspeed_offload:
return DeepSpeedCPUAdam(optim_groups, lr=self.args.lr_init, betas=self.args.betas, eps=self.args.adam_eps, bias_correction=True, adamw_mode=True, amsgrad=False)
return FusedAdam(optim_groups, lr=self.args.lr_init, betas=self.args.betas, eps=self.args.adam_eps, bias_correction=True, adam_w_mode=True, amsgrad=False)
@property
def deepspeed_offload(self) -> bool:
strategy = self.trainer.strategy
if isinstance(strategy, DeepSpeedStrategy):
cfg = strategy.config["zero_optimization"]
return cfg.get("offload_optimizer") or cfg.get("offload_param")
return False
def forward(self, x):
args = self.args
# B, T, D = x.size()
# assert T <= args.ctx_len, "Cannot forward, model ctx_len is exhausted."
if args.dropout > 0:
x = self.drop0(x)
for block in self.blocks:
if args.grad_cp == 1:
x = deepspeed.checkpointing.checkpoint(block, x)
else:
x = block(x)
x = self.ln_out(x)
x = self.head(x)
return x
def training_step(self, batch, batch_idx):
idx, targets = batch
logits = self(idx)
loss = F.cross_entropy(logits.view(-1, logits.size(-1)), targets.view(-1))
return L2Wrap.apply(loss, logits)
def training_step_end(self, batch_parts):
if pl.__version__[0]!='2':
all = self.all_gather(batch_parts)
if self.trainer.is_global_zero:
self.trainer.my_loss_all = all
class VisualRWKV(pl.LightningModule):
def __init__(self, args):
super().__init__()
self.args = args
self.rwkv = RWKV(args)
if len(args.load_model) > 0:
self.load_rwkv_from_pretrained(args.load_model)
self.vit = CLIPVisionModel.from_pretrained(args.vision_tower_name)
self.vit.requires_grad_(False)
self.proj = nn.Linear(self.vit.config.hidden_size, args.n_embd, bias=False)
def load_rwkv_from_pretrained(self, path):
self.rwkv.load_state_dict(torch.load(path, map_location="cpu"))
rank_zero_info(f"Loaded pretrained RWKV from {path}")
@property
def deepspeed_offload(self) -> bool:
strategy = self.trainer.strategy
if isinstance(strategy, DeepSpeedStrategy):
cfg = strategy.config["zero_optimization"]
return cfg.get("offload_optimizer") or cfg.get("offload_param")
return False
def freeze_rwkv(self, num_layers_to_freeze):
# freeze all layers including embedding and lm head
if num_layers_to_freeze == self.args.n_layer:
self.rwkv.requires_grad_(False)
# otherwise, freeze only the first num_layers_to_freeze layers
for i, block in enumerate(self.rwkv.blocks):
if i < num_layers_to_freeze:
for p in block.parameters():
p.requires_grad_(False)
else:
for p in block.parameters():
p.requires_grad_(True)
def freeze_emb(self):
self.rwkv.emb.requires_grad_(False)
def freeze_proj(self):
self.proj.requires_grad_(False)
def configure_optimizers(self):
zero_weight_decay_group = [p for p in self.parameters() if len(p.squeeze().shape) < 2 and p.requires_grad]
# add weight decay to len(p.squeeze().shape) >= 2
weight_decay_group = [p for p in self.parameters() if len(p.squeeze().shape) >= 2 and p.requires_grad]
name_of_trainable_params = [n for n, p in self.named_parameters() if p.requires_grad]
rank_zero_info(f"Name of trainable parameters in optimizers: {name_of_trainable_params}")
rank_zero_info(f"Number of trainable parameters in optimizers: {len(name_of_trainable_params)}")
optim_groups = []
if zero_weight_decay_group:
optim_groups += [{"params": zero_weight_decay_group, "weight_decay": 0.0}]
if weight_decay_group:
if self.args.weight_decay > 0:
optim_groups += [{"params": weight_decay_group, "weight_decay": self.args.weight_decay}]
rank_zero_info(f"Number of parameters with weight decay: {len(weight_decay_group)}, with value: {self.args.weight_decay}")
else:
optim_groups += [{"params": weight_decay_group, "weight_decay": 0.0}]
if self.deepspeed_offload:
return DeepSpeedCPUAdam(optim_groups, lr=self.args.lr_init, betas=self.args.betas, eps=self.args.adam_eps, bias_correction=True, adamw_mode=True, amsgrad=False)
return FusedAdam(optim_groups, lr=self.args.lr_init, betas=self.args.betas, eps=self.args.adam_eps, bias_correction=True, adam_w_mode=True, amsgrad=False)
def forward(self, samples):
x, targets, image_features = self.preparing_embedding(samples)
# bidirectional
logits = self.bidirectional_forward(x)
return logits, targets
def bidirectional_forward(self, x, x_emb=None):
args = self.args
if args.dropout > 0:
x = self.rwkv.drop0(x)
for i, block in enumerate(self.rwkv.blocks):
do_reverse = (i % 2 == 1)
if do_reverse: # reverse
x[:, self.img_start:self.img_end, :] = x[:, self.img_start:self.img_end, :].flip(1)
if args.grad_cp == 1:
x = deepspeed.checkpointing.checkpoint(block, x)
else:
x = block(x)
if do_reverse: # reverse back
x[:, self.img_start:self.img_end, :] = x[:, self.img_start:self.img_end, :].flip(1)
x = self.rwkv.ln_out(x)
x = self.rwkv.head(x)
return x
def training_step(self, batch, batch_idx):
logits, targets = self(batch)
shift_logits = logits[..., :-1, :].contiguous()
shift_labels = targets[..., 1:].contiguous()
# calculate valid length for each sample
valid_lengths = (shift_labels != IGNORE_INDEX).sum(1) # [B, T] -> [B]
# if valid length is 0, set it to 1, to avoid division by zero
valid_lengths = torch.max(valid_lengths, torch.ones_like(valid_lengths))
# calculate loss, loss of IGNORE_INDEX will be set to 0
loss = F.cross_entropy(shift_logits.view(-1, shift_logits.size(-1)),
shift_labels.view(-1),
ignore_index=IGNORE_INDEX,
reduction='none')
# Average the loss by valid label length
loss = loss.view(shift_labels.size()).sum(1) / valid_lengths # [B*T] -> [B, T] -> [B]
loss = loss.mean() # average over batch
return L2Wrap.apply(loss, logits)
def training_step_end(self, batch_parts):
if pl.__version__[0]!='2':
all = self.all_gather(batch_parts)
if self.trainer.is_global_zero:
self.trainer.my_loss_all = all
def encode_images(self, images):
B, N, C, H, W = images.shape
images = images.view(B*N, C, H, W)
image_features = self.vit(images).last_hidden_state
L, D = image_features.shape[1], image_features.shape[2]
# rerange [B*N, L, D] -> [B, N, L, D]
image_features = image_features.view(B, N, L, D)[:, 0, :, :]
image_features = self.grid_pooling(image_features)
return self.proj(image_features)
def grid_pooling(self, image_features):
cls_features = image_features[:, 0:1, :]
image_features = image_features[:, 1:, :] #drop cls token
if self.args.grid_size == -1: # no grid pooling
return torch.cat((image_features, cls_features), dim=1)
if self.args.grid_size == 0: # take cls token
return cls_features
if self.args.grid_size == 1: # global avg pooling
return torch.cat((image_features.mean(dim=1, keepdim=True), cls_features), dim=1)
B, L, D = image_features.shape
H_or_W = int(L**0.5)
image_features = image_features.view(B, H_or_W, H_or_W, D)
grid_stride = H_or_W // self.args.grid_size
image_features = F.avg_pool2d(image_features.permute(0, 3, 1, 2),
padding=0,
kernel_size=grid_stride,
stride=grid_stride)
image_features = image_features.permute(0, 2, 3, 1).view(B, -1, D)
return torch.cat((image_features, cls_features), dim=1)
def get_max_image_token_indice(self, samples):
max_image_token_indice = 0
for cur_input_ids in samples["input_ids"]:
num_images = (cur_input_ids == IMAGE_TOKEN_INDEX).sum()
if num_images == 1:
image_token_indice = torch.where(cur_input_ids == IMAGE_TOKEN_INDEX)[0][0]
max_image_token_indice = max(max_image_token_indice, image_token_indice)
return max_image_token_indice
def truncate_input(self, new_input_embeds, new_labels):
# prioritize retaining the labels at the beginning
# if there are no valid labels at the beginning, retain the labels from the end
truncated_input_embeds = []
truncated_labels = []
for x, y in zip(new_input_embeds, new_labels):
valid_labels = [i for i in y[:self.args.ctx_len] if i != IGNORE_INDEX]
if valid_labels:
truncated_input_embeds.append(x[:self.args.ctx_len])
truncated_labels.append(y[:self.args.ctx_len])
else:
truncated_input_embeds.append(x[-self.args.ctx_len:])
truncated_labels.append(y[-self.args.ctx_len:])
return truncated_input_embeds, truncated_labels
def preparing_embedding(self, samples, truncate=True):
device, label_dtype = samples["labels"].device, samples["labels"].dtype
emb_dtype = samples["images"].dtype
### prepare image features
image_features = self.encode_images(samples["images"]) # with cls token
### prepare input token
new_input_embeds = []
new_labels = []
max_image_token_indice = self.get_max_image_token_indice(samples)
self.img_start = max_image_token_indice
self.img_end = max_image_token_indice + (image_features.shape[1] - 1) # exclude cls token
for idx, cur_input_ids in enumerate(samples["input_ids"]):
cur_labels = samples["labels"][idx]
cur_new_input_ids = torch.zeros(max_image_token_indice, dtype=cur_input_ids.dtype, device=device)
cur_new_labels = torch.full((max_image_token_indice,), IGNORE_INDEX, device=device, dtype=label_dtype)
num_images = (cur_input_ids == IMAGE_TOKEN_INDEX).sum()
if num_images == 0: # no image in this sample
# mask image feature, set to 0
image_features[idx] = torch.zeros_like(image_features[idx])
elif num_images == 1: # only one image in this sample
image_token_indice = torch.where(cur_input_ids == IMAGE_TOKEN_INDEX)[0][0]
# first text part, left paded
cur_new_input_ids[-image_token_indice:] = cur_input_ids[:image_token_indice]
cur_new_labels[-image_token_indice:] = cur_labels[:image_token_indice]
else:
raise ValueError(f"Too many images in one sample: {num_images}, should be 0 or 1.")
# convert to list
cur_new_input_embeds = [self.rwkv.emb(cur_new_input_ids)]
cur_new_labels = [cur_new_labels]
# image part
cur_image_features = image_features[idx]
cur_new_input_embeds.append(cur_image_features)
cur_new_labels.append(torch.full((cur_image_features.shape[0],), IGNORE_INDEX, device=device, dtype=label_dtype))
# last text part
if num_images == 1:
cur_new_input_embeds.append(self.rwkv.emb(cur_input_ids[image_token_indice+1:]))
cur_new_labels.append(cur_labels[image_token_indice+1:])
else: # no image
cur_new_input_embeds.append(self.rwkv.emb(cur_input_ids))
cur_new_labels.append(cur_labels)
# concat them
cur_new_input_embeds = torch.cat(cur_new_input_embeds)
cur_new_labels = torch.cat(cur_new_labels)
new_input_embeds.append(cur_new_input_embeds)
new_labels.append(cur_new_labels)
# Truncate sequences to max length as image embeddings can make the sequence longer
# keep the first `ctx_len` tokens, to make sure instruction complete
if truncate:
new_input_embeds, new_labels = self.truncate_input(new_input_embeds, new_labels)
# Combine them
max_len = max(x.shape[0] for x in new_input_embeds)
batch_size = len(new_input_embeds)
new_input_embeds_padded = torch.zeros((batch_size, max_len, self.args.n_embd), dtype=emb_dtype, device=device)
new_labels_padded = torch.full((batch_size, max_len), IGNORE_INDEX, dtype=label_dtype, device=device)
for i, (cur_new_embed, cur_new_labels) in enumerate(zip(new_input_embeds, new_labels)):
cur_len = cur_new_embed.shape[0]
new_input_embeds_padded[i, :cur_len] = cur_new_embed
new_labels_padded[i, :cur_len] = cur_new_labels
return new_input_embeds_padded, new_labels_padded, image_features
def generate(self, input_ids, images, do_sample, temperature, top_p, max_new_tokens, stop_token_idx) -> list[int]:
''' one mode to generate, only generate one sample at a time
# input_ids: [1, seq_len]
# images: [1, 1, 3, 224, 224]
# do_sample: bool
# temperature: float
# top_p: float
# max_new_tokens: int
'''
# prepare samples
sampels = {"input_ids": input_ids, "images": images, "labels": torch.full_like(input_ids, IGNORE_INDEX)}
# prepare embedding, x: [1, seq_len, n_embd]
x, _, image_features = self.preparing_embedding(sampels, truncate=False)
# generate
generated_tokens = []
generated_token_logits = []
generated_token_probs = []
for i in range(max_new_tokens):
logits = self.bidirectional_forward(x)[:, -1, :]
if do_sample:
raise NotImplementedError
else: # greedy
# [1, vocab_size] -> [1, 1]
next_token = torch.argmax(logits, dim=-1, keepdim=True)
next_token_logit = logits.gather(-1, next_token)
probs = torch.softmax(logits, dim=-1)
next_token_prob = probs.gather(-1, next_token)
generated_tokens.append(next_token.item())
generated_token_logits.append(next_token_logit.item())
generated_token_probs.append(next_token_prob.item())
if generated_tokens[-1] == stop_token_idx:
break
x = torch.cat((x, self.rwkv.emb(next_token)), dim=-2)
x = x[:, -self.args.ctx_len:, :] # truncate
return generated_tokens, generated_token_logits, generated_token_probs