

import gc
import unittest

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

from diffusers import (
    AutoencoderKL,
    DDIMScheduler,
    EulerAncestralDiscreteScheduler,
    LMSDiscreteScheduler,
    PNDMScheduler,
    StableDiffusionPanoramaPipeline,
    UNetaDConditionModel,
)
from diffusers.utils import slow, torch_device
from diffusers.utils.testing_utils import enable_full_determinism, require_torch_gpu, skip_mps

from ..pipeline_params import TEXT_TO_IMAGE_BATCH_PARAMS, TEXT_TO_IMAGE_IMAGE_PARAMS, TEXT_TO_IMAGE_PARAMS
from ..test_pipelines_common import PipelineLatentTesterMixin, PipelineTesterMixin


enable_full_determinism()





@skip_mps
class __SCREAMING_SNAKE_CASE     (	_UpperCamelCase			,						_UpperCamelCase			,						unittest.TestCase		):
    SCREAMING_SNAKE_CASE__      =StableDiffusionPanoramaPipeline
    SCREAMING_SNAKE_CASE__      =TEXT_TO_IMAGE_PARAMS
    SCREAMING_SNAKE_CASE__      =TEXT_TO_IMAGE_BATCH_PARAMS
    SCREAMING_SNAKE_CASE__      =TEXT_TO_IMAGE_IMAGE_PARAMS
    SCREAMING_SNAKE_CASE__      =TEXT_TO_IMAGE_IMAGE_PARAMS






    def 						__lowerCAmelCase    (       self )   ->					List[Any]:
       torch.manual_seed(0 )
       __SCREAMING_SNAKE_CASE       =				UNetaDConditionModel(
           block_out_channels=(32, 64),		layers_per_block=1,		sample_size=32,		in_channels=4,		out_channels=4,		down_block_types=("DownBlock2D", "CrossAttnDownBlock2D"),		up_block_types=("CrossAttnUpBlock2D", "UpBlock2D"),		cross_attention_dim=32,		)
       __SCREAMING_SNAKE_CASE       =				DDIMScheduler()
       torch.manual_seed(0 )
       __SCREAMING_SNAKE_CASE       =				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,		)
       torch.manual_seed(0 )
       __SCREAMING_SNAKE_CASE       =				CLIPTextConfig(
           bos_token_id=0,		eos_token_id=2,		hidden_size=32,		intermediate_size=37,		layer_norm_eps=1E-0_5,		num_attention_heads=4,		num_hidden_layers=5,		pad_token_id=1,		vocab_size=10_00,		)
       __SCREAMING_SNAKE_CASE       =				CLIPTextModel(lowercase__ )
       __SCREAMING_SNAKE_CASE       =				CLIPTokenizer.from_pretrained("hf-internal-testing/tiny-random-clip" )

       __SCREAMING_SNAKE_CASE       =				{
           '''unet''': unet,
           '''scheduler''': scheduler,
           '''vae''': vae,
           '''text_encoder''': text_encoder,
           '''tokenizer''': tokenizer,
           '''safety_checker''': None,
           '''feature_extractor''': None,
       }
       return components






    def 						__lowerCAmelCase    (       self,		_a,		_a=0 )   ->					Dict:
       __SCREAMING_SNAKE_CASE       =				torch.manual_seed(lowercase__ )
       __SCREAMING_SNAKE_CASE       =				{
           '''prompt''': '''a photo of the dolomites''',
           '''generator''': generator,
           # Setting height and width to None to prevent OOMs on CPU.
           '''height''': None,
           '''width''': None,
           '''num_inference_steps''': 1,
           '''guidance_scale''': 6.0,
           '''output_type''': '''numpy''',
       }
       return inputs






    def 						__lowerCAmelCase    (       self )   ->					Any:
       __SCREAMING_SNAKE_CASE       =				'''cpu'''  # ensure determinism for the device-dependent torch.Generator
       __SCREAMING_SNAKE_CASE       =				self.get_dummy_components()
       __SCREAMING_SNAKE_CASE       =				StableDiffusionPanoramaPipeline(**lowercase__ )
       __SCREAMING_SNAKE_CASE       =				sd_pipe.to(lowercase__ )
       sd_pipe.set_progress_bar_config(disable=lowercase__ )

       __SCREAMING_SNAKE_CASE       =				self.get_dummy_inputs(lowercase__ )
       __SCREAMING_SNAKE_CASE       =				sd_pipe(**lowercase__ ).images
       __SCREAMING_SNAKE_CASE       =				image[0, -3:, -3:, -1]
       assert image.shape == (1, 64, 64, 3)

       __SCREAMING_SNAKE_CASE       =				np.array([0.6186, 0.5374, 0.4915, 0.4135, 0.4114, 0.4563, 0.5128, 0.4977, 0.4757] )

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






    def 						__lowerCAmelCase    (       self )   ->					Union[str, Any]:
       super().test_inference_batch_consistent(batch_sizes=[1, 2] )






    def 						__lowerCAmelCase    (       self )   ->					Optional[Any]:
       super().test_inference_batch_single_identical(batch_size=2,		expected_max_diff=3.2_5E-3 )






    def 						__lowerCAmelCase    (       self )   ->					List[Any]:
       __SCREAMING_SNAKE_CASE       =				'''cpu'''  # ensure determinism for the device-dependent torch.Generator
       __SCREAMING_SNAKE_CASE       =				self.get_dummy_components()
       __SCREAMING_SNAKE_CASE       =				StableDiffusionPanoramaPipeline(**lowercase__ )
       __SCREAMING_SNAKE_CASE       =				sd_pipe.to(lowercase__ )
       sd_pipe.set_progress_bar_config(disable=lowercase__ )

       __SCREAMING_SNAKE_CASE       =				self.get_dummy_inputs(lowercase__ )
       __SCREAMING_SNAKE_CASE       =				'''french fries'''
       __SCREAMING_SNAKE_CASE       =				sd_pipe(**lowercase__,		negative_prompt=lowercase__ )
       __SCREAMING_SNAKE_CASE       =				output.images
       __SCREAMING_SNAKE_CASE       =				image[0, -3:, -3:, -1]

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

       __SCREAMING_SNAKE_CASE       =				np.array([0.6187, 0.5375, 0.4915, 0.4136, 0.4114, 0.4563, 0.5128, 0.4976, 0.4757] )

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






    def 						__lowerCAmelCase    (       self )   ->					Optional[int]:
       __SCREAMING_SNAKE_CASE       =				'''cpu'''  # ensure determinism for the device-dependent torch.Generator
       __SCREAMING_SNAKE_CASE       =				self.get_dummy_components()
       __SCREAMING_SNAKE_CASE       =				StableDiffusionPanoramaPipeline(**lowercase__ )
       __SCREAMING_SNAKE_CASE       =				sd_pipe.to(lowercase__ )
       sd_pipe.set_progress_bar_config(disable=lowercase__ )

       __SCREAMING_SNAKE_CASE       =				self.get_dummy_inputs(lowercase__ )
       __SCREAMING_SNAKE_CASE       =				sd_pipe(**lowercase__,		view_batch_size=2 )
       __SCREAMING_SNAKE_CASE       =				output.images
       __SCREAMING_SNAKE_CASE       =				image[0, -3:, -3:, -1]

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

       __SCREAMING_SNAKE_CASE       =				np.array([0.6187, 0.5375, 0.4915, 0.4136, 0.4114, 0.4563, 0.5128, 0.4976, 0.4757] )

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






    def 						__lowerCAmelCase    (       self )   ->					Dict:
       __SCREAMING_SNAKE_CASE       =				'''cpu'''  # ensure determinism for the device-dependent torch.Generator
       __SCREAMING_SNAKE_CASE       =				self.get_dummy_components()
       __SCREAMING_SNAKE_CASE       =				EulerAncestralDiscreteScheduler(
           beta_start=0.0_0085,		beta_end=0.012,		beta_schedule="scaled_linear" )
       __SCREAMING_SNAKE_CASE       =				StableDiffusionPanoramaPipeline(**lowercase__ )
       __SCREAMING_SNAKE_CASE       =				sd_pipe.to(lowercase__ )
       sd_pipe.set_progress_bar_config(disable=lowercase__ )

       __SCREAMING_SNAKE_CASE       =				self.get_dummy_inputs(lowercase__ )
       __SCREAMING_SNAKE_CASE       =				sd_pipe(**lowercase__ ).images
       __SCREAMING_SNAKE_CASE       =				image[0, -3:, -3:, -1]

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

       __SCREAMING_SNAKE_CASE       =				np.array([0.4024, 0.6510, 0.4901, 0.5378, 0.5813, 0.5622, 0.4795, 0.4467, 0.4952] )

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






    def 						__lowerCAmelCase    (       self )   ->					List[str]:
       __SCREAMING_SNAKE_CASE       =				'''cpu'''  # ensure determinism for the device-dependent torch.Generator
       __SCREAMING_SNAKE_CASE       =				self.get_dummy_components()
       __SCREAMING_SNAKE_CASE       =				PNDMScheduler(
           beta_start=0.0_0085,		beta_end=0.012,		beta_schedule="scaled_linear",		skip_prk_steps=lowercase__ )
       __SCREAMING_SNAKE_CASE       =				StableDiffusionPanoramaPipeline(**lowercase__ )
       __SCREAMING_SNAKE_CASE       =				sd_pipe.to(lowercase__ )
       sd_pipe.set_progress_bar_config(disable=lowercase__ )

       __SCREAMING_SNAKE_CASE       =				self.get_dummy_inputs(lowercase__ )
       __SCREAMING_SNAKE_CASE       =				sd_pipe(**lowercase__ ).images
       __SCREAMING_SNAKE_CASE       =				image[0, -3:, -3:, -1]

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

       __SCREAMING_SNAKE_CASE       =				np.array([0.6391, 0.6291, 0.4861, 0.5134, 0.5552, 0.4578, 0.5032, 0.5023, 0.4539] )

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






