BiliSakura commited on
Commit
beef1df
·
verified ·
1 Parent(s): 946b9e1

Update all files for BitDance-ImageNet-diffusers

Browse files
Files changed (1) hide show
  1. BitDance_B_4x/transformer/utils.py +113 -0
BitDance_B_4x/transformer/utils.py ADDED
@@ -0,0 +1,113 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import logging
2
+
3
+ import torch
4
+ import torch.distributed as dist
5
+ from torch.nn import functional as F
6
+
7
+
8
+ def create_logger(logging_dir):
9
+ """
10
+ Create a logger that writes to a log file and stdout.
11
+ """
12
+ if dist.get_rank() == 0: # real logger
13
+ logging.basicConfig(
14
+ level=logging.INFO,
15
+ format="[\033[34m%(asctime)s\033[0m] %(message)s",
16
+ datefmt="%Y-%m-%d %H:%M:%S",
17
+ handlers=[
18
+ logging.StreamHandler(),
19
+ logging.FileHandler(f"{logging_dir}/log.txt"),
20
+ ],
21
+ )
22
+ logger = logging.getLogger(__name__)
23
+ else: # dummy logger (does nothing)
24
+ logger = logging.getLogger(__name__)
25
+ logger.addHandler(logging.NullHandler())
26
+ return logger
27
+
28
+
29
+ @torch.no_grad()
30
+ def update_ema(ema_model, model, decay=0.9999):
31
+ """
32
+ Step the EMA model towards the current model.
33
+ """
34
+ ema_ps = []
35
+ ps = []
36
+
37
+ for e, m in zip(ema_model.parameters(), model.parameters()):
38
+ if m.requires_grad:
39
+ ema_ps.append(e)
40
+ ps.append(m)
41
+ torch._foreach_lerp_(ema_ps, ps, 1.0 - decay)
42
+
43
+
44
+ @torch.no_grad()
45
+ def sync_frozen_params_once(ema_model, model):
46
+ for e, m in zip(ema_model.parameters(), model.parameters()):
47
+ if not m.requires_grad:
48
+ e.copy_(m)
49
+
50
+
51
+ def requires_grad(model, flag=True):
52
+ """
53
+ Set requires_grad flag for all parameters in a model.
54
+ """
55
+ for p in model.parameters():
56
+ p.requires_grad = flag
57
+
58
+ def patchify_raster(x, p):
59
+ B, C, H, W = x.shape
60
+
61
+ assert H % p == 0 and W % p == 0, f"Image dimensions ({H},{W}) must be divisible by patch size {p}"
62
+
63
+ h_patches = H // p
64
+ w_patches = W // p
65
+
66
+
67
+ x = x.view(B, C, h_patches, p, w_patches, p)
68
+
69
+
70
+ x = x.permute(0, 2, 4, 3, 5, 1).contiguous()
71
+
72
+ x = x.reshape(B, -1, C)
73
+
74
+ return x
75
+
76
+ def unpatchify_raster(x, p, target_shape):
77
+ B, N, C = x.shape
78
+ H, W = target_shape
79
+
80
+ h_patches = H // p
81
+ w_patches = W // p
82
+
83
+ x = x.view(B, h_patches, w_patches, p, p, C)
84
+
85
+ x = x.permute(0, 5, 1, 3, 2, 4).contiguous()
86
+
87
+ x = x.reshape(B, C, H, W)
88
+
89
+ return x
90
+
91
+ def patchify_raster_2d(x: torch.Tensor, p: int, H: int, W: int) -> torch.Tensor:
92
+ N, C1, C2 = x.shape
93
+
94
+ assert N == H * W, f"N ({N}) must equal H*W ({H*W})"
95
+ assert H % p == 0 and W % p == 0, f"H/W ({H}/{W}) must be divisible by patch size {p}"
96
+
97
+ C_prime = C1 * C2
98
+ x_flat = x.view(N, C_prime)
99
+
100
+ x_2d = x_flat.view(H, W, C_prime)
101
+
102
+ h_patches = H // p
103
+ w_patches = W // p
104
+
105
+ x_split = x_2d.view(h_patches, p, w_patches, p, C_prime)
106
+
107
+ x_permuted = x_split.permute(0, 2, 1, 3, 4)
108
+
109
+ x_reordered = x_permuted.reshape(N, C_prime)
110
+
111
+ out = x_reordered.view(N, C1, C2)
112
+
113
+ return out