
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 		__magic_name__   :





		def __init__(							self    :	Any						,  lowerCamelCase__    :	Optional[Any]						,  ):
							lowerCAmelCase		:					Optional[int]  	=     parent
							lowerCAmelCase		:					Tuple  	=     1_3
							lowerCAmelCase		:					Optional[Any]  	=     7
							lowerCAmelCase		:					str  	=     True
							lowerCAmelCase		:					List[str]  	=     True
							lowerCAmelCase		:					List[Any]  	=     True
							lowerCAmelCase		:					str  	=     True
							lowerCAmelCase		:					Any  	=     True
							lowerCAmelCase		:					int  	=     False
							lowerCAmelCase		:					List[Any]  	=     False
							lowerCAmelCase		:					int  	=     False
							lowerCAmelCase		:					Tuple  	=     2
							lowerCAmelCase		:					List[Any]  	=     9_9
							lowerCAmelCase		:					Union[str, Any]  	=     0
							lowerCAmelCase		:					Union[str, Any]  	=     3_2
							lowerCAmelCase		:					int  	=     2
							lowerCAmelCase		:					Tuple  	=     4
							lowerCAmelCase		:					Tuple  	=     0.1
							lowerCAmelCase		:					Optional[int]  	=     0.1
							lowerCAmelCase		:					Optional[int]  	=     5_1_2
							lowerCAmelCase		:					List[Any]  	=     1_6
							lowerCAmelCase		:					List[str]  	=     2
							lowerCAmelCase		:					Tuple  	=     0.0_2
							lowerCAmelCase		:					Optional[Any]  	=     3
							lowerCAmelCase		:					List[Any]  	=     4
							lowerCAmelCase		:					str  	=     '''last'''
							lowerCAmelCase		:					List[Any]  	=     True
							lowerCAmelCase		:					Dict  	=     None
							lowerCAmelCase		:					List[str]  	=     0





		def       _A							(							self    :	int				):
							lowerCAmelCase		:					Optional[Any]  	=     ids_tensor([self.batch_size, self.seq_length]						,  self.vocab_size				)
							lowerCAmelCase		:					str  	=     random_attention_mask([self.batch_size, self.seq_length]						,  dtype=tf.floataa				)

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

							lowerCAmelCase		:					List[Any]  	=     None
							if self.use_token_type_ids:
												lowerCAmelCase		:					str  	=     ids_tensor([self.batch_size, self.seq_length]						,  self.n_langs				)

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

							lowerCAmelCase		:					Union[str, Any]  	=     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       _A							(							self    :	Optional[int]						,  lowerCamelCase__    :	Optional[Any]						,  lowerCamelCase__    :	Union[str, Any]						,  lowerCamelCase__    :	Optional[Any]						,  lowerCamelCase__    :	Optional[int]						,  lowerCamelCase__    :	Any						,  lowerCamelCase__    :	Optional[int]						,  lowerCamelCase__    :	int						,  lowerCamelCase__    :	str						,  lowerCamelCase__    :	Dict						,  ):
							lowerCAmelCase		:					List[Any]  	=     TFXLMModel(config=lowerCamelCase__				)
							lowerCAmelCase		:					Optional[int]  	=     {'''input_ids''': input_ids, '''lengths''': input_lengths, '''langs''': token_type_ids}
							lowerCAmelCase		:					int  	=     model(lowerCamelCase__				)

							lowerCAmelCase		:					Optional[int]  	=     [input_ids, input_mask]
							lowerCAmelCase		:					List[str]  	=     model(lowerCamelCase__				)
							self.parent.assertEqual(result.last_hidden_state.shape						,  (self.batch_size, self.seq_length, self.hidden_size)				)





		def       _A							(							self    :	Optional[int]						,  lowerCamelCase__    :	Optional[int]						,  lowerCamelCase__    :	Optional[int]						,  lowerCamelCase__    :	Optional[Any]						,  lowerCamelCase__    :	int						,  lowerCamelCase__    :	List[Any]						,  lowerCamelCase__    :	str						,  lowerCamelCase__    :	Optional[int]						,  lowerCamelCase__    :	List[str]						,  lowerCamelCase__    :	Any						,  ):
							lowerCAmelCase		:					List[Any]  	=     TFXLMWithLMHeadModel(lowerCamelCase__				)

							lowerCAmelCase		:					Any  	=     {'''input_ids''': input_ids, '''lengths''': input_lengths, '''langs''': token_type_ids}
							lowerCAmelCase		:					List[Any]  	=     model(lowerCamelCase__				)

							lowerCAmelCase		:					str  	=     outputs

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





		def       _A							(							self    :	int						,  lowerCamelCase__    :	Optional[int]						,  lowerCamelCase__    :	Union[str, Any]						,  lowerCamelCase__    :	Optional[Any]						,  lowerCamelCase__    :	str						,  lowerCamelCase__    :	Dict						,  lowerCamelCase__    :	List[Any]						,  lowerCamelCase__    :	Optional[Any]						,  lowerCamelCase__    :	str						,  lowerCamelCase__    :	List[str]						,  ):
							lowerCAmelCase		:					str  	=     TFXLMForQuestionAnsweringSimple(lowerCamelCase__				)

							lowerCAmelCase		:					List[Any]  	=     {'''input_ids''': input_ids, '''lengths''': input_lengths}

							lowerCAmelCase		:					Optional[Any]  	=     model(lowerCamelCase__				)

							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       _A							(							self    :	int						,  lowerCamelCase__    :	List[Any]						,  lowerCamelCase__    :	Optional[int]						,  lowerCamelCase__    :	Dict						,  lowerCamelCase__    :	Union[str, Any]						,  lowerCamelCase__    :	Optional[int]						,  lowerCamelCase__    :	Optional[int]						,  lowerCamelCase__    :	Union[str, Any]						,  lowerCamelCase__    :	Dict						,  lowerCamelCase__    :	Any						,  ):
							lowerCAmelCase		:					int  	=     TFXLMForSequenceClassification(lowerCamelCase__				)

							lowerCAmelCase		:					Dict  	=     {'''input_ids''': input_ids, '''lengths''': input_lengths}

							lowerCAmelCase		:					Dict  	=     model(lowerCamelCase__				)

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





		def       _A							(							self    :	int						,  lowerCamelCase__    :	Union[str, Any]						,  lowerCamelCase__    :	int						,  lowerCamelCase__    :	int						,  lowerCamelCase__    :	str						,  lowerCamelCase__    :	str						,  lowerCamelCase__    :	List[str]						,  lowerCamelCase__    :	Optional[int]						,  lowerCamelCase__    :	Union[str, Any]						,  lowerCamelCase__    :	Tuple						,  ):
							lowerCAmelCase		:					Optional[Any]  	=     self.num_labels
							lowerCAmelCase		:					Tuple  	=     TFXLMForTokenClassification(config=lowerCamelCase__				)
							lowerCAmelCase		:					List[Any]  	=     {'''input_ids''': input_ids, '''attention_mask''': input_mask, '''token_type_ids''': token_type_ids}
							lowerCAmelCase		:					Tuple  	=     model(lowerCamelCase__				)
							self.parent.assertEqual(result.logits.shape						,  (self.batch_size, self.seq_length, self.num_labels)				)





		def       _A							(							self    :	List[Any]						,  lowerCamelCase__    :	Any						,  lowerCamelCase__    :	Optional[int]						,  lowerCamelCase__    :	List[str]						,  lowerCamelCase__    :	str						,  lowerCamelCase__    :	Dict						,  lowerCamelCase__    :	Optional[Any]						,  lowerCamelCase__    :	Optional[int]						,  lowerCamelCase__    :	Union[str, Any]						,  lowerCamelCase__    :	Optional[int]						,  ):
							lowerCAmelCase		:					List[str]  	=     self.num_choices
							lowerCAmelCase		:					Tuple  	=     TFXLMForMultipleChoice(config=lowerCamelCase__				)
							lowerCAmelCase		:					Optional[Any]  	=     tf.tile(tf.expand_dims(lowerCamelCase__						,  1				)						,  (1, self.num_choices, 1)				)
							lowerCAmelCase		:					int  	=     tf.tile(tf.expand_dims(lowerCamelCase__						,  1				)						,  (1, self.num_choices, 1)				)
							lowerCAmelCase		:					List[Any]  	=     tf.tile(tf.expand_dims(lowerCamelCase__						,  1				)						,  (1, self.num_choices, 1)				)
							lowerCAmelCase		:					Dict  	=     {
							    '''input_ids''': multiple_choice_inputs_ids,
							    '''attention_mask''': multiple_choice_input_mask,
							    '''token_type_ids''': multiple_choice_token_type_ids,
							}
							lowerCAmelCase		:					List[str]  	=     model(lowerCamelCase__				)
							self.parent.assertEqual(result.logits.shape						,  (self.batch_size, self.num_choices)				)





		def       _A							(							self    :	Optional[int]				):
							lowerCAmelCase		:					Optional[int]  	=     self.prepare_config_and_inputs()
							(
							    (
							    lowerCAmelCase
							)	,							(
							    lowerCAmelCase
							)	,							(
							    lowerCAmelCase
							)	,							(
							    lowerCAmelCase
							)	,							(
							    lowerCAmelCase
							)	,							(
							    lowerCAmelCase
							)	,							(
							    lowerCAmelCase
							)	,							(
							    lowerCAmelCase
							)	,							(
							    lowerCAmelCase
							)	,							
							)		:					List[str]  	=     config_and_inputs
							lowerCAmelCase		:					str  	=     {
							    '''input_ids''': input_ids,
							    '''token_type_ids''': token_type_ids,
							    '''langs''': token_type_ids,
							    '''lengths''': input_lengths,
							}
							return config, inputs_dict



