Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
renzhc
diffusers_dcu
Commits
f868d4b5
Unverified
Commit
f868d4b5
authored
Aug 19, 2025
by
Sam Yuan
Committed by
GitHub
Aug 19, 2025
Browse files
translate document to zh (#12179)
Signed-off-by:
SamYuan1990
<
yy19902439@126.com
>
parent
cc48b936
Changes
10
Hide whitespace changes
Inline
Side-by-side
Showing
10 changed files
with
1401 additions
and
0 deletions
+1401
-0
docs/source/zh/_toctree.yml
docs/source/zh/_toctree.yml
+21
-0
docs/source/zh/community_projects.md
docs/source/zh/community_projects.md
+89
-0
docs/source/zh/modular_diffusers/auto_pipeline_blocks.md
docs/source/zh/modular_diffusers/auto_pipeline_blocks.md
+156
-0
docs/source/zh/modular_diffusers/loop_sequential_pipeline_blocks.md
...e/zh/modular_diffusers/loop_sequential_pipeline_blocks.md
+93
-0
docs/source/zh/modular_diffusers/modular_diffusers_states.md
docs/source/zh/modular_diffusers/modular_diffusers_states.md
+74
-0
docs/source/zh/modular_diffusers/modular_pipeline.md
docs/source/zh/modular_diffusers/modular_pipeline.md
+358
-0
docs/source/zh/modular_diffusers/overview.md
docs/source/zh/modular_diffusers/overview.md
+38
-0
docs/source/zh/modular_diffusers/pipeline_block.md
docs/source/zh/modular_diffusers/pipeline_block.md
+114
-0
docs/source/zh/modular_diffusers/quickstart.md
docs/source/zh/modular_diffusers/quickstart.md
+346
-0
docs/source/zh/modular_diffusers/sequential_pipeline_blocks.md
...source/zh/modular_diffusers/sequential_pipeline_blocks.md
+112
-0
No files found.
docs/source/zh/_toctree.yml
View file @
f868d4b5
...
@@ -25,6 +25,25 @@
...
@@ -25,6 +25,25 @@
-
local
:
optimization/xformers
-
local
:
optimization/xformers
title
:
xFormers
title
:
xFormers
-
title
:
Modular Diffusers
isExpanded
:
false
sections
:
-
local
:
modular_diffusers/overview
title
:
Overview
-
local
:
modular_diffusers/quickstart
title
:
Quickstart
-
local
:
modular_diffusers/modular_diffusers_states
title
:
States
-
local
:
modular_diffusers/pipeline_block
title
:
ModularPipelineBlocks
-
local
:
modular_diffusers/sequential_pipeline_blocks
title
:
SequentialPipelineBlocks
-
local
:
modular_diffusers/loop_sequential_pipeline_blocks
title
:
LoopSequentialPipelineBlocks
-
local
:
modular_diffusers/auto_pipeline_blocks
title
:
AutoPipelineBlocks
-
local
:
modular_diffusers/modular_pipeline
title
:
ModularPipeline
-
title
:
Training
-
title
:
Training
isExpanded
:
false
isExpanded
:
false
...
@@ -63,6 +82,8 @@
...
@@ -63,6 +82,8 @@
sections
:
sections
:
-
title
:
Task recipes
-
title
:
Task recipes
sections
:
sections
:
-
local
:
community_projects
title
:
Projects built with Diffusers
-
local
:
conceptual/philosophy
-
local
:
conceptual/philosophy
title
:
Philosophy
title
:
Philosophy
-
local
:
conceptual/contribution
-
local
:
conceptual/contribution
...
...
docs/source/zh/community_projects.md
0 → 100644
View file @
f868d4b5
<!--版权 2025 The HuggingFace Team。保留所有权利。
根据Apache许可证,版本2.0("许可证")授权;除非符合许可证,否则不得使用此文件。您可以在
http://www.apache.org/licenses/LICENSE-2.0
获取许可证的副本。
除非适用法律要求或书面同意,根据许可证分发的软件是按"原样"分发的,没有任何形式的明示或暗示的担保或条件。有关许可证的特定语言,请参阅许可证。
-->
# 社区项目
欢迎来到社区项目。这个空间致力于展示我们充满活力的社区使用
`diffusers`
库创建的令人难以置信的工作和创新应用。
本节旨在:
-
突出使用
`diffusers`
构建的多样化和鼓舞人心的项目
-
促进我们社区内的知识共享
-
提供如何利用
`diffusers`
的实际例子
探索愉快,感谢您成为Diffusers社区的一部分!
<table>
<tr>
<th>
项目名称
</th>
<th>
描述
</th>
</tr>
<tr
style=
"border-top: 2px solid black"
>
<td><a
href=
"https://github.com/carson-katri/dream-textures"
>
dream-textures
</a></td>
<td>
Stable Diffusion内置到Blender
</td>
</tr>
<tr
style=
"border-top: 2px solid black"
>
<td><a
href=
"https://github.com/megvii-research/HiDiffusion"
>
HiDiffusion
</a></td>
<td>
仅通过添加一行代码即可提高扩散模型的分辨率和速度
</td>
</tr>
<tr
style=
"border-top: 2px solid black"
>
<td><a
href=
"https://github.com/lllyasviel/IC-Light"
>
IC-Light
</a></td>
<td>
IC-Light是一个用于操作图像照明的项目
</td>
</tr>
<tr
style=
"border-top: 2px solid black"
>
<td><a
href=
"https://github.com/InstantID/InstantID"
>
InstantID
</a></td>
<td>
InstantID:零样本身份保留生成在几秒钟内
</td>
</tr>
<tr
style=
"border-top: 2px solid black"
>
<td><a
href=
"https://github.com/Sanster/IOPaint"
>
IOPaint
</a></td>
<td>
由SOTA AI模型驱动的图像修复工具。从您的图片中移除任何不需要的物体、缺陷、人物,或擦除并替换(由stable_diffusion驱动)图片上的任何内容。
</td>
</tr>
<tr
style=
"border-top: 2px solid black"
>
<td><a
href=
"https://github.com/bmaltais/kohya_ss"
>
Kohya
</a></td>
<td>
Kohya的Stable Diffusion训练器的Gradio GUI
</td>
</tr>
<tr
style=
"border-top: 2px solid black"
>
<td><a
href=
"https://github.com/magic-research/magic-animate"
>
MagicAnimate
</a></td>
<td>
MagicAnimate:使用扩散模型进行时间一致的人体图像动画
</td>
</tr>
<tr
style=
"border-top: 2px solid black"
>
<td><a
href=
"https://github.com/levihsu/OOTDiffusion"
>
OOTDiffusion
</a></td>
<td>
基于潜在扩散的虚拟试穿控制
</td>
</tr>
<tr
style=
"border-top: 2px solid black"
>
<td><a
href=
"https://github.com/vladmandic/automatic"
>
SD.Next
</a></td>
<td>
SD.Next: Stable Diffusion 和其他基于Diffusion的生成图像模型的高级实现
</td>
</tr>
<tr
style=
"border-top: 2px solid black"
>
<td><a
href=
"https://github.com/ashawkey/stable-dreamfusion"
>
stable-dreamfusion
</a></td>
<td>
使用 NeRF + Diffusion 进行文本到3D & 图像到3D & 网格导出
</td>
</tr>
<tr
style=
"border-top: 2px solid black"
>
<td><a
href=
"https://github.com/HVision-NKU/StoryDiffusion"
>
StoryDiffusion
</a></td>
<td>
StoryDiffusion 可以通过生成一致的图像和视频来创造一个神奇的故事。
</td>
</tr>
<tr
style=
"border-top: 2px solid black"
>
<td><a
href=
"https://github.com/cumulo-autumn/StreamDiffusion"
>
StreamDiffusion
</a></td>
<td>
实时交互生成的管道级解决方案
</td>
</tr>
<tr
style=
"border-top: 2px solid black"
>
<td><a
href=
"https://github.com/Netwrck/stable-diffusion-server"
>
Stable Diffusion Server
</a></td>
<td>
配置用于使用一个 stable diffusion 模型进行修复/生成/img2img 的服务器
</td>
</tr>
<tr
style=
"border-top: 2px solid black"
>
<td><a
href=
"https://github.com/suzukimain/auto_diffusers"
>
Model Search
</a></td>
<td>
在 Civitai 和 Hugging Face 上搜索模型
</td>
</tr>
<tr
style=
"border-top: 2px solid black"
>
<td><a
href=
"https://github.com/beinsezii/skrample"
>
Skrample
</a></td>
<td>
完全模块化的调度器功能,具有一流的 diffusers 集成。
</td>
</tr>
</table>
docs/source/zh/modular_diffusers/auto_pipeline_blocks.md
0 → 100644
View file @
f868d4b5
<!--版权所有 2025 The HuggingFace Team。保留所有权利。
根据Apache许可证2.0版("许可证")授权;除非符合许可证,否则不得使用此文件。您可以在
http://www.apache.org/licenses/LICENSE-2.0
获取许可证的副本。
除非适用法律要求或书面同意,根据许可证分发的软件按"原样"分发,无任何明示或暗示的担保或条件。有关许可证的特定语言管理权限和限制,请参阅许可证。
-->
# AutoPipelineBlocks
[
`~modular_pipelines.AutoPipelineBlocks`
] 是一种包含支持不同工作流程的块的多块类型。它根据运行时提供的输入自动选择要运行的子块。这通常用于将多个工作流程(文本到图像、图像到图像、修复)打包到一个管道中以便利。
本指南展示如何创建 [
`~modular_pipelines.AutoPipelineBlocks`
]。
创建三个 [
`~modular_pipelines.ModularPipelineBlocks`
] 用于文本到图像、图像到图像和修复。这些代表了管道中可用的不同工作流程。
<hfoptions
id=
"auto"
>
<hfoption
id=
"text-to-image"
>
```
py
import
torch
from
diffusers.modular_pipelines
import
ModularPipelineBlocks
,
InputParam
,
OutputParam
class
TextToImageBlock
(
ModularPipelineBlocks
):
model_name
=
"text2img"
@
property
def
inputs
(
self
):
return
[
InputParam
(
name
=
"prompt"
)]
@
property
def
intermediate_outputs
(
self
):
return
[]
@
property
def
description
(
self
):
return
"我是一个文本到图像的工作流程!"
def
__call__
(
self
,
components
,
state
):
block_state
=
self
.
get_block_state
(
state
)
print
(
"运行文本到图像工作流程"
)
# 在这里添加你的文本到图像逻辑
# 例如:根据提示生成图像
self
.
set_block_state
(
state
,
block_state
)
return
components
,
state
```
</hfoption>
<hfoption
id=
"image-to-image"
>
```
py
class
ImageToImageBlock
(
ModularPipelineBlocks
):
model_name
=
"img2img"
@
property
def
inputs
(
self
):
return
[
InputParam
(
name
=
"prompt"
),
InputParam
(
name
=
"image"
)]
@
property
def
intermediate_outputs
(
self
):
return
[]
@
property
def
description
(
self
):
return
"我是一个图像到图像的工作流程!"
def
__call__
(
self
,
components
,
state
):
block_state
=
self
.
get_block_state
(
state
)
print
(
"运行图像到图像工作流程"
)
# 在这里添加你的图像到图像逻辑
# 例如:根据提示转换输入图像
self
.
set_block_state
(
state
,
block_state
)
return
components
,
state
```
</hfoption>
<hfoption
id=
"inpaint"
>
```
py
class
InpaintBlock
(
ModularPipelineBlocks
):
model_name
=
"inpaint"
@
property
def
inputs
(
self
):
return
[
InputParam
(
name
=
"prompt"
),
InputParam
(
name
=
"image"
),
InputParam
(
name
=
"mask"
)]
@
property
def
intermediate_outputs
(
self
):
return
[]
@
property
def
description
(
self
):
return
"我是一个修复工作流!"
def
__call__
(
self
,
components
,
state
):
block_state
=
self
.
get_block_state
(
state
)
print
(
"运行修复工作流"
)
# 在这里添加你的修复逻辑
# 例如:根据提示填充被遮罩的区域
self
.
set_block_state
(
state
,
block_state
)
return
components
,
state
```
</hfoption>
</hfoptions>
创建一个包含子块类及其对应块名称列表的[
`~modular_pipelines.AutoPipelineBlocks`
]类。
你还需要包括
`block_trigger_inputs`
,一个触发相应块的输入名称列表。如果在运行时提供了触发输入,则选择该块运行。使用
`None`
来指定如果未检测到触发输入时运行的默认块。
最后,重要的是包括一个
`description`
,清楚地解释哪些输入触发哪些工作流。这有助于用户理解如何运行特定的工作流。
```
py
from
diffusers.modular_pipelines
import
AutoPipelineBlocks
class
AutoImageBlocks
(
AutoPipelineBlocks
):
# 选择子块类的列表
block_classes
=
[
block_inpaint_cls
,
block_i2i_cls
,
block_t2i_cls
]
# 每个块的名称,顺序相同
block_names
=
[
"inpaint"
,
"img2img"
,
"text2img"
]
# 决定运行哪个块的触发输入
# - "mask" 触发修复工作流
# - "image" 触发img2img工作流(但仅在未提供mask时)
# - 如果以上都没有,运行text2img工作流(默认)
block_trigger_inputs
=
[
"mask"
,
"image"
,
None
]
# 对于AutoPipelineBlocks来说,描述极其重要
def
description
(
self
):
return
(
"Pipeline generates images given different types of conditions!
\n
"
+
"This is an auto pipeline block that works for text2img, img2img and inpainting tasks.
\n
"
+
" - inpaint workflow is run when `mask` is provided.
\n
"
+
" - img2img workflow is run when `image` is provided (but only when `mask` is not provided).
\n
"
+
" - text2img workflow is run when neither `image` nor `mask` is provided.
\n
"
)
```
包含
`description`
以避免任何关于如何运行块和需要什么输入的混淆
**非常**
重要。虽然[
`~modular_pipelines.AutoPipelineBlocks`
]很方便,但如果它没有正确解释,其条件逻辑可能难以理解。
创建
`AutoImageBlocks`
的一个实例。
```
py
auto_blocks
=
AutoImageBlocks
()
```
对于更复杂的组合,例如在更大的管道中作为子块使用的嵌套[
`~modular_pipelines.AutoPipelineBlocks`
]块,使用[
`~modular_pipelines.SequentialPipelineBlocks.get_execution_blocks`
]方法根据你的输入提取实际运行的块。
```
py
auto_blocks
.
get_execution_blocks
(
"mask"
)
```
docs/source/zh/modular_diffusers/loop_sequential_pipeline_blocks.md
0 → 100644
View file @
f868d4b5
<!--版权 2025 The HuggingFace Team。保留所有权利。
根据 Apache 许可证 2.0 版("许可证")授权;除非符合许可证,否则不得使用此文件。您可以在
http://www.apache.org/licenses/LICENSE-2.0
获取许可证的副本。
除非适用法律要求或书面同意,根据许可证分发的软件按"原样"分发,无任何明示或暗示的担保或条件。请参阅许可证了解
特定语言下的权限和限制。
-->
# LoopSequentialPipelineBlocks
[
`~modular_pipelines.LoopSequentialPipelineBlocks`
] 是一种多块类型,它将其他 [
`~modular_pipelines.ModularPipelineBlocks`
] 以循环方式组合在一起。数据循环流动,使用
`intermediate_inputs`
和
`intermediate_outputs`
,并且每个块都是迭代运行的。这通常用于创建一个默认是迭代的去噪循环。
本指南向您展示如何创建 [
`~modular_pipelines.LoopSequentialPipelineBlocks`
]。
## 循环包装器
[
`~modular_pipelines.LoopSequentialPipelineBlocks`
],也被称为
*循环包装器*
,因为它定义了循环结构、迭代变量和配置。在循环包装器内,您需要以下变量。
-
`loop_inputs`
是用户提供的值,等同于 [
`~modular_pipelines.ModularPipelineBlocks.inputs`
]。
-
`loop_intermediate_inputs`
是来自 [
`~modular_pipelines.PipelineState`
] 的中间变量,等同于 [
`~modular_pipelines.ModularPipelineBlocks.intermediate_inputs`
]。
-
`loop_intermediate_outputs`
是由块创建并添加到 [
`~modular_pipelines.PipelineState`
] 的新中间变量。它等同于 [
`~modular_pipelines.ModularPipelineBlocks.intermediate_outputs`
]。
-
`__call__`
方法定义了循环结构和迭代逻辑。
```
py
import
torch
from
diffusers.modular_pipelines
import
LoopSequentialPipelineBlocks
,
ModularPipelineBlocks
,
InputParam
,
OutputParam
class
LoopWrapper
(
LoopSequentialPipelineBlocks
):
model_name
=
"test"
@
property
def
description
(
self
):
return
"I'm a loop!!"
@
property
def
loop_inputs
(
self
):
return
[
InputParam
(
name
=
"num_steps"
)]
@
torch
.
no_grad
()
def
__call__
(
self
,
components
,
state
):
block_state
=
self
.
get_block_state
(
state
)
# 循环结构 - 可以根据您的需求定制
for
i
in
range
(
block_state
.
num_steps
):
# loop_step 按顺序执行所有注册的块
components
,
block_state
=
self
.
loop_step
(
components
,
block_state
,
i
=
i
)
self
.
set_block_state
(
state
,
block_state
)
return
components
,
state
```
循环包装器可以传递额外的参数,如当前迭代索引,到循环块。
## 循环块
循环块是一个 [
`~modular_pipelines.ModularPipelineBlocks`
],但
`__call__`
方法的行为不同。
-
它从循环包装器。
-
它直接与[
`~modular_pipelines.BlockState`
]一起工作,而不是[
`~modular_pipelines.PipelineState`
]。
-
它不需要检索或更新[
`~modular_pipelines.BlockState`
]。
循环块共享相同的[
`~modular_pipelines.BlockState`
],以允许值在循环的每次迭代中累积和变化。
```
py
class
LoopBlock
(
ModularPipelineBlocks
):
model_name
=
"test"
@
property
def
inputs
(
self
):
return
[
InputParam
(
name
=
"x"
)]
@
property
def
intermediate_outputs
(
self
):
# 这个块产生的输出
return
[
OutputParam
(
name
=
"x"
)]
@
property
def
description
(
self
):
return
"我是一个在`LoopWrapper`类内部使用的块"
def
__call__
(
self
,
components
,
block_state
,
i
:
int
):
block_state
.
x
+=
1
return
components
,
block_state
```
## LoopSequentialPipelineBlocks
使用[
`~modular_pipelines.LoopSequentialPipelineBlocks.from_blocks_dict`
]方法将循环块添加到循环包装器中,以创建[
`~modular_pipelines.LoopSequentialPipelineBlocks`
]。
```
py
loop
=
LoopWrapper
.
from_blocks_dict
({
"block1"
:
LoopBlock
})
```
添加更多的循环块以在每次迭代中运行,使用[
`~modular_pipelines.LoopSequentialPipelineBlocks.from_blocks_dict`
]。这允许您在不改变循环逻辑本身的情况下修改块。
```
py
loop
=
LoopWrapper
.
from_blocks_dict
({
"block1"
:
LoopBlock
(),
"block2"
:
LoopBlock
})
```
docs/source/zh/modular_diffusers/modular_diffusers_states.md
0 → 100644
View file @
f868d4b5
<!--版权 2025 The HuggingFace Team。保留所有权利。
根据Apache许可证2.0版("许可证")授权;除非符合许可证的规定,否则不得使用此文件。
您可以在以下网址获取许可证的副本
http://www.apache.org/licenses/LICENSE-2.0
除非适用法律要求或书面同意,根据许可证分发的软件是基于"按原样"分发的,没有任何形式的明示或暗示的担保或条件。有关许可证下特定的语言管理权限和限制,请参阅许可证。
-->
# 状态
块依赖于[
`~modular_pipelines.PipelineState`
]和[
`~modular_pipelines.BlockState`
]数据结构进行通信和数据共享。
| 状态 | 描述 |
|-------|-------------|
| [
`~modular_pipelines.PipelineState`
] | 维护管道执行所需的整体数据,并允许块读取和更新其数据。 |
| [
`~modular_pipelines.BlockState`
] | 允许每个块使用来自
`inputs`
的必要数据执行其计算 |
本指南解释了状态如何工作以及它们如何连接块。
## PipelineState
[
`~modular_pipelines.PipelineState`
]是所有块的全局状态容器。它维护管道的完整运行时状态,并为块提供了一种结构化的方式来读取和写入共享数据。
[
`~modular_pipelines.PipelineState`
]中有两个字典用于结构化数据。
-
`values`
字典是一个
**可变**
状态,包含用户提供的输入值的副本和由块生成的中间输出值。如果一个块修改了一个
`input`
,它将在调用
`set_block_state`
后反映在
`values`
字典中。
```
py
PipelineState
(
values
=
{
'prompt'
:
'a cat'
'guidance_scale'
:
7.0
'num_inference_steps'
:
25
'prompt_embeds'
:
Tensor
(
dtype
=
torch
.
float32
,
shape
=
torch
.
Size
([
1
,
1
,
1
,
1
]))
'negative_prompt_embeds'
:
None
},
)
```
## BlockState
[
`~modular_pipelines.BlockState`
]是[
`~modular_pipelines.PipelineState`
]中相关变量的局部视图,单个块需要这些变量来执行其计算。
直接作为属性访问这些变量,如
`block_state.image`
。
```
py
BlockState
(
image
:
<
PIL
.
Image
.
Image
image
mode
=
RGB
size
=
512
x512
at
0x7F3ECC494640
>
)
```
当一个块的
`__call__`
方法被执行时,它用
`self.get_block_state(state)`
检索[
`BlockState`
],执行其操作,并用
`self.set_block_state(state, block_state)`
更新[
`~modular_pipelines.PipelineState`
]。
```
py
def
__call__
(
self
,
components
,
state
):
# 检索BlockState
block_state
=
self
.
get_block_state
(
state
)
# 对输入进行计算的逻辑
# 更新PipelineState
self
.
set_block_state
(
state
,
block_state
)
return
components
,
state
```
## 状态交互
[
`~modular_pipelines.PipelineState`
]和[
`~modular_pipelines.BlockState`
]的交互由块的
`inputs`
和
`intermediate_outputs`
定义。
-
`inputs`
,
一个块可以修改输入 - 比如
`block_state.image`
- 并且这个改变可以通过调用
`set_block_state`
全局传播到 [
`~modular_pipelines.PipelineState`
]。
-
`intermediate_outputs`
,是一个块创建的新变量。它被添加到 [
`~modular_pipelines.PipelineState`
] 的
`values`
字典中,并且可以作为后续块的可用变量,或者由用户作为管道的最终输出访问。
docs/source/zh/modular_diffusers/modular_pipeline.md
0 → 100644
View file @
f868d4b5
<!--版权 2025 HuggingFace 团队。保留所有权利。
根据 Apache 许可证 2.0 版(“许可证”)授权;除非符合许可证的规定,否则不得使用此文件。您可以在
http://www.apache.org/licenses/LICENSE-2.0
获取许可证的副本。
除非适用法律要求或书面同意,根据许可证分发的软件是基于“按原样”分发的,没有任何形式的明示或暗示的保证或条件。有关许可证的特定语言,请参阅许可证。
-->
# 模块化管道
[
`ModularPipeline`
] 将 [
`~modular_pipelines.ModularPipelineBlocks`
] 转换为可执行的管道,加载模型并执行块中定义的计算步骤。它是运行管道的主要接口,与 [
`DiffusionPipeline`
] API 非常相似。
主要区别在于在管道中包含了一个预期的
`output`
参数。
<hfoptions
id=
"example"
>
<hfoption
id=
"text-to-image"
>
```
py
import
torch
from
diffusers.modular_pipelines
import
SequentialPipelineBlocks
from
diffusers.modular_pipelines.stable_diffusion_xl
import
TEXT2IMAGE_BLOCKS
blocks
=
SequentialPipelineBlocks
.
from_blocks_dict
(
TEXT2IMAGE_BLOCKS
)
modular_repo_id
=
"YiYiXu/modular-loader-t2i-0704"
pipeline
=
blocks
.
init_pipeline
(
modular_repo_id
)
pipeline
.
load_default_components
(
torch_dtype
=
torch
.
float16
)
pipeline
.
to
(
"cuda"
)
image
=
pipeline
(
prompt
=
"Astronaut in a jungle, cold color palette, muted colors, detailed, 8k"
,
output
=
"images"
)[
0
]
image
.
save
(
"modular_t2i_out.png"
)
```
</hfoption>
<hfoption
id=
"image-to-image"
>
```
py
import
torch
from
diffusers.modular_pipelines
import
SequentialPipelineBlocks
from
diffusers.modular_pipelines.stable_diffusion_xl
import
IMAGE2IMAGE_BLOCKS
blocks
=
SequentialPipelineBlocks
.
from_blocks_dict
(
IMAGE2IMAGE_BLOCKS
)
modular_repo_id
=
"YiYiXu/modular-loader-t2i-0704"
pipeline
=
blocks
.
init_pipeline
(
modular_repo_id
)
pipeline
.
load_default_components
(
torch_dtype
=
torch
.
float16
)
pipeline
.
to
(
"cuda"
)
url
=
"https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/diffusers/sdxl-text2img.png"
init_image
=
load_image
(
url
)
prompt
=
"a dog catching a frisbee in the jungle"
image
=
pipeline
(
prompt
=
prompt
,
image
=
init_image
,
strength
=
0.8
,
output
=
"images"
)[
0
]
image
.
save
(
"modular_i2i_out.png"
)
```
</hfoption>
<hfoption
id=
"inpainting"
>
```
py
import
torch
from
diffusers.modular_pipelines
import
SequentialPipelineBlocks
from
diffusers.modular_pipelines.stable_diffusion_xl
import
INPAINT_BLOCKS
from
diffusers.utils
import
load_image
blocks
=
SequentialPipelineBlocks
.
from_blocks_dict
(
INPAINT_BLOCKS
)
modular_repo_id
=
"YiYiXu/modular-loader-t2i-0704"
pipeline
=
blocks
.
init_pipeline
(
modular_repo_id
)
pipeline
.
load_default_components
(
torch_dtype
=
torch
.
float16
)
pipeline
.
to
(
"cuda"
)
img_url
=
"https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/diffusers/sdxl-text2img.png"
mask_url
=
"h
ttps://huggingface.co/datasets/huggingface/documentation-images/resolve/main/diffusers/sdxl-inpaint-mask.png"
init_image
=
load_image
(
img_url
)
mask_image
=
load_image
(
mask_url
)
prompt
=
"A deep sea diver floating"
image
=
pipeline
(
prompt
=
prompt
,
image
=
init_image
,
mask_image
=
mask_image
,
strength
=
0.85
,
output
=
"images"
)[
0
]
image
.
save
(
"moduar_inpaint_out.png"
)
```
</hfoption>
</hfoptions>
本指南将向您展示如何创建一个[
`ModularPipeline`
]并管理其中的组件。
## 添加块
块是[
`InsertableDict`
]对象,可以在特定位置插入,提供了一种灵活的方式来混合和匹配块。
使用[
`~modular_pipelines.modular_pipeline_utils.InsertableDict.insert`
]在块类或
`sub_blocks`
属性上添加一个块。
```
py
# BLOCKS是块类的字典,您需要向其中添加类
BLOCKS
.
insert
(
"block_name"
,
BlockClass
,
index
)
# sub_blocks属性包含实例,向该属性添加一个块实例
t2i_blocks
.
sub_blocks
.
insert
(
"block_name"
,
block_instance
,
index
)
```
使用[
`~modular_pipelines.modular_pipeline_utils.InsertableDict.pop`
]在块类或
`sub_blocks`
属性上移除一个块。
```
py
# 从预设中移除一个块类
BLOCKS
.
pop
(
"text_encoder"
)
# 分离出一个块实例
text_encoder_block
=
t2i_blocks
.
sub_blocks
.
pop
(
"text_encoder"
)
```
通过将现有块设置为新块来交换块。
```
py
# 在预设中替换块类
BLOCKS
[
"prepare_latents"
]
=
CustomPrepareLatents
# 使用块实例在sub_blocks属性中替换
t2i_blocks
.
sub_blocks
[
"prepare_latents"
]
=
CustomPrepareLatents
()
```
## 创建管道
有两种方法可以创建一个[
`ModularPipeline`
]。从[
`ModularPipelineBlocks`
]组装并创建管道,或使用[
`~ModularPipeline.from_pretrained`
]加载现有管道。
您还应该初始化一个[
`ComponentsManager`
]来处理设备放置和内存以及组件管理。
> [!TIP]
> 有关它如何帮助管理不同工作流中的组件的更多详细信息,请参阅[ComponentsManager](./components_manager)文档。
<hfoptions
id=
"create"
>
<hfoption
id=
"ModularPipelineBlocks"
>
使用[
`~ModularPipelineBlocks.init_pipeline`
]方法从组件和配置规范创建一个[
`ModularPipeline`
]。此方法从
`modular_model_index.json`
文件加载
*规范*
,但尚未加载
*模型*
。
```
py
from
diffusers
import
ComponentsManager
from
diffusers.modular_pipelines
import
SequentialPipelineBlocks
from
diffusers.modular_pipelines.stable_diffusion_xl
import
TEXT2IMAGE_BLOCKS
t2i_blocks
=
SequentialPipelineBlocks
.
from_blocks_dict
(
TEXT2IMAGE_BLOCKS
)
modular_repo_id
=
"YiYiXu/modular-loader-t2i-0704"
components
=
ComponentsManager
()
t2i_pipeline
=
t2i_blocks
.
init_pipeline
(
modular_repo_id
,
components_manager
=
components
)
```
</hfoption>
<hfoption
id=
"from_pretrained"
>
[
`~ModularPipeline.from_pretrained`
]方法创建一个[
`ModularPipeline`
]从Hub上的模块化仓库加载。
```
py
from
diffusers
import
ModularPipeline
,
ComponentsManager
components
=
ComponentsManager
()
pipeline
=
ModularPipeline
.
from_pretrained
(
"YiYiXu/modular-loader-t2i-0704"
,
components_manager
=
components
)
```
添加
`trust_remote_code`
参数以加载自定义的[
`ModularPipeline`
]。
```
py
from
diffusers
import
ModularPipeline
,
ComponentsManager
components
=
ComponentsManager
()
modular_repo_id
=
"YiYiXu/modular-diffdiff-0704"
diffdiff_pipeline
=
ModularPipeline
.
from_pretrained
(
modular_repo_id
,
trust_remote_code
=
True
,
components_manager
=
components
)
```
</hfoption>
</hfoptions>
## 加载组件
一个[
`ModularPipeline`
]不会自动实例化组件。它只加载配置和组件规范。您可以使用[
`~ModularPipeline.load_default_components`
]加载所有组件,或仅使用[
`~ModularPipeline.load_components`
]加载特定组件。
<hfoptions
id=
"load"
>
<hfoption
id=
"load_default_components"
>
```
py
import
torch
t2i_pipeline
.
load_default_components
(
torch_dtype
=
torch
.
float16
)
t2i_pipeline
.
to
(
"cuda"
)
```
</hfoption>
<hfoption
id=
"load_components"
>
下面的例子仅加载UNet和VAE。
```
py
import
torch
t2i_pipeline
.
load_components
(
names
=
[
"unet"
,
"vae"
],
torch_dtype
=
torch
.
float16
)
```
</hfoption>
</hfoptions>
打印管道以检查加载的预训练组件。
```
py
t2i_pipeline
```
这应该与管道初始化自的模块化仓库中的
`modular_model_index.json`
文件匹配。如果管道不需要某个组件,即使它在模块化仓库中存在,也不会被包含。
要修改组件加载的来源,编辑仓库中的
`modular_model_index.json`
文件,并将其更改为您希望的加载路径。下面的例子从不同的仓库加载UNet。
```
json
#
原始
"unet"
:
[
null
,
null
,
{
"repo"
:
"stabilityai/stable-diffusion-xl-base-1.0"
,
"subfolder"
:
"unet"
,
"variant"
:
"fp16"
}
]
#
修改后
"unet"
:
[
null
,
null
,
{
"repo"
:
"RunDiffusion/Juggernaut-XL-v9"
,
"subfolder"
:
"unet"
,
"variant"
:
"fp16"
}
]
```
### 组件加载状态
下面的管道属性提供了关于哪些组件被加载的更多信息。
使用
`component_names`
返回所有预期的组件。
```
py
t2i_pipeline
.
component_names
[
'text_encoder'
,
'text_encoder_2'
,
'tokenizer'
,
'tokenizer_2'
,
'guider'
,
'scheduler'
,
'unet'
,
'vae'
,
'image_processor'
]
```
使用
`null_component_names`
返回尚未加载的组件。使用[
`~ModularPipeline.from_pretrained`
]加载这些组件。
```
py
t2i_pipeline
.
null_component_names
[
'text_encoder'
,
'text_encoder_2'
,
'tokenizer'
,
'tokenizer_2'
,
'scheduler'
]
```
使用
`pretrained_component_names`
返回将从预训练模型加载的组件。
```
py
t2i_pipeline
.
pretrained_component_names
[
'text_encoder'
,
'text_encoder_2'
,
'tokenizer'
,
'tokenizer_2'
,
'scheduler'
,
'unet'
,
'vae'
]
```
使用
`config_component_names`
返回那些使用默认配置创建的组件(不是从模块化仓库加载的)。来自配置的组件不包括在内,因为它们已经在管道创建期间初始化。这就是为什么它们没有列在
`null_component_names`
中。
```
py
t2i_pipeline
.
config_component_names
[
'guider'
,
'image_processor'
]
```
## 更新组件
根据组件是
*预训练组件*
还是
*配置组件*
,组件可能会被更新。
> [!WARNING]
> 在更新组件时,组件可能会从预训练变为配置。组件类型最初是在块的 `expected_components` 字段中定义的。
预训练组件通过 [
`ComponentSpec`
] 更新,而配置组件则通过直接传递对象或使用 [
`ComponentSpec`
] 更新。
[
`ComponentSpec`
] 对于预训练组件显示
`default_creation_method="from_pretrained"`
,对于配置组件显示
`default_creation_method="from_config`
。
要更新预训练组件,创建一个 [
`ComponentSpec`
],指定组件的名称和从哪里加载它。使用 [
`~ComponentSpec.load`
] 方法来加载组件。
```
py
from
diffusers
import
ComponentSpec
,
UNet2DConditionModel
unet_spec
=
ComponentSpec
(
name
=
"unet"
,
type_hint
=
UNet2DConditionModel
,
repo
=
"stabilityai/stable-diffusion-xl-base-1.0"
,
subfolder
=
"unet"
,
variant
=
"fp16"
)
unet
=
unet_spec
.
load
(
torch_dtype
=
torch
.
float16
)
```
[
`~ModularPipeline.update_components`
] 方法用一个新的组件替换原来的组件。
```
py
t2i_pipeline
.
update_components
(
unet
=
unet2
)
```
当组件被更新时,加载规范也会在管道配置中更新。
### 组件提取和修改
当你使用 [
`~ComponentSpec.load`
] 时,新组件保持其加载规范。这使得提取规范并重新创建组件成为可能。
```
py
spec
=
ComponentSpec
.
from_component
(
"unet"
,
unet2
)
spec
ComponentSpec
(
name
=
'unet'
,
type_hint
=<
class
'
diffusers
.
models
.
unets
.
unet_2d_condition
.
UNet2DConditionModel
'>, description=None, config=None, repo='
stabilityai
/
stable
-
diffusion
-
xl
-
base
-
1.0
', subfolder='
unet
', variant='
fp16
', revision=None, default_creation_method='
from_pretrained
')
unet2_recreated = spec.load(torch_dtype=torch.float16)
```
[
`~ModularPipeline.get_component_spec`
] 方法获取当前组件规范的副本以进行修改或更新。
```
py
unet_spec
=
t2i_pipeline
.
get_component_spec
(
"unet"
)
unet_spec
ComponentSpec
(
name
=
'unet'
,
type_hint
=<
class
'
diffusers
.
models
.
unets
.
unet_2d_condition
.
UNet2DConditionModel
'>,
repo='
RunDiffusion
/
Juggernaut
-
XL
-
v9
',
subfolder='
unet
',
variant='
fp16
',
default_creation_method='
from_pretrained
'
)
# 修改以从不同的仓库加载
unet_spec.repo = "stabilityai/stable-diffusion-xl-base-1.0"
# 使用修改后的规范加载组件
unet = unet_spec.load(torch_dtype=torch.float16)
```
## 模块化仓库
一个仓库
如果管道块使用
*预训练组件*
,则需要y。该存储库提供了加载规范和元数据。
[
`ModularPipeline`]特别需要*模块化存储库*(参见[示例存储库
](
https://huggingface.co/YiYiXu/modular-diffdiff
)
),这比典型的存储库更灵活。它包含一个
`modular_model_index.json`
文件,包含以下3个元素。
-
`library`
和
`class`
显示组件是从哪个库加载的及其类。如果是
`null`
,则表示组件尚未加载。
-
`loading_specs_dict`
包含加载组件所需的信息,例如从中加载的存储库和子文件夹。
与标准存储库不同,模块化存储库可以根据
`loading_specs_dict`
从不同的存储库获取组件。组件不需要存在于同一个存储库中。
模块化存储库可能包含用于加载[
`ModularPipeline`
]的自定义代码。这允许您使用不是Diffusers原生的专用块。
```
modular-diffdiff-0704/
├── block.py # 自定义管道块实现
├── config.json # 管道配置和auto_map
└── modular_model_index.json # 组件加载规范
```
[
config.json
](
https://huggingface.co/YiYiXu/modular-diffdiff-0704/blob/main/config.json
)
文件包含一个
`auto_map`
键,指向
`block.py`
中定义自定义块的位置。
```
json
{
"_class_name"
:
"DiffDiffBlocks"
,
"auto_map"
:
{
"ModularPipelineBlocks"
:
"block.DiffDiffBlocks"
}
}
```
docs/source/zh/modular_diffusers/overview.md
0 → 100644
View file @
f868d4b5
<!--版权所有 2025 The HuggingFace Team。保留所有权利。
根据Apache许可证2.0版("许可证")授权;除非符合许可证,否则不得使用此文件。您可以在以下位置获取许可证的副本:
http://www.apache.org/licenses/LICENSE-2.0
除非适用法律要求或书面同意,根据许可证分发的软件按"原样"分发,无任何明示或暗示的担保或条件。有关许可证下特定语言的权限和限制,请参阅许可证。
-->
# 概述
> [!WARNING]
> 模块化Diffusers正在积极开发中,其API可能会发生变化。
模块化Diffusers是一个统一的管道系统,通过
*管道块*
简化您的工作流程。
-
块是可重用的,您只需要为您的管道创建独特的块。
-
块可以混合搭配,以适应或为特定工作流程或多个工作流程创建管道。
模块化Diffusers文档的组织如下所示。
## 快速开始
-
一个
[
快速开始
](
./quickstart
)
演示了如何使用模块化Diffusers实现一个示例工作流程。
## ModularPipelineBlocks
-
[
States
](
./modular_diffusers_states
)
解释了数据如何在块和[
`ModularPipeline`
]之间共享和通信。
-
[
ModularPipelineBlocks
](
./pipeline_block
)
是[
`ModularPipeline`
]最基本的单位,本指南向您展示如何创建一个。
-
[
SequentialPipelineBlocks
](
./sequential_pipeline_blocks
)
是一种类型的块,它将多个块链接起来,使它们一个接一个地运行,沿着链传递数据。本指南向您展示如何创建[
`~modular_pipelines.SequentialPipelineBlocks`
]以及它们如何连接和一起工作。
-
[
LoopSequentialPipelineBlocks
](
./loop_sequential_pipeline_blocks
)
是一种类型的块,它在循环中运行一系列块。本指南向您展示如何创建[
`~modular_pipelines.LoopSequentialPipelineBlocks`
]。
-
[
AutoPipelineBlocks
](
./auto_pipeline_blocks
)
是一种类型的块,它根据输入自动选择要运行的块。本指南向您展示如何创建[
`~modular_pipelines.AutoPipelineBlocks`
]。
## ModularPipeline
-
[
ModularPipeline
](
./modular_pipeline
)
向您展示如何创建并将管道块转换为可执行的[
`ModularPipeline`
]。
-
[
ComponentsManager
](
./components_manager
)
向您展示如何跨多个管道管理和重用组件。
-
[
Guiders
](
./guiders
)
向您展示如何在管道中使用不同的指导方法。
docs/source/zh/modular_diffusers/pipeline_block.md
0 → 100644
View file @
f868d4b5
<!--版权 2025 The HuggingFace Team。保留所有权利。
根据Apache许可证2.0版(“许可证”)授权;除非符合许可证,否则不得使用此文件。您可以在
http://www.apache.org/licenses/LICENSE-2.0
获取许可证的副本。
除非适用法律要求或书面同意,根据许可证分发的软件是基于“按原样”基础分发的,没有任何明示或暗示的保证或条件。请参阅许可证了解特定语言管理权限和限制。
-->
# ModularPipelineBlocks
[
`~modular_pipelines.ModularPipelineBlocks`] 是构建 [`ModularPipeline`] 的基本块。它定义了管道中特定步骤应执行的组件、输入/输出和计算。一个 [`~modular_pipelines.ModularPipelineBlocks`] 与其他块连接,使用 [状态
](
./modular_diffusers_states
)
,以实现工作流的模块化构建。
单独的 [
`~modular_pipelines.ModularPipelineBlocks`
] 无法执行。它是管道中步骤应执行的操作的蓝图。要实际运行和执行管道,需要将 [
`~modular_pipelines.ModularPipelineBlocks`
] 转换为 [
`ModularPipeline`
]。
本指南将向您展示如何创建 [
`~modular_pipelines.ModularPipelineBlocks`
]。
## 输入和输出
> [!TIP]
> 如果您不熟悉Modular Diffusers中状态的工作原理,请参考 [States](./modular_diffusers_states) 指南。
一个 [
`~modular_pipelines.ModularPipelineBlocks`
] 需要
`inputs`
和
`intermediate_outputs`
。
-
`inputs`
是由用户提供并从 [
`~modular_pipelines.PipelineState`
] 中检索的值。这很有用,因为某些工作流会调整图像大小,但仍需要原始图像。 [
`~modular_pipelines.PipelineState`
] 维护原始图像。
使用 `InputParam` 定义 `inputs`。
```py
from diffusers.modular_pipelines import InputParam
user_inputs = [
InputParam(name="image", type_hint="PIL.Image", description="要处理的原始输入图像")
]
```
-
`intermediate_inputs`
通常由前一个块创建的值,但如果前面的块没有生成它们,也可以直接提供。与
`inputs`
不同,
`intermediate_inputs`
可以被修改。
使用 `InputParam` 定义 `intermediate_inputs`。
```py
user_intermediate_inputs = [
InputParam(name="processed_image", type_hint="torch.Tensor", description="image that has been preprocessed and normalized"),
]
```
-
`intermediate_outputs`
是由块创建并添加到 [
`~modular_pipelines.PipelineState`
] 的新值。
`intermediate_outputs`
可作为后续块的
`intermediate_inputs`
使用,或作为运行管道的最终输出使用。
使用 `OutputParam` 定义 `intermediate_outputs`。
```py
from diffusers.modular_pipelines import OutputParam
user_intermediate_outputs = [
OutputParam(name="image_latents", description="latents representing the image")
]
```
中间输入和输出共享数据以连接块。它们可以在任何时候访问,允许你跟踪工作流的进度。
## 计算逻辑
一个块执行的计算在
`__call__`
方法中定义,它遵循特定的结构。
1.
检索[
`~modular_pipelines.BlockState`
]以获取
`inputs`
和
`intermediate_inputs`
的局部视图。
2.
在
`inputs`
和
`intermediate_inputs`
上实现计算逻辑。
3.
更新[
`~modular_pipelines.PipelineState`
]以将局部[
`~modular_pipelines.BlockState`
]的更改推送回全局[
`~modular_pipelines.PipelineState`
]。
4.
返回对下一个块可用的组件和状态。
```
py
def
__call__
(
self
,
components
,
state
):
# 获取该块需要的状态变量的局部视图
block_state
=
self
.
get_block_state
(
state
)
# 你的计算逻辑在这里
# block_state包含你所有的inputs和intermediate_inputs
# 像这样访问它们: block_state.image, block_state.processed_image
# 用你更新的block_states更新管道状态
self
.
set_block_state
(
state
,
block_state
)
return
components
,
state
```
### 组件和配置
块需要的组件和管道级别的配置在[
`ComponentSpec`
]和[
`~modular_pipelines.ConfigSpec`
]中指定。
-
[
`ComponentSpec`
]包含块使用的预期组件。你需要组件的
`name`
和理想情况下指定组件确切是什么的
`type_hint`
。
-
[
`~modular_pipelines.ConfigSpec`
]包含控制所有块行为的管道级别设置。
```
py
from
diffusers
import
ComponentSpec
,
ConfigSpec
expected_components
=
[
ComponentSpec
(
name
=
"unet"
,
type_hint
=
UNet2DConditionModel
),
ComponentSpec
(
name
=
"scheduler"
,
type_hint
=
EulerDiscreteScheduler
)
]
expected_config
=
[
ConfigSpec
(
"force_zeros_for_empty_prompt"
,
True
)
]
```
当块被转换为管道时,组件作为
`__call__`
中的第一个参数对块可用。
```
py
def
__call__
(
self
,
components
,
state
):
# 使用点符号访问组件
unet
=
components
.
unet
vae
=
components
.
vae
scheduler
=
components
.
scheduler
```
docs/source/zh/modular_diffusers/quickstart.md
0 → 100644
View file @
f868d4b5
<!--版权所有 2025 The HuggingFace Team。保留所有权利。
根据Apache许可证2.0版("许可证")授权;除非符合许可证,否则不得使用此文件。您可以在
http://www.apache.org/licenses/LICENSE-2.0
获取许可证的副本。
除非适用法律要求或书面同意,根据许可证分发的软件按"原样"分发,无任何明示或暗示的担保或条件。有关许可证下特定语言的管理权限和限制,请参阅许可证。
-->
# 快速入门
模块化Diffusers是一个快速构建灵活和可定制管道的框架。模块化Diffusers的核心是[
`ModularPipelineBlocks`
],可以与其他块组合以适应新的工作流程。这些块被转换为[
`ModularPipeline`
],一个开发者可以使用的友好用户界面。
本文档将向您展示如何使用模块化框架实现
[
Differential Diffusion
](
https://differential-diffusion.github.io/
)
管道。
## ModularPipelineBlocks
[
`ModularPipelineBlocks`
]是
*定义*
,指定管道中单个步骤的组件、输入、输出和计算逻辑。有四种类型的块。
-
[
`ModularPipelineBlocks`
]是最基本的单一步骤块。
-
[
`SequentialPipelineBlocks`
]是一个多块,线性组合其他块。一个块的输出是下一个块的输入。
-
[
`LoopSequentialPipelineBlocks`
]是一个多块,迭代运行,专为迭代工作流程设计。
-
[
`AutoPipelineBlocks`
]是一个针对不同工作流程的块集合,它根据输入选择运行哪个块。它旨在方便地将多个工作流程打包到单个管道中。
[
Differential Diffusion
](
https://differential-diffusion.github.io/
)
是一个图像到图像的工作流程。从
`IMAGE2IMAGE_BLOCKS`
预设开始,这是一个用于图像到图像生成的
`ModularPipelineBlocks`
集合。
```
py
from
diffusers.modular_pipelines.stable_diffusion_xl
import
IMAGE2IMAGE_BLOCKS
IMAGE2IMAGE_BLOCKS
=
InsertableDict
([
(
"text_encoder"
,
StableDiffusionXLTextEncoderStep
),
(
"image_encoder"
,
StableDiffusionXLVaeEncoderStep
),
(
"input"
,
StableDiffusionXLInputStep
),
(
"set_timesteps"
,
StableDiffusionXLImg2ImgSetTimestepsStep
),
(
"prepare_latents"
,
StableDiffusionXLImg2ImgPrepareLatentsStep
),
(
"prepare_add_cond"
,
StableDiffusionXLImg2ImgPrepareAdditionalConditioningStep
),
(
"denoise"
,
StableDiffusionXLDenoiseStep
),
(
"decode"
,
StableDiffusionXLDecodeStep
)
])
```
## 管道和块状态
模块化Diffusers使用
*状态*
在块之间通信数据。有两种类型的状态。
-
[
`PipelineState`
]是一个全局状态,可用于跟踪所有块的所有输入和输出。
-
[
`BlockState`
]是[
`PipelineState`
]中相关变量的局部视图,用于单个块。
## 自定义块
[
Differential Diffusion
](
https://differential-diffusion.github.io/
)
与标准的图像到图像转换在其
`prepare_latents`
和
`denoise`
块上有所不同。所有其他块都可以重用,但你需要修改这两个。
通过复制和修改现有的块,为
`prepare_latents`
和
`denoise`
创建占位符
`ModularPipelineBlocks`
。
打印
`denoise`
块,可以看到它由 [
`LoopSequentialPipelineBlocks`
] 组成,包含三个子块,
`before_denoiser`
、
`denoiser`
和
`after_denoiser`
。只需要修改
`before_denoiser`
子块,根据变化图为去噪器准备潜在输入。
```
py
denoise_blocks
=
IMAGE2IMAGE_BLOCKS
[
"denoise"
]()
print
(
denoise_blocks
)
```
用新的
`SDXLDiffDiffLoopBeforeDenoiser`
块替换
`StableDiffusionXLLoopBeforeDenoiser`
子块。
```
py
# 复制现有块作为占位符
class
SDXLDiffDiffPrepareLatentsStep
(
ModularPipelineBlocks
):
"""Copied from StableDiffusionXLImg2ImgPrepareLatentsStep - will modify later"""
# ... 与 StableDiffusionXLImg2ImgPrepareLatentsStep 相同的实现
class
SDXLDiffDiffDenoiseStep
(
StableDiffusionXLDenoiseLoopWrapper
):
block_classes
=
[
SDXLDiffDiffLoopBeforeDenoiser
,
StableDiffusionXLLoopDenoiser
,
StableDiffusionXLLoopAfterDenoiser
]
block_names
=
[
"before_denoiser"
,
"denoiser"
,
"after_denoiser"
]
```
### prepare_latents
`prepare_latents`
块需要进行以下更改。
-
一个处理器来处理变化图
-
一个新的
`inputs`
来接受用户提供的变化图,
`timestep`
用于预计算所有潜在变量和
`num_inference_steps`
来创建更新图像区域的掩码
-
更新
`__call__`
方法中的计算,用于处理变化图和创建掩码,并将其存储在 [
`BlockState`
] 中
```
diff
class SDXLDiffDiffPrepareLatentsStep(ModularPipelineBlocks):
@property
def expected_components(self) -> List[ComponentSpec]:
return [
ComponentSpec("vae", AutoencoderKL),
ComponentSpec("scheduler", EulerDiscreteScheduler),
+ ComponentSpec("mask_processor", VaeImageProcessor, config=FrozenDict({"do_normalize": False, "do_convert_grayscale": True}))
]
@property
def inputs(self) -> List[Tuple[str, Any]]:
return [
InputParam("generator"),
+ InputParam("diffdiff_map", required=True),
- InputParam("latent_timestep", required=True, type_hint=torch.Tensor),
+ InputParam("timesteps", type_hint=torch.Tensor),
+ InputParam("num_inference_steps", type_hint=int),
]
@property
def intermediate_outputs(self) -> List[OutputParam]:
return [
+ OutputParam("original_latents", type_hint=torch.Tensor),
+ OutputParam("diffdiff_masks", type_hint=torch.Tensor),
]
def __call__(self, components, state: PipelineState):
# ... existing logic ...
+ # Process change map and create masks
+ diffdiff_map = components.mask_processor.preprocess(block_state.diffdiff_map, height=latent_height, width=latent_width)
+ thresholds = torch.arange(block_state.num_inference_steps, dtype=diffdiff_map.dtype) / block_state.num_inference_steps
+ block_state.diffdiff_masks = diffdiff_map > (thresholds + (block_state.denoising_start or 0))
+ block_state.original_latents = block_state.latents
```
### 去噪
`before_denoiser`
子块需要进行以下更改。
-
新的
`inputs`
以接受
`denoising_start`
参数,
`original_latents`
和
`diffdiff_masks`
来自
`prepare_latents`
块
-
更新
`__call__`
方法中的计算以应用 Differential Diffusion
```
diff
class SDXLDiffDiffLoopBeforeDenoiser(ModularPipelineBlocks):
@property
def description(self) -> str:
return (
"Step within the denoising loop for differential diffusion that prepare the latent input for the denoiser"
)
@property
def inputs(self) -> List[str]:
return [
InputParam("latents", required=True, type_hint=torch.Tensor),
+ InputParam("denoising_start"),
+ InputParam("original_latents", type_hint=torch.Tensor),
+ InputParam("diffdiff_masks", type_hint=torch.Tensor),
]
def __call__(self, components, block_state, i, t):
+ # Apply differential diffusion logic
+ if i == 0 and block_state.denoising_start is None:
+ block_state.latents = block_state.original_latents[:1]
+ else:
+ block_state.mask = block_state.diffdiff_masks[i].unsqueeze(0).unsqueeze(1)
+ block_state.latents = block_state.original_latents[i] * block_state.mask + block_state.latents * (1 - block_state.mask)
# ... rest of existing logic ...
```
## 组装块
此时,您应该拥有创建 [
`ModularPipeline`
] 所需的所有块。
复制现有的
`IMAGE2IMAGE_BLOCKS`
预设,对于
`set_timesteps`
块,使用
`TEXT2IMAGE_BLOCKS`
中的
`set_timesteps`
,因为 Differential Diffusion 不需要
`strength`
参数。
将
`prepare_latents`
和
`denoise`
块设置为您刚刚修改的
`SDXLDiffDiffPrepareLatentsStep`
和
`SDXLDiffDiffDenoiseStep`
块。
调用 [
`SequentialPipelineBlocks.from_blocks_dict`
] 在块上创建一个
`SequentialPipelineBlocks`
。
```
py
DIFFDIFF_BLOCKS
=
IMAGE2IMAGE_BLOCKS
.
copy
()
DIFFDIFF_BLOCKS
[
"set_timesteps"
]
=
TEXT2IMAGE_BLOCKS
[
"set_timesteps"
]
DIFFDIFF_BLOCKS
[
"prepare_latents"
]
=
SDXLDiffDiffPrepareLatentsStep
DIFFDIFF_BLOCKS
[
"denoise"
]
=
SDXLDiffDiffDenoiseStep
dd_blocks
=
SequentialPipelineBlocks
.
from_blocks_dict
(
DIFFDIFF_BLOCKS
)
print
(
dd_blocks
)
```
## ModularPipeline
将 [
`SequentialPipelineBlocks`
] 转换为 [
`ModularPipeline`
],使用 [
`ModularPipeline.init_pipeline`
] 方法。这会初始化从
`modular_model_index.json`
文件加载的预期组件。通过调用 [
`ModularPipeline.load_defau
lt_components`
]。
初始化[
`ComponentManager`
]时传入pipeline是一个好主意,以帮助管理不同的组件。一旦调用[
`~ModularPipeline.load_default_components`
],组件就会被注册到[
`ComponentManager`
]中,并且可以在工作流之间共享。下面的例子使用
`collection`
参数为组件分配了一个
`"diffdiff"`
标签,以便更好地组织。
```
py
from
diffusers.modular_pipelines
import
ComponentsManager
components
=
ComponentManager
()
dd_pipeline
=
dd_blocks
.
init_pipeline
(
"YiYiXu/modular-demo-auto"
,
components_manager
=
components
,
collection
=
"diffdiff"
)
dd_pipeline
.
load_default_componenets
(
torch_dtype
=
torch
.
float16
)
dd_pipeline
.
to
(
"cuda"
)
```
## 添加工作流
可以向[
`ModularPipeline`
]添加其他工作流以支持更多功能,而无需从头重写整个pipeline。
本节演示如何添加IP-Adapter或ControlNet。
### IP-Adapter
Stable Diffusion XL已经有一个预设的IP-Adapter块,你可以使用,并且不需要对现有的Differential Diffusion pipeline进行任何更改。
```
py
from
diffusers.modular_pipelines.stable_diffusion_xl.encoders
import
StableDiffusionXLAutoIPAdapterStep
ip_adapter_block
=
StableDiffusionXLAutoIPAdapterStep
()
```
使用[
`sub_blocks.insert`
]方法将其插入到[
`ModularPipeline`
]中。下面的例子在位置
`0`
插入了
`ip_adapter_block`
。打印pipeline可以看到
`ip_adapter_block`
被添加了,并且它需要一个
`ip_adapter_image`
。这也向pipeline添加了两个组件,
`image_encoder`
和
`feature_extractor`
。
```
py
dd_blocks
.
sub_blocks
.
insert
(
"ip_adapter"
,
ip_adapter_block
,
0
)
```
调用[
`~ModularPipeline.init_pipeline`
]来初始化一个[
`ModularPipeline`
],并使用[
`~ModularPipeline.load_default_components`
]加载模型组件。加载并设置IP-Adapter以运行pipeline。
```
py
dd_pipeline
=
dd_blocks
.
init_pipeline
(
"YiYiXu/modular-demo-auto"
,
collection
=
"diffdiff"
)
dd_pipeline
.
load_default_components
(
torch_dtype
=
torch
.
float16
)
dd_pipeline
.
loader
.
load_ip_adapter
(
"h94/IP-Adapter"
,
subfolder
=
"sdxl_models"
,
weight_name
=
"ip-adapter_sdxl.bin"
)
dd_pipeline
.
loader
.
set_ip_adapter_scale
(
0.6
)
dd_pipeline
=
dd_pipeline
.
to
(
device
)
ip_adapter_image
=
load_image
(
"https://huggingface.co/datasets/YiYiXu/testing-images/resolve/main/diffdiff_orange.jpeg"
)
image
=
load_image
(
"https://huggingface.co/datasets/OzzyGT/testing-resources/resolve/main/differential/20240329211129_4024911930.png?download=true"
)
mask
=
load_image
(
"https://huggingface.co/datasets/OzzyGT/testing-resources/resolve/main/differential/gradient_mask.png?download=true"
)
prompt
=
"a green pear"
negative_prompt
=
"blurry"
generator
=
torch
.
Generator
(
device
=
device
).
manual_seed
(
42
)
image
=
dd_pipeline
(
prompt
=
prompt
,
negative_prompt
=
negative_prompt
,
num_inference_steps
=
25
,
generator
=
generator
,
ip_adapter_image
=
ip_adapter_image
,
diffdiff_map
=
mask
,
image
=
image
,
output
=
"images"
)[
0
]
```
### ControlNet
Stable Diffusion XL 已经预设了一个可以立即使用的 ControlNet 块。
```
py
from
diffusers.modular_pipelines.stable_diffusion_xl.modular_blocks
import
StableDiffusionXLAutoControlNetInputStep
control_input_block
=
StableDiffusionXLAutoControlNetInputStep
()
```
然而,它需要修改
`denoise`
块,因为那是 ControlNet 将控制信息注入到 UNet 的地方。
通过将
`StableDiffusionXLLoopDenoiser`
子块替换为
`StableDiffusionXLControlNetLoopDenoiser`
来修改
`denoise`
块。
```
py
class
SDXLDiffDiffControlNetDenoiseStep
(
StableDiffusionXLDenoiseLoopWrapper
):
block_classes
=
[
SDXLDiffDiffLoopBeforeDenoiser
,
StableDiffusionXLControlNetLoopDenoiser
,
StableDiffusionXLDenoiseLoopAfterDenoiser
]
block_names
=
[
"before_denoiser"
,
"denoiser"
,
"after_denoiser"
]
controlnet_denoise_block
=
SDXLDiffDiffControlNetDenoiseStep
()
```
插入
`controlnet_input`
块并用新的
`controlnet_denoise_block`
替换
`denoise`
块。初始化一个 [
`ModularPipeline`
] 并将 [
`~ModularPipeline.load_default_components`
] 加载到其中。
```
py
dd_blocks
.
sub_blocks
.
insert
(
"controlnet_input"
,
control_input_block
,
7
)
dd_blocks
.
sub_blocks
[
"denoise"
]
=
controlnet_denoise_block
dd_pipeline
=
dd_blocks
.
init_pipeline
(
"YiYiXu/modular-demo-auto"
,
collection
=
"diffdiff"
)
dd_pipeline
.
load_default_components
(
torch_dtype
=
torch
.
float16
)
dd_pipeline
=
dd_pipeline
.
to
(
device
)
control_image
=
load_image
(
"https://huggingface.co/datasets/YiYiXu/testing-images/resolve/main/diffdiff_tomato_canny.jpeg"
)
image
=
load_image
(
"https://huggingface.co/datasets/OzzyGT/testing-resources/resolve/main/differential/20240329211129_4024911930.png?download=true"
)
mask
=
load_image
(
"https://huggingface.co/datasets/OzzyGT/testing-resources/resolve/main/differential/gradient_mask.png?download=true"
)
prompt
=
"a green pear"
negative_prompt
=
"blurry"
generator
=
torch
.
Generator
(
device
=
device
).
manual_seed
(
42
)
image
=
dd_pipeline
(
prompt
=
prompt
,
negative_prompt
=
negative_prompt
,
num_inference_steps
=
25
,
generator
=
generator
,
control_image
=
control_image
,
controlnet_conditioning_scale
=
0.5
,
diffdiff_map
=
mask
,
image
=
image
,
output
=
"images"
)[
0
]
```
### AutoPipelineBlocks
差分扩散、IP-Adapter 和 ControlNet 工作流可以通过使用 [
`AutoPipelineBlocks`
] 捆绑到一个单一的 [
`ModularPipeline`
] 中。这允许根据输入如
`control_image`
或
`ip_adapter_image`
自动选择要运行的子块。如果没有传递这些输入,则默认为差分扩散。
使用
`block_trigger_inputs`
仅在提供
`control_image`
输入时运行
`SDXLDiffDiffControlNetDenoiseStep`
块。否则,使用
`SDXLDiffDiffDenoiseStep`
。
```
py
class
SDXLDiffDiffAutoDenoiseStep
(
AutoPipelineBlocks
):
block_classes
=
[
SDXLDiffDiffControlNetDenoiseStep
,
SDXLDiffDiffDenoiseStep
]
block_names
=
[
"contr
olnet_denoise"
,
"denoise"
]
block_trigger_inputs
=
[
"controlnet_cond"
,
None
]
```
添加
`ip_adapter`
和
`controlnet_input`
块。
```
py
DIFFDIFF_AUTO_BLOCKS
=
IMAGE2IMAGE_BLOCKS
.
copy
()
DIFFDIFF_AUTO_BLOCKS
[
"prepare_latents"
]
=
SDXLDiffDiffPrepareLatentsStep
DIFFDIFF_AUTO_BLOCKS
[
"set_timesteps"
]
=
TEXT2IMAGE_BLOCKS
[
"set_timesteps"
]
DIFFDIFF_AUTO_BLOCKS
[
"denoise"
]
=
SDXLDiffDiffAutoDenoiseStep
DIFFDIFF_AUTO_BLOCKS
.
insert
(
"ip_adapter"
,
StableDiffusionXLAutoIPAdapterStep
,
0
)
DIFFDIFF_AUTO_BLOCKS
.
insert
(
"controlnet_input"
,
StableDiffusionXLControlNetAutoInput
,
7
)
```
调用 [
`SequentialPipelineBlocks.from_blocks_dict`
] 来创建一个 [
`SequentialPipelineBlocks`
] 并创建一个 [
`ModularPipeline`
] 并加载模型组件以运行。
```
py
dd_auto_blocks
=
SequentialPipelineBlocks
.
from_blocks_dict
(
DIFFDIFF_AUTO_BLOCKS
)
dd_pipeline
=
dd_auto_blocks
.
init_pipeline
(
"YiYiXu/modular-demo-auto"
,
collection
=
"diffdiff"
)
dd_pipeline
.
load_default_components
(
torch_dtype
=
torch
.
float16
)
```
## 分享
使用 [
`~ModularPipeline.save_pretrained`
] 将您的 [
`ModularPipeline`
] 添加到 Hub,并将
`push_to_hub`
参数设置为
`True`
。
```
py
dd_pipeline
.
save_pretrained
(
"YiYiXu/test_modular_doc"
,
push_to_hub
=
True
)
```
其他用户可以使用 [
`~ModularPipeline.from_pretrained`
] 加载 [
`ModularPipeline`
]。
```
py
import
torch
from
diffusers.modular_pipelines
import
ModularPipeline
,
ComponentsManager
components
=
ComponentsManager
()
diffdiff_pipeline
=
ModularPipeline
.
from_pretrained
(
"YiYiXu/modular-diffdiff-0704"
,
trust_remote_code
=
True
,
components_manager
=
components
,
collection
=
"diffdiff"
)
diffdiff_pipeline
.
load_default_components
(
torch_dtype
=
torch
.
float16
)
```
docs/source/zh/modular_diffusers/sequential_pipeline_blocks.md
0 → 100644
View file @
f868d4b5
<!--版权 2025 The HuggingFace Team。保留所有权利。
根据Apache许可证2.0版("许可证")授权;除非符合许可证,否则不得使用此文件。您可以在
http://www.apache.org/licenses/LICENSE-2.0
获取许可证的副本。
除非适用法律要求或书面同意,根据许可证分发的软件是基于"按原样"基础分发的,没有任何形式的明示或暗示的保证或条件。有关许可证下特定语言的管理权限和限制,请参阅许可证。
-->
# 顺序管道块
[
`~modular_pipelines.SequentialPipelineBlocks`
] 是一种多块类型,它将其他 [
`~modular_pipelines.ModularPipelineBlocks`
] 按顺序组合在一起。数据通过
`intermediate_inputs`
和
`intermediate_outputs`
线性地从一个块流向下一个块。[
`~modular_pipelines.SequentialPipelineBlocks`
] 中的每个块通常代表管道中的一个步骤,通过组合它们,您逐步构建一个管道。
本指南向您展示如何将两个块连接成一个 [
`~modular_pipelines.SequentialPipelineBlocks`
]。
创建两个 [
`~modular_pipelines.ModularPipelineBlocks`
]。第一个块
`InputBlock`
输出一个
`batch_size`
值,第二个块
`ImageEncoderBlock`
使用
`batch_size`
作为
`intermediate_inputs`
。
<hfoptions
id=
"sequential"
>
<hfoption
id=
"InputBlock"
>
```
py
from
diffusers.modular_pipelines
import
ModularPipelineBlocks
,
InputParam
,
OutputParam
class
InputBlock
(
ModularPipelineBlocks
):
@
property
def
inputs
(
self
):
return
[
InputParam
(
name
=
"prompt"
,
type_hint
=
list
,
description
=
"list of text prompts"
),
InputParam
(
name
=
"num_images_per_prompt"
,
type_hint
=
int
,
description
=
"number of images per prompt"
),
]
@
property
def
intermediate_outputs
(
self
):
return
[
OutputParam
(
name
=
"batch_size"
,
description
=
"calculated batch size"
),
]
@
property
def
description
(
self
):
return
"A block that determines batch_size based on the number of prompts and num_images_per_prompt argument."
def
__call__
(
self
,
components
,
state
):
block_state
=
self
.
get_block_state
(
state
)
batch_size
=
len
(
block_state
.
prompt
)
block_state
.
batch_size
=
batch_size
*
block_state
.
num_images_per_prompt
self
.
set_block_state
(
state
,
block_state
)
return
components
,
state
```
</hfoption>
<hfoption
id=
"ImageEncoderBlock"
>
```
py
import
torch
from
diffusers.modular_pipelines
import
ModularPipelineBlocks
,
InputParam
,
OutputParam
class
ImageEncoderBlock
(
ModularPipelineBlocks
):
@
property
def
inputs
(
self
):
return
[
InputParam
(
name
=
"image"
,
type_hint
=
"PIL.Image"
,
description
=
"raw input image to process"
),
InputParam
(
name
=
"batch_size"
,
type_hint
=
int
),
]
@
property
def
intermediate_outputs
(
self
):
return
[
OutputParam
(
name
=
"image_latents"
,
description
=
"latents representing the image"
]
@
property
def
description
(
self
):
return
"Encode raw image into its latent presentation"
def
__call__
(
self
,
components
,
state
):
block_state
=
self
.
get_block_state
(
state
)
# 模拟处理图像
# 这将改变所有块的图像状态,从PIL图像变为张量
block_state
.
image
=
torch
.
randn
(
1
,
3
,
512
,
512
)
block_state
.
batch_size
=
block_state
.
batch_size
*
2
block_state
.
image_latents
=
torch
.
randn
(
1
,
4
,
64
,
64
)
self
.
set_block_state
(
state
,
block_state
)
return
components
,
state
```
</hfoption>
</hfoptions>
通过定义一个[
`InsertableDict`
]来连接两个块,将块名称映射到块实例。块按照它们在
`blocks_dict`
中注册的顺序执行。
使用[
`~modular_pipelines.SequentialPipelineBlocks.from_blocks_dict`
]来创建一个[
`~modular_pipelines.SequentialPipelineBlocks`
]。
```
py
from
diffusers.modular_pipelines
import
SequentialPipelineBlocks
,
InsertableDict
blocks_dict
=
InsertableDict
()
blocks_dict
[
"input"
]
=
input_block
blocks_dict
[
"image_encoder"
]
=
image_encoder_block
blocks
=
SequentialPipelineBlocks
.
from_blocks_dict
(
blocks_dict
)
```
通过调用
`blocks`
来检查[
`~modular_pipelines.SequentialPipelineBlocks`
]中的子块,要获取更多关于输入和输出的详细信息,可以访问
`docs`
属性。
```
py
print
(
blocks
)
print
(
blocks
.
doc
)
```
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment