Skip to content

Commit 6243041

Browse files
committed
update
1 parent b268df2 commit 6243041

File tree

1 file changed

+35
-8
lines changed

1 file changed

+35
-8
lines changed

docs/source/en/using-diffusers/modular.md

Lines changed: 35 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,35 +12,62 @@ specific language governing permissions and limitations under the License.
1212

1313
# Modular Diffusers
1414

15-
Modular Diffusers is a unified pipeline that greatly simplifies how you work with diffusion models. There are two main advantages of using modular Diffusers:
15+
Modular Diffusers is a unified pipeline that simplifies how you work with diffusion models. There are two main advantages of using modular Diffusers:
1616

17-
* Avoid rewriting an entire pipeline from scratch. Reuse existing blocks and only create new blocks for the functionality you need.
17+
* Avoid rewriting an entire pipeline from scratch. Reuse existing blocks and only create new blocks for the functionalities you need.
1818
* Flexibility. Compose pipeline blocks for one workflow and mix and match them for another workflow where a specific block works better.
1919

20-
Create a [`ComponentManager`] to manage the components in the pipeline. The example below adds the [stabilityai/stable-diffusion-xl-base-1.0](https://huggingface.co/stabilityai/stable-diffusion-xl-base-1.0) weights. Reduce memory usage by automatically offloading unused components to the CPU and loading them back on the GPU when they're needed.
20+
The example below composes a pipeline with an [IP-Adapter](./loading_adapters#ip-adapter) to enable image prompting.
21+
22+
Create a [`ComponentsManager`] to manage the components (text encoders, UNets, VAE, etc.) in the pipeline. Add the [stabilityai/stable-diffusion-xl-base-1.0](https://huggingface.co/stabilityai/stable-diffusion-xl-base-1.0) weights with [`add_from_pretrained`], and load the image encoder and feature extractor for the IP-Adapter with [`add`].
23+
24+
> [!TIP]
25+
> Reduce memory usage by automatically offloading unused components to the CPU and loading them back on the GPU when they're needed.
2126
2227
```py
2328
import torch
29+
from transformers import CLIPVisionModelWithProjection, CLIPImageProcessor
2430
from diffusers import ModularPipeline, StableDiffusionXLAutoPipeline
2531
from diffusers.pipelines.components_manager import ComponentsManager
32+
from diffusers.utils import load_image
2633

27-
# Load model weights
2834
components = ComponentsManager()
2935
components.add_from_pretrained("stabilityai/stable-diffusion-xl-base-1.0", torch_dtype=torch.float16)
36+
37+
image_encoder = CLIPVisionModelWithProjection.from_pretrained("h94/IP-Adapter", subfolder="sdxl_models/image_encoder", torch_dtype=torch.float16)
38+
feature_extractor = CLIPImageProcessor(size=224, crop_size=224)
39+
40+
components.add("image_encoder", image_encoder)
41+
components.add("feature_extractor", feature_extractor)
3042
components.enable_auto_cpu_offload(device="cuda:0")
3143
```
3244

33-
Use `from_block` to load the [`StableDiffusionXLAutoPipeline`] block into [`ModularPipeline`], and then use [`update_states`] to update it with the [stabilityai/stable-diffusion-xl-base-1.0](https://huggingface.co/stabilityai/stable-diffusion-xl-base-1.0) weights.
45+
Use [`from_block`] to load the [`StableDiffusionXLAutoPipeline`] block into [`ModularPipeline`], and use [`update_states`] to update it with the components in [`ComponentsManager`].
3446

3547
```py
36-
# Create pipeline
3748
auto_pipe = ModularPipeline.from_block(StableDiffusionXLAutoPipeline())
3849
auto_pipe.update_states(**components.components)
50+
auto_pipe.update_states(**components.get(["image_encoder", "feature_extractor"]))
3951
auto_pipe.to("cuda")
4052
```
4153

42-
Pass your prompt to the pipeline to generate an image.
54+
Load and set the IP-Adapter weights in the pipeline.
4355

4456
```py
45-
image = pipe(prompt="an astronaut", height=1024, width=2014, num_inference_steps=30)images[0]
57+
auto_pipe.load_ip_adapter("h94/IP-Adapter", subfolder="sdxl_models", weight_name="ip-adapter_sdxl.bin")
58+
auto_pipe.set_ip_adapter_scale(0.6)
4659
```
60+
61+
[`ModularPipeline`] automatically adapts to your input (text, image, mask image, IP-Adapter, etc.). You don't need to choose a specific pipeline for a task.
62+
63+
```py
64+
ip_adapter_image = load_image("https://huggingface.co/datasets/YiYiXu/testing-images/resolve/main/style_ziggy/img5.png")
65+
output = auto_pipe(prompt="An astronaut eating a cake in space", ip_adapter_image=ip_adapter_image, output="images").images[0]
66+
output
67+
```
68+
69+
## Pipeline blocks
70+
71+
[`StableDiffusionXLAutoPipeline`] is a preset arrangement of pipeline blocks. It can be broken down into more modular blocks and rearranged.
72+
73+
This example will show you how to recreate the same setup with [`StableDiffusionXLAutoPipeline`] in a more modular way.

0 commit comments

Comments
 (0)