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
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import logging
from abc import ABC, abstractmethod
from typing import Dict, List, Literal, Optional, Tuple, Union

import torch
import torch.nn.functional as F
from einops import rearrange, repeat

from ...util import append_dims, default

logpy = logging.getLogger(__name__)


class Guider(ABC):
    @abstractmethod
    def __call__(self, x: torch.Tensor, sigma: float) -> torch.Tensor:
        pass

    def prepare_inputs(
        self, x: torch.Tensor, s: float, c: Dict, uc: Dict
    ) -> Tuple[torch.Tensor, float, Dict]:
        pass


class VanillaCFG(Guider):
    def __init__(
        self, scale: float, low_sigma: float = 0.0, high_sigma: float = float("inf")
    ):
        self.scale = scale
        self.low_sigma = low_sigma
        self.high_sigma = high_sigma

    def set_scale(self, scale: float):
        self.scale = scale

    def __call__(self, x: torch.Tensor, sigma: torch.Tensor) -> torch.Tensor:
        x_u, x_c = x.chunk(2)

        x_pred = x_u + self.scale * (x_c - x_u)
        return x_pred

    def prepare_inputs(self, x, s, c, uc):
        c_out = dict()

        for k in c:
            if k in [
                "vector",
                "crossattn",
                "concat",
                "audio_emb",
                "image_embeds",
                "landmarks",
                "masks",
                "gt",
                "valence",
                "arousal",
            ]:
                c_out[k] = torch.cat((uc[k], c[k]), 0)
            elif k == "reference":
                c_out["reference"] = []
                for i in range(len(c[k])):
                    c_out["reference"].append(torch.cat((uc[k][i], c[k][i]), 0))
            else:
                assert c[k] == uc[k]
                c_out[k] = c[k]
        return torch.cat([x] * 2), torch.cat([s] * 2), c_out


class VanillaSTG(Guider):
    def __init__(
        self,
        scale_spatial: float,
        scale_temporal: float,
        low_sigma: float = 0.0,
        high_sigma: float = float("inf"),
        layer_skip: int = 8,
    ):
        self.scale_spatial = scale_spatial
        self.scale_temporal = scale_temporal
        self.low_sigma = low_sigma
        self.high_sigma = high_sigma
        self.layer_skip = layer_skip

    def set_scale(self, scale_spatial: float, scale_temporal: float):
        self.scale_spatial = scale_spatial
        self.scale_temporal = scale_temporal

    def __call__(self, x: torch.Tensor, sigma: torch.Tensor) -> torch.Tensor:
        x_c, x_spatial, x_temporal = x.chunk(3)
        x_pred = (
            x_c
            + self.scale_spatial * (x_c - x_spatial)
            + self.scale_temporal * (x_c - x_temporal)
        )
        return x_pred

    def prepare_inputs(self, x, s, c, uc):
        c_out = dict()

        for k in c:
            if k in [
                "vector",
                "crossattn",
                "concat",
                "audio_emb",
                "image_embeds",
                "landmarks",
                "masks",
                "gt",
                "valence",
                "arousal",
            ]:
                c_out[k] = torch.cat((c[k], c[k], c[k]), 0)
            elif k == "reference":
                c_out["reference"] = []
                for i in range(len(c[k])):
                    c_out["reference"].append(torch.cat((c[k][i], c[k][i], c[k][i]), 0))
            else:
                assert c[k] == uc[k]
                c_out[k] = c[k]

        c_out["skip_spatial_attention_at"] = [None, self.layer_skip, None]
        c_out["skip_temporal_attention_at"] = [None, None, self.layer_skip]

        return torch.cat([x] * 3), torch.cat([s] * 3), c_out


class MomentumBuffer:
    def __init__(self, momentum: float):
        self.momentum = momentum
        self.running_average = 0

    def update(self, update_value: torch.Tensor):
        new_average = self.momentum * self.running_average
        self.running_average = update_value + new_average


def project(v0: torch.Tensor, v1: torch.Tensor):
    dtype = v0.dtype
    v0, v1 = v0.double(), v1.double()
    v1 = F.normalize(v1, dim=[-1, -2, -3])
    v0_parallel = (v0 * v1).sum(dim=[-1, -2, -3], keepdim=True) * v1
    v0_orthogonal = v0 - v0_parallel
    return v0_parallel.to(dtype), v0_orthogonal.to(dtype)


