Buckets:

download
raw
38.6 kB
<meta charset="utf-8" /><meta name="hf:doc:metadata" content="{&quot;title&quot;:&quot;AnyFlow&quot;,&quot;local&quot;:&quot;anyflow&quot;,&quot;sections&quot;:[{&quot;title&quot;:&quot;Bidirectional 还是 Causal —— 怎么选 pipeline&quot;,&quot;local&quot;:&quot;bidirectional-还是-causal--怎么选-pipeline&quot;,&quot;sections&quot;:[],&quot;depth&quot;:2},{&quot;title&quot;:&quot;加载 checkpoint&quot;,&quot;local&quot;:&quot;加载-checkpoint&quot;,&quot;sections&quot;:[],&quot;depth&quot;:2},{&quot;title&quot;:&quot;Any-step 采样&quot;,&quot;local&quot;:&quot;any-step-采样&quot;,&quot;sections&quot;:[],&quot;depth&quot;:2},{&quot;title&quot;:&quot;图生视频 与 视频续写&quot;,&quot;local&quot;:&quot;图生视频-与-视频续写&quot;,&quot;sections&quot;:[],&quot;depth&quot;:2},{&quot;title&quot;:&quot;LoRA 微调&quot;,&quot;local&quot;:&quot;lora-微调&quot;,&quot;sections&quot;:[],&quot;depth&quot;:2},{&quot;title&quot;:&quot;常见坑&quot;,&quot;local&quot;:&quot;常见坑&quot;,&quot;sections&quot;:[],&quot;depth&quot;:2},{&quot;title&quot;:&quot;引用&quot;,&quot;local&quot;:&quot;引用&quot;,&quot;sections&quot;:[],&quot;depth&quot;:2}],&quot;depth&quot;:1}">
<link href="/docs/diffusers/pr_13231/zh/_app/immutable/assets/0.e3b0c442.css" rel="modulepreload">
<link rel="modulepreload" href="/docs/diffusers/pr_13231/zh/_app/immutable/entry/start.ae959d63.js">
<link rel="modulepreload" href="/docs/diffusers/pr_13231/zh/_app/immutable/chunks/scheduler.e4ff9b64.js">
<link rel="modulepreload" href="/docs/diffusers/pr_13231/zh/_app/immutable/chunks/singletons.24508a3e.js">
<link rel="modulepreload" href="/docs/diffusers/pr_13231/zh/_app/immutable/chunks/index.f9be34a7.js">
<link rel="modulepreload" href="/docs/diffusers/pr_13231/zh/_app/immutable/chunks/paths.ddcace8e.js">
<link rel="modulepreload" href="/docs/diffusers/pr_13231/zh/_app/immutable/entry/app.41a5ea7b.js">
<link rel="modulepreload" href="/docs/diffusers/pr_13231/zh/_app/immutable/chunks/preload-helper.0f3d2623.js">
<link rel="modulepreload" href="/docs/diffusers/pr_13231/zh/_app/immutable/chunks/index.09f1bca0.js">
<link rel="modulepreload" href="/docs/diffusers/pr_13231/zh/_app/immutable/nodes/0.6a113eb4.js">
<link rel="modulepreload" href="/docs/diffusers/pr_13231/zh/_app/immutable/chunks/each.e59479a4.js">
<link rel="modulepreload" href="/docs/diffusers/pr_13231/zh/_app/immutable/nodes/51.1182b623.js">
<link rel="modulepreload" href="/docs/diffusers/pr_13231/zh/_app/immutable/chunks/MermaidChart.svelte_svelte_type_style_lang.5cba152f.js">
<link rel="modulepreload" href="/docs/diffusers/pr_13231/zh/_app/immutable/chunks/CodeBlock.1f5a97d4.js"><!-- HEAD_svelte-u9bgzb_START --><meta name="hf:doc:metadata" content="{&quot;title&quot;:&quot;AnyFlow&quot;,&quot;local&quot;:&quot;anyflow&quot;,&quot;sections&quot;:[{&quot;title&quot;:&quot;Bidirectional 还是 Causal —— 怎么选 pipeline&quot;,&quot;local&quot;:&quot;bidirectional-还是-causal--怎么选-pipeline&quot;,&quot;sections&quot;:[],&quot;depth&quot;:2},{&quot;title&quot;:&quot;加载 checkpoint&quot;,&quot;local&quot;:&quot;加载-checkpoint&quot;,&quot;sections&quot;:[],&quot;depth&quot;:2},{&quot;title&quot;:&quot;Any-step 采样&quot;,&quot;local&quot;:&quot;any-step-采样&quot;,&quot;sections&quot;:[],&quot;depth&quot;:2},{&quot;title&quot;:&quot;图生视频 与 视频续写&quot;,&quot;local&quot;:&quot;图生视频-与-视频续写&quot;,&quot;sections&quot;:[],&quot;depth&quot;:2},{&quot;title&quot;:&quot;LoRA 微调&quot;,&quot;local&quot;:&quot;lora-微调&quot;,&quot;sections&quot;:[],&quot;depth&quot;:2},{&quot;title&quot;:&quot;常见坑&quot;,&quot;local&quot;:&quot;常见坑&quot;,&quot;sections&quot;:[],&quot;depth&quot;:2},{&quot;title&quot;:&quot;引用&quot;,&quot;local&quot;:&quot;引用&quot;,&quot;sections&quot;:[],&quot;depth&quot;:2}],&quot;depth&quot;:1}"><!-- HEAD_svelte-u9bgzb_END --> <p></p> <div class="items-center shrink-0 min-w-[100px] max-sm:min-w-[50px] justify-end ml-auto flex" style="float: right; margin-left: 10px; display: inline-flex; position: relative; z-index: 10;"><div class="inline-flex rounded-md max-sm:rounded-sm"><button class="inline-flex items-center gap-1 h-7 max-sm:h-7 px-2 max-sm:px-1.5 text-sm font-medium text-gray-800 border border-r-0 rounded-l-md max-sm:rounded-l-sm border-gray-200 bg-white hover:shadow-inner dark:border-gray-850 dark:bg-gray-950 dark:text-gray-200 dark:hover:bg-gray-800" aria-live="polite"><span class="inline-flex items-center justify-center rounded-md p-0.5 max-sm:p-0 hover:text-gray-800 dark:hover:text-gray-200"><svg class="sm:size-3.5 size-3" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg></span> <span>Copy page</span></button> <button class="inline-flex items-center justify-center w-6 max-sm:w-5 h-7 max-sm:h-7 disabled:pointer-events-none text-sm text-gray-500 hover:text-gray-700 dark:hover:text-white rounded-r-md max-sm:rounded-r-sm border border-l transition border-gray-200 bg-white hover:shadow-inner dark:border-gray-850 dark:bg-gray-950 dark:text-gray-200 dark:hover:bg-gray-800" aria-haspopup="menu" aria-expanded="false" aria-label="Open copy menu"><svg class="transition-transform text-gray-400 overflow-visible sm:size-3.5 size-3 rotate-0" width="1em" height="1em" viewBox="0 0 12 7" fill="none" xmlns="http://www.w3.org/2000/svg"><path d="M1 1L6 6L11 1" stroke="currentColor"></path></svg></button></div> </div> <h1 class="relative group"><a id="anyflow" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#anyflow"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>AnyFlow</span></h1> <p data-svelte-h="svelte-x7hf4q"><a href="https://huggingface.co/papers/2605.13724" rel="nofollow">AnyFlow</a> 是一个视频扩散<strong>蒸馏</strong>框架,把预训练的 Wan2.1 教师
模型蒸馏成在标准 Euler 采样下支持<em>任意步数 (any-step)</em> 的学生模型。同一个蒸馏出来的 checkpoint 可以
在 1、2、4、8、16… NFE 下推理,<strong>质量随步数单调提升</strong> —— 这一点和 consistency models 不同,后者
NFE 增加反而经常掉点。</p> <p data-svelte-h="svelte-1g3jlq0">核心思路是学习 <strong>flow map</strong> $\Phi_{r\leftarrow t}: \mathbf{z}_t \to \mathbf{z}_r$(任意 $1 \ge t \ge r \ge 0$),
而不是 consistency models 学的固定端点映射 $\mathbf{z}_t \to \mathbf{z}_0$。Flow map 的可组合性消除了
采样步之间的 re-noising;on-policy 蒸馏阶段额外用 <strong>DMD 反向散度监督</strong> + <strong>Flow-Map backward simulation</strong>
(3 段 shortcut)补上 consistency 蒸馏遗留的 exposure-bias 缺口。</p> <p data-svelte-h="svelte-1kh0gnb">AnyFlow 由 NVIDIA、新加坡国立大学(NUS)和 MIT 合作完成,作者为 Yuchao Gu、Guian Fang、Yuxin Jiang、Weijia Mao、Song Han、Han Cai、Mike Zheng Shou。原始训练代码在 <a href="https://github.com/NVlabs/AnyFlow" rel="nofollow"><code>NVlabs/AnyFlow</code></a>,项目主页是 <a href="https://nvlabs.github.io/AnyFlow" rel="nofollow">nvlabs.github.io/AnyFlow</a>,4 个发布 checkpoint 归在 <a href="https://huggingface.co/collections/nvidia/anyflow" rel="nofollow"><code>nvidia/anyflow</code></a> Hugging Face collection 里。</p> <p data-svelte-h="svelte-1g7eyu5">本文档梳理实战要点:怎么选 pipeline、怎么用 any-step 采样、怎么把 AnyFlow 嵌进 T2V / I2V / V2V 工作流。</p> <h2 class="relative group"><a id="bidirectional-还是-causal--怎么选-pipeline" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#bidirectional-还是-causal--怎么选-pipeline"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Bidirectional 还是 Causal —— 怎么选 pipeline</span></h2> <p data-svelte-h="svelte-c2dmjc">AnyFlow 提供两个 pipeline 形态,scheduler 和蒸馏方法相同,区别在于<strong>怎么对帧采样</strong></p> <ul data-svelte-h="svelte-1yhoz96"><li><a href="../api/pipelines/anyflow#anyflowpipeline"><code>AnyFlowPipeline</code></a> —— <strong>bidirectional</strong> T2V。一次性对整个
视频张量去噪,全局自注意力。<strong>纯 prompt 输入、不要流式输出</strong>时选这个。</li> <li><a href="../api/pipelines/anyflow#anyflowfarpipeline"><code>AnyFlowFARPipeline</code></a> —— <strong>causal (FAR)</strong>
按 chunk 分段去噪,块稀疏因果注意力 + 跨 chunk 复用 KV cache。<strong>图生视频 (I2V)</strong><strong>视频续写 (V2V)</strong>
或任何受益于逐帧自回归采样的场景选这个。同一个模型通过 <code>video</code>(像素空间)或 <code>video_latents</code>
(已编码 latent)这两个互斥 kwarg 来切换三种任务模式。</li></ul> <p data-svelte-h="svelte-nhe92g">简化对照表:</p> <table data-svelte-h="svelte-1r1r6ai"><thead><tr><th>场景</th> <th>Pipeline</th> <th>调用方式</th></tr></thead> <tbody><tr><td>纯文生视频,固定 NFE 求最大质量</td> <td><code>AnyFlowPipeline</code></td> <td><code>pipe(prompt, ...)</code></td></tr> <tr><td>图生视频(首帧给定)</td> <td><code>AnyFlowFARPipeline</code></td> <td><code>pipe(prompt, video=&lt;单帧 tensor&gt;, ...)</code></td></tr> <tr><td>视频续写 / V2V</td> <td><code>AnyFlowFARPipeline</code></td> <td><code>pipe(prompt, video=&lt;多帧 tensor&gt;, ...)</code></td></tr> <tr><td>流式 / 渐进式生成</td> <td><code>AnyFlowFARPipeline</code></td> <td></td></tr></tbody></table> <p data-svelte-h="svelte-147wpe4">高分辨率下 bidirectional 单 token 更快;causal 牺牲一点单步速度,换来在所有 latent 帧分配前就能开始
采样的能力,对超长序列尤其有用。</p> <h2 class="relative group"><a id="加载-checkpoint" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#加载-checkpoint"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>加载 checkpoint</span></h2> <p data-svelte-h="svelte-1ivm4tg">NVIDIA 发布了 4 个 AnyFlow checkpoint,pipeline × 规模各一份:</p> <div class="code-block relative "><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre class="language-py "><!-- HTML_TAG_START --><span class="hljs-keyword">import</span> torch
<span class="hljs-keyword">from</span> diffusers <span class="hljs-keyword">import</span> AnyFlowPipeline, AnyFlowFARPipeline
<span class="hljs-comment"># Bidirectional, 轻量</span>
pipe = AnyFlowPipeline.from_pretrained(
<span class="hljs-string">&quot;nvidia/AnyFlow-Wan2.1-T2V-1.3B-Diffusers&quot;</span>, torch_dtype=torch.bfloat16
).to(<span class="hljs-string">&quot;cuda&quot;</span>)
<span class="hljs-comment"># Bidirectional, 满血</span>
pipe = AnyFlowPipeline.from_pretrained(
<span class="hljs-string">&quot;nvidia/AnyFlow-Wan2.1-T2V-14B-Diffusers&quot;</span>, torch_dtype=torch.bfloat16
).to(<span class="hljs-string">&quot;cuda&quot;</span>)
<span class="hljs-comment"># Causal (FAR), 1.3B</span>
pipe = AnyFlowFARPipeline.from_pretrained(
<span class="hljs-string">&quot;nvidia/AnyFlow-FAR-Wan2.1-1.3B-Diffusers&quot;</span>, torch_dtype=torch.bfloat16
).to(<span class="hljs-string">&quot;cuda&quot;</span>)
<span class="hljs-comment"># Causal (FAR), 14B</span>
pipe = AnyFlowFARPipeline.from_pretrained(
<span class="hljs-string">&quot;nvidia/AnyFlow-FAR-Wan2.1-14B-Diffusers&quot;</span>, torch_dtype=torch.bfloat16
).to(<span class="hljs-string">&quot;cuda&quot;</span>)<!-- HTML_TAG_END --></pre></div> <p data-svelte-h="svelte-9t7fux">四个 checkpoint 共用同一份 <a href="../api/schedulers/flow_map_euler_discrete"><code>FlowMapEulerDiscreteScheduler</code></a>
默认 <code>shift=5.0</code></p> <h2 class="relative group"><a id="any-step-采样" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#any-step-采样"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Any-step 采样</span></h2> <p data-svelte-h="svelte-1t8mtgo">AnyFlow 最关键的特性是同一个 checkpoint <strong>不需重新调度</strong>,NFE 越大质量越高。固定 prompt、扫一下步数
就能看出模型怎么在延迟和保真度之间权衡:</p> <div class="code-block relative "><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre class="language-py "><!-- HTML_TAG_START --><span class="hljs-keyword">import</span> torch
<span class="hljs-keyword">from</span> diffusers <span class="hljs-keyword">import</span> AnyFlowPipeline
<span class="hljs-keyword">from</span> diffusers.utils <span class="hljs-keyword">import</span> export_to_video
pipe = AnyFlowPipeline.from_pretrained(
<span class="hljs-string">&quot;nvidia/AnyFlow-Wan2.1-T2V-1.3B-Diffusers&quot;</span>, torch_dtype=torch.bfloat16
).to(<span class="hljs-string">&quot;cuda&quot;</span>)
prompt = <span class="hljs-string">&quot;森林里一只小熊猫在啃竹子,电影感光照&quot;</span>
<span class="hljs-keyword">for</span> nfe <span class="hljs-keyword">in</span> [<span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-number">4</span>, <span class="hljs-number">8</span>, <span class="hljs-number">16</span>, <span class="hljs-number">32</span>]:
<span class="hljs-comment"># 每轮重建 generator —— 这样跨步数对比时唯一变量是 NFE。</span>
generator = torch.Generator(<span class="hljs-string">&quot;cuda&quot;</span>).manual_seed(<span class="hljs-number">0</span>)
video = pipe(prompt, num_inference_steps=nfe, num_frames=<span class="hljs-number">81</span>, generator=generator).frames[<span class="hljs-number">0</span>]
export_to_video(video, <span class="hljs-string">f&quot;out_nfe<span class="hljs-subst">{nfe}</span>.mp4&quot;</span>, fps=<span class="hljs-number">16</span>)<!-- HTML_TAG_END --></pre></div> <p data-svelte-h="svelte-1bqpcv3">paper 的 Tab 3 / Fig 1 表明:每个 AnyFlow checkpoint 在 4 → 32 NFE 范围 VBench Quality 都单调上升,而
consistency 类基线(rCM、Self-Forcing)在同区间反而掉点。</p> <blockquote class="tip" data-svelte-h="svelte-19oa2n2"><p>Classifier-free guidance (CFG) 已经在训练阶段融进权重。pipeline 推理
<strong>不会</strong>再跑一次 unconditional 前向 —— guidance 直接由蒸馏后的权重带出。release 出来的 checkpoint
都用默认的 <code>guidance_scale=1.0</code> 即可。</p></blockquote> <h2 class="relative group"><a id="图生视频-与-视频续写" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#图生视频-与-视频续写"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>图生视频 与 视频续写</span></h2> <p data-svelte-h="svelte-q7gkj0">Causal pipeline 用同一个蒸馏模型支持三种任务模式,<strong>通过 <code>video</code> / <code>video_latents</code> 二选一来选</strong></p> <ul data-svelte-h="svelte-1o3kz10"><li><code>video</code> —— 像素空间张量,形状 <code>(B, T, C, H, W)</code><code>[0, 1]</code>,pipeline 内部会过一遍 <code>VideoProcessor</code> <ul><li>VAE 编码;</li></ul></li> <li><code>video_latents</code> —— 已经在模型布局下的 latent,跳过 VAE 编码;</li> <li>两者都不传 —— 纯文生视频;</li> <li>两者同时传 —— 抛 <code>ValueError</code>(互斥)。</li></ul> <p data-svelte-h="svelte-j71wox">Context tensor 的帧数必须满足 <code>T = 4n + 1</code>,跟 VAE 时间步长对齐。</p> <blockquote class="important" data-svelte-h="svelte-vuhdwu"><p>FAR pipeline 是分块 (chunk) rollout,<code>num_frames</code> 必须配合 chunk 调度。发布的 checkpoint 在
transformer config 里写入 <code>chunk_partition=[1, 3, 3, 3, 3, 3, 3, 2]</code>(求和 21),对应标准
<code>num_frames=81</code>(21 = (81 − 1) // 4 + 1)。改 <code>num_frames</code><strong>必须</strong>显式传匹配的 <code>chunk_partition</code>
使其求和等于 <code>(num_frames - 1) // 4 + 1</code>,否则 pipeline 会抛 <code>ValueError</code>。比如 <code>num_frames=33</code> 对应
9 个 latent 帧,可用 <code>chunk_partition=[1, 4, 4]</code></p></blockquote> <div class="code-block relative "><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre class="language-py "><!-- HTML_TAG_START --><span class="hljs-keyword">import</span> numpy <span class="hljs-keyword">as</span> np
<span class="hljs-keyword">import</span> torch
<span class="hljs-keyword">from</span> diffusers <span class="hljs-keyword">import</span> AnyFlowFARPipeline
<span class="hljs-keyword">from</span> diffusers.utils <span class="hljs-keyword">import</span> export_to_video, load_image, load_video
pipe = AnyFlowFARPipeline.from_pretrained(
<span class="hljs-string">&quot;nvidia/AnyFlow-FAR-Wan2.1-1.3B-Diffusers&quot;</span>, torch_dtype=torch.bfloat16
).to(<span class="hljs-string">&quot;cuda&quot;</span>)
<span class="hljs-keyword">def</span> <span class="hljs-title function_">to_video_tensor</span>(<span class="hljs-params">images, height=<span class="hljs-number">480</span>, width=<span class="hljs-number">832</span></span>):
<span class="hljs-string">&quot;&quot;&quot;把 PIL 列表转成 FAR pipeline 需要的 (B, T, C, H, W) [0, 1] 张量。&quot;&quot;&quot;</span>
frames = np.stack([np.asarray(img.resize((width, height))) <span class="hljs-keyword">for</span> img <span class="hljs-keyword">in</span> images]).astype(<span class="hljs-string">&quot;float32&quot;</span>) / <span class="hljs-number">255.0</span>
<span class="hljs-comment"># frames: (T, H, W, C) → (T, C, H, W) → 加 batch 维 → (1, T, C, H, W)</span>
<span class="hljs-keyword">return</span> torch.from_numpy(frames).permute(<span class="hljs-number">0</span>, <span class="hljs-number">3</span>, <span class="hljs-number">1</span>, <span class="hljs-number">2</span>).unsqueeze(<span class="hljs-number">0</span>)
<span class="hljs-comment"># 1) 文生视频(无 context)。81 帧匹配默认 chunk_partition。</span>
video = pipe(prompt=<span class="hljs-string">&quot;一只猫在夕阳下冲浪&quot;</span>, num_inference_steps=<span class="hljs-number">4</span>, num_frames=<span class="hljs-number">81</span>).frames[<span class="hljs-number">0</span>]
export_to_video(video, <span class="hljs-string">&quot;t2v.mp4&quot;</span>, fps=<span class="hljs-number">16</span>)
<span class="hljs-comment"># 2) 图生视频 —— 单帧 context 经过 VAE 是 1 个 latent,正好对上默认 chunk_partition 的第一项 (`[1, ...]`)。</span>
first_frame = load_image(<span class="hljs-string">&quot;path/to/first_frame.png&quot;</span>)
context_tensor = to_video_tensor([first_frame]).to(<span class="hljs-string">&quot;cuda&quot;</span>) <span class="hljs-comment"># (1, 1, 3, 480, 832), [0, 1]</span>
video = pipe(
prompt=<span class="hljs-string">&quot;一只猫走过阳光下的草坪&quot;</span>,
video=context_tensor,
num_inference_steps=<span class="hljs-number">4</span>,
num_frames=<span class="hljs-number">81</span>,
).frames[<span class="hljs-number">0</span>]
export_to_video(video, <span class="hljs-string">&quot;i2v.mp4&quot;</span>, fps=<span class="hljs-number">16</span>)
<span class="hljs-comment"># 3) 视频续写。9 帧 raw context → 3 个 latent context;显式覆盖 chunk_partition,让第一块正好覆盖 context。</span>
context_frames = load_video(<span class="hljs-string">&quot;path/to/context.mp4&quot;</span>)[:<span class="hljs-number">9</span>] <span class="hljs-comment"># 9 = 4·2 + 1</span>
context_tensor = to_video_tensor(context_frames).to(<span class="hljs-string">&quot;cuda&quot;</span>) <span class="hljs-comment"># (1, 9, 3, 480, 832)</span>
video = pipe(
prompt=<span class="hljs-string">&quot;继续这个故事&quot;</span>,
video=context_tensor,
num_inference_steps=<span class="hljs-number">4</span>,
num_frames=<span class="hljs-number">81</span>,
chunk_partition=[<span class="hljs-number">3</span>, <span class="hljs-number">3</span>, <span class="hljs-number">3</span>, <span class="hljs-number">3</span>, <span class="hljs-number">3</span>, <span class="hljs-number">3</span>, <span class="hljs-number">3</span>], <span class="hljs-comment"># 7 个 chunk × 3 = 21 latent;首块就是 context</span>
).frames[<span class="hljs-number">0</span>]
export_to_video(video, <span class="hljs-string">&quot;v2v.mp4&quot;</span>, fps=<span class="hljs-number">16</span>)<!-- HTML_TAG_END --></pre></div> <p data-svelte-h="svelte-1xvogfx">底层 patchify chunk 调度根据 <code>video</code> / <code>video_latents</code> 是否给定自动调整:纯文生用 kernel 2 (full) 和
4 (compressed);有 context 时第一个 chunk 改成 kernel 1,让条件帧保留全分辨率。</p> <p data-svelte-h="svelte-6as2g1">如果你已经有 VAE 编码过的 latent,可以直接传 <code>video_latents=&lt;tensor&gt;</code> 跳过 <code>vae_encode</code> 步骤
(和 <code>video</code> 互斥)。</p> <h2 class="relative group"><a id="lora-微调" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#lora-微调"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>LoRA 微调</span></h2> <p data-svelte-h="svelte-1hp1p5d">两个 pipeline 都复用 <a href="../api/loaders/lora"><code>WanLoraLoaderMixin</code></a>,因此为对应 Wan2.1 backbone 训练的
LoRA adapter 直接加载即可:</p> <div class="code-block relative "><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre class="language-py "><!-- HTML_TAG_START -->pipe.load_lora_weights(<span class="hljs-string">&quot;path/or/repo/with/wan_lora&quot;</span>)<!-- HTML_TAG_END --></pre></div> <p data-svelte-h="svelte-v9lpd7">如果要做<strong>继续 on-policy 蒸馏微调</strong>(用论文里相同的 DMD 反向散度监督配方训新 LoRA),请参考原始
AnyFlow 训练框架 <a href="https://github.com/NVlabs/AnyFlow" rel="nofollow"><code>NVlabs/AnyFlow</code></a>,这套训练流程不在
diffusers 范围内。</p> <h2 class="relative group"><a id="常见坑" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#常见坑"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>常见坑</span></h2> <ul data-svelte-h="svelte-wokq2p"><li><strong>永远 <code>guidance_scale=1.0</code></strong> 蒸馏后的 checkpoint 已经把 CFG 融进权重。设 <code>&gt; 1</code> 会多跑一遍
unconditional 前向、延迟翻倍、质量微降。</li> <li><strong>Bidirectional pipeline 不支持流式。</strong> 所有 <code>num_frames</code> 一起去噪。需要边采边播请用 causal pipeline。</li> <li><strong>Causal pipeline KV cache 假设 chunk 调度跨调用一致。</strong> 中途重建 cache 不被 release 模型支持。</li> <li><strong><code>num_frames</code> 必须满足 VAE 时间步长。</strong> release checkpoint 用 <code>(N - 1) % 4 == 0</code> 的值(如 9、17、33、81)。</li></ul> <h2 class="relative group"><a id="引用" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#引用"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>引用</span></h2> <div class="code-block relative "><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre class="language-bibtex "><!-- HTML_TAG_START --><span class="language-xml">@misc</span><span class="hljs-template-variable">{gu2026anyflowanystepvideodiffusion,
title={AnyFlow: Any-Step Video Diffusion Model with On-Policy Flow Map Distillation}</span><span class="language-xml">,
author=</span><span class="hljs-template-variable">{Yuchao Gu and Guian Fang and Yuxin Jiang and Weijia Mao and Song Han and Han Cai and Mike Zheng Shou}</span><span class="language-xml">,
year=</span><span class="hljs-template-variable">{2026}</span><span class="language-xml">,
eprint=</span><span class="hljs-template-variable">{2605.13724}</span><span class="language-xml">,
archivePrefix=</span><span class="hljs-template-variable">{arXiv}</span><span class="language-xml">,
primaryClass=</span><span class="hljs-template-variable">{cs.CV}</span><span class="language-xml">,
url=</span><span class="hljs-template-variable">{https://arxiv.org/abs/2605.13724}</span><span class="language-xml">,
}
@article</span><span class="hljs-template-variable">{gu2025long,
title={Long-Context Autoregressive Video Modeling with Next-Frame Prediction}</span><span class="language-xml">,
author=</span><span class="hljs-template-variable">{Gu, Yuchao and Mao, Weijia and Shou, Mike Zheng}</span><span class="language-xml">,
journal=</span><span class="hljs-template-variable">{arXiv preprint arXiv:2503.19325}</span><span class="language-xml">,
year=</span><span class="hljs-template-variable">{2025}</span><span class="language-xml">
}</span><!-- HTML_TAG_END --></pre></div> <a class="!text-gray-400 !no-underline text-sm flex items-center not-prose mt-4" href="https://github.com/huggingface/diffusers/blob/main/docs/source/zh/using-diffusers/anyflow.md" target="_blank"><svg class="mr-1" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M31,16l-7,7l-1.41-1.41L28.17,16l-5.58-5.59L24,9l7,7z"></path><path d="M1,16l7-7l1.41,1.41L3.83,16l5.58,5.59L8,23l-7-7z"></path><path d="M12.419,25.484L17.639,6.552l1.932,0.518L14.351,26.002z"></path></svg> <span data-svelte-h="svelte-zjs2n5"><span class="underline">Update</span> on GitHub</span></a> <p></p>
<script>
{
__sveltekit_dylusw = {
assets: "/docs/diffusers/pr_13231/zh",
base: "/docs/diffusers/pr_13231/zh",
env: {}
};
const element = document.currentScript.parentElement;
const data = [null,null];
Promise.all([
import("/docs/diffusers/pr_13231/zh/_app/immutable/entry/start.ae959d63.js"),
import("/docs/diffusers/pr_13231/zh/_app/immutable/entry/app.41a5ea7b.js")
]).then(([kit, app]) => {
kit.start(app, element, {
node_ids: [0, 51],
data,
form: null,
error: null
});
});
}
</script>

Xet Storage Details

Size:
38.6 kB
·
Xet hash:
c067e7383e39284a217742467eb5d3d312399c2faea8b92843e7418de2f22e8b

Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.