






from typing import Dict, List, Optional, Union

import numpy as np

from ...image_processing_utils import BaseImageProcessor, BatchFeature, get_size_dict
from ...image_transforms import (
    center_crop,
    get_resize_output_image_size,
    normalize,
    rescale,
    resize,
    to_channel_dimension_format,
)
from ...image_utils import (
    IMAGENET_STANDARD_MEAN,
    IMAGENET_STANDARD_STD,
    ChannelDimension,
    ImageInput,
    PILImageResampling,
    is_valid_image,
    to_numpy_array,
    valid_images,
)
from ...utils import TensorType, is_vision_available, logging


if is_vision_available():
							import PIL


A__	:			Tuple			   = logging.get_logger(__name__)






def   a			(						lowerCamelCase_				):






				'''simple docstring'''
				if isinstance(_lowerCAmelCase		,   (list, tuple)				) and isinstance(videos[0]		,   (list, tuple)				) and is_valid_image(videos[0][0]				):
								return videos

				elif isinstance(_lowerCAmelCase		,   (list, tuple)				) and is_valid_image(videos[0]				):
								return [videos]

				elif is_valid_image(_lowerCAmelCase				):
								return [[videos]]

				raise ValueError(F"""Could not make batched video from {videos}"""				)




class       _UpperCAmelCase					(       __snake_case	):

				"""simple docstring"""




				lowercase__											=       ['pixel_values']




				def __init__(     self						:    int,  lowerCamelCase						:    Any = True,  lowerCamelCase						:    Tuple = None,  lowerCamelCase						:    List[str] = PILImageResampling.BILINEAR,  lowerCamelCase						:    int = True,  lowerCamelCase						:    str = None,  lowerCamelCase						:    Optional[int] = True,  lowerCamelCase						:    List[str] = 1 / 255,  lowerCamelCase						:    int = True,  lowerCamelCase						:    List[str] = None,  lowerCamelCase						:    str = None,  **lowerCamelCase						:    List[Any],  ):

								'''simple docstring'''
								super().__init__(**A_  )
								lowercase__    =			size if size is not None else {"shortest_edge": 224}
								lowercase__    =			get_size_dict(A_,  default_to_square=A_  )
								lowercase__    =			crop_size if crop_size is not None else {"height": 224, "width": 224}
								lowercase__    =			get_size_dict(A_,  param_name='''crop_size'''  )

								lowercase__    =			do_resize
								lowercase__    =			size
								lowercase__    =			do_center_crop
								lowercase__    =			crop_size
								lowercase__    =			resample
								lowercase__    =			do_rescale
								lowercase__    =			rescale_factor
								lowercase__    =			do_normalize
								lowercase__    =			image_mean if image_mean is not None else IMAGENET_STANDARD_MEAN
								lowercase__    =			image_std if image_std is not None else IMAGENET_STANDARD_STD




				def 				lowercase__   (     self						:    Optional[Any],  lowerCamelCase						:    List[Any],  lowerCamelCase						:    str,  lowerCamelCase						:    List[Any] = PILImageResampling.BILINEAR,  lowerCamelCase						:    Tuple = None,  **lowerCamelCase						:    List[Any],  ):

								'''simple docstring'''
								lowercase__    =			get_size_dict(A_,  default_to_square=A_  )
								if "shortest_edge" in size:
												lowercase__    =			get_resize_output_image_size(A_,  size['''shortest_edge'''],  default_to_square=A_  )
								elif "height" in size and "width" in size:
												lowercase__    =			(size["height"], size["width"])
								else:
												raise ValueError(F"""Size must have 'height' and 'width' or 'shortest_edge' as keys. Got {size.keys()}"""  )
								return resize(A_,  size=A_,  resample=A_,  data_format=A_,  **A_  )




				def 				lowercase__   (     self						:    List[str],  lowerCamelCase						:    List[Any],  lowerCamelCase						:    Dict,  lowerCamelCase						:    Optional[Any] = None,  **lowerCamelCase						:    List[str],  ):

								'''simple docstring'''
								lowercase__    =			get_size_dict(A_  )
								if "height" not in size or "width" not in size:
												raise ValueError(F"""Size must have 'height' and 'width' as keys. Got {size.keys()}"""  )
								return center_crop(A_,  size=(size['''height'''], size['''width''']),  data_format=A_,  **A_  )




				def 				lowercase__   (     self						:    Dict,  lowerCamelCase						:    int,  lowerCamelCase						:    List[str],  lowerCamelCase						:    Dict = None,  **lowerCamelCase						:    Union[str, Any],  ):

								'''simple docstring'''
								return rescale(A_,  scale=A_,  data_format=A_,  **A_  )




				def 				lowercase__   (     self						:    Optional[int],  lowerCamelCase						:    int,  lowerCamelCase						:    Tuple,  lowerCamelCase						:    int,  lowerCamelCase						:    Dict = None,  **lowerCamelCase						:    Union[str, Any],  ):

								'''simple docstring'''
								return normalize(A_,  mean=A_,  std=A_,  data_format=A_,  **A_  )




				def 				lowercase__   (     self						:    Optional[int],  lowerCamelCase						:    List[str],  lowerCamelCase						:    Optional[Any] = None,  lowerCamelCase						:    Tuple = None,  lowerCamelCase						:    int = None,  lowerCamelCase						:    Union[str, Any] = None,  lowerCamelCase						:    List[Any] = None,  lowerCamelCase						:    Any = None,  lowerCamelCase						:    Any = None,  lowerCamelCase						:    Optional[int] = None,  lowerCamelCase						:    str = None,  lowerCamelCase						:    Union[str, Any] = None,  lowerCamelCase						:    int = ChannelDimension.FIRST,  ):

								'''simple docstring'''




								if do_resize and size is None or resample is None:
												raise ValueError('''Size and resample must be specified if do_resize is True.'''  )

								if do_center_crop and crop_size is None:
												raise ValueError('''Crop size must be specified if do_center_crop is True.'''  )

								if do_rescale and rescale_factor is None:
												raise ValueError('''Rescale factor must be specified if do_rescale is True.'''  )

								if do_normalize and (image_mean is None or image_std is None):
												raise ValueError('''Image mean and std must be specified if do_normalize is True.'''  )

								# All transformations expect numpy arrays.
								lowercase__    =			to_numpy_array(A_  )

								if do_resize:
												lowercase__    =			self.resize(image=A_,  size=A_,  resample=A_  )

								if do_center_crop:
												lowercase__    =			self.center_crop(A_,  size=A_  )

								if do_rescale:
												lowercase__    =			self.rescale(image=A_,  scale=A_  )

								if do_normalize:
												lowercase__    =			self.normalize(image=A_,  mean=A_,  std=A_  )

								lowercase__    =			to_channel_dimension_format(A_,  A_  )
								return image




				def 				lowercase__   (     self						:    Any,  lowerCamelCase						:    Optional[int],  lowerCamelCase						:    Any = None,  lowerCamelCase						:    List[Any] = None,  lowerCamelCase						:    Dict = None,  lowerCamelCase						:    int = None,  lowerCamelCase						:    Any = None,  lowerCamelCase						:    List[str] = None,  lowerCamelCase						:    Tuple = None,  lowerCamelCase						:    Optional[Any] = None,  lowerCamelCase						:    List[str] = None,  lowerCamelCase						:    str = None,  lowerCamelCase						:    str = None,  lowerCamelCase						:    Tuple = ChannelDimension.FIRST,  **lowerCamelCase						:    List[str],  ):

								'''simple docstring'''
								lowercase__    =			do_resize if do_resize is not None else self.do_resize
								lowercase__    =			resample if resample is not None else self.resample
								lowercase__    =			do_center_crop if do_center_crop is not None else self.do_center_crop
								lowercase__    =			do_rescale if do_rescale is not None else self.do_rescale
								lowercase__    =			rescale_factor if rescale_factor is not None else self.rescale_factor
								lowercase__    =			do_normalize if do_normalize is not None else self.do_normalize
								lowercase__    =			image_mean if image_mean is not None else self.image_mean
								lowercase__    =			image_std if image_std is not None else self.image_std

								lowercase__    =			size if size is not None else self.size
								lowercase__    =			get_size_dict(A_,  default_to_square=A_  )
								lowercase__    =			crop_size if crop_size is not None else self.crop_size
								lowercase__    =			get_size_dict(A_,  param_name='''crop_size'''  )

								if not valid_images(A_  ):
												raise ValueError(
												    '''Invalid image type. Must be of type PIL.Image.Image, numpy.ndarray, '''
												    '''torch.Tensor, tf.Tensor or jax.ndarray.'''  )

								lowercase__    =			make_batched(A_  )

								lowercase__    =			[
								    [
								        self._preprocess_image(
								            image=A_,  do_resize=A_,  size=A_,  resample=A_,  do_center_crop=A_,  crop_size=A_,  do_rescale=A_,  rescale_factor=A_,  do_normalize=A_,  image_mean=A_,  image_std=A_,  data_format=A_,  )
								        for img in video
								    ]
								    for video in videos
								]

								lowercase__    =			{"pixel_values": videos}
								return BatchFeature(data=A_,  tensor_type=A_  )











