File size: 31,076 Bytes
3db1cbc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
# Copyright 2026 The HuggingFace Team. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import inspect

import torch
import torch.nn.functional as F
from tqdm import tqdm

from ...configuration_utils import FrozenDict
from ...guiders import ClassifierFreeGuidance
from ...models import AutoencoderKLWan, WanAnimate2Transformer3DModel
from ...models.transformers.transformer_wan_animate_2 import WanAnimate2KVCache
from ...schedulers.scheduling_utils import SchedulerMixin
from ...utils import logging
from ...utils.torch_utils import randn_tensor
from ..modular_pipeline import BlockState, LoopSequentialPipelineBlocks, ModularPipelineBlocks, PipelineState
from ..modular_pipeline_utils import ComponentSpec, InputParam, OutputParam
from .encoders import encode_vae, get_i2v_mask


logger = logging.get_logger(__name__)  # pylint: disable=invalid-name


def decode_vae(vae: AutoencoderKLWan, latents: torch.Tensor) -> torch.Tensor:
    """De-standardize latents and VAE-decode them to `[B, 3, T, H, W]` pixels in `[-1, 1]`."""
    latents = latents.to(vae.dtype)
    latents_mean = (
        torch.tensor(vae.config.latents_mean).view(1, vae.config.z_dim, 1, 1, 1).to(latents.device, latents.dtype)
    )
    latents_recip_std = 1.0 / torch.tensor(vae.config.latents_std).view(1, vae.config.z_dim, 1, 1, 1).to(
        latents.device, latents.dtype
    )
    latents = latents / latents_recip_std + latents_mean
    return vae.decode(latents, return_dict=False)[0]


# ========================================
# Segment Loop Leaf Blocks
# ========================================


class WanAnimate2SegmentVaeEncoderStep(ModularPipelineBlocks):
    model_name = "wan-animate-2"

    @property
    def description(self) -> str:
        return (
            "Step within the segment loop that VAE-encodes this segment's slice of the driving video and stacks "
            "the i2v conditioning mask on top. The Wan VAE is causal in time, so encoding the whole video once "
            "and slicing the latents would not be equivalent — each segment restarts the temporal convolution on "
            "its own slice. A streaming mode would replace this block with one fed segments incrementally. This "
            "block should be used to compose the `sub_blocks` attribute of `WanAnimate2SegmentLoopWrapper`."
        )

    @property
    def expected_components(self) -> list[ComponentSpec]:
        return [
            ComponentSpec("vae", AutoencoderKLWan),
        ]

    @property
    def inputs(self) -> list[InputParam]:
        return [
            InputParam(
                "driving_video_pixels",
                required=True,
                type_hint=torch.Tensor,
                description="The preprocessed driving video `[1, 3, T, H, W]`, from the video preprocess step",
            ),
            InputParam(
                "reference_image_latents",
                required=True,
                type_hint=torch.Tensor,
                description="i2v mask + reference image latents `[20, 1, latent_height, latent_width]`, from the image VAE encoder step",
            ),
            InputParam(
                "effective_segment",
                required=True,
                type_hint=int,
                description="Frames each segment advances: `segment_frame_length - prev_segment_conditioning_frames`, from the video preprocess step",
            ),
            InputParam(
                "segment_frame_length",
                type_hint=int,
                default=81,
                description="The number of frames in each inference segment",
            ),
        ]

    @property
    def intermediate_outputs(self) -> list[OutputParam]:
        return [
            OutputParam(
                "driving_video_latents",
                type_hint=torch.Tensor,
                description="VAE latents of this segment's driving-video slice",
            ),
            OutputParam(
                "driving_video_condition",
                type_hint=torch.Tensor,
                description="i2v mask + driving-slice latents, conditioning the reference-extraction pass",
            ),
        ]

    @torch.no_grad()
    def __call__(self, components, block_state: BlockState, k: int):
        device = components._execution_device

        latent_height, latent_width = block_state.reference_image_latents.shape[-2:]

        start = k * block_state.effective_segment
        block_state.driving_video_latents = encode_vae(
            components.vae, block_state.driving_video_pixels[:, :, start : start + block_state.segment_frame_length]
        )

        condition_mask = get_i2v_mask(
            block_state.driving_video_latents.shape[2],
            latent_height,
            latent_width,
            block_state.segment_frame_length,
            device=device,
        ).to(block_state.driving_video_latents.dtype)
        block_state.driving_video_condition = torch.cat([condition_mask, block_state.driving_video_latents[0]], dim=0)

        return components, block_state


