import inspect
from typing import Callable, List, Optional, Union

import torch
from transformers import CLIPImageProcessor, CLIPTextModel, CLIPTokenizer

from diffusers import DiffusionPipeline
from diffusers.models import AutoencoderKL, UNetaDConditionModel
from diffusers.pipelines.stable_diffusion import StableDiffusionPipelineOutput
from diffusers.pipelines.stable_diffusion.safety_checker import StableDiffusionSafetyChecker
from diffusers.schedulers import DDIMScheduler, LMSDiscreteScheduler, PNDMScheduler
from diffusers.utils import logging


lowerCAmelCase__     :   Tuple		=   logging.get_logger(__name__)  # pylint: disable=invalid-name





class  __snake_case  (			lowercase__	):




					def __init__(   self    ,       __UpperCamelCase    ,       __UpperCamelCase    ,       __UpperCamelCase    ,       __UpperCamelCase    ,       __UpperCamelCase    ,       __UpperCamelCase    ,       __UpperCamelCase    ,       )     ->  List[str]:



									'''simple docstring'''







									super().__init__()
									self.register_modules(
									    vae=SCREAMING_SNAKE_CASE__    ,       text_encoder=SCREAMING_SNAKE_CASE__    ,       tokenizer=SCREAMING_SNAKE_CASE__    ,       unet=SCREAMING_SNAKE_CASE__    ,       scheduler=SCREAMING_SNAKE_CASE__    ,       safety_checker=SCREAMING_SNAKE_CASE__    ,       feature_extractor=SCREAMING_SNAKE_CASE__    ,       )




					def 			__a			(   self    ,       __UpperCamelCase = "auto"		)     ->  Optional[int]:



									'''simple docstring'''







									if slice_size == "auto":
													# half the attention head size is usually a good trade-off between
													# speed and memory
													snake_case__					:	List[Any]												=      self.unet.config.attention_head_dim // 2
									self.unet.set_attention_slice(SCREAMING_SNAKE_CASE__		)




					def 			__a			(   self		)     ->  str:



									'''simple docstring'''







									self.enable_attention_slicing(SCREAMING_SNAKE_CASE__		)




					@torch.no_grad()
					def __call__(   self    ,       __UpperCamelCase    ,       __UpperCamelCase = 512    ,       __UpperCamelCase = 512    ,       __UpperCamelCase = 50    ,       __UpperCamelCase = 7.5    ,       __UpperCamelCase = None    ,       __UpperCamelCase = 1    ,       __UpperCamelCase = 0.0    ,       __UpperCamelCase = None    ,       __UpperCamelCase = None    ,       __UpperCamelCase = "pil"    ,       __UpperCamelCase = True    ,       __UpperCamelCase = None    ,       __UpperCamelCase = 1    ,       __UpperCamelCase = None    ,       **__UpperCamelCase    ,       )     ->  List[str]:



									'''simple docstring'''







									if isinstance(SCREAMING_SNAKE_CASE__    ,       SCREAMING_SNAKE_CASE__		):
													snake_case__					:	Optional[int]												=      1
									elif isinstance(SCREAMING_SNAKE_CASE__    ,       SCREAMING_SNAKE_CASE__		):
													snake_case__					:	str												=      len(SCREAMING_SNAKE_CASE__		)
									else:
													raise ValueError(F"""`prompt` has to be of type `str` or `list` but is {type(SCREAMING_SNAKE_CASE__		)}"""		)

									if height % 8 != 0 or width % 8 != 0:
													raise ValueError(F"""`height` and `width` have to be divisible by 8 but are {height} and {width}."""		)

									if (callback_steps is None) or (
									    callback_steps is not None and (not isinstance(SCREAMING_SNAKE_CASE__    ,       SCREAMING_SNAKE_CASE__		) or callback_steps <= 0)
									):
													raise ValueError(
													    F"""`callback_steps` has to be a positive integer but is {callback_steps} of type"""
													    F""" {type(SCREAMING_SNAKE_CASE__		)}."""		)

									# get prompt text embeddings
									snake_case__					:	List[Any]												=      self.tokenizer(
									    SCREAMING_SNAKE_CASE__    ,       padding='max_length'    ,       max_length=self.tokenizer.model_max_length    ,       return_tensors='pt'    ,       )
									snake_case__					:	List[str]												=      text_inputs.input_ids

									if text_input_ids.shape[-1] > self.tokenizer.model_max_length:
													snake_case__					:	Tuple												=      self.tokenizer.batch_decode(text_input_ids[:, self.tokenizer.model_max_length :]		)
													logger.warning(
													    'The following part of your input was truncated because CLIP can only handle sequences up to'
													    F""" {self.tokenizer.model_max_length} tokens: {removed_text}"""		)
													snake_case__					:	Any												=      text_input_ids[:, : self.tokenizer.model_max_length]

									if text_embeddings is None:
													snake_case__					:	List[str]												=      self.text_encoder(text_input_ids.to(self.device		)		)[0]

									# duplicate text embeddings for each generation per prompt, using mps friendly method
									snake_case__					:	Any												=      text_embeddings.shape
									snake_case__					:	Optional[int]												=      text_embeddings.repeat(1    ,       SCREAMING_SNAKE_CASE__    ,       1		)
									snake_case__					:	Union[str, Any]												=      text_embeddings.view(bs_embed * num_images_per_prompt    ,       SCREAMING_SNAKE_CASE__    ,       -1		)

									# here `guidance_scale` is defined analog to the guidance weight `w` of equation (2)
									# of the Imagen paper: https://arxiv.org/pdf/2205.11487.pdf . `guidance_scale = 1`
									# corresponds to doing no classifier free guidance.
									snake_case__					:	List[str]												=      guidance_scale > 1.0
									# get unconditional embeddings for classifier free guidance
									if do_classifier_free_guidance:
													snake_case__					:	List[str]
													if negative_prompt is None:
																	snake_case__					:	str												=      ['']
													elif type(SCREAMING_SNAKE_CASE__		) is not type(SCREAMING_SNAKE_CASE__		):
																	raise TypeError(
																	    F"""`negative_prompt` should be the same type to `prompt`, but got {type(SCREAMING_SNAKE_CASE__		)} !="""
																	    F""" {type(SCREAMING_SNAKE_CASE__		)}."""		)
													elif isinstance(SCREAMING_SNAKE_CASE__    ,       SCREAMING_SNAKE_CASE__		):
																	snake_case__					:	Any												=      [negative_prompt]
													elif batch_size != len(SCREAMING_SNAKE_CASE__		):
																	raise ValueError(
																	    F"""`negative_prompt`: {negative_prompt} has batch size {len(SCREAMING_SNAKE_CASE__		)}, but `prompt`:"""
																	    F""" {prompt} has batch size {batch_size}. Please make sure that passed `negative_prompt` matches"""
																	    ' the batch size of `prompt`.'		)
													else:
																	snake_case__					:	List[Any]												=      negative_prompt

													snake_case__					:	int												=      text_input_ids.shape[-1]
													snake_case__					:	Tuple												=      self.tokenizer(
													    SCREAMING_SNAKE_CASE__    ,       padding='max_length'    ,       max_length=SCREAMING_SNAKE_CASE__    ,       truncation=SCREAMING_SNAKE_CASE__    ,       return_tensors='pt'    ,       )
													snake_case__					:	Optional[int]												=      self.text_encoder(uncond_input.input_ids.to(self.device		)		)[0]

													# duplicate unconditional embeddings for each generation per prompt, using mps friendly method
													snake_case__					:	Optional[Any]												=      uncond_embeddings.shape[1]
													snake_case__					:	int												=      uncond_embeddings.repeat(SCREAMING_SNAKE_CASE__    ,       SCREAMING_SNAKE_CASE__    ,       1		)
													snake_case__					:	Optional[int]												=      uncond_embeddings.view(batch_size * num_images_per_prompt    ,       SCREAMING_SNAKE_CASE__    ,       -1		)

													# For classifier free guidance, we need to do two forward passes.
													# Here we concatenate the unconditional and text embeddings into a single batch
													# to avoid doing two forward passes
													snake_case__					:	Optional[Any]												=      torch.cat([uncond_embeddings, text_embeddings]		)

									# get the initial random noise unless the user supplied it

									# Unlike in other pipelines, latents need to be generated in the target device
									# for 1-to-1 results reproducibility with the CompVis implementation.
									# However this currently doesn't work in `mps`.
									snake_case__					:	Optional[int]												=      (batch_size * num_images_per_prompt, self.unet.config.in_channels, height // 8, width // 8)
									snake_case__					:	Optional[Any]												=      (batch_size * num_images_per_prompt, self.unet.config.in_channels, 64, 64)
									snake_case__					:	Optional[Any]												=      text_embeddings.dtype
									if latents is None:
													if self.device.type == "mps":
																	# randn does not exist on mps
																	snake_case__					:	Tuple												=      torch.randn(
																	    SCREAMING_SNAKE_CASE__    ,       generator=SCREAMING_SNAKE_CASE__    ,       device='cpu'    ,       dtype=SCREAMING_SNAKE_CASE__		).to(self.device		)
																	snake_case__					:	Optional[Any]												=      torch.randn(SCREAMING_SNAKE_CASE__    ,       generator=SCREAMING_SNAKE_CASE__    ,       device='cpu'    ,       dtype=SCREAMING_SNAKE_CASE__		).to(
																	    self.device		)
													else:
																	snake_case__					:	Union[str, Any]												=      torch.randn(
																	    SCREAMING_SNAKE_CASE__    ,       generator=SCREAMING_SNAKE_CASE__    ,       device=self.device    ,       dtype=SCREAMING_SNAKE_CASE__		)
																	snake_case__					:	Any												=      torch.randn(SCREAMING_SNAKE_CASE__    ,       generator=SCREAMING_SNAKE_CASE__    ,       device=self.device    ,       dtype=SCREAMING_SNAKE_CASE__		)
									else:
													if latents_reference.shape != latents_shape:
																	raise ValueError(F"""Unexpected latents shape, got {latents.shape}, expected {latents_shape}"""		)
													snake_case__					:	Optional[Any]												=      latents_reference.to(self.device		)
													snake_case__					:	Union[str, Any]												=      latents.to(self.device		)

									# This is the key part of the pipeline where we
									# try to ensure that the generated images w/ the same seed
									# but different sizes actually result in similar images
									snake_case__					:	Dict												=      (latents_shape[3] - latents_shape_reference[3]) // 2
									snake_case__					:	Any												=      (latents_shape[2] - latents_shape_reference[2]) // 2
									snake_case__					:	List[Any]												=      latents_shape_reference[3] if dx >= 0 else latents_shape_reference[3] + 2 * dx
									snake_case__					:	Any												=      latents_shape_reference[2] if dy >= 0 else latents_shape_reference[2] + 2 * dy
									snake_case__					:	Dict												=      0 if dx < 0 else dx
									snake_case__					:	Any												=      0 if dy < 0 else dy
									snake_case__					:	Optional[int]												=      max(-dx    ,       0		)
									snake_case__					:	Optional[int]												=      max(-dy    ,       0		)
									# import pdb
									# pdb.set_trace()
									snake_case__					:	Dict												=      latents_reference[:, :, dy : dy + h, dx : dx + w]

									# set timesteps
									self.scheduler.set_timesteps(SCREAMING_SNAKE_CASE__		)

									# Some schedulers like PNDM have timesteps as arrays
									# It's more optimized to move all timesteps to correct device beforehand
									snake_case__					:	List[Any]												=      self.scheduler.timesteps.to(self.device		)

									# scale the initial noise by the standard deviation required by the scheduler
									snake_case__					:	Optional[int]												=      latents * self.scheduler.init_noise_sigma

									# prepare extra kwargs for the scheduler step, since not all schedulers have the same signature
									# eta (η) is only used with the DDIMScheduler, it will be ignored for other schedulers.
									# eta corresponds to η in DDIM paper: https://arxiv.org/abs/2010.02502
									# and should be between [0, 1]
									snake_case__					:	Union[str, Any]												=      'eta' in set(inspect.signature(self.scheduler.step		).parameters.keys()		)
									snake_case__					:	Optional[int]												=      {}
									if accepts_eta:
													snake_case__					:	List[Any]												=      eta

									for i, t in enumerate(self.progress_bar(SCREAMING_SNAKE_CASE__		)		):
													# expand the latents if we are doing classifier free guidance
													snake_case__					:	List[Any]												=      torch.cat([latents] * 2		) if do_classifier_free_guidance else latents
													snake_case__					:	Any												=      self.scheduler.scale_model_input(SCREAMING_SNAKE_CASE__    ,       SCREAMING_SNAKE_CASE__		)

													# predict the noise residual
													snake_case__					:	Optional[int]												=      self.unet(SCREAMING_SNAKE_CASE__    ,       SCREAMING_SNAKE_CASE__    ,       encoder_hidden_states=SCREAMING_SNAKE_CASE__		).sample

													# perform guidance
													if do_classifier_free_guidance:
																	snake_case__					:	Optional[int]												=      noise_pred.chunk(2		)
																	snake_case__					:	int												=      noise_pred_uncond + guidance_scale * (noise_pred_text - noise_pred_uncond)

													# compute the previous noisy sample x_t -> x_t-1
													snake_case__					:	Any												=      self.scheduler.step(SCREAMING_SNAKE_CASE__    ,       SCREAMING_SNAKE_CASE__    ,       SCREAMING_SNAKE_CASE__    ,       **SCREAMING_SNAKE_CASE__		).prev_sample

													# call the callback, if provided
													if callback is not None and i % callback_steps == 0:
																	callback(SCREAMING_SNAKE_CASE__    ,       SCREAMING_SNAKE_CASE__    ,       SCREAMING_SNAKE_CASE__		)

									snake_case__					:	Dict												=      1 / 0.1_8_2_1_5 * latents
									snake_case__					:	Dict												=      self.vae.decode(SCREAMING_SNAKE_CASE__		).sample

									snake_case__					:	str												=      (image / 2 + 0.5).clamp(0    ,       1		)

									# we always cast to float32 as this does not cause significant overhead and is compatible with bfloat16
									snake_case__					:	Union[str, Any]												=      image.cpu().permute(0    ,       2    ,       3    ,       1		).float().numpy()

									if self.safety_checker is not None:
													snake_case__					:	int												=      self.feature_extractor(self.numpy_to_pil(SCREAMING_SNAKE_CASE__		)    ,       return_tensors='pt'		).to(
													    self.device		)
													snake_case__					:	str												=      self.safety_checker(
													    images=SCREAMING_SNAKE_CASE__    ,       clip_input=safety_checker_input.pixel_values.to(text_embeddings.dtype		)		)
									else:
													snake_case__					:	List[Any]												=      None

									if output_type == "pil":
													snake_case__					:	Tuple												=      self.numpy_to_pil(SCREAMING_SNAKE_CASE__		)

									if not return_dict:
													return (image, has_nsfw_concept)

									return StableDiffusionPipelineOutput(images=SCREAMING_SNAKE_CASE__    ,       nsfw_content_detected=SCREAMING_SNAKE_CASE__		)






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  SCREAMING_SNAKE_CASE__  :
      def __init__(       self				:       Tuple     ,							SCREAMING_SNAKE_CASE__				:       List[Any]     ,							SCREAMING_SNAKE_CASE__				:       str=1_3     ,							SCREAMING_SNAKE_CASE__				:       Optional[int]=7     ,							SCREAMING_SNAKE_CASE__				:       str=True     ,							SCREAMING_SNAKE_CASE__				:       int=True     ,							SCREAMING_SNAKE_CASE__				:       Dict=True     ,							SCREAMING_SNAKE_CASE__				:       str=True     ,							SCREAMING_SNAKE_CASE__				:       str=9_9     ,							SCREAMING_SNAKE_CASE__				:       str=2_4     ,							SCREAMING_SNAKE_CASE__				:       Union[str, Any]=2     ,							SCREAMING_SNAKE_CASE__				:       Optional[Any]=6     ,							SCREAMING_SNAKE_CASE__				:       Optional[int]=3_7     ,							SCREAMING_SNAKE_CASE__				:       List[Any]="gelu"     ,							SCREAMING_SNAKE_CASE__				:       str=0.1     ,							SCREAMING_SNAKE_CASE__				:       List[Any]=0.1     ,							SCREAMING_SNAKE_CASE__				:       List[str]=5_1_2     ,							SCREAMING_SNAKE_CASE__				:       List[str]=1_6     ,							SCREAMING_SNAKE_CASE__				:       Union[str, Any]=2     ,							SCREAMING_SNAKE_CASE__				:       int=0.02     ,							SCREAMING_SNAKE_CASE__				:       Optional[Any]=3     ,							SCREAMING_SNAKE_CASE__				:       Optional[int]=None     ,							SCREAMING_SNAKE_CASE__				:       Tuple=1_0_0_0     ,							)		->			str:
             a_					:		Optional[Any]							=  parent
             a_					:		List[str]							=  batch_size
             a_					:		List[str]							=  seq_length
             a_					:		str							=  is_training
             a_					:		str							=  use_input_mask
             a_					:		int							=  use_token_type_ids
             a_					:		List[str]							=  use_labels
             a_					:		Optional[int]							=  vocab_size
             a_					:		Any							=  hidden_size
             a_					:		int							=  num_hidden_layers
             a_					:		List[str]							=  num_attention_heads
             a_					:		str							=  intermediate_size
             a_					:		Union[str, Any]							=  hidden_act
             a_					:		List[str]							=  hidden_dropout_prob
             a_					:		int							=  attention_probs_dropout_prob
             a_					:		int							=  max_position_embeddings
             a_					:		Tuple							=  type_vocab_size
             a_					:		Optional[Any]							=  type_sequence_label_size
             a_					:		Tuple							=  initializer_range
             a_					:		Dict							=  num_labels
             a_					:		str							=  scope
             a_					:		Optional[int]							=  range_bbox
      def        SCREAMING_SNAKE_CASE							(       self				:       List[str]     )		->			int:
             a_					:		Dict							=  ids_tensor([self.batch_size, self.seq_length]     ,							self.vocab_size     )

             a_					:		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]:
                                  a_					:		int							=  bbox[i, j, 3]
                                  a_					:		str							=  bbox[i, j, 1]
                                  a_					:		List[str]							=  t
                           if bbox[i, j, 2] < bbox[i, j, 0]:
                                  a_					:		Tuple							=  bbox[i, j, 2]
                                  a_					:		List[str]							=  bbox[i, j, 0]
                                  a_					:		Union[str, Any]							=  t

             a_					:		List[Any]							=  None
             if self.use_input_mask:
                    a_					:		Dict							=  ids_tensor([self.batch_size, self.seq_length]     ,							vocab_size=2     )

             a_					:		List[Any]							=  None
             if self.use_token_type_ids:
                    a_					:		Optional[Any]							=  ids_tensor([self.batch_size, self.seq_length]     ,							self.type_vocab_size     )

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

             a_					:		Optional[int]							=  self.get_config()

             return config, input_ids, bbox, token_type_ids, input_mask, sequence_labels, token_labels
      def        SCREAMING_SNAKE_CASE							(       self				:       Dict     )		->			int:
             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        SCREAMING_SNAKE_CASE							(       self				:       Optional[int]     ,							SCREAMING_SNAKE_CASE__				:       Dict     ,							SCREAMING_SNAKE_CASE__				:       Any     ,							SCREAMING_SNAKE_CASE__				:       Any     ,							SCREAMING_SNAKE_CASE__				:       Optional[int]     ,							SCREAMING_SNAKE_CASE__				:       int     ,							SCREAMING_SNAKE_CASE__				:       Union[str, Any]     ,							SCREAMING_SNAKE_CASE__				:       Optional[Any]     ,							)		->			str:
             a_					:		Any							=  LiltModel(config=SCREAMING_SNAKE_CASE__     )
             model.to(SCREAMING_SNAKE_CASE__     )
             model.eval()
             a_					:		Any							=  model(SCREAMING_SNAKE_CASE__     ,							bbox=SCREAMING_SNAKE_CASE__     ,							attention_mask=SCREAMING_SNAKE_CASE__     ,							token_type_ids=SCREAMING_SNAKE_CASE__     )
             a_					:		Optional[int]							=  model(SCREAMING_SNAKE_CASE__     ,							bbox=SCREAMING_SNAKE_CASE__     ,							token_type_ids=SCREAMING_SNAKE_CASE__     )
             a_					:		List[Any]							=  model(SCREAMING_SNAKE_CASE__     ,							bbox=SCREAMING_SNAKE_CASE__     )

             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        SCREAMING_SNAKE_CASE							(       self				:       Tuple     ,							SCREAMING_SNAKE_CASE__				:       str     ,							SCREAMING_SNAKE_CASE__				:       int     ,							SCREAMING_SNAKE_CASE__				:       int     ,							SCREAMING_SNAKE_CASE__				:       Union[str, Any]     ,							SCREAMING_SNAKE_CASE__				:       Optional[int]     ,							SCREAMING_SNAKE_CASE__				:       Any     ,							SCREAMING_SNAKE_CASE__				:       Optional[Any]     ,							)		->			int:
             a_					:		Any							=  self.num_labels
             a_					:		str							=  LiltForTokenClassification(config=SCREAMING_SNAKE_CASE__     )
             model.to(SCREAMING_SNAKE_CASE__     )
             model.eval()
             a_					:		str							=  model(
                 SCREAMING_SNAKE_CASE__     ,							bbox=SCREAMING_SNAKE_CASE__     ,							attention_mask=SCREAMING_SNAKE_CASE__     ,							token_type_ids=SCREAMING_SNAKE_CASE__     ,							labels=SCREAMING_SNAKE_CASE__     )
             self.parent.assertEqual(result.logits.shape     ,							(self.batch_size, self.seq_length, self.num_labels)     )
      def        SCREAMING_SNAKE_CASE							(       self				:       Any     ,							SCREAMING_SNAKE_CASE__				:       Tuple     ,							SCREAMING_SNAKE_CASE__				:       List[str]     ,							SCREAMING_SNAKE_CASE__				:       List[str]     ,							SCREAMING_SNAKE_CASE__				:       int     ,							SCREAMING_SNAKE_CASE__				:       Any     ,							SCREAMING_SNAKE_CASE__				:       Dict     ,							SCREAMING_SNAKE_CASE__				:       Union[str, Any]     ,							)		->			str:
             a_					:		Union[str, Any]							=  LiltForQuestionAnswering(config=SCREAMING_SNAKE_CASE__     )
             model.to(SCREAMING_SNAKE_CASE__     )
             model.eval()
             a_					:		List[str]							=  model(
                 SCREAMING_SNAKE_CASE__     ,							bbox=SCREAMING_SNAKE_CASE__     ,							attention_mask=SCREAMING_SNAKE_CASE__     ,							token_type_ids=SCREAMING_SNAKE_CASE__     ,							start_positions=SCREAMING_SNAKE_CASE__     ,							end_positions=SCREAMING_SNAKE_CASE__     ,							)
             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				:       int     )		->			List[str]:
             a_					:		int							=  self.prepare_config_and_inputs()
             (
                 (
                 a_
             )							,		(
                 a_
             )							,		(
                 a_
             )							,		(
                 a_
             )							,		(
                 a_
             )							,		(
                 a_
             )							,		(
                 a_
             )							,		
             )					:		List[Any]							=  config_and_inputs
             a_					:		Optional[int]							=  {
                 'input_ids': input_ids,
                 'bbox': bbox,
                 'token_type_ids': token_type_ids,
                 'attention_mask': input_mask,
             }
             return config, inputs_dict