@slow
@require_torch_gpu
class __SCREAMING_SNAKE_CASE     (	unittest.TestCase		):






    def 						__lowerCAmelCase    (       self )   ->					Optional[int]:
       super().tearDown()
       gc.collect()
       torch.cuda.empty_cache()






    def 						__lowerCAmelCase    (       self,		_a=0 )   ->					List[str]:
       __SCREAMING_SNAKE_CASE       =				torch.manual_seed(lowercase__ )
       __SCREAMING_SNAKE_CASE       =				{
           '''prompt''': '''a photo of the dolomites''',
           '''generator''': generator,
           '''num_inference_steps''': 3,
           '''guidance_scale''': 7.5,
           '''output_type''': '''numpy''',
       }
       return inputs






    def 						__lowerCAmelCase    (       self )   ->					str:
       __SCREAMING_SNAKE_CASE       =				'''stabilityai/stable-diffusion-2-base'''
       __SCREAMING_SNAKE_CASE       =				DDIMScheduler.from_pretrained(lowercase__,		subfolder="scheduler" )
       __SCREAMING_SNAKE_CASE       =				StableDiffusionPanoramaPipeline.from_pretrained(lowercase__,		scheduler=lowercase__,		safety_checker=lowercase__ )
       pipe.to(lowercase__ )
       pipe.set_progress_bar_config(disable=lowercase__ )
       pipe.enable_attention_slicing()

       __SCREAMING_SNAKE_CASE       =				self.get_inputs()
       __SCREAMING_SNAKE_CASE       =				pipe(**lowercase__ ).images
       __SCREAMING_SNAKE_CASE       =				image[0, -3:, -3:, -1].flatten()

       assert image.shape == (1, 5_12, 20_48, 3)

       __SCREAMING_SNAKE_CASE       =				np.array(
           [
               0.3696_8392,
               0.2702_5372,
               0.3244_6766,
               0.2837_9387,
               0.3636_3274,
               0.3073_3347,
               0.2710_0027,
               0.2705_4125,
               0.2553_6096,
           ] )

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






    def 						__lowerCAmelCase    (       self )   ->					Optional[Any]:
       __SCREAMING_SNAKE_CASE       =				StableDiffusionPanoramaPipeline.from_pretrained(
           "stabilityai/stable-diffusion-2-base",		safety_checker=lowercase__ )
       __SCREAMING_SNAKE_CASE       =				LMSDiscreteScheduler.from_config(pipe.scheduler.config )
       pipe.to(lowercase__ )
       pipe.set_progress_bar_config(disable=lowercase__ )
       pipe.enable_attention_slicing()

       __SCREAMING_SNAKE_CASE       =				self.get_inputs()
       __SCREAMING_SNAKE_CASE       =				pipe(**lowercase__ ).images
       __SCREAMING_SNAKE_CASE       =				image[0, -3:, -3:, -1].flatten()

       assert image.shape == (1, 5_12, 20_48, 3)

       __SCREAMING_SNAKE_CASE       =				np.array(
           [
               [
                   0.0,
                   0.0,
                   0.0,
                   0.0,
                   0.0,
                   0.0,
                   0.0,
                   0.0,
                   0.0,
               ]
           ] )

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






    def 						__lowerCAmelCase    (       self )   ->					Any:
       __SCREAMING_SNAKE_CASE       =				0

       def callback_fn(_a,		_a,		_a ) -> None:
          __SCREAMING_SNAKE_CASE       =				True
          nonlocal number_of_steps
          number_of_steps += 1
          if step == 1:
             __SCREAMING_SNAKE_CASE       =				latents.detach().cpu().numpy()
             assert latents.shape == (1, 4, 64, 2_56)
             __SCREAMING_SNAKE_CASE       =				latents[0, -3:, -3:, -1]

             __SCREAMING_SNAKE_CASE       =				np.array(
                 [
                     0.1868_1869,
                     0.3390_7816,
                     0.536_1276,
                     0.1443_2865,
                     -0.0285_6611,
                     -0.7394_1123,
                     0.2339_7987,
                     0.4732_2682,
                     -0.3782_3164,
                 ] )
             assert np.abs(latents_slice.flatten() - expected_slice ).max() < 5E-2
          elif step == 2:
             __SCREAMING_SNAKE_CASE       =				latents.detach().cpu().numpy()
             assert latents.shape == (1, 4, 64, 2_56)
             __SCREAMING_SNAKE_CASE       =				latents[0, -3:, -3:, -1]

             __SCREAMING_SNAKE_CASE       =				np.array(
                 [
                     0.1853_9645,
                     0.3398_7248,
                     0.537_8559,
                     0.1443_7142,
                     -0.0245_5261,
                     -0.733_8317,
                     0.2399_0755,
                     0.4735_6272,
                     -0.378_6505,
                 ] )

             assert np.abs(latents_slice.flatten() - expected_slice ).max() < 5E-2

       __SCREAMING_SNAKE_CASE       =				False

       __SCREAMING_SNAKE_CASE       =				'''stabilityai/stable-diffusion-2-base'''
       __SCREAMING_SNAKE_CASE       =				DDIMScheduler.from_pretrained(lowercase__,		subfolder="scheduler" )
       __SCREAMING_SNAKE_CASE       =				StableDiffusionPanoramaPipeline.from_pretrained(lowercase__,		scheduler=lowercase__,		safety_checker=lowercase__ )
       __SCREAMING_SNAKE_CASE       =				pipe.to(lowercase__ )
       pipe.set_progress_bar_config(disable=lowercase__ )
       pipe.enable_attention_slicing()

       __SCREAMING_SNAKE_CASE       =				self.get_inputs()
       pipe(**lowercase__,		callback=lowercase__,		callback_steps=1 )
       assert callback_fn.has_been_called
       assert number_of_steps == 3






    def 						__lowerCAmelCase    (       self )   ->					Optional[Any]:
       torch.cuda.empty_cache()
       torch.cuda.reset_max_memory_allocated()
       torch.cuda.reset_peak_memory_stats()

       __SCREAMING_SNAKE_CASE       =				'''stabilityai/stable-diffusion-2-base'''
       __SCREAMING_SNAKE_CASE       =				DDIMScheduler.from_pretrained(lowercase__,		subfolder="scheduler" )
       __SCREAMING_SNAKE_CASE       =				StableDiffusionPanoramaPipeline.from_pretrained(lowercase__,		scheduler=lowercase__,		safety_checker=lowercase__ )
       __SCREAMING_SNAKE_CASE       =				pipe.to(lowercase__ )
       pipe.set_progress_bar_config(disable=lowercase__ )
       pipe.enable_attention_slicing(1 )
       pipe.enable_sequential_cpu_offload()

       __SCREAMING_SNAKE_CASE       =				self.get_inputs()
       __SCREAMING_SNAKE_CASE       =				pipe(**lowercase__ )

       __SCREAMING_SNAKE_CASE       =				torch.cuda.max_memory_allocated()
       # make sure that less than 5.2 GB is allocated
       assert mem_bytes < 5.5 * 10**9





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 							lowerCamelCase   :


	def __init__(    self				,     lowercase__				,     lowercase__=1_3				,     lowercase__=7				,     lowercase__=True				,     lowercase__=True				,     lowercase__=True				,     lowercase__=True				,     lowercase__=9_9				,     lowercase__=2_4				,     lowercase__=2				,     lowercase__=6				,     lowercase__=3_7				,     lowercase__="gelu"				,     lowercase__=0.1				,     lowercase__=0.1				,     lowercase__=5_1_2				,     lowercase__=1_6				,     lowercase__=2				,     lowercase__=0.0_2				,     lowercase__=3				,     lowercase__=None				,     lowercase__=1_0_0_0				,     ):
							__UpperCAmelCase			:							int	    =	parent
							__UpperCAmelCase			:							Optional[int]	    =	batch_size
							__UpperCAmelCase			:							Any	    =	seq_length
							__UpperCAmelCase			:							Dict	    =	is_training
							__UpperCAmelCase			:							str	    =	use_input_mask
							__UpperCAmelCase			:							Dict	    =	use_token_type_ids
							__UpperCAmelCase			:							Tuple	    =	use_labels
							__UpperCAmelCase			:							Union[str, Any]	    =	vocab_size
							__UpperCAmelCase			:							Tuple	    =	hidden_size
							__UpperCAmelCase			:							Dict	    =	num_hidden_layers
							__UpperCAmelCase			:							Optional[Any]	    =	num_attention_heads
							__UpperCAmelCase			:							Union[str, Any]	    =	intermediate_size
							__UpperCAmelCase			:							Optional[int]	    =	hidden_act
							__UpperCAmelCase			:							int	    =	hidden_dropout_prob
							__UpperCAmelCase			:							List[Any]	    =	attention_probs_dropout_prob
							__UpperCAmelCase			:							List[Any]	    =	max_position_embeddings
							__UpperCAmelCase			:							Optional[Any]	    =	type_vocab_size
							__UpperCAmelCase			:							Optional[Any]	    =	type_sequence_label_size
							__UpperCAmelCase			:							str	    =	initializer_range
							__UpperCAmelCase			:							str	    =	num_labels
							__UpperCAmelCase			:							str	    =	scope
							__UpperCAmelCase			:							Union[str, Any]	    =	range_bbox


	def  A(    self):
							__UpperCAmelCase			:							Union[str, Any]	    =	ids_tensor([self.batch_size, self.seq_length]				,     self.vocab_size)

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

							__UpperCAmelCase			:							Tuple	    =	None
							if self.use_input_mask:
													__UpperCAmelCase			:							Any	    =	ids_tensor([self.batch_size, self.seq_length]				,     vocab_size=2)

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

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

							__UpperCAmelCase			:							int	    =	self.get_config()

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


	def  A(    self):
							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  A(    self				,     lowercase__				,     lowercase__				,     lowercase__				,     lowercase__				,     lowercase__				,     lowercase__				,     lowercase__				,     ):
							__UpperCAmelCase			:							List[Any]	    =	LiltModel(config=lowercase__)
							model.to(lowercase__)
							model.eval()
							__UpperCAmelCase			:							List[Any]	    =	model(lowercase__				,     bbox=lowercase__				,     attention_mask=lowercase__				,     token_type_ids=lowercase__)
							__UpperCAmelCase			:							Optional[int]	    =	model(lowercase__				,     bbox=lowercase__				,     token_type_ids=lowercase__)
							__UpperCAmelCase			:							Optional[int]	    =	model(lowercase__				,     bbox=lowercase__)

							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  A(    self				,     lowercase__				,     lowercase__				,     lowercase__				,     lowercase__				,     lowercase__				,     lowercase__				,     lowercase__				,     ):
							__UpperCAmelCase			:							Optional[int]	    =	self.num_labels
							__UpperCAmelCase			:							Dict	    =	LiltForTokenClassification(config=lowercase__)
							model.to(lowercase__)
							model.eval()
							__UpperCAmelCase			:							Dict	    =	model(
							    lowercase__				,     bbox=lowercase__				,     attention_mask=lowercase__				,     token_type_ids=lowercase__				,     labels=lowercase__)
							self.parent.assertEqual(result.logits.shape				,     (self.batch_size, self.seq_length, self.num_labels))


	def  A(    self				,     lowercase__				,     lowercase__				,     lowercase__				,     lowercase__				,     lowercase__				,     lowercase__				,     lowercase__				,     ):
							__UpperCAmelCase			:							Dict	    =	LiltForQuestionAnswering(config=lowercase__)
							model.to(lowercase__)
							model.eval()
							__UpperCAmelCase			:							List[Any]	    =	model(
							    lowercase__				,     bbox=lowercase__				,     attention_mask=lowercase__				,     token_type_ids=lowercase__				,     start_positions=lowercase__				,     end_positions=lowercase__				,     )
							self.parent.assertEqual(result.start_logits.shape				,     (self.batch_size, self.seq_length))
							self.parent.assertEqual(result.end_logits.shape				,     (self.batch_size, self.seq_length))


	def  A(    self):
							__UpperCAmelCase			:							Union[str, Any]	    =	self.prepare_config_and_inputs()
							(
							    (
							    __UpperCAmelCase
							)    ,      (
							    __UpperCAmelCase
							)    ,      (
							    __UpperCAmelCase
							)    ,      (
							    __UpperCAmelCase
							)    ,      (
							    __UpperCAmelCase
							)    ,      (
							    __UpperCAmelCase
							)    ,      (
							    __UpperCAmelCase
							)    ,      
							)			:							Any	    =	config_and_inputs
							__UpperCAmelCase			:							int	    =	{
							    '''input_ids''': input_ids,
							    '''bbox''': bbox,
							    '''token_type_ids''': token_type_ids,
							    '''attention_mask''': input_mask,
							}
							return config, inputs_dict