def normalized_guidance(
    pred_cond: torch.Tensor,
    pred_uncond: torch.Tensor,
    guidance_scale: float,
    momentum_buffer: MomentumBuffer = None,
    eta: float = 1.0,
    norm_threshold: float = 0.0,
):
    diff = pred_cond - pred_uncond
    if momentum_buffer is not None:
        momentum_buffer.update(diff)
        diff = momentum_buffer.running_average
    if norm_threshold > 0:
        ones = torch.ones_like(diff)
        diff_norm = diff.norm(p=2, dim=[-1, -2, -3], keepdim=True)
        scale_factor = torch.minimum(ones, norm_threshold / diff_norm)
        diff = diff * scale_factor
    diff_parallel, diff_orthogonal = project(diff, pred_cond)
    normalized_update = diff_orthogonal + eta * diff_parallel
    pred_guided = pred_cond + (guidance_scale - 1) * normalized_update
    return pred_guided


class APGGuider(VanillaCFG):
    def __init__(
        self,
        scale: float,
        momentum: float = -0.75,
        eta: float = 0.0,
        norm_threshold: float = 2.5,
    ):
        super().__init__(scale)
        self.momentum_buffer = MomentumBuffer(momentum)
        self.eta = eta
        self.norm_threshold = norm_threshold

    def __call__(self, x: torch.Tensor, sigma: torch.Tensor) -> torch.Tensor:
        x_u, x_c = x.chunk(2)
        return normalized_guidance(
            x_c, x_u, self.scale, self.momentum_buffer, self.eta, self.norm_threshold
        )


class VanillaCFGplusplus(VanillaCFG):
    def __call__(self, x: torch.Tensor, sigma: torch.Tensor) -> torch.Tensor:
        x_u, x_c = x.chunk(2)

        x_pred = x_u + self.scale * (x_c - x_u)
        return x_pred, x_u


class KarrasGuider(VanillaCFG):
    def prepare_inputs(self, x, s, c, uc):
        c_out = dict()

        for k in c:
            if k in [
                "vector",
                "crossattn",
                "concat",
                "audio_emb",
                "image_embeds",
                "landmarks",
                "valence",
                "arousal",
            ]:
                c_out[k] = torch.cat((c[k], c[k]), 0)
            elif k == "reference":
                c_out["reference"] = []
                for i in range(len(c[k])):
                    c_out["reference"].append(torch.cat((c[k][i], c[k][i]), 0))
            else:
                assert c[k] == uc[k]
                c_out[k] = c[k]
        return torch.cat([x] * 2), torch.cat([s] * 2), c_out


class MultipleCondVanilla(Guider):
    def __init__(self, scales, condition_names) -> None:
        assert len(scales) == len(condition_names)
        self.scales = scales
        # self.condition_names = condition_names
        self.n_conditions = len(scales)
        self.map_cond_name = {
            "audio_emb": "audio_emb",
            "cond_frames_without_noise": "crossattn",
            "cond_frames": "concat",
        }
        self.condition_names = [
            self.map_cond_name.get(cond_name, cond_name)
            for cond_name in condition_names
        ]
        print("Condition names: ", self.condition_names)

    def __call__(self, x: torch.Tensor, sigma: torch.Tensor) -> torch.Tensor:
        outs = x.chunk(self.n_conditions + 1)
        x_full_cond = outs[0]
        x_pred = (1 + sum(self.scales)) * x_full_cond
        for i, scale in enumerate(self.scales):
            x_pred -= scale * outs[i + 1]
        return x_pred

    def prepare_inputs(self, x, s, c, uc):
        c_out = dict()

        # The first element is the full condition
        for k in c:
            if k in [
                "vector",
                "crossattn",
                "concat",
                "audio_emb",
                "image_embeds",
                "landmarks",
                "masks",
                "gt",
            ]:
                c_out[k] = c[k]
            else:
                assert c[k] == uc[k]
                c_out[k] = c[k]

        # The rest are the conditions removed from the full condition
        for cond_name in self.condition_names:
            if not isinstance(cond_name, list):
                cond_name = [cond_name]
            for k in c:
                if k in [
                    "vector",
                    "crossattn",
                    "concat",
                    "audio_emb",
                    "image_embeds",
                    "landmarks",
                    "masks",
                    "gt",
                ]:
                    c_out[k] = torch.cat(
                        (c_out[k], uc[k] if k in cond_name else c[k]), 0
                    )

        return (
            torch.cat([x] * (self.n_conditions + 1)),
            torch.cat([s] * (self.n_conditions + 1)),
            c_out,
        )


