Files
HuggingFace_transformer/docs
Ola Piktus c754c41c61 RAG (#6813)
* added rag WIP

* path fix

* Formatting / renaming prior to actual work

* added rag WIP

* path fix

* Formatting / renaming prior to actual work

* added rag WIP

* path fix

* Formatting / renaming prior to actual work

* added rag WIP

* Formatting / renaming prior to actual work

* First commit

* improve comments

* Retrieval evaluation scripts

* refactor to include modeling outputs + MPI retriever

* Fix rag-token model + refactor

* Various fixes + finetuning logic

* use_bos fix

* Retrieval refactor

* Finetuning refactoring and cleanup

* Add documentation and cleanup

* Remove set_up_rag_env.sh file

* Fix retrieval wit HF index

* Fix import errors

* Fix quality errors

* Refactor as per suggestions in https://github.com/huggingface/transformers/pull/6813#issuecomment-687208867

* fix quality

* Fix RAG Sequence generation

* minor cleanup plus initial tests

* fix test

* fix tests 2

* Comments fix

* post-merge fixes

* Improve readme + post-rebase refactor

* Extra dependencied for tests

* Fix tests

* Fix tests 2

* Refactor test requirements

* Fix tests 3

* Post-rebase refactor

* rename nlp->datasets

* RAG integration tests

* add tokenizer to slow integration test and allow retriever to run on cpu

* add tests; fix position ids warning

* change structure

* change structure

* add from encoder generator

* save working solution

* make all integration tests pass

* add RagTokenizer.save/from_pretrained and RagRetriever.save/from_pretrained

* don't save paths

* delete unnecessary imports

* pass config to AutoTokenizer.from_pretrained for Rag tokenizers

* init wiki_dpr only once

* hardcode legacy index and passages paths (todo: add the right urls)

* finalize config

* finalize retriver api and config api

* LegacyIndex index download refactor

* add dpr to autotokenizer

* make from pretrained more flexible

* fix ragfortokengeneration

* small name changes in tokenizer

* add labels to models

* change default index name

* add retrieval tests

* finish token generate

* align test with previous version and make all tests pass

* add tests

* finalize tests

* implement thoms suggestions

* add first version of test

* make first tests work

* make retriever platform agnostic

* naming

* style

* add legacy index URL

* docstrings + simple retrieval test for distributed

* clean model api

* add doc_ids to retriever's outputs

* fix retrieval tests

* finish model outputs

* finalize model api

* fix generate problem for rag

* fix generate for other modles

* fix some tests

* save intermediate

* set generate to default

* big refactor generate

* delete rag_api

* correct pip faiss install

* fix auto tokenization test

* fix faiss install

* fix test

* move the distributed logic to examples

* model page

* docs

* finish tests

* fix dependencies

* fix import in __init__

* Refactor eval_rag and finetune scripts

* start docstring

* add psutil to test

* fix tf test

* move require torch to top

* fix retrieval test

* align naming

* finish automodel

* fix repo consistency

* test ragtokenizer save/load

* add rag model output docs

* fix ragtokenizer save/load from pretrained

* fix tokenizer dir

* remove torch in retrieval

* fix docs

* fixe finetune scripts

* finish model docs

* finish docs

* remove auto model for now

* add require torch

* remove solved todos

* integrate sylvains suggestions

* sams comments

* correct mistake on purpose

* improve README

* Add generation test cases

* fix rag token

* clean token generate

* fix test

* add note to test

* fix attention mask

* add t5 test for rag

* Fix handling prefix in finetune.py

* don't overwrite index_name

Co-authored-by: Patrick Lewis <plewis@fb.com>
Co-authored-by: Aleksandra Piktus <piktus@devfair0141.h2.fair>
Co-authored-by: Aleksandra Piktus <piktus@learnfair5102.h2.fair>
Co-authored-by: Aleksandra Piktus <piktus@learnfair5067.h2.fair>
Co-authored-by: Your Name <you@example.com>
Co-authored-by: Patrick von Platen <patrick.v.platen@gmail.com>
Co-authored-by: Quentin Lhoest <lhoest.q@gmail.com>
2020-09-22 18:29:58 +02:00
..
2020-09-22 18:29:58 +02:00

Generating the documentation

To generate the documentation, you first have to build it. Several packages are necessary to build the doc, you can install them with the following command, at the root of the code repository:

pip install -e ".[docs]"

NOTE

You only need to generate the documentation to inspect it locally (if you're planning changes and want to check how they look like before committing for instance). You don't have to commit the built documentation.


Packages installed

Here's an overview of all the packages installed. If you ran the previous command installing all packages from requirements.txt, you do not need to run the following commands.

Building it requires the package sphinx that you can install using:

pip install -U sphinx

You would also need the custom installed theme by Read The Docs. You can install it using the following command:

pip install sphinx_rtd_theme

The third necessary package is the recommonmark package to accept Markdown as well as Restructured text:

pip install recommonmark

Building the documentation

Once you have setup sphinx, you can build the documentation by running the following command in the /docs folder:

make html

A folder called _build/html should have been created. You can now open the file _build/html/index.html in your browser.


NOTE

If you are adding/removing elements from the toc-tree or from any structural item, it is recommended to clean the build directory before rebuilding. Run the following command to clean and build:

make clean && make html

It should build the static app that will be available under /docs/_build/html

Adding a new element to the tree (toc-tree)

Accepted files are reStructuredText (.rst) and Markdown (.md). Create a file with its extension and put it in the source directory. You can then link it to the toc-tree by putting the filename without the extension.

Preview the documentation in a pull request

Once you have made your pull request, you can check what the documentation will look like after it's merged by following these steps:

  • Look at the checks at the bottom of the conversation page of your PR (you may need to click on "show all checks" to expand them).
  • Click on "details" next to the ci/circleci: build_doc check.
  • In the new window, click on the "Artifacts" tab.
  • Locate the file "docs/_build/html/index.html" (or any specific page you want to check) and click on it to get a preview.

Writing Documentation - Specification

The huggingface/transformers documentation follows the Google documentation style. It is mostly written in ReStructuredText (Sphinx simple documentation, Sourceforge complete documentation)

Adding a new section

A section is a page held in the Notes toc-tree on the documentation. Adding a new section is done in two steps:

  • Add a new file under ./source. This file can either be ReStructuredText (.rst) or Markdown (.md).
  • Link that file in ./source/index.rst on the correct toc-tree.

Adding a new model

When adding a new model:

  • Create a file xxx.rst under ./source/model_doc.
  • Link that file in ./source/index.rst on the model_doc toc-tree.
  • Write a short overview of the model:
    • Overview with paper & authors
    • Paper abstract
    • Tips and tricks and how to use it best
  • Add the classes that should be linked in the model. This generally includes the configuration, the tokenizer, and every model of that class (the base model, alongside models with additional heads), both in PyTorch and TensorFlow. The order is generally:
    • Configuration,
    • Tokenizer
    • PyTorch base model
    • PyTorch head models
    • TensorFlow base model
    • TensorFlow head models

These classes should be added using the RST syntax. Usually as follows:

XXXConfig
~~~~~~~~~~~~~~~~~~~~~

.. autoclass:: transformers.XXXConfig
    :members:

This will include every public method of the configuration. If for some reason you wish for a method not to be displayed in the documentation, you can do so by specifying which methods should be in the docs:

XXXTokenizer
~~~~~~~~~~~~~~~~~~~~~

.. autoclass:: transformers.XXXTokenizer
    :members: build_inputs_with_special_tokens, get_special_tokens_mask,
        create_token_type_ids_from_sequences, save_vocabulary

Writing source documentation

Values that should be put in code should either be surrounded by double backticks: ``like so`` or be written as an object using the :obj: syntax: :obj:`like so`.

When mentionning a class, it is recommended to use the :class: syntax as the mentioned class will be automatically linked by Sphinx: :class:`transformers.XXXClass`

When mentioning a function, it is recommended to use the :func: syntax as the mentioned method will be automatically linked by Sphinx: :func:`transformers.XXXClass.method`

Links should be done as so (note the double underscore at the end): `text for the link <./local-link-or-global-link#loc>`__

Defining arguments in a method

Arguments should be defined with the Args: prefix, followed by a line return and an indentation. The argument should be followed by its type, with its shape if it is a tensor, and a line return. Another indentation is necessary before writing the description of the argument.

Here's an example showcasing everything so far:

    Args:
        input_ids (:obj:`torch.LongTensor` of shape :obj:`(batch_size, sequence_length)`):
            Indices of input sequence tokens in the vocabulary.

            Indices can be obtained using :class:`transformers.AlbertTokenizer`.
            See :func:`transformers.PreTrainedTokenizer.encode` and
            :func:`transformers.PreTrainedTokenizer.__call__` for details.

            `What are input IDs? <../glossary.html#input-ids>`__

Writing a multi-line code block

Multi-line code blocks can be useful for displaying examples. They are done like so:

Example::

    # first line of code
    # second line
    # etc

The Example string at the beginning can be replaced by anything as long as there are two semicolons following it.

Writing a return block

Arguments should be defined with the Args: prefix, followed by a line return and an indentation. The first line should be the type of the return, followed by a line return. No need to indent further for the elements building the return.

Here's an example for tuple return, comprising several objects:

    Returns:
        :obj:`tuple(torch.FloatTensor)` comprising various elements depending on the configuration (:class:`~transformers.BertConfig`) and inputs:
        loss (`optional`, returned when ``masked_lm_labels`` is provided) ``torch.FloatTensor`` of shape ``(1,)``:
            Total loss as the sum of the masked language modeling loss and the next sequence prediction (classification) loss.
        prediction_scores (:obj:`torch.FloatTensor` of shape :obj:`(batch_size, sequence_length, config.vocab_size)`)
            Prediction scores of the language modeling head (scores for each vocabulary token before SoftMax).

Here's an example for a single value return:

    Returns:
        A list of integers in the range [0, 1]: 1 for a special token, 0 for a sequence token.