





"""simple docstring"""




import unittest

from transformers import XLMConfig, is_torch_available
from transformers.testing_utils import require_torch, slow, torch_device

from ...generation.test_utils import GenerationTesterMixin
from ...test_configuration_common import ConfigTester
from ...test_modeling_common import ModelTesterMixin, ids_tensor, random_attention_mask
from ...test_pipeline_mixin import PipelineTesterMixin


if is_torch_available():
		import torch

		from transformers import (
		    XLMForMultipleChoice,
		    XLMForQuestionAnswering,
		    XLMForQuestionAnsweringSimple,
		    XLMForSequenceClassification,
		    XLMForTokenClassification,
		    XLMModel,
		    XLMWithLMHeadModel,
		)
		from transformers.models.xlm.modeling_xlm import XLM_PRETRAINED_MODEL_ARCHIVE_LIST


class 			__UpperCamelCase							:






							def __init__(  self ,   lowerCAmelCase__ ,   lowerCAmelCase__=13 ,   lowerCAmelCase__=7 ,   lowerCAmelCase__=True ,   lowerCAmelCase__=True ,   lowerCAmelCase__=True ,   lowerCAmelCase__=True ,   lowerCAmelCase__=True ,   lowerCAmelCase__=False ,   lowerCAmelCase__=False ,   lowerCAmelCase__=False ,   lowerCAmelCase__=2 ,   lowerCAmelCase__=99 ,   lowerCAmelCase__=0 ,   lowerCAmelCase__=32 ,   lowerCAmelCase__=5 ,   lowerCAmelCase__=4 ,   lowerCAmelCase__=0.1 ,   lowerCAmelCase__=0.1 ,   lowerCAmelCase__=512 ,   lowerCAmelCase__=2 ,   lowerCAmelCase__=0.02 ,   lowerCAmelCase__=2 ,   lowerCAmelCase__=4 ,   lowerCAmelCase__="last" ,   lowerCAmelCase__=True ,   lowerCAmelCase__=None ,   lowerCAmelCase__=0 ,   )      ->					Optional[int]:
								a :						Optional[Any]      =							parent
								a :						str      =							batch_size
								a :						List[Any]      =							seq_length
								a :						Optional[Any]      =							is_training
								a :						Optional[int]      =							use_input_lengths
								a :						Optional[Any]      =							use_token_type_ids
								a :						Union[str, Any]      =							use_labels
								a :						Union[str, Any]      =							gelu_activation
								a :						Any      =							sinusoidal_embeddings
								a :						Dict      =							causal
								a :						List[str]      =							asm
								a :						int      =							n_langs
								a :						int      =							vocab_size
								a :						Any      =							n_special
								a :						List[Any]      =							hidden_size
								a :						Union[str, Any]      =							num_hidden_layers
								a :						Optional[int]      =							num_attention_heads
								a :						Optional[Any]      =							hidden_dropout_prob
								a :						List[str]      =							attention_probs_dropout_prob
								a :						Dict      =							max_position_embeddings
								a :						Union[str, Any]      =							type_sequence_label_size
								a :						int      =							initializer_range
								a :						List[Any]      =							num_labels
								a :						Optional[Any]      =							num_choices
								a :						Optional[Any]      =							summary_type
								a :						Optional[int]      =							use_proj
								a :						Optional[Any]      =							scope
								a :						Optional[int]      =							bos_token_id






							def        __a  (  self )      ->					Any:
								a :						Optional[int]      =							ids_tensor([self.batch_size, self.seq_length] ,   self.vocab_size )
								a :						Any      =							random_attention_mask([self.batch_size, self.seq_length] )

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

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

								a :						Optional[Any]      =							None
								a :						Optional[Any]      =							None
								a :						Dict      =							None
								if self.use_labels:
									a :						Union[str, Any]      =							ids_tensor([self.batch_size] ,   self.type_sequence_label_size )
									a :						List[str]      =							ids_tensor([self.batch_size, self.seq_length] ,   self.num_labels )
									a :						Any      =							ids_tensor([self.batch_size] ,   2 ).float()
									a :						Union[str, Any]      =							ids_tensor([self.batch_size] ,   self.num_choices )

								a :						Optional[Any]      =							self.get_config()

								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]:
								return 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 ,   num_labels=self.num_labels ,   bos_token_id=self.bos_token_id ,   )






							def        __a  (  self ,   lowerCAmelCase__ ,   lowerCAmelCase__ ,   lowerCAmelCase__ ,   lowerCAmelCase__ ,   lowerCAmelCase__ ,   lowerCAmelCase__ ,   lowerCAmelCase__ ,   lowerCAmelCase__ ,   lowerCAmelCase__ ,   )      ->					Any:
								a :						List[str]      =							XLMModel(config=lowerCAmelCase__ )
								model.to(lowerCAmelCase__ )
								model.eval()
								a :						List[Any]      =							model(lowerCAmelCase__ ,   lengths=lowerCAmelCase__ ,   langs=lowerCAmelCase__ )
								a :						Tuple      =							model(lowerCAmelCase__ ,   langs=lowerCAmelCase__ )
								a :						int      =							model(lowerCAmelCase__ )
								self.parent.assertEqual(result.last_hidden_state.shape ,   (self.batch_size, self.seq_length, self.hidden_size) )






							def        __a  (  self ,   lowerCAmelCase__ ,   lowerCAmelCase__ ,   lowerCAmelCase__ ,   lowerCAmelCase__ ,   lowerCAmelCase__ ,   lowerCAmelCase__ ,   lowerCAmelCase__ ,   lowerCAmelCase__ ,   lowerCAmelCase__ ,   )      ->					Union[str, Any]:
								a :						Optional[int]      =							XLMWithLMHeadModel(lowerCAmelCase__ )
								model.to(lowerCAmelCase__ )
								model.eval()

								a :						List[str]      =							model(lowerCAmelCase__ ,   token_type_ids=lowerCAmelCase__ ,   labels=lowerCAmelCase__ )
								self.parent.assertEqual(result.loss.shape ,   () )
								self.parent.assertEqual(result.logits.shape ,   (self.batch_size, self.seq_length, self.vocab_size) )






							def        __a  (  self ,   lowerCAmelCase__ ,   lowerCAmelCase__ ,   lowerCAmelCase__ ,   lowerCAmelCase__ ,   lowerCAmelCase__ ,   lowerCAmelCase__ ,   lowerCAmelCase__ ,   lowerCAmelCase__ ,   lowerCAmelCase__ ,   )      ->					Dict:
								a :						List[str]      =							XLMForQuestionAnsweringSimple(lowerCAmelCase__ )
								model.to(lowerCAmelCase__ )
								model.eval()

								a :						Optional[Any]      =							model(lowerCAmelCase__ )

								a :						List[str]      =							model(lowerCAmelCase__ ,   start_positions=lowerCAmelCase__ ,   end_positions=lowerCAmelCase__ )
								a :						Union[str, Any]      =							outputs
								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 ,   lowerCAmelCase__ ,   lowerCAmelCase__ ,   lowerCAmelCase__ ,   lowerCAmelCase__ ,   lowerCAmelCase__ ,   lowerCAmelCase__ ,   lowerCAmelCase__ ,   lowerCAmelCase__ ,   lowerCAmelCase__ ,   )      ->					Any:
								a :						Union[str, Any]      =							XLMForQuestionAnswering(lowerCAmelCase__ )
								model.to(lowerCAmelCase__ )
								model.eval()

								a :						int      =							model(lowerCAmelCase__ )

								a :						Union[str, Any]      =							model(
								    lowerCAmelCase__ ,   start_positions=lowerCAmelCase__ ,   end_positions=lowerCAmelCase__ ,   cls_index=lowerCAmelCase__ ,   is_impossible=lowerCAmelCase__ ,   p_mask=lowerCAmelCase__ ,   )

								a :						str      =							model(
								    lowerCAmelCase__ ,   start_positions=lowerCAmelCase__ ,   end_positions=lowerCAmelCase__ ,   cls_index=lowerCAmelCase__ ,   is_impossible=lowerCAmelCase__ ,   )

								((a),			) :						Dict      =							result_with_labels.to_tuple()

								a :						Tuple      =							model(lowerCAmelCase__ ,   start_positions=lowerCAmelCase__ ,   end_positions=lowerCAmelCase__ )

								((a),			) :						Any      =							result_with_labels.to_tuple()

								self.parent.assertEqual(result_with_labels.loss.shape ,   () )
								self.parent.assertEqual(result.start_top_log_probs.shape ,   (self.batch_size, model.config.start_n_top) )
								self.parent.assertEqual(result.start_top_index.shape ,   (self.batch_size, model.config.start_n_top) )
								self.parent.assertEqual(
								    result.end_top_log_probs.shape ,   (self.batch_size, model.config.start_n_top * model.config.end_n_top) )
								self.parent.assertEqual(
								    result.end_top_index.shape ,   (self.batch_size, model.config.start_n_top * model.config.end_n_top) )
								self.parent.assertEqual(result.cls_logits.shape ,   (self.batch_size,) )






							def        __a  (  self ,   lowerCAmelCase__ ,   lowerCAmelCase__ ,   lowerCAmelCase__ ,   lowerCAmelCase__ ,   lowerCAmelCase__ ,   lowerCAmelCase__ ,   lowerCAmelCase__ ,   lowerCAmelCase__ ,   lowerCAmelCase__ ,   )      ->					Tuple:
								a :						Any      =							XLMForSequenceClassification(lowerCAmelCase__ )
								model.to(lowerCAmelCase__ )
								model.eval()

								a :						Dict      =							model(lowerCAmelCase__ )
								a :						List[Any]      =							model(lowerCAmelCase__ ,   labels=lowerCAmelCase__ )
								self.parent.assertEqual(result.loss.shape ,   () )
								self.parent.assertEqual(result.logits.shape ,   (self.batch_size, self.type_sequence_label_size) )






							def        __a  (  self ,   lowerCAmelCase__ ,   lowerCAmelCase__ ,   lowerCAmelCase__ ,   lowerCAmelCase__ ,   lowerCAmelCase__ ,   lowerCAmelCase__ ,   lowerCAmelCase__ ,   lowerCAmelCase__ ,   lowerCAmelCase__ ,   )      ->					Dict:
								a :						Dict      =							self.num_labels
								a :						int      =							XLMForTokenClassification(lowerCAmelCase__ )
								model.to(lowerCAmelCase__ )
								model.eval()

								a :						Tuple      =							model(lowerCAmelCase__ ,   attention_mask=lowerCAmelCase__ ,   labels=lowerCAmelCase__ )
								self.parent.assertEqual(result.logits.shape ,   (self.batch_size, self.seq_length, self.num_labels) )






							def        __a  (  self ,   lowerCAmelCase__ ,   lowerCAmelCase__ ,   lowerCAmelCase__ ,   lowerCAmelCase__ ,   lowerCAmelCase__ ,   lowerCAmelCase__ ,   lowerCAmelCase__ ,   lowerCAmelCase__ ,   lowerCAmelCase__ ,   )      ->					Optional[Any]:
								a :						Tuple      =							self.num_choices
								a :						List[str]      =							XLMForMultipleChoice(config=lowerCAmelCase__ )
								model.to(lowerCAmelCase__ )
								model.eval()
								a :						List[str]      =							input_ids.unsqueeze(1 ).expand(-1 ,   self.num_choices ,   -1 ).contiguous()
								a :						Optional[int]      =							token_type_ids.unsqueeze(1 ).expand(-1 ,   self.num_choices ,   -1 ).contiguous()
								a :						List[Any]      =							input_mask.unsqueeze(1 ).expand(-1 ,   self.num_choices ,   -1 ).contiguous()
								a :						Union[str, Any]      =							model(
								    lowerCAmelCase__ ,   attention_mask=lowerCAmelCase__ ,   token_type_ids=lowerCAmelCase__ ,   labels=lowerCAmelCase__ ,   )
								self.parent.assertEqual(result.logits.shape ,   (self.batch_size, self.num_choices) )






							def        __a  (  self )      ->					Optional[int]:
								a :						int      =							self.prepare_config_and_inputs()
								(
								    (
								    a
								),			(
								    a
								),			(
								    a
								),			(
								    a
								),			(
								    a
								),			(
								    a
								),			(
								    a
								),			(
								    a
								),			(
								    a
								),			
								) :						Union[str, Any]      =							config_and_inputs
								a :						int      =							{"input_ids": input_ids, "token_type_ids": token_type_ids, "lengths": input_lengths}
								return config, inputs_dict


