import gc
import random
import tempfile
import unittest

import numpy as np
import torch
from transformers import CLIPTextConfig, CLIPTextModel, CLIPTokenizer

from diffusers import AutoencoderKL, DDIMScheduler, LMSDiscreteScheduler, PNDMScheduler, UNetaDConditionModel
from diffusers.pipelines.stable_diffusion_safe import StableDiffusionPipelineSafe as StableDiffusionPipeline
from diffusers.utils import floats_tensor, nightly, torch_device
from diffusers.utils.testing_utils import require_torch_gpu





class  __snake_case  (			unittest.TestCase	):




					def 			__a			(   self		)     ->  Optional[Any]:



									'''simple docstring'''







									super().tearDown()
									gc.collect()
									torch.cuda.empty_cache()




					@property
					def 			__a			(   self		)     ->  Tuple:



									'''simple docstring'''







									snake_case__					:	Optional[int]												=      1
									snake_case__					:	Union[str, Any]												=      3
									snake_case__					:	Union[str, Any]												=      (32, 32)

									snake_case__					:	str												=      floats_tensor((batch_size, num_channels) + sizes    ,       rng=random.Random(0		)		).to(SCREAMING_SNAKE_CASE__		)
									return image




					@property
					def 			__a			(   self		)     ->  str:



									'''simple docstring'''







									torch.manual_seed(0		)
									snake_case__					:	List[Any]												=      UNetaDConditionModel(
									    block_out_channels=(32, 64)    ,       layers_per_block=2    ,       sample_size=32    ,       in_channels=4    ,       out_channels=4    ,       down_block_types=('DownBlock2D', 'CrossAttnDownBlock2D')    ,       up_block_types=('CrossAttnUpBlock2D', 'UpBlock2D')    ,       cross_attention_dim=32    ,       )
									return model




					@property
					def 			__a			(   self		)     ->  List[Any]:



									'''simple docstring'''







									torch.manual_seed(0		)
									snake_case__					:	int												=      AutoencoderKL(
									    block_out_channels=[32, 64]    ,       in_channels=3    ,       out_channels=3    ,       down_block_types=['DownEncoderBlock2D', 'DownEncoderBlock2D']    ,       up_block_types=['UpDecoderBlock2D', 'UpDecoderBlock2D']    ,       latent_channels=4    ,       )
									return model




					@property
					def 			__a			(   self		)     ->  Tuple:



									'''simple docstring'''







									torch.manual_seed(0		)
									snake_case__					:	List[str]												=      CLIPTextConfig(
									    bos_token_id=0    ,       eos_token_id=2    ,       hidden_size=32    ,       intermediate_size=37    ,       layer_norm_eps=1E-05    ,       num_attention_heads=4    ,       num_hidden_layers=5    ,       pad_token_id=1    ,       vocab_size=1000    ,       )
									return CLIPTextModel(SCREAMING_SNAKE_CASE__		)




					@property
					def 			__a			(   self		)     ->  List[Any]:



									'''simple docstring'''







									def extract(*__UpperCamelCase    ,       **__UpperCamelCase		):





													class  __snake_case  :




																		def __init__(   self		)     ->  List[Any]:



																						'''simple docstring'''







																						snake_case__					:	str												=      torch.ones([0]		)




																		def 			__a			(   self    ,       __UpperCamelCase		)     ->  str:



																						'''simple docstring'''







																						self.pixel_values.to(SCREAMING_SNAKE_CASE__		)
																						return self




													return Out()

									return extract




					def 			__a			(   self		)     ->  Optional[int]:



									'''simple docstring'''







									snake_case__					:	Optional[Any]												=      'cpu'  # ensure determinism for the device-dependent torch.Generator
									snake_case__					:	Optional[Any]												=      self.dummy_cond_unet
									snake_case__					:	List[str]												=      DDIMScheduler(
									    beta_start=0.0_0_0_8_5    ,       beta_end=0.0_1_2    ,       beta_schedule='scaled_linear'    ,       clip_sample=SCREAMING_SNAKE_CASE__    ,       set_alpha_to_one=SCREAMING_SNAKE_CASE__    ,       )

									snake_case__					:	Optional[Any]												=      self.dummy_vae
									snake_case__					:	int												=      self.dummy_text_encoder
									snake_case__					:	Any												=      CLIPTokenizer.from_pretrained('hf-internal-testing/tiny-random-clip'		)

									# make sure here that pndm scheduler skips prk
									snake_case__					:	List[Any]												=      StableDiffusionPipeline(
									    unet=SCREAMING_SNAKE_CASE__    ,       scheduler=SCREAMING_SNAKE_CASE__    ,       vae=SCREAMING_SNAKE_CASE__    ,       text_encoder=SCREAMING_SNAKE_CASE__    ,       tokenizer=SCREAMING_SNAKE_CASE__    ,       safety_checker=SCREAMING_SNAKE_CASE__    ,       feature_extractor=self.dummy_extractor    ,       )
									snake_case__					:	List[str]												=      sd_pipe.to(SCREAMING_SNAKE_CASE__		)
									sd_pipe.set_progress_bar_config(disable=SCREAMING_SNAKE_CASE__		)

									snake_case__					:	Optional[int]												=      'A painting of a squirrel eating a burger'

									snake_case__					:	str												=      torch.Generator(device=SCREAMING_SNAKE_CASE__		).manual_seed(0		)
									snake_case__					:	List[Any]												=      sd_pipe([prompt]    ,       generator=SCREAMING_SNAKE_CASE__    ,       guidance_scale=6.0    ,       num_inference_steps=2    ,       output_type='np'		)
									snake_case__					:	str												=      output.images

									snake_case__					:	str												=      torch.Generator(device=SCREAMING_SNAKE_CASE__		).manual_seed(0		)
									snake_case__					:	Optional[Any]												=      sd_pipe(
									    [prompt]    ,       generator=SCREAMING_SNAKE_CASE__    ,       guidance_scale=6.0    ,       num_inference_steps=2    ,       output_type='np'    ,       return_dict=SCREAMING_SNAKE_CASE__    ,       )[0]

									snake_case__					:	str												=      image[0, -3:, -3:, -1]
									snake_case__					:	int												=      image_from_tuple[0, -3:, -3:, -1]

									assert image.shape == (1, 64, 64, 3)
									snake_case__					:	Dict												=      np.array([0.5_7_5_6, 0.6_1_1_8, 0.5_0_0_5, 0.5_0_4_1, 0.5_4_7_1, 0.4_7_2_6, 0.4_9_7_6, 0.4_8_6_5, 0.4_8_6_4]		)

									assert np.abs(image_slice.flatten() - expected_slice		).max() < 1E-2
									assert np.abs(image_from_tuple_slice.flatten() - expected_slice		).max() < 1E-2




					def 			__a			(   self		)     ->  Tuple:



									'''simple docstring'''







									snake_case__					:	str												=      'cpu'  # ensure determinism for the device-dependent torch.Generator
									snake_case__					:	Dict												=      self.dummy_cond_unet
									snake_case__					:	List[Any]												=      PNDMScheduler(skip_prk_steps=SCREAMING_SNAKE_CASE__		)
									snake_case__					:	Any												=      self.dummy_vae
									snake_case__					:	Optional[Any]												=      self.dummy_text_encoder
									snake_case__					:	List[str]												=      CLIPTokenizer.from_pretrained('hf-internal-testing/tiny-random-clip'		)

									# make sure here that pndm scheduler skips prk
									snake_case__					:	Any												=      StableDiffusionPipeline(
									    unet=SCREAMING_SNAKE_CASE__    ,       scheduler=SCREAMING_SNAKE_CASE__    ,       vae=SCREAMING_SNAKE_CASE__    ,       text_encoder=SCREAMING_SNAKE_CASE__    ,       tokenizer=SCREAMING_SNAKE_CASE__    ,       safety_checker=SCREAMING_SNAKE_CASE__    ,       feature_extractor=self.dummy_extractor    ,       )
									snake_case__					:	Dict												=      sd_pipe.to(SCREAMING_SNAKE_CASE__		)
									sd_pipe.set_progress_bar_config(disable=SCREAMING_SNAKE_CASE__		)

									snake_case__					:	int												=      'A painting of a squirrel eating a burger'
									snake_case__					:	Any												=      torch.Generator(device=SCREAMING_SNAKE_CASE__		).manual_seed(0		)
									snake_case__					:	Any												=      sd_pipe([prompt]    ,       generator=SCREAMING_SNAKE_CASE__    ,       guidance_scale=6.0    ,       num_inference_steps=2    ,       output_type='np'		)

									snake_case__					:	Any												=      output.images

									snake_case__					:	Optional[int]												=      torch.Generator(device=SCREAMING_SNAKE_CASE__		).manual_seed(0		)
									snake_case__					:	Dict												=      sd_pipe(
									    [prompt]    ,       generator=SCREAMING_SNAKE_CASE__    ,       guidance_scale=6.0    ,       num_inference_steps=2    ,       output_type='np'    ,       return_dict=SCREAMING_SNAKE_CASE__    ,       )[0]

									snake_case__					:	Tuple												=      image[0, -3:, -3:, -1]
									snake_case__					:	List[str]												=      image_from_tuple[0, -3:, -3:, -1]

									assert image.shape == (1, 64, 64, 3)
									snake_case__					:	Any												=      np.array([0.5_1_2_5, 0.5_7_1_6, 0.4_8_2_8, 0.5_0_6_0, 0.5_6_5_0, 0.4_7_6_8, 0.5_1_8_5, 0.4_8_9_5, 0.4_9_9_3]		)

									assert np.abs(image_slice.flatten() - expected_slice		).max() < 1E-2
									assert np.abs(image_from_tuple_slice.flatten() - expected_slice		).max() < 1E-2




					def 			__a			(   self		)     ->  Optional[Any]:



									'''simple docstring'''







									snake_case__					:	Optional[Any]												=      StableDiffusionPipeline.from_pretrained(
									    'hf-internal-testing/tiny-stable-diffusion-lms-pipe'    ,       safety_checker=SCREAMING_SNAKE_CASE__		)
									assert isinstance(SCREAMING_SNAKE_CASE__    ,       SCREAMING_SNAKE_CASE__		)
									assert isinstance(pipe.scheduler    ,       SCREAMING_SNAKE_CASE__		)
									assert pipe.safety_checker is None

									snake_case__					:	Union[str, Any]												=      pipe('example prompt'    ,       num_inference_steps=2		).images[0]
									assert image is not None

									# check that there's no error when saving a pipeline with one of the models being None
									with tempfile.TemporaryDirectory() as tmpdirname:
													pipe.save_pretrained(SCREAMING_SNAKE_CASE__		)
													snake_case__					:	List[str]												=      StableDiffusionPipeline.from_pretrained(SCREAMING_SNAKE_CASE__		)

									# sanity check that the pipeline still works
									assert pipe.safety_checker is None
									snake_case__					:	str												=      pipe('example prompt'    ,       num_inference_steps=2		).images[0]
									assert image is not None




					@unittest.skipIf(torch_device != 'cuda'    ,       'This test requires a GPU'		)
					def 			__a			(   self		)     ->  Optional[int]:



									'''simple docstring'''







									snake_case__					:	Union[str, Any]												=      self.dummy_cond_unet
									snake_case__					:	Dict												=      PNDMScheduler(skip_prk_steps=SCREAMING_SNAKE_CASE__		)
									snake_case__					:	Any												=      self.dummy_vae
									snake_case__					:	Tuple												=      self.dummy_text_encoder
									snake_case__					:	List[Any]												=      CLIPTokenizer.from_pretrained('hf-internal-testing/tiny-random-clip'		)

									# put models in fp16
									snake_case__					:	Union[str, Any]												=      unet.half()
									snake_case__					:	Dict												=      vae.half()
									snake_case__					:	Tuple												=      bert.half()

									# make sure here that pndm scheduler skips prk
									snake_case__					:	Tuple												=      StableDiffusionPipeline(
									    unet=SCREAMING_SNAKE_CASE__    ,       scheduler=SCREAMING_SNAKE_CASE__    ,       vae=SCREAMING_SNAKE_CASE__    ,       text_encoder=SCREAMING_SNAKE_CASE__    ,       tokenizer=SCREAMING_SNAKE_CASE__    ,       safety_checker=SCREAMING_SNAKE_CASE__    ,       feature_extractor=self.dummy_extractor    ,       )
									snake_case__					:	Union[str, Any]												=      sd_pipe.to(SCREAMING_SNAKE_CASE__		)
									sd_pipe.set_progress_bar_config(disable=SCREAMING_SNAKE_CASE__		)

									snake_case__					:	Dict												=      'A painting of a squirrel eating a burger'
									snake_case__					:	Tuple												=      sd_pipe([prompt]    ,       num_inference_steps=2    ,       output_type='np'		).images

									assert image.shape == (1, 64, 64, 3)





