Debian Codegraph @

docs/README.md

raw
1 <!---
2 Copyright 2023 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 special tool that builds the documentation:
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 ## Building the documentation
41
42 Once you have setup the `doc-builder` and additional packages, you can generate the documentation by
43 typing the following command:
44
45 ```bash
46 doc-builder build accelerate docs/source/ --build_dir ~/tmp/test-build
47 ```
48
49 You can adapt the `--build_dir` to set any temporary folder that you prefer. This command will create it and generate
50 the MDX files that will be rendered as the documentation on the main website. You can inspect them in your favorite
51 Markdown editor.
52
53 ## Previewing the documentation
54
55 To preview the docs, first install the `watchdog` module with:
56
57 ```bash
58 pip install watchdog
59 ```
60
61 Then run the following command:
62
63 ```bash
64 doc-builder preview {package_name} {path_to_docs}
65 ```
66
67 For example:
68
69 ```bash
70 doc-builder preview accelerate docs/source/
71 ```
72
73 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.
74
75 ---
76 **NOTE**
77
78 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).
79
80 ---
81
82 ## Adding a new element to the navigation bar
83
84 Accepted files are Markdown (.md).
85
86 Create a file with its extension and put it in the source directory. You can then link it to the toc-tree by putting
87 the filename without the extension in the [`_toctree.yml`](https://github.com/huggingface/accelerate/blob/main/docs/source/_toctree.yml) file.
88
89 ## Renaming section headers and moving sections
90
91 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.
92
93 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.
94
95 So if you renamed a section from: "Section A" to "Section B", then you can add at the end of the file:
96
97 ```
98 Sections that were moved:
99
100 [ <a href="#section-b">Section A</a><a id="section-a"></a> ]
101 ```
102 and of course, if you moved it to another file, then:
103
104 ```
105 Sections that were moved:
106
107 [ <a href="../new-file#section-b">Section A</a><a id="section-a"></a> ]
108 ```
109
110 Use the relative style to link to the new file so that the versioned docs continue to work.
111
112
113 ## Writing Documentation - Specification
114
115 The `huggingface/accelerate` documentation follows the
116 [Google documentation](https://sphinxcontrib-napoleon.readthedocs.io/en/latest/example_google.html) style for docstrings,
117 although we can write them directly in Markdown.
118
119 ### Adding a new tutorial
120
121 Adding a new tutorial or section is done in two steps:
122
123 - Add a new file under `./source`. This file can either be ReStructuredText (.rst) or Markdown (.md).
124 - Link that file in `./source/_toctree.yml` on the correct toc-tree.
125
126 Make sure to put your new file under the proper section. It's unlikely to go in the first section (*Get Started*), so
127 depending on the intended targets (beginners, more advanced users, or researchers) it should go in sections two, three, or
128 four.
129
130 ### Writing source documentation
131
132 Values that should be put in `code` should either be surrounded by backticks: \`like so\`. Note that argument names
133 and objects like True, None, or any strings should usually be put in `code`.
134
135 When mentioning a class, function, or method, it is recommended to use our syntax for internal links so that our tool
136 adds a link to its documentation with this syntax: \[\`XXXClass\`\] or \[\`function\`\]. This requires the class or
137 function to be in the main package.
138
139 If you want to create a link to some internal class or function, you need to
140 provide its path. For instance: \[\`utils.gather\`\]. This will be converted into a link with
141 `utils.gather` in the description. To get rid of the path and only keep the name of the object you are
142 linking to in the description, add a ~: \[\`~utils.gather\`\] will generate a link with `gather` in the description.
143
144 The same works for methods so you can either use \[\`XXXClass.method\`\] or \[~\`XXXClass.method\`\].
145
146 #### Defining arguments in a method
147
148 Arguments should be defined with the `Args:` (or `Arguments:` or `Parameters:`) prefix, followed by a line return and
149 an indentation. The argument should be followed by its type, with its shape if it is a tensor, a colon, and its
150 description:
151
152 ```
153 Args:
154 n_layers (`int`): The number of layers of the model.
155 ```
156
157 If the description is too long to fit in one line (more than 119 characters in total), another indentation is necessary
158 before writing the description after the argument.
159
160 Finally, to maintain uniformity if any *one* description is too long to fit on one line, the
161 rest of the parameters should follow suit and have an indention before their description.
162
163 Here's an example showcasing everything so far:
164
165 ```
166 Args:
167 gradient_accumulation_steps (`int`, *optional*, default to 1):
168 The number of steps that should pass before gradients are accumulated. A number > 1 should be combined with `Accelerator.accumulate`.
169 cpu (`bool`, *optional*):
170 Whether or not to force the script to execute on CPU. Will ignore GPU available if set to `True` and force the execution on one process only.
171 ```
172
173 For optional arguments or arguments with defaults we follow the following syntax: imagine we have a function with the
174 following signature:
175
176 ```
177 def my_function(x: str = None, a: float = 1):
178 ```
179
180 then its documentation should look like this:
181
182 ```
183 Args:
184 x (`str`, *optional*):
185 This argument controls ... and has a description longer than 119 chars.
186 a (`float`, *optional*, defaults to 1):
187 This argument is used to ... and has a description longer than 119 chars.
188 ```
189
190 Note that we always omit the "defaults to \`None\`" when None is the default for any argument. Also note that even
191 if the first line describing your argument type and its default gets long, you can't break it on several lines. You can
192 however write as many lines as you want in the indented description (see the example above with `input_ids`).
193
194 #### Writing a multi-line code block
195
196 Multi-line code blocks can be useful for displaying examples. They are done between two lines of three backticks as usual in Markdown:
197
198
199 ````
200 ```python
201 # first line of code
202 # second line
203 # etc
204 ```
205 ````
206
207 #### Writing a return block
208
209 The return block should be introduced with the `Returns:` prefix, followed by a line return and an indentation.
210 The first line should be the type of the return, followed by a line return. No need to indent further for the elements
211 building the return.
212
213 Here's an example of a single value return:
214
215 ```
216 Returns:
217 `List[int]`: A list of integers in the range [0, 1] --- 1 for a special token, 0 for a sequence token.
218 ```
219
220 Here's an example of a tuple return, comprising several objects:
221
222 ```
223 Returns:
224 `tuple(torch.FloatTensor)` comprising various elements depending on the configuration ([`BertConfig`]) and inputs:
225 - ** loss** (*optional*, returned when `masked_lm_labels` is provided) `torch.FloatTensor` of shape `(1,)` --
226 Total loss is the sum of the masked language modeling loss and the next sequence prediction (classification) loss.
227 - **prediction_scores** (`torch.FloatTensor` of shape `(batch_size, sequence_length, config.vocab_size)`) --
228 Prediction scores of the language modeling head (scores for each vocabulary token before SoftMax).
229 ```
230
231 ## Styling the docstring
232
233 We have an automatic script running with the `make style` comment that will make sure that:
234 - the docstrings fully take advantage of the line width
235 - all code examples are formatted using black, like the code of the Transformers library
236
237 This script may have some weird failures if you made a syntax mistake or if you uncover a bug. Therefore, it's
238 recommended to commit your changes before running `make style`, so you can revert the changes done by that script
239 easily.
240
241 ## Writing documentation examples
242
243 The syntax for Example docstrings can look as follows:
244
245 ```
246 Example:
247
248 ```python
249 >>> import time
250 >>> from accelerate import Accelerator
251 >>> accelerator = Accelerator()
252 >>> if accelerator.is_main_process:
253 ... time.sleep(2)
254 >>> else:
255 ... print("I'm waiting for the main process to finish its sleep...")
256 >>> accelerator.wait_for_everyone()
257 >>> # Should print on every process at the same time
258 >>> print("Everyone is here")
259 ```
260 ```
261
262 The docstring should give a minimal, clear example of how the respective function
263 is to be used in inference and also include the expected (ideally sensible)
264 output.
265 Often, readers will try out the example before even going through the function
266 or class definitions. Therefore, it is of utmost importance that the example
267 works as expected.