class AudioRefMultiCondGuider(MultipleCondVanilla):
    def __init__(
        self,
        audio_ratio: float = 5.0,
        ref_ratio: float = 3.0,
        use_normalized: bool = False,
        momentum: float = -0.75,
        eta: float = 0.0,
        norm_threshold: float = 2.5,
    ):
        super().__init__(
            scales=[audio_ratio, ref_ratio], condition_names=["audio_emb", "concat"]
        )
        self.audio_ratio = audio_ratio
        self.ref_ratio = ref_ratio
        self.use_normalized = use_normalized
        print(f"Use normalized: {self.use_normalized}")
        self.momentum_buffer = MomentumBuffer(momentum)
        self.eta = eta
        self.norm_threshold = norm_threshold
        self.momentum_buffer_audio = MomentumBuffer(momentum)
        self.momentum_buffer_ref = MomentumBuffer(momentum)

    def __call__(self, x: torch.Tensor, sigma: torch.Tensor) -> torch.Tensor:
        e_uc, e_ref, c_audio_ref = x.chunk(3)

        if self.use_normalized:
            # Normalized guidance version
            # Compute diff for audio guidance
            diff_audio = c_audio_ref - e_uc
            if self.momentum_buffer_audio is not None:
                self.momentum_buffer_audio.update(diff_audio)
                diff_audio = self.momentum_buffer_audio.running_average
            if self.norm_threshold > 0:
                ones = torch.ones_like(diff_audio)
                diff_norm = diff_audio.norm(p=2, dim=[-1, -2, -3], keepdim=True)
                scale_factor = torch.minimum(ones, self.norm_threshold / diff_norm)
                diff_audio = diff_audio * scale_factor
            diff_audio_parallel, diff_audio_orthogonal = project(
                diff_audio, c_audio_ref
            )
            normalized_update_audio = (
                diff_audio_orthogonal + self.eta * diff_audio_parallel
            )
            guidance_audio = (self.audio_ratio - 1) * normalized_update_audio

            # Compute diff for ref guidance
            diff_ref = e_ref - e_uc
            if self.momentum_buffer_ref is not None:
                self.momentum_buffer_ref.update(diff_ref)
                diff_ref = self.momentum_buffer_ref.running_average
            if self.norm_threshold > 0:
                ones = torch.ones_like(diff_ref)
                diff_norm = diff_ref.norm(p=2, dim=[-1, -2, -3], keepdim=True)
                scale_factor = torch.minimum(ones, self.norm_threshold / diff_norm)
                diff_ref = diff_ref * scale_factor
            diff_ref_parallel, diff_ref_orthogonal = project(diff_ref, e_ref)
            normalized_update_ref = diff_ref_orthogonal + self.eta * diff_ref_parallel
            guidance_ref = (self.ref_ratio - 1) * normalized_update_ref

            e_final = e_uc + guidance_audio + guidance_ref
        else:
            # Original version
            e_final = (
                self.audio_ratio * (c_audio_ref - e_ref)
                + self.ref_ratio * (e_ref - e_uc)
                + e_uc
            )

        return e_final

    def set_scale(self, scale: torch.Tensor):
        self.audio_ratio = float(scale[0])
        self.ref_ratio = float(scale[1])
        print(f"Audio ratio: {self.audio_ratio}, Ref ratio: {self.ref_ratio}")

    def prepare_inputs(self, x, s, c, uc):
        c_out = dict()

        # Prepare inputs for e_base (no audio, no ref concat)
        c_base = {k: v for k, v in c.items()}
        c_base["crossattn"] = uc["crossattn"]
        c_base["concat"] = uc["concat"]  # Remove ref concat

        # Prepare inputs for e_ref (no audio, with ref concat)
        c_audio_ref = {k: v for k, v in c.items()}
        # c_ref["concat"] = uc["concat"]  # Remove ref concat

        # Prepare inputs for e_audio (all conditions)
        c_ref = {k: v for k, v in c.items()}
        c_ref["crossattn"] = uc["crossattn"]

        # Combine all conditions
        for k in c:
            if k in [
                "vector",
                "crossattn",
                "concat",
                "audio_emb",
                "image_embeds",
                "landmarks",
                "masks",
                "gt",
            ]:
                c_out[k] = torch.cat((c_base[k], c_ref[k], c_audio_ref[k]), 0)
            else:
                c_out[k] = c[k]

        return torch.cat([x] * 3), torch.cat([s] * 3), c_out


class IdentityGuider(Guider):
    def __init__(self, *args, **kwargs):
        # self.num_frames = num_frames
        pass

    def __call__(self, x: torch.Tensor, sigma: float) -> torch.Tensor:
        return x

    def prepare_inputs(
        self, x: torch.Tensor, s: float, c: Dict, uc: Dict
    ) -> Tuple[torch.Tensor, float, Dict]:
        c_out = dict()

        for k in c:
            c_out[k] = c[k]

        return x, s, c_out


