---
local: xlmprophetnet
sections:
- local: overview
  title: Overview
- local: transformers.XLMProphetNetConfig
  title: XLMProphetNetConfig
- local: transformers.XLMProphetNetTokenizer
  title: XLMProphetNetTokenizer
- local: transformers.XLMProphetNetModel
  title: XLMProphetNetModel
- local: transformers.XLMProphetNetEncoder
  title: XLMProphetNetEncoder
- local: transformers.XLMProphetNetDecoder
  title: XLMProphetNetDecoder
- local: transformers.XLMProphetNetForConditionalGeneration
  title: XLMProphetNetForConditionalGeneration
- local: transformers.XLMProphetNetForCausalLM
  title: XLMProphetNetForCausalLM
title: XLM-ProphetNet
---
<script>
import Tip from "./Tip.svelte";
import Youtube from "./Youtube.svelte";
import Docstring from "./Docstring.svelte";
import CodeBlock from "./CodeBlock.svelte";
import CodeBlockFw from "./CodeBlockFw.svelte";
import DocNotebookDropdown from "./DocNotebookDropdown.svelte";
import IconCopyLink from "./IconCopyLink.svelte";
export let fw: "pt" | "tf"
</script>
<!--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.
-->

<h1 id="xlmprophetnet">XLM-ProphetNet</h1>

