-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdit_block.py
136 lines (105 loc) · 4.48 KB
/
dit_block.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
import torch
import torch.nn as nn
import torch.nn.functional as F
def modulate(x, shift, scale):
r"""
Perform dit block shift and scale
Args:
x: torch.tensor, [b, L, c]
shift: torch.tensor, [b, c]
scale: torch.tensor, [b, c]
Return:
torch.tensor, [b, L, c]
"""
return x * (1 + scale.unsqueeze(1)) + shift.unsqueeze(1)
class MultiHeadSelfAttention(nn.Module):
r"""
multi head self attention
"""
def __init__(self, embedding_size, num_heads, qkv_bias = True):
super(MultiHeadSelfAttention, self).__init__()
self.num_heads = num_heads
self.head_embedding_size = embedding_size // num_heads
assert self.head_embedding_size * num_heads == embedding_size, \
"embedding_size should be divisible by num_heads"
self.w_q = nn.Linear(embedding_size, embedding_size, bias = qkv_bias)
self.w_k = nn.Linear(embedding_size, embedding_size, bias = qkv_bias)
self.w_v = nn.Linear(embedding_size, embedding_size, bias = qkv_bias)
def forward(self, hidden_states):
r"""
Perform multihead self attention forward
Args:
hidden_states: torch.Tensor, [b, L, c]
Return:
torch.Tensor, [b, L, c]
"""
# linear
query = self.w_q(hidden_states)
key = self.w_k(hidden_states)
value = self.w_v(hidden_states)
# view
query = query.view(query.shape[0], query.shape[1], self.num_heads, self.head_embedding_size).permute(0,2,1,3).contiguous()
key = key.view(key.shape[0], key.shape[1], self.num_heads, self.head_embedding_size).permute(0,2,1,3).contiguous()
value = value.view(value.shape[0], value.shape[1], self.num_heads, self.head_embedding_size).permute(0,2,1,3).contiguous()
# attention_scores
attention_scores = torch.matmul(query, key.transpose(-1,-2)) * query.shape[-1] ** -0.5
attention_scores = F.softmax(attention_scores, dim = -1)
attention_out = torch.matmul(attention_scores, value)
# return to original size
attention_out = attention_out.view(attention_out.shape[0], attention_out.shape[2], -1)
return attention_out
class Dit_block(nn.Module):
def __init__(self, embedding_size, num_heads, mlp_ratio = 4):
super(Dit_block, self).__init__()
self.norm1 = nn.LayerNorm(embedding_size, eps = 1e-6)
self.attn = MultiHeadSelfAttention(embedding_size, num_heads)
self.norm2 = nn.LayerNorm(embedding_size, eps = 1e-6)
self.mlp = nn.Sequential(
nn.Linear(embedding_size, embedding_size * mlp_ratio, bias = True),
nn.SiLU(),
nn.Linear(embedding_size * mlp_ratio, embedding_size, bias = True)
)
self.adaLN = nn.Sequential(
nn.SiLU(),
nn.Linear(embedding_size, embedding_size * 6, bias = True)
)
def forward(self, hidden_states, condition):
r"""
Perform dit block forward
Args:
hidden_states: torch.Tensor, [B, L, embedding_size]
condition: torch.Tensor, [B, embedding_size]
Return:
torch.Tensor, [B, L, embedding_size]
"""
x = hidden_states
condition = self.adaLN(condition)
alpha1, alpha2, beta1, beta2, gamma1, gamma2 = torch.chunk(condition, chunks = 6, dim = 1) # [b, embedding_size]
# norm1
hidden_states = self.norm1(hidden_states) # [B, L, embedding_size]
# scale and shift
hidden_states = modulate(hidden_states, beta1, gamma1) # [B, L, embedding_size]
# multi-head-self-attention
hidden_states = self.attn(hidden_states)
# scale with alpha1
hidden_states = hidden_states * alpha1.unsqueeze(1)
# resudiaul block
hidden_states = x + hidden_states
x1 = hidden_states
# norm2
hidden_states = self.norm2(hidden_states)
# scale and shift
hidden_states = modulate(hidden_states, beta2, gamma2)
# mlp
hidden_states = self.mlp(hidden_states)
# scale
hidden_states = hidden_states * alpha2.unsqueeze(1)
# residual block
hidden_states = hidden_states + x1
return hidden_states
if __name__ == "__main__":
dit_block = Dit_block(128, 4)
hidden_states = torch.randn([2, 10, 128])
condition = torch.randn([2, 128])
out = dit_block(hidden_states, condition)
print(out.shape)