class LinearPredictionGuider(Guider):
    def __init__(
        self,
        max_scale: float,
        num_frames: int,
        min_scale: float = 1.0,
        additional_cond_keys: Optional[Union[List[str], str]] = None,
        only_first=False,
    ):
        self.min_scale = min_scale
        self.max_scale = max_scale
        self.num_frames = num_frames
        self.scale = torch.linspace(min_scale, max_scale, num_frames).unsqueeze(0)

        self.only_first = only_first
        if only_first:
            self.scale = torch.ones_like(self.scale) * max_scale
            self.scale[:, 0] = min_scale

        additional_cond_keys = default(additional_cond_keys, [])
        if isinstance(additional_cond_keys, str):
            additional_cond_keys = [additional_cond_keys]
        self.additional_cond_keys = additional_cond_keys

    def set_scale(self, scale: torch.Tensor):
        self.min_scale = scale
        self.scale = torch.linspace(
            self.min_scale, self.max_scale, self.num_frames
        ).unsqueeze(0)

        if self.only_first:
            self.scale = torch.ones_like(self.scale) * self.max_scale
            self.scale[:, 0] = self.min_scale

        print(self.scale)

    def __call__(self, x: torch.Tensor, sigma: torch.Tensor) -> torch.Tensor:
        x_u, x_c = x.chunk(2)

        x_u = rearrange(x_u, "(b t) ... -> b t ...", t=self.num_frames)
        x_c = rearrange(x_c, "(b t) ... -> b t ...", t=self.num_frames)
        scale = repeat(self.scale, "1 t -> b t", b=x_u.shape[0])
        scale = append_dims(scale, x_u.ndim).to(x_u.device)
        return rearrange(x_u + scale * (x_c - x_u), "b t ... -> (b t) ...")

    def prepare_inputs(
        self, x: torch.Tensor, s: torch.Tensor, c: dict, uc: dict
    ) -> Tuple[torch.Tensor, torch.Tensor, dict]:
        c_out = dict()

        for k in c:
            if (
                k
                in ["vector", "crossattn", "concat", "audio_emb", "masks", "gt"]
                + self.additional_cond_keys
            ):
                c_out[k] = torch.cat((uc[k], c[k]), 0)
            else:
                assert c[k] == uc[k]
                c_out[k] = c[k]

        return torch.cat([x] * 2), torch.cat([s] * 2), c_out


class LinearPredictionGuiderPlus(LinearPredictionGuider):
    def __init__(
        self,
        max_scale: float,
        num_frames: int,
        min_scale: float = 1.0,
        additional_cond_keys: Optional[Union[List[str], str]] = None,
    ):
        super().__init__(max_scale, num_frames, min_scale, additional_cond_keys)

    def __call__(self, x: torch.Tensor, sigma: torch.Tensor) -> torch.Tensor:
        x_u, x_c = x.chunk(2)

        x_u = rearrange(x_u, "(b t) ... -> b t ...", t=self.num_frames)
        x_c = rearrange(x_c, "(b t) ... -> b t ...", t=self.num_frames)
        scale = repeat(self.scale, "1 t -> b t", b=x_u.shape[0])
        scale = append_dims(scale, x_u.ndim).to(x_u.device)
        return rearrange(x_u + scale * (x_c - x_u), "b t ... -> (b t) ..."), x_u


class TrianglePredictionGuider(LinearPredictionGuider):
    def __init__(
        self,
        max_scale: float,
        num_frames: int,
        min_scale: float = 1.0,
        period: float | List[float] = 1.0,
        period_fusing: Literal["mean", "multiply", "max"] = "max",
        additional_cond_keys: Optional[Union[List[str], str]] = None,
    ):
        super().__init__(max_scale, num_frames, min_scale, additional_cond_keys)
        values = torch.linspace(0, 1, num_frames)
        # Constructs a triangle wave
        if isinstance(period, float):
            period = [period]

        scales = []
        for p in period:
            scales.append(self.triangle_wave(values, p))

        if period_fusing == "mean":
            scale = sum(scales) / len(period)
        elif period_fusing == "multiply":
            scale = torch.prod(torch.stack(scales), dim=0)
        elif period_fusing == "max":
            scale = torch.max(torch.stack(scales), dim=0).values
        self.scale = (scale * (max_scale - min_scale) + min_scale).unsqueeze(0)

    def triangle_wave(self, values: torch.Tensor, period) -> torch.Tensor:
        return 2 * (values / period - torch.floor(values / period + 0.5)).abs()