Skip to content

Commit 778379a

Browse files
committed
community pipelines
1 parent 153a35d commit 778379a

File tree

2 files changed

+55
-57
lines changed

2 files changed

+55
-57
lines changed

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

Lines changed: 52 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,17 @@ specific language governing permissions and limitations under the License.
1818

1919
Community pipelines are any [`DiffusionPipeline`] class that are different from the original implementation as specified in their paper (for example, the [`StableDiffusionControlNetPipeline`] corresponds to the [Text-to-Image Generation with ControlNet Conditioning](https://arxiv.org/abs/2302.05543) paper). They provide additional functionality or extend the original implementation of a pipeline.
2020

21-
There are many cool community pipelines like [Speech to Image](https://github.com/huggingface/diffusers/tree/main/examples/community#speech-to-image) or [Composable Stable Diffusion](https://github.com/huggingface/diffusers/tree/main/examples/community#composable-stable-diffusion), and you can find all the official community pipelines [here](https://github.com/huggingface/diffusers/tree/main/examples/community).
21+
There are many cool community pipelines like [Marigold Depth Estimation](https://github.com/huggingface/diffusers/tree/main/examples/community#marigold-depth-estimation) or [InstantID](https://github.com/huggingface/diffusers/tree/main/examples/community#instantid-pipeline), and you can find all the official community pipelines [here](https://github.com/huggingface/diffusers/tree/main/examples/community).
2222

23-
To load any community pipeline on the Hub, pass the repository id of the community pipeline to the `custom_pipeline` argument and the model repository where you'd like to load the pipeline weights and components from. For example, the example below loads a dummy pipeline from [`hf-internal-testing/diffusers-dummy-pipeline`](https://huggingface.co/hf-internal-testing/diffusers-dummy-pipeline/blob/main/pipeline.py) and the pipeline weights and components from [`google/ddpm-cifar10-32`](https://huggingface.co/google/ddpm-cifar10-32):
23+
There are two types of community pipelines, those stored on the Hugging Face Hub and those stored on Diffusers GitHub repository. Refer to this [table](./contribute_pipeline#share-your-pipeline) for a comparison of Hub vs GitHub community pipelines.
2424

25-
<Tip warning={true}>
25+
<hfoptions id="community">
26+
<hfoption id="Hub pipelines">
2627

27-
🔒 By loading a community pipeline from the Hugging Face Hub, you are trusting that the code you are loading is safe. Make sure to inspect the code online before loading and running it automatically!
28+
To load a Hugging Face Hub community pipeline, pass the repository id of the community pipeline to the `custom_pipeline` argument and the model repository where you'd like to load the pipeline weights and components from. For example, the example below loads a dummy pipeline from [hf-internal-testing/diffusers-dummy-pipeline](https://huggingface.co/hf-internal-testing/diffusers-dummy-pipeline/blob/main/pipeline.py) and the pipeline weights and components from [google/ddpm-cifar10-32](https://huggingface.co/google/ddpm-cifar10-32):
2829

29-
</Tip>
30+
> [!WARNING]
31+
> By loading a community pipeline from the Hugging Face Hub, you are trusting that the code you are loading is safe. Make sure to inspect the code online before loading and running it automatically!
3032
3133
```py
3234
from diffusers import DiffusionPipeline
@@ -36,7 +38,10 @@ pipeline = DiffusionPipeline.from_pretrained(
3638
)
3739
```
3840

39-
Loading an official community pipeline is similar, but you can mix loading weights from an official repository id and pass pipeline components directly. The example below loads the community [CLIP Guided Stable Diffusion](https://github.com/huggingface/diffusers/tree/main/examples/community#clip-guided-stable-diffusion) pipeline, and you can pass the CLIP model components directly to it:
41+
</hfoption>
42+
<hfoption id="GitHub pipelines">
43+
44+
To load a GitHub community pipeline, pass the repository id of the community pipeline to the `custom_pipeline` argument and the model repository where you you'd like to load the pipeline weights and components from. You can also load model components directly. The example below loads the community [CLIP Guided Stable Diffusion](https://github.com/huggingface/diffusers/tree/main/examples/community#clip-guided-stable-diffusion) pipeline and the CLIP model components.
4045

4146
```py
4247
from diffusers import DiffusionPipeline
@@ -56,9 +61,12 @@ pipeline = DiffusionPipeline.from_pretrained(
5661
)
5762
```
5863

64+
</hfoption>
65+
</hfoptions>
66+
5967
### Load from a local file
6068

61-
Community pipelines can also be loaded from a local file if you pass a file path instead. The path to the passed directory must contain a `pipeline.py` file that contains the pipeline class in order to successfully load it.
69+
Community pipelines can also be loaded from a local file if you pass a file path instead. The path to the passed directory must contain a pipeline.py file that contains the pipeline class.
6270

6371
```py
6472
pipeline = DiffusionPipeline.from_pretrained(
@@ -109,51 +117,49 @@ pipeline = DiffusionPipeline.from_pretrained(
109117
</hfoption>
110118
</hfoptions>
111119

112-
### Load from_pipe
120+
### Load with from_pipe
113121

114-
Did you know that you can use `from_pipe` with a community pipeline? Let me show you an example of using long negative prompt and prompt weighting!
122+
Community pipelines can also be loaded with the [`~DiffusionPipeline.from_pipe`] method which allows you to load and reuse multiple pipelines without any additional memory overhead (learn more in the [Reuse a pipeline](./loading#reuse-a-pipeline) guide). The memory requirement is determined by the largest single pipeline loaded.
115123

116-
```bash
124+
For example, let's load a community pipeline that supports [long prompts with weighting](https://github.com/huggingface/diffusers/tree/main/examples/community#long-prompt-weighting-stable-diffusion) from a Stable Diffusion pipeline.
125+
126+
```py
127+
import torch
128+
from diffusers import DiffusionPipeline
129+
130+
pipe_sd = DiffusionPipeline.from_pretrained("emilianJR/CyberRealistic_V3", torch_dtype=torch.float16)
131+
pipe_sd.to("cuda")
117132
pipe_lpw = DiffusionPipeline.from_pipe(
118133
pipe_sd,
119134
custom_pipeline="lpw_stable_diffusion",
120135
).to("cuda")
121136

122-
prompt = "best_quality (1girl:1.3) bow bride brown_hair closed_mouth frilled_bow frilled_hair_tubes frills (full_body:1.3) fox_ear hair_bow hair_tubes happy hood japanese_clothes kimono long_sleeves red_bow smile solo tabi uchikake white_kimono wide_sleeves cherry_blossoms"
123-
neg_prompt = "lowres, bad_anatomy, error_body, error_hair, error_arm, error_hands, bad_hands, error_fingers, bad_fingers, missing_fingers, error_legs, bad_legs, multiple_legs, missing_legs, error_lighting, error_shadow, error_reflection, text, error, extra_digit, fewer_digits, cropped, worst_quality, low_quality, normal_quality, jpeg_artifacts, signature, watermark, username, blurry"
124-
generator = torch.Generator(device="cpu").manual_seed(33)
125-
out_lpw = pipe_lpw.text2img(
137+
prompt = "cat, hiding in the leaves, ((rain)), zazie rainyday, beautiful eyes, macro shot, colorful details, natural lighting, amazing composition, subsurface scattering, amazing textures, filmic, soft light, ultra-detailed eyes, intricate details, detailed texture, light source contrast, dramatic shadows, cinematic light, depth of field, film grain, noise, dark background, hyperrealistic dslr film still, dim volumetric cinematic lighting"
138+
neg_prompt = "(deformed iris, deformed pupils, semi-realistic, cgi, 3d, render, sketch, cartoon, drawing, anime, mutated hands and fingers:1.4), (deformed, distorted, disfigured:1.3), poorly drawn, bad anatomy, wrong anatomy, extra limb, missing limb, floating limbs, disconnected limbs, mutation, mutated, ugly, disgusting, amputation"
139+
generator = torch.Generator(device="cpu").manual_seed(20)
140+
out_lpw = pipe_lpw(
126141
prompt,
127142
negative_prompt=neg_prompt,
128-
width=512,height=512,
143+
width=512,
144+
height=512,
129145
max_embeddings_multiples=3,
130-
num_inference_steps=num_inference_steps,
146+
num_inference_steps=50,
131147
generator=generator,
132148
).images[0]
149+
out_lpw
133150
```
134151

135-
<div class="flex justify-center">
136-
<img class="rounded-xl" src="https://huggingface.co/datasets/YiYiXu/testing-images/resolve/main/from_pipe_out_lpw_4.png"/>
152+
<div class="flex gap-4">
153+
<div>
154+
<img class="rounded-xl" src="https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/diffusers/from_pipe_lpw.png" />
155+
<figcaption class="mt-2 text-center text-sm text-gray-500">Stable Diffusion</figcaption>
156+
</div>
157+
<div>
158+
<img class="rounded-xl" src="https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/diffusers/from_pipe_non_lpw.png" />
159+
<figcaption class="mt-2 text-center text-sm text-gray-500">Stable Diffusion with long prompt weighting</figcaption>
160+
</div>
137161
</div>
138162

139-
let’s run StableDiffusionPipeline with the same inputs to compare: the result from the long prompt weighting pipeline is more aligned with the text prompt.
140-
141-
```
142-
generator = torch.Generator(device="cpu").manual_seed(33)
143-
out_sd = pipe_sd(
144-
prompt=prompt,
145-
negative_prompt=negative_prompt,
146-
generator=generator,
147-
num_inference_steps=num_inference_steps,
148-
).images[0]
149-
out_sd
150-
```
151-
<div class="flex justify-center">
152-
<img class="rounded-xl" src="https://huggingface.co/datasets/YiYiXu/testing-images/resolve/main/from_pipe_out_sd_5.png"/>
153-
</div>
154-
155-
For more information about community pipelines, take a look at the [Community pipelines](custom_pipeline_examples) guide for how to use them and if you're interested in adding a community pipeline check out the [How to contribute a community pipeline](contribute_pipeline) guide!
156-
157163
## Community components
158164

159165
Community components allow users to build pipelines that may have customized components that are not a part of Diffusers. If your pipeline has custom components that Diffusers doesn't already support, you need to provide their implementations as Python modules. These customized components could be a VAE, UNet, and scheduler. In most cases, the text encoder is imported from the Transformers library. The pipeline code itself can also be customized.
@@ -194,7 +200,7 @@ In steps 4 and 5, the custom [UNet](https://github.com/showlab/Show-1/blob/main/
194200

195201
</Tip>
196202

197-
4. Now you'll load a [custom UNet](https://github.com/showlab/Show-1/blob/main/showone/models/unet_3d_condition.py), which in this example, has already been implemented in the `showone_unet_3d_condition.py` [script](https://huggingface.co/sayakpaul/show-1-base-with-code/blob/main/unet/showone_unet_3d_condition.py) for your convenience. You'll notice the `UNet3DConditionModel` class name is changed to `ShowOneUNet3DConditionModel` because [`UNet3DConditionModel`] already exists in Diffusers. Any components needed for the `ShowOneUNet3DConditionModel` class should be placed in the `showone_unet_3d_condition.py` script.
203+
4. Now you'll load a [custom UNet](https://github.com/showlab/Show-1/blob/main/showone/models/unet_3d_condition.py), which in this example, has already been implemented in [showone_unet_3d_condition.py](https://huggingface.co/sayakpaul/show-1-base-with-code/blob/main/unet/showone_unet_3d_condition.py) for your convenience. You'll notice the [`UNet3DConditionModel`] class name is changed to `ShowOneUNet3DConditionModel` because [`UNet3DConditionModel`] already exists in Diffusers. Any components needed for the `ShowOneUNet3DConditionModel` class should be placed in showone_unet_3d_condition.py.
198204

199205
Once this is done, you can initialize the UNet:
200206

@@ -204,7 +210,7 @@ from showone_unet_3d_condition import ShowOneUNet3DConditionModel
204210
unet = ShowOneUNet3DConditionModel.from_pretrained(pipe_id, subfolder="unet")
205211
```
206212

207-
5. Finally, you'll load the custom pipeline code. For this example, it has already been created for you in the `pipeline_t2v_base_pixel.py` [script](https://huggingface.co/sayakpaul/show-1-base-with-code/blob/main/pipeline_t2v_base_pixel.py). This script contains a custom `TextToVideoIFPipeline` class for generating videos from text. Just like the custom UNet, any code needed for the custom pipeline to work should go in the `pipeline_t2v_base_pixel.py` script.
213+
5. Finally, you'll load the custom pipeline code. For this example, it has already been created for you in [pipeline_t2v_base_pixel.py](https://huggingface.co/sayakpaul/show-1-base-with-code/blob/main/pipeline_t2v_base_pixel.py). This script contains a custom `TextToVideoIFPipeline` class for generating videos from text. Just like the custom UNet, any code needed for the custom pipeline to work should go in pipeline_t2v_base_pixel.py.
208214

209215
Once everything is in place, you can initialize the `TextToVideoIFPipeline` with the `ShowOneUNet3DConditionModel`:
210216

@@ -231,11 +237,14 @@ pipeline.push_to_hub("custom-t2v-pipeline")
231237

232238
After the pipeline is successfully pushed, you need a couple of changes:
233239

234-
1. Change the `_class_name` attribute in [`model_index.json`](https://huggingface.co/sayakpaul/show-1-base-with-code/blob/main/model_index.json#L2) to `"pipeline_t2v_base_pixel"` and `"TextToVideoIFPipeline"`.
235-
2. Upload `showone_unet_3d_condition.py` to the `unet` [directory](https://huggingface.co/sayakpaul/show-1-base-with-code/blob/main/unet/showone_unet_3d_condition.py).
236-
3. Upload `pipeline_t2v_base_pixel.py` to the pipeline base [directory](https://huggingface.co/sayakpaul/show-1-base-with-code/blob/main/unet/showone_unet_3d_condition.py).
240+
1. Change the `_class_name` attribute in [model_index.json](https://huggingface.co/sayakpaul/show-1-base-with-code/blob/main/model_index.json#L2) to `"pipeline_t2v_base_pixel"` and `"TextToVideoIFPipeline"`.
241+
2. Upload `showone_unet_3d_condition.py` to the [unet](https://huggingface.co/sayakpaul/show-1-base-with-code/blob/main/unet/showone_unet_3d_condition.py) subfolder.
242+
3. Upload `pipeline_t2v_base_pixel.py` to the pipeline [repository](https://huggingface.co/sayakpaul/show-1-base-with-code/tree/main).
237243

238-
To run inference, simply add the `trust_remote_code` argument while initializing the pipeline to handle all the "magic" behind the scenes.
244+
To run inference, add the `trust_remote_code` argument while initializing the pipeline to handle all the "magic" behind the scenes.
245+
246+
> [!WARNING]
247+
> As an additional precaution with `trust_remote_code=True`, we strongly encourage you to pass a commit hash to the `revision` parameter in [`~DiffusionPipeline.from_pretrained`] to make sure the code hasn't been updated with some malicious new lines of code (unless you fully trust the model owners).
239248
240249
```python
241250
from diffusers import DiffusionPipeline
@@ -263,25 +272,14 @@ video_frames = pipeline(
263272
).frames
264273
```
265274

266-
As an additional reference example, you can refer to the repository structure of [stabilityai/japanese-stable-diffusion-xl](https://huggingface.co/stabilityai/japanese-stable-diffusion-xl/), that makes use of the `trust_remote_code` feature:
275+
As an additional reference, take a look at the repository structure of [stabilityai/japanese-stable-diffusion-xl](https://huggingface.co/stabilityai/japanese-stable-diffusion-xl/) which also uses the `trust_remote_code` feature.
267276

268277
```python
269-
270278
from diffusers import DiffusionPipeline
271279
import torch
272280

273281
pipeline = DiffusionPipeline.from_pretrained(
274282
"stabilityai/japanese-stable-diffusion-xl", trust_remote_code=True
275283
)
276284
pipeline.to("cuda")
277-
278-
# if using torch < 2.0
279-
# pipeline.enable_xformers_memory_efficient_attention()
280-
281-
prompt = "柴犬、カラフルアート"
282-
283-
image = pipeline(prompt=prompt).images[0]
284285
```
285-
286-
> [!TIP]
287-
> When using `trust_remote_code=True`, it is also strongly encouraged to pass a commit hash as a `revision` to make sure the author of the models did not update the code with some malicious new lines (unless you fully trust the authors of the models).

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -286,10 +286,10 @@ pipe.sag_unload_ip_adapter()
286286

287287
generator = torch.Generator(device="cpu").manual_seed(33)
288288
out_sd = pipe_sd(
289-
prompt=prompt,
290-
negative_prompt=negative_prompt,
289+
prompt="bear eats pizza",
290+
negative_prompt="wrong white balance, dark, sketches,worst quality,low quality",
291291
ip_adapter_image=image,
292-
num_inference_steps=num_inference_steps,
292+
num_inference_steps=50,
293293
generator=generator,
294294
).images[0]
295295
"AttributeError: 'NoneType' object has no attribute 'image_projection_layers'"

0 commit comments

Comments
 (0)