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






    '''simple docstring'''






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







    def     UpperCamelCase__     (			self) ->			int:
        __UpperCamelCase  :Union[str, Any]    						=	ids_tensor([self.batch_size, self.seq_length]				,				self.vocab_size)
        __UpperCamelCase  :Union[str, Any]    						=	random_attention_mask([self.batch_size, self.seq_length])

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

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

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

        __UpperCamelCase  :str    						=	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) ->			Any:
        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				,				__lowercase				,				__lowercase				,				__lowercase				,				__lowercase				,				__lowercase				,				__lowercase				,				__lowercase				,				__lowercase				,				__lowercase				,				) ->			Tuple:
        __UpperCamelCase  :str    						=	XLMModel(config=_lowerCamelCase)
        model.to(_lowerCamelCase)
        model.eval()
        __UpperCamelCase  :Any    						=	model(_lowerCamelCase				,				lengths=_lowerCamelCase				,				langs=_lowerCamelCase)
        __UpperCamelCase  :Any    						=	model(_lowerCamelCase				,				langs=_lowerCamelCase)
        __UpperCamelCase  :List[Any]    						=	model(_lowerCamelCase)
        self.parent.assertEqual(result.last_hidden_state.shape				,				(self.batch_size, self.seq_length, self.hidden_size))







    def     UpperCamelCase__     (			self				,				__lowercase				,				__lowercase				,				__lowercase				,				__lowercase				,				__lowercase				,				__lowercase				,				__lowercase				,				__lowercase				,				__lowercase				,				) ->			int:
        __UpperCamelCase  :Tuple    						=	XLMWithLMHeadModel(_lowerCamelCase)
        model.to(_lowerCamelCase)
        model.eval()

        __UpperCamelCase  :Tuple    						=	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     UpperCamelCase__     (			self				,				__lowercase				,				__lowercase				,				__lowercase				,				__lowercase				,				__lowercase				,				__lowercase				,				__lowercase				,				__lowercase				,				__lowercase				,				) ->			Tuple:
        __UpperCamelCase  :Tuple    						=	XLMForQuestionAnsweringSimple(_lowerCamelCase)
        model.to(_lowerCamelCase)
        model.eval()

        __UpperCamelCase  :Any    						=	model(_lowerCamelCase)

        __UpperCamelCase  :List[Any]    						=	model(_lowerCamelCase				,				start_positions=_lowerCamelCase				,				end_positions=_lowerCamelCase)
        __UpperCamelCase  :Optional[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				,				__lowercase				,				__lowercase				,				__lowercase				,				__lowercase				,				__lowercase				,				__lowercase				,				__lowercase				,				__lowercase				,				__lowercase				,				) ->			Tuple:
        __UpperCamelCase  :Tuple    						=	XLMForQuestionAnswering(_lowerCamelCase)
        model.to(_lowerCamelCase)
        model.eval()

        __UpperCamelCase  :int    						=	model(_lowerCamelCase)

        __UpperCamelCase  :Dict    						=	model(
            _lowerCamelCase				,				start_positions=_lowerCamelCase				,				end_positions=_lowerCamelCase				,				cls_index=_lowerCamelCase				,				is_impossible=_lowerCamelCase				,				p_mask=_lowerCamelCase				,				)

        __UpperCamelCase  :Any    						=	model(
            _lowerCamelCase				,				start_positions=_lowerCamelCase				,				end_positions=_lowerCamelCase				,				cls_index=_lowerCamelCase				,				is_impossible=_lowerCamelCase				,				)

        ((__UpperCamelCase	)						, 	)  :Tuple    						=	result_with_labels.to_tuple()

        __UpperCamelCase  :List[Any]    						=	model(_lowerCamelCase				,				start_positions=_lowerCamelCase				,				end_positions=_lowerCamelCase)

        ((__UpperCamelCase	)						, 	)  :List[str]    						=	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				,				__lowercase				,				__lowercase				,				__lowercase				,				__lowercase				,				__lowercase				,				__lowercase				,				__lowercase				,				__lowercase				,				__lowercase				,				) ->			Optional[int]:
        __UpperCamelCase  :Optional[Any]    						=	XLMForSequenceClassification(_lowerCamelCase)
        model.to(_lowerCamelCase)
        model.eval()

        __UpperCamelCase  :List[str]    						=	model(_lowerCamelCase)
        __UpperCamelCase  :Union[str, 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     UpperCamelCase__     (			self				,				__lowercase				,				__lowercase				,				__lowercase				,				__lowercase				,				__lowercase				,				__lowercase				,				__lowercase				,				__lowercase				,				__lowercase				,				) ->			Union[str, Any]:
        __UpperCamelCase  :List[Any]    						=	self.num_labels
        __UpperCamelCase  :List[Any]    						=	XLMForTokenClassification(_lowerCamelCase)
        model.to(_lowerCamelCase)
        model.eval()

        __UpperCamelCase  :Optional[int]    						=	model(_lowerCamelCase				,				attention_mask=_lowerCamelCase				,				labels=_lowerCamelCase)
        self.parent.assertEqual(result.logits.shape				,				(self.batch_size, self.seq_length, self.num_labels))







    def     UpperCamelCase__     (			self				,				__lowercase				,				__lowercase				,				__lowercase				,				__lowercase				,				__lowercase				,				__lowercase				,				__lowercase				,				__lowercase				,				__lowercase				,				) ->			List[Any]:
        __UpperCamelCase  :List[Any]    						=	self.num_choices
        __UpperCamelCase  :Dict    						=	XLMForMultipleChoice(config=_lowerCamelCase)
        model.to(_lowerCamelCase)
        model.eval()
        __UpperCamelCase  :int    						=	input_ids.unsqueeze(1).expand(-1				,				self.num_choices				,				-1).contiguous()
        __UpperCamelCase  :Optional[int]    						=	token_type_ids.unsqueeze(1).expand(-1				,				self.num_choices				,				-1).contiguous()
        __UpperCamelCase  :str    						=	input_mask.unsqueeze(1).expand(-1				,				self.num_choices				,				-1).contiguous()
        __UpperCamelCase  :Optional[int]    						=	model(
            _lowerCamelCase				,				attention_mask=_lowerCamelCase				,				token_type_ids=_lowerCamelCase				,				labels=_lowerCamelCase				,				)
        self.parent.assertEqual(result.logits.shape				,				(self.batch_size, self.num_choices))







    def     UpperCamelCase__     (			self) ->			int:
        __UpperCamelCase  :str    						=	self.prepare_config_and_inputs()
        (
            (
            __UpperCamelCase	
        )						, (
            __UpperCamelCase	
        )						, (
            __UpperCamelCase	
        )						, (
            __UpperCamelCase	
        )						, (
            __UpperCamelCase	
        )						, (
            __UpperCamelCase	
        )						, (
            __UpperCamelCase	
        )						, (
            __UpperCamelCase	
        )						, (
            __UpperCamelCase	
        )						, 	
        )  :Optional[Any]    						=	config_and_inputs
        __UpperCamelCase  :List[str]    						=	{'''input_ids''': input_ids, '''token_type_ids''': token_type_ids, '''lengths''': input_lengths}
        return config, inputs_dict


@require_torch
class 			lowerCamelCase_  (     __UpperCAmelCase		,			__UpperCAmelCase		,			__UpperCAmelCase		,			unittest.TestCase				):






    '''simple docstring'''






    a__			:		List[str]      			=   (
        (
            XLMModel,
            XLMWithLMHeadModel,
            XLMForQuestionAnswering,
            XLMForSequenceClassification,
            XLMForQuestionAnsweringSimple,
            XLMForTokenClassification,
            XLMForMultipleChoice,
        )
        if is_torch_available()
        else ()
    )
    a__			:		List[Any]      			=   (
        (XLMWithLMHeadModel,) if is_torch_available() else ()
    )  # TODO (PVP): Check other models whether language generation is also applicable
    a__			:		str      			=   (
        {
            "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				,				__lowercase				,				__lowercase				,				__lowercase				,				__lowercase				,				__lowercase) ->			Optional[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     UpperCamelCase__     (			self				,				__lowercase				,				__lowercase				,				__lowercase=False) ->			int:
        __UpperCamelCase  :int    						=	super()._prepare_for_class(_lowerCamelCase				,				_lowerCamelCase				,				return_labels=_lowerCamelCase)

        if return_labels:
            if model_class.__name__ == "XLMForQuestionAnswering":
                __UpperCamelCase  :str    						=	torch.zeros(
                    self.model_tester.batch_size				,				dtype=torch.long				,				device=_lowerCamelCase)
                __UpperCamelCase  :Tuple    						=	torch.zeros(
                    self.model_tester.batch_size				,				dtype=torch.long				,				device=_lowerCamelCase)

        return inputs_dict







    def     UpperCamelCase__     (			self) ->			Tuple:
        __UpperCamelCase  :Any    						=	XLMModelTester(self)
        __UpperCamelCase  :List[str]    						=	ConfigTester(self				,				config_class=_lowerCamelCase				,				emb_dim=37)







    def     UpperCamelCase__     (			self) ->			Any:
        self.config_tester.run_common_tests()







    def     UpperCamelCase__     (			self) ->			List[str]:
        __UpperCamelCase  :Tuple    						=	self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_xlm_model(*_lowerCamelCase)







    def     UpperCamelCase__     (			self) ->			Optional[int]:
        __UpperCamelCase  :str    						=	self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_xlm_lm_head(*_lowerCamelCase)







    def     UpperCamelCase__     (			self) ->			List[str]:
        __UpperCamelCase  :Optional[Any]    						=	self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_xlm_simple_qa(*_lowerCamelCase)







    def     UpperCamelCase__     (			self) ->			str:
        __UpperCamelCase  :int    						=	self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_xlm_qa(*_lowerCamelCase)







    def     UpperCamelCase__     (			self) ->			Dict:
        __UpperCamelCase  :str    						=	self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_xlm_sequence_classif(*_lowerCamelCase)







    def     UpperCamelCase__     (			self) ->			int:
        __UpperCamelCase  :Optional[int]    						=	self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_xlm_token_classif(*_lowerCamelCase)







    def     UpperCamelCase__     (			self) ->			Optional[Any]:
        __UpperCamelCase  :Tuple    						=	self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_xlm_for_multiple_choice(*_lowerCamelCase)







    def     UpperCamelCase__     (			self				,				__lowercase				,				__lowercase				,				__lowercase				,				__lowercase				,				__lowercase				,				__lowercase=False				,				__lowercase=1) ->			List[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
            __UpperCamelCase  :Optional[int]    						=	min_length + idx + 1
            __UpperCamelCase  :Optional[int]    						=	min_length + idx + 1

            __UpperCamelCase  :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(_lowerCamelCase))







    def     UpperCamelCase__     (			self				,				__lowercase				,				__lowercase				,				__lowercase				,				__lowercase				,				__lowercase				,				__lowercase=False				,				__lowercase=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
            __UpperCamelCase  :Tuple    						=	min_length + idx + 1
            __UpperCamelCase  :Dict    						=	(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     UpperCamelCase__     (			self) ->			Optional[int]:
        for model_name in XLM_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
            __UpperCamelCase  :Union[str, Any]    						=	XLMModel.from_pretrained(_lowerCamelCase)
            self.assertIsNotNone(_lowerCamelCase)


@require_torch
class 			lowerCamelCase_  (     unittest.TestCase				):






    '''simple docstring'''






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






def lowerCamelCase						(     SCREAMING_SNAKE_CASE						,    SCREAMING_SNAKE_CASE			):
    '''simple docstring'''







    return 1 if input_a == input_a else 0



def lowerCamelCase						(     ):
    '''simple docstring'''







    assert xnor_gate(0						,    0			) == 1
    assert xnor_gate(0						,    1			) == 0
    assert xnor_gate(1						,    0			) == 0
    assert xnor_gate(1						,    1			) == 1


if __name__ == "__main__":
    print(xnor_gate(0, 0))
    print(xnor_gate(0, 1))
    print(xnor_gate(1, 0))
    print(xnor_gate(1, 1))