@nightly
@require_torch_gpu
class  __snake_case  (			unittest.TestCase	):




					def 			__a			(   self		)     ->  str:



									'''simple docstring'''







									super().tearDown()
									gc.collect()
									torch.cuda.empty_cache()




					def 			__a			(   self		)     ->  Optional[Any]:



									'''simple docstring'''







									snake_case__					:	Dict												=      StableDiffusionPipeline.from_pretrained('runwayml/stable-diffusion-v1-5'    ,       safety_checker=SCREAMING_SNAKE_CASE__		)
									snake_case__					:	Optional[int]												=      LMSDiscreteScheduler.from_config(sd_pipe.scheduler.config		)
									snake_case__					:	Optional[Any]												=      sd_pipe.to(SCREAMING_SNAKE_CASE__		)
									sd_pipe.set_progress_bar_config(disable=SCREAMING_SNAKE_CASE__		)

									snake_case__					:	Union[str, Any]												=      (
									    'portrait of girl with smokey eyes makeup in abandoned hotel, grange clothes, redshift, wide high angle'
									    ' coloured polaroid photograph with flash, kodak film, hyper real, stunning moody cinematography, with'
									    ' anamorphic lenses, by maripol, fallen angels by wong kar - wai, style of suspiria and neon demon and'
									    ' children from bahnhof zoo, detailed '
									)
									snake_case__					:	Dict												=      4003660346
									snake_case__					:	Union[str, Any]												=      7

									# without safety guidance (sld_guidance_scale = 0)
									snake_case__					:	Dict												=      torch.manual_seed(SCREAMING_SNAKE_CASE__		)
									snake_case__					:	str												=      sd_pipe(
									    [prompt]    ,       generator=SCREAMING_SNAKE_CASE__    ,       guidance_scale=SCREAMING_SNAKE_CASE__    ,       num_inference_steps=50    ,       output_type='np'    ,       width=512    ,       height=512    ,       sld_guidance_scale=0    ,       )

									snake_case__					:	List[str]												=      output.images
									snake_case__					:	Union[str, Any]												=      image[0, -3:, -3:, -1]
									snake_case__					:	Any												=      [0.2_2_7_8, 0.2_2_3_1, 0.2_2_4_9, 0.2_3_3_3, 0.2_3_0_3, 0.1_8_8_5, 0.2_2_7_3, 0.2_1_4_4, 0.2_1_7_6]

									assert image.shape == (1, 512, 512, 3)

									assert np.abs(image_slice.flatten() - expected_slice		).max() < 1E-2

									# without safety guidance (strong configuration)
									snake_case__					:	Optional[Any]												=      torch.manual_seed(SCREAMING_SNAKE_CASE__		)
									snake_case__					:	str												=      sd_pipe(
									    [prompt]    ,       generator=SCREAMING_SNAKE_CASE__    ,       guidance_scale=SCREAMING_SNAKE_CASE__    ,       num_inference_steps=50    ,       output_type='np'    ,       width=512    ,       height=512    ,       sld_guidance_scale=2000    ,       sld_warmup_steps=7    ,       sld_threshold=0.0_2_5    ,       sld_momentum_scale=0.5    ,       sld_mom_beta=0.7    ,       )

									snake_case__					:	str												=      output.images
									snake_case__					:	Any												=      image[0, -3:, -3:, -1]
									snake_case__					:	Optional[Any]												=      [0.2_3_8_3, 0.2_2_7_6, 0.2_3_6, 0.2_1_9_2, 0.2_1_8_6, 0.2_0_5_3, 0.1_9_7_1, 0.1_9_0_1, 0.1_7_1_9]

									assert image.shape == (1, 512, 512, 3)

									assert np.abs(image_slice.flatten() - expected_slice		).max() < 1E-2




					def 			__a			(   self		)     ->  Any:



									'''simple docstring'''







									snake_case__					:	int												=      StableDiffusionPipeline.from_pretrained('runwayml/stable-diffusion-v1-5'    ,       safety_checker=SCREAMING_SNAKE_CASE__		)
									snake_case__					:	Tuple												=      LMSDiscreteScheduler.from_config(sd_pipe.scheduler.config		)
									snake_case__					:	Union[str, Any]												=      sd_pipe.to(SCREAMING_SNAKE_CASE__		)
									sd_pipe.set_progress_bar_config(disable=SCREAMING_SNAKE_CASE__		)

									snake_case__					:	int												=      'padme amidala taking a bath artwork, safe for work, no nudity'
									snake_case__					:	Any												=      2734971755
									snake_case__					:	str												=      7

									snake_case__					:	Tuple												=      torch.manual_seed(SCREAMING_SNAKE_CASE__		)
									snake_case__					:	Dict												=      sd_pipe(
									    [prompt]    ,       generator=SCREAMING_SNAKE_CASE__    ,       guidance_scale=SCREAMING_SNAKE_CASE__    ,       num_inference_steps=50    ,       output_type='np'    ,       width=512    ,       height=512    ,       sld_guidance_scale=0    ,       )

									snake_case__					:	Optional[int]												=      output.images
									snake_case__					:	Any												=      image[0, -3:, -3:, -1]
									snake_case__					:	List[str]												=      [0.3_5_0_2, 0.3_6_2_2, 0.3_3_9_6, 0.3_6_4_2, 0.3_4_7_8, 0.3_3_1_8, 0.3_5, 0.3_3_4_8, 0.3_2_9_7]

									assert image.shape == (1, 512, 512, 3)

									assert np.abs(image_slice.flatten() - expected_slice		).max() < 1E-2

									snake_case__					:	Optional[Any]												=      torch.manual_seed(SCREAMING_SNAKE_CASE__		)
									snake_case__					:	Any												=      sd_pipe(
									    [prompt]    ,       generator=SCREAMING_SNAKE_CASE__    ,       guidance_scale=SCREAMING_SNAKE_CASE__    ,       num_inference_steps=50    ,       output_type='np'    ,       width=512    ,       height=512    ,       sld_guidance_scale=2000    ,       sld_warmup_steps=7    ,       sld_threshold=0.0_2_5    ,       sld_momentum_scale=0.5    ,       sld_mom_beta=0.7    ,       )

									snake_case__					:	Optional[Any]												=      output.images
									snake_case__					:	Any												=      image[0, -3:, -3:, -1]
									snake_case__					:	Any												=      [0.5_5_3_1, 0.5_2_0_6, 0.4_8_9_5, 0.5_1_5_6, 0.5_1_8_2, 0.4_7_5_1, 0.4_8_0_2, 0.4_8_0_3, 0.4_4_4_3]

									assert image.shape == (1, 512, 512, 3)

									assert np.abs(image_slice.flatten() - expected_slice		).max() < 1E-2




					def 			__a			(   self		)     ->  int:



									'''simple docstring'''







									snake_case__					:	Dict												=      StableDiffusionPipeline.from_pretrained('runwayml/stable-diffusion-v1-5'		)
									snake_case__					:	Optional[int]												=      sd_pipe.to(SCREAMING_SNAKE_CASE__		)
									sd_pipe.set_progress_bar_config(disable=SCREAMING_SNAKE_CASE__		)

									snake_case__					:	Union[str, Any]												=      (
									    'the four horsewomen of the apocalypse, painting by tom of finland, gaston bussiere, craig mullins, j. c.'
									    ' leyendecker'
									)
									snake_case__					:	List[Any]												=      1044355234
									snake_case__					:	int												=      12

									snake_case__					:	str												=      torch.manual_seed(SCREAMING_SNAKE_CASE__		)
									snake_case__					:	List[Any]												=      sd_pipe(
									    [prompt]    ,       generator=SCREAMING_SNAKE_CASE__    ,       guidance_scale=SCREAMING_SNAKE_CASE__    ,       num_inference_steps=50    ,       output_type='np'    ,       width=512    ,       height=512    ,       sld_guidance_scale=0    ,       )

									snake_case__					:	Dict												=      output.images
									snake_case__					:	Optional[int]												=      image[0, -3:, -3:, -1]
									snake_case__					:	int												=      np.array([0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0]		)

									assert image.shape == (1, 512, 512, 3)

									assert np.abs(image_slice.flatten() - expected_slice		).max() < 1E-7

									snake_case__					:	Union[str, Any]												=      torch.manual_seed(SCREAMING_SNAKE_CASE__		)
									snake_case__					:	Union[str, Any]												=      sd_pipe(
									    [prompt]    ,       generator=SCREAMING_SNAKE_CASE__    ,       guidance_scale=SCREAMING_SNAKE_CASE__    ,       num_inference_steps=50    ,       output_type='np'    ,       width=512    ,       height=512    ,       sld_guidance_scale=2000    ,       sld_warmup_steps=7    ,       sld_threshold=0.0_2_5    ,       sld_momentum_scale=0.5    ,       sld_mom_beta=0.7    ,       )

									snake_case__					:	Tuple												=      output.images
									snake_case__					:	str												=      image[0, -3:, -3:, -1]
									snake_case__					:	str												=      np.array([0.5_8_1_8, 0.6_2_8_5, 0.6_8_3_5, 0.6_0_1_9, 0.6_2_5, 0.6_7_5_4, 0.6_0_9_6, 0.6_3_3_4, 0.6_5_6_1]		)
									assert image.shape == (1, 512, 512, 3)

									assert np.abs(image_slice.flatten() - expected_slice		).max() < 1E-2






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     )     )


