modular_blocks.py 16.4 KB
Newer Older
Aryan's avatar
Aryan committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
# Copyright 2025 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.

from ...utils import logging
from ..modular_pipeline import AutoPipelineBlocks, SequentialPipelineBlocks
from ..modular_pipeline_utils import InsertableDict
from .before_denoise import (
YiYi Xu's avatar
YiYi Xu committed
19
20
21
    WanAdditionalInputsStep,
    WanPrepareFirstFrameLatentsStep,
    WanPrepareFirstLastFrameLatentsStep,
Aryan's avatar
Aryan committed
22
23
    WanPrepareLatentsStep,
    WanSetTimestepsStep,
YiYi Xu's avatar
YiYi Xu committed
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
    WanTextInputStep,
)
from .decoders import WanImageVaeDecoderStep
from .denoise import (
    Wan22DenoiseStep,
    Wan22Image2VideoDenoiseStep,
    WanDenoiseStep,
    WanFLF2VDenoiseStep,
    WanImage2VideoDenoiseStep,
)
from .encoders import (
    WanFirstLastFrameImageEncoderStep,
    WanFirstLastFrameVaeImageEncoderStep,
    WanImageCropResizeStep,
    WanImageEncoderStep,
    WanImageResizeStep,
    WanTextEncoderStep,
    WanVaeImageEncoderStep,
Aryan's avatar
Aryan committed
42
43
44
45
46
47
)


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


YiYi Xu's avatar
YiYi Xu committed
48
49
50
# wan2.1
# wan2.1: text2vid
class WanCoreDenoiseStep(SequentialPipelineBlocks):
Aryan's avatar
Aryan committed
51
    block_classes = [
YiYi Xu's avatar
YiYi Xu committed
52
        WanTextInputStep,
Aryan's avatar
Aryan committed
53
54
        WanSetTimestepsStep,
        WanPrepareLatentsStep,
YiYi Xu's avatar
YiYi Xu committed
55
        WanDenoiseStep,
Aryan's avatar
Aryan committed
56
    ]
YiYi Xu's avatar
YiYi Xu committed
57
    block_names = ["input", "set_timesteps", "prepare_latents", "denoise"]
Aryan's avatar
Aryan committed
58
59
60
61

    @property
    def description(self):
        return (
YiYi Xu's avatar
YiYi Xu committed
62
            "denoise block that takes encoded conditions and runs the denoising process.\n"
Aryan's avatar
Aryan committed
63
            + "This is a sequential pipeline blocks:\n"
YiYi Xu's avatar
YiYi Xu committed
64
            + " - `WanTextInputStep` is used to adjust the batch size of the model inputs\n"
Aryan's avatar
Aryan committed
65
66
            + " - `WanSetTimestepsStep` is used to set the timesteps\n"
            + " - `WanPrepareLatentsStep` is used to prepare the latents\n"
YiYi Xu's avatar
YiYi Xu committed
67
            + " - `WanDenoiseStep` is used to denoise the latents\n"
Aryan's avatar
Aryan committed
68
69
70
        )


YiYi Xu's avatar
YiYi Xu committed
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
# wan2.1: image2video
## image encoder
class WanImage2VideoImageEncoderStep(SequentialPipelineBlocks):
    model_name = "wan"
    block_classes = [WanImageResizeStep, WanImageEncoderStep]
    block_names = ["image_resize", "image_encoder"]

    @property
    def description(self):
        return "Image2Video Image Encoder step that resize the image and encode the image to generate the image embeddings"


## vae encoder
class WanImage2VideoVaeImageEncoderStep(SequentialPipelineBlocks):
    model_name = "wan"
    block_classes = [WanImageResizeStep, WanVaeImageEncoderStep]
    block_names = ["image_resize", "vae_image_encoder"]

    @property
    def description(self):
        return "Image2Video Vae Image Encoder step that resize the image and encode the first frame image to its latent representation"


