



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




  def __init__( self     :						int						,					__lowerCAmelCase     :						Optional[Any]						,					__lowerCAmelCase     :						str=1_3						,					__lowerCAmelCase     :						Any=7						,					__lowerCAmelCase     :						Optional[Any]=True						,					__lowerCAmelCase     :						Optional[int]=True						,					__lowerCAmelCase     :						Optional[Any]=True						,					__lowerCAmelCase     :						Any=True						,					__lowerCAmelCase     :						int=True						,					__lowerCAmelCase     :						Optional[int]=False						,					__lowerCAmelCase     :						Any=False						,					__lowerCAmelCase     :						Optional[int]=False						,					__lowerCAmelCase     :						List[str]=2						,					__lowerCAmelCase     :						List[Any]=9_9						,					__lowerCAmelCase     :						str=0						,					__lowerCAmelCase     :						Any=3_2						,					__lowerCAmelCase     :						List[Any]=5						,					__lowerCAmelCase     :						Dict=4						,					__lowerCAmelCase     :						Optional[int]=0.1						,					__lowerCAmelCase     :						Any=0.1						,					__lowerCAmelCase     :						List[str]=5_1_2						,					__lowerCAmelCase     :						List[Any]=2						,					__lowerCAmelCase     :						Optional[int]=0.02						,					__lowerCAmelCase     :						Union[str, Any]=2						,					__lowerCAmelCase     :						Any=4						,					__lowerCAmelCase     :						List[Any]="last"						,					__lowerCAmelCase     :						List[str]=True						,					__lowerCAmelCase     :						List[Any]=None						,					__lowerCAmelCase     :						str=0						,					):






         """simple docstring"""






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




  def 						SCREAMING_SNAKE_CASE				( self     :						Union[str, Any]		):






         """simple docstring"""






         _lowerCamelCase						:				Any    			= ids_tensor([self.batch_size, self.seq_length]						,					self.vocab_size		)
         _lowerCamelCase						:				str    			= random_attention_mask([self.batch_size, self.seq_length]		)

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

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

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

         _lowerCamelCase						:				Dict    			= self.get_config()

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




  def 						SCREAMING_SNAKE_CASE				( self     :						Dict		):






         """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 						SCREAMING_SNAKE_CASE				( self     :						int						,					__lowerCAmelCase     :						Union[str, Any]						,					__lowerCAmelCase     :						int						,					__lowerCAmelCase     :						Union[str, Any]						,					__lowerCAmelCase     :						List[str]						,					__lowerCAmelCase     :						Optional[int]						,					__lowerCAmelCase     :						Dict						,					__lowerCAmelCase     :						Any						,					__lowerCAmelCase     :						int						,					__lowerCAmelCase     :						str						,					):






         """simple docstring"""






         _lowerCamelCase						:				List[Any]    			= XLMModel(config=__lowerCAmelCase		)
         model.to(__lowerCAmelCase		)
         model.eval()
         _lowerCamelCase						:				Optional[Any]    			= model(__lowerCAmelCase						,					lengths=__lowerCAmelCase						,					langs=__lowerCAmelCase		)
         _lowerCamelCase						:				List[Any]    			= model(__lowerCAmelCase						,					langs=__lowerCAmelCase		)
         _lowerCamelCase						:				List[Any]    			= model(__lowerCAmelCase		)
         self.parent.assertEqual(result.last_hidden_state.shape						,					(self.batch_size, self.seq_length, self.hidden_size)		)




  def 						SCREAMING_SNAKE_CASE				( self     :						Any						,					__lowerCAmelCase     :						Optional[Any]						,					__lowerCAmelCase     :						List[Any]						,					__lowerCAmelCase     :						List[Any]						,					__lowerCAmelCase     :						int						,					__lowerCAmelCase     :						Optional[int]						,					__lowerCAmelCase     :						List[str]						,					__lowerCAmelCase     :						Optional[int]						,					__lowerCAmelCase     :						List[str]						,					__lowerCAmelCase     :						str						,					):






         """simple docstring"""






         _lowerCamelCase						:				str    			= XLMWithLMHeadModel(__lowerCAmelCase		)
         model.to(__lowerCAmelCase		)
         model.eval()

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




  def 						SCREAMING_SNAKE_CASE				( self     :						Optional[int]						,					__lowerCAmelCase     :						Union[str, Any]						,					__lowerCAmelCase     :						Tuple						,					__lowerCAmelCase     :						List[Any]						,					__lowerCAmelCase     :						Any						,					__lowerCAmelCase     :						Optional[Any]						,					__lowerCAmelCase     :						Dict						,					__lowerCAmelCase     :						Optional[Any]						,					__lowerCAmelCase     :						Optional[int]						,					__lowerCAmelCase     :						Tuple						,					):






         """simple docstring"""






         _lowerCamelCase						:				Any    			= XLMForQuestionAnsweringSimple(__lowerCAmelCase		)
         model.to(__lowerCAmelCase		)
         model.eval()

         _lowerCamelCase						:				List[str]    			= model(__lowerCAmelCase		)

         _lowerCamelCase						:				Optional[Any]    			= model(__lowerCAmelCase						,					start_positions=__lowerCAmelCase						,					end_positions=__lowerCAmelCase		)
         _lowerCamelCase						:				List[str]    			= 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 						SCREAMING_SNAKE_CASE				( self     :						Union[str, Any]						,					__lowerCAmelCase     :						int						,					__lowerCAmelCase     :						Union[str, Any]						,					__lowerCAmelCase     :						Tuple						,					__lowerCAmelCase     :						Any						,					__lowerCAmelCase     :						Union[str, Any]						,					__lowerCAmelCase     :						Tuple						,					__lowerCAmelCase     :						Tuple						,					__lowerCAmelCase     :						List[Any]						,					__lowerCAmelCase     :						Any						,					):






         """simple docstring"""






         _lowerCamelCase						:				Optional[Any]    			= XLMForQuestionAnswering(__lowerCAmelCase		)
         model.to(__lowerCAmelCase		)
         model.eval()

         _lowerCamelCase						:				Optional[Any]    			= model(__lowerCAmelCase		)

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

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

         ((_lowerCamelCase)			,    )						:				List[str]    			= result_with_labels.to_tuple()

         _lowerCamelCase						:				str    			= model(__lowerCAmelCase						,					start_positions=__lowerCAmelCase						,					end_positions=__lowerCAmelCase		)

         ((_lowerCamelCase)			,    )						:				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 						SCREAMING_SNAKE_CASE				( self     :						Union[str, Any]						,					__lowerCAmelCase     :						Dict						,					__lowerCAmelCase     :						Union[str, Any]						,					__lowerCAmelCase     :						Optional[int]						,					__lowerCAmelCase     :						Dict						,					__lowerCAmelCase     :						int						,					__lowerCAmelCase     :						str						,					__lowerCAmelCase     :						List[str]						,					__lowerCAmelCase     :						str						,					__lowerCAmelCase     :						Union[str, Any]						,					):






         """simple docstring"""






         _lowerCamelCase						:				List[str]    			= XLMForSequenceClassification(__lowerCAmelCase		)
         model.to(__lowerCAmelCase		)
         model.eval()

         _lowerCamelCase						:				Dict    			= model(__lowerCAmelCase		)
         _lowerCamelCase						:				str    			= 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 						SCREAMING_SNAKE_CASE				( self     :						str						,					__lowerCAmelCase     :						List[Any]						,					__lowerCAmelCase     :						Dict						,					__lowerCAmelCase     :						Optional[Any]						,					__lowerCAmelCase     :						Dict						,					__lowerCAmelCase     :						Optional[Any]						,					__lowerCAmelCase     :						Tuple						,					__lowerCAmelCase     :						Optional[Any]						,					__lowerCAmelCase     :						List[str]						,					__lowerCAmelCase     :						Dict						,					):






         """simple docstring"""






         _lowerCamelCase						:				str    			= self.num_labels
         _lowerCamelCase						:				int    			= XLMForTokenClassification(__lowerCAmelCase		)
         model.to(__lowerCAmelCase		)
         model.eval()

         _lowerCamelCase						:				str    			= model(__lowerCAmelCase						,					attention_mask=__lowerCAmelCase						,					labels=__lowerCAmelCase		)
         self.parent.assertEqual(result.logits.shape						,					(self.batch_size, self.seq_length, self.num_labels)		)




  def 						SCREAMING_SNAKE_CASE				( self     :						Dict						,					__lowerCAmelCase     :						Optional[Any]						,					__lowerCAmelCase     :						Dict						,					__lowerCAmelCase     :						Optional[int]						,					__lowerCAmelCase     :						Union[str, Any]						,					__lowerCAmelCase     :						List[str]						,					__lowerCAmelCase     :						Optional[int]						,					__lowerCAmelCase     :						Dict						,					__lowerCAmelCase     :						Union[str, Any]						,					__lowerCAmelCase     :						Any						,					):






         """simple docstring"""






         _lowerCamelCase						:				Union[str, Any]    			= self.num_choices
         _lowerCamelCase						:				List[str]    			= XLMForMultipleChoice(config=__lowerCAmelCase		)
         model.to(__lowerCAmelCase		)
         model.eval()
         _lowerCamelCase						:				Union[str, Any]    			= input_ids.unsqueeze(1		).expand(-1						,					self.num_choices						,					-1		).contiguous()
         _lowerCamelCase						:				List[Any]    			= token_type_ids.unsqueeze(1		).expand(-1						,					self.num_choices						,					-1		).contiguous()
         _lowerCamelCase						:				Dict    			= input_mask.unsqueeze(1		).expand(-1						,					self.num_choices						,					-1		).contiguous()
         _lowerCamelCase						:				Tuple    			= model(
             __lowerCAmelCase						,					attention_mask=__lowerCAmelCase						,					token_type_ids=__lowerCAmelCase						,					labels=__lowerCAmelCase						,					)
         self.parent.assertEqual(result.logits.shape						,					(self.batch_size, self.num_choices)		)




  def 						SCREAMING_SNAKE_CASE				( self     :						Optional[int]		):






         """simple docstring"""






         _lowerCamelCase						:				Union[str, Any]    			= self.prepare_config_and_inputs()
         (
             (
             _lowerCamelCase
         )			,    (
             _lowerCamelCase
         )			,    (
             _lowerCamelCase
         )			,    (
             _lowerCamelCase
         )			,    (
             _lowerCamelCase
         )			,    (
             _lowerCamelCase
         )			,    (
             _lowerCamelCase
         )			,    (
             _lowerCamelCase
         )			,    (
             _lowerCamelCase
         )			,    
         )						:				Optional[Any]    			= config_and_inputs
         _lowerCamelCase						:				Any    			= {'''input_ids''': input_ids, '''token_type_ids''': token_type_ids, '''lengths''': input_lengths}
         return config, inputs_dict






@require_torch
class        __snake_case       (	_lowercase  ,     _lowercase  ,     _lowercase  ,     unittest.TestCase):
  snake_case__				:   Dict  			=						(
      (
          XLMModel,
          XLMWithLMHeadModel,
          XLMForQuestionAnswering,
          XLMForSequenceClassification,
          XLMForQuestionAnsweringSimple,
          XLMForTokenClassification,
          XLMForMultipleChoice,
      )
      if is_torch_available()
      else ()
  )
  snake_case__				:   Tuple  			=						(
      (XLMWithLMHeadModel,) if is_torch_available() else ()
  )  # TODO (PVP): Check other models whether language generation is also applicable
  snake_case__				:   Tuple  			=						(
      {
          "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 						SCREAMING_SNAKE_CASE				( self     :						Optional[int]						,					__lowerCAmelCase     :						int						,					__lowerCAmelCase     :						int						,					__lowerCAmelCase     :						Tuple						,					__lowerCAmelCase     :						int						,					__lowerCAmelCase     :						int		):






         """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 						SCREAMING_SNAKE_CASE				( self     :						Any						,					__lowerCAmelCase     :						Optional[int]						,					__lowerCAmelCase     :						int						,					__lowerCAmelCase     :						str=False		):






         """simple docstring"""






         _lowerCamelCase						:				int    			= super()._prepare_for_class(__lowerCAmelCase						,					__lowerCAmelCase						,					return_labels=__lowerCAmelCase		)

         if return_labels:
                if model_class.__name__ == "XLMForQuestionAnswering":
                       _lowerCamelCase						:				Dict    			= torch.zeros(
                           self.model_tester.batch_size						,					dtype=torch.long						,					device=__lowerCAmelCase		)
                       _lowerCamelCase						:				Any    			= torch.zeros(
                           self.model_tester.batch_size						,					dtype=torch.long						,					device=__lowerCAmelCase		)

         return inputs_dict




  def 						SCREAMING_SNAKE_CASE				( self     :						Union[str, Any]		):






         """simple docstring"""






         _lowerCamelCase						:				List[Any]    			= XLMModelTester(self		)
         _lowerCamelCase						:				List[Any]    			= ConfigTester(self						,					config_class=__lowerCAmelCase						,					emb_dim=3_7		)




  def 						SCREAMING_SNAKE_CASE				( self     :						Any		):






         """simple docstring"""






         self.config_tester.run_common_tests()




  def 						SCREAMING_SNAKE_CASE				( self     :						Optional[Any]		):






         """simple docstring"""






         _lowerCamelCase						:				str    			= self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_xlm_model(*__lowerCAmelCase		)




  def 						SCREAMING_SNAKE_CASE				( self     :						Optional[int]		):






         """simple docstring"""






         _lowerCamelCase						:				Optional[Any]    			= self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_xlm_lm_head(*__lowerCAmelCase		)




  def 						SCREAMING_SNAKE_CASE				( self     :						Optional[Any]		):






         """simple docstring"""






         _lowerCamelCase						:				Optional[Any]    			= self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_xlm_simple_qa(*__lowerCAmelCase		)




  def 						SCREAMING_SNAKE_CASE				( self     :						Any		):






         """simple docstring"""






         _lowerCamelCase						:				Union[str, Any]    			= self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_xlm_qa(*__lowerCAmelCase		)




  def 						SCREAMING_SNAKE_CASE				( self     :						List[str]		):






         """simple docstring"""






         _lowerCamelCase						:				Tuple    			= self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_xlm_sequence_classif(*__lowerCAmelCase		)




  def 						SCREAMING_SNAKE_CASE				( self     :						Any		):






         """simple docstring"""






         _lowerCamelCase						:				Union[str, Any]    			= self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_xlm_token_classif(*__lowerCAmelCase		)




  def 						SCREAMING_SNAKE_CASE				( self     :						Dict		):






         """simple docstring"""






         _lowerCamelCase						:				int    			= self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_xlm_for_multiple_choice(*__lowerCAmelCase		)




  def 						SCREAMING_SNAKE_CASE				( self     :						Dict						,					__lowerCAmelCase     :						Optional[Any]						,					__lowerCAmelCase     :						Optional[int]						,					__lowerCAmelCase     :						Optional[int]						,					__lowerCAmelCase     :						str						,					__lowerCAmelCase     :						str						,					__lowerCAmelCase     :						Optional[Any]=False						,					__lowerCAmelCase     :						Tuple=1		):






         """simple docstring"""






         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						:				Optional[Any]    			= min_length + idx + 1
                _lowerCamelCase						:				Optional[Any]    			= min_length + idx + 1

                _lowerCamelCase						:				Dict    			= (
                    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 						SCREAMING_SNAKE_CASE				( self     :						str						,					__lowerCAmelCase     :						int						,					__lowerCAmelCase     :						Optional[int]						,					__lowerCAmelCase     :						Tuple						,					__lowerCAmelCase     :						Any						,					__lowerCAmelCase     :						str						,					__lowerCAmelCase     :						str=False						,					__lowerCAmelCase     :						Tuple=1		):






         """simple docstring"""






         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						:				str    			= min_length + idx + 1
                _lowerCamelCase						:				int    			= (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 						SCREAMING_SNAKE_CASE				( self     :						int		):






         """simple docstring"""






         for model_name in XLM_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
                _lowerCamelCase						:				Dict    			= XLMModel.from_pretrained(__lowerCAmelCase		)
                self.assertIsNotNone(__lowerCAmelCase		)






@require_torch
class        __snake_case       (	unittest.TestCase):




  @slow
  def 						SCREAMING_SNAKE_CASE				( self     :						Dict		):






         """simple docstring"""






         _lowerCamelCase						:				Any    			= XLMWithLMHeadModel.from_pretrained('''xlm-mlm-en-2048'''		)
         model.to(__lowerCAmelCase		)
         _lowerCamelCase						:				Tuple    			= torch.tensor([[1_4, 4_4_7]]						,					dtype=torch.long						,					device=__lowerCAmelCase		)  # the president
         _lowerCamelCase						:				List[Any]    			= [
             1_4,
             4_4_7,
             1_4,
             4_4_7,
             1_4,
             4_4_7,
             1_4,
             4_4_7,
             1_4,
             4_4_7,
             1_4,
             4_4_7,
             1_4,
             4_4_7,
             1_4,
             4_4_7,
             1_4,
             4_4_7,
             1_4,
             4_4_7,
         ]  # 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						:				List[Any]    			= model.generate(__lowerCAmelCase						,					do_sample=__lowerCAmelCase		)
         self.assertListEqual(output_ids[0].cpu().numpy().tolist()						,					__lowerCAmelCase		)









from __future__ import annotations

from decimal import Decimal
from math import *  # noqa: F403

from sympy import diff





def      lowerCAmelCase_ (       __a	,    __a	,    __a = 10**-10		)     ->		float:







  """simple docstring"""





  lowerCamelCase__:      str			  =a
  while True:
    lowerCamelCase__:      Optional[Any]			  =Decimal(__a		) - (
        Decimal(eval(__a		)		) / Decimal(eval(str(diff(__a		)		)		)		)  # noqa: S307
    )
    # This number dictates the accuracy of the answer
    if abs(eval(__a		)		) < precision:  # noqa: S307
      return float(__a		)


# Let's Execute
if __name__ == "__main__":
     # Find root of trigonometric function
     # Find value of pi
     print(f'The root of sin(x) = 0 is {newton_raphson("sin(x)", 2)}')
     # Find root of polynomial
     print(f'The root of x**2 - 5*x + 2 = 0 is {newton_raphson("x**2 - 5*x + 2", 0.4)}')
     # Find Square Root of 5
     print(f'The root of log(x) - 1 = 0 is {newton_raphson("log(x) - 1", 2)}')
     # Exponential Roots
     print(f'The root of exp(x) - 1 = 0 is {newton_raphson("exp(x) - 1", 0)}')

