|
| 1 | +# Generating the documentation |
| 2 | + |
| 3 | +To generate the documentation, you first have to build it. Several packages are necessary to build the doc, |
| 4 | +you can install them with the following command, at the root of the code repository: |
| 5 | + |
| 6 | +```bash |
| 7 | +pip install -e ".[docs]" |
| 8 | +``` |
| 9 | + |
| 10 | +--- |
| 11 | +**NOTE** |
| 12 | + |
| 13 | +You only need to generate the documentation to inspect it locally (if you're planning changes and want to |
| 14 | +check how they look like before committing for instance). You don't have to commit the built documentation. |
| 15 | + |
| 16 | +--- |
| 17 | + |
| 18 | +## Packages installed |
| 19 | + |
| 20 | +Here's an overview of all the packages installed. If you ran the previous command installing all packages from |
| 21 | +`requirements.txt`, you do not need to run the following commands. |
| 22 | + |
| 23 | +Building it requires the package `sphinx` that you can |
| 24 | +install using: |
| 25 | + |
| 26 | +```bash |
| 27 | +pip install -U sphinx |
| 28 | +``` |
| 29 | + |
| 30 | +You would also need the custom installed [theme](https://github.com/readthedocs/sphinx_rtd_theme) by |
| 31 | +[Read The Docs](https://readthedocs.org/). You can install it using the following command: |
| 32 | + |
| 33 | +```bash |
| 34 | +pip install sphinx_rtd_theme |
| 35 | +``` |
| 36 | + |
| 37 | +The third necessary package is the `recommonmark` package to accept Markdown as well as Restructured text: |
| 38 | + |
| 39 | +```bash |
| 40 | +pip install recommonmark |
| 41 | +``` |
| 42 | + |
| 43 | +## Building the documentation |
| 44 | + |
| 45 | +Once you have setup `sphinx`, you can build the documentation by running the following command in the `/docs` folder: |
| 46 | + |
| 47 | +```bash |
| 48 | +make html |
| 49 | +``` |
| 50 | + |
| 51 | +A folder called ``_build/html`` should have been created. You can now open the file ``_build/html/index.html`` in your |
| 52 | +browser. |
| 53 | + |
| 54 | +--- |
| 55 | +**NOTE** |
| 56 | + |
| 57 | +If you are adding/removing elements from the toc-tree or from any structural item, it is recommended to clean the build |
| 58 | +directory before rebuilding. Run the following command to clean and build: |
| 59 | + |
| 60 | +```bash |
| 61 | +make clean && make html |
| 62 | +``` |
| 63 | + |
| 64 | +--- |
| 65 | + |
| 66 | +It should build the static app that will be available under `/docs/_build/html` |
| 67 | + |
| 68 | +## Adding a new element to the tree (toc-tree) |
| 69 | + |
| 70 | +Accepted files are reStructuredText (.rst) and Markdown (.md). Create a file with its extension and put it |
| 71 | +in the source directory. You can then link it to the toc-tree by putting the filename without the extension. |
| 72 | + |
| 73 | +## Preview the documentation in a pull request |
| 74 | + |
| 75 | +Once you have made your pull request, you can check what the documentation will look like after it's merged by |
| 76 | +following these steps: |
| 77 | + |
| 78 | +- Look at the checks at the bottom of the conversation page of your PR (you may need to click on "show all checks" to |
| 79 | + expand them). |
| 80 | +- Click on "details" next to the `ci/circleci: build_doc` check. |
| 81 | +- In the new window, click on the "Artifacts" tab. |
| 82 | +- Locate the file "docs/_build/html/index.html" (or any specific page you want to check) and click on it to get a |
| 83 | + preview. |
| 84 | + |
| 85 | +## Writing Documentation - Specification |
| 86 | + |
| 87 | +The `huggingface/transformers` documentation follows the |
| 88 | +[Google documentation](https://sphinxcontrib-napoleon.readthedocs.io/en/latest/example_google.html) style. It is |
| 89 | +mostly written in ReStructuredText |
| 90 | +([Sphinx simple documentation](https://www.sphinx-doc.org/en/master/usage/restructuredtext/index.html), |
| 91 | +[Sourceforge complete documentation](https://docutils.sourceforge.io/docs/ref/rst/restructuredtext.html)) |
| 92 | + |
| 93 | +### Adding a new section |
| 94 | + |
| 95 | +A section is a page held in the `Notes` toc-tree on the documentation. Adding a new section is done in two steps: |
| 96 | + |
| 97 | +- Add a new file under `./source`. This file can either be ReStructuredText (.rst) or Markdown (.md). |
| 98 | +- Link that file in `./source/index.rst` on the correct toc-tree. |
| 99 | + |
| 100 | +### Adding a new model |
| 101 | + |
| 102 | +When adding a new model: |
| 103 | + |
| 104 | +- Create a file `xxx.rst` under `./source/model_doc`. |
| 105 | +- Link that file in `./source/index.rst` on the `model_doc` toc-tree. |
| 106 | +- Write a short overview of the model: |
| 107 | + - Overview with paper & authors |
| 108 | + - Paper abstract |
| 109 | + - Tips and tricks and how to use it best |
| 110 | +- Add the classes that should be linked in the model. This generally includes the configuration, the tokenizer, and |
| 111 | + every model of that class (the base model, alongside models with additional heads), both in PyTorch and TensorFlow. |
| 112 | + The order is generally: |
| 113 | + - Configuration, |
| 114 | + - Tokenizer |
| 115 | + - PyTorch base model |
| 116 | + - PyTorch head models |
| 117 | + - TensorFlow base model |
| 118 | + - TensorFlow head models |
| 119 | + |
| 120 | +These classes should be added using the RST syntax. Usually as follows: |
| 121 | +``` |
| 122 | +XXXConfig |
| 123 | +~~~~~~~~~~~~~~~~~~~~~ |
| 124 | +
|
| 125 | +.. autoclass:: transformers.XXXConfig |
| 126 | + :members: |
| 127 | +``` |
| 128 | + |
| 129 | +This will include every public method of the configuration. If for some reason you wish for a method not to be |
| 130 | +displayed in the documentation, you can do so by specifying which methods should be in the docs: |
| 131 | + |
| 132 | +``` |
| 133 | +XXXTokenizer |
| 134 | +~~~~~~~~~~~~~~~~~~~~~ |
| 135 | +
|
| 136 | +.. autoclass:: transformers.XXXTokenizer |
| 137 | + :members: build_inputs_with_special_tokens, get_special_tokens_mask, |
| 138 | + create_token_type_ids_from_sequences, save_vocabulary |
| 139 | +
|
| 140 | +``` |
| 141 | + |
| 142 | +### Writing source documentation |
| 143 | + |
| 144 | +Values that should be put in `code` should either be surrounded by double backticks: \`\`like so\`\` or be written as |
| 145 | +an object using the :obj: syntax: :obj:\`like so\`. |
| 146 | + |
| 147 | +When mentionning a class, it is recommended to use the :class: syntax as the mentioned class will be automatically |
| 148 | +linked by Sphinx: :class:\`transformers.XXXClass\` |
| 149 | + |
| 150 | +When mentioning a function, it is recommended to use the :func: syntax as the mentioned method will be automatically |
| 151 | +linked by Sphinx: :func:\`transformers.XXXClass.method\` |
| 152 | + |
| 153 | +Links should be done as so (note the double underscore at the end): \`text for the link <./local-link-or-global-link#loc>\`__ |
| 154 | + |
| 155 | +#### Defining arguments in a method |
| 156 | + |
| 157 | +Arguments should be defined with the `Args:` prefix, followed by a line return and an indentation. |
| 158 | +The argument should be followed by its type, with its shape if it is a tensor, and a line return. |
| 159 | +Another indentation is necessary before writing the description of the argument. |
| 160 | + |
| 161 | +Here's an example showcasing everything so far: |
| 162 | + |
| 163 | +``` |
| 164 | + Args: |
| 165 | + input_ids (:obj:`torch.LongTensor` of shape :obj:`(batch_size, sequence_length)`): |
| 166 | + Indices of input sequence tokens in the vocabulary. |
| 167 | +
|
| 168 | + Indices can be obtained using :class:`transformers.AlbertTokenizer`. |
| 169 | + See :func:`transformers.PreTrainedTokenizer.encode` and |
| 170 | + :func:`transformers.PreTrainedTokenizer.__call__` for details. |
| 171 | +
|
| 172 | + `What are input IDs? <../glossary.html#input-ids>`__ |
| 173 | +``` |
| 174 | + |
| 175 | +#### Writing a multi-line code block |
| 176 | + |
| 177 | +Multi-line code blocks can be useful for displaying examples. They are done like so: |
| 178 | + |
| 179 | +``` |
| 180 | +Example:: |
| 181 | +
|
| 182 | + # first line of code |
| 183 | + # second line |
| 184 | + # etc |
| 185 | +``` |
| 186 | + |
| 187 | +The `Example` string at the beginning can be replaced by anything as long as there are two semicolons following it. |
| 188 | + |
| 189 | +#### Writing a return block |
| 190 | + |
| 191 | +Arguments should be defined with the `Args:` prefix, followed by a line return and an indentation. |
| 192 | +The first line should be the type of the return, followed by a line return. No need to indent further for the elements |
| 193 | +building the return. |
| 194 | + |
| 195 | +Here's an example for tuple return, comprising several objects: |
| 196 | + |
| 197 | +``` |
| 198 | + Returns: |
| 199 | + :obj:`tuple(torch.FloatTensor)` comprising various elements depending on the configuration (:class:`~transformers.BertConfig`) and inputs: |
| 200 | + loss (`optional`, returned when ``masked_lm_labels`` is provided) ``torch.FloatTensor`` of shape ``(1,)``: |
| 201 | + Total loss as the sum of the masked language modeling loss and the next sequence prediction (classification) loss. |
| 202 | + prediction_scores (:obj:`torch.FloatTensor` of shape :obj:`(batch_size, sequence_length, config.vocab_size)`) |
| 203 | + Prediction scores of the language modeling head (scores for each vocabulary token before SoftMax). |
| 204 | +``` |
| 205 | + |
| 206 | +Here's an example for a single value return: |
| 207 | + |
| 208 | +``` |
| 209 | + Returns: |
| 210 | + A list of integers in the range [0, 1]: 1 for a special token, 0 for a sequence token. |
| 211 | +``` |
0 commit comments