






'''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 							__SCREAMING_SNAKE_CASE       :




						def __init__(  self ,   lowerCamelCase ,   lowerCamelCase=13 ,   lowerCamelCase=7 ,   lowerCamelCase=True ,   lowerCamelCase=True ,   lowerCamelCase=True ,   lowerCamelCase=True ,   lowerCamelCase=99 ,   lowerCamelCase=24 ,   lowerCamelCase=2 ,   lowerCamelCase=6 ,   lowerCamelCase=37 ,   lowerCamelCase="gelu" ,   lowerCamelCase=0.1 ,   lowerCamelCase=0.1 ,   lowerCamelCase=512 ,   lowerCamelCase=16 ,   lowerCamelCase=2 ,   lowerCamelCase=0.02 ,   lowerCamelCase=3 ,   lowerCamelCase=None ,   lowerCamelCase=1000 ,   )							->Optional[int]:



									'''simple docstring'''
									__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  __UpperCamelCase							(  self      )							->Optional[int]:



									'''simple docstring'''
									__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  __UpperCamelCase							(  self      )							->Union[str, Any]:



									'''simple docstring'''
									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 ,   lowerCamelCase ,   lowerCamelCase ,   lowerCamelCase ,   lowerCamelCase ,   lowerCamelCase ,   lowerCamelCase ,   lowerCamelCase ,   )							->Optional[Any]:



									'''simple docstring'''
									__a				     =    LiltModel(config=lowerCamelCase      )
									model.to(lowerCamelCase      )
									model.eval()
									__a				     =    model(lowerCamelCase ,   bbox=lowerCamelCase ,   attention_mask=lowerCamelCase ,   token_type_ids=lowerCamelCase      )
									__a				     =    model(lowerCamelCase ,   bbox=lowerCamelCase ,   token_type_ids=lowerCamelCase      )
									__a				     =    model(lowerCamelCase ,   bbox=lowerCamelCase      )

									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 ,   lowerCamelCase ,   lowerCamelCase ,   lowerCamelCase ,   lowerCamelCase ,   lowerCamelCase ,   lowerCamelCase ,   lowerCamelCase ,   )							->str:



									'''simple docstring'''
									__a				     =    self.num_labels
									__a				     =    LiltForTokenClassification(config=lowerCamelCase      )
									model.to(lowerCamelCase      )
									model.eval()
									__a				     =    model(
									    lowerCamelCase ,   bbox=lowerCamelCase ,   attention_mask=lowerCamelCase ,   token_type_ids=lowerCamelCase ,   labels=lowerCamelCase      )
									self.parent.assertEqual(result.logits.shape ,   (self.batch_size, self.seq_length, self.num_labels)      )




						def  __UpperCamelCase							(  self ,   lowerCamelCase ,   lowerCamelCase ,   lowerCamelCase ,   lowerCamelCase ,   lowerCamelCase ,   lowerCamelCase ,   lowerCamelCase ,   )							->Tuple:



									'''simple docstring'''
									__a				     =    LiltForQuestionAnswering(config=lowerCamelCase      )
									model.to(lowerCamelCase      )
									model.eval()
									__a				     =    model(
									    lowerCamelCase ,   bbox=lowerCamelCase ,   attention_mask=lowerCamelCase ,   token_type_ids=lowerCamelCase ,   start_positions=lowerCamelCase ,   end_positions=lowerCamelCase ,   )
									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      )							->Any:



									'''simple docstring'''
									__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 							__SCREAMING_SNAKE_CASE       (							_lowerCAmelCase      ,      _lowerCAmelCase      ,      _lowerCAmelCase      ,      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 ,   lowerCamelCase ,   lowerCamelCase ,   lowerCamelCase ,   lowerCamelCase ,   lowerCamelCase      )							->Tuple:



									'''simple docstring'''
									return True




						def  __UpperCamelCase							(  self      )							->Tuple:



									'''simple docstring'''
									__a				     =    LiltModelTester(self      )
									__a				     =    ConfigTester(self ,   config_class=lowerCamelCase ,   hidden_size=37      )




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



									'''simple docstring'''
									self.config_tester.run_common_tests()




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



									'''simple docstring'''
									__a				     =    self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_model(*lowerCamelCase      )




						def  __UpperCamelCase							(  self      )							->Tuple:



									'''simple docstring'''
									__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(*lowerCamelCase      )




						def  __UpperCamelCase							(  self      )							->Union[str, Any]:



									'''simple docstring'''
									__a				     =    self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_for_token_classification(*lowerCamelCase      )




						def  __UpperCamelCase							(  self      )							->Union[str, Any]:



									'''simple docstring'''
									__a				     =    self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_for_question_answering(*lowerCamelCase      )






						@slow
						def  __UpperCamelCase							(  self      )							->Optional[int]:



									'''simple docstring'''
									for model_name in LILT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
												__a				     =    LiltModel.from_pretrained(lowerCamelCase      )
												self.assertIsNotNone(lowerCamelCase      )







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




						def  __UpperCamelCase							(  self      )							->str:



									'''simple docstring'''
									__a				     =    LiltModel.from_pretrained('SCUT-DLVCLab/lilt-roberta-en-base'      ).to(lowerCamelCase      )

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

									# forward pass
									with torch.no_grad():
												__a				     =    model(input_ids=lowerCamelCase ,   bbox=lowerCamelCase      )

									__a				     =    torch.Size([1, 2, 768]      )
									__a				     =    torch.tensor(
									    [[-0.0653, 0.0950, -0.0061], [-0.0545, 0.0926, -0.0324]] ,   device=lowerCamelCase ,   )

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







'''simple docstring'''



__UpperCamelCase      :       Tuple      			=      """0.18.2"""

from .configuration_utils import ConfigMixin
from .utils import (
    OptionalDependencyNotAvailable,
    is_flax_available,
    is_inflect_available,
    is_invisible_watermark_available,
    is_k_diffusion_available,
    is_k_diffusion_version,
    is_librosa_available,
    is_note_seq_available,
    is_onnx_available,
    is_scipy_available,
    is_torch_available,
    is_torchsde_available,
    is_transformers_available,
    is_transformers_version,
    is_unidecode_available,
    logging,
)


try:
 if not is_onnx_available():
  raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
 from .utils.dummy_onnx_objects import *  # noqa F403
else:
 from .pipelines import OnnxRuntimeModel

try:
 if not is_torch_available():
  raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
 from .utils.dummy_pt_objects import *  # noqa F403
else:
 from .models import (
     AutoencoderKL,
     ControlNetModel,
     ModelMixin,
     PriorTransformer,
     TaFilmDecoder,
     TransformeraDModel,
     UNetaDModel,
     UNetaDConditionModel,
     UNetaDModel,
     UNetaDConditionModel,
     VQModel,
 )
 from .optimization import (
     get_constant_schedule,
     get_constant_schedule_with_warmup,
     get_cosine_schedule_with_warmup,
     get_cosine_with_hard_restarts_schedule_with_warmup,
     get_linear_schedule_with_warmup,
     get_polynomial_decay_schedule_with_warmup,
     get_scheduler,
 )
 from .pipelines import (
     AudioPipelineOutput,
     ConsistencyModelPipeline,
     DanceDiffusionPipeline,
     DDIMPipeline,
     DDPMPipeline,
     DiffusionPipeline,
     DiTPipeline,
     ImagePipelineOutput,
     KarrasVePipeline,
     LDMPipeline,
     LDMSuperResolutionPipeline,
     PNDMPipeline,
     RePaintPipeline,
     ScoreSdeVePipeline,
 )
 from .schedulers import (
     CMStochasticIterativeScheduler,
     DDIMInverseScheduler,
     DDIMParallelScheduler,
     DDIMScheduler,
     DDPMParallelScheduler,
     DDPMScheduler,
     DEISMultistepScheduler,
     DPMSolverMultistepInverseScheduler,
     DPMSolverMultistepScheduler,
     DPMSolverSinglestepScheduler,
     EulerAncestralDiscreteScheduler,
     EulerDiscreteScheduler,
     HeunDiscreteScheduler,
     IPNDMScheduler,
     KarrasVeScheduler,
     KDPMaAncestralDiscreteScheduler,
     KDPMaDiscreteScheduler,
     PNDMScheduler,
     RePaintScheduler,
     SchedulerMixin,
     ScoreSdeVeScheduler,
     UnCLIPScheduler,
     UniPCMultistepScheduler,
     VQDiffusionScheduler,
 )
 from .training_utils import EMAModel

try:
 if not (is_torch_available() and is_scipy_available()):
  raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
 from .utils.dummy_torch_and_scipy_objects import *  # noqa F403
else:
 from .schedulers import LMSDiscreteScheduler

try:
 if not (is_torch_available() and is_torchsde_available()):
  raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
 from .utils.dummy_torch_and_torchsde_objects import *  # noqa F403
else:
 from .schedulers import DPMSolverSDEScheduler

try:
 if not (is_torch_available() and is_transformers_available()):
  raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
 from .utils.dummy_torch_and_transformers_objects import *  # noqa F403
else:
 from .pipelines import (
     AltDiffusionImgaImgPipeline,
     AltDiffusionPipeline,
     AudioLDMPipeline,
     CycleDiffusionPipeline,
     IFImgaImgPipeline,
     IFImgaImgSuperResolutionPipeline,
     IFInpaintingPipeline,
     IFInpaintingSuperResolutionPipeline,
     IFPipeline,
     IFSuperResolutionPipeline,
     ImageTextPipelineOutput,
     KandinskyImgaImgPipeline,
     KandinskyInpaintPipeline,
     KandinskyPipeline,
     KandinskyPriorPipeline,
     KandinskyVaaControlnetImgaImgPipeline,
     KandinskyVaaControlnetPipeline,
     KandinskyVaaImgaImgPipeline,
     KandinskyVaaInpaintPipeline,
     KandinskyVaaPipeline,
     KandinskyVaaPriorEmbaEmbPipeline,
     KandinskyVaaPriorPipeline,
     LDMTextToImagePipeline,
     PaintByExamplePipeline,
     SemanticStableDiffusionPipeline,
     ShapEImgaImgPipeline,
     ShapEPipeline,
     StableDiffusionAttendAndExcitePipeline,
     StableDiffusionControlNetImgaImgPipeline,
     StableDiffusionControlNetInpaintPipeline,
     StableDiffusionControlNetPipeline,
     StableDiffusionDepthaImgPipeline,
     StableDiffusionDiffEditPipeline,
     StableDiffusionImageVariationPipeline,
     StableDiffusionImgaImgPipeline,
     StableDiffusionInpaintPipeline,
     StableDiffusionInpaintPipelineLegacy,
     StableDiffusionInstructPixaPixPipeline,
     StableDiffusionLatentUpscalePipeline,
     StableDiffusionLDMaDPipeline,
     StableDiffusionModelEditingPipeline,
     StableDiffusionPanoramaPipeline,
     StableDiffusionParadigmsPipeline,
     StableDiffusionPipeline,
     StableDiffusionPipelineSafe,
     StableDiffusionPixaPixZeroPipeline,
     StableDiffusionSAGPipeline,
     StableDiffusionUpscalePipeline,
     StableUnCLIPImgaImgPipeline,
     StableUnCLIPPipeline,
     TextToVideoSDPipeline,
     TextToVideoZeroPipeline,
     UnCLIPImageVariationPipeline,
     UnCLIPPipeline,
     UniDiffuserModel,
     UniDiffuserPipeline,
     UniDiffuserTextDecoder,
     VersatileDiffusionDualGuidedPipeline,
     VersatileDiffusionImageVariationPipeline,
     VersatileDiffusionPipeline,
     VersatileDiffusionTextToImagePipeline,
     VideoToVideoSDPipeline,
     VQDiffusionPipeline,
 )

try:
 if not (is_torch_available() and is_transformers_available() and is_invisible_watermark_available()):
  raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
 from .utils.dummy_torch_and_transformers_and_invisible_watermark_objects import *  # noqa F403
else:
 from .pipelines import StableDiffusionXLImgaImgPipeline, StableDiffusionXLPipeline

try:
 if not (is_torch_available() and is_transformers_available() and is_k_diffusion_available()):
  raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
 from .utils.dummy_torch_and_transformers_and_k_diffusion_objects import *  # noqa F403
else:
 from .pipelines import StableDiffusionKDiffusionPipeline

try:
 if not (is_torch_available() and is_transformers_available() and is_onnx_available()):
  raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
 from .utils.dummy_torch_and_transformers_and_onnx_objects import *  # noqa F403
else:
 from .pipelines import (
     OnnxStableDiffusionImgaImgPipeline,
     OnnxStableDiffusionInpaintPipeline,
     OnnxStableDiffusionInpaintPipelineLegacy,
     OnnxStableDiffusionPipeline,
     OnnxStableDiffusionUpscalePipeline,
     StableDiffusionOnnxPipeline,
 )

try:
 if not (is_torch_available() and is_librosa_available()):
  raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
 from .utils.dummy_torch_and_librosa_objects import *  # noqa F403
else:
 from .pipelines import AudioDiffusionPipeline, Mel

try:
 if not (is_transformers_available() and is_torch_available() and is_note_seq_available()):
  raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
 from .utils.dummy_transformers_and_torch_and_note_seq_objects import *  # noqa F403
else:
 from .pipelines import SpectrogramDiffusionPipeline

try:
 if not is_flax_available():
  raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
 from .utils.dummy_flax_objects import *  # noqa F403
else:
 from .models.controlnet_flax import FlaxControlNetModel
 from .models.modeling_flax_utils import FlaxModelMixin
 from .models.unet_ad_condition_flax import FlaxUNetaDConditionModel
 from .models.vae_flax import FlaxAutoencoderKL
 from .pipelines import FlaxDiffusionPipeline
 from .schedulers import (
     FlaxDDIMScheduler,
     FlaxDDPMScheduler,
     FlaxDPMSolverMultistepScheduler,
     FlaxKarrasVeScheduler,
     FlaxLMSDiscreteScheduler,
     FlaxPNDMScheduler,
     FlaxSchedulerMixin,
     FlaxScoreSdeVeScheduler,
 )


try:
 if not (is_flax_available() and is_transformers_available()):
  raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
 from .utils.dummy_flax_and_transformers_objects import *  # noqa F403
else:
 from .pipelines import (
     FlaxStableDiffusionControlNetPipeline,
     FlaxStableDiffusionImgaImgPipeline,
     FlaxStableDiffusionInpaintPipeline,
     FlaxStableDiffusionPipeline,
 )

try:
 if not (is_note_seq_available()):
  raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
 from .utils.dummy_note_seq_objects import *  # noqa F403
else:
 from .pipelines import MidiProcessor