



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




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




							def    UpperCAmelCase__			(       self     :       Optional[int]):
									lowerCAmelCase_			:     List[Any]									= ids_tensor([self.batch_size, self.seq_length]				,				self.vocab_size)

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

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

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

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

									lowerCAmelCase_			:     Tuple									= self.get_config()

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




							def    UpperCAmelCase__			(       self     :       List[str]):
									return LiltConfig(
									    vocab_size=self.vocab_size				,				hidden_size=self.hidden_size				,				num_hidden_layers=self.num_hidden_layers				,				num_attention_heads=self.num_attention_heads				,				intermediate_size=self.intermediate_size				,				hidden_act=self.hidden_act				,				hidden_dropout_prob=self.hidden_dropout_prob				,				attention_probs_dropout_prob=self.attention_probs_dropout_prob				,				max_position_embeddings=self.max_position_embeddings				,				type_vocab_size=self.type_vocab_size				,				initializer_range=self.initializer_range				,				)




							def    UpperCAmelCase__			(       self     :       List[str]				,				A_     :       Optional[int]				,				A_     :       str				,				A_     :       Optional[Any]				,				A_     :       List[str]				,				A_     :       Union[str, Any]				,				A_     :       List[str]				,				A_     :       Any				,				):
									lowerCAmelCase_			:     Optional[Any]									= LiltModel(config=A_)
									model.to(A_)
									model.eval()
									lowerCAmelCase_			:     Union[str, Any]									= model(A_				,				bbox=A_				,				attention_mask=A_				,				token_type_ids=A_)
									lowerCAmelCase_			:     Tuple									= model(A_				,				bbox=A_				,				token_type_ids=A_)
									lowerCAmelCase_			:     Optional[int]									= model(A_				,				bbox=A_)

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




							def    UpperCAmelCase__			(       self     :       List[str]				,				A_     :       Union[str, Any]				,				A_     :       str				,				A_     :       Any				,				A_     :       Optional[int]				,				A_     :       Optional[int]				,				A_     :       Optional[Any]				,				A_     :       List[Any]				,				):
									lowerCAmelCase_			:     str									= self.num_labels
									lowerCAmelCase_			:     Dict									= LiltForTokenClassification(config=A_)
									model.to(A_)
									model.eval()
									lowerCAmelCase_			:     int									= model(
									    A_				,				bbox=A_				,				attention_mask=A_				,				token_type_ids=A_				,				labels=A_)
									self.parent.assertEqual(result.logits.shape				,				(self.batch_size, self.seq_length, self.num_labels))




							def    UpperCAmelCase__			(       self     :       Union[str, Any]				,				A_     :       List[str]				,				A_     :       int				,				A_     :       List[Any]				,				A_     :       List[Any]				,				A_     :       int				,				A_     :       str				,				A_     :       Union[str, Any]				,				):
									lowerCAmelCase_			:     Dict									= LiltForQuestionAnswering(config=A_)
									model.to(A_)
									model.eval()
									lowerCAmelCase_			:     Optional[Any]									= model(
									    A_				,				bbox=A_				,				attention_mask=A_				,				token_type_ids=A_				,				start_positions=A_				,				end_positions=A_				,				)
									self.parent.assertEqual(result.start_logits.shape				,				(self.batch_size, self.seq_length))
									self.parent.assertEqual(result.end_logits.shape				,				(self.batch_size, self.seq_length))







							def    UpperCAmelCase__			(       self     :       List[str]):
									lowerCAmelCase_			:     Tuple									= self.prepare_config_and_inputs()
									(
									    (
									    lowerCAmelCase_
									)					,  (
									    lowerCAmelCase_
									)					,  (
									    lowerCAmelCase_
									)					,  (
									    lowerCAmelCase_
									)					,  (
									    lowerCAmelCase_
									)					,  (
									    lowerCAmelCase_
									)					,  (
									    lowerCAmelCase_
									)					,  
									)			:     List[Any]									= config_and_inputs
									lowerCAmelCase_			:     Union[str, Any]									= {
									    '''input_ids''': input_ids,
									    '''bbox''': bbox,
									    '''token_type_ids''': token_type_ids,
									    '''attention_mask''': input_mask,
									}
									return config, inputs_dict