class WanAnimate2SegmentPrevFramesStep(ModularPipelineBlocks):
    model_name = "wan-animate-2"

    @property
    def description(self) -> str:
        return (
            "Step within the segment loop that builds the generation-side conditioning tensor `reference_latents`: the previous "
            "segment's tail frames (zeros for the first segment) are VAE-encoded, masked, and stacked under the "
            "reference half `reference_image_latents`. This is how motion continuity crosses segment boundaries — in pixel space, "
            "not latent space. This block should be used to compose the `sub_blocks` attribute of "
            "`WanAnimate2SegmentLoopWrapper`."
        )

    @property
    def expected_components(self) -> list[ComponentSpec]:
        return [
            ComponentSpec("vae", AutoencoderKLWan),
        ]

    @property
    def inputs(self) -> list[InputParam]:
        return [
            InputParam(
                "reference_image_latents",
                required=True,
                type_hint=torch.Tensor,
                description="i2v mask + reference image latents `[20, 1, latent_height, latent_width]`, from the image VAE encoder step",
            ),
            InputParam(
                "segment_frame_length",
                type_hint=int,
                default=81,
                description="The number of frames in each inference segment",
            ),
            InputParam(
                "prev_segment_conditioning_frames",
                type_hint=int,
                default=1,
                description="The number of conditioning frames carried over from the previous segment",
            ),
        ]

    @property
    def intermediate_outputs(self) -> list[OutputParam]:
        return [
            OutputParam(
                "reference_latents",
                type_hint=torch.Tensor,
                description="The full conditioning tensor: reference half stacked over the segment half",
            ),
        ]

    @torch.no_grad()
    def __call__(self, components, block_state: BlockState, k: int):
        # `block_state.out_frames` is seeded by the loop wrapper and written by the decode step of the
        # previous iteration.
        device = components._execution_device

        latent_height, latent_width = block_state.reference_image_latents.shape[-2:]
        height = latent_height * components.vae_scale_factor_spatial
        width = latent_width * components.vae_scale_factor_spatial

        num_frames = block_state.segment_frame_length + 1
        mask_len = block_state.prev_segment_conditioning_frames if k > 0 else 0
        if mask_len > 0:
            prev_frames = block_state.out_frames[0, :, -mask_len:].clone().detach()
            prev_frames = F.interpolate(prev_frames.permute(1, 0, 2, 3), size=(height, width), mode="bicubic").permute(
                1, 0, 2, 3
            )
            cond_pixels = torch.cat(
                [
                    prev_frames,
                    torch.zeros(3, num_frames - mask_len - 1, height, width, device=device),
                ],
                dim=1,
            )
        else:
            cond_pixels = torch.zeros(3, num_frames - 1, height, width, device=device)

        prev_segment_cond_latents = encode_vae(components.vae, cond_pixels.unsqueeze(0)).squeeze(0)
        prev_segment_cond_mask = get_i2v_mask(
            prev_segment_cond_latents.shape[1], latent_height, latent_width, mask_len, device=device
        ).to(prev_segment_cond_latents.dtype)
        prev_segment_cond_latents = torch.cat([prev_segment_cond_mask, prev_segment_cond_latents], dim=0)

        block_state.reference_latents = torch.cat(
            [block_state.reference_image_latents, prev_segment_cond_latents], dim=1
        )

        return components, block_state


