


'''simple docstring'''


from collections import OrderedDict
from typing import Any, Mapping, Optional

from ... import PreTrainedTokenizer
from ...configuration_utils import PretrainedConfig
from ...onnx import OnnxConfig, OnnxConfigWithPast, OnnxSeqaSeqConfigWithPast
from ...onnx.utils import compute_effective_axis_dimension
from ...utils import TensorType, is_torch_available, logging


UpperCAmelCase_	:     str							     =	logging.get_logger(__name__)

UpperCAmelCase_	:     Tuple							     =	{
    'google/bigbird-pegasus-large-arxiv': (
        'https://huggingface.co/google/bigbird-pegasus-large-arxiv/resolve/main/config.json'
    ),
    'google/bigbird-pegasus-large-pubmed': (
        'https://huggingface.co/google/bigbird-pegasus-large-pubmed/resolve/main/config.json'
    ),
    'google/bigbird-pegasus-large-bigpatent': (
        'https://huggingface.co/google/bigbird-pegasus-large-bigpatent/resolve/main/config.json'
    ),
    # See all BigBirdPegasus models at https://huggingface.co/models?filter=bigbird_pegasus
}





class 					lowercase__						(		_snake_case      ):





							'''simple docstring'''


							A_							:   List[Any]   			=						"""bigbird_pegasus"""
							A_							:   Optional[int]   			=						["""past_key_values"""]
							A_							:   int   			=						{
							    """num_attention_heads""": """encoder_attention_heads""",
							    """hidden_size""": """d_model""",
							    """attention_probs_dropout_prob""": """attention_dropout""",
							}






							def __init__(					self					,      __snake_case=9_6103					,      __snake_case=4096					,      __snake_case=16					,      __snake_case=4096					,      __snake_case=16					,      __snake_case=16					,      __snake_case=4096					,      __snake_case=16					,      __snake_case=0.0					,      __snake_case=0.0					,      __snake_case=True					,      __snake_case=True					,      __snake_case="gelu_new"					,      __snake_case=1024					,      __snake_case=0.1					,      __snake_case=0.0					,      __snake_case=0.0					,      __snake_case=0.02					,      __snake_case=2					,      __snake_case=0.0					,      __snake_case=True					,      __snake_case=0					,      __snake_case=2					,      __snake_case=1					,      __snake_case="block_sparse"					,      __snake_case=64					,      __snake_case=3					,      __snake_case=False					,      **__snake_case					,      ):
											_SCREAMING_SNAKE_CASE				: Any  	=   vocab_size
											_SCREAMING_SNAKE_CASE				: Any  	=   max_position_embeddings
											_SCREAMING_SNAKE_CASE				: Optional[int]  	=   d_model
											_SCREAMING_SNAKE_CASE				: List[Any]  	=   encoder_ffn_dim
											_SCREAMING_SNAKE_CASE				: Dict  	=   encoder_layers
											_SCREAMING_SNAKE_CASE				: Optional[int]  	=   encoder_attention_heads
											_SCREAMING_SNAKE_CASE				: str  	=   decoder_ffn_dim
											_SCREAMING_SNAKE_CASE				: List[Any]  	=   decoder_layers
											_SCREAMING_SNAKE_CASE				: Any  	=   decoder_attention_heads
											_SCREAMING_SNAKE_CASE				: Tuple  	=   dropout
											_SCREAMING_SNAKE_CASE				: Union[str, Any]  	=   attention_dropout
											_SCREAMING_SNAKE_CASE				: List[Any]  	=   activation_dropout
											_SCREAMING_SNAKE_CASE				: Tuple  	=   activation_function
											_SCREAMING_SNAKE_CASE				: str  	=   init_std
											_SCREAMING_SNAKE_CASE				: Tuple  	=   encoder_layerdrop
											_SCREAMING_SNAKE_CASE				: List[Any]  	=   decoder_layerdrop
											_SCREAMING_SNAKE_CASE				: Any  	=   classifier_dropout
											_SCREAMING_SNAKE_CASE				: str  	=   use_cache
											_SCREAMING_SNAKE_CASE				: Any  	=   encoder_layers
											_SCREAMING_SNAKE_CASE				: int  	=   scale_embedding  # scale factor will be sqrt(d_model) if True

											# extra config
											_SCREAMING_SNAKE_CASE				: Dict  	=   attention_type
											_SCREAMING_SNAKE_CASE				: Optional[int]  	=   block_size
											_SCREAMING_SNAKE_CASE				: List[str]  	=   num_random_blocks
											_SCREAMING_SNAKE_CASE				: Tuple  	=   use_bias

											super().__init__(
											    pad_token_id=__snake_case					,      bos_token_id=__snake_case					,      eos_token_id=__snake_case					,      is_encoder_decoder=__snake_case					,      decoder_start_token_id=__snake_case					,      **__snake_case					,      )





