-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Expand file tree
/
Copy pathrun_demo_avatar_single_audio_to_video.py
More file actions
441 lines (386 loc) · 17 KB
/
Copy pathrun_demo_avatar_single_audio_to_video.py
File metadata and controls
441 lines (386 loc) · 17 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
import os
import json
import time
import math
import random
import argparse
import datetime
import PIL.Image
import numpy as np
from pathlib import Path
import torch
import torch.distributed as dist
from transformers import AutoTokenizer, UMT5EncoderModel
from diffusers.utils import load_image
from longcat_video.pipeline_longcat_video_avatar import LongCatVideoAvatarPipeline
from longcat_video.modules.scheduling_flow_match_euler_discrete import FlowMatchEulerDiscreteScheduler
from longcat_video.modules.autoencoder_kl_wan import AutoencoderKLWan
from longcat_video.modules.avatar.longcat_video_dit_avatar import LongCatVideoAvatarTransformer3DModel
from longcat_video.modules.quantization import load_quantized_dit
from longcat_video.context_parallel import context_parallel_util
# -------- avatar related --------
import librosa
from longcat_video.audio_process import get_audio_encoder, get_audio_feature_extractor
from longcat_video.audio_process.torch_utils import save_video_ffmpeg
from audio_separator.separator import Separator
def torch_gc():
torch.cuda.empty_cache()
torch.cuda.ipc_collect()
def generate_random_uid():
timestamp_part = str(int(time.time()))[-6:]
random_part = str(random.randint(100000, 999999))
uid = timestamp_part + random_part
return uid
def extract_vocal_from_speech(source_path, target_path, vocal_separator, audio_output_dir_temp):
outputs = vocal_separator.separate(source_path)
if len(outputs) <= 0:
print("Audio separate failed. Using raw audio.")
return None
default_vocal_path = audio_output_dir_temp / "vocals" / outputs[0]
default_vocal_path = default_vocal_path.resolve().as_posix()
cmd = f"mv '{default_vocal_path}' '{target_path}'"
os.system(cmd)
return target_path
def generate(args):
# load parsed args
input_json = args.input_json
checkpoint_dir = args.checkpoint_dir
context_parallel_size = args.context_parallel_size
stage_1 = args.stage_1
num_inference_steps = args.num_inference_steps
text_guidance_scale = args.text_guidance_scale
audio_guidance_scale = args.audio_guidance_scale
resolution = args.resolution
num_segments = max(1, args.num_segments)
output_dir = args.output_dir
model_type = args.model_type
use_distill = args.use_distill
use_int8 = args.use_int8
if use_distill and model_type == "avatar-v1.5":
num_inference_steps = 8
text_guidance_scale = 1.0
audio_guidance_scale = 1.0
# set up default inference params
save_fps = 16
audio_stride = 2
if model_type == "avatar-v1.5":
save_fps = 25
audio_stride = 1
num_frames = 93
num_cond_frames = 13
if resolution == '480p':
height, width = 480, 832
elif resolution == '720p':
height, width = 768, 1280
# case setup
with open(input_json, 'r', encoding='utf-8') as f:
input_data = json.load(f)
prompt = input_data['prompt']
negative_prompt = "Close-up, Bright tones, overexposed, static, blurred details, subtitles, style, works, paintings, images, static, overall gray, worst quality, low quality, JPEG compression residue, ugly, incomplete, extra fingers, poorly drawn hands, poorly drawn faces, deformed, disfigured, misshapen limbs, fused fingers, still picture, messy background, three legs, many people in the background, walking backwards"
raw_speech_path = input_data['cond_audio']['person1']
# prepare distributed environment
rank = int(os.environ['RANK'])
num_gpus = torch.cuda.device_count()
local_rank = rank % num_gpus
torch.cuda.set_device(local_rank)
dist.init_process_group(backend="nccl", timeout=datetime.timedelta(seconds=3600*24))
global_rank = dist.get_rank()
num_processes = dist.get_world_size()
# initialize context parallel
context_parallel_util.init_context_parallel(context_parallel_size=context_parallel_size, global_rank=global_rank, world_size=num_processes)
cp_rank = context_parallel_util.get_cp_rank()
cp_size = context_parallel_util.get_cp_size()
cp_split_hw = context_parallel_util.get_optimal_split(cp_size)
# initialize models
tokenizer = AutoTokenizer.from_pretrained(os.path.join(checkpoint_dir, '..', 'LongCat-Video'), subfolder="tokenizer", torch_dtype=torch.bfloat16)
text_encoder = UMT5EncoderModel.from_pretrained(os.path.join(checkpoint_dir, '..', 'LongCat-Video'), subfolder="text_encoder", torch_dtype=torch.bfloat16)
vae = AutoencoderKLWan.from_pretrained(os.path.join(checkpoint_dir, '..', 'LongCat-Video'), subfolder="vae", torch_dtype=torch.bfloat16)
if model_type == "avatar-v1.0":
scheduler = FlowMatchEulerDiscreteScheduler.from_pretrained(os.path.join(checkpoint_dir, '..', 'LongCat-Video'), subfolder="scheduler", torch_dtype=torch.bfloat16)
elif model_type == "avatar-v1.5":
scheduler = FlowMatchEulerDiscreteScheduler.from_pretrained(checkpoint_dir, subfolder="scheduler", torch_dtype=torch.bfloat16)
else:
raise ValueError(f"Unsupported model_type: {model_type}. Expected 'avatar-v1.0' or 'avatar-v1.5'.")
if model_type == "avatar-v1.0":
dit = LongCatVideoAvatarTransformer3DModel.from_pretrained(checkpoint_dir, subfolder="avatar_single", cp_split_hw=cp_split_hw, torch_dtype=torch.bfloat16)
elif model_type == "avatar-v1.5":
if use_int8:
print("[INFO] Loading INT8 quantized DiT model...")
dit = load_quantized_dit(checkpoint_dir, subfolder="base_model_int8", cp_split_hw=cp_split_hw)
else:
dit = LongCatVideoAvatarTransformer3DModel.from_pretrained(checkpoint_dir, subfolder="base_model", cp_split_hw=cp_split_hw, torch_dtype=torch.bfloat16)
if use_distill:
distill_checkpoint_path = os.path.join(checkpoint_dir, 'lora', f'dmd_lora.safetensors')
if os.path.exists(distill_checkpoint_path):
dit.load_lora(distill_checkpoint_path, "dmd", multiplier=1.0, lora_network_dim=128, lora_network_alpha=64)
dit.enable_loras(["dmd"])
else:
raise ValueError(f"Unsupported model_type: {model_type}. Expected 'avatar-v1.0' or 'avatar-v1.5'.")
# initialize audio models
if model_type == "avatar-v1.0":
audio_model_checkpoint_path = os.path.join(checkpoint_dir, 'chinese-wav2vec2-base')
elif model_type == "avatar-v1.5":
audio_model_checkpoint_path = os.path.join(checkpoint_dir, 'whisper-large-v3')
audio_encoder = get_audio_encoder(audio_model_checkpoint_path, model_type).to(local_rank)
audio_feature_extractor = get_audio_feature_extractor(audio_model_checkpoint_path, model_type)
vocal_separator_path = os.path.join(checkpoint_dir, 'vocal_separator/Kim_Vocal_2.onnx')
audio_output_dir_temp = f"./audio_temp_file"
os.makedirs(audio_output_dir_temp, exist_ok=True)
audio_output_dir_temp = Path(audio_output_dir_temp)
audio_separator_model_path = os.path.dirname(vocal_separator_path)
audio_separator_model_name = os.path.basename(vocal_separator_path)
vocal_separator = Separator(
output_dir=audio_output_dir_temp / "vocals",
output_single_stem="vocals",
model_file_dir=audio_separator_model_path,
)
vocal_separator.load_model(audio_separator_model_name)
# initialize pipeline
pipe = LongCatVideoAvatarPipeline(
tokenizer = tokenizer,
text_encoder = text_encoder,
vae = vae,
scheduler = scheduler,
dit = dit,
audio_encoder=audio_encoder,
audio_feature_extractor=audio_feature_extractor,
model_type=model_type
)
pipe.to(local_rank)
global_seed = 42
seed = global_seed + global_rank
generator = torch.Generator(device=local_rank)
generator.manual_seed(seed)
if cp_rank == 0:
# extract vocal
temp_vocal_path = extract_vocal_from_speech(raw_speech_path, f"/tmp/temp_speech_{generate_random_uid()}_{global_rank}_vocal.wav", vocal_separator, audio_output_dir_temp)
assert temp_vocal_path is not None and os.path.exists(temp_vocal_path), f"No vocal detected"
# audio padding to target length
generate_duration = num_frames / save_fps + (num_segments-1)*(num_frames-num_cond_frames) / save_fps
speech_array, sr = librosa.load(temp_vocal_path, sr=16000)
source_duraion = len(speech_array) / sr
added_sample_nums = math.ceil((generate_duration - source_duraion) * sr)
if added_sample_nums > 0:
speech_array = np.append(speech_array, [0.]*added_sample_nums)
# audio embedding
full_audio_emb = pipe.get_audio_embedding(speech_array, fps=save_fps*audio_stride, device=local_rank, sample_rate=sr, model_type=model_type)
if torch.isnan(full_audio_emb).any():
raise ValueError(f"broken audio embedding with nan values")
if context_parallel_util.get_cp_size() > 1:
full_audio_emb_shape_list = list(full_audio_emb.size())
full_audio_emb_tensor_shape_list = torch.tensor(full_audio_emb_shape_list, dtype=torch.int64, device=full_audio_emb.device)
context_parallel_util.cp_broadcast(full_audio_emb_tensor_shape_list)
context_parallel_util.cp_broadcast(full_audio_emb)
if os.path.exists(temp_vocal_path):
os.remove(temp_vocal_path)
elif context_parallel_util.get_cp_size() > 1:
full_audio_emb_tensor_shape_list = torch.zeros(3, dtype=torch.int64, device=local_rank)
context_parallel_util.cp_broadcast(full_audio_emb_tensor_shape_list)
full_audio_emb_shape_list = full_audio_emb_tensor_shape_list.tolist()
full_audio_emb = torch.zeros(*full_audio_emb_shape_list, dtype=torch.float32, device=local_rank)
context_parallel_util.cp_broadcast(full_audio_emb)
# prepare audio embedding for the first clip
indices = torch.arange(2 * 2 + 1) - 2
audio_start_idx = 0
audio_end_idx = audio_start_idx + audio_stride * num_frames
center_indices = torch.arange(audio_start_idx, audio_end_idx, audio_stride).unsqueeze(1) + indices.unsqueeze(0)
center_indices = torch.clamp(center_indices, min=0, max=full_audio_emb.shape[0]-1)
audio_emb = full_audio_emb[center_indices][None,...].to(local_rank)
if local_rank == 0:
print(f"Generating segment 1/{num_segments}...")
if stage_1 == 'at2v':
# ==============================
# at2v (480P)
# ==============================
output_tuple = pipe.generate_at2v(
prompt=prompt,
negative_prompt=negative_prompt,
height=height,
width=width,
num_frames=num_frames,
num_inference_steps=num_inference_steps,
text_guidance_scale=text_guidance_scale,
audio_guidance_scale=audio_guidance_scale,
generator=generator,
output_type='both',
audio_emb=audio_emb,
use_distill=use_distill,
)
output, latent = output_tuple
output = output[0]
video = [(output[i] * 255).astype(np.uint8) for i in range(output.shape[0])]
video = [PIL.Image.fromarray(img) for img in video]
if cp_rank == 0:
output_tensor = torch.from_numpy(np.array(video))
save_video_ffmpeg(output_tensor, os.path.join(output_dir, "at2v_demo_1"), raw_speech_path, fps=save_fps, quality=5)
del output
torch_gc()
elif stage_1 == 'ai2v':
# ==============================
# ai2v (480P)
# ==============================
image_path = input_data['cond_image']
image = load_image(image_path)
output_tuple = pipe.generate_ai2v(
image=image,
prompt=prompt,
negative_prompt=negative_prompt,
resolution=resolution,
num_frames=num_frames,
num_inference_steps=num_inference_steps,
text_guidance_scale=text_guidance_scale,
audio_guidance_scale=audio_guidance_scale,
output_type='both',
generator=generator,
audio_emb=audio_emb,
use_distill=use_distill,
)
output, latent = output_tuple
output = output[0]
video = [(output[i] * 255).astype(np.uint8) for i in range(output.shape[0])]
video = [PIL.Image.fromarray(img) for img in video]
if cp_rank == 0:
output_tensor = torch.from_numpy(np.array(video))
save_video_ffmpeg(output_tensor, os.path.join(output_dir, "ai2v_demo_1"), raw_speech_path, fps=save_fps, quality=5)
del output
torch_gc()
else:
raise NotImplementedError(f"Not supported type of stage_1: {stage_1}")
if context_parallel_util.get_cp_size() > 1:
torch.distributed.barrier(group=context_parallel_util.get_cp_group())
# =========================================
# long video generation (480P)
# =========================================
# load parsed long video args
ref_img_index = args.ref_img_index
mask_frame_range = args.mask_frame_range
width, height = video[0].size
current_video = video
ref_latent = latent[:, :, :1].clone()
all_generated_frames = video
for segment_idx in range(1, num_segments):
if local_rank == 0:
print(f"Generating segment {segment_idx+1}/{num_segments}...")
# prepare audio embedding for the next clip
audio_start_idx = audio_start_idx + audio_stride * (num_frames - num_cond_frames)
audio_end_idx = audio_start_idx + audio_stride * num_frames
center_indices = torch.arange(audio_start_idx, audio_end_idx, audio_stride).unsqueeze(1) + indices.unsqueeze(0)
center_indices = torch.clamp(center_indices, min=0, max=full_audio_emb.shape[0]-1)
audio_emb = full_audio_emb[center_indices][None,...].to(local_rank)
output_tuple = pipe.generate_avc(
video=current_video,
video_latent=latent,
prompt=prompt,
negative_prompt=negative_prompt,
height=height,
width=width,
num_frames=num_frames,
num_cond_frames=num_cond_frames,
num_inference_steps=num_inference_steps,
text_guidance_scale=text_guidance_scale,
audio_guidance_scale=audio_guidance_scale,
generator=generator,
output_type='both',
use_kv_cache=True,
offload_kv_cache=False,
enhance_hf=True if not use_distill else False,
audio_emb=audio_emb,
ref_latent=ref_latent,
ref_img_index=ref_img_index,
mask_frame_range=mask_frame_range,
use_distill=use_distill,
)
output, latent = output_tuple
output = output[0]
new_video = [(output[i] * 255).astype(np.uint8) for i in range(output.shape[0])]
new_video = [PIL.Image.fromarray(img) for img in new_video]
del output
all_generated_frames.extend(new_video[num_cond_frames:])
current_video = new_video
if cp_rank == 0:
output_tensor = torch.from_numpy(np.array(all_generated_frames))
save_video_ffmpeg(output_tensor, os.path.join(output_dir, f"video_continue_{segment_idx+1}"), raw_speech_path, fps=save_fps, quality=5)
del output_tensor
def _parse_args():
parser = argparse.ArgumentParser()
parser.add_argument(
'--input_json',
type=str,
default='assets/avatar/single_example_1.json'
)
parser.add_argument(
'--output_dir',
type=str,
default='./outputs_avatar_single'
)
parser.add_argument(
'--resolution',
type=str,
default='480p',
choices=['480p', '720p']
)
parser.add_argument(
'--num_segments',
type=int,
default=1
)
parser.add_argument(
'--num_inference_steps',
type=int,
default=50
)
parser.add_argument(
'--ref_img_index',
type=int,
default=10
)
parser.add_argument(
'--mask_frame_range',
type=int,
default=3
)
parser.add_argument(
'--text_guidance_scale',
type=float,
default=4.0
)
parser.add_argument(
'--audio_guidance_scale',
type=float,
default=4.0
)
parser.add_argument(
'--stage_1',
type=str,
default='ai2v',
choices=['ai2v', 'at2v']
)
parser.add_argument(
"--context_parallel_size",
type=int,
default=1,
)
parser.add_argument(
"--checkpoint_dir",
type=str,
default="./weights/LongCat-Video-Avatar",
)
parser.add_argument(
"--model_type",
type=str,
default="avatar-v1.0",
)
parser.add_argument(
"--use_distill",
action='store_true',
)
parser.add_argument(
"--use_int8",
action='store_true',
help="Load INT8 quantized DiT model for reduced VRAM usage"
)
args = parser.parse_args()
return args
if __name__ == "__main__":
args = _parse_args()
generate(args)