Spaces:
Runtime error
Runtime error
git subrepo clone (merge) --branch=exbert-mods https://github.com/bhoov/transformers.git server/transformers
Browse filessubrepo:
subdir: "server/transformers"
merged: "a0b899d1"
upstream:
origin: "https://github.com/bhoov/transformers.git"
branch: "exbert-mods"
commit: "a0b899d1"
git-subrepo:
version: "0.4.1"
origin: "https://github.com/ingydotnet/git-subrepo"
commit: "a04d8c2"
This view is limited to 50 files because it contains too many changes.
See raw diff
- server/transformers/.circleci/config.yml +143 -0
- server/transformers/.circleci/deploy.sh +28 -0
- server/transformers/.coveragerc +12 -0
- server/transformers/.github/ISSUE_TEMPLATE/---new-benchmark.md +22 -0
- server/transformers/.github/ISSUE_TEMPLATE/--new-model-addition.md +20 -0
- server/transformers/.github/ISSUE_TEMPLATE/bug-report.md +52 -0
- server/transformers/.github/ISSUE_TEMPLATE/feature-request.md +25 -0
- server/transformers/.github/ISSUE_TEMPLATE/migration.md +57 -0
- server/transformers/.github/ISSUE_TEMPLATE/question-help.md +29 -0
- server/transformers/.github/stale.yml +17 -0
- server/transformers/.gitignore +141 -0
- server/transformers/.gitrepo +12 -0
- server/transformers/CONTRIBUTING.md +258 -0
- server/transformers/LICENSE +202 -0
- server/transformers/MANIFEST.in +1 -0
- server/transformers/Makefile +24 -0
- server/transformers/README.md +684 -0
- server/transformers/deploy_multi_version_doc.sh +23 -0
- server/transformers/docker/Dockerfile +7 -0
- server/transformers/docs/Makefile +19 -0
- server/transformers/docs/README.md +67 -0
- server/transformers/docs/source/_static/css/Calibre-Light.ttf +0 -0
- server/transformers/docs/source/_static/css/Calibre-Medium.otf +0 -0
- server/transformers/docs/source/_static/css/Calibre-Regular.otf +0 -0
- server/transformers/docs/source/_static/css/Calibre-Thin.otf +0 -0
- server/transformers/docs/source/_static/css/code-snippets.css +12 -0
- server/transformers/docs/source/_static/css/huggingface.css +196 -0
- server/transformers/docs/source/_static/js/custom.js +79 -0
- server/transformers/docs/source/_static/js/huggingface_logo.svg +47 -0
- server/transformers/docs/source/benchmarks.md +54 -0
- server/transformers/docs/source/bertology.rst +18 -0
- server/transformers/docs/source/conf.py +188 -0
- server/transformers/docs/source/converting_tensorflow_models.rst +137 -0
- server/transformers/docs/source/examples.md +1 -0
- server/transformers/docs/source/glossary.rst +145 -0
- server/transformers/docs/source/imgs/transformers_logo_name.png +0 -0
- server/transformers/docs/source/imgs/warmup_constant_schedule.png +0 -0
- server/transformers/docs/source/imgs/warmup_cosine_hard_restarts_schedule.png +0 -0
- server/transformers/docs/source/imgs/warmup_cosine_schedule.png +0 -0
- server/transformers/docs/source/imgs/warmup_cosine_warm_restarts_schedule.png +0 -0
- server/transformers/docs/source/imgs/warmup_linear_schedule.png +0 -0
- server/transformers/docs/source/index.rst +102 -0
- server/transformers/docs/source/installation.md +51 -0
- server/transformers/docs/source/main_classes/configuration.rst +10 -0
- server/transformers/docs/source/main_classes/model.rst +21 -0
- server/transformers/docs/source/main_classes/optimizer_schedules.rst +72 -0
- server/transformers/docs/source/main_classes/processors.rst +153 -0
- server/transformers/docs/source/main_classes/tokenizer.rst +16 -0
- server/transformers/docs/source/migration.md +109 -0
- server/transformers/docs/source/model_doc/albert.rst +93 -0
server/transformers/.circleci/config.yml
ADDED
@@ -0,0 +1,143 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
version: 2
|
2 |
+
jobs:
|
3 |
+
run_tests_torch_and_tf:
|
4 |
+
working_directory: ~/transformers
|
5 |
+
docker:
|
6 |
+
- image: circleci/python:3.5
|
7 |
+
environment:
|
8 |
+
OMP_NUM_THREADS: 1
|
9 |
+
resource_class: xlarge
|
10 |
+
parallelism: 1
|
11 |
+
steps:
|
12 |
+
- checkout
|
13 |
+
- run: sudo pip install .[sklearn,tf,torch,testing]
|
14 |
+
- run: sudo pip install codecov pytest-cov
|
15 |
+
- run: python -m pytest -n 8 --dist=loadfile -s -v ./tests/ --cov
|
16 |
+
- run: codecov
|
17 |
+
run_all_tests_torch_and_tf:
|
18 |
+
working_directory: ~/transformers
|
19 |
+
docker:
|
20 |
+
- image: circleci/python:3.5
|
21 |
+
environment:
|
22 |
+
OMP_NUM_THREADS: 1
|
23 |
+
RUN_SLOW: yes
|
24 |
+
RUN_CUSTOM_TOKENIZERS: yes
|
25 |
+
resource_class: xlarge
|
26 |
+
parallelism: 1
|
27 |
+
steps:
|
28 |
+
- checkout
|
29 |
+
- run: sudo pip install .[mecab,sklearn,tf,torch,testing]
|
30 |
+
- run: python -m pytest -n 8 --dist=loadfile -s -v ./tests/
|
31 |
+
run_tests_torch:
|
32 |
+
working_directory: ~/transformers
|
33 |
+
docker:
|
34 |
+
- image: circleci/python:3.7
|
35 |
+
environment:
|
36 |
+
OMP_NUM_THREADS: 1
|
37 |
+
resource_class: xlarge
|
38 |
+
parallelism: 1
|
39 |
+
steps:
|
40 |
+
- checkout
|
41 |
+
- run: sudo pip install .[sklearn,torch,testing]
|
42 |
+
- run: sudo pip install codecov pytest-cov
|
43 |
+
- run: python -m pytest -n 8 --dist=loadfile -s -v ./tests/ --cov
|
44 |
+
- run: codecov
|
45 |
+
run_tests_tf:
|
46 |
+
working_directory: ~/transformers
|
47 |
+
docker:
|
48 |
+
- image: circleci/python:3.7
|
49 |
+
environment:
|
50 |
+
OMP_NUM_THREADS: 1
|
51 |
+
resource_class: xlarge
|
52 |
+
parallelism: 1
|
53 |
+
steps:
|
54 |
+
- checkout
|
55 |
+
- run: sudo pip install .[sklearn,tf,testing]
|
56 |
+
- run: sudo pip install codecov pytest-cov
|
57 |
+
- run: python -m pytest -n 8 --dist=loadfile -s -v ./tests/ --cov
|
58 |
+
- run: codecov
|
59 |
+
run_tests_custom_tokenizers:
|
60 |
+
working_directory: ~/transformers
|
61 |
+
docker:
|
62 |
+
- image: circleci/python:3.5
|
63 |
+
environment:
|
64 |
+
RUN_CUSTOM_TOKENIZERS: yes
|
65 |
+
steps:
|
66 |
+
- checkout
|
67 |
+
- run: sudo pip install .[mecab,testing]
|
68 |
+
- run: python -m pytest -sv ./tests/test_tokenization_bert_japanese.py
|
69 |
+
run_examples_torch:
|
70 |
+
working_directory: ~/transformers
|
71 |
+
docker:
|
72 |
+
- image: circleci/python:3.5
|
73 |
+
environment:
|
74 |
+
OMP_NUM_THREADS: 1
|
75 |
+
resource_class: xlarge
|
76 |
+
parallelism: 1
|
77 |
+
steps:
|
78 |
+
- checkout
|
79 |
+
- run: sudo pip install .[sklearn,torch,testing]
|
80 |
+
- run: sudo pip install -r examples/requirements.txt
|
81 |
+
- run: python -m pytest -n 8 --dist=loadfile -s -v ./examples/
|
82 |
+
deploy_doc:
|
83 |
+
working_directory: ~/transformers
|
84 |
+
docker:
|
85 |
+
- image: circleci/python:3.5
|
86 |
+
steps:
|
87 |
+
- add_ssh_keys:
|
88 |
+
fingerprints:
|
89 |
+
- "5b:7a:95:18:07:8c:aa:76:4c:60:35:88:ad:60:56:71"
|
90 |
+
- checkout
|
91 |
+
- run: sudo pip install .[tf,torch,docs]
|
92 |
+
- run: ./.circleci/deploy.sh
|
93 |
+
check_code_quality:
|
94 |
+
working_directory: ~/transformers
|
95 |
+
docker:
|
96 |
+
- image: circleci/python:3.6
|
97 |
+
resource_class: medium
|
98 |
+
parallelism: 1
|
99 |
+
steps:
|
100 |
+
- checkout
|
101 |
+
# we need a version of isort with https://github.com/timothycrosley/isort/pull/1000
|
102 |
+
- run: sudo pip install git+git://github.com/timothycrosley/isort.git@e63ae06ec7d70b06df9e528357650281a3d3ec22#egg=isort
|
103 |
+
- run: sudo pip install .[tf,torch,quality]
|
104 |
+
- run: black --check --line-length 119 --target-version py35 examples templates tests src utils
|
105 |
+
- run: isort --check-only --recursive examples templates tests src utils
|
106 |
+
- run: flake8 examples templates tests src utils
|
107 |
+
check_repository_consistency:
|
108 |
+
working_directory: ~/transformers
|
109 |
+
docker:
|
110 |
+
- image: circleci/python:3.5
|
111 |
+
resource_class: small
|
112 |
+
parallelism: 1
|
113 |
+
steps:
|
114 |
+
- checkout
|
115 |
+
- run: sudo pip install requests
|
116 |
+
- run: python ./utils/link_tester.py
|
117 |
+
workflow_filters: &workflow_filters
|
118 |
+
filters:
|
119 |
+
branches:
|
120 |
+
only:
|
121 |
+
- master
|
122 |
+
workflows:
|
123 |
+
version: 2
|
124 |
+
build_and_test:
|
125 |
+
jobs:
|
126 |
+
- check_code_quality
|
127 |
+
- check_repository_consistency
|
128 |
+
- run_examples_torch
|
129 |
+
- run_tests_custom_tokenizers
|
130 |
+
- run_tests_torch_and_tf
|
131 |
+
- run_tests_torch
|
132 |
+
- run_tests_tf
|
133 |
+
- deploy_doc: *workflow_filters
|
134 |
+
run_slow_tests:
|
135 |
+
triggers:
|
136 |
+
- schedule:
|
137 |
+
cron: "0 4 * * 1"
|
138 |
+
filters:
|
139 |
+
branches:
|
140 |
+
only:
|
141 |
+
- master
|
142 |
+
jobs:
|
143 |
+
- run_all_tests_torch_and_tf
|
server/transformers/.circleci/deploy.sh
ADDED
@@ -0,0 +1,28 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
cd docs
|
2 |
+
|
3 |
+
function deploy_doc(){
|
4 |
+
echo "Creating doc at commit $1 and pushing to folder $2"
|
5 |
+
git checkout $1
|
6 |
+
if [ ! -z "$2" ]
|
7 |
+
then
|
8 |
+
if [ -d "$dir/$2" ]; then
|
9 |
+
echo "Directory" $2 "already exists"
|
10 |
+
else
|
11 |
+
echo "Pushing version" $2
|
12 |
+
make clean && make html && scp -r -oStrictHostKeyChecking=no _build/html $doc:$dir/$2
|
13 |
+
fi
|
14 |
+
else
|
15 |
+
echo "Pushing master"
|
16 |
+
make clean && make html && scp -r -oStrictHostKeyChecking=no _build/html/* $doc:$dir
|
17 |
+
fi
|
18 |
+
}
|
19 |
+
|
20 |
+
deploy_doc "master"
|
21 |
+
deploy_doc "b33a385" v1.0.0
|
22 |
+
deploy_doc "fe02e45" v1.1.0
|
23 |
+
deploy_doc "89fd345" v1.2.0
|
24 |
+
deploy_doc "fc9faa8" v2.0.0
|
25 |
+
deploy_doc "3ddce1d" v2.1.1
|
26 |
+
deploy_doc "3616209" v2.2.0
|
27 |
+
deploy_doc "d0f8b9a" v2.3.0
|
28 |
+
deploy_doc "6664ea9" v2.4.0
|
server/transformers/.coveragerc
ADDED
@@ -0,0 +1,12 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
[run]
|
2 |
+
source=transformers
|
3 |
+
omit =
|
4 |
+
# skip convertion scripts from testing for now
|
5 |
+
*/convert_*
|
6 |
+
*/__main__.py
|
7 |
+
[report]
|
8 |
+
exclude_lines =
|
9 |
+
pragma: no cover
|
10 |
+
raise
|
11 |
+
except
|
12 |
+
register_parameter
|
server/transformers/.github/ISSUE_TEMPLATE/---new-benchmark.md
ADDED
@@ -0,0 +1,22 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
---
|
2 |
+
name: "\U0001F5A5 New benchmark"
|
3 |
+
about: Benchmark a part of this library and share your results
|
4 |
+
title: "[Benchmark]"
|
5 |
+
labels: ''
|
6 |
+
assignees: ''
|
7 |
+
|
8 |
+
---
|
9 |
+
|
10 |
+
# 🖥 Benchmarking `transformers`
|
11 |
+
|
12 |
+
## Benchmark
|
13 |
+
|
14 |
+
Which part of `transformers` did you benchmark?
|
15 |
+
|
16 |
+
## Set-up
|
17 |
+
|
18 |
+
What did you run your benchmarks on? Please include details, such as: CPU, GPU? If using multiple GPUs, which parallelization did you use?
|
19 |
+
|
20 |
+
## Results
|
21 |
+
|
22 |
+
Put your results here!
|
server/transformers/.github/ISSUE_TEMPLATE/--new-model-addition.md
ADDED
@@ -0,0 +1,20 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
---
|
2 |
+
name: "\U0001F31F New model addition"
|
3 |
+
about: Submit a proposal/request to implement a new Transformer-based model
|
4 |
+
title: ''
|
5 |
+
labels: ''
|
6 |
+
assignees: ''
|
7 |
+
|
8 |
+
---
|
9 |
+
|
10 |
+
# 🌟 New model addition
|
11 |
+
|
12 |
+
## Model description
|
13 |
+
|
14 |
+
<!-- Important information -->
|
15 |
+
|
16 |
+
## Open source status
|
17 |
+
|
18 |
+
* [ ] the model implementation is available: (give details)
|
19 |
+
* [ ] the model weights are available: (give details)
|
20 |
+
* [ ] who are the authors: (mention them, if possible by @gh-username)
|
server/transformers/.github/ISSUE_TEMPLATE/bug-report.md
ADDED
@@ -0,0 +1,52 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
---
|
2 |
+
name: "\U0001F41B Bug Report"
|
3 |
+
about: Submit a bug report to help us improve transformers
|
4 |
+
title: ''
|
5 |
+
labels: ''
|
6 |
+
assignees: ''
|
7 |
+
|
8 |
+
---
|
9 |
+
|
10 |
+
# 🐛 Bug
|
11 |
+
|
12 |
+
## Information
|
13 |
+
|
14 |
+
Model I am using (Bert, XLNet ...):
|
15 |
+
|
16 |
+
Language I am using the model on (English, Chinese ...):
|
17 |
+
|
18 |
+
The problem arises when using:
|
19 |
+
* [ ] the official example scripts: (give details below)
|
20 |
+
* [ ] my own modified scripts: (give details below)
|
21 |
+
|
22 |
+
The tasks I am working on is:
|
23 |
+
* [ ] an official GLUE/SQUaD task: (give the name)
|
24 |
+
* [ ] my own task or dataset: (give details below)
|
25 |
+
|
26 |
+
## To reproduce
|
27 |
+
|
28 |
+
Steps to reproduce the behavior:
|
29 |
+
|
30 |
+
1.
|
31 |
+
2.
|
32 |
+
3.
|
33 |
+
|
34 |
+
<!-- If you have code snippets, error messages, stack traces please provide them here as well.
|
35 |
+
Important! Use code tags to correctly format your code. See https://help.github.com/en/github/writing-on-github/creating-and-highlighting-code-blocks#syntax-highlighting
|
36 |
+
Do not use screenshots, as they are hard to read and (more importantly) don't allow others to copy-and-paste your code.-->
|
37 |
+
|
38 |
+
## Expected behavior
|
39 |
+
|
40 |
+
<!-- A clear and concise description of what you would expect to happen. -->
|
41 |
+
|
42 |
+
## Environment info
|
43 |
+
<!-- You can run the command `python transformers-cli env` and copy-and-paste its output below.
|
44 |
+
Don't forget to fill out the missing fields in that output! -->
|
45 |
+
|
46 |
+
- `transformers` version:
|
47 |
+
- Platform:
|
48 |
+
- Python version:
|
49 |
+
- PyTorch version (GPU?):
|
50 |
+
- Tensorflow version (GPU?):
|
51 |
+
- Using GPU in script?:
|
52 |
+
- Using distributed or parallel set-up in script?:
|
server/transformers/.github/ISSUE_TEMPLATE/feature-request.md
ADDED
@@ -0,0 +1,25 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
---
|
2 |
+
name: "\U0001F680 Feature request"
|
3 |
+
about: Submit a proposal/request for a new transformers feature
|
4 |
+
title: ''
|
5 |
+
labels: ''
|
6 |
+
assignees: ''
|
7 |
+
|
8 |
+
---
|
9 |
+
|
10 |
+
# 🚀 Feature request
|
11 |
+
|
12 |
+
<!-- A clear and concise description of the feature proposal.
|
13 |
+
Please provide a link to the paper and code in case they exist. -->
|
14 |
+
|
15 |
+
## Motivation
|
16 |
+
|
17 |
+
<!-- Please outline the motivation for the proposal. Is your feature request
|
18 |
+
related to a problem? e.g., I'm always frustrated when [...]. If this is related
|
19 |
+
to another GitHub issue, please link here too. -->
|
20 |
+
|
21 |
+
## Your contribution
|
22 |
+
|
23 |
+
<!-- Is there any way that you could help, e.g. by submitting a PR?
|
24 |
+
Make sure to read the CONTRIBUTING.MD readme:
|
25 |
+
https://github.com/huggingface/transformers/blob/master/CONTRIBUTING.md -->
|
server/transformers/.github/ISSUE_TEMPLATE/migration.md
ADDED
@@ -0,0 +1,57 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
---
|
2 |
+
name: "\U0001F4DA Migration from pytorch-pretrained-bert or pytorch-transformers"
|
3 |
+
about: Report a problem when migrating from pytorch-pretrained-bert or pytorch-transformers to transformers
|
4 |
+
title: ''
|
5 |
+
labels: ''
|
6 |
+
assignees: ''
|
7 |
+
|
8 |
+
---
|
9 |
+
|
10 |
+
# 📚 Migration
|
11 |
+
|
12 |
+
## Information
|
13 |
+
|
14 |
+
<!-- Important information -->
|
15 |
+
|
16 |
+
Model I am using (Bert, XLNet ...):
|
17 |
+
|
18 |
+
Language I am using the model on (English, Chinese ...):
|
19 |
+
|
20 |
+
The problem arises when using:
|
21 |
+
* [ ] the official example scripts: (give details below)
|
22 |
+
* [ ] my own modified scripts: (give details below)
|
23 |
+
|
24 |
+
The tasks I am working on is:
|
25 |
+
* [ ] an official GLUE/SQUaD task: (give the name)
|
26 |
+
* [ ] my own task or dataset: (give details below)
|
27 |
+
|
28 |
+
## Details
|
29 |
+
|
30 |
+
<!-- A clear and concise description of the migration issue.
|
31 |
+
If you have code snippets, please provide it here as well.
|
32 |
+
Important! Use code tags to correctly format your code. See https://help.github.com/en/github/writing-on-github/creating-and-highlighting-code-blocks#syntax-highlighting
|
33 |
+
Do not use screenshots, as they are hard to read and (more importantly) don't allow others to copy-and-paste your code.
|
34 |
+
-->
|
35 |
+
|
36 |
+
## Environment info
|
37 |
+
<!-- You can run the command `python transformers-cli env` and copy-and-paste its output below.
|
38 |
+
Don't forget to fill out the missing fields in that output! -->
|
39 |
+
|
40 |
+
- `transformers` version:
|
41 |
+
- Platform:
|
42 |
+
- Python version:
|
43 |
+
- PyTorch version (GPU?):
|
44 |
+
- Tensorflow version (GPU?):
|
45 |
+
- Using GPU in script?:
|
46 |
+
- Using distributed or parallel set-up in script?:
|
47 |
+
|
48 |
+
<!-- IMPORTANT: which version of the former library do you use? -->
|
49 |
+
* `pytorch-transformers` or `pytorch-pretrained-bert` version (or branch):
|
50 |
+
|
51 |
+
|
52 |
+
## Checklist
|
53 |
+
|
54 |
+
- [ ] I have read the migration guide in the readme.
|
55 |
+
([pytorch-transformers](https://github.com/huggingface/transformers#migrating-from-pytorch-transformers-to-transformers);
|
56 |
+
[pytorch-pretrained-bert](https://github.com/huggingface/transformers#migrating-from-pytorch-pretrained-bert-to-transformers))
|
57 |
+
- [ ] I checked if a related official extension example runs on my machine.
|
server/transformers/.github/ISSUE_TEMPLATE/question-help.md
ADDED
@@ -0,0 +1,29 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
---
|
2 |
+
name: "❓ Questions & Help"
|
3 |
+
about: Post your general questions on Stack Overflow tagged huggingface-transformers
|
4 |
+
title: ''
|
5 |
+
labels: ''
|
6 |
+
assignees: ''
|
7 |
+
|
8 |
+
---
|
9 |
+
|
10 |
+
# ❓ Questions & Help
|
11 |
+
|
12 |
+
<!-- The GitHub issue tracker is primarly intended for bugs, feature requests,
|
13 |
+
new models and benchmarks, and migration questions. For all other questions,
|
14 |
+
we direct you to Stack Overflow (SO) where a whole community of PyTorch and
|
15 |
+
Tensorflow enthusiast can help you out. Make sure to tag your question with the
|
16 |
+
right deep learning framework as well as the huggingface-transformers tag:
|
17 |
+
https://stackoverflow.com/questions/tagged/huggingface-transformers
|
18 |
+
|
19 |
+
If your question wasn't answered after a period of time on Stack Overflow, you
|
20 |
+
can always open a question on GitHub. You should then link to the SO question
|
21 |
+
that you posted.
|
22 |
+
-->
|
23 |
+
|
24 |
+
## Details
|
25 |
+
<!-- Description of your issue -->
|
26 |
+
|
27 |
+
<!-- You should first ask your question on SO, and only if
|
28 |
+
you didn't get an answer ask it here on GitHub. -->
|
29 |
+
**A link to original question on Stack Overflow**:
|
server/transformers/.github/stale.yml
ADDED
@@ -0,0 +1,17 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# Number of days of inactivity before an issue becomes stale
|
2 |
+
daysUntilStale: 60
|
3 |
+
# Number of days of inactivity before a stale issue is closed
|
4 |
+
daysUntilClose: 7
|
5 |
+
# Issues with these labels will never be considered stale
|
6 |
+
exemptLabels:
|
7 |
+
- pinned
|
8 |
+
- security
|
9 |
+
# Label to use when marking an issue as stale
|
10 |
+
staleLabel: wontfix
|
11 |
+
# Comment to post when marking an issue as stale. Set to `false` to disable
|
12 |
+
markComment: >
|
13 |
+
This issue has been automatically marked as stale because it has not had
|
14 |
+
recent activity. It will be closed if no further activity occurs. Thank you
|
15 |
+
for your contributions.
|
16 |
+
# Comment to post when closing a stale issue. Set to `false` to disable
|
17 |
+
closeComment: false
|
server/transformers/.gitignore
ADDED
@@ -0,0 +1,141 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# Initially taken from Github's Python gitignore file
|
2 |
+
|
3 |
+
# Byte-compiled / optimized / DLL files
|
4 |
+
__pycache__/
|
5 |
+
*.py[cod]
|
6 |
+
*$py.class
|
7 |
+
|
8 |
+
# C extensions
|
9 |
+
*.so
|
10 |
+
|
11 |
+
# Distribution / packaging
|
12 |
+
.Python
|
13 |
+
build/
|
14 |
+
develop-eggs/
|
15 |
+
dist/
|
16 |
+
downloads/
|
17 |
+
eggs/
|
18 |
+
.eggs/
|
19 |
+
lib/
|
20 |
+
lib64/
|
21 |
+
parts/
|
22 |
+
sdist/
|
23 |
+
var/
|
24 |
+
wheels/
|
25 |
+
*.egg-info/
|
26 |
+
.installed.cfg
|
27 |
+
*.egg
|
28 |
+
MANIFEST
|
29 |
+
|
30 |
+
# PyInstaller
|
31 |
+
# Usually these files are written by a python script from a template
|
32 |
+
# before PyInstaller builds the exe, so as to inject date/other infos into it.
|
33 |
+
*.manifest
|
34 |
+
*.spec
|
35 |
+
|
36 |
+
# Installer logs
|
37 |
+
pip-log.txt
|
38 |
+
pip-delete-this-directory.txt
|
39 |
+
|
40 |
+
# Unit test / coverage reports
|
41 |
+
htmlcov/
|
42 |
+
.tox/
|
43 |
+
.nox/
|
44 |
+
.coverage
|
45 |
+
.coverage.*
|
46 |
+
.cache
|
47 |
+
nosetests.xml
|
48 |
+
coverage.xml
|
49 |
+
*.cover
|
50 |
+
.hypothesis/
|
51 |
+
.pytest_cache/
|
52 |
+
|
53 |
+
# Translations
|
54 |
+
*.mo
|
55 |
+
*.pot
|
56 |
+
|
57 |
+
# Django stuff:
|
58 |
+
*.log
|
59 |
+
local_settings.py
|
60 |
+
db.sqlite3
|
61 |
+
|
62 |
+
# Flask stuff:
|
63 |
+
instance/
|
64 |
+
.webassets-cache
|
65 |
+
|
66 |
+
# Scrapy stuff:
|
67 |
+
.scrapy
|
68 |
+
|
69 |
+
# Sphinx documentation
|
70 |
+
docs/_build/
|
71 |
+
|
72 |
+
# PyBuilder
|
73 |
+
target/
|
74 |
+
|
75 |
+
# Jupyter Notebook
|
76 |
+
.ipynb_checkpoints
|
77 |
+
|
78 |
+
# IPython
|
79 |
+
profile_default/
|
80 |
+
ipython_config.py
|
81 |
+
|
82 |
+
# pyenv
|
83 |
+
.python-version
|
84 |
+
|
85 |
+
# celery beat schedule file
|
86 |
+
celerybeat-schedule
|
87 |
+
|
88 |
+
# SageMath parsed files
|
89 |
+
*.sage.py
|
90 |
+
|
91 |
+
# Environments
|
92 |
+
.env
|
93 |
+
.venv
|
94 |
+
env/
|
95 |
+
venv/
|
96 |
+
ENV/
|
97 |
+
env.bak/
|
98 |
+
venv.bak/
|
99 |
+
|
100 |
+
# Spyder project settings
|
101 |
+
.spyderproject
|
102 |
+
.spyproject
|
103 |
+
|
104 |
+
# Rope project settings
|
105 |
+
.ropeproject
|
106 |
+
|
107 |
+
# mkdocs documentation
|
108 |
+
/site
|
109 |
+
|
110 |
+
# mypy
|
111 |
+
.mypy_cache/
|
112 |
+
.dmypy.json
|
113 |
+
dmypy.json
|
114 |
+
|
115 |
+
# Pyre type checker
|
116 |
+
.pyre/
|
117 |
+
|
118 |
+
# vscode
|
119 |
+
.vscode
|
120 |
+
|
121 |
+
# Pycharm
|
122 |
+
.idea
|
123 |
+
|
124 |
+
# TF code
|
125 |
+
tensorflow_code
|
126 |
+
|
127 |
+
# Models
|
128 |
+
models
|
129 |
+
proc_data
|
130 |
+
|
131 |
+
# examples
|
132 |
+
runs
|
133 |
+
examples/runs
|
134 |
+
|
135 |
+
# data
|
136 |
+
/data
|
137 |
+
serialization_dir
|
138 |
+
|
139 |
+
# emacs
|
140 |
+
*.*~
|
141 |
+
debug.env
|
server/transformers/.gitrepo
ADDED
@@ -0,0 +1,12 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
; DO NOT EDIT (unless you know what you are doing)
|
2 |
+
;
|
3 |
+
; This subdirectory is a git "subrepo", and this file is maintained by the
|
4 |
+
; git-subrepo command. See https://github.com/git-commands/git-subrepo#readme
|
5 |
+
;
|
6 |
+
[subrepo]
|
7 |
+
remote = https://github.com/bhoov/transformers.git
|
8 |
+
branch = exbert-mods
|
9 |
+
commit = a0b899d114c1891dc685ce448077efab4a386348
|
10 |
+
parent = 8235ef04d0dca4d47c9106f70c0bd8681895fb8f
|
11 |
+
method = merge
|
12 |
+
cmdver = 0.4.1
|
server/transformers/CONTRIBUTING.md
ADDED
@@ -0,0 +1,258 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# How to contribute to transformers?
|
2 |
+
|
3 |
+
Everyone is welcome to contribute, and we value everybody's contribution. Code
|
4 |
+
is thus not the only way to help the community. Answering questions, helping
|
5 |
+
others, reaching out and improving the documentations are immensely valuable to
|
6 |
+
the community.
|
7 |
+
|
8 |
+
It also helps us if you spread the word: reference the library from blog posts
|
9 |
+
on the awesome projects it made possible, shout out on Twitter every time it has
|
10 |
+
helped you, or simply star the repo to say "thank you".
|
11 |
+
|
12 |
+
## You can contribute in so many ways!
|
13 |
+
|
14 |
+
There are 4 ways you can contribute to transformers:
|
15 |
+
* Fixing outstanding issues with the existing code;
|
16 |
+
* Implementing new models;
|
17 |
+
* Contributing to the examples or to the documentation;
|
18 |
+
* Submitting issues related to bugs or desired new features.
|
19 |
+
|
20 |
+
*All are equally valuable to the community.*
|
21 |
+
|
22 |
+
## Submitting a new issue or feature request
|
23 |
+
|
24 |
+
Do your best to follow these guidelines when submitting an issue or a feature
|
25 |
+
request. It will make it easier for us to come back to you quickly and with good
|
26 |
+
feedback.
|
27 |
+
|
28 |
+
### Did you find a bug?
|
29 |
+
|
30 |
+
The transformers are robust and reliable thanks to the users who notify us of
|
31 |
+
the problems they encounter. So thank you for reporting an issue.
|
32 |
+
|
33 |
+
First, we would really appreciate it if you could **make sure the bug was not
|
34 |
+
already reported** (use the search bar on Github under Issues).
|
35 |
+
|
36 |
+
Did not find it? :( So we can act quickly on it, please follow these steps:
|
37 |
+
|
38 |
+
* Include your **OS type and version**, the versions of **Python**, **PyTorch** and
|
39 |
+
**Tensorflow** when applicable;
|
40 |
+
* A short, self-contained, code snippet that allows us to reproduce the bug in
|
41 |
+
less than 30s;
|
42 |
+
* Provide the *full* traceback if an exception is raised.
|
43 |
+
|
44 |
+
To get the OS and software versions automatically, you can run the following command:
|
45 |
+
|
46 |
+
```bash
|
47 |
+
python transformers-cli env
|
48 |
+
```
|
49 |
+
|
50 |
+
### Do you want to implement a new model?
|
51 |
+
|
52 |
+
Awesome! Please provide the following information:
|
53 |
+
|
54 |
+
* Short description of the model and link to the paper;
|
55 |
+
* Link to the implementation if it is open-source;
|
56 |
+
* Link to the model weights if they are available.
|
57 |
+
|
58 |
+
If you are willing to contribute the model yourself, let us know so we can best
|
59 |
+
guide you.
|
60 |
+
|
61 |
+
We have added a **detailed guide and templates** to guide you in the process of adding a new model. You can find them in the [`templates`](./templates) folder.
|
62 |
+
|
63 |
+
### Do you want a new feature (that is not a model)?
|
64 |
+
|
65 |
+
A world-class feature request addresses the following points:
|
66 |
+
|
67 |
+
1. Motivation first:
|
68 |
+
* Is it related to a problem/frustration with the library? If so, please explain
|
69 |
+
why. Providing a code snippet that demonstrates the problem is best.
|
70 |
+
* Is it related to something you would need for a project? We'd love to hear
|
71 |
+
about it!
|
72 |
+
* Is it something you worked on and think could benefit the community?
|
73 |
+
Awesome! Tell us what problem it solved for you.
|
74 |
+
2. Write a *full paragraph* describing the feature;
|
75 |
+
3. Provide a **code snippet** that demonstrates its future use;
|
76 |
+
4. In case this is related to a paper, please attach a link;
|
77 |
+
5. Attach any additional information (drawings, screenshots, etc.) you think may help.
|
78 |
+
|
79 |
+
If your issue is well written we're already 80% of the way there by the time you
|
80 |
+
post it.
|
81 |
+
|
82 |
+
We have added **templates** to guide you in the process of adding a new example script for training or testing the models in the library. You can find them in the [`templates`](./templates) folder.
|
83 |
+
|
84 |
+
## Start contributing! (Pull Requests)
|
85 |
+
|
86 |
+
Before writing code, we strongly advise you to search through the exising PRs or
|
87 |
+
issues to make sure that nobody is already working on the same thing. If you are
|
88 |
+
unsure, it is always a good idea to open an issue to get some feedback.
|
89 |
+
|
90 |
+
You will need basic `git` proficiency to be able to contribute to
|
91 |
+
`transformers`. `git` is not the easiest tool to use but it has the greatest
|
92 |
+
manual. Type `git --help` in a shell and enjoy. If you prefer books, [Pro
|
93 |
+
Git](https://git-scm.com/book/en/v2) is a very good reference.
|
94 |
+
|
95 |
+
Follow these steps to start contributing:
|
96 |
+
|
97 |
+
1. Fork the [repository](https://github.com/huggingface/transformers) by
|
98 |
+
clicking on the 'Fork' button on the repository's page. This creates a copy of the code
|
99 |
+
under your GitHub user account.
|
100 |
+
|
101 |
+
2. Clone your fork to your local disk, and add the base repository as a remote:
|
102 |
+
|
103 |
+
```bash
|
104 |
+
$ git clone [email protected]:<your Github handle>/transformers.git
|
105 |
+
$ cd transformers
|
106 |
+
$ git remote add upstream https://github.com/huggingface/transformers.git
|
107 |
+
```
|
108 |
+
|
109 |
+
3. Create a new branch to hold your development changes:
|
110 |
+
|
111 |
+
```bash
|
112 |
+
$ git checkout -b a-descriptive-name-for-my-changes
|
113 |
+
```
|
114 |
+
|
115 |
+
**do not** work on the `master` branch.
|
116 |
+
|
117 |
+
4. Set up a development environment by running the following command in a virtual environment:
|
118 |
+
|
119 |
+
```bash
|
120 |
+
$ pip install -e ".[dev]"
|
121 |
+
```
|
122 |
+
|
123 |
+
(If transformers was already installed in the virtual environment, remove
|
124 |
+
it with `pip uninstall transformers` before reinstalling it in editable
|
125 |
+
mode with the `-e` flag.)
|
126 |
+
|
127 |
+
Right now, we need an unreleased version of `isort` to avoid a
|
128 |
+
[bug](https://github.com/timothycrosley/isort/pull/1000):
|
129 |
+
|
130 |
+
```bash
|
131 |
+
$ pip install -U git+git://github.com/timothycrosley/isort.git@e63ae06ec7d70b06df9e528357650281a3d3ec22#egg=isort
|
132 |
+
```
|
133 |
+
|
134 |
+
5. Develop the features on your branch.
|
135 |
+
|
136 |
+
As you work on the features, you should make sure that the test suite
|
137 |
+
passes:
|
138 |
+
|
139 |
+
```bash
|
140 |
+
$ make test
|
141 |
+
```
|
142 |
+
|
143 |
+
`transformers` relies on `black` and `isort` to format its source code
|
144 |
+
consistently. After you make changes, format them with:
|
145 |
+
|
146 |
+
```bash
|
147 |
+
$ make style
|
148 |
+
```
|
149 |
+
|
150 |
+
`transformers` also uses `flake8` to check for coding mistakes. Quality
|
151 |
+
control runs in CI, however you can also run the same checks with:
|
152 |
+
|
153 |
+
```bash
|
154 |
+
$ make quality
|
155 |
+
```
|
156 |
+
|
157 |
+
Once you're happy with your changes, add changed files using `git add` and
|
158 |
+
make a commit with `git commit` to record your changes locally:
|
159 |
+
|
160 |
+
```bash
|
161 |
+
$ git add modified_file.py
|
162 |
+
$ git commit
|
163 |
+
```
|
164 |
+
|
165 |
+
Please write [good commit
|
166 |
+
messages](https://chris.beams.io/posts/git-commit/).
|
167 |
+
|
168 |
+
It is a good idea to sync your copy of the code with the original
|
169 |
+
repository regularly. This way you can quickly account for changes:
|
170 |
+
|
171 |
+
```bash
|
172 |
+
$ git fetch upstream
|
173 |
+
$ git rebase upstream/master
|
174 |
+
```
|
175 |
+
|
176 |
+
Push the changes to your account using:
|
177 |
+
|
178 |
+
```bash
|
179 |
+
$ git push -u origin a-descriptive-name-for-my-changes
|
180 |
+
```
|
181 |
+
|
182 |
+
6. Once you are satisfied (**and the checklist below is happy too**), go to the
|
183 |
+
webpage of your fork on GitHub. Click on 'Pull request' to send your changes
|
184 |
+
to the project maintainers for review.
|
185 |
+
|
186 |
+
7. It's ok if maintainers ask you for changes. It happens to core contributors
|
187 |
+
too! So everyone can see the changes in the Pull request, work in your local
|
188 |
+
branch and push the changes to your fork. They will automatically appear in
|
189 |
+
the pull request.
|
190 |
+
|
191 |
+
|
192 |
+
### Checklist
|
193 |
+
|
194 |
+
1. The title of your pull request should be a summary of its contribution;
|
195 |
+
2. If your pull request adresses an issue, please mention the issue number in
|
196 |
+
the pull request description to make sure they are linked (and people
|
197 |
+
consulting the issue know you are working on it);
|
198 |
+
3. To indicate a work in progress please prefix the title with `[WIP]`. These
|
199 |
+
are useful to avoid duplicated work, and to differentiate it from PRs ready
|
200 |
+
to be merged;
|
201 |
+
4. Make sure pre-existing tests still pass;
|
202 |
+
5. Add high-coverage tests. No quality test, no merge;
|
203 |
+
6. All public methods must have informative docstrings;
|
204 |
+
|
205 |
+
|
206 |
+
### Tests
|
207 |
+
|
208 |
+
You can run 🤗 Transformers tests with `unittest` or `pytest`.
|
209 |
+
|
210 |
+
We like `pytest` and `pytest-xdist` because it's faster. From the root of the
|
211 |
+
repository, here's how to run tests with `pytest` for the library:
|
212 |
+
|
213 |
+
```bash
|
214 |
+
$ python -m pytest -n auto --dist=loadfile -s -v ./tests/
|
215 |
+
```
|
216 |
+
|
217 |
+
and for the examples:
|
218 |
+
|
219 |
+
```bash
|
220 |
+
$ pip install -r examples/requirements.txt # only needed the first time
|
221 |
+
$ python -m pytest -n auto --dist=loadfile -s -v ./examples/
|
222 |
+
```
|
223 |
+
|
224 |
+
In fact, that's how `make test` and `make test-examples` are implemented!
|
225 |
+
|
226 |
+
You can specify a smaller set of tests in order to test only the feature
|
227 |
+
you're working on.
|
228 |
+
|
229 |
+
By default, slow tests are skipped. Set the `RUN_SLOW` environment variable to
|
230 |
+
`yes` to run them. This will download many gigabytes of models — make sure you
|
231 |
+
have enough disk space and a good Internet connection, or a lot of patience!
|
232 |
+
|
233 |
+
```bash
|
234 |
+
$ RUN_SLOW=yes python -m pytest -n auto --dist=loadfile -s -v ./tests/
|
235 |
+
$ RUN_SLOW=yes python -m pytest -n auto --dist=loadfile -s -v ./examples/
|
236 |
+
```
|
237 |
+
|
238 |
+
Likewise, set the `RUN_CUSTOM_TOKENIZERS` environment variable to `yes` to run
|
239 |
+
tests for custom tokenizers, which don't run by default either.
|
240 |
+
|
241 |
+
🤗 Transformers uses `pytest` as a test runner only. It doesn't use any
|
242 |
+
`pytest`-specific features in the test suite itself.
|
243 |
+
|
244 |
+
This means `unittest` is fully supported. Here's how to run tests with
|
245 |
+
`unittest`:
|
246 |
+
|
247 |
+
```bash
|
248 |
+
$ python -m unittest discover -s tests -t . -v
|
249 |
+
$ python -m unittest discover -s examples -t examples -v
|
250 |
+
```
|
251 |
+
|
252 |
+
|
253 |
+
### Style guide
|
254 |
+
|
255 |
+
For documentation strings, `transformers` follows the [google
|
256 |
+
style](https://google.github.io/styleguide/pyguide.html).
|
257 |
+
|
258 |
+
#### This guide was heavily inspired by the awesome [scikit-learn guide to contributing](https://github.com/scikit-learn/scikit-learn/blob/master/CONTRIBUTING.md)
|
server/transformers/LICENSE
ADDED
@@ -0,0 +1,202 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
|
2 |
+
Apache License
|
3 |
+
Version 2.0, January 2004
|
4 |
+
http://www.apache.org/licenses/
|
5 |
+
|
6 |
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
7 |
+
|
8 |
+
1. Definitions.
|
9 |
+
|
10 |
+
"License" shall mean the terms and conditions for use, reproduction,
|
11 |
+
and distribution as defined by Sections 1 through 9 of this document.
|
12 |
+
|
13 |
+
"Licensor" shall mean the copyright owner or entity authorized by
|
14 |
+
the copyright owner that is granting the License.
|
15 |
+
|
16 |
+
"Legal Entity" shall mean the union of the acting entity and all
|
17 |
+
other entities that control, are controlled by, or are under common
|
18 |
+
control with that entity. For the purposes of this definition,
|
19 |
+
"control" means (i) the power, direct or indirect, to cause the
|
20 |
+
direction or management of such entity, whether by contract or
|
21 |
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
22 |
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
23 |
+
|
24 |
+
"You" (or "Your") shall mean an individual or Legal Entity
|
25 |
+
exercising permissions granted by this License.
|
26 |
+
|
27 |
+
"Source" form shall mean the preferred form for making modifications,
|
28 |
+
including but not limited to software source code, documentation
|
29 |
+
source, and configuration files.
|
30 |
+
|
31 |
+
"Object" form shall mean any form resulting from mechanical
|
32 |
+
transformation or translation of a Source form, including but
|
33 |
+
not limited to compiled object code, generated documentation,
|
34 |
+
and conversions to other media types.
|
35 |
+
|
36 |
+
"Work" shall mean the work of authorship, whether in Source or
|
37 |
+
Object form, made available under the License, as indicated by a
|
38 |
+
copyright notice that is included in or attached to the work
|
39 |
+
(an example is provided in the Appendix below).
|
40 |
+
|
41 |
+
"Derivative Works" shall mean any work, whether in Source or Object
|
42 |
+
form, that is based on (or derived from) the Work and for which the
|
43 |
+
editorial revisions, annotations, elaborations, or other modifications
|
44 |
+
represent, as a whole, an original work of authorship. For the purposes
|
45 |
+
of this License, Derivative Works shall not include works that remain
|
46 |
+
separable from, or merely link (or bind by name) to the interfaces of,
|
47 |
+
the Work and Derivative Works thereof.
|
48 |
+
|
49 |
+
"Contribution" shall mean any work of authorship, including
|
50 |
+
the original version of the Work and any modifications or additions
|
51 |
+
to that Work or Derivative Works thereof, that is intentionally
|
52 |
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
53 |
+
or by an individual or Legal Entity authorized to submit on behalf of
|
54 |
+
the copyright owner. For the purposes of this definition, "submitted"
|
55 |
+
means any form of electronic, verbal, or written communication sent
|
56 |
+
to the Licensor or its representatives, including but not limited to
|
57 |
+
communication on electronic mailing lists, source code control systems,
|
58 |
+
and issue tracking systems that are managed by, or on behalf of, the
|
59 |
+
Licensor for the purpose of discussing and improving the Work, but
|
60 |
+
excluding communication that is conspicuously marked or otherwise
|
61 |
+
designated in writing by the copyright owner as "Not a Contribution."
|
62 |
+
|
63 |
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
64 |
+
on behalf of whom a Contribution has been received by Licensor and
|
65 |
+
subsequently incorporated within the Work.
|
66 |
+
|
67 |
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
68 |
+
this License, each Contributor hereby grants to You a perpetual,
|
69 |
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
70 |
+
copyright license to reproduce, prepare Derivative Works of,
|
71 |
+
publicly display, publicly perform, sublicense, and distribute the
|
72 |
+
Work and such Derivative Works in Source or Object form.
|
73 |
+
|
74 |
+
3. Grant of Patent License. Subject to the terms and conditions of
|
75 |
+
this License, each Contributor hereby grants to You a perpetual,
|
76 |
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
77 |
+
(except as stated in this section) patent license to make, have made,
|
78 |
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
79 |
+
where such license applies only to those patent claims licensable
|
80 |
+
by such Contributor that are necessarily infringed by their
|
81 |
+
Contribution(s) alone or by combination of their Contribution(s)
|
82 |
+
with the Work to which such Contribution(s) was submitted. If You
|
83 |
+
institute patent litigation against any entity (including a
|
84 |
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
85 |
+
or a Contribution incorporated within the Work constitutes direct
|
86 |
+
or contributory patent infringement, then any patent licenses
|
87 |
+
granted to You under this License for that Work shall terminate
|
88 |
+
as of the date such litigation is filed.
|
89 |
+
|
90 |
+
4. Redistribution. You may reproduce and distribute copies of the
|
91 |
+
Work or Derivative Works thereof in any medium, with or without
|
92 |
+
modifications, and in Source or Object form, provided that You
|
93 |
+
meet the following conditions:
|
94 |
+
|
95 |
+
(a) You must give any other recipients of the Work or
|
96 |
+
Derivative Works a copy of this License; and
|
97 |
+
|
98 |
+
(b) You must cause any modified files to carry prominent notices
|
99 |
+
stating that You changed the files; and
|
100 |
+
|
101 |
+
(c) You must retain, in the Source form of any Derivative Works
|
102 |
+
that You distribute, all copyright, patent, trademark, and
|
103 |
+
attribution notices from the Source form of the Work,
|
104 |
+
excluding those notices that do not pertain to any part of
|
105 |
+
the Derivative Works; and
|
106 |
+
|
107 |
+
(d) If the Work includes a "NOTICE" text file as part of its
|
108 |
+
distribution, then any Derivative Works that You distribute must
|
109 |
+
include a readable copy of the attribution notices contained
|
110 |
+
within such NOTICE file, excluding those notices that do not
|
111 |
+
pertain to any part of the Derivative Works, in at least one
|
112 |
+
of the following places: within a NOTICE text file distributed
|
113 |
+
as part of the Derivative Works; within the Source form or
|
114 |
+
documentation, if provided along with the Derivative Works; or,
|
115 |
+
within a display generated by the Derivative Works, if and
|
116 |
+
wherever such third-party notices normally appear. The contents
|
117 |
+
of the NOTICE file are for informational purposes only and
|
118 |
+
do not modify the License. You may add Your own attribution
|
119 |
+
notices within Derivative Works that You distribute, alongside
|
120 |
+
or as an addendum to the NOTICE text from the Work, provided
|
121 |
+
that such additional attribution notices cannot be construed
|
122 |
+
as modifying the License.
|
123 |
+
|
124 |
+
You may add Your own copyright statement to Your modifications and
|
125 |
+
may provide additional or different license terms and conditions
|
126 |
+
for use, reproduction, or distribution of Your modifications, or
|
127 |
+
for any such Derivative Works as a whole, provided Your use,
|
128 |
+
reproduction, and distribution of the Work otherwise complies with
|
129 |
+
the conditions stated in this License.
|
130 |
+
|
131 |
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
132 |
+
any Contribution intentionally submitted for inclusion in the Work
|
133 |
+
by You to the Licensor shall be under the terms and conditions of
|
134 |
+
this License, without any additional terms or conditions.
|
135 |
+
Notwithstanding the above, nothing herein shall supersede or modify
|
136 |
+
the terms of any separate license agreement you may have executed
|
137 |
+
with Licensor regarding such Contributions.
|
138 |
+
|
139 |
+
6. Trademarks. This License does not grant permission to use the trade
|
140 |
+
names, trademarks, service marks, or product names of the Licensor,
|
141 |
+
except as required for reasonable and customary use in describing the
|
142 |
+
origin of the Work and reproducing the content of the NOTICE file.
|
143 |
+
|
144 |
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
145 |
+
agreed to in writing, Licensor provides the Work (and each
|
146 |
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
147 |
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
148 |
+
implied, including, without limitation, any warranties or conditions
|
149 |
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
150 |
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
151 |
+
appropriateness of using or redistributing the Work and assume any
|
152 |
+
risks associated with Your exercise of permissions under this License.
|
153 |
+
|
154 |
+
8. Limitation of Liability. In no event and under no legal theory,
|
155 |
+
whether in tort (including negligence), contract, or otherwise,
|
156 |
+
unless required by applicable law (such as deliberate and grossly
|
157 |
+
negligent acts) or agreed to in writing, shall any Contributor be
|
158 |
+
liable to You for damages, including any direct, indirect, special,
|
159 |
+
incidental, or consequential damages of any character arising as a
|
160 |
+
result of this License or out of the use or inability to use the
|
161 |
+
Work (including but not limited to damages for loss of goodwill,
|
162 |
+
work stoppage, computer failure or malfunction, or any and all
|
163 |
+
other commercial damages or losses), even if such Contributor
|
164 |
+
has been advised of the possibility of such damages.
|
165 |
+
|
166 |
+
9. Accepting Warranty or Additional Liability. While redistributing
|
167 |
+
the Work or Derivative Works thereof, You may choose to offer,
|
168 |
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
169 |
+
or other liability obligations and/or rights consistent with this
|
170 |
+
License. However, in accepting such obligations, You may act only
|
171 |
+
on Your own behalf and on Your sole responsibility, not on behalf
|
172 |
+
of any other Contributor, and only if You agree to indemnify,
|
173 |
+
defend, and hold each Contributor harmless for any liability
|
174 |
+
incurred by, or claims asserted against, such Contributor by reason
|
175 |
+
of your accepting any such warranty or additional liability.
|
176 |
+
|
177 |
+
END OF TERMS AND CONDITIONS
|
178 |
+
|
179 |
+
APPENDIX: How to apply the Apache License to your work.
|
180 |
+
|
181 |
+
To apply the Apache License to your work, attach the following
|
182 |
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
183 |
+
replaced with your own identifying information. (Don't include
|
184 |
+
the brackets!) The text should be enclosed in the appropriate
|
185 |
+
comment syntax for the file format. We also recommend that a
|
186 |
+
file or class name and description of purpose be included on the
|
187 |
+
same "printed page" as the copyright notice for easier
|
188 |
+
identification within third-party archives.
|
189 |
+
|
190 |
+
Copyright [yyyy] [name of copyright owner]
|
191 |
+
|
192 |
+
Licensed under the Apache License, Version 2.0 (the "License");
|
193 |
+
you may not use this file except in compliance with the License.
|
194 |
+
You may obtain a copy of the License at
|
195 |
+
|
196 |
+
http://www.apache.org/licenses/LICENSE-2.0
|
197 |
+
|
198 |
+
Unless required by applicable law or agreed to in writing, software
|
199 |
+
distributed under the License is distributed on an "AS IS" BASIS,
|
200 |
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
201 |
+
See the License for the specific language governing permissions and
|
202 |
+
limitations under the License.
|
server/transformers/MANIFEST.in
ADDED
@@ -0,0 +1 @@
|
|
|
|
|
1 |
+
include LICENSE
|
server/transformers/Makefile
ADDED
@@ -0,0 +1,24 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
.PHONY: quality style test test-examples
|
2 |
+
|
3 |
+
# Check that source code meets quality standards
|
4 |
+
|
5 |
+
quality:
|
6 |
+
black --check --line-length 119 --target-version py35 examples templates tests src utils
|
7 |
+
isort --check-only --recursive examples templates tests src utils
|
8 |
+
flake8 examples templates tests src utils
|
9 |
+
|
10 |
+
# Format source code automatically
|
11 |
+
|
12 |
+
style:
|
13 |
+
black --line-length 119 --target-version py35 examples templates tests src utils
|
14 |
+
isort --recursive examples templates tests src utils
|
15 |
+
|
16 |
+
# Run tests for the library
|
17 |
+
|
18 |
+
test:
|
19 |
+
python -m pytest -n auto --dist=loadfile -s -v ./tests/
|
20 |
+
|
21 |
+
# Run tests for examples
|
22 |
+
|
23 |
+
test-examples:
|
24 |
+
python -m pytest -n auto --dist=loadfile -s -v ./examples/
|
server/transformers/README.md
ADDED
@@ -0,0 +1,684 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
<p align="center">
|
2 |
+
<br>
|
3 |
+
<img src="https://raw.githubusercontent.com/huggingface/transformers/master/docs/source/imgs/transformers_logo_name.png" width="400"/>
|
4 |
+
<br>
|
5 |
+
<p>
|
6 |
+
<p align="center">
|
7 |
+
<a href="https://circleci.com/gh/huggingface/transformers">
|
8 |
+
<img alt="Build" src="https://img.shields.io/circleci/build/github/huggingface/transformers/master">
|
9 |
+
</a>
|
10 |
+
<a href="https://github.com/huggingface/transformers/blob/master/LICENSE">
|
11 |
+
<img alt="GitHub" src="https://img.shields.io/github/license/huggingface/transformers.svg?color=blue">
|
12 |
+
</a>
|
13 |
+
<a href="https://huggingface.co/transformers/index.html">
|
14 |
+
<img alt="Documentation" src="https://img.shields.io/website/http/huggingface.co/transformers/index.html.svg?down_color=red&down_message=offline&up_message=online">
|
15 |
+
</a>
|
16 |
+
<a href="https://github.com/huggingface/transformers/releases">
|
17 |
+
<img alt="GitHub release" src="https://img.shields.io/github/release/huggingface/transformers.svg">
|
18 |
+
</a>
|
19 |
+
</p>
|
20 |
+
|
21 |
+
<h3 align="center">
|
22 |
+
<p>State-of-the-art Natural Language Processing for TensorFlow 2.0 and PyTorch
|
23 |
+
</h3>
|
24 |
+
|
25 |
+
🤗 Transformers (formerly known as `pytorch-transformers` and `pytorch-pretrained-bert`) provides state-of-the-art general-purpose architectures (BERT, GPT-2, RoBERTa, XLM, DistilBert, XLNet, CTRL...) for Natural Language Understanding (NLU) and Natural Language Generation (NLG) with over 32+ pretrained models in 100+ languages and deep interoperability between TensorFlow 2.0 and PyTorch.
|
26 |
+
|
27 |
+
### Features
|
28 |
+
|
29 |
+
- As easy to use as pytorch-transformers
|
30 |
+
- As powerful and concise as Keras
|
31 |
+
- High performance on NLU and NLG tasks
|
32 |
+
- Low barrier to entry for educators and practitioners
|
33 |
+
|
34 |
+
State-of-the-art NLP for everyone
|
35 |
+
- Deep learning researchers
|
36 |
+
- Hands-on practitioners
|
37 |
+
- AI/ML/NLP teachers and educators
|
38 |
+
|
39 |
+
Lower compute costs, smaller carbon footprint
|
40 |
+
- Researchers can share trained models instead of always retraining
|
41 |
+
- Practitioners can reduce compute time and production costs
|
42 |
+
- 10 architectures with over 30 pretrained models, some in more than 100 languages
|
43 |
+
|
44 |
+
Choose the right framework for every part of a model's lifetime
|
45 |
+
- Train state-of-the-art models in 3 lines of code
|
46 |
+
- Deep interoperability between TensorFlow 2.0 and PyTorch models
|
47 |
+
- Move a single model between TF2.0/PyTorch frameworks at will
|
48 |
+
- Seamlessly pick the right framework for training, evaluation, production
|
49 |
+
|
50 |
+
|
51 |
+
| Section | Description |
|
52 |
+
|-|-|
|
53 |
+
| [Installation](#installation) | How to install the package |
|
54 |
+
| [Model architectures](#model-architectures) | Architectures (with pretrained weights) |
|
55 |
+
| [Online demo](#online-demo) | Experimenting with this repo’s text generation capabilities |
|
56 |
+
| [Quick tour: Usage](#quick-tour) | Tokenizers & models usage: Bert and GPT-2 |
|
57 |
+
| [Quick tour: TF 2.0 and PyTorch ](#Quick-tour-TF-20-training-and-PyTorch-interoperability) | Train a TF 2.0 model in 10 lines of code, load it in PyTorch |
|
58 |
+
| [Quick tour: pipelines](#quick-tour-of-pipelines) | Using Pipelines: Wrapper around tokenizer and models to use finetuned models |
|
59 |
+
| [Quick tour: Fine-tuning/usage scripts](#quick-tour-of-the-fine-tuningusage-scripts) | Using provided scripts: GLUE, SQuAD and Text generation |
|
60 |
+
| [Quick tour: Share your models ](#Quick-tour-of-model-sharing) | Upload and share your fine-tuned models with the community |
|
61 |
+
| [Migrating from pytorch-transformers to transformers](#Migrating-from-pytorch-transformers-to-transformers) | Migrating your code from pytorch-transformers to transformers |
|
62 |
+
| [Migrating from pytorch-pretrained-bert to pytorch-transformers](#Migrating-from-pytorch-pretrained-bert-to-transformers) | Migrating your code from pytorch-pretrained-bert to transformers |
|
63 |
+
| [Documentation][(v2.4.0)](https://huggingface.co/transformers/v2.4.0)[(v2.3.0)](https://huggingface.co/transformers/v2.3.0)[(v2.2.0/v2.2.1/v2.2.2)](https://huggingface.co/transformers/v2.2.0) [(v2.1.1)](https://huggingface.co/transformers/v2.1.1) [(v2.0.0)](https://huggingface.co/transformers/v2.0.0) [(v1.2.0)](https://huggingface.co/transformers/v1.2.0) [(v1.1.0)](https://huggingface.co/transformers/v1.1.0) [(v1.0.0)](https://huggingface.co/transformers/v1.0.0) [(master)](https://huggingface.co/transformers) | Full API documentation and more |
|
64 |
+
|
65 |
+
## Installation
|
66 |
+
|
67 |
+
This repo is tested on Python 3.5+, PyTorch 1.0.0+ and TensorFlow 2.0.0-rc1
|
68 |
+
|
69 |
+
You should install 🤗 Transformers in a [virtual environment](https://docs.python.org/3/library/venv.html). If you're unfamiliar with Python virtual environments, check out the [user guide](https://packaging.python.org/guides/installing-using-pip-and-virtual-environments/).
|
70 |
+
|
71 |
+
Create a virtual environment with the version of Python you're going to use and activate it.
|
72 |
+
|
73 |
+
Now, if you want to use 🤗 Transformers, you can install it with pip. If you'd like to play with the examples, you must install it from source.
|
74 |
+
|
75 |
+
### With pip
|
76 |
+
|
77 |
+
First you need to install one of, or both, TensorFlow 2.0 and PyTorch.
|
78 |
+
Please refer to [TensorFlow installation page](https://www.tensorflow.org/install/pip#tensorflow-2.0-rc-is-available) and/or [PyTorch installation page](https://pytorch.org/get-started/locally/#start-locally) regarding the specific install command for your platform.
|
79 |
+
|
80 |
+
When TensorFlow 2.0 and/or PyTorch has been installed, 🤗 Transformers can be installed using pip as follows:
|
81 |
+
|
82 |
+
```bash
|
83 |
+
pip install transformers
|
84 |
+
```
|
85 |
+
|
86 |
+
### From source
|
87 |
+
|
88 |
+
Here also, you first need to install one of, or both, TensorFlow 2.0 and PyTorch.
|
89 |
+
Please refer to [TensorFlow installation page](https://www.tensorflow.org/install/pip#tensorflow-2.0-rc-is-available) and/or [PyTorch installation page](https://pytorch.org/get-started/locally/#start-locally) regarding the specific install command for your platform.
|
90 |
+
|
91 |
+
When TensorFlow 2.0 and/or PyTorch has been installed, you can install from source by cloning the repository and running:
|
92 |
+
|
93 |
+
```bash
|
94 |
+
git clone https://github.com/huggingface/transformers
|
95 |
+
cd transformers
|
96 |
+
pip install .
|
97 |
+
```
|
98 |
+
|
99 |
+
When you update the repository, you should upgrade the transformers installation and its dependencies as follows:
|
100 |
+
|
101 |
+
```bash
|
102 |
+
git pull
|
103 |
+
pip install --upgrade .
|
104 |
+
```
|
105 |
+
|
106 |
+
### Run the examples
|
107 |
+
|
108 |
+
Examples are included in the repository but are not shipped with the library.
|
109 |
+
|
110 |
+
Therefore, in order to run the latest versions of the examples, you need to install from source, as described above.
|
111 |
+
|
112 |
+
Look at the [README](https://github.com/huggingface/transformers/blob/master/examples/README.md) for how to run examples.
|
113 |
+
|
114 |
+
### Tests
|
115 |
+
|
116 |
+
A series of tests are included for the library and for some example scripts. Library tests can be found in the [tests folder](https://github.com/huggingface/transformers/tree/master/tests) and examples tests in the [examples folder](https://github.com/huggingface/transformers/tree/master/examples).
|
117 |
+
|
118 |
+
Depending on which framework is installed (TensorFlow 2.0 and/or PyTorch), the irrelevant tests will be skipped. Ensure that both frameworks are installed if you want to execute all tests.
|
119 |
+
|
120 |
+
Here's the easiest way to run tests for the library:
|
121 |
+
|
122 |
+
```bash
|
123 |
+
pip install -e ".[testing]"
|
124 |
+
make test
|
125 |
+
```
|
126 |
+
|
127 |
+
and for the examples:
|
128 |
+
|
129 |
+
```bash
|
130 |
+
pip install -e ".[testing]"
|
131 |
+
pip install -r examples/requirements.txt
|
132 |
+
make test-examples
|
133 |
+
```
|
134 |
+
|
135 |
+
For details, refer to the [contributing guide](https://github.com/huggingface/transformers/blob/master/CONTRIBUTING.md#tests).
|
136 |
+
|
137 |
+
### Do you want to run a Transformer model on a mobile device?
|
138 |
+
|
139 |
+
You should check out our [`swift-coreml-transformers`](https://github.com/huggingface/swift-coreml-transformers) repo.
|
140 |
+
|
141 |
+
It contains a set of tools to convert PyTorch or TensorFlow 2.0 trained Transformer models (currently contains `GPT-2`, `DistilGPT-2`, `BERT`, and `DistilBERT`) to CoreML models that run on iOS devices.
|
142 |
+
|
143 |
+
At some point in the future, you'll be able to seamlessly move from pre-training or fine-tuning models to productizing them in CoreML, or prototype a model or an app in CoreML then research its hyperparameters or architecture from TensorFlow 2.0 and/or PyTorch. Super exciting!
|
144 |
+
|
145 |
+
## Model architectures
|
146 |
+
|
147 |
+
🤗 Transformers currently provides the following NLU/NLG architectures:
|
148 |
+
|
149 |
+
1. **[BERT](https://github.com/google-research/bert)** (from Google) released with the paper [BERT: Pre-training of Deep Bidirectional Transformers for Language Understanding](https://arxiv.org/abs/1810.04805) by Jacob Devlin, Ming-Wei Chang, Kenton Lee and Kristina Toutanova.
|
150 |
+
2. **[GPT](https://github.com/openai/finetune-transformer-lm)** (from OpenAI) released with the paper [Improving Language Understanding by Generative Pre-Training](https://blog.openai.com/language-unsupervised/) by Alec Radford, Karthik Narasimhan, Tim Salimans and Ilya Sutskever.
|
151 |
+
3. **[GPT-2](https://blog.openai.com/better-language-models/)** (from OpenAI) released with the paper [Language Models are Unsupervised Multitask Learners](https://blog.openai.com/better-language-models/) by Alec Radford*, Jeffrey Wu*, Rewon Child, David Luan, Dario Amodei** and Ilya Sutskever**.
|
152 |
+
4. **[Transformer-XL](https://github.com/kimiyoung/transformer-xl)** (from Google/CMU) released with the paper [Transformer-XL: Attentive Language Models Beyond a Fixed-Length Context](https://arxiv.org/abs/1901.02860) by Zihang Dai*, Zhilin Yang*, Yiming Yang, Jaime Carbonell, Quoc V. Le, Ruslan Salakhutdinov.
|
153 |
+
5. **[XLNet](https://github.com/zihangdai/xlnet/)** (from Google/CMU) released with the paper [XLNet: Generalized Autoregressive Pretraining for Language Understanding](https://arxiv.org/abs/1906.08237) by Zhilin Yang*, Zihang Dai*, Yiming Yang, Jaime Carbonell, Ruslan Salakhutdinov, Quoc V. Le.
|
154 |
+
6. **[XLM](https://github.com/facebookresearch/XLM/)** (from Facebook) released together with the paper [Cross-lingual Language Model Pretraining](https://arxiv.org/abs/1901.07291) by Guillaume Lample and Alexis Conneau.
|
155 |
+
7. **[RoBERTa](https://github.com/pytorch/fairseq/tree/master/examples/roberta)** (from Facebook), released together with the paper a [Robustly Optimized BERT Pretraining Approach](https://arxiv.org/abs/1907.11692) by Yinhan Liu, Myle Ott, Naman Goyal, Jingfei Du, Mandar Joshi, Danqi Chen, Omer Levy, Mike Lewis, Luke Zettlemoyer, Veselin Stoyanov.
|
156 |
+
8. **[DistilBERT](https://github.com/huggingface/transformers/tree/master/examples/distillation)** (from HuggingFace), released together with the paper [DistilBERT, a distilled version of BERT: smaller, faster, cheaper and lighter](https://arxiv.org/abs/1910.01108) by Victor Sanh, Lysandre Debut and Thomas Wolf. The same method has been applied to compress GPT2 into [DistilGPT2](https://github.com/huggingface/transformers/tree/master/examples/distillation), RoBERTa into [DistilRoBERTa](https://github.com/huggingface/transformers/tree/master/examples/distillation), Multilingual BERT into [DistilmBERT](https://github.com/huggingface/transformers/tree/master/examples/distillation) and a German version of DistilBERT.
|
157 |
+
9. **[CTRL](https://github.com/salesforce/ctrl/)** (from Salesforce) released with the paper [CTRL: A Conditional Transformer Language Model for Controllable Generation](https://arxiv.org/abs/1909.05858) by Nitish Shirish Keskar*, Bryan McCann*, Lav R. Varshney, Caiming Xiong and Richard Socher.
|
158 |
+
10. **[CamemBERT](https://camembert-model.fr)** (from Inria/Facebook/Sorbonne) released with the paper [CamemBERT: a Tasty French Language Model](https://arxiv.org/abs/1911.03894) by Louis Martin*, Benjamin Muller*, Pedro Javier Ortiz Suárez*, Yoann Dupont, Laurent Romary, Éric Villemonte de la Clergerie, Djamé Seddah and Benoît Sagot.
|
159 |
+
11. **[ALBERT](https://github.com/google-research/ALBERT)** (from Google Research and the Toyota Technological Institute at Chicago) released with the paper [ALBERT: A Lite BERT for Self-supervised Learning of Language Representations](https://arxiv.org/abs/1909.11942), by Zhenzhong Lan, Mingda Chen, Sebastian Goodman, Kevin Gimpel, Piyush Sharma, Radu Soricut.
|
160 |
+
12. **[T5](https://github.com/google-research/text-to-text-transfer-transformer)** (from Google AI) released with the paper [Exploring the Limits of Transfer Learning with a Unified Text-to-Text Transformer](https://arxiv.org/abs/1910.10683) by Colin Raffel and Noam Shazeer and Adam Roberts and Katherine Lee and Sharan Narang and Michael Matena and Yanqi Zhou and Wei Li and Peter J. Liu.
|
161 |
+
13. **[XLM-RoBERTa](https://github.com/pytorch/fairseq/tree/master/examples/xlmr)** (from Facebook AI), released together with the paper [Unsupervised Cross-lingual Representation Learning at Scale](https://arxiv.org/abs/1911.02116) by Alexis Conneau*, Kartikay Khandelwal*, Naman Goyal, Vishrav Chaudhary, Guillaume Wenzek, Francisco Guzmán, Edouard Grave, Myle Ott, Luke Zettlemoyer and Veselin Stoyanov.
|
162 |
+
14. **[MMBT](https://github.com/facebookresearch/mmbt/)** (from Facebook), released together with the paper a [Supervised Multimodal Bitransformers for Classifying Images and Text](https://arxiv.org/pdf/1909.02950.pdf) by Douwe Kiela, Suvrat Bhooshan, Hamed Firooz, Davide Testuggine.
|
163 |
+
15. **[FlauBERT](https://github.com/getalp/Flaubert)** (from CNRS) released with the paper [FlauBERT: Unsupervised Language Model Pre-training for French](https://arxiv.org/abs/1912.05372) by Hang Le, Loïc Vial, Jibril Frej, Vincent Segonne, Maximin Coavoux, Benjamin Lecouteux, Alexandre Allauzen, Benoît Crabbé, Laurent Besacier, Didier Schwab.
|
164 |
+
16. **[Other community models](https://huggingface.co/models)**, contributed by the [community](https://huggingface.co/users).
|
165 |
+
17. Want to contribute a new model? We have added a **detailed guide and templates** to guide you in the process of adding a new model. You can find them in the [`templates`](./templates) folder of the repository. Be sure to check the [contributing guidelines](./CONTRIBUTING.md) and contact the maintainers or open an issue to collect feedbacks before starting your PR.
|
166 |
+
|
167 |
+
These implementations have been tested on several datasets (see the example scripts) and should match the performances of the original implementations (e.g. ~93 F1 on SQuAD for BERT Whole-Word-Masking, ~88 F1 on RocStories for OpenAI GPT, ~18.3 perplexity on WikiText 103 for Transformer-XL, ~0.916 Peason R coefficient on STS-B for XLNet). You can find more details on the performances in the Examples section of the [documentation](https://huggingface.co/transformers/examples.html).
|
168 |
+
|
169 |
+
## Online demo
|
170 |
+
|
171 |
+
**[Write With Transformer](https://transformer.huggingface.co)**, built by the Hugging Face team at transformer.huggingface.co, is the official demo of this repo’s text generation capabilities.
|
172 |
+
You can use it to experiment with completions generated by `GPT2Model`, `TransfoXLModel`, and `XLNetModel`.
|
173 |
+
|
174 |
+
> “🦄 Write with transformer is to writing what calculators are to calculus.”
|
175 |
+
|
176 |
+
![write_with_transformer](https://transformer.huggingface.co/front/assets/thumbnail-large.png)
|
177 |
+
|
178 |
+
## Quick tour
|
179 |
+
|
180 |
+
Let's do a very quick overview of the model architectures in 🤗 Transformers. Detailed examples for each model architecture (Bert, GPT, GPT-2, Transformer-XL, XLNet and XLM) can be found in the [full documentation](https://huggingface.co/transformers/).
|
181 |
+
|
182 |
+
```python
|
183 |
+
import torch
|
184 |
+
from transformers import *
|
185 |
+
|
186 |
+
# Transformers has a unified API
|
187 |
+
# for 10 transformer architectures and 30 pretrained weights.
|
188 |
+
# Model | Tokenizer | Pretrained weights shortcut
|
189 |
+
MODELS = [(BertModel, BertTokenizer, 'bert-base-uncased'),
|
190 |
+
(OpenAIGPTModel, OpenAIGPTTokenizer, 'openai-gpt'),
|
191 |
+
(GPT2Model, GPT2Tokenizer, 'gpt2'),
|
192 |
+
(CTRLModel, CTRLTokenizer, 'ctrl'),
|
193 |
+
(TransfoXLModel, TransfoXLTokenizer, 'transfo-xl-wt103'),
|
194 |
+
(XLNetModel, XLNetTokenizer, 'xlnet-base-cased'),
|
195 |
+
(XLMModel, XLMTokenizer, 'xlm-mlm-enfr-1024'),
|
196 |
+
(DistilBertModel, DistilBertTokenizer, 'distilbert-base-uncased'),
|
197 |
+
(RobertaModel, RobertaTokenizer, 'roberta-base'),
|
198 |
+
(XLMRobertaModel, XLMRobertaTokenizer, 'xlm-roberta-base'),
|
199 |
+
]
|
200 |
+
|
201 |
+
# To use TensorFlow 2.0 versions of the models, simply prefix the class names with 'TF', e.g. `TFRobertaModel` is the TF 2.0 counterpart of the PyTorch model `RobertaModel`
|
202 |
+
|
203 |
+
# Let's encode some text in a sequence of hidden-states using each model:
|
204 |
+
for model_class, tokenizer_class, pretrained_weights in MODELS:
|
205 |
+
# Load pretrained model/tokenizer
|
206 |
+
tokenizer = tokenizer_class.from_pretrained(pretrained_weights)
|
207 |
+
model = model_class.from_pretrained(pretrained_weights)
|
208 |
+
|
209 |
+
# Encode text
|
210 |
+
input_ids = torch.tensor([tokenizer.encode("Here is some text to encode", add_special_tokens=True)]) # Add special tokens takes care of adding [CLS], [SEP], <s>... tokens in the right way for each model.
|
211 |
+
with torch.no_grad():
|
212 |
+
last_hidden_states = model(input_ids)[0] # Models outputs are now tuples
|
213 |
+
|
214 |
+
# Each architecture is provided with several class for fine-tuning on down-stream tasks, e.g.
|
215 |
+
BERT_MODEL_CLASSES = [BertModel, BertForPreTraining, BertForMaskedLM, BertForNextSentencePrediction,
|
216 |
+
BertForSequenceClassification, BertForTokenClassification, BertForQuestionAnswering]
|
217 |
+
|
218 |
+
# All the classes for an architecture can be initiated from pretrained weights for this architecture
|
219 |
+
# Note that additional weights added for fine-tuning are only initialized
|
220 |
+
# and need to be trained on the down-stream task
|
221 |
+
pretrained_weights = 'bert-base-uncased'
|
222 |
+
tokenizer = BertTokenizer.from_pretrained(pretrained_weights)
|
223 |
+
for model_class in BERT_MODEL_CLASSES:
|
224 |
+
# Load pretrained model/tokenizer
|
225 |
+
model = model_class.from_pretrained(pretrained_weights)
|
226 |
+
|
227 |
+
# Models can return full list of hidden-states & attentions weights at each layer
|
228 |
+
model = model_class.from_pretrained(pretrained_weights,
|
229 |
+
output_hidden_states=True,
|
230 |
+
output_attentions=True)
|
231 |
+
input_ids = torch.tensor([tokenizer.encode("Let's see all hidden-states and attentions on this text")])
|
232 |
+
all_hidden_states, all_attentions = model(input_ids)[-2:]
|
233 |
+
|
234 |
+
# Models are compatible with Torchscript
|
235 |
+
model = model_class.from_pretrained(pretrained_weights, torchscript=True)
|
236 |
+
traced_model = torch.jit.trace(model, (input_ids,))
|
237 |
+
|
238 |
+
# Simple serialization for models and tokenizers
|
239 |
+
model.save_pretrained('./directory/to/save/') # save
|
240 |
+
model = model_class.from_pretrained('./directory/to/save/') # re-load
|
241 |
+
tokenizer.save_pretrained('./directory/to/save/') # save
|
242 |
+
tokenizer = BertTokenizer.from_pretrained('./directory/to/save/') # re-load
|
243 |
+
|
244 |
+
# SOTA examples for GLUE, SQUAD, text generation...
|
245 |
+
```
|
246 |
+
|
247 |
+
## Quick tour TF 2.0 training and PyTorch interoperability
|
248 |
+
|
249 |
+
Let's do a quick example of how a TensorFlow 2.0 model can be trained in 12 lines of code with 🤗 Transformers and then loaded in PyTorch for fast inspection/tests.
|
250 |
+
|
251 |
+
```python
|
252 |
+
import tensorflow as tf
|
253 |
+
import tensorflow_datasets
|
254 |
+
from transformers import *
|
255 |
+
|
256 |
+
# Load dataset, tokenizer, model from pretrained model/vocabulary
|
257 |
+
tokenizer = BertTokenizer.from_pretrained('bert-base-cased')
|
258 |
+
model = TFBertForSequenceClassification.from_pretrained('bert-base-cased')
|
259 |
+
data = tensorflow_datasets.load('glue/mrpc')
|
260 |
+
|
261 |
+
# Prepare dataset for GLUE as a tf.data.Dataset instance
|
262 |
+
train_dataset = glue_convert_examples_to_features(data['train'], tokenizer, max_length=128, task='mrpc')
|
263 |
+
valid_dataset = glue_convert_examples_to_features(data['validation'], tokenizer, max_length=128, task='mrpc')
|
264 |
+
train_dataset = train_dataset.shuffle(100).batch(32).repeat(2)
|
265 |
+
valid_dataset = valid_dataset.batch(64)
|
266 |
+
|
267 |
+
# Prepare training: Compile tf.keras model with optimizer, loss and learning rate schedule
|
268 |
+
optimizer = tf.keras.optimizers.Adam(learning_rate=3e-5, epsilon=1e-08, clipnorm=1.0)
|
269 |
+
loss = tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True)
|
270 |
+
metric = tf.keras.metrics.SparseCategoricalAccuracy('accuracy')
|
271 |
+
model.compile(optimizer=optimizer, loss=loss, metrics=[metric])
|
272 |
+
|
273 |
+
# Train and evaluate using tf.keras.Model.fit()
|
274 |
+
history = model.fit(train_dataset, epochs=2, steps_per_epoch=115,
|
275 |
+
validation_data=valid_dataset, validation_steps=7)
|
276 |
+
|
277 |
+
# Load the TensorFlow model in PyTorch for inspection
|
278 |
+
model.save_pretrained('./save/')
|
279 |
+
pytorch_model = BertForSequenceClassification.from_pretrained('./save/', from_tf=True)
|
280 |
+
|
281 |
+
# Quickly test a few predictions - MRPC is a paraphrasing task, let's see if our model learned the task
|
282 |
+
sentence_0 = "This research was consistent with his findings."
|
283 |
+
sentence_1 = "His findings were compatible with this research."
|
284 |
+
sentence_2 = "His findings were not compatible with this research."
|
285 |
+
inputs_1 = tokenizer.encode_plus(sentence_0, sentence_1, add_special_tokens=True, return_tensors='pt')
|
286 |
+
inputs_2 = tokenizer.encode_plus(sentence_0, sentence_2, add_special_tokens=True, return_tensors='pt')
|
287 |
+
|
288 |
+
pred_1 = pytorch_model(inputs_1['input_ids'], token_type_ids=inputs_1['token_type_ids'])[0].argmax().item()
|
289 |
+
pred_2 = pytorch_model(inputs_2['input_ids'], token_type_ids=inputs_2['token_type_ids'])[0].argmax().item()
|
290 |
+
|
291 |
+
print("sentence_1 is", "a paraphrase" if pred_1 else "not a paraphrase", "of sentence_0")
|
292 |
+
print("sentence_2 is", "a paraphrase" if pred_2 else "not a paraphrase", "of sentence_0")
|
293 |
+
```
|
294 |
+
|
295 |
+
## Quick tour of the fine-tuning/usage scripts
|
296 |
+
|
297 |
+
**Important**
|
298 |
+
Before running the fine-tuning scripts, please read the
|
299 |
+
[instructions](#run-the-examples) on how to
|
300 |
+
setup your environment to run the examples.
|
301 |
+
|
302 |
+
The library comprises several example scripts with SOTA performances for NLU and NLG tasks:
|
303 |
+
|
304 |
+
- `run_glue.py`: an example fine-tuning Bert, XLNet and XLM on nine different GLUE tasks (*sequence-level classification*)
|
305 |
+
- `run_squad.py`: an example fine-tuning Bert, XLNet and XLM on the question answering dataset SQuAD 2.0 (*token-level classification*)
|
306 |
+
- `run_generation.py`: an example using GPT, GPT-2, CTRL, Transformer-XL and XLNet for conditional language generation
|
307 |
+
- other model-specific examples (see the documentation).
|
308 |
+
|
309 |
+
Here are three quick usage examples for these scripts:
|
310 |
+
|
311 |
+
### `run_glue.py`: Fine-tuning on GLUE tasks for sequence classification
|
312 |
+
|
313 |
+
The [General Language Understanding Evaluation (GLUE) benchmark](https://gluebenchmark.com/) is a collection of nine sentence- or sentence-pair language understanding tasks for evaluating and analyzing natural language understanding systems.
|
314 |
+
|
315 |
+
Before running anyone of these GLUE tasks you should download the
|
316 |
+
[GLUE data](https://gluebenchmark.com/tasks) by running
|
317 |
+
[this script](https://gist.github.com/W4ngatang/60c2bdb54d156a41194446737ce03e2e)
|
318 |
+
and unpack it to some directory `$GLUE_DIR`.
|
319 |
+
|
320 |
+
You should also install the additional packages required by the examples:
|
321 |
+
|
322 |
+
```shell
|
323 |
+
pip install -r ./examples/requirements.txt
|
324 |
+
```
|
325 |
+
|
326 |
+
```shell
|
327 |
+
export GLUE_DIR=/path/to/glue
|
328 |
+
export TASK_NAME=MRPC
|
329 |
+
|
330 |
+
python ./examples/run_glue.py \
|
331 |
+
--model_type bert \
|
332 |
+
--model_name_or_path bert-base-uncased \
|
333 |
+
--task_name $TASK_NAME \
|
334 |
+
--do_train \
|
335 |
+
--do_eval \
|
336 |
+
--do_lower_case \
|
337 |
+
--data_dir $GLUE_DIR/$TASK_NAME \
|
338 |
+
--max_seq_length 128 \
|
339 |
+
--per_gpu_eval_batch_size=8 \
|
340 |
+
--per_gpu_train_batch_size=8 \
|
341 |
+
--learning_rate 2e-5 \
|
342 |
+
--num_train_epochs 3.0 \
|
343 |
+
--output_dir /tmp/$TASK_NAME/
|
344 |
+
```
|
345 |
+
|
346 |
+
where task name can be one of CoLA, SST-2, MRPC, STS-B, QQP, MNLI, QNLI, RTE, WNLI.
|
347 |
+
|
348 |
+
The dev set results will be present within the text file 'eval_results.txt' in the specified output_dir. In case of MNLI, since there are two separate dev sets, matched and mismatched, there will be a separate output folder called '/tmp/MNLI-MM/' in addition to '/tmp/MNLI/'.
|
349 |
+
|
350 |
+
#### Fine-tuning XLNet model on the STS-B regression task
|
351 |
+
|
352 |
+
This example code fine-tunes XLNet on the STS-B corpus using parallel training on a server with 4 V100 GPUs.
|
353 |
+
Parallel training is a simple way to use several GPUs (but is slower and less flexible than distributed training, see below).
|
354 |
+
|
355 |
+
```shell
|
356 |
+
export GLUE_DIR=/path/to/glue
|
357 |
+
|
358 |
+
python ./examples/run_glue.py \
|
359 |
+
--model_type xlnet \
|
360 |
+
--model_name_or_path xlnet-large-cased \
|
361 |
+
--do_train \
|
362 |
+
--do_eval \
|
363 |
+
--task_name=sts-b \
|
364 |
+
--data_dir=${GLUE_DIR}/STS-B \
|
365 |
+
--output_dir=./proc_data/sts-b-110 \
|
366 |
+
--max_seq_length=128 \
|
367 |
+
--per_gpu_eval_batch_size=8 \
|
368 |
+
--per_gpu_train_batch_size=8 \
|
369 |
+
--gradient_accumulation_steps=1 \
|
370 |
+
--max_steps=1200 \
|
371 |
+
--model_name=xlnet-large-cased \
|
372 |
+
--overwrite_output_dir \
|
373 |
+
--overwrite_cache \
|
374 |
+
--warmup_steps=120
|
375 |
+
```
|
376 |
+
|
377 |
+
On this machine we thus have a batch size of 32, please increase `gradient_accumulation_steps` to reach the same batch size if you have a smaller machine. These hyper-parameters should result in a Pearson correlation coefficient of `+0.917` on the development set.
|
378 |
+
|
379 |
+
#### Fine-tuning Bert model on the MRPC classification task
|
380 |
+
|
381 |
+
This example code fine-tunes the Bert Whole Word Masking model on the Microsoft Research Paraphrase Corpus (MRPC) corpus using distributed training on 8 V100 GPUs to reach a F1 > 92.
|
382 |
+
|
383 |
+
```bash
|
384 |
+
python -m torch.distributed.launch --nproc_per_node 8 ./examples/run_glue.py \
|
385 |
+
--model_type bert \
|
386 |
+
--model_name_or_path bert-large-uncased-whole-word-masking \
|
387 |
+
--task_name MRPC \
|
388 |
+
--do_train \
|
389 |
+
--do_eval \
|
390 |
+
--do_lower_case \
|
391 |
+
--data_dir $GLUE_DIR/MRPC/ \
|
392 |
+
--max_seq_length 128 \
|
393 |
+
--per_gpu_eval_batch_size=8 \
|
394 |
+
--per_gpu_train_batch_size=8 \
|
395 |
+
--learning_rate 2e-5 \
|
396 |
+
--num_train_epochs 3.0 \
|
397 |
+
--output_dir /tmp/mrpc_output/ \
|
398 |
+
--overwrite_output_dir \
|
399 |
+
--overwrite_cache \
|
400 |
+
```
|
401 |
+
|
402 |
+
Training with these hyper-parameters gave us the following results:
|
403 |
+
|
404 |
+
```bash
|
405 |
+
acc = 0.8823529411764706
|
406 |
+
acc_and_f1 = 0.901702786377709
|
407 |
+
eval_loss = 0.3418912578906332
|
408 |
+
f1 = 0.9210526315789473
|
409 |
+
global_step = 174
|
410 |
+
loss = 0.07231863956341798
|
411 |
+
```
|
412 |
+
|
413 |
+
### `run_squad.py`: Fine-tuning on SQuAD for question-answering
|
414 |
+
|
415 |
+
This example code fine-tunes BERT on the SQuAD dataset using distributed training on 8 V100 GPUs and Bert Whole Word Masking uncased model to reach a F1 > 93 on SQuAD:
|
416 |
+
|
417 |
+
```bash
|
418 |
+
python -m torch.distributed.launch --nproc_per_node=8 ./examples/run_squad.py \
|
419 |
+
--model_type bert \
|
420 |
+
--model_name_or_path bert-large-uncased-whole-word-masking \
|
421 |
+
--do_train \
|
422 |
+
--do_eval \
|
423 |
+
--do_lower_case \
|
424 |
+
--train_file $SQUAD_DIR/train-v1.1.json \
|
425 |
+
--predict_file $SQUAD_DIR/dev-v1.1.json \
|
426 |
+
--learning_rate 3e-5 \
|
427 |
+
--num_train_epochs 2 \
|
428 |
+
--max_seq_length 384 \
|
429 |
+
--doc_stride 128 \
|
430 |
+
--output_dir ../models/wwm_uncased_finetuned_squad/ \
|
431 |
+
--per_gpu_eval_batch_size=3 \
|
432 |
+
--per_gpu_train_batch_size=3 \
|
433 |
+
```
|
434 |
+
|
435 |
+
Training with these hyper-parameters gave us the following results:
|
436 |
+
|
437 |
+
```bash
|
438 |
+
python $SQUAD_DIR/evaluate-v1.1.py $SQUAD_DIR/dev-v1.1.json ../models/wwm_uncased_finetuned_squad/predictions.json
|
439 |
+
{"exact_match": 86.91579943235573, "f1": 93.1532499015869}
|
440 |
+
```
|
441 |
+
|
442 |
+
This is the model provided as `bert-large-uncased-whole-word-masking-finetuned-squad`.
|
443 |
+
|
444 |
+
### `run_generation.py`: Text generation with GPT, GPT-2, CTRL, Transformer-XL and XLNet
|
445 |
+
|
446 |
+
A conditional generation script is also included to generate text from a prompt.
|
447 |
+
The generation script includes the [tricks](https://github.com/rusiaaman/XLNet-gen#methodology) proposed by Aman Rusia to get high-quality generation with memory models like Transformer-XL and XLNet (include a predefined text to make short inputs longer).
|
448 |
+
|
449 |
+
Here is how to run the script with the small version of OpenAI GPT-2 model:
|
450 |
+
|
451 |
+
```shell
|
452 |
+
python ./examples/run_generation.py \
|
453 |
+
--model_type=gpt2 \
|
454 |
+
--length=20 \
|
455 |
+
--model_name_or_path=gpt2 \
|
456 |
+
```
|
457 |
+
|
458 |
+
and from the Salesforce CTRL model:
|
459 |
+
```shell
|
460 |
+
python ./examples/run_generation.py \
|
461 |
+
--model_type=ctrl \
|
462 |
+
--length=20 \
|
463 |
+
--model_name_or_path=ctrl \
|
464 |
+
--temperature=0 \
|
465 |
+
--repetition_penalty=1.2 \
|
466 |
+
```
|
467 |
+
|
468 |
+
## Quick tour of model sharing
|
469 |
+
|
470 |
+
Starting with `v2.2.2`, you can now upload and share your fine-tuned models with the community, using the <abbr title="Command-line interface">CLI</abbr> that's built-in to the library.
|
471 |
+
|
472 |
+
**First, create an account on [https://huggingface.co/join](https://huggingface.co/join)**. Then:
|
473 |
+
|
474 |
+
```shell
|
475 |
+
transformers-cli login
|
476 |
+
# log in using the same credentials as on huggingface.co
|
477 |
+
```
|
478 |
+
Upload your model:
|
479 |
+
```shell
|
480 |
+
transformers-cli upload ./path/to/pretrained_model/
|
481 |
+
|
482 |
+
# ^^ Upload folder containing weights/tokenizer/config
|
483 |
+
# saved via `.save_pretrained()`
|
484 |
+
|
485 |
+
transformers-cli upload ./config.json [--filename folder/foobar.json]
|
486 |
+
|
487 |
+
# ^^ Upload a single file
|
488 |
+
# (you can optionally override its filename, which can be nested inside a folder)
|
489 |
+
```
|
490 |
+
|
491 |
+
Your model will then be accessible through its identifier, a concatenation of your username and the folder name above:
|
492 |
+
```python
|
493 |
+
"username/pretrained_model"
|
494 |
+
```
|
495 |
+
|
496 |
+
Anyone can load it from code:
|
497 |
+
```python
|
498 |
+
tokenizer = AutoTokenizer.from_pretrained("username/pretrained_model")
|
499 |
+
model = AutoModel.from_pretrained("username/pretrained_model")
|
500 |
+
```
|
501 |
+
|
502 |
+
Finally, list all your files on S3:
|
503 |
+
```shell
|
504 |
+
transformers-cli s3 ls
|
505 |
+
# List all your S3 objects.
|
506 |
+
```
|
507 |
+
|
508 |
+
You can also delete files:
|
509 |
+
|
510 |
+
```shell
|
511 |
+
transformers-cli s3 rm …
|
512 |
+
```
|
513 |
+
|
514 |
+
## Quick tour of pipelines
|
515 |
+
|
516 |
+
New in version `v2.3`: `Pipeline` are high-level objects which automatically handle tokenization, running your data through a transformers model
|
517 |
+
and outputting the result in a structured object.
|
518 |
+
|
519 |
+
You can create `Pipeline` objects for the following down-stream tasks:
|
520 |
+
|
521 |
+
- `feature-extraction`: Generates a tensor representation for the input sequence
|
522 |
+
- `ner`: Generates named entity mapping for each word in the input sequence.
|
523 |
+
- `sentiment-analysis`: Gives the polarity (positive / negative) of the whole input sequence.
|
524 |
+
- `text-classification`: Initialize a `TextClassificationPipeline` directly, or see `sentiment-analysis` for an example.
|
525 |
+
- `question-answering`: Provided some context and a question refering to the context, it will extract the answer to the question in the context.
|
526 |
+
- `fill-mask`: Takes an input sequence containing a masked token (e.g. `<mask>`) and return list of most probable filled sequences, with their probabilities.
|
527 |
+
|
528 |
+
```python
|
529 |
+
from transformers import pipeline
|
530 |
+
|
531 |
+
# Allocate a pipeline for sentiment-analysis
|
532 |
+
nlp = pipeline('sentiment-analysis')
|
533 |
+
nlp('We are very happy to include pipeline into the transformers repository.')
|
534 |
+
>>> {'label': 'POSITIVE', 'score': 0.99893874}
|
535 |
+
|
536 |
+
# Allocate a pipeline for question-answering
|
537 |
+
nlp = pipeline('question-answering')
|
538 |
+
nlp({
|
539 |
+
'question': 'What is the name of the repository ?',
|
540 |
+
'context': 'Pipeline have been included in the huggingface/transformers repository'
|
541 |
+
})
|
542 |
+
>>> {'score': 0.28756016668193496, 'start': 35, 'end': 59, 'answer': 'huggingface/transformers'}
|
543 |
+
```
|
544 |
+
|
545 |
+
## Migrating from pytorch-transformers to transformers
|
546 |
+
|
547 |
+
Here is a quick summary of what you should take care of when migrating from `pytorch-transformers` to `transformers`.
|
548 |
+
|
549 |
+
### Positional order of some models' keywords inputs (`attention_mask`, `token_type_ids`...) changed
|
550 |
+
|
551 |
+
To be able to use Torchscript (see #1010, #1204 and #1195) the specific order of some models **keywords inputs** (`attention_mask`, `token_type_ids`...) has been changed.
|
552 |
+
|
553 |
+
If you used to call the models with keyword names for keyword arguments, e.g. `model(inputs_ids, attention_mask=attention_mask, token_type_ids=token_type_ids)`, this should not cause any change.
|
554 |
+
|
555 |
+
If you used to call the models with positional inputs for keyword arguments, e.g. `model(inputs_ids, attention_mask, token_type_ids)`, you may have to double check the exact order of input arguments.
|
556 |
+
|
557 |
+
|
558 |
+
## Migrating from pytorch-pretrained-bert to transformers
|
559 |
+
|
560 |
+
Here is a quick summary of what you should take care of when migrating from `pytorch-pretrained-bert` to `transformers`.
|
561 |
+
|
562 |
+
### Models always output `tuples`
|
563 |
+
|
564 |
+
The main breaking change when migrating from `pytorch-pretrained-bert` to `transformers` is that every model's forward method always outputs a `tuple` with various elements depending on the model and the configuration parameters.
|
565 |
+
|
566 |
+
The exact content of the tuples for each model is detailed in the models' docstrings and the [documentation](https://huggingface.co/transformers/).
|
567 |
+
|
568 |
+
In pretty much every case, you will be fine by taking the first element of the output as the output you previously used in `pytorch-pretrained-bert`.
|
569 |
+
|
570 |
+
Here is a `pytorch-pretrained-bert` to `transformers` conversion example for a `BertForSequenceClassification` classification model:
|
571 |
+
|
572 |
+
```python
|
573 |
+
# Let's load our model
|
574 |
+
model = BertForSequenceClassification.from_pretrained('bert-base-uncased')
|
575 |
+
|
576 |
+
# If you used to have this line in pytorch-pretrained-bert:
|
577 |
+
loss = model(input_ids, labels=labels)
|
578 |
+
|
579 |
+
# Now just use this line in transformers to extract the loss from the output tuple:
|
580 |
+
outputs = model(input_ids, labels=labels)
|
581 |
+
loss = outputs[0]
|
582 |
+
|
583 |
+
# In transformers you can also have access to the logits:
|
584 |
+
loss, logits = outputs[:2]
|
585 |
+
|
586 |
+
# And even the attention weights if you configure the model to output them (and other outputs too, see the docstrings and documentation)
|
587 |
+
model = BertForSequenceClassification.from_pretrained('bert-base-uncased', output_attentions=True)
|
588 |
+
outputs = model(input_ids, labels=labels)
|
589 |
+
loss, logits, attentions = outputs
|
590 |
+
```
|
591 |
+
|
592 |
+
### Using hidden states
|
593 |
+
|
594 |
+
By enabling the configuration option `output_hidden_states`, it was possible to retrieve the last hidden states of the encoder. In `pytorch-transformers` as well as `transformers` the return value has changed slightly: `all_hidden_states` now also includes the hidden state of the embeddings in addition to those of the encoding layers. This allows users to easily access the embeddings final state.
|
595 |
+
|
596 |
+
### Serialization
|
597 |
+
|
598 |
+
Breaking change in the `from_pretrained()` method:
|
599 |
+
|
600 |
+
1. Models are now set in evaluation mode by default when instantiated with the `from_pretrained()` method. To train them, don't forget to set them back in training mode (`model.train()`) to activate the dropout modules.
|
601 |
+
|
602 |
+
2. The additional `*input` and `**kwargs` arguments supplied to the `from_pretrained()` method used to be directly passed to the underlying model's class `__init__()` method. They are now used to update the model configuration attribute instead, which can break derived model classes built based on the previous `BertForSequenceClassification` examples. We are working on a way to mitigate this breaking change in [#866](https://github.com/huggingface/transformers/pull/866) by forwarding the the model's `__init__()` method (i) the provided positional arguments and (ii) the keyword arguments which do not match any configuration class attributes.
|
603 |
+
|
604 |
+
Also, while not a breaking change, the serialization methods have been standardized and you probably should switch to the new method `save_pretrained(save_directory)` if you were using any other serialization method before.
|
605 |
+
|
606 |
+
Here is an example:
|
607 |
+
|
608 |
+
```python
|
609 |
+
### Let's load a model and tokenizer
|
610 |
+
model = BertForSequenceClassification.from_pretrained('bert-base-uncased')
|
611 |
+
tokenizer = BertTokenizer.from_pretrained('bert-base-uncased')
|
612 |
+
|
613 |
+
### Do some stuff to our model and tokenizer
|
614 |
+
# Ex: add new tokens to the vocabulary and embeddings of our model
|
615 |
+
tokenizer.add_tokens(['[SPECIAL_TOKEN_1]', '[SPECIAL_TOKEN_2]'])
|
616 |
+
model.resize_token_embeddings(len(tokenizer))
|
617 |
+
# Train our model
|
618 |
+
train(model)
|
619 |
+
|
620 |
+
### Now let's save our model and tokenizer to a directory
|
621 |
+
model.save_pretrained('./my_saved_model_directory/')
|
622 |
+
tokenizer.save_pretrained('./my_saved_model_directory/')
|
623 |
+
|
624 |
+
### Reload the model and the tokenizer
|
625 |
+
model = BertForSequenceClassification.from_pretrained('./my_saved_model_directory/')
|
626 |
+
tokenizer = BertTokenizer.from_pretrained('./my_saved_model_directory/')
|
627 |
+
```
|
628 |
+
|
629 |
+
### Optimizers: BertAdam & OpenAIAdam are now AdamW, schedules are standard PyTorch schedules
|
630 |
+
|
631 |
+
The two optimizers previously included, `BertAdam` and `OpenAIAdam`, have been replaced by a single `AdamW` optimizer which has a few differences:
|
632 |
+
|
633 |
+
- it only implements weights decay correction,
|
634 |
+
- schedules are now externals (see below),
|
635 |
+
- gradient clipping is now also external (see below).
|
636 |
+
|
637 |
+
The new optimizer `AdamW` matches PyTorch `Adam` optimizer API and let you use standard PyTorch or apex methods for the schedule and clipping.
|
638 |
+
|
639 |
+
The schedules are now standard [PyTorch learning rate schedulers](https://pytorch.org/docs/stable/optim.html#how-to-adjust-learning-rate) and not part of the optimizer anymore.
|
640 |
+
|
641 |
+
Here is a conversion examples from `BertAdam` with a linear warmup and decay schedule to `AdamW` and the same schedule:
|
642 |
+
|
643 |
+
```python
|
644 |
+
# Parameters:
|
645 |
+
lr = 1e-3
|
646 |
+
max_grad_norm = 1.0
|
647 |
+
num_training_steps = 1000
|
648 |
+
num_warmup_steps = 100
|
649 |
+
warmup_proportion = float(num_warmup_steps) / float(num_training_steps) # 0.1
|
650 |
+
|
651 |
+
### Previously BertAdam optimizer was instantiated like this:
|
652 |
+
optimizer = BertAdam(model.parameters(), lr=lr, schedule='warmup_linear', warmup=warmup_proportion, t_total=num_training_steps)
|
653 |
+
### and used like this:
|
654 |
+
for batch in train_data:
|
655 |
+
loss = model(batch)
|
656 |
+
loss.backward()
|
657 |
+
optimizer.step()
|
658 |
+
|
659 |
+
### In Transformers, optimizer and schedules are splitted and instantiated like this:
|
660 |
+
optimizer = AdamW(model.parameters(), lr=lr, correct_bias=False) # To reproduce BertAdam specific behavior set correct_bias=False
|
661 |
+
scheduler = get_linear_schedule_with_warmup(optimizer, num_warmup_steps=num_warmup_steps, num_training_steps=num_training_steps) # PyTorch scheduler
|
662 |
+
### and used like this:
|
663 |
+
for batch in train_data:
|
664 |
+
model.train()
|
665 |
+
loss = model(batch)
|
666 |
+
loss.backward()
|
667 |
+
torch.nn.utils.clip_grad_norm_(model.parameters(), max_grad_norm) # Gradient clipping is not in AdamW anymore (so you can use amp without issue)
|
668 |
+
optimizer.step()
|
669 |
+
scheduler.step()
|
670 |
+
optimizer.zero_grad()
|
671 |
+
```
|
672 |
+
|
673 |
+
## Citation
|
674 |
+
|
675 |
+
We now have a paper you can cite for the 🤗 Transformers library:
|
676 |
+
```
|
677 |
+
@article{Wolf2019HuggingFacesTS,
|
678 |
+
title={HuggingFace's Transformers: State-of-the-art Natural Language Processing},
|
679 |
+
author={Thomas Wolf and Lysandre Debut and Victor Sanh and Julien Chaumond and Clement Delangue and Anthony Moi and Pierric Cistac and Tim Rault and R'emi Louf and Morgan Funtowicz and Jamie Brew},
|
680 |
+
journal={ArXiv},
|
681 |
+
year={2019},
|
682 |
+
volume={abs/1910.03771}
|
683 |
+
}
|
684 |
+
```
|
server/transformers/deploy_multi_version_doc.sh
ADDED
@@ -0,0 +1,23 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
cd docs
|
2 |
+
|
3 |
+
function deploy_doc(){
|
4 |
+
echo "Creating doc at commit $1 and pushing to folder $2"
|
5 |
+
git checkout $1
|
6 |
+
if [ ! -z "$2" ]
|
7 |
+
then
|
8 |
+
echo "Pushing version" $2
|
9 |
+
make clean && make html && scp -r -oStrictHostKeyChecking=no _build/html $doc:$dir/$2
|
10 |
+
else
|
11 |
+
echo "Pushing master"
|
12 |
+
make clean && make html && scp -r -oStrictHostKeyChecking=no _build/html/* $doc:$dir
|
13 |
+
fi
|
14 |
+
}
|
15 |
+
|
16 |
+
deploy_doc "master"
|
17 |
+
deploy_doc "b33a385" v1.0.0
|
18 |
+
deploy_doc "fe02e45" v1.1.0
|
19 |
+
deploy_doc "89fd345" v1.2.0
|
20 |
+
deploy_doc "fc9faa8" v2.0.0
|
21 |
+
deploy_doc "3ddce1d" v2.1.1
|
22 |
+
deploy_doc "f2f3294" v2.2.0
|
23 |
+
deploy_doc "d0f8b9a" v2.3.0
|
server/transformers/docker/Dockerfile
ADDED
@@ -0,0 +1,7 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
FROM pytorch/pytorch:latest
|
2 |
+
|
3 |
+
RUN git clone https://github.com/NVIDIA/apex.git && cd apex && python setup.py install --cuda_ext --cpp_ext
|
4 |
+
|
5 |
+
RUN pip install transformers
|
6 |
+
|
7 |
+
WORKDIR /workspace
|
server/transformers/docs/Makefile
ADDED
@@ -0,0 +1,19 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# Minimal makefile for Sphinx documentation
|
2 |
+
#
|
3 |
+
|
4 |
+
# You can set these variables from the command line.
|
5 |
+
SPHINXOPTS =
|
6 |
+
SPHINXBUILD = sphinx-build
|
7 |
+
SOURCEDIR = source
|
8 |
+
BUILDDIR = _build
|
9 |
+
|
10 |
+
# Put it first so that "make" without argument is like "make help".
|
11 |
+
help:
|
12 |
+
@$(SPHINXBUILD) -M help "$(SOURCEDIR)" "$(BUILDDIR)" $(SPHINXOPTS) $(O)
|
13 |
+
|
14 |
+
.PHONY: help Makefile
|
15 |
+
|
16 |
+
# Catch-all target: route all unknown targets to Sphinx using the new
|
17 |
+
# "make mode" option. $(O) is meant as a shortcut for $(SPHINXOPTS).
|
18 |
+
%: Makefile
|
19 |
+
@$(SPHINXBUILD) -M $@ "$(SOURCEDIR)" "$(BUILDDIR)" $(SPHINXOPTS) $(O)
|
server/transformers/docs/README.md
ADDED
@@ -0,0 +1,67 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 |
+
## Packages installed
|
11 |
+
|
12 |
+
Here's an overview of all the packages installed. If you ran the previous command installing all packages from
|
13 |
+
`requirements.txt`, you do not need to run the following commands.
|
14 |
+
|
15 |
+
Building it requires the package `sphinx` that you can
|
16 |
+
install using:
|
17 |
+
|
18 |
+
```bash
|
19 |
+
pip install -U sphinx
|
20 |
+
```
|
21 |
+
|
22 |
+
You would also need the custom installed [theme](https://github.com/readthedocs/sphinx_rtd_theme) by
|
23 |
+
[Read The Docs](https://readthedocs.org/). You can install it using the following command:
|
24 |
+
|
25 |
+
```bash
|
26 |
+
pip install sphinx_rtd_theme
|
27 |
+
```
|
28 |
+
|
29 |
+
The third necessary package is the `recommonmark` package to accept Markdown as well as Restructured text:
|
30 |
+
|
31 |
+
```bash
|
32 |
+
pip install recommonmark
|
33 |
+
```
|
34 |
+
|
35 |
+
## Building the documentation
|
36 |
+
|
37 |
+
Make sure that there is a symlink from the `example` file (in /examples) inside the source folder. Run the following
|
38 |
+
command to generate it:
|
39 |
+
|
40 |
+
```bash
|
41 |
+
ln -s ../../examples/README.md examples.md
|
42 |
+
```
|
43 |
+
|
44 |
+
Once you have setup `sphinx`, you can build the documentation by running the following command in the `/docs` folder:
|
45 |
+
|
46 |
+
```bash
|
47 |
+
make html
|
48 |
+
```
|
49 |
+
|
50 |
+
---
|
51 |
+
**NOTE**
|
52 |
+
|
53 |
+
If you are adding/removing elements from the toc-tree or from any structural item, it is recommended to clean the build
|
54 |
+
directory before rebuilding. Run the following command to clean and build:
|
55 |
+
|
56 |
+
```bash
|
57 |
+
make clean && make html
|
58 |
+
```
|
59 |
+
|
60 |
+
---
|
61 |
+
|
62 |
+
It should build the static app that will be available under `/docs/_build/html`
|
63 |
+
|
64 |
+
## Adding a new element to the tree (toc-tree)
|
65 |
+
|
66 |
+
Accepted files are reStructuredText (.rst) and Markdown (.md). Create a file with its extension and put it
|
67 |
+
in the source directory. You can then link it to the toc-tree by putting the filename without the extension.
|
server/transformers/docs/source/_static/css/Calibre-Light.ttf
ADDED
Binary file (62.5 kB). View file
|
|
server/transformers/docs/source/_static/css/Calibre-Medium.otf
ADDED
Binary file (47.9 kB). View file
|
|
server/transformers/docs/source/_static/css/Calibre-Regular.otf
ADDED
Binary file (49.9 kB). View file
|
|
server/transformers/docs/source/_static/css/Calibre-Thin.otf
ADDED
Binary file (46.7 kB). View file
|
|
server/transformers/docs/source/_static/css/code-snippets.css
ADDED
@@ -0,0 +1,12 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
|
2 |
+
.highlight .c1, .highlight .sd{
|
3 |
+
color: #999
|
4 |
+
}
|
5 |
+
|
6 |
+
.highlight .nn, .highlight .k, .highlight .s1, .highlight .nb, .highlight .bp, .highlight .kc {
|
7 |
+
color: #FB8D68;
|
8 |
+
}
|
9 |
+
|
10 |
+
.highlight .kn, .highlight .nv, .highlight .s2, .highlight .ow {
|
11 |
+
color: #6670FF;
|
12 |
+
}
|
server/transformers/docs/source/_static/css/huggingface.css
ADDED
@@ -0,0 +1,196 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
/* The literal code blocks */
|
2 |
+
.rst-content tt.literal, .rst-content tt.literal, .rst-content code.literal {
|
3 |
+
color: #6670FF;
|
4 |
+
}
|
5 |
+
|
6 |
+
/* To keep the logo centered */
|
7 |
+
.wy-side-scroll {
|
8 |
+
width: auto;
|
9 |
+
font-size: 20px;
|
10 |
+
}
|
11 |
+
|
12 |
+
/* The div that holds the Hugging Face logo */
|
13 |
+
.HuggingFaceDiv {
|
14 |
+
width: 100%
|
15 |
+
}
|
16 |
+
|
17 |
+
/* The research field on top of the toc tree */
|
18 |
+
.wy-side-nav-search{
|
19 |
+
background-color: #6670FF;
|
20 |
+
}
|
21 |
+
|
22 |
+
/* The toc tree */
|
23 |
+
.wy-nav-side{
|
24 |
+
background-color: #6670FF;
|
25 |
+
}
|
26 |
+
|
27 |
+
/* The selected items in the toc tree */
|
28 |
+
.wy-menu-vertical li.current{
|
29 |
+
background-color: #A6B0FF;
|
30 |
+
}
|
31 |
+
|
32 |
+
/* When a list item that does belong to the selected block from the toc tree is hovered */
|
33 |
+
.wy-menu-vertical li.current a:hover{
|
34 |
+
background-color: #B6C0FF;
|
35 |
+
}
|
36 |
+
|
37 |
+
/* When a list item that does NOT belong to the selected block from the toc tree is hovered. */
|
38 |
+
.wy-menu-vertical li a:hover{
|
39 |
+
background-color: #A7AFFB;
|
40 |
+
}
|
41 |
+
|
42 |
+
/* The text items on the toc tree */
|
43 |
+
.wy-menu-vertical a {
|
44 |
+
color: #FFFFDD;
|
45 |
+
font-family: Calibre-Light, sans-serif;
|
46 |
+
}
|
47 |
+
.wy-menu-vertical header, .wy-menu-vertical p.caption{
|
48 |
+
color: white;
|
49 |
+
font-family: Calibre-Light, sans-serif;
|
50 |
+
}
|
51 |
+
|
52 |
+
/* The color inside the selected toc tree block */
|
53 |
+
.wy-menu-vertical li.toctree-l2 a, .wy-menu-vertical li.toctree-l3 a, .wy-menu-vertical li.toctree-l4 a {
|
54 |
+
color: black;
|
55 |
+
}
|
56 |
+
|
57 |
+
/* Inside the depth-2 selected toc tree block */
|
58 |
+
.wy-menu-vertical li.toctree-l2.current>a {
|
59 |
+
background-color: #B6C0FF
|
60 |
+
}
|
61 |
+
.wy-menu-vertical li.toctree-l2.current li.toctree-l3>a {
|
62 |
+
background-color: #C6D0FF
|
63 |
+
}
|
64 |
+
|
65 |
+
/* Inside the depth-3 selected toc tree block */
|
66 |
+
.wy-menu-vertical li.toctree-l3.current li.toctree-l4>a{
|
67 |
+
background-color: #D6E0FF
|
68 |
+
}
|
69 |
+
|
70 |
+
/* Inside code snippets */
|
71 |
+
.rst-content dl:not(.docutils) dt{
|
72 |
+
font-size: 15px;
|
73 |
+
}
|
74 |
+
|
75 |
+
/* Links */
|
76 |
+
a {
|
77 |
+
color: #6670FF;
|
78 |
+
}
|
79 |
+
|
80 |
+
/* Content bars */
|
81 |
+
.rst-content dl:not(.docutils) dt {
|
82 |
+
background-color: rgba(251, 141, 104, 0.1);
|
83 |
+
border-right: solid 2px #FB8D68;
|
84 |
+
border-left: solid 2px #FB8D68;
|
85 |
+
color: #FB8D68;
|
86 |
+
font-family: Calibre-Light, sans-serif;
|
87 |
+
border-top: none;
|
88 |
+
font-style: normal !important;
|
89 |
+
}
|
90 |
+
|
91 |
+
/* Expand button */
|
92 |
+
.wy-menu-vertical li.toctree-l2 span.toctree-expand,
|
93 |
+
.wy-menu-vertical li.on a span.toctree-expand, .wy-menu-vertical li.current>a span.toctree-expand,
|
94 |
+
.wy-menu-vertical li.toctree-l3 span.toctree-expand{
|
95 |
+
color: black;
|
96 |
+
}
|
97 |
+
|
98 |
+
/* Max window size */
|
99 |
+
.wy-nav-content{
|
100 |
+
max-width: 1200px;
|
101 |
+
}
|
102 |
+
|
103 |
+
/* Mobile header */
|
104 |
+
.wy-nav-top{
|
105 |
+
background-color: #6670FF;
|
106 |
+
}
|
107 |
+
|
108 |
+
|
109 |
+
/* Source spans */
|
110 |
+
.rst-content .viewcode-link, .rst-content .viewcode-back{
|
111 |
+
color: #6670FF;
|
112 |
+
font-size: 110%;
|
113 |
+
letter-spacing: 2px;
|
114 |
+
text-transform: uppercase;
|
115 |
+
}
|
116 |
+
|
117 |
+
/* It would be better for table to be visible without horizontal scrolling */
|
118 |
+
.wy-table-responsive table td, .wy-table-responsive table th{
|
119 |
+
white-space: normal;
|
120 |
+
}
|
121 |
+
|
122 |
+
.footer {
|
123 |
+
margin-top: 20px;
|
124 |
+
}
|
125 |
+
|
126 |
+
.footer__Social {
|
127 |
+
display: flex;
|
128 |
+
flex-direction: row;
|
129 |
+
}
|
130 |
+
|
131 |
+
.footer__CustomImage {
|
132 |
+
margin: 2px 5px 0 0;
|
133 |
+
}
|
134 |
+
|
135 |
+
/* class and method names in doc */
|
136 |
+
.rst-content dl:not(.docutils) tt.descname, .rst-content dl:not(.docutils) tt.descclassname, .rst-content dl:not(.docutils) tt.descname, .rst-content dl:not(.docutils) code.descname, .rst-content dl:not(.docutils) tt.descclassname, .rst-content dl:not(.docutils) code.descclassname{
|
137 |
+
font-family: Calibre, sans-serif;
|
138 |
+
font-size: 20px !important;
|
139 |
+
}
|
140 |
+
|
141 |
+
/* class name in doc*/
|
142 |
+
.rst-content dl:not(.docutils) tt.descname, .rst-content dl:not(.docutils) tt.descname, .rst-content dl:not(.docutils) code.descname{
|
143 |
+
margin-right: 10px;
|
144 |
+
font-family: Calibre-Medium, sans-serif;
|
145 |
+
}
|
146 |
+
|
147 |
+
/* Method and class parameters */
|
148 |
+
.sig-param{
|
149 |
+
line-height: 23px;
|
150 |
+
}
|
151 |
+
|
152 |
+
/* Class introduction "class" string at beginning */
|
153 |
+
.rst-content dl:not(.docutils) .property{
|
154 |
+
font-size: 18px;
|
155 |
+
color: black;
|
156 |
+
}
|
157 |
+
|
158 |
+
|
159 |
+
/* FONTS */
|
160 |
+
body{
|
161 |
+
font-family: Calibre, sans-serif;
|
162 |
+
font-size: 16px;
|
163 |
+
}
|
164 |
+
|
165 |
+
h1 {
|
166 |
+
font-family: Calibre-Thin, sans-serif;
|
167 |
+
font-size: 70px;
|
168 |
+
}
|
169 |
+
|
170 |
+
h2, .rst-content .toctree-wrapper p.caption, h3, h4, h5, h6, legend{
|
171 |
+
font-family: Calibre-Medium, sans-serif;
|
172 |
+
}
|
173 |
+
|
174 |
+
@font-face {
|
175 |
+
font-family: Calibre-Medium;
|
176 |
+
src: url(./Calibre-Medium.otf);
|
177 |
+
font-weight:400;
|
178 |
+
}
|
179 |
+
|
180 |
+
@font-face {
|
181 |
+
font-family: Calibre;
|
182 |
+
src: url(./Calibre-Regular.otf);
|
183 |
+
font-weight:400;
|
184 |
+
}
|
185 |
+
|
186 |
+
@font-face {
|
187 |
+
font-family: Calibre-Light;
|
188 |
+
src: url(./Calibre-Light.ttf);
|
189 |
+
font-weight:400;
|
190 |
+
}
|
191 |
+
|
192 |
+
@font-face {
|
193 |
+
font-family: Calibre-Thin;
|
194 |
+
src: url(./Calibre-Thin.otf);
|
195 |
+
font-weight:400;
|
196 |
+
}
|
server/transformers/docs/source/_static/js/custom.js
ADDED
@@ -0,0 +1,79 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
function addIcon() {
|
2 |
+
const huggingFaceLogo = "https://huggingface.co/landing/assets/transformers-docs/huggingface_logo.svg";
|
3 |
+
const image = document.createElement("img");
|
4 |
+
image.setAttribute("src", huggingFaceLogo);
|
5 |
+
|
6 |
+
const div = document.createElement("div");
|
7 |
+
div.appendChild(image);
|
8 |
+
div.style.textAlign = 'center';
|
9 |
+
div.style.paddingTop = '30px';
|
10 |
+
div.style.backgroundColor = '#6670FF';
|
11 |
+
|
12 |
+
const scrollDiv = document.querySelector(".wy-side-scroll");
|
13 |
+
scrollDiv.prepend(div);
|
14 |
+
}
|
15 |
+
|
16 |
+
function addCustomFooter() {
|
17 |
+
const customFooter = document.createElement("div");
|
18 |
+
const questionOrIssue = document.createElement("div");
|
19 |
+
questionOrIssue.innerHTML = "Stuck? Read our <a href='https://medium.com/huggingface'>Blog posts</a> or <a href='https://github.com/huggingface/transformers'>Create an issue</a>";
|
20 |
+
customFooter.appendChild(questionOrIssue);
|
21 |
+
customFooter.classList.add("footer");
|
22 |
+
|
23 |
+
const social = document.createElement("div");
|
24 |
+
social.classList.add("footer__Social");
|
25 |
+
|
26 |
+
const imageDetails = [
|
27 |
+
{ link: "https://huggingface.co", imageLink: "https://huggingface.co/landing/assets/transformers-docs/website.svg" },
|
28 |
+
{ link: "https://twitter.com/huggingface", imageLink: "https://huggingface.co/landing/assets/transformers-docs/twitter.svg" },
|
29 |
+
{ link: "https://github.com/huggingface", imageLink: "https://huggingface.co/landing/assets/transformers-docs/github.svg" },
|
30 |
+
{ link: "https://www.linkedin.com/company/huggingface/", imageLink: "https://huggingface.co/landing/assets/transformers-docs/linkedin.svg" }
|
31 |
+
];
|
32 |
+
|
33 |
+
imageDetails.forEach(imageLinks => {
|
34 |
+
const link = document.createElement("a");
|
35 |
+
const image = document.createElement("img");
|
36 |
+
image.src = imageLinks.imageLink;
|
37 |
+
link.href = imageLinks.link;
|
38 |
+
image.style.width = "30px";
|
39 |
+
image.classList.add("footer__CustomImage");
|
40 |
+
link.appendChild(image);
|
41 |
+
social.appendChild(link);
|
42 |
+
});
|
43 |
+
|
44 |
+
customFooter.appendChild(social);
|
45 |
+
document.querySelector("footer").appendChild(customFooter);
|
46 |
+
}
|
47 |
+
|
48 |
+
function addGithubButton() {
|
49 |
+
const div = `
|
50 |
+
<div class="github-repo">
|
51 |
+
<a
|
52 |
+
class="github-button"
|
53 |
+
href="https://github.com/huggingface/transformers" data-size="large" data-show-count="true" aria-label="Star huggingface/pytorch-transformers on GitHub">
|
54 |
+
Star
|
55 |
+
</a>
|
56 |
+
</div>
|
57 |
+
`;
|
58 |
+
document.querySelector(".wy-side-nav-search .icon-home").insertAdjacentHTML('afterend', div);
|
59 |
+
}
|
60 |
+
|
61 |
+
/*!
|
62 |
+
* github-buttons v2.2.10
|
63 |
+
* (c) 2019 なつき
|
64 |
+
* @license BSD-2-Clause
|
65 |
+
*/
|
66 |
+
/**
|
67 |
+
* modified to run programmatically
|
68 |
+
*/
|
69 |
+
function parseGithubButtons (){"use strict";var e=window.document,t=e.location,o=window.encodeURIComponent,r=window.decodeURIComponent,n=window.Math,a=window.HTMLElement,i=window.XMLHttpRequest,l="https://unpkg.com/[email protected]/dist/buttons.html",c=i&&i.prototype&&"withCredentials"in i.prototype,d=c&&a&&a.prototype.attachShadow&&!a.prototype.attachShadow.prototype,s=function(e,t,o){e.addEventListener?e.addEventListener(t,o):e.attachEvent("on"+t,o)},u=function(e,t,o){e.removeEventListener?e.removeEventListener(t,o):e.detachEvent("on"+t,o)},h=function(e,t,o){var r=function(n){return u(e,t,r),o(n)};s(e,t,r)},f=function(e,t,o){var r=function(n){if(t.test(e.readyState))return u(e,"readystatechange",r),o(n)};s(e,"readystatechange",r)},p=function(e){return function(t,o,r){var n=e.createElement(t);if(o)for(var a in o){var i=o[a];null!=i&&(null!=n[a]?n[a]=i:n.setAttribute(a,i))}if(r)for(var l=0,c=r.length;l<c;l++){var d=r[l];n.appendChild("string"==typeof d?e.createTextNode(d):d)}return n}},g=p(e),b=function(e){var t;return function(){t||(t=1,e.apply(this,arguments))}},m="body{margin:0}a{color:#24292e;text-decoration:none;outline:0}.octicon{display:inline-block;vertical-align:text-top;fill:currentColor}.widget{ display:inline-block;overflow:hidden;font-family:-apple-system, BlinkMacSystemFont, \"Segoe UI\", Helvetica, Arial, sans-serif;font-size:0;white-space:nowrap;-webkit-user-select:none;-moz-user-select:none;-ms-user-select:none;user-select:none}.btn,.social-count{display:inline-block;height:14px;padding:2px 5px;font-size:11px;font-weight:600;line-height:14px;vertical-align:bottom;cursor:pointer;border:1px solid #c5c9cc;border-radius:0.25em}.btn{background-color:#eff3f6;background-image:-webkit-linear-gradient(top, #fafbfc, #eff3f6 90%);background-image:-moz-linear-gradient(top, #fafbfc, #eff3f6 90%);background-image:linear-gradient(180deg, #fafbfc, #eff3f6 90%);background-position:-1px -1px;background-repeat:repeat-x;background-size:110% 110%;border-color:rgba(27,31,35,0.2);-ms-filter:\"progid:DXImageTransform.Microsoft.Gradient(startColorstr='#FFFAFBFC', endColorstr='#FFEEF2F5')\";*filter:progid:DXImageTransform.Microsoft.Gradient(startColorstr='#FFFAFBFC', endColorstr='#FFEEF2F5')}.btn:active{background-color:#e9ecef;background-image:none;border-color:#a5a9ac;border-color:rgba(27,31,35,0.35);box-shadow:inset 0 0.15em 0.3em rgba(27,31,35,0.15)}.btn:focus,.btn:hover{background-color:#e6ebf1;background-image:-webkit-linear-gradient(top, #f0f3f6, #e6ebf1 90%);background-image:-moz-linear-gradient(top, #f0f3f6, #e6ebf1 90%);background-image:linear-gradient(180deg, #f0f3f6, #e6ebf1 90%);border-color:#a5a9ac;border-color:rgba(27,31,35,0.35);-ms-filter:\"progid:DXImageTransform.Microsoft.Gradient(startColorstr='#FFF0F3F6', endColorstr='#FFE5EAF0')\";*filter:progid:DXImageTransform.Microsoft.Gradient(startColorstr='#FFF0F3F6', endColorstr='#FFE5EAF0')}.social-count{position:relative;margin-left:5px;background-color:#fff}.social-count:focus,.social-count:hover{color:#0366d6}.social-count b,.social-count i{position:absolute;top:50%;left:0;display:block;width:0;height:0;margin:-4px 0 0 -4px;border:solid transparent;border-width:4px 4px 4px 0;_line-height:0;_border-top-color:red !important;_border-bottom-color:red !important;_border-left-color:red !important;_filter:chroma(color=red)}.social-count b{border-right-color:#c5c9cc}.social-count i{margin-left:-3px;border-right-color:#fff}.lg .btn,.lg .social-count{height:16px;padding:5px 10px;font-size:12px;line-height:16px}.lg .social-count{margin-left:6px}.lg .social-count b,.lg .social-count i{margin:-5px 0 0 -5px;border-width:5px 5px 5px 0}.lg .social-count i{margin-left:-4px}\n",v={"mark-github":{width:16,height:16,path:'<path fill-rule="evenodd" d="M8 0C3.58 0 0 3.58 0 8c0 3.54 2.29 6.53 5.47 7.59.4.07.55-.17.55-.38 0-.19-.01-.82-.01-1.49-2.01.37-2.53-.49-2.69-.94-.09-.23-.48-.94-.82-1.13-.28-.15-.68-.52-.01-.53.63-.01 1.08.58 1.23.82.72 1.21 1.87.87 2.33.66.07-.52.28-.87.51-1.07-1.78-.2-3.64-.89-3.64-3.95 0-.87.31-1.59.82-2.15-.08-.2-.36-1.02.08-2.12 0 0 .67-.21 2.2.82.64-.18 1.32-.27 2-.27.68 0 1.36.09 2 .27 1.53-1.04 2.2-.82 2.2-.82.44 1.1.16 1.92.08 2.12.51.56.82 1.27.82 2.15 0 3.07-1.87 3.75-3.65 3.95.29.25.54.73.54 1.48 0 1.07-.01 1.93-.01 2.2 0 .21.15.46.55.38A8.013 8.013 0 0 0 16 8c0-4.42-3.58-8-8-8z"/>'},eye:{width:16,height:16,path:'<path fill-rule="evenodd" d="M8.06 2C3 2 0 8 0 8s3 6 8.06 6C13 14 16 8 16 8s-3-6-7.94-6zM8 12c-2.2 0-4-1.78-4-4 0-2.2 1.8-4 4-4 2.22 0 4 1.8 4 4 0 2.22-1.78 4-4 4zm2-4c0 1.11-.89 2-2 2-1.11 0-2-.89-2-2 0-1.11.89-2 2-2 1.11 0 2 .89 2 2z"/>'},star:{width:14,height:16,path:'<path fill-rule="evenodd" d="M14 6l-4.9-.64L7 1 4.9 5.36 0 6l3.6 3.26L2.67 14 7 11.67 11.33 14l-.93-4.74L14 6z"/>'},"repo-forked":{width:10,height:16,path:'<path fill-rule="evenodd" d="M8 1a1.993 1.993 0 0 0-1 3.72V6L5 8 3 6V4.72A1.993 1.993 0 0 0 2 1a1.993 1.993 0 0 0-1 3.72V6.5l3 3v1.78A1.993 1.993 0 0 0 5 15a1.993 1.993 0 0 0 1-3.72V9.5l3-3V4.72A1.993 1.993 0 0 0 8 1zM2 4.2C1.34 4.2.8 3.65.8 3c0-.65.55-1.2 1.2-1.2.65 0 1.2.55 1.2 1.2 0 .65-.55 1.2-1.2 1.2zm3 10c-.66 0-1.2-.55-1.2-1.2 0-.65.55-1.2 1.2-1.2.65 0 1.2.55 1.2 1.2 0 .65-.55 1.2-1.2 1.2zm3-10c-.66 0-1.2-.55-1.2-1.2 0-.65.55-1.2 1.2-1.2.65 0 1.2.55 1.2 1.2 0 .65-.55 1.2-1.2 1.2z"/>'},"issue-opened":{width:14,height:16,path:'<path fill-rule="evenodd" d="M7 2.3c3.14 0 5.7 2.56 5.7 5.7s-2.56 5.7-5.7 5.7A5.71 5.71 0 0 1 1.3 8c0-3.14 2.56-5.7 5.7-5.7zM7 1C3.14 1 0 4.14 0 8s3.14 7 7 7 7-3.14 7-7-3.14-7-7-7zm1 3H6v5h2V4zm0 6H6v2h2v-2z"/>'},"cloud-download":{width:16,height:16,path:'<path fill-rule="evenodd" d="M9 12h2l-3 3-3-3h2V7h2v5zm3-8c0-.44-.91-3-4.5-3C5.08 1 3 2.92 3 5 1.02 5 0 6.52 0 8c0 1.53 1 3 3 3h3V9.7H3C1.38 9.7 1.3 8.28 1.3 8c0-.17.05-1.7 1.7-1.7h1.3V5c0-1.39 1.56-2.7 3.2-2.7 2.55 0 3.13 1.55 3.2 1.8v1.2H12c.81 0 2.7.22 2.7 2.2 0 2.09-2.25 2.2-2.7 2.2h-2V11h2c2.08 0 4-1.16 4-3.5C16 5.06 14.08 4 12 4z"/>'}},w={},x=function(e,t,o){var r=p(e.ownerDocument),n=e.appendChild(r("style",{type:"text/css"}));n.styleSheet?n.styleSheet.cssText=m:n.appendChild(e.ownerDocument.createTextNode(m));var a,l,d=r("a",{className:"btn",href:t.href,target:"_blank",innerHTML:(a=t["data-icon"],l=/^large$/i.test(t["data-size"])?16:14,a=(""+a).toLowerCase().replace(/^octicon-/,""),{}.hasOwnProperty.call(v,a)||(a="mark-github"),'<svg version="1.1" width="'+l*v[a].width/v[a].height+'" height="'+l+'" viewBox="0 0 '+v[a].width+" "+v[a].height+'" class="octicon octicon-'+a+'" aria-hidden="true">'+v[a].path+"</svg>"),"aria-label":t["aria-label"]||void 0},[" ",r("span",{},[t["data-text"]||""])]);/\.github\.com$/.test("."+d.hostname)?/^https?:\/\/((gist\.)?github\.com\/[^\/?#]+\/[^\/?#]+\/archive\/|github\.com\/[^\/?#]+\/[^\/?#]+\/releases\/download\/|codeload\.github\.com\/)/.test(d.href)&&(d.target="_top"):(d.href="#",d.target="_self");var u,h,g,x,y=e.appendChild(r("div",{className:"widget"+(/^large$/i.test(t["data-size"])?" lg":"")},[d]));/^(true|1)$/i.test(t["data-show-count"])&&"github.com"===d.hostname&&(u=d.pathname.replace(/^(?!\/)/,"/").match(/^\/([^\/?#]+)(?:\/([^\/?#]+)(?:\/(?:(subscription)|(fork)|(issues)|([^\/?#]+)))?)?(?:[\/?#]|$)/))&&!u[6]?(u[2]?(h="/repos/"+u[1]+"/"+u[2],u[3]?(x="subscribers_count",g="watchers"):u[4]?(x="forks_count",g="network"):u[5]?(x="open_issues_count",g="issues"):(x="stargazers_count",g="stargazers")):(h="/users/"+u[1],g=x="followers"),function(e,t){var o=w[e]||(w[e]=[]);if(!(o.push(t)>1)){var r=b(function(){for(delete w[e];t=o.shift();)t.apply(null,arguments)});if(c){var n=new i;s(n,"abort",r),s(n,"error",r),s(n,"load",function(){var e;try{e=JSON.parse(n.responseText)}catch(e){return void r(e)}r(200!==n.status,e)}),n.open("GET",e),n.send()}else{var a=this||window;a._=function(e){a._=null,r(200!==e.meta.status,e.data)};var l=p(a.document)("script",{async:!0,src:e+(/\?/.test(e)?"&":"?")+"callback=_"}),d=function(){a._&&a._({meta:{}})};s(l,"load",d),s(l,"error",d),l.readyState&&f(l,/de|m/,d),a.document.getElementsByTagName("head")[0].appendChild(l)}}}.call(this,"https://api.github.com"+h,function(e,t){if(!e){var n=t[x];y.appendChild(r("a",{className:"social-count",href:t.html_url+"/"+g,target:"_blank","aria-label":n+" "+x.replace(/_count$/,"").replace("_"," ").slice(0,n<2?-1:void 0)+" on GitHub"},[r("b"),r("i"),r("span",{},[(""+n).replace(/\B(?=(\d{3})+(?!\d))/g,",")])]))}o&&o(y)})):o&&o(y)},y=window.devicePixelRatio||1,C=function(e){return(y>1?n.ceil(n.round(e*y)/y*2)/2:n.ceil(e))||0},F=function(e,t){e.style.width=t[0]+"px",e.style.height=t[1]+"px"},k=function(t,r){if(null!=t&&null!=r)if(t.getAttribute&&(t=function(e){for(var t={href:e.href,title:e.title,"aria-label":e.getAttribute("aria-label")},o=["icon","text","size","show-count"],r=0,n=o.length;r<n;r++){var a="data-"+o[r];t[a]=e.getAttribute(a)}return null==t["data-text"]&&(t["data-text"]=e.textContent||e.innerText),t}(t)),d){var a=g("span",{title:t.title||void 0});x(a.attachShadow({mode:"closed"}),t,function(){r(a)})}else{var i=g("iframe",{src:"javascript:0",title:t.title||void 0,allowtransparency:!0,scrolling:"no",frameBorder:0});F(i,[0,0]),i.style.border="none";var c=function(){var a,d=i.contentWindow;try{a=d.document.body}catch(t){return void e.body.appendChild(i.parentNode.removeChild(i))}u(i,"load",c),x.call(d,a,t,function(e){var a=function(e){var t=e.offsetWidth,o=e.offsetHeight;if(e.getBoundingClientRect){var r=e.getBoundingClientRect();t=n.max(t,C(r.width)),o=n.max(o,C(r.height))}return[t,o]}(e);i.parentNode.removeChild(i),h(i,"load",function(){F(i,a)}),i.src=l+"#"+(i.name=function(e){var t=[];for(var r in e){var n=e[r];null!=n&&t.push(o(r)+"="+o(n))}return t.join("&")}(t)),r(i)})};s(i,"load",c),e.body.appendChild(i)}};t.protocol+"//"+t.host+t.pathname===l?x(e.body,function(e){for(var t={},o=e.split("&"),n=0,a=o.length;n<a;n++){var i=o[n];if(""!==i){var l=i.split("=");t[r(l[0])]=null!=l[1]?r(l.slice(1).join("=")):void 0}}return t}(window.name||t.hash.replace(/^#/,""))):function(t){if(/m/.test(e.readyState)||!/g/.test(e.readyState)&&!e.documentElement.doScroll)setTimeout(t);else if(e.addEventListener){var o=b(t);h(e,"DOMContentLoaded",o),h(window,"load",o)}else f(e,/m/,t)}(function(){for(var t=e.querySelectorAll?e.querySelectorAll("a.github-button"):function(){for(var t=[],o=e.getElementsByTagName("a"),r=0,n=o.length;r<n;r++)~(" "+o[r].className+" ").replace(/[ \t\n\f\r]+/g," ").indexOf(" github-button ")&&t.push(o[r]);return t}(),o=0,r=t.length;o<r;o++)!function(e){k(e,function(t){e.parentNode.replaceChild(t,e)})}(t[o])})};
|
70 |
+
|
71 |
+
|
72 |
+
function onLoad() {
|
73 |
+
addIcon();
|
74 |
+
addCustomFooter();
|
75 |
+
addGithubButton();
|
76 |
+
parseGithubButtons();
|
77 |
+
}
|
78 |
+
|
79 |
+
window.addEventListener("load", onLoad);
|
server/transformers/docs/source/_static/js/huggingface_logo.svg
ADDED
server/transformers/docs/source/benchmarks.md
ADDED
@@ -0,0 +1,54 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# Benchmarks
|
2 |
+
|
3 |
+
This section is dedicated to the Benchmarks done by the library, both by maintainers, contributors and users. These
|
4 |
+
benchmark will help keep track of the preformance improvements that are brought to our models across versions.
|
5 |
+
|
6 |
+
## Benchmarking all models for inference
|
7 |
+
|
8 |
+
As of version 2.1 we have benchmarked all models for inference, across many different settings: using PyTorch, with
|
9 |
+
and without TorchScript, using TensorFlow, with and without XLA. All of those tests were done across CPUs (except for
|
10 |
+
TensorFlow XLA) and GPUs.
|
11 |
+
|
12 |
+
The approach is detailed in the [following blogpost](https://medium.com/huggingface/benchmarking-transformers-pytorch-and-tensorflow-e2917fb891c2)
|
13 |
+
|
14 |
+
The results are available [here](https://docs.google.com/spreadsheets/d/1sryqufw2D0XlUH4sq3e9Wnxu5EAQkaohzrJbd5HdQ_w/edit?usp=sharing).
|
15 |
+
|
16 |
+
## TF2 with mixed precision, XLA, Distribution (@tlkh)
|
17 |
+
|
18 |
+
This work was done by [Timothy Liu](https://github.com/tlkh).
|
19 |
+
|
20 |
+
There are very positive results to be gained from the various TensorFlow 2.0 features:
|
21 |
+
|
22 |
+
- Automatic Mixed Precision (AMP)
|
23 |
+
- XLA compiler
|
24 |
+
- Distribution strategies (multi-GPU)
|
25 |
+
|
26 |
+
The benefits are listed here (tested on CoLA, MRPC, SST-2):
|
27 |
+
|
28 |
+
- AMP: Between 1.4x to 1.6x decrease in overall time without change in batch size
|
29 |
+
- AMP+XLA: Up to 2.5x decrease in overall time on SST-2 (larger dataset)
|
30 |
+
- Distribution: Between 1.4x to 3.4x decrease in overall time on 4xV100
|
31 |
+
- Combined: Up to 5.7x decrease in overall training time, or 9.1x training throughput
|
32 |
+
|
33 |
+
The model quality (measured by the validation accuracy) fluctuates slightly. Taking an average of 4 training runs
|
34 |
+
on a single GPU gives the following results:
|
35 |
+
|
36 |
+
- CoLA: AMP results in slighter lower acc (0.820 vs 0.824)
|
37 |
+
- MRPC: AMP results in lower acc (0.823 vs 0.835)
|
38 |
+
- SST-2: AMP results in slighter lower acc (0.918 vs 0.922)
|
39 |
+
|
40 |
+
However, in a distributed setting with 4xV100 (4x batch size), AMP can yield in better results:
|
41 |
+
|
42 |
+
CoLA: AMP results in higher acc (0.828 vs 0.812)
|
43 |
+
MRPC: AMP results in lower acc (0.817 vs 0.827)
|
44 |
+
SST-2: AMP results in slightly lower acc (0.926 vs 0.929)
|
45 |
+
|
46 |
+
The benchmark script is available [here](https://github.com/NVAITC/benchmarking/blob/master/tf2/bert_dist.py).
|
47 |
+
|
48 |
+
Note: on some tasks (e.g. MRPC), the dataset is too small. The overhead due to the model compilation with XLA as well
|
49 |
+
as the distribution strategy setup does not speed things up. The XLA compile time is also the reason why although throughput
|
50 |
+
can increase a lot (e.g. 2.7x for single GPU), overall (end-to-end) training speed-up is not as fast (as low as 1.4x)
|
51 |
+
|
52 |
+
The benefits as seen on SST-2 (larger dataset) is much clear.
|
53 |
+
|
54 |
+
All results can be seen on this [Google Sheet](https://docs.google.com/spreadsheets/d/1538MN224EzjbRL239sqSiUy6YY-rAjHyXhTzz_Zptls/edit#gid=960868445).
|
server/transformers/docs/source/bertology.rst
ADDED
@@ -0,0 +1,18 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
BERTology
|
2 |
+
---------
|
3 |
+
|
4 |
+
There is a growing field of study concerned with investigating the inner working of large-scale transformers like BERT (that some call "BERTology"). Some good examples of this field are:
|
5 |
+
|
6 |
+
|
7 |
+
* BERT Rediscovers the Classical NLP Pipeline by Ian Tenney, Dipanjan Das, Ellie Pavlick: https://arxiv.org/abs/1905.05950
|
8 |
+
* Are Sixteen Heads Really Better than One? by Paul Michel, Omer Levy, Graham Neubig: https://arxiv.org/abs/1905.10650
|
9 |
+
* What Does BERT Look At? An Analysis of BERT's Attention by Kevin Clark, Urvashi Khandelwal, Omer Levy, Christopher D. Manning: https://arxiv.org/abs/1906.04341
|
10 |
+
|
11 |
+
In order to help this new field develop, we have included a few additional features in the BERT/GPT/GPT-2 models to help people access the inner representations, mainly adapted from the great work of Paul Michel (https://arxiv.org/abs/1905.10650):
|
12 |
+
|
13 |
+
|
14 |
+
* accessing all the hidden-states of BERT/GPT/GPT-2,
|
15 |
+
* accessing all the attention weights for each head of BERT/GPT/GPT-2,
|
16 |
+
* retrieving heads output values and gradients to be able to compute head importance score and prune head as explained in https://arxiv.org/abs/1905.10650.
|
17 |
+
|
18 |
+
To help you understand and use these features, we have added a specific example script: `bertology.py <https://github.com/huggingface/transformers/blob/master/examples/run_bertology.py>`_ while extract information and prune a model pre-trained on GLUE.
|
server/transformers/docs/source/conf.py
ADDED
@@ -0,0 +1,188 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# -*- coding: utf-8 -*-
|
2 |
+
#
|
3 |
+
# Configuration file for the Sphinx documentation builder.
|
4 |
+
#
|
5 |
+
# This file does only contain a selection of the most common options. For a
|
6 |
+
# full list see the documentation:
|
7 |
+
# http://www.sphinx-doc.org/en/master/config
|
8 |
+
|
9 |
+
# -- Path setup --------------------------------------------------------------
|
10 |
+
|
11 |
+
# If extensions (or modules to document with autodoc) are in another directory,
|
12 |
+
# add these directories to sys.path here. If the directory is relative to the
|
13 |
+
# documentation root, use os.path.abspath to make it absolute, like shown here.
|
14 |
+
#
|
15 |
+
import os
|
16 |
+
import sys
|
17 |
+
sys.path.insert(0, os.path.abspath('../../src'))
|
18 |
+
|
19 |
+
|
20 |
+
# -- Project information -----------------------------------------------------
|
21 |
+
|
22 |
+
project = u'transformers'
|
23 |
+
copyright = u'2019, huggingface'
|
24 |
+
author = u'huggingface'
|
25 |
+
|
26 |
+
# The short X.Y version
|
27 |
+
version = u''
|
28 |
+
# The full version, including alpha/beta/rc tags
|
29 |
+
release = u'2.4.1'
|
30 |
+
|
31 |
+
|
32 |
+
# -- General configuration ---------------------------------------------------
|
33 |
+
|
34 |
+
# If your documentation needs a minimal Sphinx version, state it here.
|
35 |
+
#
|
36 |
+
# needs_sphinx = '1.0'
|
37 |
+
|
38 |
+
# Add any Sphinx extension module names here, as strings. They can be
|
39 |
+
# extensions coming with Sphinx (named 'sphinx.ext.*') or your custom
|
40 |
+
# ones.
|
41 |
+
extensions = [
|
42 |
+
'sphinx.ext.autodoc',
|
43 |
+
'sphinx.ext.coverage',
|
44 |
+
'sphinx.ext.napoleon',
|
45 |
+
'recommonmark',
|
46 |
+
'sphinx.ext.viewcode',
|
47 |
+
'sphinx_markdown_tables'
|
48 |
+
]
|
49 |
+
|
50 |
+
# Add any paths that contain templates here, relative to this directory.
|
51 |
+
templates_path = ['_templates']
|
52 |
+
|
53 |
+
# The suffix(es) of source filenames.
|
54 |
+
# You can specify multiple suffix as a list of string:
|
55 |
+
#
|
56 |
+
source_suffix = ['.rst', '.md']
|
57 |
+
# source_suffix = '.rst'
|
58 |
+
|
59 |
+
# The master toctree document.
|
60 |
+
master_doc = 'index'
|
61 |
+
|
62 |
+
# The language for content autogenerated by Sphinx. Refer to documentation
|
63 |
+
# for a list of supported languages.
|
64 |
+
#
|
65 |
+
# This is also used if you do content translation via gettext catalogs.
|
66 |
+
# Usually you set "language" from the command line for these cases.
|
67 |
+
language = None
|
68 |
+
|
69 |
+
# List of patterns, relative to source directory, that match files and
|
70 |
+
# directories to ignore when looking for source files.
|
71 |
+
# This pattern also affects html_static_path and html_extra_path.
|
72 |
+
exclude_patterns = [u'_build', 'Thumbs.db', '.DS_Store']
|
73 |
+
|
74 |
+
# The name of the Pygments (syntax highlighting) style to use.
|
75 |
+
pygments_style = None
|
76 |
+
|
77 |
+
|
78 |
+
# -- Options for HTML output -------------------------------------------------
|
79 |
+
|
80 |
+
# The theme to use for HTML and HTML Help pages. See the documentation for
|
81 |
+
# a list of builtin themes.
|
82 |
+
#
|
83 |
+
html_theme = 'sphinx_rtd_theme'
|
84 |
+
|
85 |
+
# Theme options are theme-specific and customize the look and feel of a theme
|
86 |
+
# further. For a list of options available for each theme, see the
|
87 |
+
# documentation.
|
88 |
+
#
|
89 |
+
html_theme_options = {
|
90 |
+
'analytics_id': 'UA-83738774-2'
|
91 |
+
}
|
92 |
+
|
93 |
+
# Add any paths that contain custom static files (such as style sheets) here,
|
94 |
+
# relative to this directory. They are copied after the builtin static files,
|
95 |
+
# so a file named "default.css" will overwrite the builtin "default.css".
|
96 |
+
html_static_path = ['_static']
|
97 |
+
|
98 |
+
# Custom sidebar templates, must be a dictionary that maps document names
|
99 |
+
# to template names.
|
100 |
+
#
|
101 |
+
# The default sidebars (for documents that don't match any pattern) are
|
102 |
+
# defined by theme itself. Builtin themes are using these templates by
|
103 |
+
# default: ``['localtoc.html', 'relations.html', 'sourcelink.html',
|
104 |
+
# 'searchbox.html']``.
|
105 |
+
#
|
106 |
+
# html_sidebars = {}
|
107 |
+
|
108 |
+
|
109 |
+
# -- Options for HTMLHelp output ---------------------------------------------
|
110 |
+
|
111 |
+
# Output file base name for HTML help builder.
|
112 |
+
htmlhelp_basename = 'transformersdoc'
|
113 |
+
|
114 |
+
|
115 |
+
# -- Options for LaTeX output ------------------------------------------------
|
116 |
+
|
117 |
+
latex_elements = {
|
118 |
+
# The paper size ('letterpaper' or 'a4paper').
|
119 |
+
#
|
120 |
+
# 'papersize': 'letterpaper',
|
121 |
+
|
122 |
+
# The font size ('10pt', '11pt' or '12pt').
|
123 |
+
#
|
124 |
+
# 'pointsize': '10pt',
|
125 |
+
|
126 |
+
# Additional stuff for the LaTeX preamble.
|
127 |
+
#
|
128 |
+
# 'preamble': '',
|
129 |
+
|
130 |
+
# Latex figure (float) alignment
|
131 |
+
#
|
132 |
+
# 'figure_align': 'htbp',
|
133 |
+
}
|
134 |
+
|
135 |
+
# Grouping the document tree into LaTeX files. List of tuples
|
136 |
+
# (source start file, target name, title,
|
137 |
+
# author, documentclass [howto, manual, or own class]).
|
138 |
+
latex_documents = [
|
139 |
+
(master_doc, 'transformers.tex', u'transformers Documentation',
|
140 |
+
u'huggingface', 'manual'),
|
141 |
+
]
|
142 |
+
|
143 |
+
|
144 |
+
# -- Options for manual page output ------------------------------------------
|
145 |
+
|
146 |
+
# One entry per manual page. List of tuples
|
147 |
+
# (source start file, name, description, authors, manual section).
|
148 |
+
man_pages = [
|
149 |
+
(master_doc, 'transformers', u'transformers Documentation',
|
150 |
+
[author], 1)
|
151 |
+
]
|
152 |
+
|
153 |
+
|
154 |
+
# -- Options for Texinfo output ----------------------------------------------
|
155 |
+
|
156 |
+
# Grouping the document tree into Texinfo files. List of tuples
|
157 |
+
# (source start file, target name, title, author,
|
158 |
+
# dir menu entry, description, category)
|
159 |
+
texinfo_documents = [
|
160 |
+
(master_doc, 'transformers', u'transformers Documentation',
|
161 |
+
author, 'transformers', 'One line description of project.',
|
162 |
+
'Miscellaneous'),
|
163 |
+
]
|
164 |
+
|
165 |
+
|
166 |
+
# -- Options for Epub output -------------------------------------------------
|
167 |
+
|
168 |
+
# Bibliographic Dublin Core info.
|
169 |
+
epub_title = project
|
170 |
+
|
171 |
+
# The unique identifier of the text. This can be a ISBN number
|
172 |
+
# or the project homepage.
|
173 |
+
#
|
174 |
+
# epub_identifier = ''
|
175 |
+
|
176 |
+
# A unique identification for the text.
|
177 |
+
#
|
178 |
+
# epub_uid = ''
|
179 |
+
|
180 |
+
# A list of files that should not be packed into the epub file.
|
181 |
+
epub_exclude_files = ['search.html']
|
182 |
+
|
183 |
+
def setup(app):
|
184 |
+
app.add_stylesheet('css/huggingface.css')
|
185 |
+
app.add_stylesheet('css/code-snippets.css')
|
186 |
+
app.add_js_file('js/custom.js')
|
187 |
+
|
188 |
+
# -- Extension configuration -------------------------------------------------
|
server/transformers/docs/source/converting_tensorflow_models.rst
ADDED
@@ -0,0 +1,137 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
Converting Tensorflow Checkpoints
|
2 |
+
================================================
|
3 |
+
|
4 |
+
A command-line interface is provided to convert original Bert/GPT/GPT-2/Transformer-XL/XLNet/XLM checkpoints in models than be loaded using the ``from_pretrained`` methods of the library.
|
5 |
+
|
6 |
+
.. note::
|
7 |
+
Since 2.3.0 the conversion script is now part of the transformers CLI (**transformers-cli**)
|
8 |
+
available in any transformers >= 2.3.0 installation.
|
9 |
+
|
10 |
+
The documentation below reflects the **transformers-cli convert** command format.
|
11 |
+
|
12 |
+
BERT
|
13 |
+
^^^^
|
14 |
+
|
15 |
+
You can convert any TensorFlow checkpoint for BERT (in particular `the pre-trained models released by Google <https://github.com/google-research/bert#pre-trained-models>`_\ ) in a PyTorch save file by using the `convert_tf_checkpoint_to_pytorch.py <https://github.com/huggingface/transformers/blob/master/transformers/convert_tf_checkpoint_to_pytorch.py>`_ script.
|
16 |
+
|
17 |
+
This CLI takes as input a TensorFlow checkpoint (three files starting with ``bert_model.ckpt``\ ) and the associated configuration file (\ ``bert_config.json``\ ), and creates a PyTorch model for this configuration, loads the weights from the TensorFlow checkpoint in the PyTorch model and saves the resulting model in a standard PyTorch save file that can be imported using ``torch.load()`` (see examples in `run_bert_extract_features.py <https://github.com/huggingface/pytorch-pretrained-BERT/tree/master/examples/run_bert_extract_features.py>`_\ , `run_bert_classifier.py <https://github.com/huggingface/pytorch-pretrained-BERT/tree/master/examples/run_bert_classifier.py>`_ and `run_bert_squad.py <https://github.com/huggingface/pytorch-pretrained-BERT/tree/master/examples/run_bert_squad.py>`_\ ).
|
18 |
+
|
19 |
+
You only need to run this conversion script **once** to get a PyTorch model. You can then disregard the TensorFlow checkpoint (the three files starting with ``bert_model.ckpt``\ ) but be sure to keep the configuration file (\ ``bert_config.json``\ ) and the vocabulary file (\ ``vocab.txt``\ ) as these are needed for the PyTorch model too.
|
20 |
+
|
21 |
+
To run this specific conversion script you will need to have TensorFlow and PyTorch installed (\ ``pip install tensorflow``\ ). The rest of the repository only requires PyTorch.
|
22 |
+
|
23 |
+
Here is an example of the conversion process for a pre-trained ``BERT-Base Uncased`` model:
|
24 |
+
|
25 |
+
.. code-block:: shell
|
26 |
+
|
27 |
+
export BERT_BASE_DIR=/path/to/bert/uncased_L-12_H-768_A-12
|
28 |
+
|
29 |
+
<<<<<<< HEAD
|
30 |
+
transformers-cli --model_type bert \
|
31 |
+
=======
|
32 |
+
transformers-cli convert --model_type bert \
|
33 |
+
>>>>>>> bfec203d4ed95255619e7e2f28c9040744a16232
|
34 |
+
--tf_checkpoint $BERT_BASE_DIR/bert_model.ckpt \
|
35 |
+
--config $BERT_BASE_DIR/bert_config.json \
|
36 |
+
--pytorch_dump_output $BERT_BASE_DIR/pytorch_model.bin
|
37 |
+
|
38 |
+
You can download Google's pre-trained models for the conversion `here <https://github.com/google-research/bert#pre-trained-models>`__.
|
39 |
+
|
40 |
+
OpenAI GPT
|
41 |
+
^^^^^^^^^^
|
42 |
+
|
43 |
+
Here is an example of the conversion process for a pre-trained OpenAI GPT model, assuming that your NumPy checkpoint save as the same format than OpenAI pretrained model (see `here <https://github.com/openai/finetune-transformer-lm>`__\ )
|
44 |
+
|
45 |
+
.. code-block:: shell
|
46 |
+
|
47 |
+
export OPENAI_GPT_CHECKPOINT_FOLDER_PATH=/path/to/openai/pretrained/numpy/weights
|
48 |
+
|
49 |
+
<<<<<<< HEAD
|
50 |
+
transformers-cli --model_type gpt \
|
51 |
+
=======
|
52 |
+
transformers-cli convert --model_type gpt \
|
53 |
+
>>>>>>> bfec203d4ed95255619e7e2f28c9040744a16232
|
54 |
+
--tf_checkpoint $OPENAI_GPT_CHECKPOINT_FOLDER_PATH \
|
55 |
+
--pytorch_dump_output $PYTORCH_DUMP_OUTPUT \
|
56 |
+
[--config OPENAI_GPT_CONFIG] \
|
57 |
+
[--finetuning_task_name OPENAI_GPT_FINETUNED_TASK] \
|
58 |
+
|
59 |
+
|
60 |
+
OpenAI GPT-2
|
61 |
+
^^^^^^^^^^^^
|
62 |
+
|
63 |
+
Here is an example of the conversion process for a pre-trained OpenAI GPT-2 model (see `here <https://github.com/openai/gpt-2>`__\ )
|
64 |
+
|
65 |
+
.. code-block:: shell
|
66 |
+
|
67 |
+
export OPENAI_GPT2_CHECKPOINT_PATH=/path/to/gpt2/pretrained/weights
|
68 |
+
|
69 |
+
<<<<<<< HEAD
|
70 |
+
transformers-cli --model_type gpt2 \
|
71 |
+
=======
|
72 |
+
transformers-cli convert --model_type gpt2 \
|
73 |
+
>>>>>>> bfec203d4ed95255619e7e2f28c9040744a16232
|
74 |
+
--tf_checkpoint $OPENAI_GPT2_CHECKPOINT_PATH \
|
75 |
+
--pytorch_dump_output $PYTORCH_DUMP_OUTPUT \
|
76 |
+
[--config OPENAI_GPT2_CONFIG] \
|
77 |
+
[--finetuning_task_name OPENAI_GPT2_FINETUNED_TASK]
|
78 |
+
|
79 |
+
Transformer-XL
|
80 |
+
^^^^^^^^^^^^^^
|
81 |
+
|
82 |
+
Here is an example of the conversion process for a pre-trained Transformer-XL model (see `here <https://github.com/kimiyoung/transformer-xl/tree/master/tf#obtain-and-evaluate-pretrained-sota-models>`__\ )
|
83 |
+
|
84 |
+
.. code-block:: shell
|
85 |
+
|
86 |
+
export TRANSFO_XL_CHECKPOINT_FOLDER_PATH=/path/to/transfo/xl/checkpoint
|
87 |
+
|
88 |
+
<<<<<<< HEAD
|
89 |
+
transformers-cli --model_type transfo_xl \
|
90 |
+
=======
|
91 |
+
transformers-cli convert --model_type transfo_xl \
|
92 |
+
>>>>>>> bfec203d4ed95255619e7e2f28c9040744a16232
|
93 |
+
--tf_checkpoint $TRANSFO_XL_CHECKPOINT_FOLDER_PATH \
|
94 |
+
--pytorch_dump_output $PYTORCH_DUMP_OUTPUT \
|
95 |
+
[--config TRANSFO_XL_CONFIG] \
|
96 |
+
[--finetuning_task_name TRANSFO_XL_FINETUNED_TASK]
|
97 |
+
|
98 |
+
|
99 |
+
XLNet
|
100 |
+
^^^^^
|
101 |
+
|
102 |
+
Here is an example of the conversion process for a pre-trained XLNet model:
|
103 |
+
|
104 |
+
.. code-block:: shell
|
105 |
+
|
106 |
+
export TRANSFO_XL_CHECKPOINT_PATH=/path/to/xlnet/checkpoint
|
107 |
+
export TRANSFO_XL_CONFIG_PATH=/path/to/xlnet/config
|
108 |
+
|
109 |
+
<<<<<<< HEAD
|
110 |
+
transformers-cli --model_type xlnet \
|
111 |
+
=======
|
112 |
+
transformers-cli convert --model_type xlnet \
|
113 |
+
>>>>>>> bfec203d4ed95255619e7e2f28c9040744a16232
|
114 |
+
--tf_checkpoint $TRANSFO_XL_CHECKPOINT_PATH \
|
115 |
+
--config $TRANSFO_XL_CONFIG_PATH \
|
116 |
+
--pytorch_dump_output $PYTORCH_DUMP_OUTPUT \
|
117 |
+
[--finetuning_task_name XLNET_FINETUNED_TASK] \
|
118 |
+
|
119 |
+
|
120 |
+
XLM
|
121 |
+
^^^
|
122 |
+
|
123 |
+
Here is an example of the conversion process for a pre-trained XLM model:
|
124 |
+
|
125 |
+
.. code-block:: shell
|
126 |
+
|
127 |
+
export XLM_CHECKPOINT_PATH=/path/to/xlm/checkpoint
|
128 |
+
|
129 |
+
<<<<<<< HEAD
|
130 |
+
transformers-cli --model_type xlm \
|
131 |
+
=======
|
132 |
+
transformers-cli convert --model_type xlm \
|
133 |
+
>>>>>>> bfec203d4ed95255619e7e2f28c9040744a16232
|
134 |
+
--tf_checkpoint $XLM_CHECKPOINT_PATH \
|
135 |
+
--pytorch_dump_output $PYTORCH_DUMP_OUTPUT
|
136 |
+
[--config XML_CONFIG] \
|
137 |
+
[--finetuning_task_name XML_FINETUNED_TASK]
|
server/transformers/docs/source/examples.md
ADDED
@@ -0,0 +1 @@
|
|
|
|
|
1 |
+
../../examples/README.md
|
server/transformers/docs/source/glossary.rst
ADDED
@@ -0,0 +1,145 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
Glossary
|
2 |
+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
3 |
+
|
4 |
+
Every model is different yet bears similarities with the others. Therefore most models use the same inputs, which are
|
5 |
+
detailed here alongside usage examples.
|
6 |
+
|
7 |
+
Input IDs
|
8 |
+
--------------------------
|
9 |
+
|
10 |
+
The input ids are often the only required parameters to be passed to the model as input. *They are token indices,
|
11 |
+
numerical representations of tokens building the sequences that will be used as input by the model*.
|
12 |
+
|
13 |
+
Each tokenizer works differently but the underlying mechanism remains the same. Here's an example using the BERT
|
14 |
+
tokenizer, which is a `WordPiece <https://arxiv.org/pdf/1609.08144.pdf>`__ tokenizer:
|
15 |
+
|
16 |
+
::
|
17 |
+
|
18 |
+
from transformers import BertTokenizer
|
19 |
+
tokenizer = BertTokenizer.from_pretrained("bert-base-cased")
|
20 |
+
|
21 |
+
sequence = "A Titan RTX has 24GB of VRAM"
|
22 |
+
|
23 |
+
The tokenizer takes care of splitting the sequence into tokens available in the tokenizer vocabulary.
|
24 |
+
|
25 |
+
::
|
26 |
+
|
27 |
+
# Continuation of the previous script
|
28 |
+
tokenized_sequence = tokenizer.tokenize(sequence)
|
29 |
+
assert tokenized_sequence == ['A', 'Titan', 'R', '##T', '##X', 'has', '24', '##GB', 'of', 'V', '##RA', '##M']
|
30 |
+
|
31 |
+
These tokens can then be converted into IDs which are understandable by the model. Several methods are available for
|
32 |
+
this, the recommended being `encode` or `encode_plus`, which leverage the Rust implementation of
|
33 |
+
`huggingface/tokenizers <https://github.com/huggingface/tokenizers>`__ for peak performance.
|
34 |
+
|
35 |
+
::
|
36 |
+
|
37 |
+
# Continuation of the previous script
|
38 |
+
encoded_sequence = tokenizer.encode(sequence)
|
39 |
+
assert encoded_sequence == [101, 138, 18696, 155, 1942, 3190, 1144, 1572, 13745, 1104, 159, 9664, 2107, 102]
|
40 |
+
|
41 |
+
The `encode` and `encode_plus` methods automatically add "special tokens" which are special IDs the model uses.
|
42 |
+
|
43 |
+
Attention mask
|
44 |
+
--------------------------
|
45 |
+
|
46 |
+
The attention mask is an optional argument used when batching sequences together. This argument indicates to the
|
47 |
+
model which tokens should be attended to, and which should not.
|
48 |
+
|
49 |
+
For example, consider these two sequences:
|
50 |
+
|
51 |
+
::
|
52 |
+
|
53 |
+
from transformers import BertTokenizer
|
54 |
+
tokenizer = BertTokenizer.from_pretrained("bert-base-cased")
|
55 |
+
|
56 |
+
sequence_a = "This is a short sequence."
|
57 |
+
sequence_b = "This is a rather long sequence. It is at least longer than the sequence A."
|
58 |
+
|
59 |
+
encoded_sequence_a = tokenizer.encode(sequence_a)
|
60 |
+
assert len(encoded_sequence_a) == 8
|
61 |
+
|
62 |
+
encoded_sequence_b = tokenizer.encode(sequence_b)
|
63 |
+
assert len(encoded_sequence_b) == 19
|
64 |
+
|
65 |
+
These two sequences have different lengths and therefore can't be put together in a same tensor as-is. The first
|
66 |
+
sequence needs to be padded up to the length of the second one, or the second one needs to be truncated down to
|
67 |
+
the length of the first one.
|
68 |
+
|
69 |
+
In the first case, the list of IDs will be extended by the padding indices:
|
70 |
+
|
71 |
+
::
|
72 |
+
|
73 |
+
# Continuation of the previous script
|
74 |
+
padded_sequence_a = tokenizer.encode(sequence_a, max_length=19, pad_to_max_length=True)
|
75 |
+
|
76 |
+
assert padded_sequence_a == [101, 1188, 1110, 170, 1603, 4954, 119, 102, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
|
77 |
+
assert encoded_sequence_b == [101, 1188, 1110, 170, 1897, 1263, 4954, 119, 1135, 1110, 1120, 1655, 2039, 1190, 1103, 4954, 138, 119, 102]
|
78 |
+
|
79 |
+
These can then be converted into a tensor in PyTorch or TensorFlow. The attention mask is a binary tensor indicating
|
80 |
+
the position of the padded indices so that the model does not attend to them. For the
|
81 |
+
:class:`~transformers.BertTokenizer`, :obj:`1` indicate a value that should be attended to while :obj:`0` indicate
|
82 |
+
a padded value.
|
83 |
+
|
84 |
+
The method :func:`~transformers.PreTrainedTokenizer.encode_plus` may be used to obtain the attention mask directly:
|
85 |
+
|
86 |
+
::
|
87 |
+
|
88 |
+
# Continuation of the previous script
|
89 |
+
sequence_a_dict = tokenizer.encode_plus(sequence_a, max_length=19, pad_to_max_length=True)
|
90 |
+
|
91 |
+
assert sequence_a_dict['input_ids'] == [101, 1188, 1110, 170, 1603, 4954, 119, 102, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
|
92 |
+
assert sequence_a_dict['attention_mask'] == [1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
|
93 |
+
|
94 |
+
|
95 |
+
Token Type IDs
|
96 |
+
--------------------------
|
97 |
+
|
98 |
+
Some models' purpose is to do sequence classification or question answering. These require two different sequences to
|
99 |
+
be encoded in the same input IDs. They are usually separated by special tokens, such as the classifier and separator
|
100 |
+
tokens. For example, the BERT model builds its two sequence input as such:
|
101 |
+
|
102 |
+
::
|
103 |
+
|
104 |
+
from transformers import BertTokenizer
|
105 |
+
tokenizer = BertTokenizer.from_pretrained("bert-base-cased")
|
106 |
+
|
107 |
+
# [CLS] SEQ_A [SEP] SEQ_B [SEP]
|
108 |
+
|
109 |
+
sequence_a = "HuggingFace is based in NYC"
|
110 |
+
sequence_b = "Where is HuggingFace based?"
|
111 |
+
|
112 |
+
encoded_sequence = tokenizer.encode(sequence_a, sequence_b)
|
113 |
+
assert tokenizer.decode(encoded_sequence) == "[CLS] HuggingFace is based in NYC [SEP] Where is HuggingFace based? [SEP]"
|
114 |
+
|
115 |
+
This is enough for some models to understand where one sequence ends and where another begins. However, other models
|
116 |
+
such as BERT have an additional mechanism, which are the segment IDs. The Token Type IDs are a binary mask identifying
|
117 |
+
the different sequences in the model.
|
118 |
+
|
119 |
+
We can leverage :func:`~transformers.PreTrainedTokenizer.encode_plus` to output the Token Type IDs for us:
|
120 |
+
|
121 |
+
::
|
122 |
+
|
123 |
+
# Continuation of the previous script
|
124 |
+
encoded_dict = tokenizer.encode_plus(sequence_a, sequence_b)
|
125 |
+
|
126 |
+
assert encoded_dict['input_ids'] == [101, 20164, 10932, 2271, 7954, 1110, 1359, 1107, 17520, 102, 2777, 1110, 20164, 10932, 2271, 7954, 1359, 136, 102]
|
127 |
+
assert encoded_dict['token_type_ids'] == [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1]
|
128 |
+
|
129 |
+
The first sequence, the "context" used for the question, has all its tokens represented by :obj:`0`, whereas the
|
130 |
+
question has all its tokens represented by :obj:`1`. Some models, like :class:`~transformers.XLNetModel` use an
|
131 |
+
additional token represented by a :obj:`2`.
|
132 |
+
|
133 |
+
|
134 |
+
Position IDs
|
135 |
+
--------------------------
|
136 |
+
|
137 |
+
The position IDs are used by the model to identify which token is at which position. Contrary to RNNs that have the
|
138 |
+
position of each token embedded within them, transformers are unaware of the position of each token. The position
|
139 |
+
IDs are created for this purpose.
|
140 |
+
|
141 |
+
They are an optional parameter. If no position IDs are passed to the model, they are automatically created as absolute
|
142 |
+
positional embeddings.
|
143 |
+
|
144 |
+
Absolute positional embeddings are selected in the range ``[0, config.max_position_embeddings - 1]``. Some models
|
145 |
+
use other types of positional embeddings, such as sinusoidal position embeddings or relative position embeddings.
|
server/transformers/docs/source/imgs/transformers_logo_name.png
ADDED
server/transformers/docs/source/imgs/warmup_constant_schedule.png
ADDED
server/transformers/docs/source/imgs/warmup_cosine_hard_restarts_schedule.png
ADDED
server/transformers/docs/source/imgs/warmup_cosine_schedule.png
ADDED
server/transformers/docs/source/imgs/warmup_cosine_warm_restarts_schedule.png
ADDED
server/transformers/docs/source/imgs/warmup_linear_schedule.png
ADDED
server/transformers/docs/source/index.rst
ADDED
@@ -0,0 +1,102 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
Transformers
|
2 |
+
================================================================================================================================================
|
3 |
+
|
4 |
+
🤗 Transformers (formerly known as `pytorch-transformers` and `pytorch-pretrained-bert`) provides general-purpose architectures
|
5 |
+
(BERT, GPT-2, RoBERTa, XLM, DistilBert, XLNet...) for Natural Language Understanding (NLU) and Natural Language Generation
|
6 |
+
(NLG) with over 32+ pretrained models in 100+ languages and deep interoperability between TensorFlow 2.0 and PyTorch.
|
7 |
+
|
8 |
+
This is the documentation of our repository `transformers <https://github.com/huggingface/transformers>`__.
|
9 |
+
|
10 |
+
Features
|
11 |
+
---------------------------------------------------
|
12 |
+
|
13 |
+
- As easy to use as pytorch-transformers
|
14 |
+
- As powerful and concise as Keras
|
15 |
+
- High performance on NLU and NLG tasks
|
16 |
+
- Low barrier to entry for educators and practitioners
|
17 |
+
|
18 |
+
State-of-the-art NLP for everyone:
|
19 |
+
|
20 |
+
- Deep learning researchers
|
21 |
+
- Hands-on practitioners
|
22 |
+
- AI/ML/NLP teachers and educators
|
23 |
+
|
24 |
+
Lower compute costs, smaller carbon footprint:
|
25 |
+
|
26 |
+
- Researchers can share trained models instead of always retraining
|
27 |
+
- Practitioners can reduce compute time and production costs
|
28 |
+
- 8 architectures with over 30 pretrained models, some in more than 100 languages
|
29 |
+
|
30 |
+
Choose the right framework for every part of a model's lifetime:
|
31 |
+
|
32 |
+
- Train state-of-the-art models in 3 lines of code
|
33 |
+
- Deep interoperability between TensorFlow 2.0 and PyTorch models
|
34 |
+
- Move a single model between TF2.0/PyTorch frameworks at will
|
35 |
+
- Seamlessly pick the right framework for training, evaluation, production
|
36 |
+
|
37 |
+
Contents
|
38 |
+
---------------------------------
|
39 |
+
|
40 |
+
The library currently contains PyTorch and Tensorflow implementations, pre-trained model weights, usage scripts and conversion utilities for the following models:
|
41 |
+
|
42 |
+
1. `BERT <https://github.com/google-research/bert>`_ (from Google) released with the paper `BERT: Pre-training of Deep Bidirectional Transformers for Language Understanding <https://arxiv.org/abs/1810.04805>`_ by Jacob Devlin, Ming-Wei Chang, Kenton Lee and Kristina Toutanova.
|
43 |
+
2. `GPT <https://github.com/openai/finetune-transformer-lm>`_ (from OpenAI) released with the paper `Improving Language Understanding by Generative Pre-Training <https://blog.openai.com/language-unsupervised>`_ by Alec Radford, Karthik Narasimhan, Tim Salimans and Ilya Sutskever.
|
44 |
+
3. `GPT-2 <https://blog.openai.com/better-language-models>`_ (from OpenAI) released with the paper `Language Models are Unsupervised Multitask Learners <https://blog.openai.com/better-language-models>`_ by Alec Radford*, Jeffrey Wu*, Rewon Child, David Luan, Dario Amodei** and Ilya Sutskever**.
|
45 |
+
4. `Transformer-XL <https://github.com/kimiyoung/transformer-xl>`_ (from Google/CMU) released with the paper `Transformer-XL: Attentive Language Models Beyond a Fixed-Length Context <https://arxiv.org/abs/1901.02860>`_ by Zihang Dai*, Zhilin Yang*, Yiming Yang, Jaime Carbonell, Quoc V. Le, Ruslan Salakhutdinov.
|
46 |
+
5. `XLNet <https://github.com/zihangdai/xlnet>`_ (from Google/CMU) released with the paper `XLNet: Generalized Autoregressive Pretraining for Language Understanding <https://arxiv.org/abs/1906.08237>`_ by Zhilin Yang*, Zihang Dai*, Yiming Yang, Jaime Carbonell, Ruslan Salakhutdinov, Quoc V. Le.
|
47 |
+
6. `XLM <https://github.com/facebookresearch/XLM>`_ (from Facebook) released together with the paper `Cross-lingual Language Model Pretraining <https://arxiv.org/abs/1901.07291>`_ by Guillaume Lample and Alexis Conneau.
|
48 |
+
7. `RoBERTa <https://github.com/pytorch/fairseq/tree/master/examples/roberta>`_ (from Facebook), released together with the paper a `Robustly Optimized BERT Pretraining Approach <https://arxiv.org/abs/1907.11692>`_ by Yinhan Liu, Myle Ott, Naman Goyal, Jingfei Du, Mandar Joshi, Danqi Chen, Omer Levy, Mike Lewis, Luke Zettlemoyer, Veselin Stoyanov.
|
49 |
+
8. `DistilBERT <https://huggingface.co/transformers/model_doc/distilbert.html>`_ (from HuggingFace) released together with the paper `DistilBERT, a distilled version of BERT: smaller, faster, cheaper and lighter <https://arxiv.org/abs/1910.01108>`_ by Victor Sanh, Lysandre Debut and Thomas Wolf. The same method has been applied to compress GPT2 into `DistilGPT2 <https://github.com/huggingface/transformers/tree/master/examples/distillation>`_.
|
50 |
+
9. `CTRL <https://github.com/pytorch/fairseq/tree/master/examples/ctrl>`_ (from Salesforce), released together with the paper `CTRL: A Conditional Transformer Language Model for Controllable Generation <https://www.github.com/salesforce/ctrl>`_ by Nitish Shirish Keskar*, Bryan McCann*, Lav R. Varshney, Caiming Xiong and Richard Socher.
|
51 |
+
10. `CamemBERT <https://huggingface.co/transformers/model_doc/camembert.html>`_ (from FAIR, Inria, Sorbonne Université) released together with the paper `CamemBERT: a Tasty French Language Model <https://arxiv.org/abs/1911.03894>`_ by Louis Martin, Benjamin Muller, Pedro Javier Ortiz Suarez, Yoann Dupont, Laurent Romary, Eric Villemonte de la Clergerie, Djame Seddah, and Benoît Sagot.
|
52 |
+
11. `ALBERT <https://github.com/google-research/ALBERT>`_ (from Google Research), released together with the paper a `ALBERT: A Lite BERT for Self-supervised Learning of Language Representations <https://arxiv.org/abs/1909.11942>`_ by Zhenzhong Lan, Mingda Chen, Sebastian Goodman, Kevin Gimpel, Piyush Sharma, Radu Soricut.
|
53 |
+
12. `XLM-RoBERTa <https://github.com/pytorch/fairseq/tree/master/examples/xlmr>`_ (from Facebook AI), released together with the paper `Unsupervised Cross-lingual Representation Learning at Scale <https://arxiv.org/abs/1911.02116>`_ by Alexis Conneau*, Kartikay Khandelwal*, Naman Goyal, Vishrav Chaudhary, Guillaume Wenzek, Francisco Guzmán, Edouard Grave, Myle Ott, Luke Zettlemoyer and Veselin Stoyanov.
|
54 |
+
13. `FlauBERT <https://github.com/getalp/Flaubert>`_ (from CNRS) released with the paper `FlauBERT: Unsupervised Language Model Pre-training for French <https://arxiv.org/abs/1912.05372>`_ by Hang Le, Loïc Vial, Jibril Frej, Vincent Segonne, Maximin Coavoux, Benjamin Lecouteux, Alexandre Allauzen, Benoît Crabbé, Laurent Besacier, Didier Schwab.
|
55 |
+
|
56 |
+
.. toctree::
|
57 |
+
:maxdepth: 2
|
58 |
+
:caption: Notes
|
59 |
+
|
60 |
+
installation
|
61 |
+
quickstart
|
62 |
+
glossary
|
63 |
+
pretrained_models
|
64 |
+
model_sharing
|
65 |
+
examples
|
66 |
+
notebooks
|
67 |
+
serialization
|
68 |
+
converting_tensorflow_models
|
69 |
+
migration
|
70 |
+
bertology
|
71 |
+
torchscript
|
72 |
+
multilingual
|
73 |
+
benchmarks
|
74 |
+
|
75 |
+
.. toctree::
|
76 |
+
:maxdepth: 2
|
77 |
+
:caption: Main classes
|
78 |
+
|
79 |
+
main_classes/configuration
|
80 |
+
main_classes/model
|
81 |
+
main_classes/tokenizer
|
82 |
+
main_classes/optimizer_schedules
|
83 |
+
main_classes/processors
|
84 |
+
|
85 |
+
.. toctree::
|
86 |
+
:maxdepth: 2
|
87 |
+
:caption: Package Reference
|
88 |
+
|
89 |
+
model_doc/auto
|
90 |
+
model_doc/bert
|
91 |
+
model_doc/gpt
|
92 |
+
model_doc/transformerxl
|
93 |
+
model_doc/gpt2
|
94 |
+
model_doc/xlm
|
95 |
+
model_doc/xlnet
|
96 |
+
model_doc/roberta
|
97 |
+
model_doc/distilbert
|
98 |
+
model_doc/ctrl
|
99 |
+
model_doc/camembert
|
100 |
+
model_doc/albert
|
101 |
+
model_doc/xlmroberta
|
102 |
+
model_doc/flaubert
|
server/transformers/docs/source/installation.md
ADDED
@@ -0,0 +1,51 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# Installation
|
2 |
+
|
3 |
+
Transformers is tested on Python 3.5+ and PyTorch 1.1.0
|
4 |
+
|
5 |
+
## With pip
|
6 |
+
|
7 |
+
PyTorch Transformers can be installed using pip as follows:
|
8 |
+
|
9 |
+
``` bash
|
10 |
+
pip install transformers
|
11 |
+
```
|
12 |
+
|
13 |
+
## From source
|
14 |
+
|
15 |
+
To install from source, clone the repository and install with:
|
16 |
+
|
17 |
+
``` bash
|
18 |
+
git clone https://github.com/huggingface/transformers.git
|
19 |
+
cd transformers
|
20 |
+
pip install .
|
21 |
+
```
|
22 |
+
|
23 |
+
## Tests
|
24 |
+
|
25 |
+
An extensive test suite is included to test the library behavior and several examples. Library tests can be found in the [tests folder](https://github.com/huggingface/transformers/tree/master/tests) and examples tests in the [examples folder](https://github.com/huggingface/transformers/tree/master/examples).
|
26 |
+
|
27 |
+
Refer to the [contributing guide](https://github.com/huggingface/transformers/blob/master/CONTRIBUTING.md#tests) for details about running tests.
|
28 |
+
|
29 |
+
## OpenAI GPT original tokenization workflow
|
30 |
+
|
31 |
+
If you want to reproduce the original tokenization process of the `OpenAI GPT` paper, you will need to install `ftfy` and `SpaCy`:
|
32 |
+
|
33 |
+
``` bash
|
34 |
+
pip install spacy ftfy==4.4.3
|
35 |
+
python -m spacy download en
|
36 |
+
```
|
37 |
+
|
38 |
+
If you don't install `ftfy` and `SpaCy`, the `OpenAI GPT` tokenizer will default to tokenize using BERT's `BasicTokenizer` followed by Byte-Pair Encoding (which should be fine for most usage, don't worry).
|
39 |
+
|
40 |
+
## Note on model downloads (Continuous Integration or large-scale deployments)
|
41 |
+
|
42 |
+
If you expect to be downloading large volumes of models (more than 1,000) from our hosted bucket (for instance through your CI setup, or a large-scale production deployment), please cache the model files on your end. It will be way faster, and cheaper. Feel free to contact us privately if you need any help.
|
43 |
+
|
44 |
+
## Do you want to run a Transformer model on a mobile device?
|
45 |
+
|
46 |
+
You should check out our [swift-coreml-transformers](https://github.com/huggingface/swift-coreml-transformers) repo.
|
47 |
+
|
48 |
+
It contains a set of tools to convert PyTorch or TensorFlow 2.0 trained Transformer models (currently contains `GPT-2`, `DistilGPT-2`, `BERT`, and `DistilBERT`) to CoreML models that run on iOS devices.
|
49 |
+
|
50 |
+
At some point in the future, you'll be able to seamlessly move from pre-training or fine-tuning models in PyTorch to productizing them in CoreML,
|
51 |
+
or prototype a model or an app in CoreML then research its hyperparameters or architecture from PyTorch. Super exciting!
|
server/transformers/docs/source/main_classes/configuration.rst
ADDED
@@ -0,0 +1,10 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
Configuration
|
2 |
+
----------------------------------------------------
|
3 |
+
|
4 |
+
The base class ``PretrainedConfig`` implements the common methods for loading/saving a configuration either from a local file or directory, or from a pretrained model configuration provided by the library (downloaded from HuggingFace's AWS S3 repository).
|
5 |
+
|
6 |
+
``PretrainedConfig``
|
7 |
+
~~~~~~~~~~~~~~~~~~~~~
|
8 |
+
|
9 |
+
.. autoclass:: transformers.PretrainedConfig
|
10 |
+
:members:
|
server/transformers/docs/source/main_classes/model.rst
ADDED
@@ -0,0 +1,21 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
Models
|
2 |
+
----------------------------------------------------
|
3 |
+
|
4 |
+
The base class ``PreTrainedModel`` implements the common methods for loading/saving a model either from a local file or directory, or from a pretrained model configuration provided by the library (downloaded from HuggingFace's AWS S3 repository).
|
5 |
+
|
6 |
+
``PreTrainedModel`` also implements a few methods which are common among all the models to:
|
7 |
+
|
8 |
+
- resize the input token embeddings when new tokens are added to the vocabulary
|
9 |
+
- prune the attention heads of the model.
|
10 |
+
|
11 |
+
``PreTrainedModel``
|
12 |
+
~~~~~~~~~~~~~~~~~~~~~
|
13 |
+
|
14 |
+
.. autoclass:: transformers.PreTrainedModel
|
15 |
+
:members:
|
16 |
+
|
17 |
+
``TFPreTrainedModel``
|
18 |
+
~~~~~~~~~~~~~~~~~~~~~
|
19 |
+
|
20 |
+
.. autoclass:: transformers.TFPreTrainedModel
|
21 |
+
:members:
|
server/transformers/docs/source/main_classes/optimizer_schedules.rst
ADDED
@@ -0,0 +1,72 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
Optimizer
|
2 |
+
----------------------------------------------------
|
3 |
+
|
4 |
+
The ``.optimization`` module provides:
|
5 |
+
|
6 |
+
- an optimizer with weight decay fixed that can be used to fine-tuned models, and
|
7 |
+
- several schedules in the form of schedule objects that inherit from ``_LRSchedule``:
|
8 |
+
- a gradient accumulation class to accumulate the gradients of multiple batches
|
9 |
+
|
10 |
+
``AdamW``
|
11 |
+
~~~~~~~~~~~~~~~~
|
12 |
+
|
13 |
+
.. autoclass:: transformers.AdamW
|
14 |
+
:members:
|
15 |
+
|
16 |
+
``AdamWeightDecay``
|
17 |
+
~~~~~~~~~~~~~~~~~~~
|
18 |
+
|
19 |
+
.. autoclass:: transformers.AdamWeightDecay
|
20 |
+
:members:
|
21 |
+
|
22 |
+
.. autofunction:: transformers.create_optimizer
|
23 |
+
|
24 |
+
Schedules
|
25 |
+
----------------------------------------------------
|
26 |
+
|
27 |
+
Learning Rate Schedules
|
28 |
+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
29 |
+
.. autofunction:: transformers.get_constant_schedule
|
30 |
+
|
31 |
+
|
32 |
+
.. autofunction:: transformers.get_constant_schedule_with_warmup
|
33 |
+
|
34 |
+
.. image:: /imgs/warmup_constant_schedule.png
|
35 |
+
:target: /imgs/warmup_constant_schedule.png
|
36 |
+
:alt:
|
37 |
+
|
38 |
+
|
39 |
+
.. autofunction:: transformers.get_cosine_schedule_with_warmup
|
40 |
+
|
41 |
+
.. image:: /imgs/warmup_cosine_schedule.png
|
42 |
+
:target: /imgs/warmup_cosine_schedule.png
|
43 |
+
:alt:
|
44 |
+
|
45 |
+
|
46 |
+
.. autofunction:: transformers.get_cosine_with_hard_restarts_schedule_with_warmup
|
47 |
+
|
48 |
+
.. image:: /imgs/warmup_cosine_hard_restarts_schedule.png
|
49 |
+
:target: /imgs/warmup_cosine_hard_restarts_schedule.png
|
50 |
+
:alt:
|
51 |
+
|
52 |
+
|
53 |
+
|
54 |
+
.. autofunction:: transformers.get_linear_schedule_with_warmup
|
55 |
+
|
56 |
+
.. image:: /imgs/warmup_linear_schedule.png
|
57 |
+
:target: /imgs/warmup_linear_schedule.png
|
58 |
+
:alt:
|
59 |
+
|
60 |
+
``Warmup``
|
61 |
+
~~~~~~~~~~~~~~~~
|
62 |
+
|
63 |
+
.. autoclass:: transformers.WarmUp
|
64 |
+
:members:
|
65 |
+
|
66 |
+
Gradient Strategies
|
67 |
+
----------------------------------------------------
|
68 |
+
|
69 |
+
``GradientAccumulator``
|
70 |
+
~~~~~~~~~~~~~~~~~~~~~~~
|
71 |
+
|
72 |
+
.. autoclass:: transformers.GradientAccumulator
|
server/transformers/docs/source/main_classes/processors.rst
ADDED
@@ -0,0 +1,153 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
Processors
|
2 |
+
----------------------------------------------------
|
3 |
+
|
4 |
+
This library includes processors for several traditional tasks. These processors can be used to process a dataset into
|
5 |
+
examples that can be fed to a model.
|
6 |
+
|
7 |
+
Processors
|
8 |
+
~~~~~~~~~~~~~~~~~~~~~
|
9 |
+
|
10 |
+
All processors follow the same architecture which is that of the
|
11 |
+
:class:`~transformers.data.processors.utils.DataProcessor`. The processor returns a list
|
12 |
+
of :class:`~transformers.data.processors.utils.InputExample`. These
|
13 |
+
:class:`~transformers.data.processors.utils.InputExample` can be converted to
|
14 |
+
:class:`~transformers.data.processors.utils.InputFeatures` in order to be fed to the model.
|
15 |
+
|
16 |
+
.. autoclass:: transformers.data.processors.utils.DataProcessor
|
17 |
+
:members:
|
18 |
+
|
19 |
+
|
20 |
+
.. autoclass:: transformers.data.processors.utils.InputExample
|
21 |
+
:members:
|
22 |
+
|
23 |
+
|
24 |
+
.. autoclass:: transformers.data.processors.utils.InputFeatures
|
25 |
+
:members:
|
26 |
+
|
27 |
+
|
28 |
+
GLUE
|
29 |
+
~~~~~~~~~~~~~~~~~~~~~
|
30 |
+
|
31 |
+
`General Language Understanding Evaluation (GLUE) <https://gluebenchmark.com/>`__ is a benchmark that evaluates
|
32 |
+
the performance of models across a diverse set of existing NLU tasks. It was released together with the paper
|
33 |
+
`GLUE: A multi-task benchmark and analysis platform for natural language understanding <https://openreview.net/pdf?id=rJ4km2R5t7>`__
|
34 |
+
|
35 |
+
This library hosts a total of 10 processors for the following tasks: MRPC, MNLI, MNLI (mismatched),
|
36 |
+
CoLA, SST2, STSB, QQP, QNLI, RTE and WNLI.
|
37 |
+
|
38 |
+
Those processors are:
|
39 |
+
- :class:`~transformers.data.processors.utils.MrpcProcessor`
|
40 |
+
- :class:`~transformers.data.processors.utils.MnliProcessor`
|
41 |
+
- :class:`~transformers.data.processors.utils.MnliMismatchedProcessor`
|
42 |
+
- :class:`~transformers.data.processors.utils.Sst2Processor`
|
43 |
+
- :class:`~transformers.data.processors.utils.StsbProcessor`
|
44 |
+
- :class:`~transformers.data.processors.utils.QqpProcessor`
|
45 |
+
- :class:`~transformers.data.processors.utils.QnliProcessor`
|
46 |
+
- :class:`~transformers.data.processors.utils.RteProcessor`
|
47 |
+
- :class:`~transformers.data.processors.utils.WnliProcessor`
|
48 |
+
|
49 |
+
Additionally, the following method can be used to load values from a data file and convert them to a list of
|
50 |
+
:class:`~transformers.data.processors.utils.InputExample`.
|
51 |
+
|
52 |
+
.. automethod:: transformers.data.processors.glue.glue_convert_examples_to_features
|
53 |
+
|
54 |
+
Example usage
|
55 |
+
^^^^^^^^^^^^^^^^^^^^^^^^^
|
56 |
+
|
57 |
+
An example using these processors is given in the `run_glue.py <https://github.com/huggingface/pytorch-transformers/blob/master/examples/run_glue.py>`__ script.
|
58 |
+
|
59 |
+
|
60 |
+
XNLI
|
61 |
+
~~~~~~~~~~~~~~~~~~~~~
|
62 |
+
|
63 |
+
`The Cross-Lingual NLI Corpus (XNLI) <https://www.nyu.edu/projects/bowman/xnli/>`__ is a benchmark that evaluates
|
64 |
+
the quality of cross-lingual text representations.
|
65 |
+
XNLI is crowd-sourced dataset based on `MultiNLI <http://www.nyu.edu/projects/bowman/multinli/>`: pairs of text are labeled with textual entailment
|
66 |
+
annotations for 15 different languages (including both high-ressource language such as English and low-ressource languages such as Swahili).
|
67 |
+
|
68 |
+
It was released together with the paper
|
69 |
+
`XNLI: Evaluating Cross-lingual Sentence Representations <https://arxiv.org/abs/1809.05053>`__
|
70 |
+
|
71 |
+
This library hosts the processor to load the XNLI data:
|
72 |
+
- :class:`~transformers.data.processors.utils.XnliProcessor`
|
73 |
+
|
74 |
+
Please note that since the gold labels are available on the test set, evaluation is performed on the test set.
|
75 |
+
|
76 |
+
An example using these processors is given in the
|
77 |
+
`run_xnli.py <https://github.com/huggingface/pytorch-transformers/blob/master/examples/run_xnli.py>`__ script.
|
78 |
+
|
79 |
+
|
80 |
+
SQuAD
|
81 |
+
~~~~~~~~~~~~~~~~~~~~~
|
82 |
+
|
83 |
+
`The Stanford Question Answering Dataset (SQuAD) <https://rajpurkar.github.io/SQuAD-explorer//>`__ is a benchmark that evaluates
|
84 |
+
the performance of models on question answering. Two versions are available, v1.1 and v2.0. The first version (v1.1) was released together with the paper
|
85 |
+
`SQuAD: 100,000+ Questions for Machine Comprehension of Text <https://arxiv.org/abs/1606.05250>`__. The second version (v2.0) was released alongside
|
86 |
+
the paper `Know What You Don't Know: Unanswerable Questions for SQuAD <https://arxiv.org/abs/1806.03822>`__.
|
87 |
+
|
88 |
+
This library hosts a processor for each of the two versions:
|
89 |
+
|
90 |
+
Processors
|
91 |
+
^^^^^^^^^^^^^^^^^^^^^^^^^
|
92 |
+
|
93 |
+
Those processors are:
|
94 |
+
- :class:`~transformers.data.processors.utils.SquadV1Processor`
|
95 |
+
- :class:`~transformers.data.processors.utils.SquadV2Processor`
|
96 |
+
|
97 |
+
They both inherit from the abstract class :class:`~transformers.data.processors.utils.SquadProcessor`
|
98 |
+
|
99 |
+
.. autoclass:: transformers.data.processors.squad.SquadProcessor
|
100 |
+
:members:
|
101 |
+
|
102 |
+
Additionally, the following method can be used to convert SQuAD examples into :class:`~transformers.data.processors.utils.SquadFeatures`
|
103 |
+
that can be used as model inputs.
|
104 |
+
|
105 |
+
.. automethod:: transformers.data.processors.squad.squad_convert_examples_to_features
|
106 |
+
|
107 |
+
These processors as well as the aforementionned method can be used with files containing the data as well as with the `tensorflow_datasets` package.
|
108 |
+
Examples are given below.
|
109 |
+
|
110 |
+
|
111 |
+
Example usage
|
112 |
+
^^^^^^^^^^^^^^^^^^^^^^^^^
|
113 |
+
Here is an example using the processors as well as the conversion method using data files:
|
114 |
+
|
115 |
+
Example::
|
116 |
+
|
117 |
+
# Loading a V2 processor
|
118 |
+
processor = SquadV2Processor()
|
119 |
+
examples = processor.get_dev_examples(squad_v2_data_dir)
|
120 |
+
|
121 |
+
# Loading a V1 processor
|
122 |
+
processor = SquadV1Processor()
|
123 |
+
examples = processor.get_dev_examples(squad_v1_data_dir)
|
124 |
+
|
125 |
+
features = squad_convert_examples_to_features(
|
126 |
+
examples=examples,
|
127 |
+
tokenizer=tokenizer,
|
128 |
+
max_seq_length=max_seq_length,
|
129 |
+
doc_stride=args.doc_stride,
|
130 |
+
max_query_length=max_query_length,
|
131 |
+
is_training=not evaluate,
|
132 |
+
)
|
133 |
+
|
134 |
+
Using `tensorflow_datasets` is as easy as using a data file:
|
135 |
+
|
136 |
+
Example::
|
137 |
+
|
138 |
+
# tensorflow_datasets only handle Squad V1.
|
139 |
+
tfds_examples = tfds.load("squad")
|
140 |
+
examples = SquadV1Processor().get_examples_from_dataset(tfds_examples, evaluate=evaluate)
|
141 |
+
|
142 |
+
features = squad_convert_examples_to_features(
|
143 |
+
examples=examples,
|
144 |
+
tokenizer=tokenizer,
|
145 |
+
max_seq_length=max_seq_length,
|
146 |
+
doc_stride=args.doc_stride,
|
147 |
+
max_query_length=max_query_length,
|
148 |
+
is_training=not evaluate,
|
149 |
+
)
|
150 |
+
|
151 |
+
|
152 |
+
Another example using these processors is given in the
|
153 |
+
`run_squad.py <https://github.com/huggingface/transformers/blob/master/examples/run_squad.py>`__ script.
|
server/transformers/docs/source/main_classes/tokenizer.rst
ADDED
@@ -0,0 +1,16 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
Tokenizer
|
2 |
+
----------------------------------------------------
|
3 |
+
|
4 |
+
The base class ``PreTrainedTokenizer`` implements the common methods for loading/saving a tokenizer either from a local file or directory, or from a pretrained tokenizer provided by the library (downloaded from HuggingFace's AWS S3 repository).
|
5 |
+
|
6 |
+
``PreTrainedTokenizer`` is the main entry point into tokenizers as it also implements the main methods for using all the tokenizers:
|
7 |
+
|
8 |
+
- tokenizing, converting tokens to ids and back and encoding/decoding,
|
9 |
+
- adding new tokens to the vocabulary in a way that is independant of the underlying structure (BPE, SentencePiece...),
|
10 |
+
- managing special tokens (adding them, assigning them to roles, making sure they are not split during tokenization)
|
11 |
+
|
12 |
+
``PreTrainedTokenizer``
|
13 |
+
~~~~~~~~~~~~~~~~~~~~~~~~
|
14 |
+
|
15 |
+
.. autoclass:: transformers.PreTrainedTokenizer
|
16 |
+
:members:
|
server/transformers/docs/source/migration.md
ADDED
@@ -0,0 +1,109 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# Migrating from pytorch-pretrained-bert
|
2 |
+
|
3 |
+
|
4 |
+
Here is a quick summary of what you should take care of when migrating from `pytorch-pretrained-bert` to `transformers`
|
5 |
+
|
6 |
+
### Models always output `tuples`
|
7 |
+
|
8 |
+
The main breaking change when migrating from `pytorch-pretrained-bert` to `transformers` is that the models forward method always outputs a `tuple` with various elements depending on the model and the configuration parameters.
|
9 |
+
|
10 |
+
The exact content of the tuples for each model are detailled in the models' docstrings and the [documentation](https://huggingface.co/transformers/).
|
11 |
+
|
12 |
+
In pretty much every case, you will be fine by taking the first element of the output as the output you previously used in `pytorch-pretrained-bert`.
|
13 |
+
|
14 |
+
Here is a `pytorch-pretrained-bert` to `transformers` conversion example for a `BertForSequenceClassification` classification model:
|
15 |
+
|
16 |
+
```python
|
17 |
+
# Let's load our model
|
18 |
+
model = BertForSequenceClassification.from_pretrained('bert-base-uncased')
|
19 |
+
|
20 |
+
# If you used to have this line in pytorch-pretrained-bert:
|
21 |
+
loss = model(input_ids, labels=labels)
|
22 |
+
|
23 |
+
# Now just use this line in transformers to extract the loss from the output tuple:
|
24 |
+
outputs = model(input_ids, labels=labels)
|
25 |
+
loss = outputs[0]
|
26 |
+
|
27 |
+
# In transformers you can also have access to the logits:
|
28 |
+
loss, logits = outputs[:2]
|
29 |
+
|
30 |
+
# And even the attention weigths if you configure the model to output them (and other outputs too, see the docstrings and documentation)
|
31 |
+
model = BertForSequenceClassification.from_pretrained('bert-base-uncased', output_attentions=True)
|
32 |
+
outputs = model(input_ids, labels=labels)
|
33 |
+
loss, logits, attentions = outputs
|
34 |
+
```
|
35 |
+
|
36 |
+
### Serialization
|
37 |
+
|
38 |
+
Breaking change in the `from_pretrained()`method:
|
39 |
+
|
40 |
+
1. Models are now set in evaluation mode by default when instantiated with the `from_pretrained()` method. To train them don't forget to set them back in training mode (`model.train()`) to activate the dropout modules.
|
41 |
+
|
42 |
+
2. The additional `*inputs` and `**kwargs` arguments supplied to the `from_pretrained()` method used to be directly passed to the underlying model's class `__init__()` method. They are now used to update the model configuration attribute first which can break derived model classes build based on the previous `BertForSequenceClassification` examples. More precisely, the positional arguments `*inputs` provided to `from_pretrained()` are directly forwarded the model `__init__()` method while the keyword arguments `**kwargs` (i) which match configuration class attributes are used to update said attributes (ii) which don't match any configuration class attributes are forwarded to the model `__init__()` method.
|
43 |
+
|
44 |
+
Also, while not a breaking change, the serialization methods have been standardized and you probably should switch to the new method `save_pretrained(save_directory)` if you were using any other serialization method before.
|
45 |
+
|
46 |
+
Here is an example:
|
47 |
+
|
48 |
+
```python
|
49 |
+
### Let's load a model and tokenizer
|
50 |
+
model = BertForSequenceClassification.from_pretrained('bert-base-uncased')
|
51 |
+
tokenizer = BertTokenizer.from_pretrained('bert-base-uncased')
|
52 |
+
|
53 |
+
### Do some stuff to our model and tokenizer
|
54 |
+
# Ex: add new tokens to the vocabulary and embeddings of our model
|
55 |
+
tokenizer.add_tokens(['[SPECIAL_TOKEN_1]', '[SPECIAL_TOKEN_2]'])
|
56 |
+
model.resize_token_embeddings(len(tokenizer))
|
57 |
+
# Train our model
|
58 |
+
train(model)
|
59 |
+
|
60 |
+
### Now let's save our model and tokenizer to a directory
|
61 |
+
model.save_pretrained('./my_saved_model_directory/')
|
62 |
+
tokenizer.save_pretrained('./my_saved_model_directory/')
|
63 |
+
|
64 |
+
### Reload the model and the tokenizer
|
65 |
+
model = BertForSequenceClassification.from_pretrained('./my_saved_model_directory/')
|
66 |
+
tokenizer = BertTokenizer.from_pretrained('./my_saved_model_directory/')
|
67 |
+
```
|
68 |
+
|
69 |
+
### Optimizers: BertAdam & OpenAIAdam are now AdamW, schedules are standard PyTorch schedules
|
70 |
+
|
71 |
+
The two optimizers previously included, `BertAdam` and `OpenAIAdam`, have been replaced by a single `AdamW` optimizer which has a few differences:
|
72 |
+
|
73 |
+
- it only implements weights decay correction,
|
74 |
+
- schedules are now externals (see below),
|
75 |
+
- gradient clipping is now also external (see below).
|
76 |
+
|
77 |
+
The new optimizer `AdamW` matches PyTorch `Adam` optimizer API and let you use standard PyTorch or apex methods for the schedule and clipping.
|
78 |
+
|
79 |
+
The schedules are now standard [PyTorch learning rate schedulers](https://pytorch.org/docs/stable/optim.html#how-to-adjust-learning-rate) and not part of the optimizer anymore.
|
80 |
+
|
81 |
+
Here is a conversion examples from `BertAdam` with a linear warmup and decay schedule to `AdamW` and the same schedule:
|
82 |
+
|
83 |
+
```python
|
84 |
+
# Parameters:
|
85 |
+
lr = 1e-3
|
86 |
+
max_grad_norm = 1.0
|
87 |
+
num_training_steps = 1000
|
88 |
+
num_warmup_steps = 100
|
89 |
+
warmup_proportion = float(num_warmup_steps) / float(num_training_steps) # 0.1
|
90 |
+
|
91 |
+
### Previously BertAdam optimizer was instantiated like this:
|
92 |
+
optimizer = BertAdam(model.parameters(), lr=lr, schedule='warmup_linear', warmup=warmup_proportion, num_training_steps=num_training_steps)
|
93 |
+
### and used like this:
|
94 |
+
for batch in train_data:
|
95 |
+
loss = model(batch)
|
96 |
+
loss.backward()
|
97 |
+
optimizer.step()
|
98 |
+
|
99 |
+
### In Transformers, optimizer and schedules are splitted and instantiated like this:
|
100 |
+
optimizer = AdamW(model.parameters(), lr=lr, correct_bias=False) # To reproduce BertAdam specific behavior set correct_bias=False
|
101 |
+
scheduler = get_linear_schedule_with_warmup(optimizer, num_warmup_steps=num_warmup_steps, num_training_steps=num_training_steps) # PyTorch scheduler
|
102 |
+
### and used like this:
|
103 |
+
for batch in train_data:
|
104 |
+
loss = model(batch)
|
105 |
+
loss.backward()
|
106 |
+
torch.nn.utils.clip_grad_norm_(model.parameters(), max_grad_norm) # Gradient clipping is not in AdamW anymore (so you can use amp without issue)
|
107 |
+
optimizer.step()
|
108 |
+
scheduler.step()
|
109 |
+
```
|
server/transformers/docs/source/model_doc/albert.rst
ADDED
@@ -0,0 +1,93 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
ALBERT
|
2 |
+
----------------------------------------------------
|
3 |
+
|
4 |
+
Overview
|
5 |
+
~~~~~~~~~~~~~~~~~~~~~
|
6 |
+
|
7 |
+
The ALBERT model was proposed in `ALBERT: A Lite BERT for Self-supervised Learning of Language Representations <https://arxiv.org/abs/1909.11942>`_
|
8 |
+
by Zhenzhong Lan, Mingda Chen, Sebastian Goodman, Kevin Gimpel, Piyush Sharma, Radu Soricut. It presents
|
9 |
+
two parameter-reduction techniques to lower memory consumption and increase the trainig speed of BERT:
|
10 |
+
|
11 |
+
- Splitting the embedding matrix into two smaller matrices
|
12 |
+
- Using repeating layers split among groups
|
13 |
+
|
14 |
+
The abstract from the paper is the following:
|
15 |
+
|
16 |
+
*Increasing model size when pretraining natural language representations often results in improved performance on
|
17 |
+
downstream tasks. However, at some point further model increases become harder due to GPU/TPU memory limitations,
|
18 |
+
longer training times, and unexpected model degradation. To address these problems, we present two parameter-reduction
|
19 |
+
techniques to lower memory consumption and increase the training speed of BERT. Comprehensive empirical evidence shows
|
20 |
+
that our proposed methods lead to models that scale much better compared to the original BERT. We also use a
|
21 |
+
self-supervised loss that focuses on modeling inter-sentence coherence, and show it consistently helps downstream
|
22 |
+
tasks with multi-sentence inputs. As a result, our best model establishes new state-of-the-art results on the GLUE,
|
23 |
+
RACE, and SQuAD benchmarks while having fewer parameters compared to BERT-large.*
|
24 |
+
|
25 |
+
Tips:
|
26 |
+
|
27 |
+
- ALBERT is a model with absolute position embeddings so it's usually advised to pad the inputs on
|
28 |
+
the right rather than the left.
|
29 |
+
- ALBERT uses repeating layers which results in a small memory footprint, however the computational cost remains
|
30 |
+
similar to a BERT-like architecture with the same number of hidden layers as it has to iterate through the same
|
31 |
+
number of (repeating) layers.
|
32 |
+
|
33 |
+
AlbertConfig
|
34 |
+
~~~~~~~~~~~~~~~~~~~~~
|
35 |
+
|
36 |
+
.. autoclass:: transformers.AlbertConfig
|
37 |
+
:members:
|
38 |
+
|
39 |
+
|
40 |
+
AlbertTokenizer
|
41 |
+
~~~~~~~~~~~~~~~~~~~~~
|
42 |
+
|
43 |
+
.. autoclass:: transformers.AlbertTokenizer
|
44 |
+
:members:
|
45 |
+
|
46 |
+
|
47 |
+
AlbertModel
|
48 |
+
~~~~~~~~~~~~~~~~~~~~
|
49 |
+
|
50 |
+
.. autoclass:: transformers.AlbertModel
|
51 |
+
:members:
|
52 |
+
|
53 |
+
|
54 |
+
AlbertForMaskedLM
|
55 |
+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
56 |
+
|
57 |
+
.. autoclass:: transformers.AlbertForMaskedLM
|
58 |
+
:members:
|
59 |
+
|
60 |
+
|
61 |
+
AlbertForSequenceClassification
|
62 |
+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
63 |
+
|
64 |
+
.. autoclass:: transformers.AlbertForSequenceClassification
|
65 |
+
:members:
|
66 |
+
|
67 |
+
|
68 |
+
AlbertForQuestionAnswering
|
69 |
+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
70 |
+
|
71 |
+
.. autoclass:: transformers.AlbertForQuestionAnswering
|
72 |
+
:members:
|
73 |
+
|
74 |
+
|
75 |
+
TFAlbertModel
|
76 |
+
~~~~~~~~~~~~~~~~~~~~
|
77 |
+
|
78 |
+
.. autoclass:: transformers.TFAlbertModel
|
79 |
+
:members:
|
80 |
+
|
81 |
+
|
82 |
+
TFAlbertForMaskedLM
|
83 |
+
~~~~~~~~~~~~~~~~~~~~~~~~~~
|
84 |
+
|
85 |
+
.. autoclass:: transformers.TFAlbertForMaskedLM
|
86 |
+
:members:
|
87 |
+
|
88 |
+
|
89 |
+
TFAlbertForSequenceClassification
|
90 |
+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
91 |
+
|
92 |
+
.. autoclass:: transformers.TFAlbertForSequenceClassification
|
93 |
+
:members:
|