class 					lowercase__						(		_snake_case      ):





							'''simple docstring'''


							@property
							def UpperCAmelCase_			(					self						):
											if self.task in ["default", "seq2seq-lm"]:
															_SCREAMING_SNAKE_CASE				: str  	=   OrderedDict(
															    [
															        ("""input_ids""", {0: """batch""", 1: """encoder_sequence"""}),
															        ("""attention_mask""", {0: """batch""", 1: """encoder_sequence"""}),
															    ]						)

															if self.use_past:
																			_SCREAMING_SNAKE_CASE				: int  	=   {0: """batch"""}
																			_SCREAMING_SNAKE_CASE				: Tuple  	=   {0: """batch""", 1: """past_decoder_sequence + sequence"""}
															else:
																			_SCREAMING_SNAKE_CASE				: int  	=   {0: """batch""", 1: """decoder_sequence"""}
																			_SCREAMING_SNAKE_CASE				: str  	=   {0: """batch""", 1: """decoder_sequence"""}

															if self.use_past:
																			self.fill_with_past_key_values_(__snake_case					,      direction="""inputs"""						)
											elif self.task == "causal-lm":
															# TODO: figure this case out.
															_SCREAMING_SNAKE_CASE				: int  	=   OrderedDict(
															    [
															        ("""input_ids""", {0: """batch""", 1: """encoder_sequence"""}),
															        ("""attention_mask""", {0: """batch""", 1: """encoder_sequence"""}),
															    ]						)
															if self.use_past:
																			_SCREAMING_SNAKE_CASE	, _SCREAMING_SNAKE_CASE				: Union[str, Any]  	=   self.num_layers
																			for i in range(__snake_case						):
																							_SCREAMING_SNAKE_CASE				: Dict  	=   {0: """batch""", 2: """past_sequence + sequence"""}
																							_SCREAMING_SNAKE_CASE				: str  	=   {0: """batch""", 2: """past_sequence + sequence"""}
											else:
															_SCREAMING_SNAKE_CASE				: Any  	=   OrderedDict(
															    [
															        ("""input_ids""", {0: """batch""", 1: """encoder_sequence"""}),
															        ("""attention_mask""", {0: """batch""", 1: """encoder_sequence"""}),
															        ("""decoder_input_ids""", {0: """batch""", 1: """decoder_sequence"""}),
															        ("""decoder_attention_mask""", {0: """batch""", 1: """decoder_sequence"""}),
															    ]						)

											return common_inputs






							@property
							def UpperCAmelCase_			(					self						):
											if self.task in ["default", "seq2seq-lm"]:
															_SCREAMING_SNAKE_CASE				: str  	=   super().outputs
											else:
															_SCREAMING_SNAKE_CASE				: Optional[Any]  	=   super(__snake_case					,      self						).outputs
															if self.use_past:
																			_SCREAMING_SNAKE_CASE	, _SCREAMING_SNAKE_CASE				: Optional[int]  	=   self.num_layers
																			for i in range(__snake_case						):
																							_SCREAMING_SNAKE_CASE				: Optional[Any]  	=   {0: """batch""", 2: """past_sequence + sequence"""}
																							_SCREAMING_SNAKE_CASE				: str  	=   {0: """batch""", 2: """past_sequence + sequence"""}
											return common_outputs






							def UpperCAmelCase_			(					self					,      __snake_case					,      __snake_case = -1					,      __snake_case = -1					,      __snake_case = False					,      __snake_case = None					,      ):
											_SCREAMING_SNAKE_CASE				: List[str]  	=   self._generate_dummy_inputs_for_sequence_classification_and_question_answering(
											    __snake_case					,      __snake_case					,      __snake_case					,      __snake_case					,      __snake_case						)

											# Generate decoder inputs
											_SCREAMING_SNAKE_CASE				: Tuple  	=   seq_length if not self.use_past else 1
											_SCREAMING_SNAKE_CASE				: Dict  	=   self._generate_dummy_inputs_for_sequence_classification_and_question_answering(
											    __snake_case					,      __snake_case					,      __snake_case					,      __snake_case					,      __snake_case						)
											_SCREAMING_SNAKE_CASE				: Dict  	=   {f"""decoder_{name}""": tensor for name, tensor in decoder_inputs.items()}
											_SCREAMING_SNAKE_CASE				: int  	=   dict(**__snake_case					,      **__snake_case						)

											if self.use_past:
															if not is_torch_available():
																			raise ValueError("""Cannot generate dummy past_keys inputs without PyTorch installed."""						)
															else:
																			import torch
															_SCREAMING_SNAKE_CASE	, _SCREAMING_SNAKE_CASE				: Union[str, Any]  	=   common_inputs["""input_ids"""].shape
															_SCREAMING_SNAKE_CASE				: Optional[Any]  	=   common_inputs["""decoder_input_ids"""].shape[1]
															_SCREAMING_SNAKE_CASE	, _SCREAMING_SNAKE_CASE				: Any  	=   self.num_attention_heads
															_SCREAMING_SNAKE_CASE				: Tuple  	=   (
															    batch,
															    num_encoder_attention_heads,
															    encoder_seq_length,
															    self._config.hidden_size // num_encoder_attention_heads,
															)
															_SCREAMING_SNAKE_CASE				: Tuple  	=   decoder_seq_length + 3
															_SCREAMING_SNAKE_CASE				: List[Any]  	=   (
															    batch,
															    num_decoder_attention_heads,
															    decoder_past_length,
															    self._config.hidden_size // num_decoder_attention_heads,
															)

															_SCREAMING_SNAKE_CASE				: List[Any]  	=   torch.cat(
															    [common_inputs["""decoder_attention_mask"""], torch.ones(__snake_case					,      __snake_case						)]					,      dim=1						)

															_SCREAMING_SNAKE_CASE				: Dict  	=   []
															# If the number of encoder and decoder layers are present in the model configuration, both are considered
															_SCREAMING_SNAKE_CASE	, _SCREAMING_SNAKE_CASE				: List[str]  	=   self.num_layers
															_SCREAMING_SNAKE_CASE				: Tuple  	=   min(__snake_case					,      __snake_case						)
															_SCREAMING_SNAKE_CASE				: Dict  	=   max(__snake_case					,      __snake_case						) - min_num_layers
															_SCREAMING_SNAKE_CASE				: Tuple  	=   """encoder""" if num_encoder_layers > num_decoder_layers else """decoder"""

															for _ in range(__snake_case						):
																			common_inputs["past_key_values"].append(
																			    (
																			        torch.zeros(__snake_case						),
																			        torch.zeros(__snake_case						),
																			        torch.zeros(__snake_case						),
																			        torch.zeros(__snake_case						),
																			    )						)
															# TODO: test this.
															_SCREAMING_SNAKE_CASE				: List[str]  	=   encoder_shape if remaining_side_name == """encoder""" else decoder_shape
															for _ in range(__snake_case					,      __snake_case						):
																			common_inputs["past_key_values"].append((torch.zeros(__snake_case						), torch.zeros(__snake_case						))						)
											return common_inputs






							def UpperCAmelCase_			(					self					,      __snake_case					,      __snake_case = -1					,      __snake_case = -1					,      __snake_case = False					,      __snake_case = None					,      ):
											_SCREAMING_SNAKE_CASE				: Tuple  	=   self._generate_dummy_inputs_for_sequence_classification_and_question_answering(
											    __snake_case					,      __snake_case					,      __snake_case					,      __snake_case					,      __snake_case						)

											if self.use_past:
															if not is_torch_available():
																			raise ValueError("""Cannot generate dummy past_keys inputs without PyTorch installed."""						)
															else:
																			import torch
															_SCREAMING_SNAKE_CASE	, _SCREAMING_SNAKE_CASE				: Optional[Any]  	=   common_inputs["""input_ids"""].shape
															# Not using the same length for past_key_values
															_SCREAMING_SNAKE_CASE				: Optional[Any]  	=   seqlen + 2
															_SCREAMING_SNAKE_CASE	, _SCREAMING_SNAKE_CASE				: int  	=   self.num_layers
															_SCREAMING_SNAKE_CASE	, _SCREAMING_SNAKE_CASE				: Tuple  	=   self.num_attention_heads
															_SCREAMING_SNAKE_CASE				: Tuple  	=   (
															    batch,
															    num_encoder_attention_heads,
															    past_key_values_length,
															    self._config.hidden_size // num_encoder_attention_heads,
															)

															_SCREAMING_SNAKE_CASE				: Tuple  	=   common_inputs["""attention_mask"""].dtype
															_SCREAMING_SNAKE_CASE				: Optional[int]  	=   torch.cat(
															    [common_inputs["""attention_mask"""], torch.ones(__snake_case					,      __snake_case					,      dtype=__snake_case						)]					,      dim=1						)
															_SCREAMING_SNAKE_CASE				: str  	=   [
															    (torch.zeros(__snake_case						), torch.zeros(__snake_case						)) for _ in range(__snake_case						)
															]
											return common_inputs






							def UpperCAmelCase_			(					self					,      __snake_case					,      __snake_case = -1					,      __snake_case = -1					,      __snake_case = False					,      __snake_case = None					,      ):
											# Copied from OnnxConfig.generate_dummy_inputs
											# Did not use super(OnnxConfigWithPast, self).generate_dummy_inputs for code clarity.
											# If dynamic axis (-1) we forward with a fixed dimension of 2 samples to avoid optimizations made by ONNX
											_SCREAMING_SNAKE_CASE				: Optional[int]  	=   compute_effective_axis_dimension(
											    __snake_case					,      fixed_dimension=OnnxConfig.default_fixed_batch					,      num_token_to_add=0						)

											# If dynamic axis (-1) we forward with a fixed dimension of 8 tokens to avoid optimizations made by ONNX
											_SCREAMING_SNAKE_CASE				: Tuple  	=   tokenizer.num_special_tokens_to_add(__snake_case						)
											_SCREAMING_SNAKE_CASE				: List[str]  	=   compute_effective_axis_dimension(
											    __snake_case					,      fixed_dimension=OnnxConfig.default_fixed_sequence					,      num_token_to_add=__snake_case						)

											# Generate dummy inputs according to compute batch and sequence
											_SCREAMING_SNAKE_CASE				: Tuple  	=   [""" """.join([tokenizer.unk_token]						) * seq_length] * batch_size
											_SCREAMING_SNAKE_CASE				: str  	=   dict(tokenizer(__snake_case					,      return_tensors=__snake_case						)						)
											return common_inputs






							def UpperCAmelCase_			(					self					,      __snake_case					,      __snake_case = -1					,      __snake_case = -1					,      __snake_case = False					,      __snake_case = None					,      ):
											if self.task in ["default", "seq2seq-lm"]:
															_SCREAMING_SNAKE_CASE				: Any  	=   self._generate_dummy_inputs_for_default_and_seqaseq_lm(
															    __snake_case					,      batch_size=__snake_case					,      seq_length=__snake_case					,      is_pair=__snake_case					,      framework=__snake_case						)

											elif self.task == "causal-lm":
															_SCREAMING_SNAKE_CASE				: Optional[Any]  	=   self._generate_dummy_inputs_for_causal_lm(
															    __snake_case					,      batch_size=__snake_case					,      seq_length=__snake_case					,      is_pair=__snake_case					,      framework=__snake_case						)
											else:
															_SCREAMING_SNAKE_CASE				: Dict  	=   self._generate_dummy_inputs_for_sequence_classification_and_question_answering(
															    __snake_case					,      batch_size=__snake_case					,      seq_length=__snake_case					,      is_pair=__snake_case					,      framework=__snake_case						)

											return common_inputs






							def UpperCAmelCase_			(					self					,      __snake_case					,      __snake_case					,      __snake_case					,      __snake_case						):
											if self.task in ["default", "seq2seq-lm"]:
															_SCREAMING_SNAKE_CASE				: Dict  	=   super()._flatten_past_key_values_(__snake_case					,      __snake_case					,      __snake_case					,      __snake_case						)
											else:
															_SCREAMING_SNAKE_CASE				: List[Any]  	=   super(__snake_case					,      self						)._flatten_past_key_values_(
															    __snake_case					,      __snake_case					,      __snake_case					,      __snake_case						)