import unittest

from transformers import LiltConfig, 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
from ...test_pipeline_mixin import PipelineTesterMixin


if is_torch_available():
    import torch

    from transformers import (
        LiltForQuestionAnswering,
        LiltForSequenceClassification,
        LiltForTokenClassification,
        LiltModel,
    )
    from transformers.models.lilt.modeling_lilt import LILT_PRETRAINED_MODEL_ARCHIVE_LIST



class 					A__   :





      def __init__( self       ,							A_       ,							A_=13       ,							A_=7       ,							A_=True       ,							A_=True       ,							A_=True       ,							A_=True       ,							A_=99       ,							A_=24       ,							A_=2       ,							A_=6       ,							A_=37       ,							A_="gelu"       ,							A_=0.1       ,							A_=0.1       ,							A_=512       ,							A_=16       ,							A_=2       ,							A_=0.02       ,							A_=3       ,							A_=None       ,							A_=1000       ,							):
       '''simple docstring'''




       UpperCamelCase	:					Union[str, Any]       	=   parent
       UpperCamelCase	:					List[Any]       	=   batch_size
       UpperCamelCase	:					Dict       	=   seq_length
       UpperCamelCase	:					Tuple       	=   is_training
       UpperCamelCase	:					Union[str, Any]       	=   use_input_mask
       UpperCamelCase	:					Tuple       	=   use_token_type_ids
       UpperCamelCase	:					Optional[Any]       	=   use_labels
       UpperCamelCase	:					str       	=   vocab_size
       UpperCamelCase	:					Optional[int]       	=   hidden_size
       UpperCamelCase	:					Any       	=   num_hidden_layers
       UpperCamelCase	:					Optional[Any]       	=   num_attention_heads
       UpperCamelCase	:					Optional[Any]       	=   intermediate_size
       UpperCamelCase	:					Optional[Any]       	=   hidden_act
       UpperCamelCase	:					Union[str, Any]       	=   hidden_dropout_prob
       UpperCamelCase	:					Union[str, Any]       	=   attention_probs_dropout_prob
       UpperCamelCase	:					List[Any]       	=   max_position_embeddings
       UpperCamelCase	:					str       	=   type_vocab_size
       UpperCamelCase	:					Optional[int]       	=   type_sequence_label_size
       UpperCamelCase	:					Dict       	=   initializer_range
       UpperCamelCase	:					int       	=   num_labels
       UpperCamelCase	:					Optional[int]       	=   scope
       UpperCamelCase	:					int       	=   range_bbox





      def        __UpperCamelCase( self   ):
       '''simple docstring'''




       UpperCamelCase	:					int       	=   ids_tensor([self.batch_size, self.seq_length]       ,							self.vocab_size   )

       UpperCamelCase	:					Any       	=   ids_tensor([self.batch_size, self.seq_length, 4]       ,							self.range_bbox   )
       # Ensure that bbox is legal
       for i in range(bbox.shape[0]   ):
        for j in range(bbox.shape[1]   ):
         if bbox[i, j, 3] < bbox[i, j, 1]:
          UpperCamelCase	:					Union[str, Any]       	=   bbox[i, j, 3]
          UpperCamelCase	:					int       	=   bbox[i, j, 1]
          UpperCamelCase	:					int       	=   t
         if bbox[i, j, 2] < bbox[i, j, 0]:
          UpperCamelCase	:					List[str]       	=   bbox[i, j, 2]
          UpperCamelCase	:					Optional[int]       	=   bbox[i, j, 0]
          UpperCamelCase	:					Optional[Any]       	=   t

       UpperCamelCase	:					Dict       	=   None
       if self.use_input_mask:
        UpperCamelCase	:					Optional[int]       	=   ids_tensor([self.batch_size, self.seq_length]       ,							vocab_size=2   )

       UpperCamelCase	:					str       	=   None
       if self.use_token_type_ids:
        UpperCamelCase	:					str       	=   ids_tensor([self.batch_size, self.seq_length]       ,							self.type_vocab_size   )

       UpperCamelCase	:					Dict       	=   None
       UpperCamelCase	:					int       	=   None
       if self.use_labels:
        UpperCamelCase	:					List[Any]       	=   ids_tensor([self.batch_size]       ,							self.type_sequence_label_size   )
        UpperCamelCase	:					int       	=   ids_tensor([self.batch_size, self.seq_length]       ,							self.num_labels   )

       UpperCamelCase	:					List[Any]       	=   self.get_config()

       return config, input_ids, bbox, token_type_ids, input_mask, sequence_labels, token_labels





      def        __UpperCamelCase( self   ):
       '''simple docstring'''




       return LiltConfig(
           vocab_size=self.vocab_size       ,							hidden_size=self.hidden_size       ,							num_hidden_layers=self.num_hidden_layers       ,							num_attention_heads=self.num_attention_heads       ,							intermediate_size=self.intermediate_size       ,							hidden_act=self.hidden_act       ,							hidden_dropout_prob=self.hidden_dropout_prob       ,							attention_probs_dropout_prob=self.attention_probs_dropout_prob       ,							max_position_embeddings=self.max_position_embeddings       ,							type_vocab_size=self.type_vocab_size       ,							initializer_range=self.initializer_range       ,							)





      def        __UpperCamelCase( self       ,							A_       ,							A_       ,							A_       ,							A_       ,							A_       ,							A_       ,							A_       ,							):
       '''simple docstring'''




       UpperCamelCase	:					Any       	=   LiltModel(config=A_   )
       model.to(A_   )
       model.eval()
       UpperCamelCase	:					str       	=   model(A_       ,							bbox=A_       ,							attention_mask=A_       ,							token_type_ids=A_   )
       UpperCamelCase	:					Optional[int]       	=   model(A_       ,							bbox=A_       ,							token_type_ids=A_   )
       UpperCamelCase	:					Any       	=   model(A_       ,							bbox=A_   )

       self.parent.assertEqual(result.last_hidden_state.shape       ,							(self.batch_size, self.seq_length, self.hidden_size)   )
       self.parent.assertEqual(result.pooler_output.shape       ,							(self.batch_size, self.hidden_size)   )





      def        __UpperCamelCase( self       ,							A_       ,							A_       ,							A_       ,							A_       ,							A_       ,							A_       ,							A_       ,							):
       '''simple docstring'''




       UpperCamelCase	:					Any       	=   self.num_labels
       UpperCamelCase	:					Dict       	=   LiltForTokenClassification(config=A_   )
       model.to(A_   )
       model.eval()
       UpperCamelCase	:					Dict       	=   model(
           A_       ,							bbox=A_       ,							attention_mask=A_       ,							token_type_ids=A_       ,							labels=A_   )
       self.parent.assertEqual(result.logits.shape       ,							(self.batch_size, self.seq_length, self.num_labels)   )





      def        __UpperCamelCase( self       ,							A_       ,							A_       ,							A_       ,							A_       ,							A_       ,							A_       ,							A_       ,							):
       '''simple docstring'''




       UpperCamelCase	:					Dict       	=   LiltForQuestionAnswering(config=A_   )
       model.to(A_   )
       model.eval()
       UpperCamelCase	:					List[str]       	=   model(
           A_       ,							bbox=A_       ,							attention_mask=A_       ,							token_type_ids=A_       ,							start_positions=A_       ,							end_positions=A_       ,							)
       self.parent.assertEqual(result.start_logits.shape       ,							(self.batch_size, self.seq_length)   )
       self.parent.assertEqual(result.end_logits.shape       ,							(self.batch_size, self.seq_length)   )





      def        __UpperCamelCase( self   ):
       '''simple docstring'''




       UpperCamelCase	:					Any       	=   self.prepare_config_and_inputs()
       (
           (
           UpperCamelCase
       )	,    (
           UpperCamelCase
       )	,    (
           UpperCamelCase
       )	,    (
           UpperCamelCase
       )	,    (
           UpperCamelCase
       )	,    (
           UpperCamelCase
       )	,    (
           UpperCamelCase
       )	,    
       )	:					Tuple       	=   config_and_inputs
       UpperCamelCase	:					Tuple       	=   {
           "input_ids": input_ids,
           "bbox": bbox,
           "token_type_ids": token_type_ids,
           "attention_mask": input_mask,
       }
       return config, inputs_dict



