-
Notifications
You must be signed in to change notification settings - Fork 225
Expand file tree
/
Copy pathgenerate.py
More file actions
468 lines (413 loc) · 18.6 KB
/
generate.py
File metadata and controls
468 lines (413 loc) · 18.6 KB
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
# Licensed under the TENCENT HUNYUAN COMMUNITY LICENSE AGREEMENT (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https://github.com/Tencent-Hunyuan/HunyuanVideo-1.5/blob/main/LICENSE
#
# Unless and only to the extent required by applicable law, the Tencent Hunyuan works and any
# output and results therefrom are provided "AS IS" without any express or implied warranties of
# any kind including any warranties of title, merchantability, noninfringement, course of dealing,
# usage of trade, or fitness for a particular purpose. You are solely responsible for determining the
# appropriateness of using, reproducing, modifying, performing, displaying or distributing any of
# the Tencent Hunyuan works or outputs and assume any and all risks associated with your or a
# third party's use or distribution of any of the Tencent Hunyuan works or outputs and your exercise
# of rights and permissions under this agreement.
# See the License for the specific language governing permissions and limitations under the License.
import os
if 'PYTORCH_CUDA_ALLOC_CONF' not in os.environ:
os.environ['PYTORCH_CUDA_ALLOC_CONF'] = 'expandable_segments:True'
import copy
import datetime
import json
import loguru
import torch
import argparse
import einops
import imageio
from torch import distributed as dist
import torch.distributed.checkpoint as dcp
from torch.distributed.checkpoint.state_dict import get_model_state_dict
from hyvideo.pipelines.hunyuan_video_pipeline import HunyuanVideo_1_5_Pipeline
from hyvideo.commons.parallel_states import initialize_parallel_state
from hyvideo.commons.infer_state import initialize_infer_state
parallel_dims = initialize_parallel_state(sp=int(os.environ.get('WORLD_SIZE', '1')))
torch.cuda.set_device(int(os.environ.get('LOCAL_RANK', '0')))
def save_video(video, path):
if video.ndim == 5:
assert video.shape[0] == 1
video = video[0]
vid = (video * 255).clamp(0, 255).to(torch.uint8)
vid = einops.rearrange(vid, 'c f h w -> f h w c')
imageio.mimwrite(path, vid, fps=24)
def rank0_log(message, level):
if int(os.environ.get('RANK', '0')) == 0:
loguru.logger.log(level, message)
def save_config(args, output_path, task, transformer_version):
arguments = {}
for key, value in vars(args).items():
if not key.startswith('_') and not callable(value):
try:
json.dumps(value)
arguments[key] = value
except (TypeError, ValueError):
arguments[key] = str(value)
config = {
'timestamp': datetime.datetime.now().isoformat(),
'task': task,
'transformer_version': transformer_version,
'output_path': output_path,
'arguments': arguments
}
base_path, _ = os.path.splitext(output_path)
config_path = f"{base_path}_config.json"
with open(config_path, 'w', encoding='utf-8') as f:
json.dump(config, f, indent=2, ensure_ascii=False)
print(f"Saved generation config to: {config_path}")
return config_path
def str_to_bool(value):
"""Convert string to boolean, supporting true/false, 1/0, yes/no.
If value is None (when flag is provided without value), returns True."""
if value is None:
return True # When --flag is provided without value, enable it
if isinstance(value, bool):
return value
if isinstance(value, str):
value = value.lower().strip()
if value in ('true', '1', 'yes', 'on'):
return True
elif value in ('false', '0', 'no', 'off'):
return False
raise argparse.ArgumentTypeError(f"Boolean value expected, got: {value}")
def load_checkpoint_to_transformer(pipe, checkpoint_path):
if not os.path.exists(checkpoint_path):
raise ValueError(f"Checkpoint path does not exist: {checkpoint_path}")
rank0_log(f"Loading checkpoint from {checkpoint_path}", "INFO")
try:
model_state_dict = get_model_state_dict(pipe.transformer)
dcp.load(
state_dict={"model": model_state_dict},
checkpoint_id=checkpoint_path,
)
rank0_log("Transformer model state loaded successfully", "INFO")
except Exception as e:
rank0_log(f"Error loading checkpoint: {e}", "ERROR")
raise
def load_lora_adapter(pipe, lora_path):
rank0_log(f"Loading LoRA adapter from {lora_path}", "INFO")
try:
pipe.transformer.load_lora_adapter(
pretrained_model_name_or_path_or_dict=lora_path,
prefix=None,
adapter_name="default",
use_safetensors=True,
hotswap=False,
)
rank0_log("LoRA adapter loaded successfully", "INFO")
except Exception as e:
rank0_log(f"Error loading LoRA adapter: {e}", "ERROR")
raise
def generate_video(args):
infer_state = initialize_infer_state(args)
if args.sparse_attn and args.use_sageattn:
raise ValueError("sparse_attn and use_sageattn cannot be enabled simultaneously. Please enable only one of them.")
if args.use_fp8_gemm and 'sgl' in args.quant_type:
try:
import sgl_kernel
except Exception:
raise ValueError("sgl_kernel is not installed. Please install it using `pip install sgl-kernel==0.3.18`")
# if args.enable_torch_compile:
# torch._logging.set_logs(graph_code=True)
if args.enable_step_distill and args.enable_cache:
raise ValueError("Enabling both step distilled model and cache will lead to performance degradation.")
task = 'i2v' if args.image_path else 't2v'
enable_sr = args.sr
transformer_version = HunyuanVideo_1_5_Pipeline.get_transformer_version(args.resolution, task, args.cfg_distilled, args.enable_step_distill, args.sparse_attn)
if args.dtype == 'bf16':
transformer_dtype = torch.bfloat16
elif args.dtype == 'fp32':
transformer_dtype = torch.float32
else:
raise ValueError(f"Unsupported dtype: {args.dtype}. Must be 'bf16' or 'fp32'")
# Determine offloading settings
enable_offloading = args.offloading
if args.group_offloading is None:
# Auto-detect based on offloading config
offloading_config = HunyuanVideo_1_5_Pipeline.get_offloading_config()
enable_group_offloading = offloading_config['enable_group_offloading']
else:
enable_group_offloading = args.group_offloading
overlap_group_offloading = args.overlap_group_offloading
# Determine device and transformer_init_device based on offloading settings
if enable_offloading:
device = torch.device('cpu')
else:
device = torch.device('cuda')
if enable_group_offloading:
transformer_init_device = torch.device('cpu')
else:
transformer_init_device = device
pipe = HunyuanVideo_1_5_Pipeline.create_pipeline(
pretrained_model_name_or_path=args.model_path,
transformer_version=transformer_version,
create_sr_pipeline=enable_sr,
transformer_dtype=transformer_dtype,
device=device,
transformer_init_device=transformer_init_device,
)
loguru.logger.info(f"{enable_offloading=} {enable_group_offloading=} {overlap_group_offloading=}")
pipe.apply_infer_optimization(
infer_state=infer_state,
enable_offloading=enable_offloading,
enable_group_offloading=enable_group_offloading,
overlap_group_offloading=overlap_group_offloading,
)
# Load checkpoint if provided
if args.checkpoint_path:
load_checkpoint_to_transformer(pipe, args.checkpoint_path)
if args.lora_path:
load_lora_adapter(pipe, args.lora_path)
# Apply optimizations to SR pipeline if exists
if enable_sr and hasattr(pipe, 'sr_pipeline'):
sr_infer_state = copy.deepcopy(infer_state)
sr_infer_state.enable_cache = False # SR pipeline does not require cache optimization yet
pipe.sr_pipeline.apply_infer_optimization(
infer_state=sr_infer_state,
enable_offloading=enable_offloading,
enable_group_offloading=enable_group_offloading,
overlap_group_offloading=overlap_group_offloading,
)
extra_kwargs = {}
if task == 'i2v':
extra_kwargs['reference_image'] = args.image_path
if args.video_length != 121:
rank0_log(f"Warning: 121 frames is the optimal value for best quality. "
f"Attempting to generate {args.video_length} frames...", "WARNING")
enable_rewrite = args.rewrite
if not args.rewrite:
rank0_log("Warning: Prompt rewriting is disabled. This may affect the quality of generated videos.", "WARNING")
out = pipe(
enable_sr=enable_sr,
prompt=args.prompt,
aspect_ratio=args.aspect_ratio,
num_inference_steps=args.num_inference_steps,
sr_num_inference_steps=None,
video_length=args.video_length,
negative_prompt=args.negative_prompt,
seed=args.seed,
output_type="pt",
prompt_rewrite=enable_rewrite,
return_pre_sr_video=args.save_pre_sr_video,
**extra_kwargs,
)
if int(os.environ.get('RANK', '0')) == 0:
output_path = args.output_path
if output_path is None:
now = f'{datetime.datetime.now():%Y-%m-%d %H:%M:%S}'
output_path = f'./outputs/output_{transformer_version}_{now}.mp4'
output_dir = os.path.dirname(output_path)
if output_dir:
os.makedirs(output_dir, exist_ok=True)
original_path = None
if enable_sr and hasattr(out, 'sr_videos'):
save_video(out.sr_videos, output_path)
print(f"Saved SR video to: {output_path}")
if args.save_pre_sr_video:
base_path, ext = os.path.splitext(output_path)
original_path = f"{base_path}_before_sr{ext}"
save_video(out.videos, original_path)
print(f"Saved original video (before SR) to: {original_path}")
else:
save_video(out.videos, output_path)
print(f"Saved video to: {output_path}")
if args.save_generation_config:
try:
save_config(args, output_path, task, transformer_version)
except Exception:
pass
def main():
parser = argparse.ArgumentParser(description='Generate video using HunyuanVideo-1.5')
parser.add_argument(
'--prompt', type=str, required=True,
help='Text prompt for video generation'
)
parser.add_argument(
'--negative_prompt', type=str, default='',
help='Negative prompt for video generation (default: empty string)'
)
parser.add_argument(
'--resolution', type=str, required=True, choices=['480p', '720p'],
help='Video resolution (480p or 720p)'
)
parser.add_argument(
'--model_path', type=str, required=True,
help='Path to pretrained model'
)
parser.add_argument(
'--aspect_ratio', type=str, default='16:9',
help='Aspect ratio (default: 16:9)'
)
parser.add_argument(
'--num_inference_steps', type=int, default=None,
help='Number of inference steps (default: 50)'
)
parser.add_argument(
'--video_length', type=int, default=121,
help='Number of frames to generate (default: 121)'
)
parser.add_argument(
'--sr', type=str_to_bool, nargs='?', const=True, default=True,
help='Enable super resolution (default: true). '
'Use --sr or --sr true/1 to enable, --sr false/0 to disable'
)
parser.add_argument(
'--save_pre_sr_video', type=str_to_bool, nargs='?', const=True, default=False,
help='Save original video before super resolution (default: false). '
'Use --save_pre_sr_video or --save_pre_sr_video true/1 to enable, '
'--save_pre_sr_video false/0 to disable'
)
parser.add_argument(
'--rewrite', type=str_to_bool, nargs='?', const=True, default=False,
help='Enable prompt rewriting (default: true). '
'Use --rewrite or --rewrite true/1 to enable, --rewrite false/0 to disable'
)
parser.add_argument(
'--cfg_distilled', type=str_to_bool, nargs='?', const=True, default=False,
help='Enable CFG distilled model (default: false). '
'Use --cfg_distilled or --cfg_distilled true/1 to enable, '
'--cfg_distilled false/0 to disable'
)
parser.add_argument(
'--enable_step_distill', type=str_to_bool, nargs='?', const=True, default=False,
help='Enable step distilled model (default: false). '
'Use --enable_step_distill or --enable_step_distill true/1 to enable, '
'--enable_step_distill false/0 to disable'
)
parser.add_argument(
'--sparse_attn', type=str_to_bool, nargs='?', const=True, default=False,
help='Enable sparse attention (default: false). '
'Use --sparse_attn or --sparse_attn true/1 to enable, '
'--sparse_attn false/0 to disable'
)
parser.add_argument(
'--offloading', type=str_to_bool, nargs='?', const=True, default=True,
help='Enable offloading (default: true). '
'Use --offloading or --offloading true/1 to enable, '
'--offloading false/0 to disable'
)
parser.add_argument(
'--group_offloading', type=str_to_bool, nargs='?', const=True, default=None,
help='Enable group offloading (default: None, automatically enabled if offloading is enabled). '
'Use --group_offloading or --group_offloading true/1 to enable, '
'--group_offloading false/0 to disable'
)
parser.add_argument(
'--overlap_group_offloading', type=str_to_bool, nargs='?', const=True, default=True,
help='Enable overlap group offloading (default: true). '
'Significantly increases CPU memory usage but speeds up inference. '
'Use --overlap_group_offloading or --overlap_group_offloading true/1 to enable, '
'--overlap_group_offloading false/0 to disable'
)
parser.add_argument(
'--dtype', type=str, default='bf16', choices=['bf16', 'fp32'],
help='Data type for transformer (default: bf16). '
'bf16: faster, lower memory; fp32: better quality, slower, higher memory'
)
parser.add_argument(
'--seed', type=int, default=123,
help='Random seed (default: 123)'
)
parser.add_argument(
'--image_path', type=str, default=None,
help='Path to reference image for i2v (if provided, uses i2v mode)'
)
parser.add_argument(
'--output_path', type=str, default=None,
help='Output file path for generated video (if not provided, saves to ./outputs/output.mp4)'
)
parser.add_argument(
'--use_sageattn', type=str_to_bool, nargs='?', const=True, default=False,
help='Enable sageattn (default: false). '
'Use --use_sageattn or --use_sageattn true/1 to enable, '
'--use_sageattn false/0 to disable'
)
parser.add_argument(
'--sage_blocks_range', type=str, default="0-53",
help='Sageattn blocks range (e.g., 0-5 or 0,1,2,3,4,5)'
)
parser.add_argument(
'--enable_torch_compile', type=str_to_bool, nargs='?', const=True, default=False,
help='Enable torch compile for transformer (default: false). '
'Use --enable_torch_compile or --enable_torch_compile true/1 to enable, '
'--enable_torch_compile false/0 to disable'
)
parser.add_argument(
'--enable_cache', type=str_to_bool, nargs='?', const=True, default=False,
help='Enable cache for transformer (default: false). '
'Use --enable_cache or --enable_cache true/1 to enable, '
'--enable_cache false/0 to disable'
)
parser.add_argument(
'--cache_type', type=str, default="deepcache",
help='Cache type for transformer (e.g., deepcache, teacache, taylorcache)'
)
parser.add_argument(
'--no_cache_block_id', type=str, default="53",
help='Blocks to exclude from deepcache (e.g., 0-5 or 0,1,2,3,4,5)'
)
parser.add_argument(
'--cache_start_step', type=int, default=11,
help='Start step to skip when using cache (default: 11)'
)
parser.add_argument(
'--cache_end_step', type=int, default=45,
help='End step to skip when using cache (default: 45)'
)
parser.add_argument(
'--total_steps', type=int, default=50,
help='Total inference steps (default: 50)'
)
parser.add_argument(
'--cache_step_interval', type=int, default=4,
help='Step interval to skip when using cache (default: 4)'
)
parser.add_argument(
'--save_generation_config', type=str_to_bool, nargs='?', const=True, default=True,
help='Save generation config file (default: true). '
'Use --save_generation_config or --save_generation_config true/1 to enable, '
'--save_generation_config false/0 to disable'
)
parser.add_argument(
'--checkpoint_path', type=str, default=None,
help='Path to checkpoint directory containing transformer weights (e.g., ./outputs/checkpoint-1000/transformer). '
'The checkpoint directory should contain a "transformer" subdirectory. '
'If provided, the transformer model weights will be loaded from this checkpoint.'
)
parser.add_argument(
'--lora_path', type=str, default=None,
help='Path to LoRA adapter directory or checkpoint directory containing LoRA adapter. '
'If provided, the LoRA adapter will be loaded to the transformer model.'
)
# fp8 gemm related
parser.add_argument(
'--use_fp8_gemm', type=str_to_bool, nargs='?', const=True, default=False,
help='Enable fp8 gemm for transformer (default: false). '
'Use --use_fp8_gemm or --use_fp8_gemm true/1 to enable, '
'--use_fp8_gemm false/0 to disable'
)
parser.add_argument(
'--quant_type', type=str, default="fp8-per-token-sgl",
help='Quantization type for fp8 gemm (e.g., fp8-per-tensor-weight-only, fp8-per-tensor, fp8-per-token-sgl)'
)
parser.add_argument(
'--include_patterns', type=str, default="double_blocks",
help='Include patterns for fp8 gemm (default: double_blocks)'
)
args = parser.parse_args()
# Convert string "none" to None for image_path
if args.image_path is not None and args.image_path.lower().strip() == 'none':
args.image_path = None
generate_video(args)
if dist.is_initialized():
dist.destroy_process_group()
if __name__ == '__main__':
main()