"...lm-evaluation-harness.git" did not exist on "3ddfa4a632d1ac1e091dcf6cffe46930c91b05d2"
multilingual.rst 6.33 KB
Newer Older
Sylvain Gugger's avatar
Sylvain Gugger committed
1
2
3
4
5
6
7
8
9
10
11
12
.. 
    Copyright 2020 The HuggingFace Team. All rights reserved.

    Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
    the License. You may obtain a copy of the License at

        http://www.apache.org/licenses/LICENSE-2.0

    Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
    an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
    specific language governing permissions and limitations under the License.

LysandreJik's avatar
LysandreJik committed
13
Multi-lingual models
Sylvain Gugger's avatar
Sylvain Gugger committed
14
=======================================================================================================================
LysandreJik's avatar
LysandreJik committed
15

Sylvain Gugger's avatar
Sylvain Gugger committed
16
17
18
Most of the models available in this library are mono-lingual models (English, Chinese and German). A few multi-lingual
models are available and have a different mechanisms than mono-lingual models. This page details the usage of these
models.
LysandreJik's avatar
LysandreJik committed
19
20

XLM
Sylvain Gugger's avatar
Sylvain Gugger committed
21
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
LysandreJik's avatar
LysandreJik committed
22
23
24
25
26

XLM has a total of 10 different checkpoints, only one of which is mono-lingual. The 9 remaining model checkpoints can
be split in two categories: the checkpoints that make use of language embeddings, and those that don't

XLM & Language Embeddings
Sylvain Gugger's avatar
Sylvain Gugger committed
27
-----------------------------------------------------------------------------------------------------------------------
LysandreJik's avatar
LysandreJik committed
28
29
30
31
32
33
34
35
36
37
38
39
40

This section concerns the following checkpoints:

- ``xlm-mlm-ende-1024`` (Masked language modeling, English-German)
- ``xlm-mlm-enfr-1024`` (Masked language modeling, English-French)
- ``xlm-mlm-enro-1024`` (Masked language modeling, English-Romanian)
- ``xlm-mlm-xnli15-1024`` (Masked language modeling, XNLI languages)
- ``xlm-mlm-tlm-xnli15-1024`` (Masked language modeling + Translation, XNLI languages)
- ``xlm-clm-enfr-1024`` (Causal language modeling, English-French)
- ``xlm-clm-ende-1024`` (Causal language modeling, English-German)

These checkpoints require language embeddings that will specify the language used at inference time. These language
embeddings are represented as a tensor that is of the same shape as the input ids passed to the model. The values in
Sylvain Gugger's avatar
Sylvain Gugger committed
41
42
these tensors depend on the language used and are identifiable using the ``lang2id`` and ``id2lang`` attributes from
the tokenizer.
LysandreJik's avatar
LysandreJik committed
43
44
45
46
47
48

Here is an example using the ``xlm-clm-enfr-1024`` checkpoint (Causal language modeling, English-French):


.. code-block::

49
50
    >>> import torch
    >>> from transformers import XLMTokenizer, XLMWithLMHeadModel
LysandreJik's avatar
LysandreJik committed
51

52
53
    >>> tokenizer = XLMTokenizer.from_pretrained("xlm-clm-enfr-1024")
    >>> model = XLMWithLMHeadModel.from_pretrained("xlm-clm-enfr-1024")
LysandreJik's avatar
LysandreJik committed
54
55
56
57
58
59
60


The different languages this model/tokenizer handles, as well as the ids of these languages are visible using the
``lang2id`` attribute:

.. code-block::

61
62
    >>> print(tokenizer.lang2id)
    {'en': 0, 'fr': 1}
LysandreJik's avatar
LysandreJik committed
63
64
65
66
67
68


These ids should be used when passing a language parameter during a model pass. Let's define our inputs:

.. code-block::

69
    >>> input_ids = torch.tensor([tokenizer.encode("Wikipedia was used to")]) # batch size of 1
LysandreJik's avatar
LysandreJik committed
70
71
72
73
74
75
76