'''simple docstring'''


from __future__ import annotations

import unittest

from transformers import is_tf_available
from transformers.testing_utils import require_tf, slow

from ...test_configuration_common import ConfigTester
from ...test_modeling_tf_common import TFModelTesterMixin, ids_tensor, random_attention_mask
from ...test_pipeline_mixin import PipelineTesterMixin


if is_tf_available():
				import tensorflow as tf

				from transformers import (
				    TF_XLM_PRETRAINED_MODEL_ARCHIVE_LIST,
				    TFXLMForMultipleChoice,
				    TFXLMForQuestionAnsweringSimple,
				    TFXLMForSequenceClassification,
				    TFXLMForTokenClassification,
				    TFXLMModel,
				    TFXLMWithLMHeadModel,
				    XLMConfig,
				)





class 					lowercase__						:





							'''simple docstring'''


							def __init__(					self					,      __snake_case					,      ):
											_SCREAMING_SNAKE_CASE				: Dict  	=   parent
											_SCREAMING_SNAKE_CASE				: Any  	=   13
											_SCREAMING_SNAKE_CASE				: Union[str, Any]  	=   7
											_SCREAMING_SNAKE_CASE				: Optional[Any]  	=   True
											_SCREAMING_SNAKE_CASE				: str  	=   True
											_SCREAMING_SNAKE_CASE				: Dict  	=   True
											_SCREAMING_SNAKE_CASE				: List[Any]  	=   True
											_SCREAMING_SNAKE_CASE				: List[Any]  	=   True
											_SCREAMING_SNAKE_CASE				: List[Any]  	=   False
											_SCREAMING_SNAKE_CASE				: Tuple  	=   False
											_SCREAMING_SNAKE_CASE				: Any  	=   False
											_SCREAMING_SNAKE_CASE				: Dict  	=   2
											_SCREAMING_SNAKE_CASE				: str  	=   99
											_SCREAMING_SNAKE_CASE				: Any  	=   0
											_SCREAMING_SNAKE_CASE				: List[str]  	=   32
											_SCREAMING_SNAKE_CASE				: Union[str, Any]  	=   2
											_SCREAMING_SNAKE_CASE				: List[str]  	=   4
											_SCREAMING_SNAKE_CASE				: List[str]  	=   0.1
											_SCREAMING_SNAKE_CASE				: Union[str, Any]  	=   0.1
											_SCREAMING_SNAKE_CASE				: int  	=   512
											_SCREAMING_SNAKE_CASE				: List[str]  	=   16
											_SCREAMING_SNAKE_CASE				: List[Any]  	=   2
											_SCREAMING_SNAKE_CASE				: List[Any]  	=   0.02
											_SCREAMING_SNAKE_CASE				: List[Any]  	=   3
											_SCREAMING_SNAKE_CASE				: Union[str, Any]  	=   4
											_SCREAMING_SNAKE_CASE				: Optional[Any]  	=   """last"""
											_SCREAMING_SNAKE_CASE				: Optional[int]  	=   True
											_SCREAMING_SNAKE_CASE				: List[Any]  	=   None
											_SCREAMING_SNAKE_CASE				: Optional[int]  	=   0






							def UpperCAmelCase_			(					self						):
											_SCREAMING_SNAKE_CASE				: Tuple  	=   ids_tensor([self.batch_size, self.seq_length]					,      self.vocab_size						)
											_SCREAMING_SNAKE_CASE				: Optional[int]  	=   random_attention_mask([self.batch_size, self.seq_length]					,      dtype=tf.floataa						)

											_SCREAMING_SNAKE_CASE				: List[str]  	=   None
											if self.use_input_lengths:
															_SCREAMING_SNAKE_CASE				: int  	=   (
															    ids_tensor([self.batch_size]					,      vocab_size=2						) + self.seq_length - 2
															)  # small variation of seq_length

											_SCREAMING_SNAKE_CASE				: Tuple  	=   None
											if self.use_token_type_ids:
															_SCREAMING_SNAKE_CASE				: Union[str, Any]  	=   ids_tensor([self.batch_size, self.seq_length]					,      self.n_langs						)

											_SCREAMING_SNAKE_CASE				: Optional[int]  	=   None
											_SCREAMING_SNAKE_CASE				: Optional[int]  	=   None
											_SCREAMING_SNAKE_CASE				: Dict  	=   None
											if self.use_labels:
															_SCREAMING_SNAKE_CASE				: Tuple  	=   ids_tensor([self.batch_size]					,      self.type_sequence_label_size						)
															_SCREAMING_SNAKE_CASE				: Optional[Any]  	=   ids_tensor([self.batch_size, self.seq_length]					,      self.num_labels						)
															_SCREAMING_SNAKE_CASE				: Tuple  	=   ids_tensor([self.batch_size]					,      2					,      dtype=tf.floataa						)
															_SCREAMING_SNAKE_CASE				: Optional[int]  	=   ids_tensor([self.batch_size]					,      self.num_choices						)

											_SCREAMING_SNAKE_CASE				: Dict  	=   XLMConfig(
											    vocab_size=self.vocab_size					,      n_special=self.n_special					,      emb_dim=self.hidden_size					,      n_layers=self.num_hidden_layers					,      n_heads=self.num_attention_heads					,      dropout=self.hidden_dropout_prob					,      attention_dropout=self.attention_probs_dropout_prob					,      gelu_activation=self.gelu_activation					,      sinusoidal_embeddings=self.sinusoidal_embeddings					,      asm=self.asm					,      causal=self.causal					,      n_langs=self.n_langs					,      max_position_embeddings=self.max_position_embeddings					,      initializer_range=self.initializer_range					,      summary_type=self.summary_type					,      use_proj=self.use_proj					,      bos_token_id=self.bos_token_id					,      )

											return (
											    config,
											    input_ids,
											    token_type_ids,
											    input_lengths,
											    sequence_labels,
											    token_labels,
											    is_impossible_labels,
											    choice_labels,
											    input_mask,
											)






							def UpperCAmelCase_			(					self					,      __snake_case					,      __snake_case					,      __snake_case					,      __snake_case					,      __snake_case					,      __snake_case					,      __snake_case					,      __snake_case					,      __snake_case					,      ):
											_SCREAMING_SNAKE_CASE				: Tuple  	=   TFXLMModel(config=__snake_case						)
											_SCREAMING_SNAKE_CASE				: int  	=   {"""input_ids""": input_ids, """lengths""": input_lengths, """langs""": token_type_ids}
											_SCREAMING_SNAKE_CASE				: int  	=   model(__snake_case						)

											_SCREAMING_SNAKE_CASE				: str  	=   [input_ids, input_mask]
											_SCREAMING_SNAKE_CASE				: str  	=   model(__snake_case						)
											self.parent.assertEqual(result.last_hidden_state.shape					,      (self.batch_size, self.seq_length, self.hidden_size)						)






							def UpperCAmelCase_			(					self					,      __snake_case					,      __snake_case					,      __snake_case					,      __snake_case					,      __snake_case					,      __snake_case					,      __snake_case					,      __snake_case					,      __snake_case					,      ):
											_SCREAMING_SNAKE_CASE				: Tuple  	=   TFXLMWithLMHeadModel(__snake_case						)

											_SCREAMING_SNAKE_CASE				: Optional[Any]  	=   {"""input_ids""": input_ids, """lengths""": input_lengths, """langs""": token_type_ids}
											_SCREAMING_SNAKE_CASE				: Optional[int]  	=   model(__snake_case						)

											_SCREAMING_SNAKE_CASE				: str  	=   outputs

											self.parent.assertEqual(result.logits.shape					,      (self.batch_size, self.seq_length, self.vocab_size)						)






							def UpperCAmelCase_			(					self					,      __snake_case					,      __snake_case					,      __snake_case					,      __snake_case					,      __snake_case					,      __snake_case					,      __snake_case					,      __snake_case					,      __snake_case					,      ):
											_SCREAMING_SNAKE_CASE				: List[str]  	=   TFXLMForQuestionAnsweringSimple(__snake_case						)

											_SCREAMING_SNAKE_CASE				: Any  	=   {"""input_ids""": input_ids, """lengths""": input_lengths}

											_SCREAMING_SNAKE_CASE				: Optional[Any]  	=   model(__snake_case						)

											self.parent.assertEqual(result.start_logits.shape					,      (self.batch_size, self.seq_length)						)
											self.parent.assertEqual(result.end_logits.shape					,      (self.batch_size, self.seq_length)						)






							def UpperCAmelCase_			(					self					,      __snake_case					,      __snake_case					,      __snake_case					,      __snake_case					,      __snake_case					,      __snake_case					,      __snake_case					,      __snake_case					,      __snake_case					,      ):
											_SCREAMING_SNAKE_CASE				: Dict  	=   TFXLMForSequenceClassification(__snake_case						)

											_SCREAMING_SNAKE_CASE				: int  	=   {"""input_ids""": input_ids, """lengths""": input_lengths}

											_SCREAMING_SNAKE_CASE				: List[str]  	=   model(__snake_case						)

											self.parent.assertEqual(result.logits.shape					,      (self.batch_size, self.type_sequence_label_size)						)






							def UpperCAmelCase_			(					self					,      __snake_case					,      __snake_case					,      __snake_case					,      __snake_case					,      __snake_case					,      __snake_case					,      __snake_case					,      __snake_case					,      __snake_case					,      ):
											_SCREAMING_SNAKE_CASE				: Union[str, Any]  	=   self.num_labels
											_SCREAMING_SNAKE_CASE				: Dict  	=   TFXLMForTokenClassification(config=__snake_case						)
											_SCREAMING_SNAKE_CASE				: Any  	=   {"""input_ids""": input_ids, """attention_mask""": input_mask, """token_type_ids""": token_type_ids}
											_SCREAMING_SNAKE_CASE				: Dict  	=   model(__snake_case						)
											self.parent.assertEqual(result.logits.shape					,      (self.batch_size, self.seq_length, self.num_labels)						)






							def UpperCAmelCase_			(					self					,      __snake_case					,      __snake_case					,      __snake_case					,      __snake_case					,      __snake_case					,      __snake_case					,      __snake_case					,      __snake_case					,      __snake_case					,      ):
											_SCREAMING_SNAKE_CASE				: Optional[Any]  	=   self.num_choices
											_SCREAMING_SNAKE_CASE				: List[str]  	=   TFXLMForMultipleChoice(config=__snake_case						)
											_SCREAMING_SNAKE_CASE				: Union[str, Any]  	=   tf.tile(tf.expand_dims(__snake_case					,      1						)					,      (1, self.num_choices, 1)						)
											_SCREAMING_SNAKE_CASE				: List[str]  	=   tf.tile(tf.expand_dims(__snake_case					,      1						)					,      (1, self.num_choices, 1)						)
											_SCREAMING_SNAKE_CASE				: int  	=   tf.tile(tf.expand_dims(__snake_case					,      1						)					,      (1, self.num_choices, 1)						)
											_SCREAMING_SNAKE_CASE				: Optional[int]  	=   {
											    """input_ids""": multiple_choice_inputs_ids,
											    """attention_mask""": multiple_choice_input_mask,
											    """token_type_ids""": multiple_choice_token_type_ids,
											}
											_SCREAMING_SNAKE_CASE				: Any  	=   model(__snake_case						)
											self.parent.assertEqual(result.logits.shape					,      (self.batch_size, self.num_choices)						)






							def UpperCAmelCase_			(					self						):
											_SCREAMING_SNAKE_CASE				: Optional[Any]  	=   self.prepare_config_and_inputs()
											(
											    (
											    _SCREAMING_SNAKE_CASE
											)	, (
											    _SCREAMING_SNAKE_CASE
											)	, (
											    _SCREAMING_SNAKE_CASE
											)	, (
											    _SCREAMING_SNAKE_CASE
											)	, (
											    _SCREAMING_SNAKE_CASE
											)	, (
											    _SCREAMING_SNAKE_CASE
											)	, (
											    _SCREAMING_SNAKE_CASE
											)	, (
											    _SCREAMING_SNAKE_CASE
											)	, (
											    _SCREAMING_SNAKE_CASE
											)	, 
											)				: Tuple  	=   config_and_inputs
											_SCREAMING_SNAKE_CASE				: Tuple  	=   {
											    """input_ids""": input_ids,
											    """token_type_ids""": token_type_ids,
											    """langs""": token_type_ids,
											    """lengths""": input_lengths,
											}
											return config, inputs_dict