class WanAnimate2SegmentPrepareStep(ModularPipelineBlocks):
    model_name = "wan-animate-2"

    @property
    def description(self) -> str:
        return (
            "Step within the segment loop that draws this segment's initial noise and allocates a fresh KV cache "
            "for the reference-extraction pass. This block should be used to compose the `sub_blocks` attribute "
            "of `WanAnimate2SegmentLoopWrapper`."
        )

    @property
    def expected_components(self) -> list[ComponentSpec]:
        return [
            ComponentSpec("transformer", WanAnimate2Transformer3DModel),
        ]

    @property
    def inputs(self) -> list[InputParam]:
        return [
            InputParam.template("generator"),
            InputParam(
                "reference_latents",
                required=True,
                type_hint=torch.Tensor,
                description="The full conditioning tensor: reference half stacked over the segment half",
            ),
        ]

    @property
    def intermediate_outputs(self) -> list[OutputParam]:
        return [
            OutputParam("latents", type_hint=torch.Tensor, description="This segment's initial noise"),
            OutputParam(
                "kv_cache",
                type_hint=WanAnimate2KVCache,
                description="Fresh per-segment cache for the reference K/V",
            ),
        ]

    @torch.no_grad()
    def __call__(self, components, block_state: BlockState, k: int):
        device = components._execution_device

        block_state.latents = randn_tensor(
            (
                components.num_channels_latents,
                block_state.reference_latents.shape[1],
                block_state.reference_latents.shape[-2],
                block_state.reference_latents.shape[-1],
            ),
            generator=block_state.generator,
            device=device,
            dtype=torch.float32,
        )
        block_state.kv_cache = WanAnimate2KVCache(components.transformer.config.num_layers)

        return components, block_state


class WanAnimate2SegmentSchedulerResetStep(ModularPipelineBlocks):
    model_name = "wan-animate-2"

    @property
    def description(self) -> str:
        return (
            "Step within the segment loop that resets the scheduler: each segment is an independent denoising "
            "trajectory, so the solver state and timesteps are re-prepared per segment. This block should be used "
            "to compose the `sub_blocks` attribute of `WanAnimate2SegmentLoopWrapper`."
        )

    @property
    def expected_components(self) -> list[ComponentSpec]:
        return [
            ComponentSpec("scheduler", SchedulerMixin),
        ]

    @property
    def inputs(self) -> list[InputParam]:
        return [
            InputParam.template("num_inference_steps", default=40),
        ]

    @property
    def intermediate_outputs(self) -> list[OutputParam]:
        return [
            OutputParam("timesteps", type_hint=torch.Tensor, description="This segment's denoising timesteps"),
        ]

    @torch.no_grad()
    def __call__(self, components, block_state: BlockState, k: int):
        device = components._execution_device

        components.scheduler.set_timesteps(block_state.num_inference_steps, device=device)
        block_state.timesteps = components.scheduler.timesteps

        return components, block_state


class WanAnimate2RefExtractStep(ModularPipelineBlocks):
    model_name = "wan-animate-2"

    @property
    def description(self) -> str:
        return (
            "Step within the segment loop that runs the transformer's reference-extraction pass "
            '(`kv_cache_mode="extract"`): the driving-video segment is encoded once and every layer\'s reference '
            "K/V is stored in the KV cache, which the denoising forwards then attend over. This block should be "
            "used to compose the `sub_blocks` attribute of `WanAnimate2SegmentLoopWrapper`."
        )

    @property
    def expected_components(self) -> list[ComponentSpec]:
        return [
            ComponentSpec("transformer", WanAnimate2Transformer3DModel),
        ]

    @property
    def inputs(self) -> list[InputParam]:
        return [
            InputParam(
                "driving_video_latents",
                required=True,
                type_hint=torch.Tensor,
                description="VAE latents of this segment's driving-video slice",
            ),
            InputParam(
                "driving_video_condition",
                required=True,
                type_hint=torch.Tensor,
                description="i2v mask + driving-slice latents, conditioning the reference-extraction pass",
            ),
            InputParam(
                "condition_clip_context",
                required=True,
                type_hint=torch.Tensor,
                description="CLIP vision features of the driving video's first frame",
            ),
            InputParam(
                "prompt_ref_embeds",
                required=True,
                type_hint=torch.Tensor,
                description="Text embeddings of the reference prompt, guiding the reference-extraction pass",
            ),
            InputParam(
                "kv_cache",
                required=True,
                type_hint=WanAnimate2KVCache,
                description="Per-segment cache holding every layer's reference K/V",
            ),
            InputParam(
                "timesteps",
                required=True,
                type_hint=torch.Tensor,
                description="This segment's denoising timesteps",
            ),
            InputParam(
                "max_seq_len_ref",
                required=True,
                type_hint=int,
                description="Packed sequence length of the reference tokens",
            ),
            InputParam(
                "grid_sizes_ref",
                required=True,
                type_hint=torch.Tensor,
                description="Post-patch latent grid `[[T, H/2, W/2]]` of a driving-video segment",
            ),
        ]

    @torch.no_grad()
    def __call__(self, components, block_state: BlockState, k: int):
        device = components._execution_device
        transformer_dtype = components.transformer.dtype

        t_ref = torch.tensor([block_state.timesteps[0].item()], device=device, dtype=transformer_dtype)
        components.transformer(
            [block_state.driving_video_latents[0].to(transformer_dtype)],
            timestep=t_ref,
            encoder_hidden_states=[block_state.prompt_ref_embeds[0].to(transformer_dtype)],
            encoder_hidden_states_image=block_state.condition_clip_context.to(transformer_dtype),
            condition_latents=[block_state.driving_video_condition.to(transformer_dtype)],
            kv_cache=block_state.kv_cache,
            kv_cache_mode="extract",
            seq_len=block_state.max_seq_len_ref,
            offset_grid_sizes=block_state.grid_sizes_ref,
        )

        return components, block_state


