File size: 15,730 Bytes
788f431
 
 
1e5ce4d
788f431
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ca066a9
788f431
 
 
051fd3e
788f431
 
 
 
 
 
 
 
 
 
1fe00bb
1e5ce4d
788f431
 
 
 
1e5ce4d
788f431
 
1e5ce4d
ca066a9
1e5ce4d
788f431
 
 
 
 
051fd3e
ca066a9
788f431
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
051fd3e
788f431
 
 
 
1e5ce4d
788f431
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1e5ce4d
ca066a9
788f431
 
 
ca066a9
788f431
 
 
 
ca066a9
788f431
 
 
 
 
ca066a9
788f431
1e5ce4d
788f431
 
1e5ce4d
788f431
1e5ce4d
ca066a9
788f431
 
 
 
ca066a9
788f431
 
 
1e5ce4d
788f431
 
 
1e5ce4d
ca066a9
788f431
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1e5ce4d
 
788f431
1e5ce4d
788f431
 
 
 
 
 
 
 
 
1e5ce4d
 
788f431
 
 
 
 
 
 
 
1e5ce4d
 
788f431
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1e5ce4d
788f431
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
from typing import Tuple

import torch
import torch.nn as nn


class DualConversionNames:
    """
    Mapping from legacy dual adapter layer names to updated
    condition/modulation schema. Also supports delta/gate harmonization.
    """
    LAYER_NAMES = {
        # Projection remapping
        "t5_proj":       "condition_projection",
        "clip_proj":     "modulation_projection",

        # Cross attention
        "cross_t2c":     "cross_c2m",   # condition to modulation
        "cross_c2t":     "cross_m2c",   # modulation to condition

        # Output projections
        "anchor_proj":   "anchor_projection",
        "delta_proj":    "delta_projection",
        "logsig_proj":   "log_sigma_projection",

        # Gate and guidance
        "gate_proj":     "gate_projection",
        "guidance_proj": "guidance_projection",

        # Fuse block
        "fuse":          "fusion_block",

        # Pocket residual
        "pocket_blocks": "residual_pocket_block"
    }