## denoise
class WanImage2VideoCoreDenoiseStep(SequentialPipelineBlocks):
Aryan's avatar
Aryan committed
96
    block_classes = [
YiYi Xu's avatar
YiYi Xu committed
97
98
99
100
101
102
103
104
105
106
107
108
109
110
        WanTextInputStep,
        WanAdditionalInputsStep(image_latent_inputs=["first_frame_latents"]),
        WanSetTimestepsStep,
        WanPrepareLatentsStep,
        WanPrepareFirstFrameLatentsStep,
        WanImage2VideoDenoiseStep,
    ]
    block_names = [
        "input",
        "additional_inputs",
        "set_timesteps",
        "prepare_latents",
        "prepare_first_frame_latents",
        "denoise",
Aryan's avatar
Aryan committed
111
112
113
114
115
    ]

    @property
    def description(self):
        return (
YiYi Xu's avatar
YiYi Xu committed
116
117
118
119
120
121
122
123
            "denoise block that takes encoded text and image latent conditions and runs the denoising process.\n"
            + "This is a sequential pipeline blocks:\n"
            + " - `WanTextInputStep` is used to adjust the batch size of the model inputs\n"
            + " - `WanAdditionalInputsStep` is used to adjust the batch size of the latent conditions\n"
            + " - `WanSetTimestepsStep` is used to set the timesteps\n"
            + " - `WanPrepareLatentsStep` is used to prepare the latents\n"
            + " - `WanPrepareFirstFrameLatentsStep` is used to prepare the first frame latent conditions\n"
            + " - `WanImage2VideoDenoiseStep` is used to denoise the latents\n"
Aryan's avatar
Aryan committed
124
125
126
        )


YiYi Xu's avatar
YiYi Xu committed
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
# wan2.1: FLF2v


## image encoder
class WanFLF2VImageEncoderStep(SequentialPipelineBlocks):
    model_name = "wan"
    block_classes = [WanImageResizeStep, WanImageCropResizeStep, WanFirstLastFrameImageEncoderStep]
    block_names = ["image_resize", "last_image_resize", "image_encoder"]

    @property
    def description(self):
        return "FLF2V Image Encoder step that resize and encode and encode the first and last frame images to generate the image embeddings"


## vae encoder
class WanFLF2VVaeImageEncoderStep(SequentialPipelineBlocks):
    model_name = "wan"
    block_classes = [WanImageResizeStep, WanImageCropResizeStep, WanFirstLastFrameVaeImageEncoderStep]
    block_names = ["image_resize", "last_image_resize", "vae_image_encoder"]

    @property
    def description(self):
        return "FLF2V Vae Image Encoder step that resize and encode and encode the first and last frame images to generate the latent conditions"


## denoise
class WanFLF2VCoreDenoiseStep(SequentialPipelineBlocks):
Aryan's avatar
Aryan committed
154
    block_classes = [
YiYi Xu's avatar
YiYi Xu committed
155
156
157
158
159
160
161
162
163
164
165
166
167
168
        WanTextInputStep,
        WanAdditionalInputsStep(image_latent_inputs=["first_last_frame_latents"]),
        WanSetTimestepsStep,
        WanPrepareLatentsStep,
        WanPrepareFirstLastFrameLatentsStep,
        WanFLF2VDenoiseStep,
    ]
    block_names = [
        "input",
        "additional_inputs",
        "set_timesteps",
        "prepare_latents",
        "prepare_first_last_frame_latents",
        "denoise",
Aryan's avatar
Aryan committed
169
170
171
    ]

    @property
YiYi Xu's avatar
YiYi Xu committed
172
    def description(self):
Aryan's avatar
Aryan committed
173
        return (
YiYi Xu's avatar
YiYi Xu committed
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
            "denoise block that takes encoded text and image latent conditions and runs the denoising process.\n"
            + "This is a sequential pipeline blocks:\n"
            + " - `WanTextInputStep` is used to adjust the batch size of the model inputs\n"
            + " - `WanAdditionalInputsStep` is used to adjust the batch size of the latent conditions\n"
            + " - `WanSetTimestepsStep` is used to set the timesteps\n"
            + " - `WanPrepareLatentsStep` is used to prepare the latents\n"
            + " - `WanPrepareFirstLastFrameLatentsStep` is used to prepare the latent conditions\n"
            + " - `WanImage2VideoDenoiseStep` is used to denoise the latents\n"
        )