# ========================================
# Inner Denoising Blocks
# ========================================


class WanAnimate2SegmentDenoiseInner(ModularPipelineBlocks):
    model_name = "wan-animate-2"

    @property
    def description(self) -> str:
        return (
            "Inner timestep loop that denoises one segment with guidance, attending over the segment's cached "
            "reference K/V. The unconditional branch passes `is_uncondtion=True` to the transformer (it skips a "
            "dedicated layer on that branch), routed through the guider as a per-branch input. This block should "
            "be used to compose the `sub_blocks` attribute of `WanAnimate2SegmentLoopWrapper`."
        )

    @property
    def expected_components(self) -> list[ComponentSpec]:
        return [
            ComponentSpec("transformer", WanAnimate2Transformer3DModel),
            ComponentSpec("scheduler", SchedulerMixin),
            ComponentSpec(
                "guider",
                ClassifierFreeGuidance,
                config=FrozenDict({"guidance_scale": 3.0}),
                default_creation_method="from_config",
            ),
        ]

    @property
    def inputs(self) -> list[InputParam]:
        return [
            InputParam(
                "latents",
                required=True,
                type_hint=torch.Tensor,
                description="This segment's latents",
            ),
            InputParam(
                "reference_latents",
                required=True,
                type_hint=torch.Tensor,
                description="The full conditioning tensor: reference half stacked over the segment half",
            ),
            InputParam(
                "kv_cache",
                required=True,
                type_hint=WanAnimate2KVCache,
                description="Per-segment cache holding every layer's reference K/V",
            ),
            InputParam(
                "timesteps",
                required=True,
                type_hint=torch.Tensor,
                description="This segment's denoising timesteps",
            ),
            InputParam.template("num_inference_steps", default=40),
            InputParam(
                "num_segments",
                required=True,
                type_hint=int,
                description="Total number of segments in the driving video, from the video preprocess step",
            ),
            InputParam(
                "max_seq_len",
                required=True,
                type_hint=int,
                description="Packed sequence length of the generation tokens",
            ),
            InputParam(
                "grid_sizes_ref",
                required=True,
                type_hint=torch.Tensor,
                description="Post-patch latent grid `[[T, H/2, W/2]]` of a driving-video segment",
            ),
            InputParam(
                "segment_frame_length",
                type_hint=int,
                default=81,
                description="The number of frames in each inference segment",
            ),
            InputParam(
                "height",
                required=True,
                type_hint=int,
                description="The resolved frame height in pixels",
            ),
            InputParam(
                "width",
                required=True,
                type_hint=int,
                description="The resolved frame width in pixels",
            ),
            InputParam.template("generator"),
            InputParam.template("prompt_embeds"),
            InputParam.template("negative_prompt_embeds"),
            InputParam.template("denoiser_input_fields"),
        ]

    @property
    def intermediate_outputs(self) -> list[OutputParam]:
        return [
            OutputParam.template("latents"),
        ]

    @torch.no_grad()
    def __call__(self, components, block_state: BlockState, k: int):
        transformer_dtype = components.transformer.dtype

        guider_inputs = {
            "encoder_hidden_states": (block_state.prompt_embeds, block_state.negative_prompt_embeds),
            "is_uncondtion": (False, True),
        }

        # Everything the transformer accepts from the tagged conditioning fields, minus what the guider manages
        # per branch (currently the reference image's CLIP features, `encoder_hidden_states_image`).
        transformer_args = set(inspect.signature(components.transformer.forward).parameters.keys())
        shared_kwargs = {
            name: value.to(transformer_dtype) if isinstance(value, torch.Tensor) else value
            for name, value in block_state.denoiser_input_fields.items()
            if name in transformer_args and name not in guider_inputs
        }

        with tqdm(
            total=len(block_state.timesteps), desc=f"Segment {k + 1}/{block_state.num_segments}"
        ) as progress_bar:
            for i, t in enumerate(block_state.timesteps):
                timestep = torch.stack([t])

                components.guider.set_state(step=i, num_inference_steps=block_state.num_inference_steps, timestep=t)
                guider_state = components.guider.prepare_inputs(guider_inputs)

                for guider_state_batch in guider_state:
                    components.guider.prepare_models(components.transformer)

                    guider_state_batch.noise_pred = components.transformer(
                        [block_state.latents.to(transformer_dtype)],
                        timestep=timestep,
                        encoder_hidden_states=[guider_state_batch.encoder_hidden_states[0].to(transformer_dtype)],
                        condition_latents=[block_state.reference_latents.to(transformer_dtype)],
                        kv_cache=block_state.kv_cache,
                        kv_cache_mode="cached",
                        seq_len=block_state.max_seq_len,
                        reference_grid_sizes=block_state.grid_sizes_ref,
                        origin_len=block_state.segment_frame_length,
                        origin_area=[block_state.height, block_state.width],
                        is_uncondtion=guider_state_batch.is_uncondtion,
                        **shared_kwargs,
                    ).sample[0]

                    components.guider.cleanup_models(components.transformer)

                noise_pred = components.guider(guider_state)[0]

                latents = components.scheduler.step(
                    noise_pred.unsqueeze(0),
                    t,
                    block_state.latents.unsqueeze(0),
                    return_dict=False,
                    generator=block_state.generator,
                )[0]
                block_state.latents = latents.squeeze(0)

                progress_bar.update()

        return components, block_state


