

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







       def __init__(							self	:       int  ,					__lowercase	:       str  ,					__lowercase	:       Any=13  ,					__lowercase	:       Dict=7  ,					__lowercase	:       str=True  ,					__lowercase	:       Optional[int]=True  ,					__lowercase	:       Union[str, Any]=True  ,					__lowercase	:       Union[str, Any]=True  ,					__lowercase	:       Union[str, Any]=99  ,					__lowercase	:       List[str]=24  ,					__lowercase	:       Dict=2  ,					__lowercase	:       int=6  ,					__lowercase	:       Optional[int]=37  ,					__lowercase	:       Any="gelu"  ,					__lowercase	:       str=0.1  ,					__lowercase	:       Optional[int]=0.1  ,					__lowercase	:       Union[str, Any]=512  ,					__lowercase	:       Union[str, Any]=16  ,					__lowercase	:       List[str]=2  ,					__lowercase	:       str=0.0_2  ,					__lowercase	:       int=3  ,					__lowercase	:       List[str]=None  ,					__lowercase	:       Optional[Any]=1000  ,					):







            """simple docstring"""







            __lowercase							=parent
            __lowercase							=batch_size
            __lowercase							=seq_length
            __lowercase							=is_training
            __lowercase							=use_input_mask
            __lowercase							=use_token_type_ids
            __lowercase							=use_labels
            __lowercase							=vocab_size
            __lowercase							=hidden_size
            __lowercase							=num_hidden_layers
            __lowercase							=num_attention_heads
            __lowercase							=intermediate_size
            __lowercase							=hidden_act
            __lowercase							=hidden_dropout_prob
            __lowercase							=attention_probs_dropout_prob
            __lowercase							=max_position_embeddings
            __lowercase							=type_vocab_size
            __lowercase							=type_sequence_label_size
            __lowercase							=initializer_range
            __lowercase							=num_labels
            __lowercase							=scope
            __lowercase							=range_bbox







       def 	snake_case      (							self	:       Dict					):







            """simple docstring"""







            __lowercase							=ids_tensor([self.batch_size, self.seq_length]  ,					self.vocab_size					)

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

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

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

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

            __lowercase							=self.get_config()

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







       def 	snake_case      (							self	:       int					):







            """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 	snake_case      (							self	:       Optional[int]  ,					__lowercase	:       Any  ,					__lowercase	:       Dict  ,					__lowercase	:       Tuple  ,					__lowercase	:       Optional[Any]  ,					__lowercase	:       Optional[int]  ,					__lowercase	:       Dict  ,					__lowercase	:       Dict  ,					):







            """simple docstring"""







            __lowercase							=LiltModel(config=snake_case_					)
            model.to(snake_case_					)
            model.eval()
            __lowercase							=model(snake_case_  ,					bbox=snake_case_  ,					attention_mask=snake_case_  ,					token_type_ids=snake_case_					)
            __lowercase							=model(snake_case_  ,					bbox=snake_case_  ,					token_type_ids=snake_case_					)
            __lowercase							=model(snake_case_  ,					bbox=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 	snake_case      (							self	:       int  ,					__lowercase	:       List[Any]  ,					__lowercase	:       int  ,					__lowercase	:       Tuple  ,					__lowercase	:       Tuple  ,					__lowercase	:       Dict  ,					__lowercase	:       Optional[int]  ,					__lowercase	:       List[str]  ,					):







            """simple docstring"""







            __lowercase							=self.num_labels
            __lowercase							=LiltForTokenClassification(config=snake_case_					)
            model.to(snake_case_					)
            model.eval()
            __lowercase							=model(
                snake_case_  ,					bbox=snake_case_  ,					attention_mask=snake_case_  ,					token_type_ids=snake_case_  ,					labels=snake_case_					)
            self.parent.assertEqual(result.logits.shape  ,					(self.batch_size, self.seq_length, self.num_labels)					)







       def 	snake_case      (							self	:       List[Any]  ,					__lowercase	:       Tuple  ,					__lowercase	:       str  ,					__lowercase	:       Dict  ,					__lowercase	:       Optional[Any]  ,					__lowercase	:       Union[str, Any]  ,					__lowercase	:       Any  ,					__lowercase	:       str  ,					):







            """simple docstring"""







            __lowercase							=LiltForQuestionAnswering(config=snake_case_					)
            model.to(snake_case_					)
            model.eval()
            __lowercase							=model(
                snake_case_  ,					bbox=snake_case_  ,					attention_mask=snake_case_  ,					token_type_ids=snake_case_  ,					start_positions=snake_case_  ,					end_positions=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 	snake_case      (							self	:       int					):







            """simple docstring"""







            __lowercase							=self.prepare_config_and_inputs()
            (
                (
                __lowercase  
            )						,   (
                __lowercase  
            )						,   (
                __lowercase  
            )						,   (
                __lowercase  
            )						,   (
                __lowercase  
            )						,   (
                __lowercase  
            )						,   (
                __lowercase  
            )						,     
            )							=config_and_inputs
            __lowercase							={
                'input_ids': input_ids,
                'bbox': bbox,
                'token_type_ids': token_type_ids,
                'attention_mask': input_mask,
            }
            return config, inputs_dict






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







       def 	snake_case      (							self	:       List[Any]  ,					__lowercase	:       str  ,					__lowercase	:       str  ,					__lowercase	:       Dict  ,					__lowercase	:       Any  ,					__lowercase	:       Any					):







            """simple docstring"""







            return True







       def 	snake_case      (							self	:       Tuple					):







            """simple docstring"""







            __lowercase							=LiltModelTester(self					)
            __lowercase							=ConfigTester(self  ,					config_class=snake_case_  ,					hidden_size=37					)







       def 	snake_case      (							self	:       Optional[Any]					):







            """simple docstring"""







            self.config_tester.run_common_tests()







       def 	snake_case      (							self	:       Any					):







            """simple docstring"""







            __lowercase							=self.model_tester.prepare_config_and_inputs()
            self.model_tester.create_and_check_model(*snake_case_					)







       def 	snake_case      (							self	:       Union[str, Any]					):







            """simple docstring"""







            __lowercase							=self.model_tester.prepare_config_and_inputs()
            for type in ["absolute", "relative_key", "relative_key_query"]:
                 __lowercase							=type
                 self.model_tester.create_and_check_model(*snake_case_					)







       def 	snake_case      (							self	:       Union[str, Any]					):







            """simple docstring"""







            __lowercase							=self.model_tester.prepare_config_and_inputs()
            self.model_tester.create_and_check_for_token_classification(*snake_case_					)







       def 	snake_case      (							self	:       Tuple					):







            """simple docstring"""







            __lowercase							=self.model_tester.prepare_config_and_inputs()
            self.model_tester.create_and_check_for_question_answering(*snake_case_					)







       @slow
       def 	snake_case      (							self	:       Tuple					):







            """simple docstring"""







            for model_name in LILT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
                 __lowercase							=LiltModel.from_pretrained(snake_case_					)
                 self.assertIsNotNone(snake_case_					)






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







       def 	snake_case      (							self	:       Tuple					):







            """simple docstring"""







            __lowercase							=LiltModel.from_pretrained('SCUT-DLVCLab/lilt-roberta-en-base'					).to(snake_case_					)

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

            # forward pass
            with torch.no_grad():
                 __lowercase							=model(input_ids=snake_case_  ,					bbox=snake_case_					)

            __lowercase							=torch.Size([1, 2, 768]					)
            __lowercase							=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=snake_case_  ,					)

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









'''simple docstring'''




__SCREAMING_SNAKE_CASE     :List[str]       =				'''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