# wan2.1: auto blocks
## image encoder
class WanAutoImageEncoderStep(AutoPipelineBlocks):
    block_classes = [WanFLF2VImageEncoderStep, WanImage2VideoImageEncoderStep]
    block_names = ["flf2v_image_encoder", "image2video_image_encoder"]
    block_trigger_inputs = ["last_image", "image"]

    @property
    def description(self):
        return (
            "Image Encoder step that encode the image to generate the image embeddings"
            + "This is an auto pipeline block that works for image2video tasks."
            + " - `WanFLF2VImageEncoderStep` (flf2v) is used when `last_image` is provided."
            + " - `WanImage2VideoImageEncoderStep` (image2video) is used when `image` is provided."
            + " - if `last_image` or `image` is not provided, step will be skipped."
Aryan's avatar
Aryan committed
200
201
202
        )


YiYi Xu's avatar
YiYi Xu committed
203
204
205
206
207
## vae encoder
class WanAutoVaeImageEncoderStep(AutoPipelineBlocks):
    block_classes = [WanFLF2VVaeImageEncoderStep, WanImage2VideoVaeImageEncoderStep]
    block_names = ["flf2v_vae_image_encoder", "image2video_vae_image_encoder"]
    block_trigger_inputs = ["last_image", "image"]
Aryan's avatar
Aryan committed
208
209
210

    @property
    def description(self):
YiYi Xu's avatar
YiYi Xu committed
211
212
213
214
215
216
217
218
        return (
            "Vae Image Encoder step that encode the image to generate the image latents"
            + "This is an auto pipeline block that works for image2video tasks."
            + " - `WanFLF2VVaeImageEncoderStep` (flf2v) is used when `last_image` is provided."
            + " - `WanImage2VideoVaeImageEncoderStep` (image2video) is used when `image` is provided."
            + " - if `last_image` or `image` is not provided, step will be skipped."
        )

Aryan's avatar
Aryan committed
219

YiYi Xu's avatar
YiYi Xu committed
220
221
222
223
224
225
226
227
228
## denoise
class WanAutoDenoiseStep(AutoPipelineBlocks):
    block_classes = [
        WanFLF2VCoreDenoiseStep,
        WanImage2VideoCoreDenoiseStep,
        WanCoreDenoiseStep,
    ]
    block_names = ["flf2v", "image2video", "text2video"]
    block_trigger_inputs = ["first_last_frame_latents", "first_frame_latents", None]
Aryan's avatar
Aryan committed
229

YiYi Xu's avatar
YiYi Xu committed
230
231
232
233
234
235
236
237
238
239
240
241
242
    @property
    def description(self) -> str:
        return (
            "Denoise step that iteratively denoise the latents. "
            "This is a auto pipeline block that works for text2video and image2video tasks."
            " - `WanCoreDenoiseStep` (text2video) for text2vid tasks."
            " - `WanCoreImage2VideoCoreDenoiseStep` (image2video) for image2video tasks."
            + " - if `first_frame_latents` is provided, `WanCoreImage2VideoDenoiseStep` will be used.\n"
            + " - if `first_frame_latents` is not provided, `WanCoreDenoiseStep` will be used.\n"
        )


# auto pipeline blocks
Aryan's avatar
Aryan committed
243
244
245
class WanAutoBlocks(SequentialPipelineBlocks):
    block_classes = [
        WanTextEncoderStep,
YiYi Xu's avatar
YiYi Xu committed
246
247
        WanAutoImageEncoderStep,
        WanAutoVaeImageEncoderStep,
Aryan's avatar
Aryan committed
248
        WanAutoDenoiseStep,
YiYi Xu's avatar
YiYi Xu committed
249
        WanImageVaeDecoderStep,
Aryan's avatar
Aryan committed
250
251
252
    ]
    block_names = [
        "text_encoder",
YiYi Xu's avatar
YiYi Xu committed
253
254
        "image_encoder",
        "vae_image_encoder",
Aryan's avatar
Aryan committed
255
        "denoise",
YiYi Xu's avatar
YiYi Xu committed
256
        "decode",
Aryan's avatar
Aryan committed
257
258
259
260
261
262
263
264
265
266
    ]

    @property
    def description(self):
        return (
            "Auto Modular pipeline for text-to-video using Wan.\n"
            + "- for text-to-video generation, all you need to provide is `prompt`"
        )


YiYi Xu's avatar
YiYi Xu committed
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
# wan22
# wan2.2: text2vid