@require_torch
class 							lowerCamelCase   (  _UpperCamelCase							,						_UpperCamelCase							,						_UpperCamelCase							,						unittest.TestCase ):
	_lowerCAmelCase     :		str										=						(
	    (
	        LiltModel,
	        LiltForSequenceClassification,
	        LiltForTokenClassification,
	        LiltForQuestionAnswering,
	    )
	    if is_torch_available()
	    else ()
	)
	_lowerCAmelCase     :		str										=						(
	    {
	        '''feature-extraction''': LiltModel,
	        '''question-answering''': LiltForQuestionAnswering,
	        '''text-classification''': LiltForSequenceClassification,
	        '''token-classification''': LiltForTokenClassification,
	        '''zero-shot''': LiltForSequenceClassification,
	    }
	    if is_torch_available()
	    else {}
	)
	_lowerCAmelCase     :		Any										=						False
	_lowerCAmelCase     :		List[Any]										=						False


	def  A(    self				,     lowercase__				,     lowercase__				,     lowercase__				,     lowercase__				,     lowercase__):
							return True


	def  A(    self):
							__UpperCAmelCase			:							Tuple	    =	LiltModelTester(self)
							__UpperCAmelCase			:							List[Any]	    =	ConfigTester(self				,     config_class=lowercase__				,     hidden_size=3_7)


	def  A(    self):
							self.config_tester.run_common_tests()


	def  A(    self):
							__UpperCAmelCase			:							Union[str, Any]	    =	self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_model(*lowercase__)


	def  A(    self):
							__UpperCAmelCase			:							int	    =	self.model_tester.prepare_config_and_inputs()
							for type in ["absolute", "relative_key", "relative_key_query"]:
													__UpperCAmelCase			:							Any	    =	type
													self.model_tester.create_and_check_model(*lowercase__)


	def  A(    self):
							__UpperCAmelCase			:							Dict	    =	self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_for_token_classification(*lowercase__)


	def  A(    self):
							__UpperCAmelCase			:							Optional[int]	    =	self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_for_question_answering(*lowercase__)


	@slow
	def  A(    self):
							for model_name in LILT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
													__UpperCAmelCase			:							Optional[Any]	    =	LiltModel.from_pretrained(lowercase__)
													self.assertIsNotNone(lowercase__)






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


	def  A(    self):
							__UpperCAmelCase			:							Any	    =	LiltModel.from_pretrained('''SCUT-DLVCLab/lilt-roberta-en-base''').to(lowercase__)

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

							# forward pass
							with torch.no_grad():
													__UpperCAmelCase			:							Dict	    =	model(input_ids=lowercase__				,     bbox=lowercase__)

							__UpperCAmelCase			:							List[Any]	    =	torch.Size([1, 2, 7_6_8])
							__UpperCAmelCase			:							Union[str, Any]	    =	torch.tensor(
							    [[-0.0_6_5_3, 0.0_9_5_0, -0.0_0_6_1], [-0.0_5_4_5, 0.0_9_2_6, -0.0_3_2_4]]				,     device=lowercase__				,     )

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

