Views
No views yet
A translucent jellyfish pulses rhythmically in the deep blue ocean, its tentacles trailing like silk ribbons in the current.
A fluffy red panda clings to a moss-covered branch in a misty bamboo forest, its tail curled tightly around the wood for balance.
FASTVIDEO_ATTENTION_BACKEND=VIDEO_SPARSE_ATTN 稀疏注意力后端,区别于原生Torch SDPA密集注意力机制,通过VSA稀疏度0.8的精细化配置,过滤视频时序冗余注意力计算,在几乎无损生成画质的基础上,大幅减少时序维度计算量,有效降低推理显存占用与运算耗时。1000,757,522,结合时序比例阈值(0.02~0.98)约束,实现模型多尺度时序特征学习。通过生成器迭代更新、真实分数引导缩放(3.5)的组合优化,让蒸馏模型精准复刻原模型的视频光影、运动趋势、细节纹理特征,避免蒸馏后画面模糊、动态卡顿、文本对齐偏差等问题。1#下载fastvideo推理框架
2pip install fastvideo
3#下载vsa注意力
4pip install vsa --index-url https://pypi.tuna.tsinghua.edu.cn/simple
5#下载模型权重
6modelscope download --model HZNing/Wan2.1-DMD2 --local_dir ./Wan2.1-DMD2-Local1import os
2import time
3from fastvideo import VideoGenerator
4
5def main():
6 # ==========================
7 # 1. Environment Setup
8 # ==========================
9 os.environ["FASTVIDEO_ATTENTION_BACKEND"] = "VIDEO_SPARSE_ATTN"
10 # 如果显存不足,可以尝试切换回 TORCH_SDPA
11 # os.environ["FASTVIDEO_ATTENTION_BACKEND"] = "TORCH_SDPA"
12
13 # ==========================
14 # 2. Configuration
15 # ==========================
16 MODEL_PATH = "./Wan2.1-DMD2-Local"
17 OUTPUT_DIR = "my_videos/"
18 NUM_GPUS = 1
19
20 # 生成参数 (根据模型能力调整)
21 GENERATE_KWARGS = {
22 "num_frames": 81, # 视频帧数
23 "height": 480, # 高度
24 "width": 832, # 宽度
25 "guidance_scale": 6.0, # 引导系数
26 "num_inference_steps": 30, # 推理步数
27 }
28
29 # ==========================
30 # 3. Prompts List
31 # ==========================
32 prompts = [
33 "A curious raccoon peers through a vibrant field of yellow sunflowers, its eyes wide with interest.",
34 "A fluffy red panda clings to a moss-covered branch in a misty bamboo forest, its tail curled tightly around the wood for balance.",
35 ]
36
37 # ==========================
38 # 4. Initialize Generator
39 # ==========================
40 print(f"Loading model from: {MODEL_PATH}")
41 generator = VideoGenerator.from_pretrained(
42 MODEL_PATH,
43 num_gpus=NUM_GPUS,
44 )
45 print("Model loaded successfully.\n")
46
47 # Create output directory if it doesn't exist
48 os.makedirs(OUTPUT_DIR, exist_ok=True)
49
50 # ==========================
51 # 5. Batch Generation
52 # ==========================
53 total_prompts = len(prompts)
54
55 for idx, prompt in enumerate(prompts, 1):
56 print(f"[{idx}/{total_prompts}] Generating video for prompt:")
57 print(f" Prompt: {prompt[:80]}...") # 打印前80个字符
58
59 # 生成安全的文件名 (使用索引 + 简短描述)
60 # 替换空格和特殊字符为下划线
61 safe_name = f"video_{idx:03d}"
62 output_path = os.path.join(OUTPUT_DIR, safe_name)
63
64 start_time = time.time()
65
66 try:
67 # 生成视频
68 video = generator.generate_video(
69 prompt=prompt,
70 output_path=output_path,
71 save_video=True,
72 **GENERATE_KWARGS # 传入额外参数
73 )
74
75 elapsed_time = time.time() - start_time
76 print(f" ✅ Success! Saved to: {output_path}.mp4 (Time: {elapsed_time:.2f}s)\n")
77
78 except Exception as e:
79 elapsed_time = time.time() - start_time
80 print(f" ❌ Failed! Error: {str(e)} (Time: {elapsed_time:.2f}s)\n")
81 # 继续下一个,不中断流程
82 continue
83
84 print("="*50)
85 print("Batch generation completed!")
86 print(f"Output directory: {os.path.abspath(OUTPUT_DIR)}")
87
88if __name__ == '__main__':
89 main()
90(注:部分内容可能由 AI 生成)