class WanAnimate2DistilledSegmentDenoiseInner(WanAnimate2SegmentDenoiseInner):
    model_name = "wan-animate-2-distilled"

    @property
    def description(self) -> str:
        return (
            "Inner timestep loop that denoises one segment for the distilled model, which is trained for few-step "
            "sampling without classifier-free guidance — the guider defaults to `guidance_scale=1.0`, so only the "
            "conditional branch runs. This block should be used to compose the `sub_blocks` attribute of "
            "`WanAnimate2SegmentLoopWrapper`."
        )

    @property
    def expected_components(self) -> list[ComponentSpec]:
        return [
            ComponentSpec("transformer", WanAnimate2Transformer3DModel),
            ComponentSpec("scheduler", SchedulerMixin),
            ComponentSpec(
                "guider",
                ClassifierFreeGuidance,
                config=FrozenDict({"guidance_scale": 1.0}),
                default_creation_method="from_config",
            ),
        ]


# ========================================
# Post-Denoise
# ========================================


class WanAnimate2SegmentDecodeStep(ModularPipelineBlocks):
    model_name = "wan-animate-2"

    @property
    def description(self) -> str:
        return (
            "Step within the segment loop that VAE-decodes the denoised segment. Decoding happens inside the loop "
            "because the next segment conditions on this segment's decoded pixels. Finished frames move to CPU and "
            "the per-segment KV cache and latents are freed — holding them across segments fragments the "
            "allocator enough to OOM at high resolution. This block should be used to compose the `sub_blocks` "
            "attribute of `WanAnimate2SegmentLoopWrapper`."
        )

    @property
    def expected_components(self) -> list[ComponentSpec]:
        return [
            ComponentSpec("vae", AutoencoderKLWan),
        ]

    @property
    def inputs(self) -> list[InputParam]:
        return [
            InputParam(
                "latents",
                required=True,
                type_hint=torch.Tensor,
                description="This segment's latents",
            ),
            InputParam(
                "kv_cache",
                required=True,
                type_hint=WanAnimate2KVCache,
                description="Per-segment cache holding every layer's reference K/V",
            ),
            InputParam(
                "prev_segment_conditioning_frames",
                type_hint=int,
                default=1,
                description="The number of conditioning frames carried over from the previous segment",
            ),
        ]

    @property
    def intermediate_outputs(self) -> list[OutputParam]:
        return [
            OutputParam(
                "out_frames",
                type_hint=torch.Tensor,
                description="This segment's decoded frames on device; the next segment conditions on its tail",
            ),
        ]

    @torch.no_grad()
    def __call__(self, components, block_state: BlockState, k: int):
        latents = block_state.latents.to(torch.float32)
        # The first latent frame is the reference image's slot, not video content.
        out_frames = decode_vae(components.vae, latents[:, 1:])

        if k > 0:
            out_frames = out_frames[:, :, block_state.prev_segment_conditioning_frames :]

        block_state.segment_frames.append(out_frames.cpu())
        block_state.out_frames = out_frames

        block_state.kv_cache.clear()
        block_state.kv_cache = None
        block_state.latents = None
        torch.cuda.empty_cache()

        return components, block_state


