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







 """simple docstring"""






 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_=5_12		,				lowerCAmelCase_=2		,				lowerCAmelCase_=0.02		,				lowerCAmelCase_=2		,				lowerCAmelCase_=4		,				lowerCAmelCase_="last"		,				lowerCAmelCase_=True		,				lowerCAmelCase_=None		,				lowerCAmelCase_=0		,				)  ->					Union[str, Any]:
        _A        =  parent
        _A        =  batch_size
        _A        =  seq_length
        _A        =  is_training
        _A        =  use_input_lengths
        _A        =  use_token_type_ids
        _A        =  use_labels
        _A        =  gelu_activation
        _A        =  sinusoidal_embeddings
        _A        =  causal
        _A        =  asm
        _A        =  n_langs
        _A        =  vocab_size
        _A        =  n_special
        _A        =  hidden_size
        _A        =  num_hidden_layers
        _A        =  num_attention_heads
        _A        =  hidden_dropout_prob
        _A        =  attention_probs_dropout_prob
        _A        =  max_position_embeddings
        _A        =  type_sequence_label_size
        _A        =  initializer_range
        _A        =  num_labels
        _A        =  num_choices
        _A        =  summary_type
        _A        =  use_proj
        _A        =  scope
        _A        =  bos_token_id



 def      UpperCAmelCase							(					self							)  ->					int:
        _A        =  ids_tensor([self.batch_size, self.seq_length]		,				self.vocab_size							)
        _A        =  random_attention_mask([self.batch_size, self.seq_length]							)

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

        _A        =  None
        if self.use_token_type_ids:
               _A        =  ids_tensor([self.batch_size, self.seq_length]		,				self.n_langs							)

        _A        =  None
        _A        =  None
        _A        =  None
        if self.use_labels:
               _A        =  ids_tensor([self.batch_size]		,				self.type_sequence_label_size							)
               _A        =  ids_tensor([self.batch_size, self.seq_length]		,				self.num_labels							)
               _A        =  ids_tensor([self.batch_size]		,				2							).float()
               _A        =  ids_tensor([self.batch_size]		,				self.num_choices							)

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



 def      UpperCAmelCase							(					self		,				lowerCAmelCase_		,				lowerCAmelCase_		,				lowerCAmelCase_		,				lowerCAmelCase_		,				lowerCAmelCase_		,				lowerCAmelCase_		,				lowerCAmelCase_		,				lowerCAmelCase_		,				lowerCAmelCase_		,				)  ->					Any:
        _A        =  XLMWithLMHeadModel(__snake_case							)
        model.to(__snake_case							)
        model.eval()

        _A        =  model(__snake_case		,				token_type_ids=__snake_case		,				labels=__snake_case							)
        self.parent.assertEqual(result.loss.shape		,				()							)
        self.parent.assertEqual(result.logits.shape		,				(self.batch_size, self.seq_length, self.vocab_size)							)



 def      UpperCAmelCase							(					self		,				lowerCAmelCase_		,				lowerCAmelCase_		,				lowerCAmelCase_		,				lowerCAmelCase_		,				lowerCAmelCase_		,				lowerCAmelCase_		,				lowerCAmelCase_		,				lowerCAmelCase_		,				lowerCAmelCase_		,				)  ->					int:
        _A        =  XLMForQuestionAnsweringSimple(__snake_case							)
        model.to(__snake_case							)
        model.eval()

        _A        =  model(__snake_case							)

        _A        =  model(__snake_case		,				start_positions=__snake_case		,				end_positions=__snake_case							)
        _A        =  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		,				lowerCAmelCase_		,				lowerCAmelCase_		,				lowerCAmelCase_		,				lowerCAmelCase_		,				lowerCAmelCase_		,				lowerCAmelCase_		,				lowerCAmelCase_		,				lowerCAmelCase_		,				lowerCAmelCase_		,				)  ->					List[Any]:
        _A        =  XLMForQuestionAnswering(__snake_case							)
        model.to(__snake_case							)
        model.eval()

        _A        =  model(__snake_case							)

        _A        =  model(
            __snake_case		,				start_positions=__snake_case		,				end_positions=__snake_case		,				cls_index=__snake_case		,				is_impossible=__snake_case		,				p_mask=__snake_case		,				)

        _A        =  model(
            __snake_case		,				start_positions=__snake_case		,				end_positions=__snake_case		,				cls_index=__snake_case		,				is_impossible=__snake_case		,				)

        (_A       )        =  result_with_labels.to_tuple()

        _A        =  model(__snake_case		,				start_positions=__snake_case		,				end_positions=__snake_case							)

        (_A       )        =  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		,				lowerCAmelCase_		,				lowerCAmelCase_		,				lowerCAmelCase_		,				lowerCAmelCase_		,				lowerCAmelCase_		,				lowerCAmelCase_		,				lowerCAmelCase_		,				lowerCAmelCase_		,				lowerCAmelCase_		,				)  ->					List[str]:
        _A        =  XLMForSequenceClassification(__snake_case							)
        model.to(__snake_case							)
        model.eval()

        _A        =  model(__snake_case							)
        _A        =  model(__snake_case		,				labels=__snake_case							)
        self.parent.assertEqual(result.loss.shape		,				()							)
        self.parent.assertEqual(result.logits.shape		,				(self.batch_size, self.type_sequence_label_size)							)



 def      UpperCAmelCase							(					self		,				lowerCAmelCase_		,				lowerCAmelCase_		,				lowerCAmelCase_		,				lowerCAmelCase_		,				lowerCAmelCase_		,				lowerCAmelCase_		,				lowerCAmelCase_		,				lowerCAmelCase_		,				lowerCAmelCase_		,				)  ->					Union[str, Any]:
        _A        =  self.num_labels
        _A        =  XLMForTokenClassification(__snake_case							)
        model.to(__snake_case							)
        model.eval()

        _A        =  model(__snake_case		,				attention_mask=__snake_case		,				labels=__snake_case							)
        self.parent.assertEqual(result.logits.shape		,				(self.batch_size, self.seq_length, self.num_labels)							)



 def      UpperCAmelCase							(					self		,				lowerCAmelCase_		,				lowerCAmelCase_		,				lowerCAmelCase_		,				lowerCAmelCase_		,				lowerCAmelCase_		,				lowerCAmelCase_		,				lowerCAmelCase_		,				lowerCAmelCase_		,				lowerCAmelCase_		,				)  ->					Any:
        _A        =  self.num_choices
        _A        =  XLMForMultipleChoice(config=__snake_case							)
        model.to(__snake_case							)
        model.eval()
        _A        =  input_ids.unsqueeze(1							).expand(-1		,				self.num_choices		,				-1							).contiguous()
        _A        =  token_type_ids.unsqueeze(1							).expand(-1		,				self.num_choices		,				-1							).contiguous()
        _A        =  input_mask.unsqueeze(1							).expand(-1		,				self.num_choices		,				-1							).contiguous()
        _A        =  model(
            __snake_case		,				attention_mask=__snake_case		,				token_type_ids=__snake_case		,				labels=__snake_case		,				)
        self.parent.assertEqual(result.logits.shape		,				(self.batch_size, self.num_choices)							)



 def      UpperCAmelCase							(					self							)  ->					int:
        _A        =  self.prepare_config_and_inputs()
        (
            _A       
        )        =  config_and_inputs
        _A        =  {"""input_ids""": input_ids, """token_type_ids""": token_type_ids, """lengths""": input_lengths}
        return config, inputs_dict



