






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  SCREAMING_SNAKE_CASE			:
     '''simple docstring'''


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


     def 				_A       (				self       :	List[str]      ):
       SCREAMING_SNAKE_CASE       :      Union[str, Any]							=    ids_tensor([self.batch_size, self.seq_length]					, self.vocab_size      )
       SCREAMING_SNAKE_CASE       :      List[Any]							=    random_attention_mask([self.batch_size, self.seq_length]      )

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

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

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

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


     def 				_A       (				self       :	str					, UpperCAmelCase_       :	List[Any]					, UpperCAmelCase_       :	Dict					, UpperCAmelCase_       :	Optional[Any]					, UpperCAmelCase_       :	Optional[int]					, UpperCAmelCase_       :	Tuple					, UpperCAmelCase_       :	Tuple					, UpperCAmelCase_       :	Any					, UpperCAmelCase_       :	Tuple					, UpperCAmelCase_       :	Dict					, ):
       SCREAMING_SNAKE_CASE       :      Dict							=    XLMWithLMHeadModel(UpperCAmelCase_      )
       model.to(UpperCAmelCase_      )
       model.eval()

       SCREAMING_SNAKE_CASE       :      Dict							=    model(UpperCAmelCase_					, token_type_ids=UpperCAmelCase_					, labels=UpperCAmelCase_      )
       self.parent.assertEqual(result.loss.shape					, ()      )
       self.parent.assertEqual(result.logits.shape					, (self.batch_size, self.seq_length, self.vocab_size)      )


     def 				_A       (				self       :	Tuple					, UpperCAmelCase_       :	Optional[int]					, UpperCAmelCase_       :	Optional[Any]					, UpperCAmelCase_       :	str					, UpperCAmelCase_       :	List[str]					, UpperCAmelCase_       :	Union[str, Any]					, UpperCAmelCase_       :	int					, UpperCAmelCase_       :	str					, UpperCAmelCase_       :	Tuple					, UpperCAmelCase_       :	Tuple					, ):
       SCREAMING_SNAKE_CASE       :      Optional[Any]							=    XLMForQuestionAnsweringSimple(UpperCAmelCase_      )
       model.to(UpperCAmelCase_      )
       model.eval()

       SCREAMING_SNAKE_CASE       :      Dict							=    model(UpperCAmelCase_      )

       SCREAMING_SNAKE_CASE       :      List[str]							=    model(UpperCAmelCase_					, start_positions=UpperCAmelCase_					, end_positions=UpperCAmelCase_      )
       SCREAMING_SNAKE_CASE       :      Union[str, Any]							=    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 				_A       (				self       :	int					, UpperCAmelCase_       :	Union[str, Any]					, UpperCAmelCase_       :	Tuple					, UpperCAmelCase_       :	Optional[int]					, UpperCAmelCase_       :	Dict					, UpperCAmelCase_       :	Tuple					, UpperCAmelCase_       :	List[Any]					, UpperCAmelCase_       :	Optional[Any]					, UpperCAmelCase_       :	Union[str, Any]					, UpperCAmelCase_       :	List[str]					, ):
       SCREAMING_SNAKE_CASE       :      str							=    XLMForQuestionAnswering(UpperCAmelCase_      )
       model.to(UpperCAmelCase_      )
       model.eval()

       SCREAMING_SNAKE_CASE       :      Dict							=    model(UpperCAmelCase_      )

       SCREAMING_SNAKE_CASE       :      Any							=    model(
           UpperCAmelCase_					, start_positions=UpperCAmelCase_					, end_positions=UpperCAmelCase_					, cls_index=UpperCAmelCase_					, is_impossible=UpperCAmelCase_					, p_mask=UpperCAmelCase_					, )

       SCREAMING_SNAKE_CASE       :      Optional[Any]							=    model(
           UpperCAmelCase_					, start_positions=UpperCAmelCase_					, end_positions=UpperCAmelCase_					, cls_index=UpperCAmelCase_					, is_impossible=UpperCAmelCase_					, )

       (SCREAMING_SNAKE_CASE    )       :      int							=    result_with_labels.to_tuple()

       SCREAMING_SNAKE_CASE       :      Dict							=    model(UpperCAmelCase_					, start_positions=UpperCAmelCase_					, end_positions=UpperCAmelCase_      )

       (SCREAMING_SNAKE_CASE    )       :      Any							=    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 				_A       (				self       :	List[Any]					, UpperCAmelCase_       :	Dict					, UpperCAmelCase_       :	Dict					, UpperCAmelCase_       :	List[Any]					, UpperCAmelCase_       :	List[str]					, UpperCAmelCase_       :	List[Any]					, UpperCAmelCase_       :	Optional[int]					, UpperCAmelCase_       :	Any					, UpperCAmelCase_       :	List[Any]					, UpperCAmelCase_       :	str					, ):
       SCREAMING_SNAKE_CASE       :      Union[str, Any]							=    XLMForSequenceClassification(UpperCAmelCase_      )
       model.to(UpperCAmelCase_      )
       model.eval()

       SCREAMING_SNAKE_CASE       :      Optional[Any]							=    model(UpperCAmelCase_      )
       SCREAMING_SNAKE_CASE       :      int							=    model(UpperCAmelCase_					, labels=UpperCAmelCase_      )
       self.parent.assertEqual(result.loss.shape					, ()      )
       self.parent.assertEqual(result.logits.shape					, (self.batch_size, self.type_sequence_label_size)      )


     def 				_A       (				self       :	Dict					, UpperCAmelCase_       :	str					, UpperCAmelCase_       :	int					, UpperCAmelCase_       :	Any					, UpperCAmelCase_       :	Any					, UpperCAmelCase_       :	Optional[int]					, UpperCAmelCase_       :	Optional[int]					, UpperCAmelCase_       :	Optional[Any]					, UpperCAmelCase_       :	int					, UpperCAmelCase_       :	Tuple					, ):
       SCREAMING_SNAKE_CASE       :      int							=    self.num_labels
       SCREAMING_SNAKE_CASE       :      List[str]							=    XLMForTokenClassification(UpperCAmelCase_      )
       model.to(UpperCAmelCase_      )
       model.eval()

       SCREAMING_SNAKE_CASE       :      List[str]							=    model(UpperCAmelCase_					, attention_mask=UpperCAmelCase_					, labels=UpperCAmelCase_      )
       self.parent.assertEqual(result.logits.shape					, (self.batch_size, self.seq_length, self.num_labels)      )


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






     def 				_A       (				self       :	int      ):
       SCREAMING_SNAKE_CASE       :      Any							=    self.prepare_config_and_inputs()
       (
           SCREAMING_SNAKE_CASE    
       )       :      Any							=    config_and_inputs
       SCREAMING_SNAKE_CASE       :      str							=    {'input_ids': input_ids, 'token_type_ids': token_type_ids, 'lengths': input_lengths}
       return config, inputs_dict





