README.md 4.75 KB
Newer Older
1
# How to add a new model in 馃 Transformers
thomwolf's avatar
thomwolf committed
2

3
This folder describes the process to add a new model in 馃 Transformers and provide templates for the required files.
thomwolf's avatar
thomwolf committed
4

5
6
7
The library is designed to incorporate a variety of models and code bases. As such the process for adding a new model
usually mostly consists in copy-pasting to relevant original code in the various sections of the templates included in
the present repository.
thomwolf's avatar
thomwolf committed
8
9
10

One important point though is that the library has the following goals impacting the way models are incorporated:

11
12
13
14
15
16
17
- One specific feature of the API is the capability to run the model and tokenizer inline. The tokenization code thus
  often have to be slightly adapted to allow for running in the python interpreter.
- the package is also designed to be as self-consistent and with a small and reliable set of packages dependencies. In
  consequence, additional dependencies are usually not allowed when adding a model but can be allowed for the
  inclusion of a new tokenizer (recent examples of dependencies added for tokenizer specificities include
  `sentencepiece` and `sacremoses`). Please make sure to check the existing dependencies when possible before adding a
  new one.
thomwolf's avatar
thomwolf committed
18

19
20
For a quick overview of the general philosphy of the library and its organization, please check the
[QuickStart section of the documentation](https://huggingface.co/transformers/philosophy.html).
thomwolf's avatar
thomwolf committed
21
22
23
24
25

# Typical workflow for including a model

Here an overview of the general workflow: 

26
27
28
29
30
- [ ] Add model/configuration/tokenization classes.
- [ ] Add conversion scripts.
- [ ] Add tests and a @slow integration test.
- [ ] Document your model.
- [ ] Finalize.
thomwolf's avatar
thomwolf committed
31

32
Let's detail what should be done at each step.
thomwolf's avatar
thomwolf committed
33
34
35
36
37

## Adding model/configuration/tokenization classes

Here is the workflow for adding model/configuration/tokenization classes:

38
39
40
41
42
43
- [ ] Copy the python files from the present folder to the main folder and rename them, replacing `xxx` with your model
  name.
- [ ] Edit the files to replace `XXX` (with various casing) with your model name.
- [ ] Copy-paste or create a simple configuration class for your model in the `configuration_...` file.
- [ ] Copy-paste or create the code for your model in the `modeling_...` files (PyTorch and TF 2.0).
- [ ] Copy-paste or create a tokenizer class for your model in the `tokenization_...` file.
thomwolf's avatar
thomwolf committed
44

45
## Adding conversion scripts
thomwolf's avatar
thomwolf committed
46
47
48

Here is the workflow for the conversion scripts:

49
50
- [ ] Copy the conversion script (`convert_...`) from the present folder to the main folder.
- [ ] Edit this script to convert your original checkpoint weights to the current pytorch ones.
thomwolf's avatar
thomwolf committed
51

52
## Adding tests:
thomwolf's avatar
thomwolf committed
53
54
55

Here is the workflow for the adding tests:

56
57
58
59
- [ ] Copy the python files from the `tests` sub-folder of the present folder to the `tests` subfolder of the main
  folder and rename them, replacing `xxx` with your model name.
- [ ] Edit the tests files to replace `XXX` (with various casing) with your model name.
- [ ] Edit the tests code as needed.
thomwolf's avatar
thomwolf committed
60

61
62
63
64
65
66
67
68
69
70
71
72
73
74
## Documenting your model:

Here is the workflow for documentation:

- [ ] Make sure all your arguments are properly documened in your configuration and tokenizer.
- [ ] Most of the documentation of the models is automatically generated, you just ahve to male sure that
  `XXX_START_DOCSTRING` contains an introduction to the model you're adding and a link to the original
  article and that `XXX_INPUTS_DOCSTRING` contains all the inputs of your model.
- [ ] Create a new page `xxx.rst` in the folder `docs/source/model_doc` and add this file in `docs/source/index.rst`.

Make sure to check you have no sphinx warnings when building the documentation locally and follow our
[documentaiton guide](https://github.com/huggingface/transformers/tree/master/docs#writing-documentation---specification).

## Final steps
thomwolf's avatar
thomwolf committed
75
76
77

You can then finish the addition step by adding imports for your classes in the common files:

78
79
80
81
82
83
84
85
86
87
88
89
90
- [ ] Add import for all the relevant classes in `__init__.py`.
- [ ] Add your configuration in `configuration_auto.py`.
- [ ] Add your PyTorch and TF 2.0 model respectively in `modeling_auto.py` and `modeling_tf_auto.py`.
- [ ] Add your tokenizer in `tokenization_auto.py`.
- [ ] Add your models and tokenizer to `pipeline.py`.
- [ ] Add a link to your conversion script in the main conversion utility (in `commands/convert.py`)
- [ ] Edit the PyTorch to TF 2.0 conversion script to add your model in the `convert_pytorch_checkpoint_to_tf2.py`
  file.
- [ ] Add a mention of your model in the doc: `README.md` and the documentation itself
  in `docs/source/index.rst` and `docs/source/pretrained_models.rst`.
- [ ] Upload the pretrained weights, configurations and vocabulary files.
- [ ] Create model card(s) for your models on huggingface.co. For those last two steps, check the
  [model sharing documentation](https://huggingface.co/transformers/model_sharing.html).