@require_torch
class 			__UpperCamelCase							(		a__						,			a__						,			a__						,			unittest.TestCase		):
							lowerCamelCase  :   List[str]    =(
							    (
							        XLMModel,
							        XLMWithLMHeadModel,
							        XLMForQuestionAnswering,
							        XLMForSequenceClassification,
							        XLMForQuestionAnsweringSimple,
							        XLMForTokenClassification,
							        XLMForMultipleChoice,
							    )
							    if is_torch_available()
							    else ()
							)
							lowerCamelCase  :   str    =(
							    (XLMWithLMHeadModel,) if is_torch_available() else ()
							)  # TODO (PVP): Check other models whether language generation is also applicable
							lowerCamelCase  :   List[Any]    =(
							    {
							        """feature-extraction""": XLMModel,
							        """fill-mask""": XLMWithLMHeadModel,
							        """question-answering""": XLMForQuestionAnsweringSimple,
							        """text-classification""": XLMForSequenceClassification,
							        """text-generation""": XLMWithLMHeadModel,
							        """token-classification""": XLMForTokenClassification,
							        """zero-shot""": XLMForSequenceClassification,
							    }
							    if is_torch_available()
							    else {}
							)






							def        __a  (  self ,   lowerCAmelCase__ ,   lowerCAmelCase__ ,   lowerCAmelCase__ ,   lowerCAmelCase__ ,   lowerCAmelCase__ )      ->					int:
								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 ,   lowerCAmelCase__ ,   lowerCAmelCase__ ,   lowerCAmelCase__=False )      ->					int:
								a :						Any      =							super()._prepare_for_class(lowerCAmelCase__ ,   lowerCAmelCase__ ,   return_labels=lowerCAmelCase__ )

								if return_labels:
									if model_class.__name__ == "XLMForQuestionAnswering":
										a :						Optional[int]      =							torch.zeros(
										    self.model_tester.batch_size ,   dtype=torch.long ,   device=lowerCAmelCase__ )
										a :						Any      =							torch.zeros(
										    self.model_tester.batch_size ,   dtype=torch.long ,   device=lowerCAmelCase__ )

								return inputs_dict






							def        __a  (  self )      ->					Optional[int]:
								a :						List[Any]      =							XLMModelTester(self )
								a :						List[Any]      =							ConfigTester(self ,   config_class=lowerCAmelCase__ ,   emb_dim=37 )






							def        __a  (  self )      ->					List[str]:
								self.config_tester.run_common_tests()






							def        __a  (  self )      ->					int:
								a :						str      =							self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_xlm_model(*lowerCAmelCase__ )






							def        __a  (  self )      ->					Optional[int]:
								a :						List[Any]      =							self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_xlm_lm_head(*lowerCAmelCase__ )






							def        __a  (  self )      ->					int:
								a :						str      =							self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_xlm_simple_qa(*lowerCAmelCase__ )






							def        __a  (  self )      ->					List[Any]:
								a :						List[str]      =							self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_xlm_qa(*lowerCAmelCase__ )






							def        __a  (  self )      ->					int:
								a :						str      =							self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_xlm_sequence_classif(*lowerCAmelCase__ )






							def        __a  (  self )      ->					Optional[int]:
								a :						Tuple      =							self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_xlm_token_classif(*lowerCAmelCase__ )






							def        __a  (  self )      ->					List[str]:
								a :						Any      =							self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_xlm_for_multiple_choice(*lowerCAmelCase__ )






							def        __a  (  self ,   lowerCAmelCase__ ,   lowerCAmelCase__ ,   lowerCAmelCase__ ,   lowerCAmelCase__ ,   lowerCAmelCase__ ,   lowerCAmelCase__=False ,   lowerCAmelCase__=1 )      ->					Optional[Any]:
								self.assertIsInstance(lowerCAmelCase__ ,   lowerCAmelCase__ )
								self.assertListEqual(
								    [isinstance(lowerCAmelCase__ ,   lowerCAmelCase__ ) for iter_attentions in attentions] ,   [True] * len(lowerCAmelCase__ ) )
								self.assertEqual(len(lowerCAmelCase__ ) ,   (max_length - min_length) * num_beam_groups )

								for idx, iter_attentions in enumerate(lowerCAmelCase__ ):
									# adds PAD dummy token
									a :						List[Any]      =							min_length + idx + 1
									a :						str      =							min_length + idx + 1

									a :						int      =							(
									    batch_size * num_beam_groups,
									    config.num_attention_heads,
									    tgt_len,
									    src_len,
									)
									# check attn size
									self.assertListEqual(
									    [layer_attention.shape for layer_attention in iter_attentions] ,   [expected_shape] * len(lowerCAmelCase__ ) )






							def        __a  (  self ,   lowerCAmelCase__ ,   lowerCAmelCase__ ,   lowerCAmelCase__ ,   lowerCAmelCase__ ,   lowerCAmelCase__ ,   lowerCAmelCase__=False ,   lowerCAmelCase__=1 )      ->					Optional[int]:
								self.assertIsInstance(lowerCAmelCase__ ,   lowerCAmelCase__ )
								self.assertListEqual(
								    [isinstance(lowerCAmelCase__ ,   lowerCAmelCase__ ) for iter_hidden_states in hidden_states] ,   [True] * len(lowerCAmelCase__ ) ,   )
								self.assertEqual(len(lowerCAmelCase__ ) ,   (max_length - min_length) * num_beam_groups )

								for idx, iter_hidden_states in enumerate(lowerCAmelCase__ ):
									# adds PAD dummy token
									a :						int      =							min_length + idx + 1
									a :						Any      =							(batch_size * num_beam_groups, seq_len, config.hidden_size)
									# check hidden size
									self.assertListEqual(
									    [layer_hidden_states.shape for layer_hidden_states in iter_hidden_states] ,   [expected_shape] * len(lowerCAmelCase__ ) ,   )
								pass






							@slow
							def        __a  (  self )      ->					Union[str, Any]:
								for model_name in XLM_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
									a :						List[str]      =							XLMModel.from_pretrained(lowerCAmelCase__ )
									self.assertIsNotNone(lowerCAmelCase__ )