@require_tf
class 		__magic_name__   (						snake_case,				snake_case,				unittest.TestCase    ):
		_lowerCAmelCase		=	(
		    (
		        TFXLMModel,
		        TFXLMWithLMHeadModel,
		        TFXLMForSequenceClassification,
		        TFXLMForQuestionAnsweringSimple,
		        TFXLMForTokenClassification,
		        TFXLMForMultipleChoice,
		    )
		    if is_tf_available()
		    else ()
		)
		_lowerCAmelCase		=	(
		    (TFXLMWithLMHeadModel,) if is_tf_available() else ()
		)  # TODO (PVP): Check other models whether language generation is also applicable
		_lowerCAmelCase		=	(
		    {
		        "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 {}
		)
		_lowerCAmelCase		=	False
		_lowerCAmelCase		=	False





		def       _A							(							self    :	Any						,  lowerCamelCase__    :	List[Any]						,  lowerCamelCase__    :	Dict						,  lowerCamelCase__    :	str						,  lowerCamelCase__    :	int						,  lowerCamelCase__    :	Tuple				):
							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       _A							(							self    :	Optional[Any]				):
							lowerCAmelCase		:					Dict  	=     TFXLMModelTester(self				)
							lowerCAmelCase		:					Union[str, Any]  	=     ConfigTester(self						,  config_class=lowerCamelCase__						,  emb_dim=3_7				)





		def       _A							(							self    :	Dict				):
							self.config_tester.run_common_tests()





		def       _A							(							self    :	Optional[Any]				):
							lowerCAmelCase		:					int  	=     self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_xlm_model(*lowerCamelCase__				)





		def       _A							(							self    :	List[Any]				):
							lowerCAmelCase		:					Tuple  	=     self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_xlm_lm_head(*lowerCamelCase__				)





		def       _A							(							self    :	Optional[Any]				):
							lowerCAmelCase		:					List[Any]  	=     self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_xlm_qa(*lowerCamelCase__				)





		def       _A							(							self    :	int				):
							lowerCAmelCase		:					int  	=     self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_xlm_sequence_classif(*lowerCamelCase__				)





		def       _A							(							self    :	int				):
							lowerCAmelCase		:					Union[str, Any]  	=     self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_xlm_for_token_classification(*lowerCamelCase__				)





		def       _A							(							self    :	int				):
							lowerCAmelCase		:					Optional[Any]  	=     self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_xlm_for_multiple_choice(*lowerCamelCase__				)





		@slow
		def       _A							(							self    :	Any				):
							for model_name in TF_XLM_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
												lowerCAmelCase		:					Union[str, Any]  	=     TFXLMModel.from_pretrained(lowerCamelCase__				)
												self.assertIsNotNone(lowerCamelCase__				)



@require_tf
class 		__magic_name__   (						unittest.TestCase    ):





		@slow
		def       _A							(							self    :	Tuple				):
							lowerCAmelCase		:					int  	=     TFXLMWithLMHeadModel.from_pretrained('''xlm-mlm-en-2048'''				)
							lowerCAmelCase		:					Dict  	=     tf.convert_to_tensor([[1_4, 4_4_7]]						,  dtype=tf.intaa				)  # the president
							lowerCAmelCase		:					str  	=     [
							    1_4,
							    4_4_7,
							    1_4,
							    4_4_7,
							    1_4,
							    4_4_7,
							    1_4,
							    4_4_7,
							    1_4,
							    4_4_7,
							    1_4,
							    4_4_7,
							    1_4,
							    4_4_7,
							    1_4,
							    4_4_7,
							    1_4,
							    4_4_7,
							    1_4,
							    4_4_7,
							]  # 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
							lowerCAmelCase		:					Dict  	=     model.generate(lowerCamelCase__						,  do_sample=lowerCamelCase__				)
							self.assertListEqual(output_ids[0].numpy().tolist()						,  lowerCamelCase__				)


from typing import TYPE_CHECKING

from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available, is_vision_available


__SCREAMING_SNAKE_CASE			:   List[str]         =     {'configuration_vilt': ['VILT_PRETRAINED_CONFIG_ARCHIVE_MAP', 'ViltConfig']}

try:
		if not is_vision_available():
				raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
		pass
else:
		__SCREAMING_SNAKE_CASE			:   Any         =     ['ViltFeatureExtractor']
		__SCREAMING_SNAKE_CASE			:   Optional[int]         =     ['ViltImageProcessor']
		__SCREAMING_SNAKE_CASE			:   str         =     ['ViltProcessor']

try:
		if not is_torch_available():
				raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
		pass
else:
		__SCREAMING_SNAKE_CASE			:   Dict         =     [
		    'VILT_PRETRAINED_MODEL_ARCHIVE_LIST',
		    'ViltForImageAndTextRetrieval',
		    'ViltForImagesAndTextClassification',
		    'ViltForTokenClassification',
		    'ViltForMaskedLM',
		    'ViltForQuestionAnswering',
		    'ViltLayer',
		    'ViltModel',
		    'ViltPreTrainedModel',
		]


if TYPE_CHECKING:
		from .configuration_vilt import VILT_PRETRAINED_CONFIG_ARCHIVE_MAP, ViltConfig

		try:
				if not is_vision_available():
						raise OptionalDependencyNotAvailable()
		except OptionalDependencyNotAvailable:
				pass
		else:
				from .feature_extraction_vilt import ViltFeatureExtractor
				from .image_processing_vilt import ViltImageProcessor
				from .processing_vilt import ViltProcessor

		try:
				if not is_torch_available():
						raise OptionalDependencyNotAvailable()
		except OptionalDependencyNotAvailable:
				pass
		else:
				from .modeling_vilt import (
				    VILT_PRETRAINED_MODEL_ARCHIVE_LIST,
				    ViltForImageAndTextRetrieval,
				    ViltForImagesAndTextClassification,
				    ViltForMaskedLM,
				    ViltForQuestionAnswering,
				    ViltForTokenClassification,
				    ViltLayer,
				    ViltModel,
				    ViltPreTrainedModel,
				)


else:
		import sys

		__SCREAMING_SNAKE_CASE			:   Optional[Any]         =     _LazyModule(__name__, globals()['__file__'], _import_structure)