@require_tf
class 					lowercase__						(		_snake_case  ,  _snake_case  ,  unittest.TestCase      ):





							'''simple docstring'''


							A_							:   List[str]   			=						(
							    (
							        TFXLMModel,
							        TFXLMWithLMHeadModel,
							        TFXLMForSequenceClassification,
							        TFXLMForQuestionAnsweringSimple,
							        TFXLMForTokenClassification,
							        TFXLMForMultipleChoice,
							    )
							    if is_tf_available()
							    else ()
							)
							A_							:   List[str]   			=						(
							    (TFXLMWithLMHeadModel,) if is_tf_available() else ()
							)  # TODO (PVP): Check other models whether language generation is also applicable
							A_							:   List[str]   			=						(
							    {
							        """feature-extraction""": TFXLMModel,
							        """fill-mask""": TFXLMWithLMHeadModel,
							        """question-answering""": TFXLMForQuestionAnsweringSimple,
							        """text-classification""": TFXLMForSequenceClassification,
							        """text-generation""": TFXLMWithLMHeadModel,
							        """token-classification""": TFXLMForTokenClassification,
							        """zero-shot""": TFXLMForSequenceClassification,
							    }
							    if is_tf_available()
							    else {}
							)
							A_							:   Tuple   			=						False
							A_							:   Dict   			=						False






							def UpperCAmelCase_			(					self					,      __snake_case					,      __snake_case					,      __snake_case					,      __snake_case					,      __snake_case						):
											if (
											    pipeline_test_casse_name == "QAPipelineTests"
											    and tokenizer_name is not None
											    and not tokenizer_name.endswith("""Fast"""						)
											):
															# `QAPipelineTests` fails for a few models when the slower tokenizer are used.
															# (The slower tokenizers were never used for pipeline tests before the pipeline testing rework)
															# TODO: check (and possibly fix) the `QAPipelineTests` with slower tokenizer
															return True

											return False






							def UpperCAmelCase_			(					self						):
											_SCREAMING_SNAKE_CASE				: Tuple  	=   TFXLMModelTester(self						)
											_SCREAMING_SNAKE_CASE				: List[str]  	=   ConfigTester(self					,      config_class=__snake_case					,      emb_dim=37						)






							def UpperCAmelCase_			(					self						):
											self.config_tester.run_common_tests()






							def UpperCAmelCase_			(					self						):
											_SCREAMING_SNAKE_CASE				: int  	=   self.model_tester.prepare_config_and_inputs()
											self.model_tester.create_and_check_xlm_model(*__snake_case						)






							def UpperCAmelCase_			(					self						):
											_SCREAMING_SNAKE_CASE				: Dict  	=   self.model_tester.prepare_config_and_inputs()
											self.model_tester.create_and_check_xlm_lm_head(*__snake_case						)






							def UpperCAmelCase_			(					self						):
											_SCREAMING_SNAKE_CASE				: Tuple  	=   self.model_tester.prepare_config_and_inputs()
											self.model_tester.create_and_check_xlm_qa(*__snake_case						)






							def UpperCAmelCase_			(					self						):
											_SCREAMING_SNAKE_CASE				: Optional[Any]  	=   self.model_tester.prepare_config_and_inputs()
											self.model_tester.create_and_check_xlm_sequence_classif(*__snake_case						)






							def UpperCAmelCase_			(					self						):
											_SCREAMING_SNAKE_CASE				: Tuple  	=   self.model_tester.prepare_config_and_inputs()
											self.model_tester.create_and_check_xlm_for_token_classification(*__snake_case						)






							def UpperCAmelCase_			(					self						):
											_SCREAMING_SNAKE_CASE				: Dict  	=   self.model_tester.prepare_config_and_inputs()
											self.model_tester.create_and_check_xlm_for_multiple_choice(*__snake_case						)






							@slow
							def UpperCAmelCase_			(					self						):
											for model_name in TF_XLM_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
															_SCREAMING_SNAKE_CASE				: int  	=   TFXLMModel.from_pretrained(__snake_case						)
															self.assertIsNotNone(__snake_case						)





@require_tf
class 					lowercase__						(		unittest.TestCase      ):





							'''simple docstring'''


							@slow
							def UpperCAmelCase_			(					self						):
											_SCREAMING_SNAKE_CASE				: Optional[int]  	=   TFXLMWithLMHeadModel.from_pretrained("""xlm-mlm-en-2048"""						)
											_SCREAMING_SNAKE_CASE				: Any  	=   tf.convert_to_tensor([[14, 447]]					,      dtype=tf.intaa						)  # the president
											_SCREAMING_SNAKE_CASE				: Union[str, Any]  	=   [
											    14,
											    447,
											    14,
											    447,
											    14,
											    447,
											    14,
											    447,
											    14,
											    447,
											    14,
											    447,
											    14,
											    447,
											    14,
											    447,
											    14,
											    447,
											    14,
											    447,
											]  # the president the president the president the president the president the president the president the president the president the president
											# TODO(PVP): this and other input_ids I tried for generation give pretty bad results. Not sure why. Model might just not be made for auto-regressive inference
											_SCREAMING_SNAKE_CASE				: Dict  	=   model.generate(__snake_case					,      do_sample=__snake_case						)
											self.assertListEqual(output_ids[0].numpy().tolist()					,      __snake_case						)




