



"""simple docstring"""




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



	def __init__(       self			,	UpperCAmelCase__			,	UpperCAmelCase__=13			,	UpperCAmelCase__=7			,	UpperCAmelCase__=True			,	UpperCAmelCase__=True			,	UpperCAmelCase__=True			,	UpperCAmelCase__=True			,	UpperCAmelCase__=99			,	UpperCAmelCase__=24			,	UpperCAmelCase__=2			,	UpperCAmelCase__=6			,	UpperCAmelCase__=37			,	UpperCAmelCase__="gelu"			,	UpperCAmelCase__=0.1			,	UpperCAmelCase__=0.1			,	UpperCAmelCase__=512			,	UpperCAmelCase__=16			,	UpperCAmelCase__=2			,	UpperCAmelCase__=0.02			,	UpperCAmelCase__=3			,	UpperCAmelCase__=None			,	UpperCAmelCase__=1_000			,	):
							A__							=       parent
							A__							=       batch_size
							A__							=       seq_length
							A__							=       is_training
							A__							=       use_input_mask
							A__							=       use_token_type_ids
							A__							=       use_labels
							A__							=       vocab_size
							A__							=       hidden_size
							A__							=       num_hidden_layers
							A__							=       num_attention_heads
							A__							=       intermediate_size
							A__							=       hidden_act
							A__							=       hidden_dropout_prob
							A__							=       attention_probs_dropout_prob
							A__							=       max_position_embeddings
							A__							=       type_vocab_size
							A__							=       type_sequence_label_size
							A__							=       initializer_range
							A__							=       num_labels
							A__							=       scope
							A__							=       range_bbox



	def 			__A							(       self		):
							A__							=       ids_tensor([self.batch_size, self.seq_length]			,	self.vocab_size		)

							A__							=       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__							=       bbox[i, j, 3]
																									A__							=       bbox[i, j, 1]
																									A__							=       t
																			if bbox[i, j, 2] < bbox[i, j, 0]:
																									A__							=       bbox[i, j, 2]
																									A__							=       bbox[i, j, 0]
																									A__							=       t

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

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

							A__							=       None
							A__							=       None
							if self.use_labels:
													A__							=       ids_tensor([self.batch_size]			,	self.type_sequence_label_size		)
													A__							=       ids_tensor([self.batch_size, self.seq_length]			,	self.num_labels		)

							A__							=       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			,	UpperCAmelCase__			,	UpperCAmelCase__			,	UpperCAmelCase__			,	UpperCAmelCase__			,	UpperCAmelCase__			,	UpperCAmelCase__			,	UpperCAmelCase__			,	):
							A__							=       LiltModel(config=UpperCAmelCase__		)
							model.to(UpperCAmelCase__		)
							model.eval()
							A__							=       model(UpperCAmelCase__			,	bbox=UpperCAmelCase__			,	attention_mask=UpperCAmelCase__			,	token_type_ids=UpperCAmelCase__		)
							A__							=       model(UpperCAmelCase__			,	bbox=UpperCAmelCase__			,	token_type_ids=UpperCAmelCase__		)
							A__							=       model(UpperCAmelCase__			,	bbox=UpperCAmelCase__		)

							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			,	UpperCAmelCase__			,	UpperCAmelCase__			,	UpperCAmelCase__			,	UpperCAmelCase__			,	UpperCAmelCase__			,	UpperCAmelCase__			,	UpperCAmelCase__			,	):
							A__							=       self.num_labels
							A__							=       LiltForTokenClassification(config=UpperCAmelCase__		)
							model.to(UpperCAmelCase__		)
							model.eval()
							A__							=       model(
							    UpperCAmelCase__			,	bbox=UpperCAmelCase__			,	attention_mask=UpperCAmelCase__			,	token_type_ids=UpperCAmelCase__			,	labels=UpperCAmelCase__		)
							self.parent.assertEqual(result.logits.shape			,	(self.batch_size, self.seq_length, self.num_labels)		)



	def 			__A							(       self			,	UpperCAmelCase__			,	UpperCAmelCase__			,	UpperCAmelCase__			,	UpperCAmelCase__			,	UpperCAmelCase__			,	UpperCAmelCase__			,	UpperCAmelCase__			,	):
							A__							=       LiltForQuestionAnswering(config=UpperCAmelCase__		)
							model.to(UpperCAmelCase__		)
							model.eval()
							A__							=       model(
							    UpperCAmelCase__			,	bbox=UpperCAmelCase__			,	attention_mask=UpperCAmelCase__			,	token_type_ids=UpperCAmelCase__			,	start_positions=UpperCAmelCase__			,	end_positions=UpperCAmelCase__			,	)
							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		):
							A__							=       self.prepare_config_and_inputs()
							(
							    (
							    A__   
							)						,       (
							    A__   
							)						,       (
							    A__   
							)						,       (
							    A__   
							)						,       (
							    A__   
							)						,       (
							    A__   
							)						,       (
							    A__   
							)						,          
							)							=       config_and_inputs
							A__							=       {
							    "input_ids": input_ids,
							    "bbox": bbox,
							    "token_type_ids": token_type_ids,
							    "attention_mask": input_mask,
							}
							return config, inputs_dict