@require_torch
class 				__snake_case						(     UpperCamelCase_						,UpperCamelCase_						,UpperCamelCase_						,unittest.TestCase						):
							_a			      = (
							    (
							        LiltModel,
							        LiltForSequenceClassification,
							        LiltForTokenClassification,
							        LiltForQuestionAnswering,
							    )
							    if is_torch_available()
							    else ()
							)
							_a			      = (
							    {
							        '''feature-extraction''': LiltModel,
							        '''question-answering''': LiltForQuestionAnswering,
							        '''text-classification''': LiltForSequenceClassification,
							        '''token-classification''': LiltForTokenClassification,
							        '''zero-shot''': LiltForSequenceClassification,
							    }
							    if is_torch_available()
							    else {}
							)
							_a			      = False
							_a			      = False




							def    UpperCAmelCase__			(       self     :       Tuple				,				A_     :       Tuple				,				A_     :       List[str]				,				A_     :       Union[str, Any]				,				A_     :       Optional[int]				,				A_     :       Dict):
									return True




							def    UpperCAmelCase__			(       self     :       List[str]):
									lowerCAmelCase_			:     Dict									= LiltModelTester(self)
									lowerCAmelCase_			:     Optional[int]									= ConfigTester(self				,				config_class=A_				,				hidden_size=3_7)




							def    UpperCAmelCase__			(       self     :       List[Any]):
									self.config_tester.run_common_tests()




							def    UpperCAmelCase__			(       self     :       Union[str, Any]):
									lowerCAmelCase_			:     Optional[int]									= self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_model(*A_)




							def    UpperCAmelCase__			(       self     :       Any):
									lowerCAmelCase_			:     Any									= self.model_tester.prepare_config_and_inputs()
									for type in ["absolute", "relative_key", "relative_key_query"]:
											lowerCAmelCase_			:     int									= type
											self.model_tester.create_and_check_model(*A_)




							def    UpperCAmelCase__			(       self     :       Union[str, Any]):
									lowerCAmelCase_			:     Tuple									= self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_for_token_classification(*A_)




							def    UpperCAmelCase__			(       self     :       int):
									lowerCAmelCase_			:     List[str]									= self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_for_question_answering(*A_)







							@slow
							def    UpperCAmelCase__			(       self     :       Tuple):
									for model_name in LILT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
											lowerCAmelCase_			:     Union[str, Any]									= LiltModel.from_pretrained(A_)
											self.assertIsNotNone(A_)





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




							def    UpperCAmelCase__			(       self     :       Tuple):
									lowerCAmelCase_			:     Optional[int]									= LiltModel.from_pretrained('''SCUT-DLVCLab/lilt-roberta-en-base''').to(A_)

									lowerCAmelCase_			:     Optional[int]									= torch.tensor([[1, 2]]				,				device=A_)
									lowerCAmelCase_			:     Any									= torch.tensor([[[1, 2, 3, 4], [5, 6, 7, 8]]]				,				device=A_)

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

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

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










import gc
import unittest

from diffusers import FlaxStableDiffusionInpaintPipeline
from diffusers.utils import is_flax_available, load_image, slow
from diffusers.utils.testing_utils import require_flax


if is_flax_available():
						import jax
						import jax.numpy as jnp
						from flax.jax_utils import replicate
						from flax.training.common_utils import shard



@slow
@require_flax
class 				__snake_case						(     unittest.TestCase						):




							def    UpperCAmelCase__			(       self     :       Tuple):
									# clean up the VRAM after each test
									super().tearDown()
									gc.collect()







							def    UpperCAmelCase__			(       self     :       List[str]):
									lowerCAmelCase_			:     Any									= load_image(
									    '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main'''
									    '''/sd2-inpaint/init_image.png''')
									lowerCAmelCase_			:     str									= load_image(
									    '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/sd2-inpaint/mask.png''')

									lowerCAmelCase_			:     List[Any]									= '''xvjiarui/stable-diffusion-2-inpainting'''
									lowerCAmelCase_					,  lowerCAmelCase_			:     Optional[Any]									= FlaxStableDiffusionInpaintPipeline.from_pretrained(A_				,				safety_checker=A_)

									lowerCAmelCase_			:     List[str]									= '''Face of a yellow cat, high resolution, sitting on a park bench'''

									lowerCAmelCase_			:     List[Any]									= jax.random.PRNGKey(0)
									lowerCAmelCase_			:     str									= 5_0

									lowerCAmelCase_			:     List[Any]									= jax.device_count()
									lowerCAmelCase_			:     Union[str, Any]									= num_samples * [prompt]
									lowerCAmelCase_			:     str									= num_samples * [init_image]
									lowerCAmelCase_			:     Union[str, Any]									= num_samples * [mask_image]
									lowerCAmelCase_					,  lowerCAmelCase_					,  lowerCAmelCase_			:     Optional[Any]									= pipeline.prepare_inputs(A_				,				A_				,				A_)

									# shard inputs and rng
									lowerCAmelCase_			:     str									= replicate(A_)
									lowerCAmelCase_			:     str									= jax.random.split(A_				,				jax.device_count())
									lowerCAmelCase_			:     List[Any]									= shard(A_)
									lowerCAmelCase_			:     str									= shard(A_)
									lowerCAmelCase_			:     Tuple									= shard(A_)

									lowerCAmelCase_			:     int									= pipeline(
									    A_				,				A_				,				A_				,				A_				,				A_				,				A_				,				jit=A_)

									lowerCAmelCase_			:     Optional[int]									= output.images.reshape(A_				,				5_1_2				,				5_1_2				,				3)

									lowerCAmelCase_			:     List[str]									= images[0, 2_5_3:2_5_6, 2_5_3:2_5_6, -1]

									lowerCAmelCase_			:     int									= jnp.asarray(jax.device_get(image_slice.flatten()))
									lowerCAmelCase_			:     str									= jnp.array(
									    [0.361_1307, 0.3764_9736, 0.375_7408, 0.3821_3953, 0.3929_5167, 0.384_1631, 0.4155_4978, 0.413_7475, 0.421_7084])
									print(F"""output_slice: {output_slice}""")
									assert jnp.abs(output_slice - expected_slice).max() < 1e-2





