stable-diffusion.cpp/docs/dgl/wan/wan2-2-s2v-14b.dgl

70 lines
4 KiB
Text
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# version: 0.0.0
# docs/models/wan.md — Wan2.2 S2V 14B
var {
model_path = "../models/diffusion_models/wan2.2_s2v-14B-Q8_0.gguf"
text_path = "../models/text_encoders/umt5-xxl-encoder-Q8_0.gguf"
vae_path = "../models/vae/wan_2.1_vae.safetensors"
audio_encoder_path = "../models/text_encoders/audio_encoders/wav2vec2_large_english_fp16.safetensors"
first_frame_path = "../inputs/cat_with_sd_cpp_42.png"
driving_audio_path = "./input/speech.wav"
output_path = "output.avi"
prompt = "a person is talking"
negative_prompt = "色调艳丽,过曝,静态,细节模糊不清,字幕,风格,作品,画作,画面,静止,整体发灰,最差质量,低质量,JPEG压缩残留,丑陋的,残缺的,多余的手指,画得不好的手部,画得不好的脸部,畸形的,毁容的,形态畸形的肢体,手指融合,静止不动的画面,杂乱的背景,三条腿,背景人很多,倒着走"
seed = 42
rng_type = "cuda"
width = 832
height = 480
frames = 81
fps = 16
steps = 20
cfg_scale = 6.0
method = "euler"
scheduler = "discrete"
}
fn fit_canvas(input, width, height, mode) {
shape = tensor::shape(input)
crop_width = math::min(shape[1], math::floor(shape[2] * width / height))
crop_height = math::min(shape[2], math::floor(shape[1] * height / width))
start = [math::floor((shape[1] - crop_width) / 2), math::floor((shape[2] - crop_height) / 2)]
cropped = tensor::crop(input, axes = [1, 2], start = start, size = [crop_width, crop_height])
return tensor::interpolate(cropped, size = [width, height], axes = [1, 2], mode = mode)
}
fn main() {
driving_audio_media = audio::load(driving_audio_path)
sample_rate = driving_audio_media.sample_rate
driving_audio = driving_audio_media.samples
first_frame = image::load(first_frame_path)
model = load_dit(model_path, family = "wan2_2_s2v", params_backend = "cpu")
text = load_text_encoder(text_path, family = "wan2_2_s2v", params_backend = "cpu", t5_path = text_path)
vae = load_vae(vae_path, family = "wan2_2_s2v", params_backend = "cpu")
rng = make_rng(rng_type, seed = seed)
base = tensor::zeros(latent_shape(vae, width = width, height = height, frames = frames))
first_frame_pixels = tensor::unsqueeze(image::to_f32(fit_canvas(first_frame, width, height, mode = "nearest"), channels = 3), axis = 2)
reference_video = vae_to_dit_latents(vae, vae_encode(vae, first_frame_pixels, rng = rng, tiling = tiling()))
reference_shape = tensor::shape(reference_video)
reference_latent = tensor::reshape(reference_video, reference_shape[:4])
audio_encoder = load_audio_encoder(audio_encoder_path, family = "wan2_2_s2v", params_backend = "cpu")
speech = encode_audio(audio_encoder, driving_audio, sample_rate = sample_rate)
audio_embedding = wan::s2v_audio_window(speech, latent_frames = tensor::shape(base)[2])
positive = encode_prompt(text, prompt, width = width, height = height)
negative = encode_prompt(text, negative_prompt, width = width, height = height)
pos = predictor(model, positive, ref_latents = [reference_latent], audio_embedding = audio_embedding)
neg = predictor(model, negative, ref_latents = [reference_latent], audio_embedding = audio_embedding)
guided = cfg(pos, neg, scale = cfg_scale)
sigmas = make_sigmas(guided, latent = base, steps = steps, scheduler = scheduler, flow_shift = 3.0)
start = add_noise(guided, base, tensor::randn_like(base, rng = rng), sigma = sigmas[0])
latent = sample(start, sigmas, predict = guided, method = method, rng = rng)
output_frames = vae_decode(vae, dit_to_vae_latents(vae, latent), tiling = tiling())
audio = {samples = driving_audio[:, :math::floor(sample_rate * frames / fps)], sample_rate = sample_rate}
video::save(output_path, {frames = output_frames, fps_num = fps, fps_den = 1}, audio = audio)
return {video = {frames = output_frames, fps_num = fps, fps_den = 1}, latent = latent, audio = audio}
}