@require_torch
class      UpperCamelCase    (						snake_case_       ,    snake_case_       ,    snake_case_       ,    unittest.TestCase     ):
	lowerCAmelCase	:   str				=	(
	    (
	        LiltModel,
	        LiltForSequenceClassification,
	        LiltForTokenClassification,
	        LiltForQuestionAnswering,
	    )
	    if is_torch_available()
	    else ()
	)
	lowerCAmelCase	:   Any				=	(
	    {
	        """feature-extraction""": LiltModel,
	        """question-answering""": LiltForQuestionAnswering,
	        """text-classification""": LiltForSequenceClassification,
	        """token-classification""": LiltForTokenClassification,
	        """zero-shot""": LiltForSequenceClassification,
	    }
	    if is_torch_available()
	    else {}
	)
	lowerCAmelCase	:   Optional[int]				=	False
	lowerCAmelCase	:   Optional[Any]				=	False



	def 			__A							(       self			,	UpperCAmelCase__			,	UpperCAmelCase__			,	UpperCAmelCase__			,	UpperCAmelCase__			,	UpperCAmelCase__		):
							return True



	def 			__A							(       self		):
							A__							=       LiltModelTester(self		)
							A__							=       ConfigTester(self			,	config_class=UpperCAmelCase__			,	hidden_size=37		)



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



	def 			__A							(       self		):
							A__							=       self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_model(*UpperCAmelCase__		)



	def 			__A							(       self		):
							A__							=       self.model_tester.prepare_config_and_inputs()
							for type in ["absolute", "relative_key", "relative_key_query"]:
													A__							=       type
													self.model_tester.create_and_check_model(*UpperCAmelCase__		)



	def 			__A							(       self		):
							A__							=       self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_for_token_classification(*UpperCAmelCase__		)



	def 			__A							(       self		):
							A__							=       self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_for_question_answering(*UpperCAmelCase__		)



	@slow
	def 			__A							(       self		):
							for model_name in LILT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
													A__							=       LiltModel.from_pretrained(UpperCAmelCase__		)
													self.assertIsNotNone(UpperCAmelCase__		)






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



	def 			__A							(       self		):
							A__							=       LiltModel.from_pretrained("SCUT-DLVCLab/lilt-roberta-en-base"		).to(UpperCAmelCase__		)

							A__							=       torch.tensor([[1, 2]]			,	device=UpperCAmelCase__		)
							A__							=       torch.tensor([[[1, 2, 3, 4], [5, 6, 7, 8]]]			,	device=UpperCAmelCase__		)

							# forward pass
							with torch.no_grad():
													A__							=       model(input_ids=UpperCAmelCase__			,	bbox=UpperCAmelCase__		)

							A__							=       torch.Size([1, 2, 768]		)
							A__							=       torch.tensor(
							    [[-0.0_653, 0.0_950, -0.0_061], [-0.0_545, 0.0_926, -0.0_324]]			,	device=UpperCAmelCase__			,	)

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










import gc
import unittest

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

from diffusers import (
    AutoencoderKL,
    DDIMScheduler,
    StableDiffusionAttendAndExcitePipeline,
    UNetaDConditionModel,
)
from diffusers.utils import load_numpy, skip_mps, slow
from diffusers.utils.testing_utils import require_torch_gpu

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


UpperCAmelCase_							:      Dict      =  False