## denoise
class Wan22CoreDenoiseStep(SequentialPipelineBlocks):
    block_classes = [
        WanTextInputStep,
        WanSetTimestepsStep,
        WanPrepareLatentsStep,
        Wan22DenoiseStep,
    ]
    block_names = ["input", "set_timesteps", "prepare_latents", "denoise"]

    @property
    def description(self):
        return (
            "denoise block that takes encoded conditions and runs the denoising process.\n"
            + "This is a sequential pipeline blocks:\n"
            + " - `WanTextInputStep` is used to adjust the batch size of the model inputs\n"
            + " - `WanSetTimestepsStep` is used to set the timesteps\n"
            + " - `WanPrepareLatentsStep` is used to prepare the latents\n"
            + " - `Wan22DenoiseStep` is used to denoise the latents in wan2.2\n"
        )


# wan2.2: image2video
## denoise
class Wan22Image2VideoCoreDenoiseStep(SequentialPipelineBlocks):
    block_classes = [
        WanTextInputStep,
        WanAdditionalInputsStep(image_latent_inputs=["first_frame_latents"]),
        WanSetTimestepsStep,
        WanPrepareLatentsStep,
        WanPrepareFirstFrameLatentsStep,
        Wan22Image2VideoDenoiseStep,
    ]
    block_names = [
        "input",
        "additional_inputs",
        "set_timesteps",
        "prepare_latents",
        "prepare_first_frame_latents",
        "denoise",
    ]

    @property
    def description(self):
        return (
            "denoise block that takes encoded text and image latent conditions and runs the denoising process.\n"
            + "This is a sequential pipeline blocks:\n"
            + " - `WanTextInputStep` is used to adjust the batch size of the model inputs\n"
            + " - `WanAdditionalInputsStep` is used to adjust the batch size of the latent conditions\n"
            + " - `WanSetTimestepsStep` is used to set the timesteps\n"
            + " - `WanPrepareLatentsStep` is used to prepare the latents\n"
            + " - `WanPrepareFirstFrameLatentsStep` is used to prepare the first frame latent conditions\n"
            + " - `Wan22Image2VideoDenoiseStep` is used to denoise the latents in wan2.2\n"
        )


class Wan22AutoDenoiseStep(AutoPipelineBlocks):
    block_classes = [
        Wan22Image2VideoCoreDenoiseStep,
        Wan22CoreDenoiseStep,
    ]
    block_names = ["image2video", "text2video"]
    block_trigger_inputs = ["first_frame_latents", None]

    @property
    def description(self) -> str:
        return (
            "Denoise step that iteratively denoise the latents. "
            "This is a auto pipeline block that works for text2video and image2video tasks."
            " - `Wan22Image2VideoCoreDenoiseStep` (image2video) for image2video tasks."
            " - `Wan22CoreDenoiseStep` (text2video) for text2vid tasks."
            + " - if `first_frame_latents` is provided, `Wan22Image2VideoCoreDenoiseStep` will be used.\n"
            + " - if `first_frame_latents` is not provided, `Wan22CoreDenoiseStep` will be used.\n"
        )


class Wan22AutoBlocks(SequentialPipelineBlocks):
    block_classes = [
        WanTextEncoderStep,
        WanAutoVaeImageEncoderStep,
        Wan22AutoDenoiseStep,
        WanImageVaeDecoderStep,
    ]
    block_names = [
        "text_encoder",
        "vae_image_encoder",
        "denoise",
        "decode",
    ]

    @property
    def description(self):
        return (
            "Auto Modular pipeline for text-to-video using Wan2.2.\n"
            + "- for text-to-video generation, all you need to provide is `prompt`"
        )


# presets for wan2.1 and wan2.2
# YiYi Notes: should we move these to doc?
# wan2.1
Aryan's avatar
Aryan committed
372
373
374
TEXT2VIDEO_BLOCKS = InsertableDict(
    [
        ("text_encoder", WanTextEncoderStep),
YiYi Xu's avatar
YiYi Xu committed
375
        ("input", WanTextInputStep),
Aryan's avatar
Aryan committed
376
377
378
        ("set_timesteps", WanSetTimestepsStep),
        ("prepare_latents", WanPrepareLatentsStep),
        ("denoise", WanDenoiseStep),
YiYi Xu's avatar
YiYi Xu committed
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
        ("decode", WanImageVaeDecoderStep),
    ]
)

