Skip to content

Commit 21e61eb

Browse files
yiyixuxupatrickvonplatenpcuenca
authored
Added a README page for docs and a "schedulers" page (huggingface#1710)
Co-authored-by: Patrick von Platen <[email protected]> Co-authored-by: Pedro Cuenca <[email protected]>
1 parent c891330 commit 21e61eb

27 files changed

+791
-214
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,3 +166,6 @@ tags
166166
.DS_Store
167167
# RL pipelines may produce mp4 outputs
168168
*.mp4
169+
170+
# dependencies
171+
/transformers

docs/README.md

Lines changed: 266 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,266 @@
1+
<!---
2+
Copyright 2022- The HuggingFace Team. All rights reserved.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
-->
16+
17+
# Generating the documentation
18+
19+
To generate the documentation, you first have to build it. Several packages are necessary to build the doc,
20+
you can install them with the following command, at the root of the code repository:
21+
22+
```bash
23+
pip install -e ".[docs]"
24+
```
25+
26+
Then you need to install our open source documentation builder tool:
27+
28+
```bash
29+
pip install git+https://github.com/huggingface/doc-builder
30+
```
31+
32+
---
33+
**NOTE**
34+
35+
You only need to generate the documentation to inspect it locally (if you're planning changes and want to
36+
check how they look before committing for instance). You don't have to commit the built documentation.
37+
38+
---
39+
40+
## Previewing the documentation
41+
42+
To preview the docs, first install the `watchdog` module with:
43+
44+
```bash
45+
pip install watchdog
46+
```
47+
48+
Then run the following command:
49+
50+
```bash
51+
doc-builder preview {package_name} {path_to_docs}
52+
```
53+
54+
For example:
55+
56+
```bash
57+
doc-builder preview diffusers docs/source/
58+
```
59+
60+
The docs will be viewable at [http://localhost:3000](http://localhost:3000). You can also preview the docs once you have opened a PR. You will see a bot add a comment to a link where the documentation with your changes lives.
61+
62+
---
63+
**NOTE**
64+
65+
The `preview` command only works with existing doc files. When you add a completely new file, you need to update `_toctree.yml` & restart `preview` command (`ctrl-c` to stop it & call `doc-builder preview ...` again).
66+
67+
---
68+
69+
## Adding a new element to the navigation bar
70+
71+
Accepted files are Markdown (.md or .mdx).
72+
73+
Create a file with its extension and put it in the source directory. You can then link it to the toc-tree by putting
74+
the filename without the extension in the [`_toctree.yml`](https://github.com/huggingface/diffusers/blob/main/docs/source/_toctree.yml) file.
75+
76+
## Renaming section headers and moving sections
77+
78+
It helps to keep the old links working when renaming the section header and/or moving sections from one document to another. This is because the old links are likely to be used in Issues, Forums, and Social media and it'd make for a much more superior user experience if users reading those months later could still easily navigate to the originally intended information.
79+
80+
Therefore, we simply keep a little map of moved sections at the end of the document where the original section was. The key is to preserve the original anchor.
81+
82+
So if you renamed a section from: "Section A" to "Section B", then you can add at the end of the file:
83+
84+
```
85+
Sections that were moved:
86+
87+
[ <a href="#section-b">Section A</a><a id="section-a"></a> ]
88+
```
89+
and of course, if you moved it to another file, then:
90+
91+
```
92+
Sections that were moved:
93+
94+
[ <a href="../new-file#section-b">Section A</a><a id="section-a"></a> ]
95+
```
96+
97+
Use the relative style to link to the new file so that the versioned docs continue to work.
98+
99+
For an example of a rich moved section set please see the very end of [the transformers Trainer doc](https://github.com/huggingface/transformers/blob/main/docs/source/en/main_classes/trainer.mdx).
100+
101+
102+
## Writing Documentation - Specification
103+
104+
The `huggingface/diffusers` documentation follows the
105+
[Google documentation](https://sphinxcontrib-napoleon.readthedocs.io/en/latest/example_google.html) style for docstrings,
106+
although we can write them directly in Markdown.
107+
108+
### Adding a new tutorial
109+
110+
Adding a new tutorial or section is done in two steps:
111+
112+
- Add a new file under `docs/source`. This file can either be ReStructuredText (.rst) or Markdown (.md).
113+
- Link that file in `docs/source/_toctree.yml` on the correct toc-tree.
114+
115+
Make sure to put your new file under the proper section. It's unlikely to go in the first section (*Get Started*), so
116+
depending on the intended targets (beginners, more advanced users, or researchers) it should go in sections two, three, or four.
117+
118+
### Adding a new pipeline/scheduler
119+
120+
When adding a new pipeline:
121+
122+
- create a file `xxx.mdx` under `docs/source/api/pipelines` (don't hesitate to copy an existing file as template).
123+
- Link that file in (*Diffusers Summary*) section in `docs/source/api/pipelines/overview.mdx`, along with the link to the paper, and a colab notebook (if available).
124+
- Write a short overview of the diffusion model:
125+
- Overview with paper & authors
126+
- Paper abstract
127+
- Tips and tricks and how to use it best
128+
- Possible an end-to-end example of how to use it
129+
- Add all the pipeline classes that should be linked in the diffusion model. These classes should be added using our Markdown syntax. Usually as follows:
130+
131+
```
132+
## XXXPipeline
133+
134+
[[autodoc]] XXXPipeline
135+
```
136+
137+
This will include every public method of the pipeline that is documented. You can specify which methods should be in the docs:
138+
139+
```
140+
## XXXPipeline
141+
142+
[[autodoc]] XXXPipeline
143+
- __call__
144+
```
145+
146+
You can follow the same process to create a new scheduler under the `docs/source/api/schedulers` folder
147+
148+
### Writing source documentation
149+
150+
Values that should be put in `code` should either be surrounded by backticks: \`like so\`. Note that argument names
151+
and objects like True, None, or any strings should usually be put in `code`.
152+
153+
When mentioning a class, function, or method, it is recommended to use our syntax for internal links so that our tool
154+
adds a link to its documentation with this syntax: \[\`XXXClass\`\] or \[\`function\`\]. This requires the class or
155+
function to be in the main package.
156+
157+
If you want to create a link to some internal class or function, you need to
158+
provide its path. For instance: \[\`pipeline_utils.ImagePipelineOutput\`\]. This will be converted into a link with
159+
`pipeline_utils.ImagePipelineOutput` in the description. To get rid of the path and only keep the name of the object you are
160+
linking to in the description, add a ~: \[\`~pipeline_utils.ImagePipelineOutput\`\] will generate a link with `ImagePipelineOutput` in the description.
161+
162+
The same works for methods so you can either use \[\`XXXClass.method\`\] or \[~\`XXXClass.method\`\].
163+
164+
#### Defining arguments in a method
165+
166+
Arguments should be defined with the `Args:` (or `Arguments:` or `Parameters:`) prefix, followed by a line return and
167+
an indentation. The argument should be followed by its type, with its shape if it is a tensor, a colon, and its
168+
description:
169+
170+
```
171+
Args:
172+
n_layers (`int`): The number of layers of the model.
173+
```
174+
175+
If the description is too long to fit in one line, another indentation is necessary before writing the description
176+
after the argument.
177+
178+
Here's an example showcasing everything so far:
179+
180+
```
181+
Args:
182+
input_ids (`torch.LongTensor` of shape `(batch_size, sequence_length)`):
183+
Indices of input sequence tokens in the vocabulary.
184+
185+
Indices can be obtained using [`AlbertTokenizer`]. See [`~PreTrainedTokenizer.encode`] and
186+
[`~PreTrainedTokenizer.__call__`] for details.
187+
188+
[What are input IDs?](../glossary#input-ids)
189+
```
190+
191+
For optional arguments or arguments with defaults we follow the following syntax: imagine we have a function with the
192+
following signature:
193+
194+
```
195+
def my_function(x: str = None, a: float = 1):
196+
```
197+
198+
then its documentation should look like this:
199+
200+
```
201+
Args:
202+
x (`str`, *optional*):
203+
This argument controls ...
204+
a (`float`, *optional*, defaults to 1):
205+
This argument is used to ...
206+
```
207+
208+
Note that we always omit the "defaults to \`None\`" when None is the default for any argument. Also note that even
209+
if the first line describing your argument type and its default gets long, you can't break it on several lines. You can
210+
however write as many lines as you want in the indented description (see the example above with `input_ids`).
211+
212+
#### Writing a multi-line code block
213+
214+
Multi-line code blocks can be useful for displaying examples. They are done between two lines of three backticks as usual in Markdown:
215+
216+
217+
````
218+
```
219+
# first line of code
220+
# second line
221+
# etc
222+
```
223+
````
224+
225+
#### Writing a return block
226+
227+
The return block should be introduced with the `Returns:` prefix, followed by a line return and an indentation.
228+
The first line should be the type of the return, followed by a line return. No need to indent further for the elements
229+
building the return.
230+
231+
Here's an example of a single value return:
232+
233+
```
234+
Returns:
235+
`List[int]`: A list of integers in the range [0, 1] --- 1 for a special token, 0 for a sequence token.
236+
```
237+
238+
Here's an example of a tuple return, comprising several objects:
239+
240+
```
241+
Returns:
242+
`tuple(torch.FloatTensor)` comprising various elements depending on the configuration ([`BertConfig`]) and inputs:
243+
- ** loss** (*optional*, returned when `masked_lm_labels` is provided) `torch.FloatTensor` of shape `(1,)` --
244+
Total loss is the sum of the masked language modeling loss and the next sequence prediction (classification) loss.
245+
- **prediction_scores** (`torch.FloatTensor` of shape `(batch_size, sequence_length, config.vocab_size)`) --
246+
Prediction scores of the language modeling head (scores for each vocabulary token before SoftMax).
247+
```
248+
249+
#### Adding an image
250+
251+
Due to the rapidly growing repository, it is important to make sure that no files that would significantly weigh down the repository are added. This includes images, videos, and other non-text files. We prefer to leverage a hf.co hosted `dataset` like
252+
the ones hosted on [`hf-internal-testing`](https://huggingface.co/hf-internal-testing) in which to place these files and reference
253+
them by URL. We recommend putting them in the following dataset: [huggingface/documentation-images](https://huggingface.co/datasets/huggingface/documentation-images).
254+
If an external contribution, feel free to add the images to your PR and ask a Hugging Face member to migrate your images
255+
to this dataset.
256+
257+
## Styling the docstring
258+
259+
We have an automatic script running with the `make style` command that will make sure that:
260+
- the docstrings fully take advantage of the line width
261+
- all code examples are formatted using black, like the code of the Transformers library
262+
263+
This script may have some weird failures if you made a syntax mistake or if you uncover a bug. Therefore, it's
264+
recommended to commit your changes before running `make style`, so you can revert the changes done by that script
265+
easily.
266+

docs/source/_toctree.yml

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,8 +78,6 @@
7878
- sections:
7979
- local: api/models
8080
title: "Models"
81-
- local: api/schedulers
82-
title: "Schedulers"
8381
- local: api/diffusion_pipeline
8482
title: "Diffusion Pipeline"
8583
- local: api/logging
@@ -129,6 +127,44 @@
129127
- local: api/pipelines/audio_diffusion
130128
title: "Audio Diffusion"
131129
title: "Pipelines"
130+
- sections:
131+
- local: api/schedulers/overview
132+
title: "Overview"
133+
- local: api/schedulers/ddim
134+
title: "DDIM"
135+
- local: api/schedulers/ddpm
136+
title: "DDPM"
137+
- local: api/schedulers/singlestep_dpm_solver
138+
title: "Singlestep DPM-Solver"
139+
- local: api/schedulers/multistep_dpm_solver
140+
title: "Multistep DPM-Solver"
141+
- local: api/schedulers/heun
142+
title: "Heun Scheduler"
143+
- local: api/schedulers/dpm_discrete
144+
title: "DPM Discrete Scheduler"
145+
- local: api/schedulers/dpm_discrete_ancestral
146+
title: "DPM Discrete Scheduler with ancestral sampling"
147+
- local: api/schedulers/stochastic_karras_ve
148+
title: "Stochastic Kerras VE"
149+
- local: api/schedulers/lms_discrete
150+
title: "Linear Multistep"
151+
- local: api/schedulers/pndm
152+
title: "PNDM"
153+
- local: api/schedulers/score_sde_ve
154+
title: "VE-SDE"
155+
- local: api/schedulers/ipndm
156+
title: "IPNDM"
157+
- local: api/schedulers/score_sde_vp
158+
title: "VP-SDE"
159+
- local: api/schedulers/euler
160+
title: "Euler scheduler"
161+
- local: api/schedulers/euler_ancestral
162+
title: "Euler Ancestral Scheduler"
163+
- local: api/schedulers/vq_diffusion
164+
title: "VQDiffusionScheduler"
165+
- local: api/schedulers/repaint
166+
title: "RePaint Scheduler"
167+
title: "Schedulers"
132168
- sections:
133169
- local: api/experimental/rl
134170
title: "RL Planning"

0 commit comments

Comments
 (0)