We should now define the language embedding by using the previously defined language id. We want to create a tensor
filled with the appropriate language ids, of the same size as input_ids. For english, the id is 0:

.. code-block::

77
78
    >>> language_id = tokenizer.lang2id['en']  # 0
    >>> langs = torch.tensor([language_id] * input_ids.shape[1])  # torch.tensor([0, 0, 0, ..., 0])
LysandreJik's avatar
LysandreJik committed
79

80
81
    >>> # We reshape it to be of size (batch_size, sequence_length)
    >>> langs = langs.view(1, -1) # is now of shape [1, sequence_length] (we have a batch size of 1)
LysandreJik's avatar
LysandreJik committed
82
83
84
85
86
87


You can then feed it all as input to your model:

.. code-block::

88
    >>> outputs = model(input_ids, langs=langs)
LysandreJik's avatar
LysandreJik committed
89
90


Sylvain Gugger's avatar
Sylvain Gugger committed
91
92
The example :prefix_link:`run_generation.py <examples/pytorch/text-generation/run_generation.py>` can generate text
using the CLM checkpoints from XLM, using the language embeddings.
LysandreJik's avatar
LysandreJik committed
93
94

XLM without Language Embeddings
Sylvain Gugger's avatar
Sylvain Gugger committed
95
-----------------------------------------------------------------------------------------------------------------------
LysandreJik's avatar
LysandreJik committed
96
97
98
99
100
101

This section concerns the following checkpoints:

- ``xlm-mlm-17-1280`` (Masked language modeling, 17 languages)
- ``xlm-mlm-100-1280`` (Masked language modeling, 100 languages)

Sylvain Gugger's avatar
Sylvain Gugger committed
102
103
These checkpoints do not require language embeddings at inference time. These models are used to have generic sentence
representations, differently from previously-mentioned XLM checkpoints.
LysandreJik's avatar
LysandreJik committed
104
105
106


BERT
Sylvain Gugger's avatar
Sylvain Gugger committed
107
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
LysandreJik's avatar
LysandreJik committed
108
109
110
111
112
113

BERT has two checkpoints that can be used for multi-lingual tasks:

- ``bert-base-multilingual-uncased`` (Masked language modeling + Next sentence prediction, 102 languages)
- ``bert-base-multilingual-cased`` (Masked language modeling + Next sentence prediction, 104 languages)

Sylvain Gugger's avatar
Sylvain Gugger committed
114
115
These checkpoints do not require language embeddings at inference time. They should identify the language used in the
context and infer accordingly.
116
117

XLM-RoBERTa
Sylvain Gugger's avatar
Sylvain Gugger committed
118
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
119

Sylvain Gugger's avatar
Sylvain Gugger committed
120
XLM-RoBERTa was trained on 2.5TB of newly created clean CommonCrawl data in 100 languages. It provides strong gains
Santiago Castro's avatar
Santiago Castro committed
121
over previously released multi-lingual models like mBERT or XLM on downstream tasks like classification, sequence
Sylvain Gugger's avatar
Sylvain Gugger committed
122
labeling and question answering.
123
124
125
126
127

Two XLM-RoBERTa checkpoints can be used for multi-lingual tasks:

- ``xlm-roberta-base`` (Masked language modeling, 100 languages)
- ``xlm-roberta-large`` (Masked language modeling, 100 languages)
Ryokan RI's avatar
Ryokan RI committed
128
129
130
131
132
133
134
135
136
137
138
139
140
141

mLUKE
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

mLUKE is based on XLM-RoBERTa and further trained on Wikipedia articles in 24 languages with masked language modeling
as well as masked entity prediction objective.

The model can be used in the same way as other models solely based on word-piece inputs, but also can be used with
entity representations to achieve further performance gain, with entity-related tasks such as relation extraction,
named entity recognition and question answering (see :doc:`LUKE <model_doc/luke>`).

Currently, one mLUKE checkpoint is available:

- ``studio-ousia/mluke-base`` (Masked language modeling + Masked entity prediction, 100 languages)