-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdenoise_model.py
787 lines (672 loc) · 30.4 KB
/
denoise_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
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
# Copyright (c) Shanghai AI Lab. All rights reserved.
from torch.utils.cpp_extension import load
from timm.models.layers import DropPath, to_2tuple, trunc_normal_
from typing import Sequence
import math
import os
from functools import partial
import logging
import torch
import torch.nn as nn
import torch.nn.functional as F
import torch.utils.checkpoint as cp
from einops import rearrange
# from mmcv.runner.base_module import BaseModule, ModuleList
# from mmcv.cnn.bricks.transformer import PatchEmbed, AdaptivePadding
import numpy as np
import einops
def resize_pos_embed(pos_embed,
src_shape,
dst_shape,
mode='bicubic',
num_extra_tokens=1):
"""Resize pos_embed weights.
Args:
pos_embed (torch.Tensor): Position embedding weights with shape
[1, L, C].
src_shape (tuple): The resolution of downsampled origin training
image, in format (H, W).
dst_shape (tuple): The resolution of downsampled new training
image, in format (H, W).
mode (str): Algorithm used for upsampling. Choose one from 'nearest',
'linear', 'bilinear', 'bicubic' and 'trilinear'.
Defaults to 'bicubic'.
num_extra_tokens (int): The number of extra tokens, such as cls_token.
Defaults to 1.
Returns:
torch.Tensor: The resized pos_embed of shape [1, L_new, C]
"""
if src_shape[0] == dst_shape[0] and src_shape[1] == dst_shape[1]:
return pos_embed
assert pos_embed.ndim == 3, 'shape of pos_embed must be [1, L, C]'
_, L, C = pos_embed.shape
src_h, src_w = src_shape
assert L == src_h * src_w + num_extra_tokens, \
f"The length of `pos_embed` ({L}) doesn't match the expected " \
f'shape ({src_h}*{src_w}+{num_extra_tokens}). Please check the' \
'`img_size` argument.'
extra_tokens = pos_embed[:, :num_extra_tokens]
src_weight = pos_embed[:, num_extra_tokens:]
src_weight = src_weight.reshape(1, src_h, src_w, C).permute(0, 3, 1, 2)
# The cubic interpolate algorithm only accepts float32
dst_weight = F.interpolate(
src_weight.float(), size=dst_shape, align_corners=False, mode=mode)
dst_weight = torch.flatten(dst_weight, 2).transpose(1, 2).contiguous()
dst_weight = dst_weight.to(src_weight.dtype)
return torch.cat((extra_tokens, dst_weight), dim=1)
class PatchEmbed(nn.Module):
""" Image to Patch Embedding
"""
def __init__(self, patch_size, in_chans=3, embed_dim=768):
super().__init__()
self.patch_size = patch_size
self.proj = nn.Conv2d(in_chans, embed_dim,
kernel_size=patch_size, stride=patch_size)
def forward(self, x):
B, C, H, W = x.shape
h, w = x.size(2), x.size(3)
pad_h = 0 if h % 8 == 0 else 8 - (h % 8)
pad_w = 0 if w % 8 == 0 else 8 - (w % 8)
x = F.pad(x, (0, pad_h, 0, pad_w))
b, c, h, w = x.shape
# assert H % self.patch_size == 0 and W % self.patch_size == 0
x = self.proj(x).flatten(2).transpose(1, 2)
return x, h, w
def patchify(imgs, patch_size):
x = einops.rearrange(
imgs, 'B C (h p1) (w p2) -> B (h w) (p1 p2 C)', p1=patch_size, p2=patch_size)
return x
def unpatchify(x, channels=3):
patch_size = int((x.shape[2] // channels) ** 0.5)
h = w = int(x.shape[1] ** .5)
assert h * w == x.shape[1] and patch_size ** 2 * channels == x.shape[2]
x = einops.rearrange(
x, 'B (h w) (p1 p2 C) -> B C (h p1) (w p2)', h=h, p1=patch_size, p2=patch_size)
return x
def get_2d_sincos_pos_embed_from_grid(embed_dim, grid):
assert embed_dim % 2 == 0
# use half of dimensions to encode grid_h
emb_h = get_1d_sincos_pos_embed_from_grid(
embed_dim // 2, grid[0]) # (H*W, D/2)
emb_w = get_1d_sincos_pos_embed_from_grid(
embed_dim // 2, grid[1]) # (H*W, D/2)
emb = np.concatenate([emb_h, emb_w], axis=1) # (H*W, D)
return emb
def get_1d_sincos_pos_embed_from_grid(embed_dim, pos):
"""
embed_dim: output dimension for each position
pos: a list of positions to be encoded: size (M,)
out: (M, D)
"""
assert embed_dim % 2 == 0
omega = np.arange(embed_dim // 2, dtype=np.float64)
omega /= embed_dim / 2.
omega = 1. / 10000**omega # (D/2,)
pos = pos.reshape(-1) # (M,)
out = np.einsum('m,d->md', pos, omega) # (M, D/2), outer product
emb_sin = np.sin(out) # (M, D/2)
emb_cos = np.cos(out) # (M, D/2)
emb = np.concatenate([emb_sin, emb_cos], axis=1) # (M, D)
return emb
def get_2d_sincos_pos_embed(embed_dim, grid_size, cls_token=False, extra_tokens=0):
"""
grid_size: int of the grid height and width
return:
pos_embed: [grid_size*grid_size, embed_dim] or [1+grid_size*grid_size, embed_dim] (w/ or w/o cls_token)
"""
grid_h = np.arange(grid_size, dtype=np.float32)
grid_w = np.arange(grid_size, dtype=np.float32)
grid = np.meshgrid(grid_w, grid_h) # here w goes first
grid = np.stack(grid, axis=0)
grid = grid.reshape([2, 1, grid_size, grid_size])
pos_embed = get_2d_sincos_pos_embed_from_grid(embed_dim, grid)
if cls_token and extra_tokens > 0:
pos_embed = np.concatenate(
[np.zeros([extra_tokens, embed_dim]), pos_embed], axis=0)
return pos_embed
def resize_pos_embed(pos_embed,
src_shape,
dst_shape,
mode='bicubic',
num_extra_tokens=1):
"""Resize pos_embed weights.
Args:
pos_embed (torch.Tensor): Position embedding weights with shape
[1, L, C].
src_shape (tuple): The resolution of downsampled origin training
image, in format (H, W).
dst_shape (tuple): The resolution of downsampled new training
image, in format (H, W).
mode (str): Algorithm used for upsampling. Choose one from 'nearest',
'linear', 'bilinear', 'bicubic' and 'trilinear'.
Defaults to 'bicubic'.
num_extra_tokens (int): The number of extra tokens, such as cls_token.
Defaults to 1.
Returns:
torch.Tensor: The resized pos_embed of shape [1, L_new, C]
"""
if src_shape[0] == dst_shape[0] and src_shape[1] == dst_shape[1]:
return pos_embed
assert pos_embed.ndim == 3, 'shape of pos_embed must be [1, L, C]'
_, L, C = pos_embed.shape
src_h, src_w = src_shape
assert L == src_h * src_w + num_extra_tokens, \
f"The length of `pos_embed` ({L}) doesn't match the expected " \
f'shape ({src_h}*{src_w}+{num_extra_tokens}). Please check the' \
'`img_size` argument.'
extra_tokens = pos_embed[:, :num_extra_tokens]
src_weight = pos_embed[:, num_extra_tokens:]
src_weight = src_weight.reshape(1, src_h, src_w, C).permute(0, 3, 1, 2)
# The cubic interpolate algorithm only accepts float32
dst_weight = F.interpolate(
src_weight.float(), size=dst_shape, align_corners=False, mode=mode)
dst_weight = torch.flatten(dst_weight, 2).transpose(1, 2).contiguous()
dst_weight = dst_weight.to(src_weight.dtype)
return torch.cat((extra_tokens, dst_weight), dim=1)
class PatchUnEmbed(nn.Module):
def __init__(self, patch_size=4, embed_dim=96, norm_layer=None):
super().__init__()
self.patch_size = (patch_size, patch_size)
self.embed_dim = embed_dim
if norm_layer is not None:
self.norm = norm_layer(embed_dim)
else:
self.norm = None
def forward(self, x: torch.Tensor, x_size: tuple = None):
B, HWPP, CPP = x.shape
HP, WP = x_size
H, W = HP * self.patch_size[0], WP * self.patch_size[1]
C = CPP // (self.patch_size[0] * self.patch_size[1])
P = self.patch_size[0]
x = x.view(B, HP, WP, CPP).contiguous()
x = x.view(B, HP, WP, P, P, C).permute(0, 1, 3, 2, 4, 5).contiguous()
x = x.view(B, H, W, C).permute(0, 3, 1, 2).contiguous()
if self.norm is not None:
x = self.norm(x)
return x
logger = logging.getLogger(__name__)
T_MAX = 65536
HEAD_SIZE = 64
import os
from torch.utils.cpp_extension import load
wkv6_cuda = load(name="wkv6", sources=[f"{os.path.dirname(__file__)}/csrc/cuda/wkv_op.cpp", f"{os.path.dirname(__file__)}/csrc/cuda/wkv_cuda.cu"],
verbose=True, extra_cuda_cflags=["-res-usage", "--use_fast_math", f"-D_N_={HEAD_SIZE}", "-O3", "-Xptxas -O3", "--extra-device-vectorization",f"-D_T_={T_MAX}"])
class WKV_6(torch.autograd.Function):
@staticmethod
def forward(ctx, B, T, C, H, r, k, v, w, u):
with torch.no_grad():
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.float32, 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():
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.float32, memory_format=torch.contiguous_format)#.uniform_(-100, 100)
gk = torch.empty((B, T, C), device=gy.device, requires_grad=False, dtype=torch.float32, memory_format=torch.contiguous_format)#.uniform_(-100, 100)
gv = torch.empty((B, T, C), device=gy.device, requires_grad=False, dtype=torch.float32, memory_format=torch.contiguous_format)#.uniform_(-100, 100)
gw = torch.empty((B, T, C), device=gy.device, requires_grad=False, dtype=torch.float32, memory_format=torch.contiguous_format)#.uniform_(-100, 100)
gu = torch.empty((B, C), device=gy.device, requires_grad=False, dtype=torch.float32, 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)
def q_shift_multihead(input, shift_pixel=1, head_dim=HEAD_SIZE,
patch_resolution=None, with_cls_token=False):
B, N, C = input.shape
assert C % head_dim == 0
assert head_dim % 4 == 0
if with_cls_token:
cls_tokens = input[:, [-1], :]
input = input[:, :-1, :]
input = input.transpose(1, 2).reshape(
B, -1, head_dim, patch_resolution[0], patch_resolution[1]) # [B, n_head, head_dim H, W]
B, _, _, H, W = input.shape
output = torch.zeros_like(input)
output[:, :, 0:int(head_dim*1/4), :, shift_pixel:W] = \
input[:, :, 0:int(head_dim*1/4), :, 0:W-shift_pixel]
output[:, :, int(head_dim/4):int(head_dim/2), :, 0:W-shift_pixel] = \
input[:, :, int(head_dim/4):int(head_dim/2), :, shift_pixel:W]
output[:, :, int(head_dim/2):int(head_dim/4*3), shift_pixel:H, :] = \
input[:, :, int(head_dim/2):int(head_dim/4*3), 0:H-shift_pixel, :]
output[:, :, int(head_dim*3/4):int(head_dim), 0:H-shift_pixel, :] = \
input[:, :, int(head_dim*3/4):int(head_dim), shift_pixel:H, :]
if with_cls_token:
output = output.reshape(B, C, N-1).transpose(1, 2)
output = torch.cat((output, cls_tokens), dim=1)
else:
output = output.reshape(B, C, N).transpose(1, 2)
return output
class VRWKV_SpatialMix_V6(nn.Module):
def __init__(self, n_embd, n_head, n_layer, layer_id, shift_mode='q_shift_multihead',
shift_pixel=1, init_mode='fancy', key_norm=False, with_cls_token=False,
with_cp=False):
super().__init__()
self.layer_id = layer_id
self.n_layer = n_layer
self.n_embd = n_embd
self.attn_sz = n_embd
self.n_head = n_head
self.head_size = self.attn_sz // self.n_head
assert self.head_size == HEAD_SIZE
self.device = None
self._init_weights(init_mode)
self.with_cls_token = with_cls_token
self.shift_pixel = shift_pixel
self.shift_mode = shift_mode
self.shift_func = eval(shift_mode)
self.key = nn.Linear(self.n_embd, self.attn_sz, bias=False)
self.value = nn.Linear(self.n_embd, self.attn_sz, bias=False)
self.receptance = nn.Linear(self.n_embd, self.attn_sz, bias=False)
self.gate = nn.Linear(self.n_embd, self.attn_sz, bias=False)
if key_norm:
self.key_norm = nn.LayerNorm(n_embd)
else:
self.key_norm = None
self.output = nn.Linear(self.attn_sz, n_embd, bias=False)
self.ln_x = nn.GroupNorm(self.n_head, self.attn_sz, eps=1e-5)
self.with_cp = with_cp
def _init_weights(self, init_mode):
if init_mode=='fancy':
with torch.no_grad():
ratio_0_to_1 = self.layer_id / (self.n_layer - 1) # 0 to 1
ratio_1_to_almost0 = 1.0 - (self.layer_id / self.n_layer) # 1 to ~0
ddd = torch.ones(1, 1, self.n_embd)
for i in range(self.n_embd):
ddd[0, 0, i] = i / self.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))
TIME_MIX_EXTRA_DIM = 32 # generate TIME_MIX for w,k,v,r,g
self.time_maa_w1 = nn.Parameter(torch.zeros(self.n_embd, TIME_MIX_EXTRA_DIM*5).uniform_(-1e-4, 1e-4))
self.time_maa_w2 = nn.Parameter(torch.zeros(5, TIME_MIX_EXTRA_DIM, self.n_embd).uniform_(-1e-4, 1e-4))
# fancy time_decay
decay_speed = torch.ones(self.attn_sz)
for n in range(self.attn_sz):
decay_speed[n] = -6 + 5 * (n / (self.attn_sz - 1)) ** (0.7 + 1.3 * ratio_0_to_1)
self.time_decay = nn.Parameter(decay_speed.reshape(1,1,self.attn_sz))
TIME_DECAY_EXTRA_DIM = 64
self.time_decay_w1 = nn.Parameter(torch.zeros(self.n_embd, TIME_DECAY_EXTRA_DIM).uniform_(-1e-4, 1e-4))
self.time_decay_w2 = nn.Parameter(torch.zeros(TIME_DECAY_EXTRA_DIM, self.attn_sz).uniform_(-1e-4, 1e-4))
tmp = torch.zeros(self.attn_sz)
for n in range(self.attn_sz):
zigzag = ((n + 1) % 3 - 1) * 0.1
tmp[n] = ratio_0_to_1 * (1 - (n / (self.attn_sz - 1))) + zigzag
self.time_faaaa = nn.Parameter(tmp.reshape(self.n_head, self.head_size))
else:
raise NotImplementedError
def jit_func(self, x, patch_resolution):
# Mix x with the previous timestep to produce xk, xv, xr
B, T, C = x.size()
xx = self.shift_func(x, self.shift_pixel, patch_resolution=patch_resolution,
with_cls_token=self.with_cls_token) - x
xxx = x + xx * self.time_maa_x # [B, T, C]
xxx = torch.tanh(xxx @ self.time_maa_w1).view(B*T, 5, -1).transpose(0, 1)
# [5, B*T, TIME_MIX_EXTRA_DIM]
xxx = torch.bmm(xxx, self.time_maa_w2).view(5, B, T, -1)
# [5, B, T, C]
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
# [B, T, C]
w = self.time_decay + ww
return r, k, v, g, w
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, patch_resolution=None):
def _inner_forward(x):
B, T, C = x.size()
self.device = x.device
r, k, v, g, w = self.jit_func(x, patch_resolution)
x = RUN_CUDA_RWKV6(B, T, C, self.n_head, r, k, v, w, u=self.time_faaaa)
if self.key_norm is not None:
x = self.key_norm(x)
return self.jit_func_2(x, g)
if self.with_cp and x.requires_grad:
x = cp.checkpoint(_inner_forward, x)
else:
x = _inner_forward(x)
return x
class VRWKV_ChannelMix(nn.Module):
def __init__(self, n_embd, n_head, n_layer, layer_id, shift_mode='q_shift_multihead',
shift_pixel=1, hidden_rate=4, init_mode='fancy', key_norm=False,
with_cls_token=False, with_cp=False):
super().__init__()
self.layer_id = layer_id
self.n_layer = n_layer
self.n_embd = n_embd
self.attn_sz = n_embd
self.n_head = n_head
self.head_size = self.attn_sz // self.n_head
assert self.head_size == HEAD_SIZE
self.with_cp = with_cp
self._init_weights(init_mode)
self.with_cls_token = with_cls_token
self.shift_pixel = shift_pixel
self.shift_mode = shift_mode
self.shift_func = eval(shift_mode)
hidden_sz = hidden_rate * n_embd
self.key = nn.Linear(n_embd, hidden_sz, bias=False)
if key_norm:
self.key_norm = nn.LayerNorm(hidden_sz)
else:
self.key_norm = None
self.receptance = nn.Linear(n_embd, n_embd, bias=False)
self.value = nn.Linear(hidden_sz, n_embd, bias=False)
def _init_weights(self, init_mode):
if init_mode == 'fancy':
with torch.no_grad(): # fancy init of time_mix
ratio_1_to_almost0 = (1.0 - (self.layer_id / self.n_layer)) # 1 to ~0
x = torch.ones(1, 1, self.n_embd)
for i in range(self.n_embd):
x[0, 0, i] = i / self.n_embd
self.spatial_mix_k = nn.Parameter(torch.pow(x, ratio_1_to_almost0))
self.spatial_mix_r = nn.Parameter(torch.pow(x, ratio_1_to_almost0))
else:
raise NotImplementedError
def forward(self, x, patch_resolution=None):
def _inner_forward(x):
xx = self.shift_func(x, self.shift_pixel, patch_resolution=patch_resolution,
with_cls_token=self.with_cls_token)
xk = x * self.spatial_mix_k + xx * (1 - self.spatial_mix_k)
xr = x * self.spatial_mix_r + xx * (1 - self.spatial_mix_r)
k = self.key(xk)
k = torch.square(torch.relu(k))
if self.key_norm is not None:
k = self.key_norm(k)
kv = self.value(k)
x = torch.sigmoid(self.receptance(xr)) * kv
return x
if self.with_cp and x.requires_grad:
x = cp.checkpoint(_inner_forward, x)
else:
x = _inner_forward(x)
return x
class Block(nn.Module):
def __init__(self, n_embd, n_head, n_layer, layer_id, shift_mode='q_shift_multihead',
shift_pixel=1, drop_path=0., hidden_rate=4, init_mode='fancy',
init_values=None, post_norm=False, key_norm=False, with_cls_token=False,
with_cp=False,skip=False):
super().__init__()
self.layer_id = layer_id
self.ln1 = nn.LayerNorm(n_embd)
self.ln2 = nn.LayerNorm(n_embd)
self.drop_path = DropPath(drop_path) if drop_path > 0. else nn.Identity()
if self.layer_id == 0:
self.ln0 = nn.LayerNorm(n_embd)
self.att = VRWKV_SpatialMix_V6(n_embd, n_head, n_layer, layer_id, shift_mode,
shift_pixel, init_mode, key_norm=key_norm,
with_cls_token=with_cls_token)
self.ffn = VRWKV_ChannelMix(n_embd, n_head, n_layer, layer_id, shift_mode,
shift_pixel, hidden_rate, init_mode, key_norm=key_norm,
with_cls_token=with_cls_token)
self.layer_scale = (init_values is not None)
self.post_norm = post_norm
if self.layer_scale:
self.gamma1 = nn.Parameter(init_values * torch.ones((n_embd)), requires_grad=True)
self.gamma2 = nn.Parameter(init_values * torch.ones((n_embd)), requires_grad=True)
self.with_cp = with_cp
self.skip=False
self.skip_linear = nn.Linear(2 * n_embd, n_embd) if skip else None
def forward(self, x, patch_resolution=None,skip=None):
if self.skip_linear is not None:
x = self.skip_linear(torch.cat([x, skip], dim=-1))
def _inner_forward(x):
if self.layer_id == 0:
x = self.ln0(x)
if self.post_norm:
if self.layer_scale:
x = x + self.drop_path(self.gamma1 * self.ln1(self.att(x, patch_resolution)))
x = x + self.drop_path(self.gamma2 * self.ln2(self.ffn(x, patch_resolution)))
else:
x = x + self.drop_path(self.ln1(self.att(x, patch_resolution)))
x = x + self.drop_path(self.ln2(self.ffn(x, patch_resolution)))
else:
if self.layer_scale:
x = x + self.drop_path(self.gamma1 * self.att(self.ln1(x), patch_resolution))
x = x + self.drop_path(self.gamma2 * self.ffn(self.ln2(x), patch_resolution))
else:
x = x + self.drop_path(self.att(self.ln1(x), patch_resolution))
x = x + self.drop_path(self.ffn(self.ln2(x), patch_resolution))
return x
if self.with_cp and x.requires_grad:
x = cp.checkpoint(_inner_forward, x)
else:
x = _inner_forward(x)
return x
def modulate(x, shift, scale):
return x * (1 + scale.unsqueeze(1)) + shift.unsqueeze(1)
class FinalLayer(nn.Module):
"""
The final layer of DiT.
"""
def __init__(self, hidden_size, patch_size, out_channels, condition=True):
super().__init__()
self.norm_final = nn.LayerNorm(
hidden_size, elementwise_affine=False, eps=1e-6)
self.linear = nn.Linear(
hidden_size, patch_size * patch_size * out_channels, bias=True)
if condition == True:
self.adaLN_modulation = nn.Sequential(
nn.SiLU(),
nn.Linear(hidden_size, 2 * hidden_size, bias=True)
)
def forward(self, x, c=None):
if c is not None:
c = self.adaLN_modulation(c).squeeze(1)
shift, scale = c.chunk(2, dim=1)
x = modulate(self.norm_final(x), shift, scale)
x = self.linear(x)
else:
x = self.norm_final(x)
x = self.linear(x)
return x
class DiffRWKVModel(nn.Module):
def __init__(
self,
img_size=64,
patch_size=2,
embed_dim=256,
channels=3,
depth=25,
num_classes=-1,
drop_path_rate=0.1,
norm_epsilon: float = 1e-5,
shift_pixel=1,
shift_mode='q_shift_multihead',
init_mode='fancy',
post_norm=False,
key_norm=False,
hidden_rate=4,
with_cp=False,
device=None,
dtype=None,
learn_sigma=False,
**kwargs
):
super().__init__()
self.num_classes = num_classes
self.embed_dim = embed_dim
self.channels = channels
factory_kwargs = {"device": device, "dtype": dtype}
# add factory_kwargs into kwargs
kwargs.update(factory_kwargs)
self.patch_size = patch_size
self.patch_resolution = (img_size//patch_size, img_size//patch_size)
self.patch_embed = PatchEmbed(
patch_size=patch_size, in_chans=channels, embed_dim=embed_dim)
num_patches = (img_size // patch_size) ** 2
self.num_patches = num_patches
self.pos_embed = nn.Parameter(torch.zeros(1, num_patches, embed_dim))
# print(self.pos_embed.size())
# TODO: release this comment
# stochastic depth decay rule
dpr = [x.item() for x in torch.linspace(0, drop_path_rate, depth + 1)]
# import ipdb;ipdb.set_trace()
inter_dpr = [0.0] + dpr
self.drop_path = DropPath(
drop_path_rate) if drop_path_rate > 0. else nn.Identity()
self.in_blocks = nn.ModuleList([
Block(
n_embd=embed_dim,
n_head=embed_dim//HEAD_SIZE,
n_layer=depth,
layer_id=i,
shift_pixel=shift_pixel,
shift_mode=shift_mode,
hidden_rate=hidden_rate,
drop_path=dpr[i],
init_mode=init_mode,
post_norm=post_norm,
with_cp=with_cp
)
for i in range(depth // 2)])
self.mid_block = Block(
n_embd=embed_dim,
n_head=embed_dim//HEAD_SIZE,
n_layer=depth,
layer_id=depth // 2,
shift_pixel=shift_pixel,
shift_mode=shift_mode,
hidden_rate=hidden_rate,
drop_path=dpr[depth//2],
init_mode=init_mode,
post_norm=post_norm,
key_norm=key_norm,
with_cp=with_cp
)
self.out_blocks = nn.ModuleList([
Block(
n_embd=embed_dim,
n_head=embed_dim//HEAD_SIZE,
n_layer=depth,
layer_id=i + depth // 2 + 1,
shift_pixel=shift_pixel,
shift_mode=shift_mode,
hidden_rate=hidden_rate,
drop_path=dpr[i + depth // 2 + 1],
init_mode=init_mode,
post_norm=post_norm,
key_norm=key_norm,
skip=True,
with_cp=with_cp
)
for i in range(depth // 2)])
# output head
self.norm_f = nn.LayerNorm(
embed_dim, eps=norm_epsilon, **factory_kwargs
)
if learn_sigma == True:
self.patch_dim = patch_size ** 2 * channels * 2
else:
self.patch_dim = patch_size ** 2 * channels
self.out_channels = channels * 2 if learn_sigma else channels
# self.decoder_pred = nn.Linear(embed_dim, self.patch_dim, bias=True)
# print('out_channedls', self.out_channels)
self.final_layer = FinalLayer(
embed_dim, patch_size, self.out_channels, condition=True)
self.initialize_weights()
def initialize_weights(self):
def _basic_init(module):
if isinstance(module, nn.Linear):
torch.nn.init.xavier_uniform_(module.weight)
if module.bias is not None:
nn.init.constant_(module.bias, 0)
self.apply(_basic_init)
# Initialize (and freeze) pos_embed by sin-cos embedding:
pos_embed = get_2d_sincos_pos_embed(
self.pos_embed.shape[-1], int(self.num_patches ** 0.5))
self.pos_embed.data.copy_(
torch.from_numpy(pos_embed).float().unsqueeze(0))
# Initialize patch_embed like nn.Linear (instead of nn.Conv2d):
w = self.patch_embed.proj.weight.data
nn.init.xavier_uniform_(w.view([w.shape[0], -1]))
nn.init.constant_(self.patch_embed.proj.bias, 0)
# Initialize label embedding table:
if self.num_classes > 0:
nn.init.normal_(self.label_emb.embedding_table.weight, std=0.02)
# Initialize timestep embedding MLP:
# nn.init.normal_(self.time_embed.mlp[0].weight, std=0.02)
# nn.init.normal_(self.time_embed.mlp[2].weight, std=0.02)
# Zero-out adaLN modulation layers:
# for block in self.in_blocks:
# nn.init.constant_(block.adaLN_modulation[-1].weight, 0)
# nn.init.constant_(block.adaLN_modulation[-1].bias, 0)
# nn.init.constant_(self.mid_block.adaLN_modulation[-1].weight, 0)
# nn.init.constant_(self.mid_block.adaLN_modulation[-1].bias, 0)
# for block in self.out_blocks:
# nn.init.constant_(block.adaLN_modulation[-1].weight, 0)
# nn.init.constant_(block.adaLN_modulation[-1].bias, 0)
# # Zero-out output layers:
# nn.init.constant_(self.final_layer.adaLN_modulation[-1].weight, 0)
# nn.init.constant_(self.final_layer.adaLN_modulation[-1].bias, 0)
nn.init.constant_(self.final_layer.linear.weight, 0)
nn.init.constant_(self.final_layer.linear.bias, 0)
@torch.jit.ignore
def no_weight_decay(self):
return {'pos_embed'}
def forward(self, x):
b, c, h, w = x.shape
x, _h, _w = self.patch_embed(x)
B, L, D = x.shape
y = resize_pos_embed(
self.pos_embed,
self.patch_resolution,
(_h//self.patch_size, _w//self.patch_size),
num_extra_tokens=0)
x = x + y
patch_resolution = (_h//self.patch_size, _w//self.patch_size)
hidden_states = x
skips = []
for blk in self.in_blocks:
hidden_states = blk(hidden_states,
patch_resolution=patch_resolution)
skips.append(hidden_states)
hidden_states = self.mid_block(
hidden_states, patch_resolution=patch_resolution)
for blk in self.out_blocks:
hidden_states = blk(
hidden_states, patch_resolution=patch_resolution, skip=skips.pop())
x = hidden_states
x = self.norm_f(hidden_states)
# x = self.decoder_pred(x)
x = self.final_layer(x)
x = einops.rearrange(
x, 'B (h w) (p1 p2 C) -> B C (h p1) (w p2)', h=_h//self.patch_size, p1=self.patch_size, p2=self.patch_size)
# x = unpatchify(x, self.out_channels)
# x = self.final_layer(x)
return x[..., :h, :w]