File size: 6,546 Bytes
5af269e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import torch
import torch.nn as nn
from collections import OrderedDict
from lvdm.basics import (
    zero_module,
    conv_nd,
    avg_pool_nd
)
from einops import rearrange
from lvdm.modules.attention import register_attn_processor, set_attn_processor, DualCrossAttnProcessor, get_attn_processor
from lvdm.modules.attention import DualCrossAttnProcessorAS
from utils.utils import instantiate_from_config

from lvdm.modules.encoders.arch_transformer import Transformer


class StyleTransformer(nn.Module):
    def __init__(self, in_dim=1024, out_dim=1024, num_heads=8, num_tokens=4, n_layers=2):
        super().__init__()
        scale = in_dim ** -0.5
        self.num_tokens = num_tokens
        self.style_emb = nn.Parameter(torch.randn(1, num_tokens, in_dim) * scale)
        self.transformer_blocks = Transformer(
            width=in_dim,
            layers=n_layers,
            heads=num_heads,
        )
        self.ln1 = nn.LayerNorm(in_dim)
        self.ln2 = nn.LayerNorm(in_dim)
        self.proj = nn.Parameter(torch.randn(in_dim, out_dim) * scale)
    
    def forward(self, x):
        style_emb = self.style_emb.repeat(x.shape[0], 1, 1)
        x = torch.cat([style_emb, x], dim=1)
        # x = torch.cat([x, style_emb], dim=1)
        x = self.ln1(x)
        
        x = x.permute(1, 0, 2)
        x = self.transformer_blocks(x)
        x = x.permute(1, 0, 2)

        x = self.ln2(x[:, :self.num_tokens, :])
        x = x @ self.proj
        return x


class ScaleEncoder(nn.Module):
    def __init__(self, in_dim=1024, out_dim=1, num_heads=8, num_tokens=16, n_layers=2):
        super().__init__()
        scale = in_dim ** -0.5
        self.num_tokens = num_tokens
        self.scale_emb = nn.Parameter(torch.randn(1, num_tokens, in_dim) * scale)
        self.transformer_blocks = Transformer(
            width=in_dim,
            layers=n_layers,
            heads=num_heads,
        )
        self.ln1 = nn.LayerNorm(in_dim)
        self.ln2 = nn.LayerNorm(in_dim)

        self.out = nn.Sequential(
            nn.Linear(in_dim, 32),
            nn.GELU(),
            nn.Linear(32, out_dim),
            nn.Tanh(),
        )
        
    def forward(self, x):
        scale_emb = self.scale_emb.repeat(x.shape[0], 1, 1)
        x = torch.cat([scale_emb, x], dim=1)
        x = self.ln1(x)
        
        x = x.permute(1, 0, 2)
        x = self.transformer_blocks(x)
        x = x.permute(1, 0, 2)

        x = self.ln2(x[:, :self.num_tokens, :])
        x = self.out(x)
        return x


class DropPath(nn.Module):
    r"""DropPath but without rescaling and supports optional all-zero and/or all-keep.
    """
    def __init__(self, p):
        super(DropPath, self).__init__()
        self.p = p
    
    def forward(self, *args, zero=None, keep=None):
        if not self.training:
            return args[0] if len(args) == 1 else args
        
        # params
        x = args[0]
        b = x.size(0)
        n = (torch.rand(b) < self.p).sum()

        # non-zero and non-keep mask
        mask = x.new_ones(b, dtype=torch.bool)
        if keep is not None:
            mask[keep] = False
        if zero is not None:
            mask[zero] = False
        
        # drop-path index
        index = torch.where(mask)[0]
        index = index[torch.randperm(len(index))[:n]]
        if zero is not None:
            index = torch.cat([index, torch.where(zero)[0]], dim=0)
        
        # drop-path multiplier
        multiplier = x.new_ones(b)
        multiplier[index] = 0.0
        output = tuple(u * self.broadcast(multiplier, u) for u in args)
        return output[0] if len(args) == 1 else output
    
    def broadcast(self, src, dst):
        assert src.size(0) == dst.size(0)
        shape = (dst.size(0), ) + (1, ) * (dst.ndim - 1)
        return src.view(shape)
    

class ImageContext(nn.Module):
    def __init__(self, width=1024, context_dim=768, token_num=1):
        super().__init__()
        self.width = width
        self.token_num = token_num
        self.context_dim = context_dim

        self.fc = nn.Sequential(
            nn.Linear(context_dim, width),
            nn.SiLU(),
            nn.Linear(width, token_num * context_dim),
        )
        self.drop_path = DropPath(0.5)

    def forward(self, x):
        # x shape [B, C]
        out = self.drop_path(self.fc(x))
        out = rearrange(out, 'b (n c) -> b n c', n=self.token_num)
        return out


class StyleAdapterDualAttnAS(nn.Module):
    def __init__(self, image_context_config, scale_predictor_config, scale=1.0, use_norm=False, time_embed_dim=1024, mid_dim=32):
        super().__init__()
        self.image_context_model = instantiate_from_config(image_context_config)
        self.scale_predictor = instantiate_from_config(scale_predictor_config)
        self.scale = scale
        self.use_norm = use_norm
        self.time_embed_dim = time_embed_dim
        self.mid_dim = mid_dim
        
    def create_cross_attention_adapter(self, unet):
        ori_processor = register_attn_processor(unet)
        dual_attn_processor = {}
        for idx, key in enumerate(ori_processor.keys()):
            kv_state_dicts = {
                'k': {'weight': unet.state_dict()[key[:-10] + '.to_k.weight']},
                'v': {'weight': unet.state_dict()[key[:-10] + '.to_v.weight']},
            }
            context_dim = kv_state_dicts['k']['weight'].shape[1]
            inner_dim = kv_state_dicts['k']['weight'].shape[0]
            print(key, context_dim, inner_dim)
            
            dual_attn_processor[key] = DualCrossAttnProcessorAS(
                context_dim=context_dim,
                inner_dim=inner_dim,
                state_dict=kv_state_dicts,
                scale=self.scale,
                use_norm=self.use_norm,
                layer_idx=idx,
            )
    
        set_attn_processor(unet, dual_attn_processor)

        dual_attn_processor = {key.replace('.', '_'): value for key, value in dual_attn_processor.items()}
        self.add_module('kv_attn_layers', nn.ModuleDict(dual_attn_processor))
            
    def set_cross_attention_adapter(self, unet):
        dual_attn_processor = get_attn_processor(unet)
        for key in dual_attn_processor.keys():
            module_key = key.replace('.', '_')
            dual_attn_processor[key] = self.kv_attn_layers[module_key]
            print('set', key, module_key)
        set_attn_processor(unet, dual_attn_processor)

    def forward(self, x):
        # x shape [B, C]
        return self.image_context_model(x)