@require_torch
class 			__UpperCamelCase							(		unittest.TestCase		):






							@slow
							def        __a  (  self )      ->					str:
								a :						Any      =							XLMWithLMHeadModel.from_pretrained("xlm-mlm-en-2048" )
								model.to(lowerCAmelCase__ )
								a :						int      =							torch.tensor([[14, 447]] ,   dtype=torch.long ,   device=lowerCAmelCase__ )  # the president
								a :						List[str]      =							[
								    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
								a :						Optional[Any]      =							model.generate(lowerCAmelCase__ ,   do_sample=lowerCAmelCase__ )
								self.assertListEqual(output_ids[0].cpu().numpy().tolist() ,   lowerCAmelCase__ )









"""simple docstring"""




from typing import TYPE_CHECKING

from ...utils import (
    OptionalDependencyNotAvailable,
    _LazyModule,
    is_flax_available,
    is_tf_available,
    is_tokenizers_available,
    is_torch_available,
    is_vision_available,
)


a		:					Optional[Any]	      =     {
    '''configuration_owlvit''': [
        '''OWLVIT_PRETRAINED_CONFIG_ARCHIVE_MAP''',
        '''OwlViTConfig''',
        '''OwlViTOnnxConfig''',
        '''OwlViTTextConfig''',
        '''OwlViTVisionConfig''',
    ],
    '''processing_owlvit''': ['''OwlViTProcessor'''],
}


try:
		if not is_vision_available():
				raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
		pass
else:
		a		:					str	      =     ['''OwlViTFeatureExtractor''']
		a		:					List[Any]	      =     ['''OwlViTImageProcessor''']

try:
		if not is_torch_available():
				raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
		pass
else:
		a		:					List[Any]	      =     [
		    '''OWLVIT_PRETRAINED_MODEL_ARCHIVE_LIST''',
		    '''OwlViTModel''',
		    '''OwlViTPreTrainedModel''',
		    '''OwlViTTextModel''',
		    '''OwlViTVisionModel''',
		    '''OwlViTForObjectDetection''',
		]

if TYPE_CHECKING:
		from .configuration_owlvit import (
		    OWLVIT_PRETRAINED_CONFIG_ARCHIVE_MAP,
		    OwlViTConfig,
		    OwlViTOnnxConfig,
		    OwlViTTextConfig,
		    OwlViTVisionConfig,
		)
		from .processing_owlvit import OwlViTProcessor

		try:
				if not is_vision_available():
						raise OptionalDependencyNotAvailable()
		except OptionalDependencyNotAvailable:
				pass
		else:
				from .feature_extraction_owlvit import OwlViTFeatureExtractor
				from .image_processing_owlvit import OwlViTImageProcessor

		try:
				if not is_torch_available():
						raise OptionalDependencyNotAvailable()
		except OptionalDependencyNotAvailable:
				pass
		else:
				from .modeling_owlvit import (
				    OWLVIT_PRETRAINED_MODEL_ARCHIVE_LIST,
				    OwlViTForObjectDetection,
				    OwlViTModel,
				    OwlViTPreTrainedModel,
				    OwlViTTextModel,
				    OwlViTVisionModel,
				)

else:
		import sys

		a		:					List[str]	      =     _LazyModule(__name__, globals()['''__file__'''], _import_structure, module_spec=__spec__)


