From b29fd6971d9cd6ba2a824628effe243f543b8f61 Mon Sep 17 00:00:00 2001 From: Joao Gante Date: Tue, 28 Mar 2023 15:42:02 +0100 Subject: [PATCH] MBart: Fix docs and doctests (#22422) Fix docs and doctests --- .../models/mbart/modeling_mbart.py | 5 +-- .../models/mbart/modeling_tf_mbart.py | 35 +++++++++++-------- utils/documentation_tests.txt | 3 +- 3 files changed, 26 insertions(+), 17 deletions(-) diff --git a/src/transformers/models/mbart/modeling_mbart.py b/src/transformers/models/mbart/modeling_mbart.py index fe12c82dcb..778d132ac3 100755 --- a/src/transformers/models/mbart/modeling_mbart.py +++ b/src/transformers/models/mbart/modeling_mbart.py @@ -561,7 +561,7 @@ MBART_GENERATION_EXAMPLE = r""" >>> 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' ``` @@ -1266,7 +1266,8 @@ class MBartModel(MBartPreTrainedModel): @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" diff --git a/src/transformers/models/mbart/modeling_tf_mbart.py b/src/transformers/models/mbart/modeling_tf_mbart.py index 0f6e18b0b3..6f48062fc6 100644 --- a/src/transformers/models/mbart/modeling_tf_mbart.py +++ b/src/transformers/models/mbart/modeling_tf_mbart.py @@ -619,37 +619,44 @@ MBART_INPUTS_DOCSTRING = r""" """ 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 for German >>> TXT = " Meine Freunde sind nett aber sie essen zu viel Kuchen. 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'] ``` """ @@ -1299,7 +1306,7 @@ class BiasLayer(tf.keras.layers.Layer): @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): diff --git a/utils/documentation_tests.txt b/utils/documentation_tests.txt index 3357c1d569..ddb8520394 100644 --- a/utils/documentation_tests.txt +++ b/utils/documentation_tests.txt @@ -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 @@ -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 \ No newline at end of file +src/transformers/models/transfo_xl/tokenization_transfo_xl.py