@require_torch
class  SCREAMING_SNAKE_CASE__  (				lowercase__						,    lowercase__						,    lowercase__						,    unittest.TestCase			):
      snake_case__				:     Union[str, Any]        =						(
          (
              LiltModel,
              LiltForSequenceClassification,
              LiltForTokenClassification,
              LiltForQuestionAnswering,
          )
          if is_torch_available()
          else ()
      )
      snake_case__				:     str        =						(
          {
              '''feature-extraction''': LiltModel,
              '''question-answering''': LiltForQuestionAnswering,
              '''text-classification''': LiltForSequenceClassification,
              '''token-classification''': LiltForTokenClassification,
              '''zero-shot''': LiltForSequenceClassification,
          }
          if is_torch_available()
          else {}
      )
      snake_case__				:     List[str]        =						False
      snake_case__				:     str        =						False
      def        SCREAMING_SNAKE_CASE							(       self				:       Dict     ,							SCREAMING_SNAKE_CASE__				:       str     ,							SCREAMING_SNAKE_CASE__				:       Tuple     ,							SCREAMING_SNAKE_CASE__				:       List[str]     ,							SCREAMING_SNAKE_CASE__				:       Optional[int]     ,							SCREAMING_SNAKE_CASE__				:       int     )		->			int:
             return True
      def        SCREAMING_SNAKE_CASE							(       self				:       Optional[Any]     )		->			Tuple:
             a_					:		str							=  LiltModelTester(self     )
             a_					:		List[Any]							=  ConfigTester(self     ,							config_class=SCREAMING_SNAKE_CASE__     ,							hidden_size=3_7     )
      def        SCREAMING_SNAKE_CASE							(       self				:       Dict     )		->			List[Any]:
             self.config_tester.run_common_tests()
      def        SCREAMING_SNAKE_CASE							(       self				:       Optional[int]     )		->			str:
             a_					:		Dict							=  self.model_tester.prepare_config_and_inputs()
             self.model_tester.create_and_check_model(*SCREAMING_SNAKE_CASE__     )
      def        SCREAMING_SNAKE_CASE							(       self				:       Dict     )		->			Optional[int]:
             a_					:		Tuple							=  self.model_tester.prepare_config_and_inputs()
             for type in ["absolute", "relative_key", "relative_key_query"]:
                    a_					:		List[str]							=  type
                    self.model_tester.create_and_check_model(*SCREAMING_SNAKE_CASE__     )
      def        SCREAMING_SNAKE_CASE							(       self				:       int     )		->			Optional[Any]:
             a_					:		List[Any]							=  self.model_tester.prepare_config_and_inputs()
             self.model_tester.create_and_check_for_token_classification(*SCREAMING_SNAKE_CASE__     )
      def        SCREAMING_SNAKE_CASE							(       self				:       List[str]     )		->			List[str]:
             a_					:		Optional[Any]							=  self.model_tester.prepare_config_and_inputs()
             self.model_tester.create_and_check_for_question_answering(*SCREAMING_SNAKE_CASE__     )

      @slow
      def        SCREAMING_SNAKE_CASE							(       self				:       str     )		->			Union[str, Any]:
             for model_name in LILT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
                    a_					:		List[Any]							=  LiltModel.from_pretrained(SCREAMING_SNAKE_CASE__     )
                    self.assertIsNotNone(SCREAMING_SNAKE_CASE__     )
@require_torch
@slow
class  SCREAMING_SNAKE_CASE__  (				unittest.TestCase			):
      def        SCREAMING_SNAKE_CASE							(       self				:       Union[str, Any]     )		->			Union[str, Any]:
             a_					:		List[str]							=  LiltModel.from_pretrained('SCUT-DLVCLab/lilt-roberta-en-base'     ).to(SCREAMING_SNAKE_CASE__     )

             a_					:		str							=  torch.tensor([[1, 2]]     ,							device=SCREAMING_SNAKE_CASE__     )
             a_					:		List[Any]							=  torch.tensor([[[1, 2, 3, 4], [5, 6, 7, 8]]]     ,							device=SCREAMING_SNAKE_CASE__     )

             # forward pass
             with torch.no_grad():
                    a_					:		str							=  model(input_ids=SCREAMING_SNAKE_CASE__     ,							bbox=SCREAMING_SNAKE_CASE__     )

             a_					:		Optional[int]							=  torch.Size([1, 2, 7_6_8]     )
             a_					:		int							=  torch.tensor(
                 [[-0.0653, 0.0950, -0.0061], [-0.0545, 0.0926, -0.0324]]     ,							device=SCREAMING_SNAKE_CASE__     ,							)

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