@require_torch
class 				a							(			_snake_case       ,			_snake_case       ,			_snake_case       ,			unittest.TestCase       ):







 """simple docstring"""






 lowerCamelCase	:str    			=		(
     (
         XLMModel,
         XLMWithLMHeadModel,
         XLMForQuestionAnswering,
         XLMForSequenceClassification,
         XLMForQuestionAnsweringSimple,
         XLMForTokenClassification,
         XLMForMultipleChoice,
     )
     if is_torch_available()
     else ()
 )
 lowerCamelCase	:Any    			=		(
     (XLMWithLMHeadModel,) if is_torch_available() else ()
 )  # TODO (PVP): Check other models whether language generation is also applicable
 lowerCamelCase	:Optional[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		,				lowerCAmelCase_		,				lowerCAmelCase_		,				lowerCAmelCase_		,				lowerCAmelCase_		,				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      UpperCAmelCase							(					self		,				lowerCAmelCase_		,				lowerCAmelCase_		,				lowerCAmelCase_=False							)  ->					Optional[Any]:
        _A        =  super()._prepare_for_class(__snake_case		,				__snake_case		,				return_labels=__snake_case							)

        if return_labels:
               if model_class.__name__ == "XLMForQuestionAnswering":
                      _A        =  torch.zeros(
                          self.model_tester.batch_size		,				dtype=torch.long		,				device=__snake_case							)
                      _A        =  torch.zeros(
                          self.model_tester.batch_size		,				dtype=torch.long		,				device=__snake_case							)

        return inputs_dict



 def      UpperCAmelCase							(					self							)  ->					int:
        _A        =  XLMModelTester(self							)
        _A        =  ConfigTester(self		,				config_class=__snake_case		,				emb_dim=37							)



 def      UpperCAmelCase							(					self							)  ->					int:
        self.config_tester.run_common_tests()



 def      UpperCAmelCase							(					self							)  ->					Optional[int]:
        _A        =  self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_xlm_model(*__snake_case							)



 def      UpperCAmelCase							(					self							)  ->					Optional[Any]:
        _A        =  self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_xlm_lm_head(*__snake_case							)



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



 def      UpperCAmelCase							(					self							)  ->					List[str]:
        _A        =  self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_xlm_qa(*__snake_case							)



 def      UpperCAmelCase							(					self							)  ->					Tuple:
        _A        =  self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_xlm_sequence_classif(*__snake_case							)



 def      UpperCAmelCase							(					self							)  ->					Union[str, Any]:
        _A        =  self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_xlm_token_classif(*__snake_case							)



 def      UpperCAmelCase							(					self							)  ->					int:
        _A        =  self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_xlm_for_multiple_choice(*__snake_case							)



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

        for idx, iter_attentions in enumerate(__snake_case							):
               # adds PAD dummy token
               _A        =  min_length + idx + 1
               _A        =  min_length + idx + 1

               _A        =  (
                   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(__snake_case							)							)



 def      UpperCAmelCase							(					self		,				lowerCAmelCase_		,				lowerCAmelCase_		,				lowerCAmelCase_		,				lowerCAmelCase_		,				lowerCAmelCase_		,				lowerCAmelCase_=False		,				lowerCAmelCase_=1							)  ->					Union[str, Any]:
        self.assertIsInstance(__snake_case		,				__snake_case							)
        self.assertListEqual(
            [isinstance(__snake_case		,				__snake_case							) for iter_hidden_states in hidden_states]		,				[True] * len(__snake_case							)		,				)
        self.assertEqual(len(__snake_case							)		,				(max_length - min_length) * num_beam_groups							)

        for idx, iter_hidden_states in enumerate(__snake_case							):
               # adds PAD dummy token
               _A        =  min_length + idx + 1
               _A        =  (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(__snake_case							)		,				)
        pass



 @slow
 def      UpperCAmelCase							(					self							)  ->					Optional[Any]:
        for model_name in XLM_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
               _A        =  XLMModel.from_pretrained(__snake_case							)
               self.assertIsNotNone(__snake_case							)



@require_torch
class 				a							(			unittest.TestCase       ):







 """simple docstring"""






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








'''simple docstring'''


from ...configuration_utils import PretrainedConfig
from ...utils import logging


UpperCAmelCase_		:   Dict							   =		logging.get_logger(__name__)

UpperCAmelCase_		:   Optional[int]							   =		{
    'google/canine-s': 'https://huggingface.co/google/canine-s/resolve/main/config.json',
    # See all CANINE models at https://huggingface.co/models?filter=canine
}





class     lowercase__  (						_snake_case					):





							'''simple docstring'''


							A_							:      Optional[int]      	=  """canine"""






							def __init__(    self    ,					__snake_case=768    ,					__snake_case=12    ,					__snake_case=12    ,					__snake_case=3072    ,					__snake_case="gelu"    ,					__snake_case=0.1    ,					__snake_case=0.1    ,					__snake_case=1_6384    ,					__snake_case=16    ,					__snake_case=0.02    ,					__snake_case=1e-12    ,					__snake_case=0    ,					__snake_case=0XE000    ,					__snake_case=0XE001    ,					__snake_case=4    ,					__snake_case=4    ,					__snake_case=8    ,					__snake_case=1_6384    ,					__snake_case=128    ,					**__snake_case    ,					):
											super().__init__(pad_token_id=__snake_case    ,					bos_token_id=__snake_case    ,					eos_token_id=__snake_case    ,					**__snake_case  )

											_SCREAMING_SNAKE_CASE :       str     		=      max_position_embeddings
											_SCREAMING_SNAKE_CASE :       Any     		=      hidden_size
											_SCREAMING_SNAKE_CASE :       Dict     		=      num_hidden_layers
											_SCREAMING_SNAKE_CASE :       Tuple     		=      num_attention_heads
											_SCREAMING_SNAKE_CASE :       List[str]     		=      intermediate_size
											_SCREAMING_SNAKE_CASE :       Tuple     		=      hidden_act
											_SCREAMING_SNAKE_CASE :       Union[str, Any]     		=      hidden_dropout_prob
											_SCREAMING_SNAKE_CASE :       str     		=      attention_probs_dropout_prob
											_SCREAMING_SNAKE_CASE :       Dict     		=      initializer_range
											_SCREAMING_SNAKE_CASE :       Optional[int]     		=      type_vocab_size
											_SCREAMING_SNAKE_CASE :       Optional[Any]     		=      layer_norm_eps

											# Character config:
											_SCREAMING_SNAKE_CASE :       Any     		=      downsampling_rate
											_SCREAMING_SNAKE_CASE :       List[str]     		=      upsampling_kernel_size
											_SCREAMING_SNAKE_CASE :       Union[str, Any]     		=      num_hash_functions
											_SCREAMING_SNAKE_CASE :       Optional[Any]     		=      num_hash_buckets
											_SCREAMING_SNAKE_CASE :       Union[str, Any]     		=      local_transformer_stride