@require_torch
class 					A__   (       __snake_case       ,   __snake_case       ,   __snake_case       ,   unittest.TestCase			):
      _UpperCAmelCase	:Union[str, Any]						    =   (
          (
              LiltModel,
              LiltForSequenceClassification,
              LiltForTokenClassification,
              LiltForQuestionAnswering,
          )
          if is_torch_available()
          else ()
      )
      _UpperCAmelCase	:Optional[Any]						    =   (
          {
              'feature-extraction': LiltModel,
              'question-answering': LiltForQuestionAnswering,
              'text-classification': LiltForSequenceClassification,
              'token-classification': LiltForTokenClassification,
              'zero-shot': LiltForSequenceClassification,
          }
          if is_torch_available()
          else {}
      )
      _UpperCAmelCase	:Dict						    =   False
      _UpperCAmelCase	:Union[str, Any]						    =   False





      def        __UpperCamelCase( self       ,							A_       ,							A_       ,							A_       ,							A_       ,							A_   ):
       '''simple docstring'''




       return True





      def        __UpperCamelCase( self   ):
       '''simple docstring'''




       UpperCamelCase	:					Optional[int]       	=   LiltModelTester(self   )
       UpperCamelCase	:					Optional[int]       	=   ConfigTester(self       ,							config_class=A_       ,							hidden_size=37   )





      def        __UpperCamelCase( self   ):
       '''simple docstring'''




       self.config_tester.run_common_tests()





      def        __UpperCamelCase( self   ):
       '''simple docstring'''




       UpperCamelCase	:					Tuple       	=   self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_model(*A_   )





      def        __UpperCamelCase( self   ):
       '''simple docstring'''




       UpperCamelCase	:					Tuple       	=   self.model_tester.prepare_config_and_inputs()
       for type in ["absolute", "relative_key", "relative_key_query"]:
        UpperCamelCase	:					Union[str, Any]       	=   type
        self.model_tester.create_and_check_model(*A_   )





      def        __UpperCamelCase( self   ):
       '''simple docstring'''




       UpperCamelCase	:					int       	=   self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_for_token_classification(*A_   )





      def        __UpperCamelCase( self   ):
       '''simple docstring'''




       UpperCamelCase	:					List[Any]       	=   self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_for_question_answering(*A_   )





      @slow
      def        __UpperCamelCase( self   ):
       '''simple docstring'''




       for model_name in LILT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
        UpperCamelCase	:					Dict       	=   LiltModel.from_pretrained(A_   )
        self.assertIsNotNone(A_   )



@require_torch
@slow
class 					A__   (       unittest.TestCase			):





      def        __UpperCamelCase( self   ):
       '''simple docstring'''




       UpperCamelCase	:					int       	=   LiltModel.from_pretrained("SCUT-DLVCLab/lilt-roberta-en-base"   ).to(A_   )

       UpperCamelCase	:					Tuple       	=   torch.tensor([[1, 2]]       ,							device=A_   )
       UpperCamelCase	:					List[str]       	=   torch.tensor([[[1, 2, 3, 4], [5, 6, 7, 8]]]       ,							device=A_   )

       # forward pass
       with torch.no_grad():
        UpperCamelCase	:					Optional[int]       	=   model(input_ids=A_       ,							bbox=A_   )

       UpperCamelCase	:					List[str]       	=   torch.Size([1, 2, 768]   )
       UpperCamelCase	:					Any       	=   torch.tensor(
           [[-0.06_53, 0.09_50, -0.00_61], [-0.05_45, 0.09_26, -0.03_24]]       ,							device=A_       ,							)

       self.assertTrue(outputs.last_hidden_state.shape       ,							A_   )
       self.assertTrue(torch.allclose(outputs.last_hidden_state[0, :, :3]       ,							A_       ,							atol=1e-3   )   )