IMAGE2VIDEO_BLOCKS = InsertableDict(
    [
        ("image_resize", WanImageResizeStep),
        ("image_encoder", WanImage2VideoImageEncoderStep),
        ("vae_image_encoder", WanImage2VideoVaeImageEncoderStep),
        ("input", WanTextInputStep),
        ("additional_inputs", WanAdditionalInputsStep(image_latent_inputs=["first_frame_latents"])),
        ("set_timesteps", WanSetTimestepsStep),
        ("prepare_latents", WanPrepareLatentsStep),
        ("prepare_first_frame_latents", WanPrepareFirstFrameLatentsStep),
        ("denoise", WanImage2VideoDenoiseStep),
        ("decode", WanImageVaeDecoderStep),
Aryan's avatar
Aryan committed
395
396
397
398
    ]
)


YiYi Xu's avatar
YiYi Xu committed
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
FLF2V_BLOCKS = InsertableDict(
    [
        ("image_resize", WanImageResizeStep),
        ("last_image_resize", WanImageCropResizeStep),
        ("image_encoder", WanFLF2VImageEncoderStep),
        ("vae_image_encoder", WanFLF2VVaeImageEncoderStep),
        ("input", WanTextInputStep),
        ("additional_inputs", WanAdditionalInputsStep(image_latent_inputs=["first_last_frame_latents"])),
        ("set_timesteps", WanSetTimestepsStep),
        ("prepare_latents", WanPrepareLatentsStep),
        ("prepare_first_last_frame_latents", WanPrepareFirstLastFrameLatentsStep),
        ("denoise", WanFLF2VDenoiseStep),
        ("decode", WanImageVaeDecoderStep),
    ]
)

Aryan's avatar
Aryan committed
415
416
417
AUTO_BLOCKS = InsertableDict(
    [
        ("text_encoder", WanTextEncoderStep),
YiYi Xu's avatar
YiYi Xu committed
418
419
        ("image_encoder", WanAutoImageEncoderStep),
        ("vae_image_encoder", WanAutoVaeImageEncoderStep),
Aryan's avatar
Aryan committed
420
        ("denoise", WanAutoDenoiseStep),
YiYi Xu's avatar
YiYi Xu committed
421
        ("decode", WanImageVaeDecoderStep),
Aryan's avatar
Aryan committed
422
423
424
    ]
)

YiYi Xu's avatar
YiYi Xu committed
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
# wan2.2 presets

TEXT2VIDEO_BLOCKS_WAN22 = InsertableDict(
    [
        ("text_encoder", WanTextEncoderStep),
        ("input", WanTextInputStep),
        ("set_timesteps", WanSetTimestepsStep),
        ("prepare_latents", WanPrepareLatentsStep),
        ("denoise", Wan22DenoiseStep),
        ("decode", WanImageVaeDecoderStep),
    ]
)

IMAGE2VIDEO_BLOCKS_WAN22 = InsertableDict(
    [
        ("image_resize", WanImageResizeStep),
        ("vae_image_encoder", WanImage2VideoVaeImageEncoderStep),
        ("input", WanTextInputStep),
        ("set_timesteps", WanSetTimestepsStep),
        ("prepare_latents", WanPrepareLatentsStep),
        ("denoise", Wan22DenoiseStep),
        ("decode", WanImageVaeDecoderStep),
    ]
)

AUTO_BLOCKS_WAN22 = InsertableDict(
    [
        ("text_encoder", WanTextEncoderStep),
        ("vae_image_encoder", WanAutoVaeImageEncoderStep),
        ("denoise", Wan22AutoDenoiseStep),
        ("decode", WanImageVaeDecoderStep),
    ]
)

# presets all blocks (wan and wan22)

Aryan's avatar
Aryan committed
461
462

ALL_BLOCKS = {
YiYi Xu's avatar
YiYi Xu committed
463
464
465
466
467
468
469
470
471
472
473
    "wan2.1": {
        "text2video": TEXT2VIDEO_BLOCKS,
        "image2video": IMAGE2VIDEO_BLOCKS,
        "flf2v": FLF2V_BLOCKS,
        "auto": AUTO_BLOCKS,
    },
    "wan2.2": {
        "text2video": TEXT2VIDEO_BLOCKS_WAN22,
        "image2video": IMAGE2VIDEO_BLOCKS_WAN22,
        "auto": AUTO_BLOCKS_WAN22,
    },
Aryan's avatar
Aryan committed
474
}