

"""simple docstring"""




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   __lowercase      (     unittest.TestCase       ):

  def __init__(	self		:      Union[str, Any]       ,A		:      Union[str, Any]       ,A		:      Dict=7       ,A		:      Optional[int]=3       ,A		:      List[str]=18       ,A		:      Union[str, Any]=30       ,A		:      Tuple=400       ,A		:      Dict=True       ,A		:      List[str]=None       ,A		:      str=True       ,A		:      Optional[Any]=False       ,A		:      Optional[Any]=True       ,A		:      List[str]=True       ,A		:      Optional[int]=[0.5, 0.5, 0.5]       ,A		:      List[str]=[0.5, 0.5, 0.5]       ,):






   '''simple docstring'''
   UpperCAmelCase__       :	str        =       parent
   UpperCAmelCase__       :	List[str]        =       batch_size
   UpperCAmelCase__       :	List[str]        =       num_channels
   UpperCAmelCase__       :	Union[str, Any]        =       image_size
   UpperCAmelCase__       :	List[Any]        =       min_resolution
   UpperCAmelCase__       :	Optional[int]        =       max_resolution
   UpperCAmelCase__       :	str        =       do_resize
   UpperCAmelCase__       :	Tuple        =       size if size is not None else {"""height""": 18, """width""": 20}
   UpperCAmelCase__       :	List[str]        =       do_thumbnail
   UpperCAmelCase__       :	Optional[int]        =       do_align_axis
   UpperCAmelCase__       :	Union[str, Any]        =       do_pad
   UpperCAmelCase__       :	Tuple        =       do_normalize
   UpperCAmelCase__       :	Optional[Any]        =       image_mean
   UpperCAmelCase__       :	List[Any]        =       image_std




  def  __lowercase							(	self		:      Optional[int]     ):






   '''simple docstring'''
   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   __lowercase      (     __lowerCamelCase						,      unittest.TestCase       ):
  snake_case_												=							DonutImageProcessor if is_vision_available() else None

  def  __lowercase							(	self		:      str     ):






   '''simple docstring'''
   UpperCAmelCase__       :	Tuple        =       DonutImageProcessingTester(self     )

  @property
  def  __lowercase							(	self		:      Dict     ):






   '''simple docstring'''
   return self.image_processor_tester.prepare_image_processor_dict()

  def  __lowercase							(	self		:      Any     ):






   '''simple docstring'''
   UpperCAmelCase__       :	Any        =       self.image_processing_class(**self.image_processor_dict     )
   self.assertTrue(hasattr(A       ,"""do_resize"""     )     )
   self.assertTrue(hasattr(A       ,"""size"""     )     )
   self.assertTrue(hasattr(A       ,"""do_thumbnail"""     )     )
   self.assertTrue(hasattr(A       ,"""do_align_long_axis"""     )     )
   self.assertTrue(hasattr(A       ,"""do_pad"""     )     )
   self.assertTrue(hasattr(A       ,"""do_normalize"""     )     )
   self.assertTrue(hasattr(A       ,"""image_mean"""     )     )
   self.assertTrue(hasattr(A       ,"""image_std"""     )     )

  def  __lowercase							(	self		:      Optional[Any]     ):






   '''simple docstring'''
   UpperCAmelCase__       :	List[Any]        =       self.image_processing_class.from_dict(self.image_processor_dict     )
   self.assertEqual(image_processor.size       ,{"""height""": 18, """width""": 20}     )

   UpperCAmelCase__       :	str        =       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
   UpperCAmelCase__       :	str        =       self.image_processing_class.from_dict(self.image_processor_dict       ,size=(42, 84)     )
   self.assertEqual(image_processor.size       ,{"""height""": 84, """width""": 42}     )

  def  __lowercase							(	self		:      Dict     ):






   '''simple docstring'''
   pass

  @is_flaky()
  def  __lowercase							(	self		:      int     ):






   '''simple docstring'''
   # Initialize image_processing
   UpperCAmelCase__       :	Optional[int]        =       self.image_processing_class(**self.image_processor_dict     )
   # create random PIL images
   UpperCAmelCase__       :	Dict        =       prepare_image_inputs(self.image_processor_tester       ,equal_resolution=A     )
   for image in image_inputs:
    self.assertIsInstance(A       ,Image.Image     )

   # Test not batched input
   UpperCAmelCase__       :	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
   UpperCAmelCase__       :	Tuple        =       image_processing(A       ,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  __lowercase							(	self		:      List[str]     ):






   '''simple docstring'''
   # Initialize image_processing
   UpperCAmelCase__       :	Union[str, Any]        =       self.image_processing_class(**self.image_processor_dict     )
   # create random numpy tensors
   UpperCAmelCase__       :	Dict        =       prepare_image_inputs(self.image_processor_tester       ,equal_resolution=A       ,numpify=A     )
   for image in image_inputs:
    self.assertIsInstance(A       ,np.ndarray     )

   # Test not batched input
   UpperCAmelCase__       :	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
   UpperCAmelCase__       :	Optional[int]        =       image_processing(A       ,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  __lowercase							(	self		:      Any     ):






   '''simple docstring'''
   # Initialize image_processing
   UpperCAmelCase__       :	int        =       self.image_processing_class(**self.image_processor_dict     )
   # create random PyTorch tensors
   UpperCAmelCase__       :	int        =       prepare_image_inputs(self.image_processor_tester       ,equal_resolution=A       ,torchify=A     )
   for image in image_inputs:
    self.assertIsInstance(A       ,torch.Tensor     )

   # Test not batched input
   UpperCAmelCase__       :	List[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
   UpperCAmelCase__       :	List[Any]        =       image_processing(A       ,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"""],
       )       ,)






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


a  :      Any            =    logging.get_logger(__name__)

a  :      List[str]            =    {
    """transfo-xl-wt103""": """https://huggingface.co/transfo-xl-wt103/resolve/main/config.json""",
}

class 					_lowercase    (     UpperCAmelCase__			):






    '''simple docstring'''
    SCREAMING_SNAKE_CASE:				Optional[Any]          =    'transfo-xl'
    SCREAMING_SNAKE_CASE:				List[Any]          =    ['mems']
    SCREAMING_SNAKE_CASE:				List[Any]          =    {
        'n_token': 'vocab_size',
        'hidden_size': 'd_model',
        'num_attention_heads': 'n_head',
        'num_hidden_layers': 'n_layer',
    }




    def __init__(    self       ,						lowerCamelCase__=267_735       ,						lowerCamelCase__=[20_000, 40_000, 200_000]       ,						lowerCamelCase__=1_024       ,						lowerCamelCase__=1_024       ,						lowerCamelCase__=16       ,						lowerCamelCase__=64       ,						lowerCamelCase__=4_096       ,						lowerCamelCase__=4       ,						lowerCamelCase__=False       ,						lowerCamelCase__=18       ,						lowerCamelCase__=1_600       ,						lowerCamelCase__=1_000       ,						lowerCamelCase__=True       ,						lowerCamelCase__=True       ,						lowerCamelCase__=0       ,						lowerCamelCase__=-1       ,						lowerCamelCase__=True       ,						lowerCamelCase__=0.1       ,						lowerCamelCase__=0.0       ,						lowerCamelCase__=True       ,						lowerCamelCase__="normal"       ,						lowerCamelCase__=0.0_1       ,						lowerCamelCase__=0.0_1       ,						lowerCamelCase__=0.0_2       ,						lowerCamelCase__=1E-5       ,						lowerCamelCase__=0       ,						**lowerCamelCase__       ,						):
       lowerCAmelCase_:							Dict       =	vocab_size
       lowerCAmelCase_:							Any       =	[]
       self.cutoffs.extend(lowerCamelCase__ )
       if proj_share_all_but_first:
          lowerCAmelCase_:							int       =	[False] + [True] * len(self.cutoffs )
       else:
          lowerCAmelCase_:							Any       =	[False] + [False] * len(self.cutoffs )
       lowerCAmelCase_:							Dict       =	d_model
       lowerCAmelCase_:							Union[str, Any]       =	d_embed
       lowerCAmelCase_:							Dict       =	d_head
       lowerCAmelCase_:							Dict       =	d_inner
       lowerCAmelCase_:							List[str]       =	div_val
       lowerCAmelCase_:							List[str]       =	pre_lnorm
       lowerCAmelCase_:							List[str]       =	n_layer
       lowerCAmelCase_:							List[str]       =	n_head
       lowerCAmelCase_:							str       =	mem_len
       lowerCAmelCase_:							Any       =	same_length
       lowerCAmelCase_:							Optional[Any]       =	attn_type
       lowerCAmelCase_:							int       =	clamp_len
       lowerCAmelCase_:							Optional[int]       =	sample_softmax
       lowerCAmelCase_:							Optional[Any]       =	adaptive
       lowerCAmelCase_:							Optional[int]       =	dropout
       lowerCAmelCase_:							Union[str, Any]       =	dropatt
       lowerCAmelCase_:							Tuple       =	untie_r
       lowerCAmelCase_:							Union[str, Any]       =	init
       lowerCAmelCase_:							Optional[Any]       =	init_range
       lowerCAmelCase_:							Optional[Any]       =	proj_init_std
       lowerCAmelCase_:							Tuple       =	init_std
       lowerCAmelCase_:							Tuple       =	layer_norm_epsilon
       super().__init__(eos_token_id=lowerCamelCase__       ,						**lowerCamelCase__ )




    @property
    def      _a    (    self ):
       # Message copied from Transformer-XL documentation
       logger.info(F'''The model {self.model_type} is one of the few models that has no sequence length limit.''' )
       return -1




    @max_position_embeddings.setter
    def      _a    (    self       ,						lowerCamelCase__ ):
       # Message copied from Transformer-XL documentation
       raise NotImplementedError(
           F'''The model {self.model_type} is one of the few models that has no sequence length limit.''' )