Skip to content

Commit

Permalink
MBart: Fix docs and doctests (huggingface#22422)
Browse files Browse the repository at this point in the history
Fix docs and doctests
  • Loading branch information
gante authored Mar 28, 2023
1 parent ae5fc2d commit b29fd69
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 17 deletions.
5 changes: 3 additions & 2 deletions src/transformers/models/mbart/modeling_mbart.py
Original file line number Diff line number Diff line change
Expand Up @@ -561,7 +561,7 @@ def dummy_inputs(self):
>>> inputs = tokenizer(example_english_phrase, return_tensors="pt")
>>> # Translate
>>> generated_ids = model.generate(inputs["input_ids"], num_beams=4, max_length=5)
>>> generated_ids = model.generate(**inputs, num_beams=4, max_length=5)
>>> tokenizer.batch_decode(generated_ids, skip_special_tokens=True, clean_up_tokenization_spaces=False)[0]
'42 este răspuns'
```
Expand Down Expand Up @@ -1266,7 +1266,8 @@ def forward(


@add_start_docstrings(
"The MBART Model with a language modeling head. Can be used for summarization.", MBART_START_DOCSTRING
"The MBART Model with a language modeling head. Can be used for summarization, after fine-tuning the pretrained models.",
MBART_START_DOCSTRING,
)
class MBartForConditionalGeneration(MBartPreTrainedModel):
base_model_prefix = "model"
Expand Down
35 changes: 21 additions & 14 deletions src/transformers/models/mbart/modeling_tf_mbart.py
Original file line number Diff line number Diff line change
Expand Up @@ -619,37 +619,44 @@ def serving(self, inputs):
"""

MBART_GENERATION_EXAMPLE = r"""
Summarization example:
Translation example:
```python
>>> from transformers import AutoTokenizer, TFMBartForConditionalGeneration, MBartConfig
>>> from transformers import AutoTokenizer, TFMBartForConditionalGeneration
>>> model = TFMBartForConditionalGeneration.from_pretrained("facebook/mbart-large-cc25")
>>> tokenizer = AutoTokenizer.from_pretrained("facebook/mbart-large-cc25")
>>> model = TFMBartForConditionalGeneration.from_pretrained("facebook/mbart-large-en-ro")
>>> tokenizer = AutoTokenizer.from_pretrained("facebook/mbart-large-en-ro")
>>> ARTICLE_TO_SUMMARIZE = "Meine Freunde sind cool, aber sie essen zu viel Kuchen."
>>> inputs = tokenizer([ARTICLE_TO_SUMMARIZE], max_length=1024, return_tensors="tf")
>>> example_english_phrase = "42 is the answer"
>>> inputs = tokenizer(example_english_phrase, return_tensors="tf")
>>> # Generate Summary
>>> summary_ids = model.generate(inputs["input_ids"], num_beams=4, max_length=5)
>>> print(tokenizer.batch_decode(summary_ids, skip_special_tokens=True, clean_up_tokenization_spaces=False))
>>> # Translate
>>> generated_ids = model.generate(**inputs, num_beams=4, max_length=5)
>>> tokenizer.batch_decode(generated_ids, skip_special_tokens=True, clean_up_tokenization_spaces=False)[0]
'42 este răspuns'
```
Mask filling example:
```python
>>> from transformers import AutoTokenizer, TFMBartForConditionalGeneration
>>> import tensorflow as tf
>>> model = MBartForConditionalGeneration.from_pretrained("facebook/mbart-large-cc25")
>>> model = TFMBartForConditionalGeneration.from_pretrained("facebook/mbart-large-cc25")
>>> tokenizer = AutoTokenizer.from_pretrained("facebook/mbart-large-cc25")
>>> # de_DE is the language symbol id <LID> for German
>>> TXT = "</s> Meine Freunde sind <mask> nett aber sie essen zu viel Kuchen. </s> de_DE"
>>> input_ids = tokenizer([TXT], add_special_tokens=False, return_tensors="tf")["input_ids"]
>>> input_ids = tokenizer([TXT], add_special_tokens=False, return_tensors="tf")["input_ids"]
>>> logits = model(input_ids).logits
>>> probs = tf.nn.softmax(logits[0])
>>> # probs[5] is associated with the mask token
>>> masked_index = tf.where(input_ids[0] == tokenizer.mask_token_id)[0, 0]
>>> probs = tf.nn.softmax(logits[0, masked_index], axis=0)
>>> values, predictions = tf.math.top_k(probs, 5)
>>> tokenizer.decode(predictions).split()
['nett', 'sehr', 'ganz', 'nicht', 'so']
```
"""

Expand Down Expand Up @@ -1299,7 +1306,7 @@ def call(self, x):


@add_start_docstrings(
"The MBART Model with a language modeling head. Can be used for summarization.",
"The MBART Model with a language modeling head. Can be used for summarization, after fine-tuning the pretrained models.",
MBART_START_DOCSTRING,
)
class TFMBartForConditionalGeneration(TFMBartPreTrainedModel, TFCausalLanguageModelingLoss):
Expand Down
3 changes: 2 additions & 1 deletion utils/documentation_tests.txt
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ src/transformers/models/maskformer/configuration_maskformer.py
src/transformers/models/maskformer/modeling_maskformer.py
src/transformers/models/mbart/configuration_mbart.py
src/transformers/models/mbart/modeling_mbart.py
src/transformers/models/mbart/modeling_tf_mbart.py
src/transformers/models/mctct/configuration_mctct.py
src/transformers/models/megatron_bert/configuration_megatron_bert.py
src/transformers/models/mobilebert/configuration_mobilebert.py
Expand Down Expand Up @@ -479,4 +480,4 @@ src/transformers/models/m2m_100/tokenization_m2m_100.py
src/transformers/models/marian/tokenization_marian.py
src/transformers/models/roformer/tokenization_roformer.py
src/transformers/models/roformer/tokenization_roformer_fast.py
src/transformers/models/transfo_xl/tokenization_transfo_xl.py
src/transformers/models/transfo_xl/tokenization_transfo_xl.py

0 comments on commit b29fd69

Please sign in to comment.