@skip_mps
class      UpperCamelCase    (						_UpperCAmelCase       ,    _UpperCAmelCase       ,    _UpperCAmelCase       ,    unittest.TestCase     ):
	lowerCAmelCase	:   Optional[Any]				=	StableDiffusionAttendAndExcitePipeline
	lowerCAmelCase	:   Optional[Any]				=	False
	lowerCAmelCase	:   Any				=	TEXT_TO_IMAGE_PARAMS
	lowerCAmelCase	:   Dict				=	TEXT_TO_IMAGE_BATCH_PARAMS.union({"""token_indices"""}     )
	lowerCAmelCase	:   List[str]				=	TEXT_TO_IMAGE_IMAGE_PARAMS
	lowerCAmelCase	:   List[Any]				=	TEXT_TO_IMAGE_IMAGE_PARAMS



	@classmethod
	def 			__A							(       cls		):
							super().setUpClass()
							torch.use_deterministic_algorithms(UpperCAmelCase__		)



	@classmethod
	def 			__A							(       cls		):
							super().tearDownClass()
							torch.use_deterministic_algorithms(UpperCAmelCase__		)



	def 			__A							(       self		):
							torch.manual_seed(0		)
							A__							=       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			,	attention_head_dim=(2, 4)			,	use_linear_projection=UpperCAmelCase__			,	)
							A__							=       DDIMScheduler(
							    beta_start=0.00_085			,	beta_end=0.012			,	beta_schedule="scaled_linear"			,	clip_sample=UpperCAmelCase__			,	set_alpha_to_one=UpperCAmelCase__			,	)
							torch.manual_seed(0		)
							A__							=       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			,	sample_size=128			,	)
							torch.manual_seed(0		)
							A__							=       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=1_000			,	hidden_act="gelu"			,	projection_dim=512			,	)
							A__							=       CLIPTextModel(UpperCAmelCase__		)
							A__							=       CLIPTokenizer.from_pretrained("hf-internal-testing/tiny-random-clip"		)

							A__							=       {
							    "unet": unet,
							    "scheduler": scheduler,
							    "vae": vae,
							    "text_encoder": text_encoder,
							    "tokenizer": tokenizer,
							    "safety_checker": None,
							    "feature_extractor": None,
							}

							return components



	def 			__A							(       self			,	UpperCAmelCase__			,	UpperCAmelCase__=0		):
							if str(UpperCAmelCase__		).startswith("mps"		):
													A__							=       torch.manual_seed(UpperCAmelCase__		)
							else:
													A__							=       torch.Generator(device=UpperCAmelCase__		).manual_seed(UpperCAmelCase__		)
							A__							=       A__							=       {
							    "prompt": "a cat and a frog",
							    "token_indices": [2, 5],
							    "generator": generator,
							    "num_inference_steps": 1,
							    "guidance_scale": 6.0,
							    "output_type": "numpy",
							    "max_iter_to_alter": 2,
							    "thresholds": {0: 0.7},
							}
							return inputs



	def 			__A							(       self		):
							A__							=       "cpu"

							A__							=       self.get_dummy_components()
							A__							=       self.pipeline_class(**UpperCAmelCase__		)
							pipe.to(UpperCAmelCase__		)
							pipe.set_progress_bar_config(disable=UpperCAmelCase__		)

							A__							=       self.get_dummy_inputs(UpperCAmelCase__		)
							A__							=       pipe(**UpperCAmelCase__		).images
							A__							=       image[0, -3:, -3:, -1]

							self.assertEqual(image.shape			,	(1, 64, 64, 3)		)
							A__							=       np.array(
							    [0.63_905_364, 0.62_897_307, 0.48_599_017, 0.5_133_624, 0.5_550_048, 0.45_769_516, 0.50_326_973, 0.5_023_139, 0.45_384_496]		)
							A__							=       np.abs(image_slice.flatten() - expected_slice		).max()
							self.assertLessEqual(UpperCAmelCase__			,	1e-3		)



	def 			__A							(       self		):
							super().test_cpu_offload_forward_pass(expected_max_diff=5e-4		)



	def 			__A							(       self		):
							# NOTE: Larger batch sizes cause this test to timeout, only test on smaller batches
							self._test_inference_batch_consistent(batch_sizes=[1, 2]		)



	def 			__A							(       self		):
							self._test_inference_batch_single_identical(batch_size=2			,	expected_max_diff=7e-4		)



	def 			__A							(       self		):
							super().test_dict_tuple_outputs_equivalent(expected_max_difference=3e-3		)



	def 			__A							(       self		):
							super().test_pt_np_pil_outputs_equivalent(expected_max_diff=5e-4		)



	def 			__A							(       self		):
							super().test_save_load_local(expected_max_difference=5e-4		)



	def 			__A							(       self		):
							super().test_save_load_optional_components(expected_max_difference=4e-4		)






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



	@classmethod
	def 			__A							(       cls		):
							super().setUpClass()
							torch.use_deterministic_algorithms(UpperCAmelCase__		)



	@classmethod
	def 			__A							(       cls		):
							super().tearDownClass()
							torch.use_deterministic_algorithms(UpperCAmelCase__		)



	def 			__A							(       self		):
							super().tearDown()
							gc.collect()
							torch.cuda.empty_cache()



	def 			__A							(       self		):
							A__							=       torch.manual_seed(51		)

							A__							=       StableDiffusionAttendAndExcitePipeline.from_pretrained(
							    "CompVis/stable-diffusion-v1-4"			,	safety_checker=UpperCAmelCase__			,	torch_dtype=torch.floataa		)
							pipe.to("cuda"		)

							A__							=       "a painting of an elephant with glasses"
							A__							=       [5, 7]

							A__							=       pipe(
							    prompt=UpperCAmelCase__			,	token_indices=UpperCAmelCase__			,	guidance_scale=7.5			,	generator=UpperCAmelCase__			,	num_inference_steps=5			,	max_iter_to_alter=5			,	output_type="numpy"			,	).images[0]

							A__							=       load_numpy(
							    "https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/attend-and-excite/elephant_glasses.npy"		)
							assert np.abs((expected_image - image).max()		) < 5e-1





