





"""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 						lowercase   :





      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 ,		)						->	Union[str, Any]:
            lowerCAmelCase    		=			parent
            lowerCAmelCase    		=			batch_size
            lowerCAmelCase    		=			seq_length
            lowerCAmelCase    		=			is_training
            lowerCAmelCase    		=			use_input_lengths
            lowerCAmelCase    		=			use_token_type_ids
            lowerCAmelCase    		=			use_labels
            lowerCAmelCase    		=			gelu_activation
            lowerCAmelCase    		=			sinusoidal_embeddings
            lowerCAmelCase    		=			causal
            lowerCAmelCase    		=			asm
            lowerCAmelCase    		=			n_langs
            lowerCAmelCase    		=			vocab_size
            lowerCAmelCase    		=			n_special
            lowerCAmelCase    		=			hidden_size
            lowerCAmelCase    		=			num_hidden_layers
            lowerCAmelCase    		=			num_attention_heads
            lowerCAmelCase    		=			hidden_dropout_prob
            lowerCAmelCase    		=			attention_probs_dropout_prob
            lowerCAmelCase    		=			max_position_embeddings
            lowerCAmelCase    		=			type_sequence_label_size
            lowerCAmelCase    		=			initializer_range
            lowerCAmelCase    		=			num_labels
            lowerCAmelCase    		=			num_choices
            lowerCAmelCase    		=			summary_type
            lowerCAmelCase    		=			use_proj
            lowerCAmelCase    		=			scope
            lowerCAmelCase    		=			bos_token_id





      def        _snake_case     (			self					)						->	List[Any]:
            lowerCAmelCase    		=			ids_tensor([self.batch_size, self.seq_length] ,		self.vocab_size					)
            lowerCAmelCase    		=			random_attention_mask([self.batch_size, self.seq_length]					)

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

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

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

            lowerCAmelCase    		=			self.get_config()

            return (
                config,
                input_ids,
                token_type_ids,
                input_lengths,
                sequence_labels,
                token_labels,
                is_impossible_labels,
                choice_labels,
                input_mask,
            )





      def        _snake_case     (			self					)						->	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        _snake_case     (			self ,		lowercase ,		lowercase ,		lowercase ,		lowercase ,		lowercase ,		lowercase ,		lowercase ,		lowercase ,		lowercase ,		)						->	List[str]:
            lowerCAmelCase    		=			XLMModel(config=__lowerCamelCase					)
            model.to(__lowerCamelCase					)
            model.eval()
            lowerCAmelCase    		=			model(__lowerCamelCase ,		lengths=__lowerCamelCase ,		langs=__lowerCamelCase					)
            lowerCAmelCase    		=			model(__lowerCamelCase ,		langs=__lowerCamelCase					)
            lowerCAmelCase    		=			model(__lowerCamelCase					)
            self.parent.assertEqual(result.last_hidden_state.shape ,		(self.batch_size, self.seq_length, self.hidden_size)					)





      def        _snake_case     (			self ,		lowercase ,		lowercase ,		lowercase ,		lowercase ,		lowercase ,		lowercase ,		lowercase ,		lowercase ,		lowercase ,		)						->	Optional[Any]:
            lowerCAmelCase    		=			XLMWithLMHeadModel(__lowerCamelCase					)
            model.to(__lowerCamelCase					)
            model.eval()

            lowerCAmelCase    		=			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        _snake_case     (			self ,		lowercase ,		lowercase ,		lowercase ,		lowercase ,		lowercase ,		lowercase ,		lowercase ,		lowercase ,		lowercase ,		)						->	Any:
            lowerCAmelCase    		=			XLMForQuestionAnsweringSimple(__lowerCamelCase					)
            model.to(__lowerCamelCase					)
            model.eval()

            lowerCAmelCase    		=			model(__lowerCamelCase					)

            lowerCAmelCase    		=			model(__lowerCamelCase ,		start_positions=__lowerCamelCase ,		end_positions=__lowerCamelCase					)
            lowerCAmelCase    		=			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        _snake_case     (			self ,		lowercase ,		lowercase ,		lowercase ,		lowercase ,		lowercase ,		lowercase ,		lowercase ,		lowercase ,		lowercase ,		)						->	List[Any]:
            lowerCAmelCase    		=			XLMForQuestionAnswering(__lowerCamelCase					)
            model.to(__lowerCamelCase					)
            model.eval()

            lowerCAmelCase    		=			model(__lowerCamelCase					)

            lowerCAmelCase    		=			model(
                __lowerCamelCase ,		start_positions=__lowerCamelCase ,		end_positions=__lowerCamelCase ,		cls_index=__lowerCamelCase ,		is_impossible=__lowerCamelCase ,		p_mask=__lowerCamelCase ,		)

            lowerCAmelCase    		=			model(
                __lowerCamelCase ,		start_positions=__lowerCamelCase ,		end_positions=__lowerCamelCase ,		cls_index=__lowerCamelCase ,		is_impossible=__lowerCamelCase ,		)

            ((lowerCAmelCase      )		,						      )    		=			result_with_labels.to_tuple()

            lowerCAmelCase    		=			model(__lowerCamelCase ,		start_positions=__lowerCamelCase ,		end_positions=__lowerCamelCase					)

            ((lowerCAmelCase      )		,						      )    		=			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        _snake_case     (			self ,		lowercase ,		lowercase ,		lowercase ,		lowercase ,		lowercase ,		lowercase ,		lowercase ,		lowercase ,		lowercase ,		)						->	Optional[int]:
            lowerCAmelCase    		=			XLMForSequenceClassification(__lowerCamelCase					)
            model.to(__lowerCamelCase					)
            model.eval()

            lowerCAmelCase    		=			model(__lowerCamelCase					)
            lowerCAmelCase    		=			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        _snake_case     (			self ,		lowercase ,		lowercase ,		lowercase ,		lowercase ,		lowercase ,		lowercase ,		lowercase ,		lowercase ,		lowercase ,		)						->	str:
            lowerCAmelCase    		=			self.num_labels
            lowerCAmelCase    		=			XLMForTokenClassification(__lowerCamelCase					)
            model.to(__lowerCamelCase					)
            model.eval()

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





      def        _snake_case     (			self ,		lowercase ,		lowercase ,		lowercase ,		lowercase ,		lowercase ,		lowercase ,		lowercase ,		lowercase ,		lowercase ,		)						->	List[Any]:
            lowerCAmelCase    		=			self.num_choices
            lowerCAmelCase    		=			XLMForMultipleChoice(config=__lowerCamelCase					)
            model.to(__lowerCamelCase					)
            model.eval()
            lowerCAmelCase    		=			input_ids.unsqueeze(1					).expand(-1 ,		self.num_choices ,		-1					).contiguous()
            lowerCAmelCase    		=			token_type_ids.unsqueeze(1					).expand(-1 ,		self.num_choices ,		-1					).contiguous()
            lowerCAmelCase    		=			input_mask.unsqueeze(1					).expand(-1 ,		self.num_choices ,		-1					).contiguous()
            lowerCAmelCase    		=			model(
                __lowerCamelCase ,		attention_mask=__lowerCamelCase ,		token_type_ids=__lowerCamelCase ,		labels=__lowerCamelCase ,		)
            self.parent.assertEqual(result.logits.shape ,		(self.batch_size, self.num_choices)					)





      def        _snake_case     (			self					)						->	str:
            lowerCAmelCase    		=			self.prepare_config_and_inputs()
            (
                (
                lowerCAmelCase      
            )		,						(
                lowerCAmelCase      
            )		,						(
                lowerCAmelCase      
            )		,						(
                lowerCAmelCase      
            )		,						(
                lowerCAmelCase      
            )		,						(
                lowerCAmelCase      
            )		,						(
                lowerCAmelCase      
            )		,						(
                lowerCAmelCase      
            )		,						(
                lowerCAmelCase      
            )		,						      
            )    		=			config_and_inputs
            lowerCAmelCase    		=			{"""input_ids""": input_ids, """token_type_ids""": token_type_ids, """lengths""": input_lengths}
            return config, inputs_dict







@require_torch
class 						lowercase   (   UpperCAmelCase_	,    UpperCAmelCase_	,    UpperCAmelCase_	,    unittest.TestCase   ):
      _SCREAMING_SNAKE_CASE							      = (
          (
              XLMModel,
              XLMWithLMHeadModel,
              XLMForQuestionAnswering,
              XLMForSequenceClassification,
              XLMForQuestionAnsweringSimple,
              XLMForTokenClassification,
              XLMForMultipleChoice,
          )
          if is_torch_available()
          else ()
      )
      _SCREAMING_SNAKE_CASE							      = (
          (XLMWithLMHeadModel,) if is_torch_available() else ()
      )  # TODO (PVP): Check other models whether language generation is also applicable
      _SCREAMING_SNAKE_CASE							      = (
          {
              '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        _snake_case     (			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        _snake_case     (			self ,		lowercase ,		lowercase ,		lowercase=False					)						->	Optional[int]:
            lowerCAmelCase    		=			super()._prepare_for_class(__lowerCamelCase ,		__lowerCamelCase ,		return_labels=__lowerCamelCase					)

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

            return inputs_dict





      def        _snake_case     (			self					)						->	Any:
            lowerCAmelCase    		=			XLMModelTester(self					)
            lowerCAmelCase    		=			ConfigTester(self ,		config_class=__lowerCamelCase ,		emb_dim=37					)





      def        _snake_case     (			self					)						->	Optional[int]:
            self.config_tester.run_common_tests()





      def        _snake_case     (			self					)						->	List[str]:
            lowerCAmelCase    		=			self.model_tester.prepare_config_and_inputs()
            self.model_tester.create_and_check_xlm_model(*__lowerCamelCase					)





      def        _snake_case     (			self					)						->	Any:
            lowerCAmelCase    		=			self.model_tester.prepare_config_and_inputs()
            self.model_tester.create_and_check_xlm_lm_head(*__lowerCamelCase					)





      def        _snake_case     (			self					)						->	List[Any]:
            lowerCAmelCase    		=			self.model_tester.prepare_config_and_inputs()
            self.model_tester.create_and_check_xlm_simple_qa(*__lowerCamelCase					)





      def        _snake_case     (			self					)						->	str:
            lowerCAmelCase    		=			self.model_tester.prepare_config_and_inputs()
            self.model_tester.create_and_check_xlm_qa(*__lowerCamelCase					)





      def        _snake_case     (			self					)						->	List[Any]:
            lowerCAmelCase    		=			self.model_tester.prepare_config_and_inputs()
            self.model_tester.create_and_check_xlm_sequence_classif(*__lowerCamelCase					)





      def        _snake_case     (			self					)						->	Tuple:
            lowerCAmelCase    		=			self.model_tester.prepare_config_and_inputs()
            self.model_tester.create_and_check_xlm_token_classif(*__lowerCamelCase					)





      def        _snake_case     (			self					)						->	Optional[int]:
            lowerCAmelCase    		=			self.model_tester.prepare_config_and_inputs()
            self.model_tester.create_and_check_xlm_for_multiple_choice(*__lowerCamelCase					)





      def        _snake_case     (			self ,		lowercase ,		lowercase ,		lowercase ,		lowercase ,		lowercase ,		lowercase=False ,		lowercase=1					)						->	int:
            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
                  lowerCAmelCase    		=			min_length + idx + 1
                  lowerCAmelCase    		=			min_length + idx + 1

                  lowerCAmelCase    		=			(
                      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        _snake_case     (			self ,		lowercase ,		lowercase ,		lowercase ,		lowercase ,		lowercase ,		lowercase=False ,		lowercase=1					)						->	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
                  lowerCAmelCase    		=			min_length + idx + 1
                  lowerCAmelCase    		=			(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        _snake_case     (			self					)						->	List[str]:




            for model_name in XLM_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
                  lowerCAmelCase    		=			XLMModel.from_pretrained(__lowerCamelCase					)
                  self.assertIsNotNone(__lowerCamelCase					)







@require_torch
class 						lowercase   (   unittest.TestCase   ):





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













import os
from collections import namedtuple

import pytest

from datasets import ClassLabel, Features, Sequence, Value
from datasets.commands.test import TestCommand
from datasets.info import DatasetInfo, DatasetInfosDict


SCREAMING_SNAKE_CASE_   :      Optional[Any]			=   namedtuple(
    '''_TestCommandArgs''',
    [
        '''dataset''',
        '''name''',
        '''cache_dir''',
        '''data_dir''',
        '''all_configs''',
        '''save_infos''',
        '''ignore_verifications''',
        '''force_redownload''',
        '''clear_cache''',
    ],
    defaults=[None, None, None, False, False, False, False, False],
)



def  SCREAMING_SNAKE_CASE				(      snake_case						,   snake_case							)		->    Optional[int]:
		return (abs(source - target							) / target) < 0.01



@pytest.mark.integration
def  SCREAMING_SNAKE_CASE				(      snake_case							)		->    Dict:
		__lowercase											=     _TestCommandArgs(dataset=snake_case						,   all_configs=snake_case						,   save_infos=snake_case							)
		__lowercase											=     TestCommand(*snake_case							)
		test_command.run()
		__lowercase											=     os.path.join(snake_case						,   'README.md'							)
		assert os.path.exists(snake_case							)
		__lowercase											=     DatasetInfosDict.from_directory(snake_case							)
		__lowercase											=     DatasetInfosDict(
		    {
		        'default': DatasetInfo(
		            features=Features(
		                {
		                    'tokens': Sequence(Value('string'							)							),
		                    'ner_tags': Sequence(
		                        ClassLabel(names=['O', 'B-PER', 'I-PER', 'B-ORG', 'I-ORG', 'B-LOC', 'I-LOC']							)							),
		                    'langs': Sequence(Value('string'							)							),
		                    'spans': Sequence(Value('string'							)							),
		                }							)						,   splits=[
		                {
		                    'name': 'train',
		                    'num_bytes': 2_351_563,
		                    'num_examples': 10_000,
		                },
		                {
		                    'name': 'validation',
		                    'num_bytes': 238_418,
		                    'num_examples': 1_000,
		                },
		            ]						,   download_size=3_940_680						,   dataset_size=2_589_981						,   )
		    }							)
		assert dataset_infos.keys() == expected_dataset_infos.keys()
		for key in DatasetInfo._INCLUDED_INFO_IN_YAML:
				__lowercase						,      __lowercase											=     getattr(dataset_infos['default']						,   snake_case							), getattr(expected_dataset_infos['default']						,   snake_case							)
				if key == "num_bytes":
						assert is_apercent_close(snake_case						,   snake_case							)
				elif key == "splits":
						assert list(snake_case							) == list(snake_case							)
						for split in result:
								assert result[split].name == expected[split].name
								assert result[split].num_examples == expected[split].num_examples
								assert is_apercent_close(result[split].num_bytes						,   expected[split].num_bytes							)
				else:
						result == expected




