mirror of
https://github.com/huggingface/transformers.git
synced 2025-07-31 02:02:21 +06:00
Rework automatic code samples in docstrings (#20757)
* Rework automatic code samples in docstrings * ImageProcessor->AutoImageProcessor * Add models to fix copies * Last typos * A couple more models * Fix copies
This commit is contained in:
parent
7f65d2366a
commit
c8f35a9ce3
@ -56,7 +56,6 @@ logger = logging.get_logger(__name__)
|
||||
|
||||
_CHECKPOINT_FOR_DOC = "bert-base-uncased"
|
||||
_CONFIG_FOR_DOC = "BertConfig"
|
||||
_TOKENIZER_FOR_DOC = "BertTokenizer"
|
||||
|
||||
# TokenClassification docstring
|
||||
_CHECKPOINT_FOR_TOKEN_CLASSIFICATION = "dbmdz/bert-large-cased-finetuned-conll03-english"
|
||||
@ -911,7 +910,6 @@ class BertModel(BertPreTrainedModel):
|
||||
|
||||
@add_start_docstrings_to_model_forward(BERT_INPUTS_DOCSTRING.format("batch_size, sequence_length"))
|
||||
@add_code_sample_docstrings(
|
||||
processor_class=_TOKENIZER_FOR_DOC,
|
||||
checkpoint=_CHECKPOINT_FOR_DOC,
|
||||
output_type=BaseModelOutputWithPoolingAndCrossAttentions,
|
||||
config_class=_CONFIG_FOR_DOC,
|
||||
@ -1184,7 +1182,6 @@ class BertLMHeadModel(BertPreTrainedModel):
|
||||
|
||||
@add_start_docstrings_to_model_forward(BERT_INPUTS_DOCSTRING.format("batch_size, sequence_length"))
|
||||
@add_code_sample_docstrings(
|
||||
processor_class=_TOKENIZER_FOR_DOC,
|
||||
checkpoint=_CHECKPOINT_FOR_DOC,
|
||||
output_type=CausalLMOutputWithCrossAttentions,
|
||||
config_class=_CONFIG_FOR_DOC,
|
||||
@ -1329,7 +1326,6 @@ class BertForMaskedLM(BertPreTrainedModel):
|
||||
|
||||
@add_start_docstrings_to_model_forward(BERT_INPUTS_DOCSTRING.format("batch_size, sequence_length"))
|
||||
@add_code_sample_docstrings(
|
||||
processor_class=_TOKENIZER_FOR_DOC,
|
||||
checkpoint=_CHECKPOINT_FOR_DOC,
|
||||
output_type=MaskedLMOutput,
|
||||
config_class=_CONFIG_FOR_DOC,
|
||||
@ -1537,7 +1533,6 @@ class BertForSequenceClassification(BertPreTrainedModel):
|
||||
|
||||
@add_start_docstrings_to_model_forward(BERT_INPUTS_DOCSTRING.format("batch_size, sequence_length"))
|
||||
@add_code_sample_docstrings(
|
||||
processor_class=_TOKENIZER_FOR_DOC,
|
||||
checkpoint=_CHECKPOINT_FOR_SEQUENCE_CLASSIFICATION,
|
||||
output_type=SequenceClassifierOutput,
|
||||
config_class=_CONFIG_FOR_DOC,
|
||||
@ -1639,7 +1634,6 @@ class BertForMultipleChoice(BertPreTrainedModel):
|
||||
|
||||
@add_start_docstrings_to_model_forward(BERT_INPUTS_DOCSTRING.format("batch_size, num_choices, sequence_length"))
|
||||
@add_code_sample_docstrings(
|
||||
processor_class=_TOKENIZER_FOR_DOC,
|
||||
checkpoint=_CHECKPOINT_FOR_DOC,
|
||||
output_type=MultipleChoiceModelOutput,
|
||||
config_class=_CONFIG_FOR_DOC,
|
||||
@ -1738,7 +1732,6 @@ class BertForTokenClassification(BertPreTrainedModel):
|
||||
|
||||
@add_start_docstrings_to_model_forward(BERT_INPUTS_DOCSTRING.format("batch_size, sequence_length"))
|
||||
@add_code_sample_docstrings(
|
||||
processor_class=_TOKENIZER_FOR_DOC,
|
||||
checkpoint=_CHECKPOINT_FOR_TOKEN_CLASSIFICATION,
|
||||
output_type=TokenClassifierOutput,
|
||||
config_class=_CONFIG_FOR_DOC,
|
||||
@ -1821,7 +1814,6 @@ class BertForQuestionAnswering(BertPreTrainedModel):
|
||||
|
||||
@add_start_docstrings_to_model_forward(BERT_INPUTS_DOCSTRING.format("batch_size, sequence_length"))
|
||||
@add_code_sample_docstrings(
|
||||
processor_class=_TOKENIZER_FOR_DOC,
|
||||
checkpoint=_CHECKPOINT_FOR_QA,
|
||||
output_type=QuestionAnsweringModelOutput,
|
||||
config_class=_CONFIG_FOR_DOC,
|
||||
|
@ -51,21 +51,8 @@ logger = logging.get_logger(__name__)
|
||||
|
||||
_CHECKPOINT_FOR_DOC = "google/bigbird-pegasus-large-arxiv"
|
||||
_CONFIG_FOR_DOC = "BigBirdPegasusConfig"
|
||||
_TOKENIZER_FOR_DOC = "PegasusTokenizerFast"
|
||||
|
||||
# Base model docstring
|
||||
_EXPECTED_OUTPUT_SHAPE = [1, 7, 1024]
|
||||
|
||||
# SequenceClassification docstring
|
||||
_CHECKPOINT_FOR_SEQUENCE_CLASSIFICATION = "hf-internal-testing/tiny-random-bigbird_pegasus"
|
||||
_SEQ_CLASS_EXPECTED_LOSS = 0.69
|
||||
_SEQ_CLASS_EXPECTED_OUTPUT = "'LABEL_1'"
|
||||
|
||||
# QuestionAsnwering docstring
|
||||
_CHECKPOINT_FOR_QA = "hf-internal-testing/tiny-random-bigbird_pegasus"
|
||||
_QA_EXPECTED_LOSS = 3.96
|
||||
_QA_EXPECTED_OUTPUT = "''"
|
||||
|
||||
|
||||
BIGBIRD_PEGASUS_PRETRAINED_MODEL_ARCHIVE_LIST = [
|
||||
"google/bigbird-pegasus-large-arxiv",
|
||||
@ -2355,7 +2342,6 @@ class BigBirdPegasusDecoder(BigBirdPegasusPreTrainedModel):
|
||||
"The bare BigBirdPegasus Model outputting raw hidden-states without any specific head on top.",
|
||||
BIGBIRD_PEGASUS_START_DOCSTRING,
|
||||
)
|
||||
# Copied from transformers.models.bart.modeling_bart.BartModel with Bart->BigBirdPegasus, BART->BIGBIRD_PEGASUS
|
||||
class BigBirdPegasusModel(BigBirdPegasusPreTrainedModel):
|
||||
_keys_to_ignore_on_load_missing = ["encoder.embed_tokens.weight", "decoder.embed_tokens.weight"]
|
||||
|
||||
@ -2387,12 +2373,12 @@ class BigBirdPegasusModel(BigBirdPegasusPreTrainedModel):
|
||||
|
||||
@add_start_docstrings_to_model_forward(BIGBIRD_PEGASUS_INPUTS_DOCSTRING)
|
||||
@add_code_sample_docstrings(
|
||||
processor_class=_TOKENIZER_FOR_DOC,
|
||||
checkpoint=_CHECKPOINT_FOR_DOC,
|
||||
output_type=Seq2SeqModelOutput,
|
||||
config_class=_CONFIG_FOR_DOC,
|
||||
expected_output=_EXPECTED_OUTPUT_SHAPE,
|
||||
)
|
||||
# Copied from transformers.models.bart.modeling_bart.BartModel.forward with Bart->BigBirdPegasus
|
||||
def forward(
|
||||
self,
|
||||
input_ids: torch.LongTensor = None,
|
||||
@ -2665,7 +2651,6 @@ class BigBirdPegasusForConditionalGeneration(BigBirdPegasusPreTrainedModel):
|
||||
""",
|
||||
BIGBIRD_PEGASUS_START_DOCSTRING,
|
||||
)
|
||||
# Copied from transformers.models.bart.modeling_bart.BartForSequenceClassification with Bart->BigBirdPegasus, BART->BIGBIRD_PEGASUS
|
||||
class BigBirdPegasusForSequenceClassification(BigBirdPegasusPreTrainedModel):
|
||||
_keys_to_ignore_on_load_missing = ["encoder.embed_tokens.weight", "decoder.embed_tokens.weight"]
|
||||
|
||||
@ -2683,13 +2668,11 @@ class BigBirdPegasusForSequenceClassification(BigBirdPegasusPreTrainedModel):
|
||||
|
||||
@add_start_docstrings_to_model_forward(BIGBIRD_PEGASUS_INPUTS_DOCSTRING)
|
||||
@add_code_sample_docstrings(
|
||||
processor_class=_TOKENIZER_FOR_DOC,
|
||||
checkpoint=_CHECKPOINT_FOR_SEQUENCE_CLASSIFICATION,
|
||||
checkpoint=_CHECKPOINT_FOR_DOC,
|
||||
output_type=Seq2SeqSequenceClassifierOutput,
|
||||
config_class=_CONFIG_FOR_DOC,
|
||||
expected_output=_SEQ_CLASS_EXPECTED_OUTPUT,
|
||||
expected_loss=_SEQ_CLASS_EXPECTED_LOSS,
|
||||
)
|
||||
# Copied from transformers.models.bart.modeling_bart.BartForSequenceClassification.forward
|
||||
def forward(
|
||||
self,
|
||||
input_ids: torch.LongTensor = None,
|
||||
@ -2795,7 +2778,6 @@ class BigBirdPegasusForSequenceClassification(BigBirdPegasusPreTrainedModel):
|
||||
""",
|
||||
BIGBIRD_PEGASUS_START_DOCSTRING,
|
||||
)
|
||||
# Copied from transformers.models.bart.modeling_bart.BartForQuestionAnswering with Bart->BigBirdPegasus, BART->BIGBIRD_PEGASUS
|
||||
class BigBirdPegasusForQuestionAnswering(BigBirdPegasusPreTrainedModel):
|
||||
_keys_to_ignore_on_load_missing = ["encoder.embed_tokens.weight", "decoder.embed_tokens.weight"]
|
||||
|
||||
@ -2812,13 +2794,11 @@ class BigBirdPegasusForQuestionAnswering(BigBirdPegasusPreTrainedModel):
|
||||
|
||||
@add_start_docstrings_to_model_forward(BIGBIRD_PEGASUS_INPUTS_DOCSTRING)
|
||||
@add_code_sample_docstrings(
|
||||
processor_class=_TOKENIZER_FOR_DOC,
|
||||
checkpoint=_CHECKPOINT_FOR_QA,
|
||||
checkpoint=_CHECKPOINT_FOR_DOC,
|
||||
output_type=Seq2SeqQuestionAnsweringModelOutput,
|
||||
config_class=_CONFIG_FOR_DOC,
|
||||
expected_loss=_QA_EXPECTED_LOSS,
|
||||
expected_output=_QA_EXPECTED_OUTPUT,
|
||||
)
|
||||
# Copied from transformers.models.bart.modeling_bart.BartForQuestionAnswering.forward
|
||||
def forward(
|
||||
self,
|
||||
input_ids: torch.Tensor = None,
|
||||
|
@ -47,7 +47,6 @@ _HIDDEN_STATES_START_POSITION = 2
|
||||
|
||||
# General docstring
|
||||
_CONFIG_FOR_DOC = "Data2VecAudioConfig"
|
||||
_PROCESSOR_FOR_DOC = "Wav2Vec2Processor"
|
||||
|
||||
# Base docstring
|
||||
_CHECKPOINT_FOR_DOC = "facebook/data2vec-audio-base-960h"
|
||||
@ -57,20 +56,6 @@ _EXPECTED_OUTPUT_SHAPE = [1, 292, 768]
|
||||
_CTC_EXPECTED_OUTPUT = "'MISTER QUILTER IS THE APOSTLE OF THE MIDDLE CLASSES AND WE ARE GLAD TO WELCOME HIS GOSPEL'"
|
||||
_CTC_EXPECTED_LOSS = 66.95
|
||||
|
||||
# Audio class docstring
|
||||
_FEAT_EXTRACTOR_FOR_DOC = "Wav2Vec2FeatureExtractor"
|
||||
_SEQ_CLASS_CHECKPOINT = "hf-internal-testing/tiny-random-data2vec-seq-class"
|
||||
_SEQ_CLASS_EXPECTED_OUTPUT = "'LABEL_1'"
|
||||
_SEQ_CLASS_EXPECTED_LOSS = 0.69
|
||||
|
||||
# Frame class docstring
|
||||
_FRAME_CLASS_CHECKPOINT = "hf-internal-testing/tiny-random-data2vec-audio-frame"
|
||||
_FRAME_EXPECTED_OUTPUT = [1, 1]
|
||||
|
||||
# Speaker Verification docstring
|
||||
_XVECTOR_CHECKPOINT = "hf-internal-testing/tiny-random-data2vec-xvector"
|
||||
_XVECTOR_EXPECTED_OUTPUT = 1.0
|
||||
|
||||
|
||||
DATA2VEC_AUDIO_PRETRAINED_MODEL_ARCHIVE_LIST = [
|
||||
"facebook/data2vec-audio-base",
|
||||
@ -917,12 +902,12 @@ class Data2VecAudioModel(Data2VecAudioPreTrainedModel):
|
||||
|
||||
@add_start_docstrings_to_model_forward(DATA2VEC_AUDIO_INPUTS_DOCSTRING)
|
||||
@add_code_sample_docstrings(
|
||||
processor_class=_PROCESSOR_FOR_DOC,
|
||||
checkpoint=_CHECKPOINT_FOR_DOC,
|
||||
output_type=Wav2Vec2BaseModelOutput,
|
||||
config_class=_CONFIG_FOR_DOC,
|
||||
modality="audio",
|
||||
expected_output=_EXPECTED_OUTPUT_SHAPE,
|
||||
processor_class="AutoProcessor",
|
||||
)
|
||||
def forward(
|
||||
self,
|
||||
@ -981,7 +966,6 @@ class Data2VecAudioModel(Data2VecAudioPreTrainedModel):
|
||||
"""Data2VecAudio Model with a `language modeling` head on top for Connectionist Temporal Classification (CTC).""",
|
||||
DATA2VEC_AUDIO_START_DOCSTRING,
|
||||
)
|
||||
# Copied from transformers.models.wav2vec2.modeling_wav2vec2.Wav2Vec2ForCTC with Wav2Vec2->Data2VecAudio, wav2vec2->data2vec_audio, WAV_2_VEC_2->DATA2VEC_AUDIO
|
||||
class Data2VecAudioForCTC(Data2VecAudioPreTrainedModel):
|
||||
def __init__(self, config):
|
||||
super().__init__(config)
|
||||
@ -1025,13 +1009,13 @@ class Data2VecAudioForCTC(Data2VecAudioPreTrainedModel):
|
||||
|
||||
@add_start_docstrings_to_model_forward(DATA2VEC_AUDIO_INPUTS_DOCSTRING)
|
||||
@add_code_sample_docstrings(
|
||||
processor_class=_PROCESSOR_FOR_DOC,
|
||||
checkpoint=_CHECKPOINT_FOR_DOC,
|
||||
output_type=CausalLMOutput,
|
||||
config_class=_CONFIG_FOR_DOC,
|
||||
expected_output=_CTC_EXPECTED_OUTPUT,
|
||||
expected_loss=_CTC_EXPECTED_LOSS,
|
||||
)
|
||||
# Copied from transformers.models.wav2vec2.modeling_wav2vec2.Wav2Vec2ForCTC.forward with wav2vec2->data2vec_audio
|
||||
def forward(
|
||||
self,
|
||||
input_values: Optional[torch.Tensor],
|
||||
@ -1112,7 +1096,6 @@ class Data2VecAudioForCTC(Data2VecAudioPreTrainedModel):
|
||||
""",
|
||||
DATA2VEC_AUDIO_START_DOCSTRING,
|
||||
)
|
||||
# Copied from transformers.models.wav2vec2.modeling_wav2vec2.Wav2Vec2ForSequenceClassification with Wav2Vec2->Data2VecAudio, wav2vec2->data2vec_audio, WAV_2_VEC_2->DATA2VEC_AUDIO
|
||||
class Data2VecAudioForSequenceClassification(Data2VecAudioPreTrainedModel):
|
||||
def __init__(self, config):
|
||||
super().__init__(config)
|
||||
@ -1160,14 +1143,12 @@ class Data2VecAudioForSequenceClassification(Data2VecAudioPreTrainedModel):
|
||||
|
||||
@add_start_docstrings_to_model_forward(DATA2VEC_AUDIO_INPUTS_DOCSTRING)
|
||||
@add_code_sample_docstrings(
|
||||
processor_class=_FEAT_EXTRACTOR_FOR_DOC,
|
||||
checkpoint=_SEQ_CLASS_CHECKPOINT,
|
||||
checkpoint=_CHECKPOINT_FOR_DOC,
|
||||
output_type=SequenceClassifierOutput,
|
||||
config_class=_CONFIG_FOR_DOC,
|
||||
modality="audio",
|
||||
expected_output=_SEQ_CLASS_EXPECTED_OUTPUT,
|
||||
expected_loss=_SEQ_CLASS_EXPECTED_LOSS,
|
||||
)
|
||||
# Copied from transformers.models.wav2vec2.modeling_wav2vec2.Wav2Vec2ForSequenceClassification.forward with wav2vec2->data2vec_audio
|
||||
def forward(
|
||||
self,
|
||||
input_values: Optional[torch.Tensor],
|
||||
@ -1236,7 +1217,6 @@ class Data2VecAudioForSequenceClassification(Data2VecAudioPreTrainedModel):
|
||||
""",
|
||||
DATA2VEC_AUDIO_START_DOCSTRING,
|
||||
)
|
||||
# Copied from transformers.models.wav2vec2.modeling_wav2vec2.Wav2Vec2ForAudioFrameClassification with Wav2Vec2->Data2VecAudio, wav2vec2->data2vec_audio, WAV_2_VEC_2->DATA2VEC_AUDIO
|
||||
class Data2VecAudioForAudioFrameClassification(Data2VecAudioPreTrainedModel):
|
||||
def __init__(self, config):
|
||||
super().__init__(config)
|
||||
@ -1284,13 +1264,12 @@ class Data2VecAudioForAudioFrameClassification(Data2VecAudioPreTrainedModel):
|
||||
|
||||
@add_start_docstrings_to_model_forward(DATA2VEC_AUDIO_INPUTS_DOCSTRING)
|
||||
@add_code_sample_docstrings(
|
||||
processor_class=_FEAT_EXTRACTOR_FOR_DOC,
|
||||
checkpoint=_FRAME_CLASS_CHECKPOINT,
|
||||
checkpoint=_CHECKPOINT_FOR_DOC,
|
||||
output_type=TokenClassifierOutput,
|
||||
config_class=_CONFIG_FOR_DOC,
|
||||
modality="audio",
|
||||
expected_output=_FRAME_EXPECTED_OUTPUT,
|
||||
)
|
||||
# Copied from transformers.models.wav2vec2.modeling_wav2vec2.Wav2Vec2ForAudioFrameClassification.forward with wav2vec2->data2vec_audio
|
||||
def forward(
|
||||
self,
|
||||
input_values: Optional[torch.Tensor],
|
||||
@ -1402,7 +1381,6 @@ class TDNNLayer(nn.Module):
|
||||
""",
|
||||
DATA2VEC_AUDIO_START_DOCSTRING,
|
||||
)
|
||||
# Copied from transformers.models.wav2vec2.modeling_wav2vec2.Wav2Vec2ForXVector with Wav2Vec2->Data2VecAudio, wav2vec2->data2vec_audio, WAV_2_VEC_2->DATA2VEC_AUDIO
|
||||
class Data2VecAudioForXVector(Data2VecAudioPreTrainedModel):
|
||||
def __init__(self, config):
|
||||
super().__init__(config)
|
||||
@ -1467,13 +1445,12 @@ class Data2VecAudioForXVector(Data2VecAudioPreTrainedModel):
|
||||
|
||||
@add_start_docstrings_to_model_forward(DATA2VEC_AUDIO_INPUTS_DOCSTRING)
|
||||
@add_code_sample_docstrings(
|
||||
processor_class=_FEAT_EXTRACTOR_FOR_DOC,
|
||||
checkpoint=_XVECTOR_CHECKPOINT,
|
||||
checkpoint=_CHECKPOINT_FOR_DOC,
|
||||
output_type=XVectorOutput,
|
||||
config_class=_CONFIG_FOR_DOC,
|
||||
modality="audio",
|
||||
expected_output=_XVECTOR_EXPECTED_OUTPUT,
|
||||
)
|
||||
# Copied from transformers.models.wav2vec2.modeling_wav2vec2.Wav2Vec2ForXVector.forward with wav2vec2->data2vec_audio
|
||||
def forward(
|
||||
self,
|
||||
input_values: Optional[torch.Tensor],
|
||||
|
@ -38,7 +38,6 @@ from .configuration_deberta import DebertaConfig
|
||||
|
||||
logger = logging.get_logger(__name__)
|
||||
_CONFIG_FOR_DOC = "DebertaConfig"
|
||||
_TOKENIZER_FOR_DOC = "DebertaTokenizer"
|
||||
_CHECKPOINT_FOR_DOC = "microsoft/deberta-base"
|
||||
|
||||
# Masked LM docstring
|
||||
@ -46,14 +45,6 @@ _CHECKPOINT_FOR_MASKED_LM = "lsanochkin/deberta-large-feedback"
|
||||
_MASKED_LM_EXPECTED_OUTPUT = "' Paris'"
|
||||
_MASKED_LM_EXPECTED_LOSS = "0.54"
|
||||
|
||||
# TokenClassification docstring
|
||||
_CHECKPOINT_FOR_TOKEN_CLASSIFICATION = "dbsamu/deberta-base-finetuned-ner"
|
||||
_TOKEN_CLASS_EXPECTED_OUTPUT = (
|
||||
"['LABEL_0', 'LABEL_0', 'LABEL_0', 'LABEL_0', 'LABEL_0', 'LABEL_0', 'LABEL_0', 'LABEL_0', 'LABEL_0', 'LABEL_0',"
|
||||
" 'LABEL_0', 'LABEL_0']"
|
||||
)
|
||||
_TOKEN_CLASS_EXPECTED_LOSS = 0.04
|
||||
|
||||
# QuestionAnswering docstring
|
||||
_CHECKPOINT_FOR_QA = "Palak/microsoft_deberta-large_squad"
|
||||
_QA_EXPECTED_OUTPUT = "' a nice puppet'"
|
||||
@ -61,11 +52,6 @@ _QA_EXPECTED_LOSS = 0.14
|
||||
_QA_TARGET_START_INDEX = 12
|
||||
_QA_TARGET_END_INDEX = 14
|
||||
|
||||
# SequenceClassification docstring
|
||||
_CHECKPOINT_FOR_SEQUENCE_CLASSIFICATION = "hf-internal-testing/tiny-random-deberta"
|
||||
_SEQ_CLASS_EXPECTED_OUTPUT = "'LABEL_0'"
|
||||
_SEQ_CLASS_EXPECTED_LOSS = "0.69"
|
||||
|
||||
|
||||
DEBERTA_PRETRAINED_MODEL_ARCHIVE_LIST = [
|
||||
"microsoft/deberta-base",
|
||||
@ -950,7 +936,6 @@ class DebertaModel(DebertaPreTrainedModel):
|
||||
|
||||
@add_start_docstrings_to_model_forward(DEBERTA_INPUTS_DOCSTRING.format("batch_size, sequence_length"))
|
||||
@add_code_sample_docstrings(
|
||||
processor_class=_TOKENIZER_FOR_DOC,
|
||||
checkpoint=_CHECKPOINT_FOR_DOC,
|
||||
output_type=BaseModelOutput,
|
||||
config_class=_CONFIG_FOR_DOC,
|
||||
@ -1057,7 +1042,6 @@ class DebertaForMaskedLM(DebertaPreTrainedModel):
|
||||
|
||||
@add_start_docstrings_to_model_forward(DEBERTA_INPUTS_DOCSTRING.format("batch_size, sequence_length"))
|
||||
@add_code_sample_docstrings(
|
||||
processor_class=_TOKENIZER_FOR_DOC,
|
||||
checkpoint=_CHECKPOINT_FOR_MASKED_LM,
|
||||
output_type=MaskedLMOutput,
|
||||
config_class=_CONFIG_FOR_DOC,
|
||||
@ -1201,12 +1185,9 @@ class DebertaForSequenceClassification(DebertaPreTrainedModel):
|
||||
|
||||
@add_start_docstrings_to_model_forward(DEBERTA_INPUTS_DOCSTRING.format("batch_size, sequence_length"))
|
||||
@add_code_sample_docstrings(
|
||||
processor_class=_TOKENIZER_FOR_DOC,
|
||||
checkpoint=_CHECKPOINT_FOR_SEQUENCE_CLASSIFICATION,
|
||||
checkpoint=_CHECKPOINT_FOR_DOC,
|
||||
output_type=SequenceClassifierOutput,
|
||||
config_class=_CONFIG_FOR_DOC,
|
||||
expected_output=_SEQ_CLASS_EXPECTED_OUTPUT,
|
||||
expected_loss=_SEQ_CLASS_EXPECTED_LOSS,
|
||||
)
|
||||
def forward(
|
||||
self,
|
||||
@ -1311,12 +1292,9 @@ class DebertaForTokenClassification(DebertaPreTrainedModel):
|
||||
|
||||
@add_start_docstrings_to_model_forward(DEBERTA_INPUTS_DOCSTRING.format("batch_size, sequence_length"))
|
||||
@add_code_sample_docstrings(
|
||||
processor_class=_TOKENIZER_FOR_DOC,
|
||||
checkpoint=_CHECKPOINT_FOR_TOKEN_CLASSIFICATION,
|
||||
checkpoint=_CHECKPOINT_FOR_DOC,
|
||||
output_type=TokenClassifierOutput,
|
||||
config_class=_CONFIG_FOR_DOC,
|
||||
expected_output=_TOKEN_CLASS_EXPECTED_OUTPUT,
|
||||
expected_loss=_TOKEN_CLASS_EXPECTED_LOSS,
|
||||
)
|
||||
def forward(
|
||||
self,
|
||||
@ -1388,7 +1366,6 @@ class DebertaForQuestionAnswering(DebertaPreTrainedModel):
|
||||
|
||||
@add_start_docstrings_to_model_forward(DEBERTA_INPUTS_DOCSTRING.format("batch_size, sequence_length"))
|
||||
@add_code_sample_docstrings(
|
||||
processor_class=_TOKENIZER_FOR_DOC,
|
||||
checkpoint=_CHECKPOINT_FOR_QA,
|
||||
output_type=QuestionAnsweringModelOutput,
|
||||
config_class=_CONFIG_FOR_DOC,
|
||||
|
@ -40,34 +40,10 @@ from .configuration_deberta_v2 import DebertaV2Config
|
||||
logger = logging.get_logger(__name__)
|
||||
|
||||
_CONFIG_FOR_DOC = "DebertaV2Config"
|
||||
_TOKENIZER_FOR_DOC = "DebertaV2Tokenizer"
|
||||
_CHECKPOINT_FOR_DOC = "microsoft/deberta-v2-xlarge"
|
||||
|
||||
# Masked LM docstring
|
||||
_CHECKPOINT_FOR_MASKED_LM = "hf-internal-testing/tiny-random-deberta-v2"
|
||||
_MASKED_LM_EXPECTED_OUTPUT = "'enberry'"
|
||||
_MASKED_LM_EXPECTED_LOSS = "11.85"
|
||||
|
||||
# TokenClassification docstring
|
||||
_CHECKPOINT_FOR_TOKEN_CLASSIFICATION = "hf-internal-testing/tiny-random-deberta-v2"
|
||||
_TOKEN_CLASS_EXPECTED_OUTPUT = (
|
||||
"['LABEL_0', 'LABEL_0', 'LABEL_1', 'LABEL_0', 'LABEL_0', 'LABEL_1', 'LABEL_0', 'LABEL_0', 'LABEL_0', 'LABEL_0',"
|
||||
" 'LABEL_0', 'LABEL_0']"
|
||||
)
|
||||
_TOKEN_CLASS_EXPECTED_LOSS = 0.61
|
||||
|
||||
# QuestionAnswering docstring
|
||||
_CHECKPOINT_FOR_QA = "hf-internal-testing/tiny-random-deberta-v2"
|
||||
_QA_EXPECTED_OUTPUT = "'was Jim Henson? Jim Henson was'"
|
||||
_QA_EXPECTED_LOSS = 2.47
|
||||
_QA_TARGET_START_INDEX = 2
|
||||
_QA_TARGET_END_INDEX = 9
|
||||
|
||||
# SequenceClassification docstring
|
||||
_CHECKPOINT_FOR_SEQUENCE_CLASSIFICATION = "hf-internal-testing/tiny-random-deberta-v2"
|
||||
_SEQ_CLASS_EXPECTED_OUTPUT = "'LABEL_1'"
|
||||
_SEQ_CLASS_EXPECTED_LOSS = "0.69"
|
||||
|
||||
DEBERTA_V2_PRETRAINED_MODEL_ARCHIVE_LIST = [
|
||||
"microsoft/deberta-v2-xlarge",
|
||||
"microsoft/deberta-v2-xxlarge",
|
||||
@ -1060,7 +1036,6 @@ class DebertaV2Model(DebertaV2PreTrainedModel):
|
||||
|
||||
@add_start_docstrings_to_model_forward(DEBERTA_INPUTS_DOCSTRING.format("batch_size, sequence_length"))
|
||||
@add_code_sample_docstrings(
|
||||
processor_class=_TOKENIZER_FOR_DOC,
|
||||
checkpoint=_CHECKPOINT_FOR_DOC,
|
||||
output_type=BaseModelOutput,
|
||||
config_class=_CONFIG_FOR_DOC,
|
||||
@ -1146,7 +1121,6 @@ class DebertaV2Model(DebertaV2PreTrainedModel):
|
||||
|
||||
|
||||
@add_start_docstrings("""DeBERTa Model with a `language modeling` head on top.""", DEBERTA_START_DOCSTRING)
|
||||
# Copied from transformers.models.deberta.modeling_deberta.DebertaForMaskedLM with Deberta->DebertaV2
|
||||
class DebertaV2ForMaskedLM(DebertaV2PreTrainedModel):
|
||||
_keys_to_ignore_on_load_unexpected = [r"pooler"]
|
||||
_keys_to_ignore_on_load_missing = [r"position_ids", r"predictions.decoder.bias", "cls.predictions.decoder.weight"]
|
||||
@ -1168,14 +1142,12 @@ class DebertaV2ForMaskedLM(DebertaV2PreTrainedModel):
|
||||
|
||||
@add_start_docstrings_to_model_forward(DEBERTA_INPUTS_DOCSTRING.format("batch_size, sequence_length"))
|
||||
@add_code_sample_docstrings(
|
||||
processor_class=_TOKENIZER_FOR_DOC,
|
||||
checkpoint=_CHECKPOINT_FOR_MASKED_LM,
|
||||
checkpoint=_CHECKPOINT_FOR_DOC,
|
||||
output_type=MaskedLMOutput,
|
||||
config_class=_CONFIG_FOR_DOC,
|
||||
mask="[MASK]",
|
||||
expected_output=_MASKED_LM_EXPECTED_OUTPUT,
|
||||
expected_loss=_MASKED_LM_EXPECTED_LOSS,
|
||||
)
|
||||
# Copied from transformers.models.deberta.modeling_deberta.DebertaForMaskedLM.forward with Deberta->DebertaV2
|
||||
def forward(
|
||||
self,
|
||||
input_ids: Optional[torch.Tensor] = None,
|
||||
@ -1285,7 +1257,6 @@ class DebertaV2OnlyMLMHead(nn.Module):
|
||||
""",
|
||||
DEBERTA_START_DOCSTRING,
|
||||
)
|
||||
# Copied from transformers.models.deberta.modeling_deberta.DebertaForSequenceClassification with Deberta->DebertaV2
|
||||
class DebertaV2ForSequenceClassification(DebertaV2PreTrainedModel):
|
||||
def __init__(self, config):
|
||||
super().__init__(config)
|
||||
@ -1313,13 +1284,11 @@ class DebertaV2ForSequenceClassification(DebertaV2PreTrainedModel):
|
||||
|
||||
@add_start_docstrings_to_model_forward(DEBERTA_INPUTS_DOCSTRING.format("batch_size, sequence_length"))
|
||||
@add_code_sample_docstrings(
|
||||
processor_class=_TOKENIZER_FOR_DOC,
|
||||
checkpoint=_CHECKPOINT_FOR_SEQUENCE_CLASSIFICATION,
|
||||
checkpoint=_CHECKPOINT_FOR_DOC,
|
||||
output_type=SequenceClassifierOutput,
|
||||
config_class=_CONFIG_FOR_DOC,
|
||||
expected_output=_SEQ_CLASS_EXPECTED_OUTPUT,
|
||||
expected_loss=_SEQ_CLASS_EXPECTED_LOSS,
|
||||
)
|
||||
# Copied from transformers.models.deberta.modeling_deberta.DebertaForSequenceClassification.forward with Deberta->DebertaV2
|
||||
def forward(
|
||||
self,
|
||||
input_ids: Optional[torch.Tensor] = None,
|
||||
@ -1424,12 +1393,9 @@ class DebertaV2ForTokenClassification(DebertaV2PreTrainedModel):
|
||||
|
||||
@add_start_docstrings_to_model_forward(DEBERTA_INPUTS_DOCSTRING.format("batch_size, sequence_length"))
|
||||
@add_code_sample_docstrings(
|
||||
processor_class=_TOKENIZER_FOR_DOC,
|
||||
checkpoint=_CHECKPOINT_FOR_TOKEN_CLASSIFICATION,
|
||||
checkpoint=_CHECKPOINT_FOR_DOC,
|
||||
output_type=TokenClassifierOutput,
|
||||
config_class=_CONFIG_FOR_DOC,
|
||||
expected_output=_TOKEN_CLASS_EXPECTED_OUTPUT,
|
||||
expected_loss=_TOKEN_CLASS_EXPECTED_LOSS,
|
||||
)
|
||||
def forward(
|
||||
self,
|
||||
@ -1486,7 +1452,6 @@ class DebertaV2ForTokenClassification(DebertaV2PreTrainedModel):
|
||||
""",
|
||||
DEBERTA_START_DOCSTRING,
|
||||
)
|
||||
# Copied from transformers.models.deberta.modeling_deberta.DebertaForQuestionAnswering with Deberta->DebertaV2
|
||||
class DebertaV2ForQuestionAnswering(DebertaV2PreTrainedModel):
|
||||
_keys_to_ignore_on_load_unexpected = [r"pooler"]
|
||||
|
||||
@ -1502,15 +1467,13 @@ class DebertaV2ForQuestionAnswering(DebertaV2PreTrainedModel):
|
||||
|
||||
@add_start_docstrings_to_model_forward(DEBERTA_INPUTS_DOCSTRING.format("batch_size, sequence_length"))
|
||||
@add_code_sample_docstrings(
|
||||
processor_class=_TOKENIZER_FOR_DOC,
|
||||
checkpoint=_CHECKPOINT_FOR_QA,
|
||||
checkpoint=_CHECKPOINT_FOR_DOC,
|
||||
output_type=QuestionAnsweringModelOutput,
|
||||
config_class=_CONFIG_FOR_DOC,
|
||||
expected_output=_QA_EXPECTED_OUTPUT,
|
||||
expected_loss=_QA_EXPECTED_LOSS,
|
||||
qa_target_start_index=_QA_TARGET_START_INDEX,
|
||||
qa_target_end_index=_QA_TARGET_END_INDEX,
|
||||
)
|
||||
# Copied from transformers.models.deberta.modeling_deberta.DebertaForQuestionAnswering.forward with Deberta->DebertaV2
|
||||
def forward(
|
||||
self,
|
||||
input_ids: Optional[torch.Tensor] = None,
|
||||
@ -1617,7 +1580,6 @@ class DebertaV2ForMultipleChoice(DebertaV2PreTrainedModel):
|
||||
|
||||
@add_start_docstrings_to_model_forward(DEBERTA_INPUTS_DOCSTRING.format("batch_size, sequence_length"))
|
||||
@add_code_sample_docstrings(
|
||||
processor_class=_TOKENIZER_FOR_DOC,
|
||||
checkpoint=_CHECKPOINT_FOR_DOC,
|
||||
output_type=MultipleChoiceModelOutput,
|
||||
config_class=_CONFIG_FOR_DOC,
|
||||
|
@ -58,7 +58,6 @@ logger = logging.get_logger(__name__)
|
||||
|
||||
_CHECKPOINT_FOR_DOC = "google/mobilebert-uncased"
|
||||
_CONFIG_FOR_DOC = "MobileBertConfig"
|
||||
_TOKENIZER_FOR_DOC = "MobileBertTokenizer"
|
||||
|
||||
# TokenClassification docstring
|
||||
_CHECKPOINT_FOR_TOKEN_CLASSIFICATION = "mrm8488/mobilebert-finetuned-ner"
|
||||
@ -842,7 +841,6 @@ class MobileBertModel(MobileBertPreTrainedModel):
|
||||
|
||||
@add_start_docstrings_to_model_forward(MOBILEBERT_INPUTS_DOCSTRING.format("batch_size, sequence_length"))
|
||||
@add_code_sample_docstrings(
|
||||
processor_class=_TOKENIZER_FOR_DOC,
|
||||
checkpoint=_CHECKPOINT_FOR_DOC,
|
||||
output_type=BaseModelOutputWithPooling,
|
||||
config_class=_CONFIG_FOR_DOC,
|
||||
@ -1069,7 +1067,6 @@ class MobileBertForMaskedLM(MobileBertPreTrainedModel):
|
||||
|
||||
@add_start_docstrings_to_model_forward(MOBILEBERT_INPUTS_DOCSTRING.format("batch_size, sequence_length"))
|
||||
@add_code_sample_docstrings(
|
||||
processor_class=_TOKENIZER_FOR_DOC,
|
||||
checkpoint=_CHECKPOINT_FOR_DOC,
|
||||
output_type=MaskedLMOutput,
|
||||
config_class=_CONFIG_FOR_DOC,
|
||||
@ -1265,7 +1262,6 @@ class MobileBertForSequenceClassification(MobileBertPreTrainedModel):
|
||||
|
||||
@add_start_docstrings_to_model_forward(MOBILEBERT_INPUTS_DOCSTRING.format("batch_size, sequence_length"))
|
||||
@add_code_sample_docstrings(
|
||||
processor_class=_TOKENIZER_FOR_DOC,
|
||||
checkpoint=_CHECKPOINT_FOR_SEQUENCE_CLASSIFICATION,
|
||||
output_type=SequenceClassifierOutput,
|
||||
config_class=_CONFIG_FOR_DOC,
|
||||
@ -1368,7 +1364,6 @@ class MobileBertForQuestionAnswering(MobileBertPreTrainedModel):
|
||||
|
||||
@add_start_docstrings_to_model_forward(MOBILEBERT_INPUTS_DOCSTRING.format("batch_size, sequence_length"))
|
||||
@add_code_sample_docstrings(
|
||||
processor_class=_TOKENIZER_FOR_DOC,
|
||||
checkpoint=_CHECKPOINT_FOR_QA,
|
||||
output_type=QuestionAnsweringModelOutput,
|
||||
config_class=_CONFIG_FOR_DOC,
|
||||
@ -1478,7 +1473,6 @@ class MobileBertForMultipleChoice(MobileBertPreTrainedModel):
|
||||
MOBILEBERT_INPUTS_DOCSTRING.format("batch_size, num_choices, sequence_length")
|
||||
)
|
||||
@add_code_sample_docstrings(
|
||||
processor_class=_TOKENIZER_FOR_DOC,
|
||||
checkpoint=_CHECKPOINT_FOR_DOC,
|
||||
output_type=MultipleChoiceModelOutput,
|
||||
config_class=_CONFIG_FOR_DOC,
|
||||
@ -1578,7 +1572,6 @@ class MobileBertForTokenClassification(MobileBertPreTrainedModel):
|
||||
|
||||
@add_start_docstrings_to_model_forward(MOBILEBERT_INPUTS_DOCSTRING.format("batch_size, sequence_length"))
|
||||
@add_code_sample_docstrings(
|
||||
processor_class=_TOKENIZER_FOR_DOC,
|
||||
checkpoint=_CHECKPOINT_FOR_TOKEN_CLASSIFICATION,
|
||||
output_type=TokenClassifierOutput,
|
||||
config_class=_CONFIG_FOR_DOC,
|
||||
|
@ -150,10 +150,10 @@ PT_TOKEN_CLASSIFICATION_SAMPLE = r"""
|
||||
Example:
|
||||
|
||||
```python
|
||||
>>> from transformers import {processor_class}, {model_class}
|
||||
>>> from transformers import AutoTokenizer, {model_class}
|
||||
>>> import torch
|
||||
|
||||
>>> tokenizer = {processor_class}.from_pretrained("{checkpoint}")
|
||||
>>> tokenizer = AutoTokenizer.from_pretrained("{checkpoint}")
|
||||
>>> model = {model_class}.from_pretrained("{checkpoint}")
|
||||
|
||||
>>> inputs = tokenizer(
|
||||
@ -171,9 +171,7 @@ PT_TOKEN_CLASSIFICATION_SAMPLE = r"""
|
||||
>>> predicted_tokens_classes = [model.config.id2label[t.item()] for t in predicted_token_class_ids[0]]
|
||||
>>> predicted_tokens_classes
|
||||
{expected_output}
|
||||
```
|
||||
|
||||
```python
|
||||
>>> labels = predicted_token_class_ids
|
||||
>>> loss = model(**inputs, labels=labels).loss
|
||||
>>> round(loss.item(), 2)
|
||||
@ -185,10 +183,10 @@ PT_QUESTION_ANSWERING_SAMPLE = r"""
|
||||
Example:
|
||||
|
||||
```python
|
||||
>>> from transformers import {processor_class}, {model_class}
|
||||
>>> from transformers import AutoTokenizer, {model_class}
|
||||
>>> import torch
|
||||
|
||||
>>> tokenizer = {processor_class}.from_pretrained("{checkpoint}")
|
||||
>>> tokenizer = AutoTokenizer.from_pretrained("{checkpoint}")
|
||||
>>> model = {model_class}.from_pretrained("{checkpoint}")
|
||||
|
||||
>>> question, text = "Who was Jim Henson?", "Jim Henson was a nice puppet"
|
||||
@ -203,9 +201,7 @@ PT_QUESTION_ANSWERING_SAMPLE = r"""
|
||||
>>> predict_answer_tokens = inputs.input_ids[0, answer_start_index : answer_end_index + 1]
|
||||
>>> tokenizer.decode(predict_answer_tokens, skip_special_tokens=True)
|
||||
{expected_output}
|
||||
```
|
||||
|
||||
```python
|
||||
>>> # target is "nice puppet"
|
||||
>>> target_start_index = torch.tensor([{qa_target_start_index}])
|
||||
>>> target_end_index = torch.tensor([{qa_target_end_index}])
|
||||
@ -222,9 +218,9 @@ PT_SEQUENCE_CLASSIFICATION_SAMPLE = r"""
|
||||
|
||||
```python
|
||||
>>> import torch
|
||||
>>> from transformers import {processor_class}, {model_class}
|
||||
>>> from transformers import AutoTokenizer, {model_class}
|
||||
|
||||
>>> tokenizer = {processor_class}.from_pretrained("{checkpoint}")
|
||||
>>> tokenizer = AutoTokenizer.from_pretrained("{checkpoint}")
|
||||
>>> model = {model_class}.from_pretrained("{checkpoint}")
|
||||
|
||||
>>> inputs = tokenizer("Hello, my dog is cute", return_tensors="pt")
|
||||
@ -235,9 +231,7 @@ PT_SEQUENCE_CLASSIFICATION_SAMPLE = r"""
|
||||
>>> predicted_class_id = logits.argmax().item()
|
||||
>>> model.config.id2label[predicted_class_id]
|
||||
{expected_output}
|
||||
```
|
||||
|
||||
```python
|
||||
>>> # To train a model on `num_labels` classes, you can pass `num_labels=num_labels` to `.from_pretrained(...)`
|
||||
>>> num_labels = len(model.config.id2label)
|
||||
>>> model = {model_class}.from_pretrained("{checkpoint}", num_labels=num_labels)
|
||||
@ -252,9 +246,9 @@ PT_SEQUENCE_CLASSIFICATION_SAMPLE = r"""
|
||||
|
||||
```python
|
||||
>>> import torch
|
||||
>>> from transformers import {processor_class}, {model_class}
|
||||
>>> from transformers import AutoTokenizer, {model_class}
|
||||
|
||||
>>> tokenizer = {processor_class}.from_pretrained("{checkpoint}")
|
||||
>>> tokenizer = AutoTokenizer.from_pretrained("{checkpoint}")
|
||||
>>> model = {model_class}.from_pretrained("{checkpoint}", problem_type="multi_label_classification")
|
||||
|
||||
>>> inputs = tokenizer("Hello, my dog is cute", return_tensors="pt")
|
||||
@ -262,23 +256,16 @@ PT_SEQUENCE_CLASSIFICATION_SAMPLE = r"""
|
||||
>>> with torch.no_grad():
|
||||
... logits = model(**inputs).logits
|
||||
|
||||
>>> predicted_class_id = logits.argmax().item()
|
||||
>>> model.config.id2label[predicted_class_id]
|
||||
{expected_output}
|
||||
```
|
||||
>>> predicted_class_ids = torch.arange(0, logits.shape[-1])[torch.sigmoid(logits).squeeze() > 0.5]
|
||||
|
||||
```python
|
||||
>>> # To train a model on `num_labels` classes, you can pass `num_labels=num_labels` to `.from_pretrained(...)`
|
||||
>>> num_labels = len(model.config.id2label)
|
||||
>>> model = {model_class}.from_pretrained(
|
||||
... "{checkpoint}", num_labels=num_labels, problem_type="multi_label_classification"
|
||||
... )
|
||||
|
||||
>>> labels = torch.nn.functional.one_hot(torch.tensor([predicted_class_id]), num_classes=num_labels).to(
|
||||
... torch.float
|
||||
... )
|
||||
>>> labels = torch.nn.functional.one_hot(torch.tensor(predicted_class_ids), num_classes=num_labels).to(torch.float)
|
||||
>>> loss = model(**inputs, labels=labels).loss
|
||||
>>> loss.backward() # doctest: +IGNORE_RESULT
|
||||
```
|
||||
"""
|
||||
|
||||
@ -286,10 +273,10 @@ PT_MASKED_LM_SAMPLE = r"""
|
||||
Example:
|
||||
|
||||
```python
|
||||
>>> from transformers import {processor_class}, {model_class}
|
||||
>>> from transformers import AutoTokenizer, {model_class}
|
||||
>>> import torch
|
||||
|
||||
>>> tokenizer = {processor_class}.from_pretrained("{checkpoint}")
|
||||
>>> tokenizer = AutoTokenizer.from_pretrained("{checkpoint}")
|
||||
>>> model = {model_class}.from_pretrained("{checkpoint}")
|
||||
|
||||
>>> inputs = tokenizer("The capital of France is {mask}.", return_tensors="pt")
|
||||
@ -303,9 +290,7 @@ PT_MASKED_LM_SAMPLE = r"""
|
||||
>>> predicted_token_id = logits[0, mask_token_index].argmax(axis=-1)
|
||||
>>> tokenizer.decode(predicted_token_id)
|
||||
{expected_output}
|
||||
```
|
||||
|
||||
```python
|
||||
>>> labels = tokenizer("The capital of France is Paris.", return_tensors="pt")["input_ids"]
|
||||
>>> # mask labels of non-{mask} tokens
|
||||
>>> labels = torch.where(inputs.input_ids == tokenizer.mask_token_id, labels, -100)
|
||||
@ -320,10 +305,10 @@ PT_BASE_MODEL_SAMPLE = r"""
|
||||
Example:
|
||||
|
||||
```python
|
||||
>>> from transformers import {processor_class}, {model_class}
|
||||
>>> from transformers import AutoTokenizer, {model_class}
|
||||
>>> import torch
|
||||
|
||||
>>> tokenizer = {processor_class}.from_pretrained("{checkpoint}")
|
||||
>>> tokenizer = AutoTokenizer.from_pretrained("{checkpoint}")
|
||||
>>> model = {model_class}.from_pretrained("{checkpoint}")
|
||||
|
||||
>>> inputs = tokenizer("Hello, my dog is cute", return_tensors="pt")
|
||||
@ -337,10 +322,10 @@ PT_MULTIPLE_CHOICE_SAMPLE = r"""
|
||||
Example:
|
||||
|
||||
```python
|
||||
>>> from transformers import {processor_class}, {model_class}
|
||||
>>> from transformers import AutoTokenizer, {model_class}
|
||||
>>> import torch
|
||||
|
||||
>>> tokenizer = {processor_class}.from_pretrained("{checkpoint}")
|
||||
>>> tokenizer = AutoTokenizer.from_pretrained("{checkpoint}")
|
||||
>>> model = {model_class}.from_pretrained("{checkpoint}")
|
||||
|
||||
>>> prompt = "In Italy, pizza served in formal settings, such as at a restaurant, is presented unsliced."
|
||||
@ -362,9 +347,9 @@ PT_CAUSAL_LM_SAMPLE = r"""
|
||||
|
||||
```python
|
||||
>>> import torch
|
||||
>>> from transformers import {processor_class}, {model_class}
|
||||
>>> from transformers import AutoTokenizer, {model_class}
|
||||
|
||||
>>> tokenizer = {processor_class}.from_pretrained("{checkpoint}")
|
||||
>>> tokenizer = AutoTokenizer.from_pretrained("{checkpoint}")
|
||||
>>> model = {model_class}.from_pretrained("{checkpoint}")
|
||||
|
||||
>>> inputs = tokenizer("Hello, my dog is cute", return_tensors="pt")
|
||||
@ -378,7 +363,7 @@ PT_SPEECH_BASE_MODEL_SAMPLE = r"""
|
||||
Example:
|
||||
|
||||
```python
|
||||
>>> from transformers import {processor_class}, {model_class}
|
||||
>>> from transformers import AutoProcessor, {model_class}
|
||||
>>> import torch
|
||||
>>> from datasets import load_dataset
|
||||
|
||||
@ -386,7 +371,7 @@ PT_SPEECH_BASE_MODEL_SAMPLE = r"""
|
||||
>>> dataset = dataset.sort("id")
|
||||
>>> sampling_rate = dataset.features["audio"].sampling_rate
|
||||
|
||||
>>> processor = {processor_class}.from_pretrained("{checkpoint}")
|
||||
>>> processor = AutoProcessor.from_pretrained("{checkpoint}")
|
||||
>>> model = {model_class}.from_pretrained("{checkpoint}")
|
||||
|
||||
>>> # audio file is decoded on the fly
|
||||
@ -404,7 +389,7 @@ PT_SPEECH_CTC_SAMPLE = r"""
|
||||
Example:
|
||||
|
||||
```python
|
||||
>>> from transformers import {processor_class}, {model_class}
|
||||
>>> from transformers import AutoProcessor, {model_class}
|
||||
>>> from datasets import load_dataset
|
||||
>>> import torch
|
||||
|
||||
@ -412,7 +397,7 @@ PT_SPEECH_CTC_SAMPLE = r"""
|
||||
>>> dataset = dataset.sort("id")
|
||||
>>> sampling_rate = dataset.features["audio"].sampling_rate
|
||||
|
||||
>>> processor = {processor_class}.from_pretrained("{checkpoint}")
|
||||
>>> processor = AutoProcessor.from_pretrained("{checkpoint}")
|
||||
>>> model = {model_class}.from_pretrained("{checkpoint}")
|
||||
|
||||
>>> # audio file is decoded on the fly
|
||||
@ -425,9 +410,7 @@ PT_SPEECH_CTC_SAMPLE = r"""
|
||||
>>> transcription = processor.batch_decode(predicted_ids)
|
||||
>>> transcription[0]
|
||||
{expected_output}
|
||||
```
|
||||
|
||||
```python
|
||||
>>> inputs["labels"] = processor(text=dataset[0]["text"], return_tensors="pt").input_ids
|
||||
|
||||
>>> # compute loss
|
||||
@ -441,7 +424,7 @@ PT_SPEECH_SEQ_CLASS_SAMPLE = r"""
|
||||
Example:
|
||||
|
||||
```python
|
||||
>>> from transformers import {processor_class}, {model_class}
|
||||
>>> from transformers import AutoFeatureExtractor, {model_class}
|
||||
>>> from datasets import load_dataset
|
||||
>>> import torch
|
||||
|
||||
@ -449,7 +432,7 @@ PT_SPEECH_SEQ_CLASS_SAMPLE = r"""
|
||||
>>> dataset = dataset.sort("id")
|
||||
>>> sampling_rate = dataset.features["audio"].sampling_rate
|
||||
|
||||
>>> feature_extractor = {processor_class}.from_pretrained("{checkpoint}")
|
||||
>>> feature_extractor = AutoFeatureExtractor.from_pretrained("{checkpoint}")
|
||||
>>> model = {model_class}.from_pretrained("{checkpoint}")
|
||||
|
||||
>>> # audio file is decoded on the fly
|
||||
@ -462,9 +445,7 @@ PT_SPEECH_SEQ_CLASS_SAMPLE = r"""
|
||||
>>> predicted_label = model.config.id2label[predicted_class_ids]
|
||||
>>> predicted_label
|
||||
{expected_output}
|
||||
```
|
||||
|
||||
```python
|
||||
>>> # compute loss - target_label is e.g. "down"
|
||||
>>> target_label = model.config.id2label[0]
|
||||
>>> inputs["labels"] = torch.tensor([model.config.label2id[target_label]])
|
||||
@ -479,7 +460,7 @@ PT_SPEECH_FRAME_CLASS_SAMPLE = r"""
|
||||
Example:
|
||||
|
||||
```python
|
||||
>>> from transformers import {processor_class}, {model_class}
|
||||
>>> from transformers import AutoFeatureExtractor, {model_class}
|
||||
>>> from datasets import load_dataset
|
||||
>>> import torch
|
||||
|
||||
@ -487,7 +468,7 @@ PT_SPEECH_FRAME_CLASS_SAMPLE = r"""
|
||||
>>> dataset = dataset.sort("id")
|
||||
>>> sampling_rate = dataset.features["audio"].sampling_rate
|
||||
|
||||
>>> feature_extractor = {processor_class}.from_pretrained("{checkpoint}")
|
||||
>>> feature_extractor = AutoFeatureExtractor.from_pretrained("{checkpoint}")
|
||||
>>> model = {model_class}.from_pretrained("{checkpoint}")
|
||||
|
||||
>>> # audio file is decoded on the fly
|
||||
@ -508,7 +489,7 @@ PT_SPEECH_XVECTOR_SAMPLE = r"""
|
||||
Example:
|
||||
|
||||
```python
|
||||
>>> from transformers import {processor_class}, {model_class}
|
||||
>>> from transformers import AutoFeatureExtractor, {model_class}
|
||||
>>> from datasets import load_dataset
|
||||
>>> import torch
|
||||
|
||||
@ -516,7 +497,7 @@ PT_SPEECH_XVECTOR_SAMPLE = r"""
|
||||
>>> dataset = dataset.sort("id")
|
||||
>>> sampling_rate = dataset.features["audio"].sampling_rate
|
||||
|
||||
>>> feature_extractor = {processor_class}.from_pretrained("{checkpoint}")
|
||||
>>> feature_extractor = AutoFeatureExtractor.from_pretrained("{checkpoint}")
|
||||
>>> model = {model_class}.from_pretrained("{checkpoint}")
|
||||
|
||||
>>> # audio file is decoded on the fly
|
||||
@ -543,17 +524,17 @@ PT_VISION_BASE_MODEL_SAMPLE = r"""
|
||||
Example:
|
||||
|
||||
```python
|
||||
>>> from transformers import {processor_class}, {model_class}
|
||||
>>> from transformers import AutoImageProcessor, {model_class}
|
||||
>>> import torch
|
||||
>>> from datasets import load_dataset
|
||||
|
||||
>>> dataset = load_dataset("huggingface/cats-image")
|
||||
>>> image = dataset["test"]["image"][0]
|
||||
|
||||
>>> feature_extractor = {processor_class}.from_pretrained("{checkpoint}")
|
||||
>>> image_processor = AutoImageProcessor.from_pretrained("{checkpoint}")
|
||||
>>> model = {model_class}.from_pretrained("{checkpoint}")
|
||||
|
||||
>>> inputs = feature_extractor(image, return_tensors="pt")
|
||||
>>> inputs = image_processor(image, return_tensors="pt")
|
||||
|
||||
>>> with torch.no_grad():
|
||||
... outputs = model(**inputs)
|
||||
@ -568,17 +549,17 @@ PT_VISION_SEQ_CLASS_SAMPLE = r"""
|
||||
Example:
|
||||
|
||||
```python
|
||||
>>> from transformers import {processor_class}, {model_class}
|
||||
>>> from transformers import AutoImageProcessor, {model_class}
|
||||
>>> import torch
|
||||
>>> from datasets import load_dataset
|
||||
|
||||
>>> dataset = load_dataset("huggingface/cats-image")
|
||||
>>> image = dataset["test"]["image"][0]
|
||||
|
||||
>>> feature_extractor = {processor_class}.from_pretrained("{checkpoint}")
|
||||
>>> image_processor = AutoImageProcessor.from_pretrained("{checkpoint}")
|
||||
>>> model = {model_class}.from_pretrained("{checkpoint}")
|
||||
|
||||
>>> inputs = feature_extractor(image, return_tensors="pt")
|
||||
>>> inputs = image_processor(image, return_tensors="pt")
|
||||
|
||||
>>> with torch.no_grad():
|
||||
... logits = model(**inputs).logits
|
||||
@ -1048,6 +1029,20 @@ FLAX_SAMPLE_DOCSTRINGS = {
|
||||
}
|
||||
|
||||
|
||||
def filter_outputs_from_example(docstring, **kwargs):
|
||||
"""
|
||||
Removes the lines testing an output with the doctest syntax in a code sample when it's set to `None`.
|
||||
"""
|
||||
for key, value in kwargs.items():
|
||||
if value is not None:
|
||||
continue
|
||||
|
||||
doc_key = "{" + key + "}"
|
||||
docstring = re.sub(rf"\n([^\n]+)\n\s+{doc_key}\n", "\n", docstring)
|
||||
|
||||
return docstring
|
||||
|
||||
|
||||
def add_code_sample_docstrings(
|
||||
*docstr,
|
||||
processor_class=None,
|
||||
@ -1059,8 +1054,8 @@ def add_code_sample_docstrings(
|
||||
qa_target_end_index=15,
|
||||
model_cls=None,
|
||||
modality=None,
|
||||
expected_output="",
|
||||
expected_loss="",
|
||||
expected_output=None,
|
||||
expected_loss=None,
|
||||
):
|
||||
def docstring_decorator(fn):
|
||||
# model_class defaults to function's class if not specified otherwise
|
||||
@ -1118,6 +1113,9 @@ def add_code_sample_docstrings(
|
||||
else:
|
||||
raise ValueError(f"Docstring can't be built for model {model_class}")
|
||||
|
||||
code_sample = filter_outputs_from_example(
|
||||
code_sample, expected_output=expected_output, expected_loss=expected_loss
|
||||
)
|
||||
func_doc = (fn.__doc__ or "") + "".join(docstr)
|
||||
output_doc = "" if output_type is None else _prepare_output_docstrings(output_type, config_class)
|
||||
built_doc = code_sample.format(**doc_kwargs)
|
||||
|
Loading…
Reference in New Issue
Block a user