Skip to content

Commit 0d08370

Browse files
authored
[docs] Community pipelines (#7819)
* community pipelines * feedback * consolidate
1 parent b8ccb46 commit 0d08370

File tree

5 files changed

+165
-330
lines changed

5 files changed

+165
-330
lines changed

docs/source/en/_toctree.yml

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -87,10 +87,6 @@
8787
title: Shap-E
8888
- local: using-diffusers/diffedit
8989
title: DiffEdit
90-
- local: using-diffusers/custom_pipeline_examples
91-
title: Community pipelines
92-
- local: using-diffusers/contribute_pipeline
93-
title: Contribute a community pipeline
9490
- local: using-diffusers/inference_with_lcm_lora
9591
title: Latent Consistency Model-LoRA
9692
- local: using-diffusers/inference_with_lcm

docs/source/en/conceptual/contribution.md

Lines changed: 65 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -198,38 +198,81 @@ Anything displayed on [the official Diffusers doc page](https://huggingface.co/d
198198

199199
Please have a look at [this page](https://github.com/huggingface/diffusers/tree/main/docs) on how to verify changes made to the documentation locally.
200200

201-
202201
### 6. Contribute a community pipeline
203202

204-
[Pipelines](https://huggingface.co/docs/diffusers/api/pipelines/overview) are usually the first point of contact between the Diffusers library and the user.
205-
Pipelines are examples of how to use Diffusers [models](https://huggingface.co/docs/diffusers/api/models/overview) and [schedulers](https://huggingface.co/docs/diffusers/api/schedulers/overview).
206-
We support two types of pipelines:
203+
> [!TIP]
204+
> Read the [Community pipelines](../using-diffusers/custom_pipeline_overview#community-pipelines) guide to learn more about the difference between a GitHub and Hugging Face Hub community pipeline. If you're interested in why we have community pipelines, take a look at GitHub Issue [#841](https://github.com/huggingface/diffusers/issues/841) (basically, we can't maintain all the possible ways diffusion models can be used for inference but we also don't want to prevent the community from building them).
205+
206+
Contributing a community pipeline is a great way to share your creativity and work with the community. It lets you build on top of the [`DiffusionPipeline`] so that anyone can load and use it by setting the `custom_pipeline` parameter. This section will walk you through how to create a simple pipeline where the UNet only does a single forward pass and calls the scheduler once (a "one-step" pipeline).
207+
208+
1. Create a one_step_unet.py file for your community pipeline. This file can contain whatever package you want to use as long as it's installed by the user. Make sure you only have one pipeline class that inherits from [`DiffusionPipeline`] to load model weights and the scheduler configuration from the Hub. Add a UNet and scheduler to the `__init__` function.
209+
210+
You should also add the `register_modules` function to ensure your pipeline and its components can be saved with [`~DiffusionPipeline.save_pretrained`].
211+
212+
```py
213+
from diffusers import DiffusionPipeline
214+
import torch
215+
216+
class UnetSchedulerOneForwardPipeline(DiffusionPipeline):
217+
def __init__(self, unet, scheduler):
218+
super().__init__()
219+
220+
self.register_modules(unet=unet, scheduler=scheduler)
221+
```
222+
223+
1. In the forward pass (which we recommend defining as `__call__`), you can add any feature you'd like. For the "one-step" pipeline, create a random image and call the UNet and scheduler once by setting `timestep=1`.
224+
225+
```py
226+
from diffusers import DiffusionPipeline
227+
import torch
228+
229+
class UnetSchedulerOneForwardPipeline(DiffusionPipeline):
230+
def __init__(self, unet, scheduler):
231+
super().__init__()
232+
233+
self.register_modules(unet=unet, scheduler=scheduler)
207234

208-
- Official Pipelines
209-
- Community Pipelines
235+
def __call__(self):
236+
image = torch.randn(
237+
(1, self.unet.config.in_channels, self.unet.config.sample_size, self.unet.config.sample_size),
238+
)
239+
timestep = 1
240+
241+
model_output = self.unet(image, timestep).sample
242+
scheduler_output = self.scheduler.step(model_output, timestep, image).prev_sample
243+
244+
return scheduler_output
245+
```
210246

211-
Both official and community pipelines follow the same design and consist of the same type of components.
247+
Now you can run the pipeline by passing a UNet and scheduler to it or load pretrained weights if the pipeline structure is identical.
248+
249+
```py
250+
from diffusers import DDPMScheduler, UNet2DModel
251+
252+
scheduler = DDPMScheduler()
253+
unet = UNet2DModel()
254+
255+
pipeline = UnetSchedulerOneForwardPipeline(unet=unet, scheduler=scheduler)
256+
output = pipeline()
257+
# load pretrained weights
258+
pipeline = UnetSchedulerOneForwardPipeline.from_pretrained("google/ddpm-cifar10-32", use_safetensors=True)
259+
output = pipeline()
260+
```
212261

213-
Official pipelines are tested and maintained by the core maintainers of Diffusers. Their code
214-
resides in [src/diffusers/pipelines](https://github.com/huggingface/diffusers/tree/main/src/diffusers/pipelines).
215-
In contrast, community pipelines are contributed and maintained purely by the **community** and are **not** tested.
216-
They reside in [examples/community](https://github.com/huggingface/diffusers/tree/main/examples/community) and while they can be accessed via the [PyPI diffusers package](https://pypi.org/project/diffusers/), their code is not part of the PyPI distribution.
262+
You can either share your pipeline as a GitHub community pipeline or Hub community pipeline.
217263

218-
The reason for the distinction is that the core maintainers of the Diffusers library cannot maintain and test all
219-
possible ways diffusion models can be used for inference, but some of them may be of interest to the community.
220-
Officially released diffusion pipelines,
221-
such as Stable Diffusion are added to the core src/diffusers/pipelines package which ensures
222-
high quality of maintenance, no backward-breaking code changes, and testing.
223-
More bleeding edge pipelines should be added as community pipelines. If usage for a community pipeline is high, the pipeline can be moved to the official pipelines upon request from the community. This is one of the ways we strive to be a community-driven library.
264+
<hfoptions id="pipeline type">
265+
<hfoption id="GitHub pipeline">
224266

225-
To add a community pipeline, one should add a <name-of-the-community>.py file to [examples/community](https://github.com/huggingface/diffusers/tree/main/examples/community) and adapt the [examples/community/README.md](https://github.com/huggingface/diffusers/tree/main/examples/community/README.md) to include an example of the new pipeline.
267+
Share your GitHub pipeline by opening a pull request on the Diffusers [repository](https://github.com/huggingface/diffusers) and add the one_step_unet.py file to the [examples/community](https://github.com/huggingface/diffusers/tree/main/examples/community) subfolder.
226268

227-
An example can be seen [here](https://github.com/huggingface/diffusers/pull/2400).
269+
</hfoption>
270+
<hfoption id="Hub pipeline">
228271

229-
Community pipeline PRs are only checked at a superficial level and ideally they should be maintained by their original authors.
272+
Share your Hub pipeline by creating a model repository on the Hub and uploading the one_step_unet.py file to it.
230273

231-
Contributing a community pipeline is a great way to understand how Diffusers models and schedulers work. Having contributed a community pipeline is usually the first stepping stone to contributing an official pipeline to the
232-
core package.
274+
</hfoption>
275+
</hfoptions>
233276

234277
### 7. Contribute to training examples
235278

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

Lines changed: 0 additions & 184 deletions
This file was deleted.

0 commit comments

Comments
 (0)