# ─── Residual Pocket Block ───────────────────────────────────
class BottleneckResBlock(nn.Module):
    def __init__(self, dim, kernel=3, dropout=0.0):
        super().__init__()
        self.norm = nn.LayerNorm(dim)
        self.conv = nn.Conv1d(dim, dim, kernel_size=kernel, padding=kernel // 2, groups=1)
        self.proj = nn.Sequential(
            nn.Linear(dim, dim * 2),
            nn.GELU(),
            nn.Linear(dim * 2, dim),
            nn.Dropout(dropout)
        )

    def forward(self, x):
        residual = x
        x = self.norm(x)
        x = x.transpose(1, 2)
        x = self.conv(x).transpose(1, 2)
        return residual + self.proj(x)


class ConditionModulationShuntAdapter(nn.Module):
    def __init__(self, config: dict):
        super().__init__()
        self.config = config
        self.dtype = config.get("dtype", torch.float32)
        self.condition_dim = config.get("condition_encoders", [])[0].get("hidden_size", 768)
        self.modulation_dim = config.get("modulation_encoders", [])[0].get("hidden_size", 768)
        self.bneck = config["bottleneck"]
        self.heads = config["heads"]
        self.tau_init = config["tau_init"]
        self.max_guidance = config["max_guidance"]

        use_norm   = config.get("layer_norm", True)
        use_do     = config.get("use_dropout", True)
        do_p       = config.get("dropout", 0.0)
        proj_depth = config.get("proj_layers", 2)

        def build_projection(input_dim, output_dim):
            layers = []
            last_dim = input_dim
            if use_norm:
                layers.append(nn.LayerNorm(last_dim))
            for i in range(proj_depth):
                next_dim = self.bneck * (2 if i == 0 and proj_depth > 1 else 1)
                layers.append(nn.Linear(last_dim, next_dim))
                layers.append(nn.GELU())
                if use_do:
                    layers.append(nn.Dropout(do_p))
                last_dim = next_dim
            layers.append(nn.Linear(last_dim, output_dim))
            return nn.Sequential(*layers)

        # Projection layers
        self.condition_projection = build_projection(self.condition_dim, self.bneck)
        self.modulation_projection = build_projection(self.modulation_dim, self.bneck)

        # Cross attention blocks
        self.cross_c2m = nn.MultiheadAttention(self.bneck, self.heads, batch_first=True, dropout=do_p)
        self.cross_m2c = nn.MultiheadAttention(self.bneck, self.heads, batch_first=True, dropout=do_p)
        self.tau       = nn.Parameter(torch.full((self.heads, 1, 1), self.tau_init))

        # Residual processing block
        self.residual_pocket_block = nn.Sequential(
            BottleneckResBlock(self.bneck, dropout=do_p),
            BottleneckResBlock(self.bneck, dropout=do_p)
        )

        # Fusion pathway
        self.fusion_block = nn.Sequential(
            nn.LayerNorm(2 * self.bneck),
            nn.Linear(2 * self.bneck, self.bneck * 2),
            nn.GELU(),
            nn.Linear(self.bneck * 2, self.bneck)
        )

        # Output projections
        self.anchor_projection    = build_projection(self.bneck, self.modulation_dim)
        self.delta_projection     = build_projection(self.bneck, self.modulation_dim)
        self.log_sigma_projection = build_projection(self.bneck, self.modulation_dim)

        # Gate and guidance
        self.gate_projection = nn.Sequential(
            nn.LayerNorm(self.bneck),
            nn.Linear(self.bneck, self.bneck),
            nn.GELU(),
            nn.Linear(self.bneck, 1),
            nn.Tanh(),
            nn.Sigmoid()
        )
        self.guidance_projection = nn.Sequential(
            nn.LayerNorm(self.bneck),
            nn.Linear(self.bneck, 1),
            nn.Sigmoid()
        )

        # ─── Legacy Aliases (Version 1 Compatibility) ──────────────────────────
        self.proj_t5        = self.condition_projection
        self.proj_clip      = self.modulation_projection
        self.cross_t2c      = self.cross_c2m
        self.cross_c2t      = self.cross_m2c
        self.pocket_blocks  = self.residual_pocket_block
        self.fuse           = self.fusion_block
        self.anchor_proj    = self.anchor_projection
        self.delta_proj     = self.delta_projection
        self.logsig_proj    = self.log_sigma_projection
        self.gate_proj      = self.gate_projection
        self.guidance_proj  = self.guidance_projection

    def forward(self, cond_seq: torch.Tensor, mod_seq: torch.Tensor, config: dict = None):
        if self.config.get("assert_input_dims", True):
            assert cond_seq.size(-1) == self.condition_dim
            assert mod_seq.size(-1) == self.modulation_dim

        max_guidance = self.max_guidance if config is None else config.get("max_guidance", 0.0)
        if max_guidance <= 0:
            max_guidance = self.max_guidance
        if max_guidance <= 0:
            max_guidance = config.get("guidance_scale", 10.0)

        cond_b = self.condition_projection(cond_seq)
        mod_b  = self.modulation_projection(mod_seq)

        c2m, attn_c2m = self.cross_c2m(cond_b, mod_b, mod_b, need_weights=True, average_attn_weights=False)
        m2c, attn_m2c = self.cross_m2c(mod_b, cond_b, cond_b, need_weights=True, average_attn_weights=False)

        pocket = self.residual_pocket_block(c2m)
        pocket_mean = pocket.mean(1, keepdim=True).expand(-1, mod_b.size(1), -1)

        h = self.fusion_block(torch.cat([pocket_mean, m2c], dim=-1))

        anchor    = self.anchor_projection(h)
        delta     = self.delta_projection(h) * self.gate_projection(h)
        log_sigma = self.log_sigma_projection(h)

        g_tok  = self.guidance_projection(h).squeeze(-1)
        g_pred = g_tok.mean(1, keepdim=True) * max_guidance

        return anchor, delta, log_sigma, attn_c2m, attn_m2c, self.tau, g_pred, self.gate_projection(h)


# ─── V1 Original Two Stream Shunt Adapter ──────────────────────────────────────
class TwoStreamShuntAdapter(nn.Module):
    def __init__(self, config: dict):
        super().__init__()
        self.config = config
        self.dtype = config.get("dtype", torch.float32)
        self.t5_dim = config.get("condition_encoders", [])[0].get("hidden_size", 768)
        self.clip_dim = config.get("modulation_encoders", [])[0].get("hidden_size", 768)
        self.bneck = config["bottleneck"]
        self.heads = config["heads"]
        self.tau_init = config["tau_init"]
        self.max_guidance = config["max_guidance"]

        use_norm   = config.get("layer_norm", True)
        use_do     = config.get("use_dropout", True)
        do_p       = config.get("dropout", 0.0)
        proj_depth = config.get("proj_layers", 2)

        def build_projection(input_dim, output_dim):
            layers = []
            last_dim = input_dim
            if use_norm:
                layers.append(nn.LayerNorm(last_dim))
            for i in range(proj_depth):
                next_dim = self.bneck * (2 if i == 0 and proj_depth > 1 else 1)
                layers.append(nn.Linear(last_dim, next_dim))
                layers.append(nn.GELU())
                if use_do:
                    layers.append(nn.Dropout(do_p))
                last_dim = next_dim
            layers.append(nn.Linear(last_dim, output_dim))
            return nn.Sequential(*layers)

        # Projections
        self.proj_t5   = build_projection(self.t5_dim, self.bneck)
        self.proj_clip = build_projection(self.clip_dim, self.bneck)

        # Attention
        self.cross_t2c = nn.MultiheadAttention(self.bneck, self.heads, batch_first=True, dropout=do_p)
        self.cross_c2t = nn.MultiheadAttention(self.bneck, self.heads, batch_first=True, dropout=do_p)
        self.tau       = nn.Parameter(torch.full((self.heads, 1, 1), self.tau_init))

        # Residual Pocket
        self.pocket_blocks = nn.Sequential(
            BottleneckResBlock(self.bneck, dropout=do_p),
            BottleneckResBlock(self.bneck, dropout=do_p)
        )

        # Fuse
        self.fuse = nn.Sequential(
            nn.LayerNorm(2 * self.bneck),
            nn.Linear(2 * self.bneck, self.bneck * 2),
            nn.GELU(),
            nn.Linear(self.bneck * 2, self.bneck)
        )

        # Output Projections
        self.anchor_proj = build_projection(self.bneck, self.clip_dim)
        self.delta_proj  = build_projection(self.bneck, self.clip_dim)
        self.logsig_proj = build_projection(self.bneck, self.clip_dim)

        self.gate_proj = nn.Sequential(
            nn.LayerNorm(self.bneck),
            nn.Linear(self.bneck, self.bneck),
            nn.GELU(),
            nn.Linear(self.bneck, 1),
            nn.Tanh(),
            nn.Sigmoid()
        )

        self.guidance_proj = nn.Sequential(
            nn.LayerNorm(self.bneck),
            nn.Linear(self.bneck, 1),
            nn.Sigmoid()
        )

    def forward(self, t5_seq: torch.Tensor, clip_seq: torch.Tensor, config: dict = None):
        if self.config.get("assert_input_dims", True):
            assert t5_seq.size(-1) == self.t5_dim
            assert clip_seq.size(-1) == self.clip_dim

        max_guidance = self.max_guidance if config is None else config.get("max_guidance", 0.0)
        if max_guidance <= 0:
            max_guidance = self.max_guidance
        if max_guidance <= 0:
            max_guidance = 10
        max_guidance = config.get("guidance_scale", 5.0)

        t5_b   = self.proj_t5(t5_seq)
        clip_b = self.proj_clip(clip_seq)

        t2c, attn_t2c = self.cross_t2c(t5_b, clip_b, clip_b, need_weights=True, average_attn_weights=False)
        c2t, attn_c2t = self.cross_c2t(clip_b, t5_b, t5_b, need_weights=True, average_attn_weights=False)

        pocket = self.pocket_blocks(t2c)

        pocket_mean = pocket.mean(1, keepdim=True).expand(-1, clip_b.size(1), -1)
        h = self.fuse(torch.cat([pocket_mean, c2t], dim=-1))

        anchor    = self.anchor_proj(h)
        delta     = self.delta_proj(h) * self.gate_proj(h)
        log_sigma = self.logsig_proj(h)

        g_tok  = self.guidance_proj(h).squeeze(-1)
        g_pred = g_tok.mean(1, keepdim=True) * max_guidance

        return anchor, delta, log_sigma, attn_t2c, attn_c2t, self.tau, g_pred, self.gate_proj(h)




from safetensors.torch import save_file, load_file

def save_safetensors(adapter: nn.Module, path: str, metadata: dict = None):
    """
    Save the current adapter state to safetensors format.
    All tensors are moved to CPU and saved as float32 for compatibility.
    Optional metadata may be embedded (e.g., version, prompt_mode).
    """
    state = {k: v.float().cpu() for k, v in adapter.state_dict().items()}
    save_file(state, path, metadata=metadata or {})
    print(f"βœ… Model saved to {path}")


def load_safetensors(adapter: nn.Module, path: str, map_location="cpu"):
    """
    Load a safetensors checkpoint into the adapter.
    Uses strict key matching. Tensors are loaded to the specified device.
    """
    state = load_file(path, device=map_location)
    adapter.load_state_dict(state, strict=True)
    print(f"βœ… Model loaded from {path}")


def load_converted_safetensors(adapter: nn.Module, path: str, map_location="cpu"):
    """
    Load a legacy-format adapter into the updated dual-shunt schema.
    Converts key names according to DualConversionNames mapping.
    """
    state = load_file(path, device=map_location)
    new_state = {}

    rename_map = DualConversionNames.LAYER_NAMES
    matched, renamed, skipped = 0, 0, 0

    for key, tensor in state.items():
        found = False
        for old, new in rename_map.items():
            if old in key:
                new_key = key.replace(old, new)
                new_state[new_key] = tensor
                print(f"[MIGRATE] {key} β†’ {new_key}")
                renamed += 1
                found = True
                break
        if not found:
            if key in adapter.state_dict():
                new_state[key] = tensor
                matched += 1
            else:
                print(f"[SKIP]   {key} not found in target adapter.")
                skipped += 1

    adapter.load_state_dict(new_state, strict=False)

    print(f"\nβœ… Converted model loaded from {path}")
    print(f"   πŸ” Renamed Keys: {renamed}")
    print(f"   βœ… Direct Matches: {matched}")
    print(f"   ⚠️  Skipped Keys: {skipped}")


def reshape_for_shunt(
    encoder_embeddings: torch.Tensor,
    clip_slice: torch.Tensor,
    adapter_model
) -> Tuple[torch.Tensor, torch.Tensor]:
    """
    Ensures encoder_embeddings and clip_slice match the required dimensions
    for adapter_model: [B, adapter_seq, adapter_dim].

    Applies sequence interpolation and feature projection as needed.
    """
    return encoder_embeddings, clip_slice
    B, encoder_seq, encoder_dim = encoder_embeddings.shape
    B2, clip_seq, clip_dim = clip_slice.shape

    assert B == B2, "Batch sizes must match"

    # -- Step 1: Interpolate SEQUENCE LENGTH (dim=1) if needed --
    target_seq = max(adapter_model.condition_dim, adapter_model.modulation_dim)

    if clip_seq != target_seq:
        clip_slice = clip_slice.permute(0, 0, 2)  # [B, C, T]
        clip_slice = torch.nn.functional.interpolate(
            clip_slice.float(),
            size=target_seq,
            mode="nearest"
        )
        clip_slice = clip_slice.permute(0, 0, 2)  # [B, T, C]

    if encoder_seq != target_seq:
        encoder_embeddings = encoder_embeddings.permute(0, 0, 2)
        encoder_embeddings = torch.nn.functional.interpolate(
            encoder_embeddings.float(),
            size=target_seq,
            mode="nearest"
        )
        encoder_embeddings = encoder_embeddings.permute(0, 0, 2)

    # -- Step 2: Project FEATURE DIMENSION (dim=2) if needed --
    if clip_slice.size(-1) != adapter_model.condition_dim:
        projection_clip = torch.nn.Linear(
            clip_slice.size(-1),
            adapter_model.condition_dim,
            bias=True,
            device=clip_slice.device
        )
        clip_slice = projection_clip(clip_slice)
        del projection_clip

    if encoder_embeddings.size(-1) != adapter_model.modulation_dim:
        projection_encoder = torch.nn.Linear(
            encoder_embeddings.size(-1),
            adapter_model.modulation_dim,
            bias=True,
            device=encoder_embeddings.device
        )
        encoder_embeddings = projection_encoder(encoder_embeddings)
        del projection_encoder

    return encoder_embeddings, clip_slice