**DISCLAIMER:** If you see something strange, file a [Github Issue](https://github.com/huggingface/transformers/issues/new?assignees=&labels=&template=bug-report.md&title) and assign
@patrickvonplaten


<h2 id="overview">Overview</h2>

The XLM-ProphetNet model was proposed in [ProphetNet: Predicting Future N-gram for Sequence-to-Sequence Pre-training,](https://arxiv.org/abs/2001.04063) by Yu Yan, Weizhen Qi, Yeyun Gong, Dayiheng Liu, Nan Duan, Jiusheng Chen, Ruofei
Zhang, Ming Zhou on 13 Jan, 2020.

XLM-ProphetNet is an encoder-decoder model and can predict n-future tokens for "ngram" language modeling instead of
just the next token. Its architecture is identical to ProhpetNet, but the model was trained on the multi-lingual
"wiki100" Wikipedia dump.

The abstract from the paper is the following:

*In this paper, we present a new sequence-to-sequence pretraining model called ProphetNet, which introduces a novel
self-supervised objective named future n-gram prediction and the proposed n-stream self-attention mechanism. Instead of
the optimization of one-step ahead prediction in traditional sequence-to-sequence model, the ProphetNet is optimized by
n-step ahead prediction which predicts the next n tokens simultaneously based on previous context tokens at each time
step. The future n-gram prediction explicitly encourages the model to plan for the future tokens and prevent
overfitting on strong local correlations. We pre-train ProphetNet using a base scale dataset (16GB) and a large scale
dataset (160GB) respectively. Then we conduct experiments on CNN/DailyMail, Gigaword, and SQuAD 1.1 benchmarks for
abstractive summarization and question generation tasks. Experimental results show that ProphetNet achieves new
state-of-the-art results on all these datasets compared to the models using the same scale pretraining corpus.*

The Authors' code can be found [here](https://github.com/microsoft/ProphetNet).

<h2 id="transformers.XLMProphetNetConfig">XLMProphetNetConfig</h2>

<div class="docstring">

<docstring><name>class transformers.XLMProphetNetConfig</name><anchor>transformers.XLMProphetNetConfig</anchor><source>https://github.com/huggingface/transformers/blob/pr_15592/src/transformers/models/xlm_prophetnet/configuration_xlm_prophetnet.py#L29</source><parameters>[{"name": "activation_dropout", "val": " = 0.1"}, {"name": "activation_function", "val": " = 'gelu'"}, {"name": "vocab_size", "val": " = 30522"}, {"name": "hidden_size", "val": " = 1024"}, {"name": "encoder_ffn_dim", "val": " = 4096"}, {"name": "num_encoder_layers", "val": " = 12"}, {"name": "num_encoder_attention_heads", "val": " = 16"}, {"name": "decoder_ffn_dim", "val": " = 4096"}, {"name": "num_decoder_layers", "val": " = 12"}, {"name": "num_decoder_attention_heads", "val": " = 16"}, {"name": "attention_dropout", "val": " = 0.1"}, {"name": "dropout", "val": " = 0.1"}, {"name": "max_position_embeddings", "val": " = 512"}, {"name": "init_std", "val": " = 0.02"}, {"name": "is_encoder_decoder", "val": " = True"}, {"name": "add_cross_attention", "val": " = True"}, {"name": "decoder_start_token_id", "val": " = 0"}, {"name": "ngram", "val": " = 2"}, {"name": "num_buckets", "val": " = 32"}, {"name": "relative_max_distance", "val": " = 128"}, {"name": "disable_ngram_loss", "val": " = False"}, {"name": "eps", "val": " = 0.0"}, {"name": "use_cache", "val": " = True"}, {"name": "pad_token_id", "val": " = 0"}, {"name": "bos_token_id", "val": " = 1"}, {"name": "eos_token_id", "val": " = 2"}, {"name": "**kwargs", "val": ""}]</parameters></docstring>

This class overrides [ProphetNetConfig](/docs/transformers/pr_15592/en/model_doc/prophetnet#transformers.ProphetNetConfig). Please check the superclass for the appropriate documentation alongside
usage examples.


</div>

<h2 id="transformers.XLMProphetNetTokenizer">XLMProphetNetTokenizer</h2>

<div class="docstring">

<docstring><name>class transformers.XLMProphetNetTokenizer</name><anchor>transformers.XLMProphetNetTokenizer</anchor><source>https://github.com/huggingface/transformers/blob/pr_15592/src/transformers/models/xlm_prophetnet/tokenization_xlm_prophetnet.py#L57</source><parameters>[{"name": "vocab_file", "val": ""}, {"name": "bos_token", "val": " = '[SEP]'"}, {"name": "eos_token", "val": " = '[SEP]'"}, {"name": "sep_token", "val": " = '[SEP]'"}, {"name": "unk_token", "val": " = '[UNK]'"}, {"name": "pad_token", "val": " = '[PAD]'"}, {"name": "cls_token", "val": " = '[CLS]'"}, {"name": "mask_token", "val": " = '[MASK]'"}, {"name": "sp_model_kwargs", "val": ": typing.Union[typing.Dict[str, typing.Any], NoneType] = None"}, {"name": "**kwargs", "val": ""}]</parameters><paramsdesc>- **vocab_file** (`str`) --
Path to the vocabulary file.
- **bos_token** (`str`, *optional*, defaults to `"<s>"`) --
  The beginning of sequence token that was used during pretraining. Can be used a sequence classifier token.

  <Tip>

  When building a sequence using special tokens, this is not the token that is used for the beginning of
  sequence. The token used is the `cls_token`.

  </Tip>

- **eos_token** (`str`, *optional*, defaults to `"</s>"`) --
  The end of sequence token.

  <Tip>

  When building a sequence using special tokens, this is not the token that is used for the end of sequence.
  The token used is the `sep_token`.

  </Tip>

- **sep_token** (`str`, *optional*, defaults to `"&amp;lt;/s>"`) --
  The separator token, which is used when building a sequence from multiple sequences, e.g. two sequences for
  sequence classification or for a text and a question for question answering. It is also used as the last
  token of a sequence built with special tokens.
- **cls_token** (`str`, *optional*, defaults to `"<s>"`) --
  The classifier token which is used when doing sequence classification (classification of the whole sequence
  instead of per-token classification). It is the first token of the sequence when built with special tokens.
- **unk_token** (`str`, *optional*, defaults to `"&amp;lt;unk>"`) --
  The unknown token. A token that is not in the vocabulary cannot be converted to an ID and is set to be this
  token instead.
- **pad_token** (`str`, *optional*, defaults to `"&amp;lt;pad>"`) --
  The token used for padding, for example when batching sequences of different lengths.
- **mask_token** (`str`, *optional*, defaults to `"&amp;lt;mask>"`) --
  The token used for masking values. This is the token used when training this model with masked language
  modeling. This is the token which the model will try to predict.
- **additional_special_tokens** (`List[str]`, *optional*, defaults to `["&amp;lt;s>NOTUSED", "</s>NOTUSED"]`) --
  Additional special tokens used by the tokenizer.
- **sp_model_kwargs** (`dict`, *optional*) --
  Will be passed to the `SentencePieceProcessor.__init__()` method. The [Python wrapper for
  SentencePiece](https://github.com/google/sentencepiece/tree/master/python) can be used, among other things,
  to set:

  - `enable_sampling`: Enable subword regularization.
  - `nbest_size`: Sampling parameters for unigram. Invalid for BPE-Dropout.

    - `nbest_size = &amp;lcub;0,1}`: No sampling is performed.
    - `nbest_size > 1`: samples from the nbest_size results.
    - `nbest_size &amp;lt; 0`: assuming that nbest_size is infinite and samples from the all hypothesis (lattice)
      using forward-filtering-and-backward-sampling algorithm.

  - `alpha`: Smoothing parameter for unigram sampling, and dropout probability of merge operations for
    BPE-dropout.
- **sp_model** (`SentencePieceProcessor`) --
  The *SentencePiece* processor that is used for every conversion (string, tokens and IDs).</paramsdesc><paramgroups>0</paramgroups></docstring>

Adapted from [RobertaTokenizer](/docs/transformers/pr_15592/en/model_doc/roberta#transformers.RobertaTokenizer) and [XLNetTokenizer](/docs/transformers/pr_15592/en/model_doc/xlnet#transformers.XLNetTokenizer). Based on
[SentencePiece](https://github.com/google/sentencepiece).

This tokenizer inherits from [PreTrainedTokenizer](/docs/transformers/pr_15592/en/main_classes/tokenizer#transformers.PreTrainedTokenizer) which contains most of the main methods. Users should refer to
this superclass for more information regarding those methods.









<div class="docstring">
<docstring><name>build\_inputs\_with\_special\_tokens</name><anchor>transformers.XLMProphetNetTokenizer.build_inputs_with_special_tokens</anchor><source>https://github.com/huggingface/transformers/blob/pr_15592/src/transformers/models/xlm_prophetnet/tokenization_xlm_prophetnet.py#L314</source><parameters>[{"name": "token_ids_0", "val": ": typing.List[int]"}, {"name": "token_ids_1", "val": ": typing.Optional[typing.List[int]] = None"}]</parameters><paramsdesc>- **token_ids_0** (`List[int]`) --
  List of IDs to which the special tokens will be added
- **token_ids_1** (`List[int]`, *optional*) --
  Optional second list of IDs for sequence pairs.</paramsdesc><paramgroups>0</paramgroups><rettype>`List[int]`</rettype><retdesc>list of [input IDs](../glossary#input-ids) with the appropriate special tokens.</retdesc></docstring>

Build model inputs from a sequence or a pair of sequence for sequence classification tasks by concatenating and
adding special tokens. A XLMProphetNet sequence has the following format:

- single sequence: `X [SEP]`
- pair of sequences: `A [SEP] B [SEP]`








</div>
<div class="docstring">
<docstring><name>convert\_tokens\_to\_string</name><anchor>transformers.XLMProphetNetTokenizer.convert_tokens_to_string</anchor><source>https://github.com/huggingface/transformers/blob/pr_15592/src/transformers/models/xlm_prophetnet/tokenization_xlm_prophetnet.py#L292</source><parameters>[{"name": "tokens", "val": ""}]</parameters></docstring>
Converts a sequence of tokens (strings for sub-words) in a single string.

</div>
<div class="docstring">
<docstring><name>create\_token\_type\_ids\_from\_sequences</name><anchor>transformers.XLMProphetNetTokenizer.create_token_type_ids_from_sequences</anchor><source>https://github.com/huggingface/transformers/blob/pr_15592/src/transformers/models/xlm_prophetnet/tokenization_xlm_prophetnet.py#L241</source><parameters>[{"name": "token_ids_0", "val": ": typing.List[int]"}, {"name": "token_ids_1", "val": ": typing.Optional[typing.List[int]] = None"}]</parameters><paramsdesc>- **token_ids_0** (`List[int]`) --
  List of IDs.
- **token_ids_1** (`List[int]`, *optional*) --
  Optional second list of IDs for sequence pairs.</paramsdesc><paramgroups>0</paramgroups><rettype>`List[int]`</rettype><retdesc>List of zeros.</retdesc></docstring>

Create a mask from the two sequences passed to be used in a sequence-pair classification task. XLMProphetNet
does not make use of token type ids, therefore a list of zeros is returned.








</div>
<div class="docstring">
<docstring><name>get\_special\_tokens\_mask</name><anchor>transformers.XLMProphetNetTokenizer.get_special_tokens_mask</anchor><source>https://github.com/huggingface/transformers/blob/pr_15592/src/transformers/models/xlm_prophetnet/tokenization_xlm_prophetnet.py#L213</source><parameters>[{"name": "token_ids_0", "val": ": typing.List[int]"}, {"name": "token_ids_1", "val": ": typing.Optional[typing.List[int]] = None"}, {"name": "already_has_special_tokens", "val": ": bool = False"}]</parameters><paramsdesc>- **token_ids_0** (`List[int]`) --
  List of IDs.
- **token_ids_1** (`List[int]`, *optional*) --
  Optional second list of IDs for sequence pairs.
- **already_has_special_tokens** (`bool`, *optional*, defaults to `False`) --
  Whether or not the token list is already formatted with special tokens for the model.</paramsdesc><paramgroups>0</paramgroups><rettype>`List[int]`</rettype><retdesc>A list of integers in the range [0, 1]: 1 for a special token, 0 for a sequence token.</retdesc></docstring>

Retrieve sequence ids from a token list that has no special tokens added. This method is called when adding
special tokens using the tokenizer `prepare_for_model` method.








</div></div>

<h2 id="transformers.XLMProphetNetModel">XLMProphetNetModel</h2>

<div class="docstring">

<docstring><name>class transformers.XLMProphetNetModel</name><anchor>transformers.XLMProphetNetModel</anchor><source>https://github.com/huggingface/transformers/blob/pr_15592/src/transformers/models/xlm_prophetnet/modeling_xlm_prophetnet.py#L86</source><parameters>[{"name": "config", "val": ""}]</parameters></docstring>

This class overrides [ProphetNetModel](/docs/transformers/pr_15592/en/model_doc/prophetnet#transformers.ProphetNetModel). Please check the superclass for the appropriate documentation alongside
usage examples.

Example:

```python
>>> from transformers import XLMProphetNetTokenizer, XLMProphetNetModel

>>> tokenizer = XLMProphetNetTokenizer.from_pretrained("microsoft/xprophetnet-large-wiki100-cased")
>>> model = XLMProphetNetModel.from_pretrained("microsoft/xprophetnet-large-wiki100-cased")

>>> input_ids = tokenizer(
...     "Studies have been shown that owning a dog is good for you", return_tensors="pt"
>>> ).input_ids  # Batch size 1
>>> decoder_input_ids = tokenizer("Studies show that", return_tensors="pt").input_ids  # Batch size 1
>>> outputs = model(input_ids=input_ids, decoder_input_ids=decoder_input_ids)

>>> last_hidden_states = outputs.last_hidden_state  # main stream hidden states
>>> last_hidden_states_ngram = outputs.last_hidden_state_ngram  # predict hidden states
```

</div>

<h2 id="transformers.XLMProphetNetEncoder">XLMProphetNetEncoder</h2>

<div class="docstring">

<docstring><name>class transformers.XLMProphetNetEncoder</name><anchor>transformers.XLMProphetNetEncoder</anchor><source>https://github.com/huggingface/transformers/blob/pr_15592/src/transformers/models/xlm_prophetnet/modeling_xlm_prophetnet.py#L38</source><parameters>[{"name": "config", "val": ": ProphetNetConfig"}, {"name": "word_embeddings", "val": ": Embedding = None"}]</parameters></docstring>

This class overrides [ProphetNetEncoder](/docs/transformers/pr_15592/en/model_doc/prophetnet#transformers.ProphetNetEncoder). Please check the superclass for the appropriate documentation alongside
usage examples.

Example:

```python
>>> from transformers import XLMProphetNetTokenizer, XLMProphetNetEncoder
>>> import torch

>>> tokenizer = XLMProphetNetTokenizer.from_pretrained("microsoft/xprophetnet-large-wiki100-cased")
>>> model = XLMProphetNetEncoder.from_pretrained("patrickvonplaten/xprophetnet-large-uncased-standalone")
>>> assert model.config.is_decoder, f"&amp;lcub;model.__class__} has to be configured as a decoder."
>>> inputs = tokenizer("Hello, my dog is cute", return_tensors="pt")
>>> outputs = model(**inputs)

>>> last_hidden_states = outputs.last_hidden_state
```

</div>

<h2 id="transformers.XLMProphetNetDecoder">XLMProphetNetDecoder</h2>

<div class="docstring">

<docstring><name>class transformers.XLMProphetNetDecoder</name><anchor>transformers.XLMProphetNetDecoder</anchor><source>https://github.com/huggingface/transformers/blob/pr_15592/src/transformers/models/xlm_prophetnet/modeling_xlm_prophetnet.py#L61</source><parameters>[{"name": "config", "val": ": ProphetNetConfig"}, {"name": "word_embeddings", "val": ": Embedding = None"}]</parameters></docstring>

This class overrides [ProphetNetDecoder](/docs/transformers/pr_15592/en/model_doc/prophetnet#transformers.ProphetNetDecoder). Please check the superclass for the appropriate documentation alongside
usage examples.

Example:

```python
>>> from transformers import XLMProphetNetTokenizer, XLMProphetNetDecoder
>>> import torch

>>> tokenizer = XLMProphetNetTokenizer.from_pretrained("microsoft/xprophetnet-large-wiki100-cased")
>>> model = XLMProphetNetDecoder.from_pretrained(
...     "patrickvonplaten/xprophetnet-large-uncased-standalone", add_cross_attention=False
... )
>>> assert model.config.is_decoder, f"&amp;lcub;model.__class__} has to be configured as a decoder."
>>> inputs = tokenizer("Hello, my dog is cute", return_tensors="pt")
>>> outputs = model(**inputs)

>>> last_hidden_states = outputs.last_hidden_state
```

</div>

<h2 id="transformers.XLMProphetNetForConditionalGeneration">XLMProphetNetForConditionalGeneration</h2>

<div class="docstring">

<docstring><name>class transformers.XLMProphetNetForConditionalGeneration</name><anchor>transformers.XLMProphetNetForConditionalGeneration</anchor><source>https://github.com/huggingface/transformers/blob/pr_15592/src/transformers/models/xlm_prophetnet/modeling_xlm_prophetnet.py#L112</source><parameters>[{"name": "config", "val": ": ProphetNetConfig"}]</parameters></docstring>

This class overrides [ProphetNetForConditionalGeneration](/docs/transformers/pr_15592/en/model_doc/prophetnet#transformers.ProphetNetForConditionalGeneration). Please check the superclass for the appropriate
documentation alongside usage examples.

Example:

```python
>>> from transformers import XLMProphetNetTokenizer, XLMProphetNetForConditionalGeneration

>>> tokenizer = XLMProphetNetTokenizer.from_pretrained("microsoft/xprophetnet-large-wiki100-cased")
>>> model = XLMProphetNetForConditionalGeneration.from_pretrained("microsoft/xprophetnet-large-wiki100-cased")

>>> input_ids = tokenizer(
...     "Studies have been shown that owning a dog is good for you", return_tensors="pt"
>>> ).input_ids  # Batch size 1
>>> decoder_input_ids = tokenizer("Studies show that", return_tensors="pt").input_ids  # Batch size 1
>>> outputs = model(input_ids=input_ids, decoder_input_ids=decoder_input_ids)

>>> logits_next_token = outputs.logits  # logits to predict next token as usual
>>> logits_ngram_next_tokens = outputs.logits_ngram  # logits to predict 2nd, 3rd, ... next tokens
```

</div>

<h2 id="transformers.XLMProphetNetForCausalLM">XLMProphetNetForCausalLM</h2>

<div class="docstring">

<docstring><name>class transformers.XLMProphetNetForCausalLM</name><anchor>transformers.XLMProphetNetForCausalLM</anchor><source>https://github.com/huggingface/transformers/blob/pr_15592/src/transformers/models/xlm_prophetnet/modeling_xlm_prophetnet.py#L138</source><parameters>[{"name": "config", "val": ""}]</parameters></docstring>

This class overrides [ProphetNetForCausalLM](/docs/transformers/pr_15592/en/model_doc/prophetnet#transformers.ProphetNetForCausalLM). Please check the superclass for the appropriate documentation
alongside usage examples.

Example:

```python
>>> from transformers import XLMProphetNetTokenizer, XLMProphetNetForCausalLM
>>> import torch

>>> tokenizer = XLMProphetNetTokenizer.from_pretrained("microsoft/xprophetnet-large-wiki100-cased")
>>> model = XLMProphetNetForCausalLM.from_pretrained("microsoft/xprophetnet-large-wiki100-cased")
>>> assert model.config.is_decoder, f"&amp;lcub;model.__class__} has to be configured as a decoder."
>>> inputs = tokenizer("Hello, my dog is cute", return_tensors="pt")
>>> outputs = model(**inputs)

>>> logits = outputs.logits

>>> # Model can also be used with EncoderDecoder framework
>>> from transformers import EncoderDecoderModel, XLMProphetNetTokenizer, XLMRobertaTokenizer
>>> import torch

>>> tokenizer_enc = XLMRobertaTokenizer.from_pretrained("xlm-roberta-large")
>>> tokenizer_dec = XLMProphetNetTokenizer.from_pretrained("microsoft/xprophetnet-large-wiki100-cased")
>>> model = EncoderDecoderModel.from_encoder_decoder_pretrained(
...     "xlm-roberta-large", "microsoft/xprophetnet-large-wiki100-cased"
... )

>>> ARTICLE = (
...     "the us state department said wednesday it had received no "
...     "formal word from bolivia that it was expelling the us ambassador there "
...     "but said the charges made against him are `` baseless ."
... )
>>> input_ids = tokenizer_enc(ARTICLE, return_tensors="pt").input_ids
>>> labels = tokenizer_dec("us rejects charges against its ambassador in bolivia", return_tensors="pt").input_ids
>>> outputs = model(input_ids=input_ids, decoder_input_ids=labels[:, :-1], labels=labels[:, 1:])

>>> loss = outputs.loss
```

</div>