# ========================================
# Segment Loop Wrapper
# ========================================


class WanAnimate2SegmentLoopWrapper(LoopSequentialPipelineBlocks):
    model_name = "wan-animate-2"

    @property
    def description(self) -> str:
        return (
            "Pipeline block that iterates over the driving video's segments. At each segment it runs sub-blocks "
            "for per-segment encoding, preparation, reference extraction, denoising, and decoding; each segment "
            "conditions on the previous one's decoded tail frames."
        )

    @property
    def loop_inputs(self) -> list[InputParam]:
        return [
            InputParam(
                "num_segments",
                required=True,
                type_hint=int,
                description="Total number of segments in the driving video, from the video preprocess step",
            ),
        ]

    @property
    def loop_intermediate_outputs(self) -> list[OutputParam]:
        return [
            OutputParam(
                "segment_frames",
                type_hint=list[torch.Tensor],
                description="Per-segment decoded frames on CPU, each `[1, 3, T, H, W]`",
            ),
        ]

    @torch.no_grad()
    def __call__(self, components, state: PipelineState) -> PipelineState:
        block_state = self.get_block_state(state)

        # Seed the loop-carried state: `segment_frames` collects each segment's decoded frames (the decode step
        # appends to it); `out_frames` is the previous segment's decoded frames — written by the decode step, read
        # by the prev-frames step of the next iteration. `None` marks "no previous segment" for the first iteration.
        block_state.segment_frames = []
        block_state.out_frames = None

        for k in range(block_state.num_segments):
            components, block_state = self.loop_step(components, block_state, k=k)

        self.set_block_state(state, block_state)
        return components, state


# ========================================
# Composed Segment Denoise Steps
# ========================================


class WanAnimate2DenoiseStep(WanAnimate2SegmentLoopWrapper):
    block_classes = [
        WanAnimate2SegmentVaeEncoderStep,
        WanAnimate2SegmentPrevFramesStep,
        WanAnimate2SegmentPrepareStep,
        WanAnimate2SegmentSchedulerResetStep,
        WanAnimate2RefExtractStep,
        WanAnimate2SegmentDenoiseInner,
        WanAnimate2SegmentDecodeStep,
    ]
    block_names = [
        "vae_encoder",
        "prev_frames",
        "prepare",
        "scheduler_reset",
        "ref_extract",
        "denoise_inner",
        "decode",
    ]

    @property
    def description(self) -> str:
        return (
            "Segment denoise step that iterates over the driving video's segments.\n"
            "At each segment: vae_encoder -> prev_frames -> prepare -> scheduler_reset -> ref_extract -> "
            "denoise_inner -> decode."
        )


class WanAnimate2DistilledDenoiseStep(WanAnimate2SegmentLoopWrapper):
    model_name = "wan-animate-2-distilled"

    block_classes = [
        WanAnimate2SegmentVaeEncoderStep,
        WanAnimate2SegmentPrevFramesStep,
        WanAnimate2SegmentPrepareStep,
        WanAnimate2SegmentSchedulerResetStep,
        WanAnimate2RefExtractStep,
        WanAnimate2DistilledSegmentDenoiseInner,
        WanAnimate2SegmentDecodeStep,
    ]
    block_names = [
        "vae_encoder",
        "prev_frames",
        "prepare",
        "scheduler_reset",
        "ref_extract",
        "denoise_inner",
        "decode",
    ]

    @property
    def description(self) -> str:
        return (
            "Segment denoise step for the distilled model that iterates over the driving video's segments.\n"
            "At each segment: vae_encoder -> prev_frames -> prepare -> scheduler_reset -> ref_extract -> "
            "denoise_inner (no classifier-free guidance) -> decode."
        )