





"""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    :			Tuple	, a_    :			str	, a_    :			Any=13	, a_    :			Union[str, Any]=7	, a_    :			List[Any]=True	, a_    :			Union[str, Any]=True	, a_    :			Optional[Any]=True	, a_    :			int=True	, a_    :			int=True	, a_    :			Dict=False	, a_    :			int=False	, a_    :			Tuple=False	, a_    :			List[Any]=2	, a_    :			Tuple=99	, a_    :			Tuple=0	, a_    :			List[str]=32	, a_    :			Any=5	, a_    :			Union[str, Any]=4	, a_    :			List[Any]=0.1	, a_    :			str=0.1	, a_    :			List[str]=5_12	, a_    :			List[Any]=2	, a_    :			Union[str, Any]=0.02	, a_    :			Optional[Any]=2	, a_    :			List[str]=4	, a_    :			List[Any]="last"	, a_    :			List[Any]=True	, a_    :			List[str]=None	, a_    :			Union[str, Any]=0	, )	->	Optional[Any]:



							'''simple docstring'''



							a__  :	Optional[Any]		 =			parent
							a__  :	Any		 =			batch_size
							a__  :	Any		 =			seq_length
							a__  :	List[str]		 =			is_training
							a__  :	Dict		 =			use_input_lengths
							a__  :	Optional[Any]		 =			use_token_type_ids
							a__  :	Union[str, Any]		 =			use_labels
							a__  :	int		 =			gelu_activation
							a__  :	Dict		 =			sinusoidal_embeddings
							a__  :	str		 =			causal
							a__  :	int		 =			asm
							a__  :	Any		 =			n_langs
							a__  :	int		 =			vocab_size
							a__  :	str		 =			n_special
							a__  :	List[Any]		 =			hidden_size
							a__  :	List[Any]		 =			num_hidden_layers
							a__  :	List[Any]		 =			num_attention_heads
							a__  :	List[str]		 =			hidden_dropout_prob
							a__  :	Optional[Any]		 =			attention_probs_dropout_prob
							a__  :	Dict		 =			max_position_embeddings
							a__  :	Optional[Any]		 =			type_sequence_label_size
							a__  :	Optional[int]		 =			initializer_range
							a__  :	Optional[Any]		 =			num_labels
							a__  :	Optional[int]		 =			num_choices
							a__  :	Tuple		 =			summary_type
							a__  :	int		 =			use_proj
							a__  :	Union[str, Any]		 =			scope
							a__  :	int		 =			bos_token_id
				def       UpperCAmelCase      (		self    :			Optional[Any]    )	->	Any:



							'''simple docstring'''



							a__  :	int		 =			ids_tensor([self.batch_size, self.seq_length]	, self.vocab_size    )
							a__  :	Dict		 =			random_attention_mask([self.batch_size, self.seq_length]    )

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

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

							a__  :	Tuple		 =			None
							a__  :	Union[str, Any]		 =			None
							a__  :	List[str]		 =			None
							if self.use_labels:
										a__  :	List[Any]		 =			ids_tensor([self.batch_size]	, self.type_sequence_label_size    )
										a__  :	List[Any]		 =			ids_tensor([self.batch_size, self.seq_length]	, self.num_labels    )
										a__  :	Any		 =			ids_tensor([self.batch_size]	, 2    ).float()
										a__  :	Optional[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       UpperCAmelCase      (		self    :			int    )	->	int:



							'''simple docstring'''



							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       UpperCAmelCase      (		self    :			Tuple	, a_    :			Dict	, a_    :			Dict	, a_    :			str	, a_    :			Optional[Any]	, a_    :			int	, a_    :			str	, a_    :			Tuple	, a_    :			Any	, a_    :			Dict	, )	->	Any:



							'''simple docstring'''



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



							'''simple docstring'''



							a__  :	str		 =			XLMWithLMHeadModel(a_    )
							model.to(a_    )
							model.eval()

							a__  :	Union[str, Any]		 =			model(a_	, token_type_ids=a_	, labels=a_    )
							self.parent.assertEqual(result.loss.shape	, ()    )
							self.parent.assertEqual(result.logits.shape	, (self.batch_size, self.seq_length, self.vocab_size)    )
				def       UpperCAmelCase      (		self    :			int	, a_    :			int	, a_    :			Dict	, a_    :			List[Any]	, a_    :			Any	, a_    :			List[Any]	, a_    :			List[str]	, a_    :			Union[str, Any]	, a_    :			List[str]	, a_    :			int	, )	->	List[Any]:



							'''simple docstring'''



							a__  :	List[Any]		 =			XLMForQuestionAnsweringSimple(a_    )
							model.to(a_    )
							model.eval()

							a__  :	Optional[int]		 =			model(a_    )

							a__  :	List[str]		 =			model(a_	, start_positions=a_	, end_positions=a_    )
							a__  :	int		 =			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       UpperCAmelCase      (		self    :			Dict	, a_    :			List[str]	, a_    :			int	, a_    :			Optional[Any]	, a_    :			int	, a_    :			List[str]	, a_    :			List[str]	, a_    :			Optional[int]	, a_    :			Tuple	, a_    :			List[str]	, )	->	Tuple:



							'''simple docstring'''



							a__  :	int		 =			XLMForQuestionAnswering(a_    )
							model.to(a_    )
							model.eval()

							a__  :	Union[str, Any]		 =			model(a_    )

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

							a__  :	List[str]		 =			model(
							    a_	, start_positions=a_	, end_positions=a_	, cls_index=a_	, is_impossible=a_	, )

							(a__						)  :	int		 =			result_with_labels.to_tuple()

							a__  :	Union[str, Any]		 =			model(a_	, start_positions=a_	, end_positions=a_    )

							(a__						)  :	Optional[int]		 =			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       UpperCAmelCase      (		self    :			Union[str, Any]	, a_    :			Tuple	, a_    :			Tuple	, a_    :			List[Any]	, a_    :			int	, a_    :			List[Any]	, a_    :			List[Any]	, a_    :			List[Any]	, a_    :			Dict	, a_    :			List[str]	, )	->	List[Any]:



							'''simple docstring'''



							a__  :	Any		 =			XLMForSequenceClassification(a_    )
							model.to(a_    )
							model.eval()

							a__  :	Union[str, Any]		 =			model(a_    )
							a__  :	Optional[Any]		 =			model(a_	, labels=a_    )
							self.parent.assertEqual(result.loss.shape	, ()    )
							self.parent.assertEqual(result.logits.shape	, (self.batch_size, self.type_sequence_label_size)    )
				def       UpperCAmelCase      (		self    :			Any	, a_    :			List[Any]	, a_    :			Union[str, Any]	, a_    :			int	, a_    :			Dict	, a_    :			Dict	, a_    :			Optional[Any]	, a_    :			Dict	, a_    :			Tuple	, a_    :			Optional[int]	, )	->	Tuple:



							'''simple docstring'''



							a__  :	str		 =			self.num_labels
							a__  :	List[str]		 =			XLMForTokenClassification(a_    )
							model.to(a_    )
							model.eval()

							a__  :	Any		 =			model(a_	, attention_mask=a_	, labels=a_    )
							self.parent.assertEqual(result.logits.shape	, (self.batch_size, self.seq_length, self.num_labels)    )
				def       UpperCAmelCase      (		self    :			List[str]	, a_    :			Tuple	, a_    :			int	, a_    :			List[str]	, a_    :			Tuple	, a_    :			str	, a_    :			Union[str, Any]	, a_    :			Any	, a_    :			Optional[Any]	, a_    :			List[str]	, )	->	Union[str, Any]:



							'''simple docstring'''



							a__  :	str		 =			self.num_choices
							a__  :	Tuple		 =			XLMForMultipleChoice(config=a_    )
							model.to(a_    )
							model.eval()
							a__  :	Dict		 =			input_ids.unsqueeze(1    ).expand(-1	, self.num_choices	, -1    ).contiguous()
							a__  :	Dict		 =			token_type_ids.unsqueeze(1    ).expand(-1	, self.num_choices	, -1    ).contiguous()
							a__  :	Tuple		 =			input_mask.unsqueeze(1    ).expand(-1	, self.num_choices	, -1    ).contiguous()
							a__  :	Tuple		 =			model(
							    a_	, attention_mask=a_	, token_type_ids=a_	, labels=a_	, )
							self.parent.assertEqual(result.logits.shape	, (self.batch_size, self.num_choices)    )






				def       UpperCAmelCase      (		self    :			str    )	->	Any:



							'''simple docstring'''



							a__  :	Optional[int]		 =			self.prepare_config_and_inputs()
							(
							    a__						
							)  :	Any		 =			config_and_inputs
							a__  :	Optional[int]		 =			{"input_ids": input_ids, "token_type_ids": token_type_ids, "lengths": input_lengths}
							return config, inputs_dict



@require_torch
class 					__UpperCAmelCase							(						_UpperCamelCase						,  _UpperCamelCase						,  _UpperCamelCase						,  unittest.TestCase  ):
				__lowerCamelCase				:	str				     =				(
				    (
				        XLMModel,
				        XLMWithLMHeadModel,
				        XLMForQuestionAnswering,
				        XLMForSequenceClassification,
				        XLMForQuestionAnsweringSimple,
				        XLMForTokenClassification,
				        XLMForMultipleChoice,
				    )
				    if is_torch_available()
				    else ()
				)
				__lowerCamelCase				:	Optional[int]				     =				(
				    (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       UpperCAmelCase      (		self    :			Tuple	, a_    :			Tuple	, a_    :			str	, a_    :			Optional[int]	, a_    :			Union[str, Any]	, a_    :			int    )	->	Optional[Any]:



							'''simple docstring'''



							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    :			Union[str, Any]	, a_    :			List[str]	, a_    :			Dict	, a_    :			Any=False    )	->	Optional[int]:



							'''simple docstring'''



							a__  :	Optional[Any]		 =			super()._prepare_for_class(a_	, a_	, return_labels=a_    )

							if return_labels:
										if model_class.__name__ == "XLMForQuestionAnswering":
													a__  :	Any		 =			torch.zeros(
													    self.model_tester.batch_size	, dtype=torch.long	, device=a_    )
													a__  :	List[str]		 =			torch.zeros(
													    self.model_tester.batch_size	, dtype=torch.long	, device=a_    )

							return inputs_dict
				def       UpperCAmelCase      (		self    :			Union[str, Any]    )	->	List[Any]:



							'''simple docstring'''



							a__  :	List[str]		 =			XLMModelTester(self    )
							a__  :	Dict		 =			ConfigTester(self	, config_class=a_	, emb_dim=37    )
				def       UpperCAmelCase      (		self    :			List[Any]    )	->	int:



							'''simple docstring'''



							self.config_tester.run_common_tests()
				def       UpperCAmelCase      (		self    :			Dict    )	->	Any:



							'''simple docstring'''



							a__  :	Union[str, Any]		 =			self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_xlm_model(*a_    )
				def       UpperCAmelCase      (		self    :			int    )	->	Dict:



							'''simple docstring'''



							a__  :	Tuple		 =			self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_xlm_lm_head(*a_    )
				def       UpperCAmelCase      (		self    :			Union[str, Any]    )	->	Union[str, Any]:



							'''simple docstring'''



							a__  :	Union[str, Any]		 =			self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_xlm_simple_qa(*a_    )
				def       UpperCAmelCase      (		self    :			Union[str, Any]    )	->	str:



							'''simple docstring'''



							a__  :	Optional[int]		 =			self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_xlm_qa(*a_    )
				def       UpperCAmelCase      (		self    :			int    )	->	List[Any]:



							'''simple docstring'''



							a__  :	int		 =			self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_xlm_sequence_classif(*a_    )
				def       UpperCAmelCase      (		self    :			Optional[Any]    )	->	Tuple:



							'''simple docstring'''



							a__  :	List[str]		 =			self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_xlm_token_classif(*a_    )
				def       UpperCAmelCase      (		self    :			Dict    )	->	Dict:



							'''simple docstring'''



							a__  :	int		 =			self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_xlm_for_multiple_choice(*a_    )
				def       UpperCAmelCase      (		self    :			Optional[Any]	, a_    :			Any	, a_    :			int	, a_    :			Optional[Any]	, a_    :			List[str]	, a_    :			Optional[Any]	, a_    :			Any=False	, a_    :			Optional[Any]=1    )	->	str:



							'''simple docstring'''



							self.assertIsInstance(a_	, a_    )
							self.assertListEqual(
							    [isinstance(a_	, a_    ) for iter_attentions in attentions]	, [True] * len(a_    )    )
							self.assertEqual(len(a_    )	, (max_length - min_length) * num_beam_groups    )

							for idx, iter_attentions in enumerate(a_    ):
										# adds PAD dummy token
										a__  :	Union[str, Any]		 =			min_length + idx + 1
										a__  :	Dict		 =			min_length + idx + 1

										a__  :	List[Any]		 =			(
										    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(a_    )    )
				def       UpperCAmelCase      (		self    :			Union[str, Any]	, a_    :			Union[str, Any]	, a_    :			Tuple	, a_    :			List[Any]	, a_    :			Optional[int]	, a_    :			str	, a_    :			int=False	, a_    :			Optional[Any]=1    )	->	Optional[int]:



							'''simple docstring'''



							self.assertIsInstance(a_	, a_    )
							self.assertListEqual(
							    [isinstance(a_	, a_    ) for iter_hidden_states in hidden_states]	, [True] * len(a_    )	, )
							self.assertEqual(len(a_    )	, (max_length - min_length) * num_beam_groups    )

							for idx, iter_hidden_states in enumerate(a_    ):
										# adds PAD dummy token
										a__  :	List[str]		 =			min_length + idx + 1
										a__  :	List[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(a_    )	, )
							pass






				@slow
				def       UpperCAmelCase      (		self    :			int    )	->	List[str]:



							'''simple docstring'''



							for model_name in XLM_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
										a__  :	List[str]		 =			XLMModel.from_pretrained(a_    )
										self.assertIsNotNone(a_    )



@require_torch
class 					__UpperCAmelCase							(						unittest.TestCase  ):
				@slow
				def       UpperCAmelCase      (		self    :			Tuple    )	->	Any:



							'''simple docstring'''



							a__  :	Any		 =			XLMWithLMHeadModel.from_pretrained("xlm-mlm-en-2048"    )
							model.to(a_    )
							a__  :	Tuple		 =			torch.tensor([[14, 4_47]]	, dtype=torch.long	, device=a_    )  # the president
							a__  :	Any		 =			[
							    14,
							    4_47,
							    14,
							    4_47,
							    14,
							    4_47,
							    14,
							    4_47,
							    14,
							    4_47,
							    14,
							    4_47,
							    14,
							    4_47,
							    14,
							    4_47,
							    14,
							    4_47,
							    14,
							    4_47,
							]  # 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__  :	Dict		 =			model.generate(a_	, do_sample=a_    )
							self.assertListEqual(output_ids[0].cpu().numpy().tolist()	, a_    )


"""simple docstring"""

from typing import Union

from ..utils import add_end_docstrings, is_torch_available, is_vision_available, logging
from .base import PIPELINE_INIT_ARGS, Pipeline


if is_vision_available():
    from PIL import Image

    from ..image_utils import load_image

if is_torch_available():
    from ..models.auto.modeling_auto import MODEL_FOR_VISUAL_QUESTION_ANSWERING_MAPPING

_UpperCamelCase		     =    logging.get_logger(__name__)






@add_end_docstrings(__magic_name__       )
class 			__a					(						__magic_name__       ):





       """simple docstring"""


       def __init__(   self			,					*snake_case			,					**snake_case   ):





        """simple docstring"""

        super().__init__(*snake_case			,					**snake_case   )
        self.check_model_type(snake_case   )






       def 			SCREAMING_SNAKE_CASE_	(   self			,					snake_case=None			,					snake_case=None			,					snake_case=None			,					**snake_case   ):





        """simple docstring"""

        lowerCAmelCase__			,						lowerCAmelCase__   :				List[Any] 			=				{}, {}
        if padding is not None:
         lowerCAmelCase__   :				Dict 			=				padding
        if truncation is not None:
         lowerCAmelCase__   :				Tuple 			=				truncation
        if top_k is not None:
         lowerCAmelCase__   :				str 			=				top_k
        return preprocess_params, {}, postprocess_params






       def __call__(   self			,					snake_case			,					snake_case = None			,					**snake_case   ):





        """simple docstring"""

        if isinstance(snake_case			,					(Image.Image, str)   ) and isinstance(snake_case			,					snake_case   ):
         lowerCAmelCase__   :				int 			=				{"image": image, "question": question}
        else:
         lowerCAmelCase__   :				Optional[int] 			=				image
        lowerCAmelCase__   :				Optional[Any] 			=				super().__call__(snake_case			,					**snake_case   )
        return results






       def 			SCREAMING_SNAKE_CASE_	(   self			,					snake_case			,					snake_case=False			,					snake_case=False   ):





        """simple docstring"""

        lowerCAmelCase__   :				Union[str, Any] 			=				load_image(inputs["image"]   )
        lowerCAmelCase__   :				str 			=				self.tokenizer(
            inputs["question"]			,					return_tensors=self.framework			,					padding=snake_case			,					truncation=snake_case   )
        lowerCAmelCase__   :				int 			=				self.image_processor(images=snake_case			,					return_tensors=self.framework   )
        model_inputs.update(snake_case   )
        return model_inputs






       def 			SCREAMING_SNAKE_CASE_	(   self			,					snake_case   ):





        """simple docstring"""

        lowerCAmelCase__   :				Optional[int] 			=				self.model(**snake_case   )
        return model_outputs






       def 			SCREAMING_SNAKE_CASE_	(   self			,					snake_case			,					snake_case=5   ):





        """simple docstring"""

        if top_k > self.model.config.num_labels:
         lowerCAmelCase__   :				List[Any] 			=				self.model.config.num_labels

        if self.framework == "pt":
         lowerCAmelCase__   :				Optional[int] 			=				model_outputs.logits.sigmoid()[0]
         lowerCAmelCase__			,						lowerCAmelCase__   :				Optional[int] 			=				probs.topk(snake_case   )
        else:
         raise ValueError(F"""Unsupported framework: {self.framework}"""   )

        lowerCAmelCase__   :				int 			=				scores.tolist()
        lowerCAmelCase__   :				str 			=				ids.tolist()
        return [{"score": score, "answer": self.model.config.idalabel[_id]} for score, _id in zip(snake_case			,					snake_case   )]