@require_torch
class  SCREAMING_SNAKE_CASE			(			__A      ,		__A      ,		__A      ,		unittest.TestCase					):
     '''simple docstring'''


     UpperCamelCase_							:		Optional[Any]			=	(
         (
             XLMModel,
             XLMWithLMHeadModel,
             XLMForQuestionAnswering,
             XLMForSequenceClassification,
             XLMForQuestionAnsweringSimple,
             XLMForTokenClassification,
             XLMForMultipleChoice,
         )
         if is_torch_available()
         else ()
     )
     UpperCamelCase_							:		str			=	(
         (XLMWithLMHeadModel,) if is_torch_available() else ()
     )  # TODO (PVP): Check other models whether language generation is also applicable
     UpperCamelCase_							:		int			=	(
         {
             '''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 				_A       (				self       :	str					, UpperCAmelCase_       :	int					, UpperCAmelCase_       :	List[str]					, UpperCAmelCase_       :	str					, UpperCAmelCase_       :	Optional[Any]					, UpperCAmelCase_       :	str      ):





       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 				_A       (				self       :	int					, UpperCAmelCase_       :	Optional[Any]					, UpperCAmelCase_       :	Dict					, UpperCAmelCase_       :	Union[str, Any]=False      ):
       SCREAMING_SNAKE_CASE       :      Union[str, Any]							=    super()._prepare_for_class(UpperCAmelCase_					, UpperCAmelCase_					, return_labels=UpperCAmelCase_      )

       if return_labels:
         if model_class.__name__ == "XLMForQuestionAnswering":
           SCREAMING_SNAKE_CASE       :      Any							=    torch.zeros(
               self.model_tester.batch_size					, dtype=torch.long					, device=UpperCAmelCase_      )
           SCREAMING_SNAKE_CASE       :      Optional[int]							=    torch.zeros(
               self.model_tester.batch_size					, dtype=torch.long					, device=UpperCAmelCase_      )

       return inputs_dict


     def 				_A       (				self       :	Any      ):
       SCREAMING_SNAKE_CASE       :      Dict							=    XLMModelTester(self      )
       SCREAMING_SNAKE_CASE       :      Tuple							=    ConfigTester(self					, config_class=UpperCAmelCase_					, emb_dim=37      )


     def 				_A       (				self       :	Union[str, Any]      ):
       self.config_tester.run_common_tests()


     def 				_A       (				self       :	Dict      ):
       SCREAMING_SNAKE_CASE       :      Dict							=    self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_xlm_model(*UpperCAmelCase_      )


     def 				_A       (				self       :	List[Any]      ):
       SCREAMING_SNAKE_CASE       :      Optional[int]							=    self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_xlm_lm_head(*UpperCAmelCase_      )


     def 				_A       (				self       :	str      ):
       SCREAMING_SNAKE_CASE       :      str							=    self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_xlm_simple_qa(*UpperCAmelCase_      )


     def 				_A       (				self       :	Union[str, Any]      ):
       SCREAMING_SNAKE_CASE       :      Dict							=    self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_xlm_qa(*UpperCAmelCase_      )


     def 				_A       (				self       :	List[Any]      ):
       SCREAMING_SNAKE_CASE       :      Tuple							=    self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_xlm_sequence_classif(*UpperCAmelCase_      )


     def 				_A       (				self       :	Optional[Any]      ):
       SCREAMING_SNAKE_CASE       :      Union[str, Any]							=    self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_xlm_token_classif(*UpperCAmelCase_      )


     def 				_A       (				self       :	str      ):
       SCREAMING_SNAKE_CASE       :      List[str]							=    self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_xlm_for_multiple_choice(*UpperCAmelCase_      )


     def 				_A       (				self       :	Optional[int]					, UpperCAmelCase_       :	Optional[int]					, UpperCAmelCase_       :	int					, UpperCAmelCase_       :	Union[str, Any]					, UpperCAmelCase_       :	Dict					, UpperCAmelCase_       :	Optional[int]					, UpperCAmelCase_       :	Dict=False					, UpperCAmelCase_       :	List[Any]=1      ):
       self.assertIsInstance(UpperCAmelCase_					, UpperCAmelCase_      )
       self.assertListEqual(
           [isinstance(UpperCAmelCase_					, UpperCAmelCase_      ) for iter_attentions in attentions]					, [True] * len(UpperCAmelCase_      )      )
       self.assertEqual(len(UpperCAmelCase_      )					, (max_length - min_length) * num_beam_groups      )

       for idx, iter_attentions in enumerate(UpperCAmelCase_      ):
         # adds PAD dummy token
         SCREAMING_SNAKE_CASE       :      List[str]							=    min_length + idx + 1
         SCREAMING_SNAKE_CASE       :      Optional[Any]							=    min_length + idx + 1

         SCREAMING_SNAKE_CASE       :      Optional[int]							=    (
             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(UpperCAmelCase_      )      )


     def 				_A       (				self       :	List[str]					, UpperCAmelCase_       :	List[str]					, UpperCAmelCase_       :	Dict					, UpperCAmelCase_       :	List[str]					, UpperCAmelCase_       :	Dict					, UpperCAmelCase_       :	List[str]					, UpperCAmelCase_       :	Optional[int]=False					, UpperCAmelCase_       :	Optional[Any]=1      ):
       self.assertIsInstance(UpperCAmelCase_					, UpperCAmelCase_      )
       self.assertListEqual(
           [isinstance(UpperCAmelCase_					, UpperCAmelCase_      ) for iter_hidden_states in hidden_states]					, [True] * len(UpperCAmelCase_      )					, )
       self.assertEqual(len(UpperCAmelCase_      )					, (max_length - min_length) * num_beam_groups      )

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






     @slow
     def 				_A       (				self       :	Optional[Any]      ):





       for model_name in XLM_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
         SCREAMING_SNAKE_CASE       :      Optional[int]							=    XLMModel.from_pretrained(UpperCAmelCase_      )
         self.assertIsNotNone(UpperCAmelCase_      )





@require_torch
class  SCREAMING_SNAKE_CASE			(			unittest.TestCase					):
     '''simple docstring'''


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














import unittest

import numpy as np

from transformers.testing_utils import is_flaky, require_torch, require_vision
from transformers.utils import is_torch_available, is_vision_available

from ...test_image_processing_common import ImageProcessingSavingTestMixin, prepare_image_inputs


if is_torch_available():
   import torch

if is_vision_available():
   from PIL import Image

   from transformers import DonutImageProcessor


class  SCREAMING_SNAKE_CASE			(			unittest.TestCase					):
     '''simple docstring'''


     def __init__(				self       :	int					, UpperCAmelCase_       :	List[Any]					, UpperCAmelCase_       :	Tuple=7					, UpperCAmelCase_       :	Dict=3					, UpperCAmelCase_       :	Any=18					, UpperCAmelCase_       :	Optional[int]=30					, UpperCAmelCase_       :	Any=400					, UpperCAmelCase_       :	Optional[int]=True					, UpperCAmelCase_       :	Union[str, Any]=None					, UpperCAmelCase_       :	int=True					, UpperCAmelCase_       :	Optional[int]=False					, UpperCAmelCase_       :	List[Any]=True					, UpperCAmelCase_       :	Optional[Any]=True					, UpperCAmelCase_       :	Any=[0.5, 0.5, 0.5]					, UpperCAmelCase_       :	Dict=[0.5, 0.5, 0.5]					, ):
       SCREAMING_SNAKE_CASE       :      Any							=    parent
       SCREAMING_SNAKE_CASE       :      int							=    batch_size
       SCREAMING_SNAKE_CASE       :      Dict							=    num_channels
       SCREAMING_SNAKE_CASE       :      str							=    image_size
       SCREAMING_SNAKE_CASE       :      Union[str, Any]							=    min_resolution
       SCREAMING_SNAKE_CASE       :      Any							=    max_resolution
       SCREAMING_SNAKE_CASE       :      int							=    do_resize
       SCREAMING_SNAKE_CASE       :      Union[str, Any]							=    size if size is not None else {"height": 18, "width": 20}
       SCREAMING_SNAKE_CASE       :      Optional[Any]							=    do_thumbnail
       SCREAMING_SNAKE_CASE       :      Tuple							=    do_align_axis
       SCREAMING_SNAKE_CASE       :      List[str]							=    do_pad
       SCREAMING_SNAKE_CASE       :      Optional[Any]							=    do_normalize
       SCREAMING_SNAKE_CASE       :      Any							=    image_mean
       SCREAMING_SNAKE_CASE       :      Any							=    image_std






     def 				_A       (				self       :	str      ):
       return {
           "do_resize": self.do_resize,
           "size": self.size,
           "do_thumbnail": self.do_thumbnail,
           "do_align_long_axis": self.do_align_axis,
           "do_pad": self.do_pad,
           "do_normalize": self.do_normalize,
           "image_mean": self.image_mean,
           "image_std": self.image_std,
       }





@require_torch
@require_vision
class  SCREAMING_SNAKE_CASE			(			lowerCAmelCase      ,		unittest.TestCase					):
     '''simple docstring'''


     UpperCamelCase_							:		str			=	DonutImageProcessor if is_vision_available() else None


     def 				_A       (				self       :	Tuple      ):
       SCREAMING_SNAKE_CASE       :      Optional[Any]							=    DonutImageProcessingTester(self      )


     @property
     def 				_A       (				self       :	Tuple      ):
       return self.image_processor_tester.prepare_image_processor_dict()


     def 				_A       (				self       :	List[Any]      ):
       SCREAMING_SNAKE_CASE       :      Optional[Any]							=    self.image_processing_class(**self.image_processor_dict      )
       self.assertTrue(hasattr(UpperCAmelCase_					, "do_resize"      )      )
       self.assertTrue(hasattr(UpperCAmelCase_					, "size"      )      )
       self.assertTrue(hasattr(UpperCAmelCase_					, "do_thumbnail"      )      )
       self.assertTrue(hasattr(UpperCAmelCase_					, "do_align_long_axis"      )      )
       self.assertTrue(hasattr(UpperCAmelCase_					, "do_pad"      )      )
       self.assertTrue(hasattr(UpperCAmelCase_					, "do_normalize"      )      )
       self.assertTrue(hasattr(UpperCAmelCase_					, "image_mean"      )      )
       self.assertTrue(hasattr(UpperCAmelCase_					, "image_std"      )      )


     def 				_A       (				self       :	Optional[Any]      ):
       SCREAMING_SNAKE_CASE       :      Any							=    self.image_processing_class.from_dict(self.image_processor_dict      )
       self.assertEqual(image_processor.size					, {"height": 18, "width": 20}      )

       SCREAMING_SNAKE_CASE       :      List[Any]							=    self.image_processing_class.from_dict(self.image_processor_dict					, size=42      )
       self.assertEqual(image_processor.size					, {"height": 42, "width": 42}      )

       # Previous config had dimensions in (width, height) order
       SCREAMING_SNAKE_CASE       :      Union[str, Any]							=    self.image_processing_class.from_dict(self.image_processor_dict					, size=(42, 84)      )
       self.assertEqual(image_processor.size					, {"height": 84, "width": 42}      )


     def 				_A       (				self       :	Dict      ):
       pass


     @is_flaky()
     def 				_A       (				self       :	Optional[Any]      ):
       # Initialize image_processing
       SCREAMING_SNAKE_CASE       :      Union[str, Any]							=    self.image_processing_class(**self.image_processor_dict      )
       # create random PIL images
       SCREAMING_SNAKE_CASE       :      Tuple							=    prepare_image_inputs(self.image_processor_tester					, equal_resolution=UpperCAmelCase_      )
       for image in image_inputs:
         self.assertIsInstance(UpperCAmelCase_					, Image.Image      )

       # Test not batched input
       SCREAMING_SNAKE_CASE       :      List[str]							=    image_processing(image_inputs[0]					, return_tensors="pt"      ).pixel_values
       self.assertEqual(
           encoded_images.shape					, (
               1,
               self.image_processor_tester.num_channels,
               self.image_processor_tester.size["height"],
               self.image_processor_tester.size["width"],
           )					, )

       # Test batched
       SCREAMING_SNAKE_CASE       :      Dict							=    image_processing(UpperCAmelCase_					, return_tensors="pt"      ).pixel_values
       self.assertEqual(
           encoded_images.shape					, (
               self.image_processor_tester.batch_size,
               self.image_processor_tester.num_channels,
               self.image_processor_tester.size["height"],
               self.image_processor_tester.size["width"],
           )					, )


     @is_flaky()
     def 				_A       (				self       :	List[str]      ):
       # Initialize image_processing
       SCREAMING_SNAKE_CASE       :      Dict							=    self.image_processing_class(**self.image_processor_dict      )
       # create random numpy tensors
       SCREAMING_SNAKE_CASE       :      List[Any]							=    prepare_image_inputs(self.image_processor_tester					, equal_resolution=UpperCAmelCase_					, numpify=UpperCAmelCase_      )
       for image in image_inputs:
         self.assertIsInstance(UpperCAmelCase_					, np.ndarray      )

       # Test not batched input
       SCREAMING_SNAKE_CASE       :      Any							=    image_processing(image_inputs[0]					, return_tensors="pt"      ).pixel_values
       self.assertEqual(
           encoded_images.shape					, (
               1,
               self.image_processor_tester.num_channels,
               self.image_processor_tester.size["height"],
               self.image_processor_tester.size["width"],
           )					, )

       # Test batched
       SCREAMING_SNAKE_CASE       :      Optional[int]							=    image_processing(UpperCAmelCase_					, return_tensors="pt"      ).pixel_values
       self.assertEqual(
           encoded_images.shape					, (
               self.image_processor_tester.batch_size,
               self.image_processor_tester.num_channels,
               self.image_processor_tester.size["height"],
               self.image_processor_tester.size["width"],
           )					, )






     @is_flaky()
     def 				_A       (				self       :	Dict      ):
       # Initialize image_processing
       SCREAMING_SNAKE_CASE       :      List[str]							=    self.image_processing_class(**self.image_processor_dict      )
       # create random PyTorch tensors
       SCREAMING_SNAKE_CASE       :      Tuple							=    prepare_image_inputs(self.image_processor_tester					, equal_resolution=UpperCAmelCase_					, torchify=UpperCAmelCase_      )
       for image in image_inputs:
         self.assertIsInstance(UpperCAmelCase_					, torch.Tensor      )

       # Test not batched input
       SCREAMING_SNAKE_CASE       :      int							=    image_processing(image_inputs[0]					, return_tensors="pt"      ).pixel_values
       self.assertEqual(
           encoded_images.shape					, (
               1,
               self.image_processor_tester.num_channels,
               self.image_processor_tester.size["height"],
               self.image_processor_tester.size["width"],
           )					, )

       # Test batched
       SCREAMING_SNAKE_CASE       :      Union[str, Any]							=    image_processing(UpperCAmelCase_					, return_tensors="pt"      ).pixel_values
       self.assertEqual(
           encoded_images.shape					, (
               self.image_processor_tester.batch_size,
               self.image_processor_tester.num_channels,
               self.image_processor_tester.size["height"],
               self.image_processor_tester.size["width"],
           )					, )






