






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






 """simple docstring"""





 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=5_12      ,  __lowerCAmelCase=16      ,  __lowerCAmelCase=2      ,  __lowerCAmelCase=0.02      ,  __lowerCAmelCase=3      ,  __lowerCAmelCase=None      ,  __lowerCAmelCase=10_00      ,  ):
       UpperCamelCase_					: Any            =				parent
       UpperCamelCase_					: Tuple            =				batch_size
       UpperCamelCase_					: Union[str, Any]            =				seq_length
       UpperCamelCase_					: List[str]            =				is_training
       UpperCamelCase_					: int            =				use_input_mask
       UpperCamelCase_					: Any            =				use_token_type_ids
       UpperCamelCase_					: Union[str, Any]            =				use_labels
       UpperCamelCase_					: List[str]            =				vocab_size
       UpperCamelCase_					: Any            =				hidden_size
       UpperCamelCase_					: str            =				num_hidden_layers
       UpperCamelCase_					: Optional[int]            =				num_attention_heads
       UpperCamelCase_					: Optional[int]            =				intermediate_size
       UpperCamelCase_					: int            =				hidden_act
       UpperCamelCase_					: Optional[Any]            =				hidden_dropout_prob
       UpperCamelCase_					: str            =				attention_probs_dropout_prob
       UpperCamelCase_					: str            =				max_position_embeddings
       UpperCamelCase_					: Tuple            =				type_vocab_size
       UpperCamelCase_					: int            =				type_sequence_label_size
       UpperCamelCase_					: Optional[Any]            =				initializer_range
       UpperCamelCase_					: List[str]            =				num_labels
       UpperCamelCase_					: List[Any]            =				scope
       UpperCamelCase_					: Any            =				range_bbox
 def 	_UpperCAmelCase							(				self			):
       UpperCamelCase_					: str            =				ids_tensor([self.batch_size, self.seq_length]      ,  self.vocab_size			)

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

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

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

       UpperCamelCase_					: str            =				None
       UpperCamelCase_					: str            =				None
       if self.use_labels:
             UpperCamelCase_					: int            =				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_					: Optional[int]            =				self.get_config()

       return config, input_ids, bbox, token_type_ids, input_mask, sequence_labels, token_labels
 def 	_UpperCAmelCase							(				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 	_UpperCAmelCase							(				self      ,  __lowerCAmelCase      ,  __lowerCAmelCase      ,  __lowerCAmelCase      ,  __lowerCAmelCase      ,  __lowerCAmelCase      ,  __lowerCAmelCase      ,  __lowerCAmelCase      ,  ):
       UpperCamelCase_					: Any            =				LiltModel(config=__lowerCAmelCase			)
       model.to(__lowerCAmelCase			)
       model.eval()
       UpperCamelCase_					: Tuple            =				model(__lowerCAmelCase      ,  bbox=__lowerCAmelCase      ,  attention_mask=__lowerCAmelCase      ,  token_type_ids=__lowerCAmelCase			)
       UpperCamelCase_					: List[Any]            =				model(__lowerCAmelCase      ,  bbox=__lowerCAmelCase      ,  token_type_ids=__lowerCAmelCase			)
       UpperCamelCase_					: Union[str, Any]            =				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      ,  ):
       UpperCamelCase_					: int            =				self.num_labels
       UpperCamelCase_					: Dict            =				LiltForTokenClassification(config=__lowerCAmelCase			)
       model.to(__lowerCAmelCase			)
       model.eval()
       UpperCamelCase_					: str            =				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      ,  ):
       UpperCamelCase_					: Optional[Any]            =				LiltForQuestionAnswering(config=__lowerCAmelCase			)
       model.to(__lowerCAmelCase			)
       model.eval()
       UpperCamelCase_					: str            =				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			):
       UpperCamelCase_					: List[str]            =				self.prepare_config_and_inputs()
       (
           (
           UpperCamelCase_
       )     ,      (
           UpperCamelCase_
       )     ,      (
           UpperCamelCase_
       )     ,      (
           UpperCamelCase_
       )     ,      (
           UpperCamelCase_
       )     ,      (
           UpperCamelCase_
       )     ,      (
           UpperCamelCase_
       )     ,      
       )					: Optional[int]            =				config_and_inputs
       UpperCamelCase_					: 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      A     (   SCREAMING_SNAKE_CASE__, SCREAMING_SNAKE_CASE__, SCREAMING_SNAKE_CASE__, unittest.TestCase    ):






 """simple docstring"""





 __a      :    str			  =    (
     (
         LiltModel,
         LiltForSequenceClassification,
         LiltForTokenClassification,
         LiltForQuestionAnswering,
     )
     if is_torch_available()
     else ()
 )
 __a      :    Dict			  =    (
     {
         '''feature-extraction''': LiltModel,
         '''question-answering''': LiltForQuestionAnswering,
         '''text-classification''': LiltForSequenceClassification,
         '''token-classification''': LiltForTokenClassification,
         '''zero-shot''': LiltForSequenceClassification,
     }
     if is_torch_available()
     else {}
 )
 __a      :    Union[str, Any]			  =    False
 __a      :    Tuple			  =    False
 def 	_UpperCAmelCase							(				self      ,  __lowerCAmelCase      ,  __lowerCAmelCase      ,  __lowerCAmelCase      ,  __lowerCAmelCase      ,  __lowerCAmelCase			):
       return True
 def 	_UpperCAmelCase							(				self			):
       UpperCamelCase_					: Union[str, Any]            =				LiltModelTester(self			)
       UpperCamelCase_					: Optional[int]            =				ConfigTester(self      ,  config_class=__lowerCAmelCase      ,  hidden_size=37			)
 def 	_UpperCAmelCase							(				self			):
       self.config_tester.run_common_tests()
 def 	_UpperCAmelCase							(				self			):
       UpperCamelCase_					: Dict            =				self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_model(*__lowerCAmelCase			)
 def 	_UpperCAmelCase							(				self			):
       UpperCamelCase_					: Any            =				self.model_tester.prepare_config_and_inputs()
       for type in ["absolute", "relative_key", "relative_key_query"]:
             UpperCamelCase_					: List[str]            =				type
             self.model_tester.create_and_check_model(*__lowerCAmelCase			)
 def 	_UpperCAmelCase							(				self			):
       UpperCamelCase_					: List[Any]            =				self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_for_token_classification(*__lowerCAmelCase			)
 def 	_UpperCAmelCase							(				self			):
       UpperCamelCase_					: int            =				self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_for_question_answering(*__lowerCAmelCase			)





 @slow
 def 	_UpperCAmelCase							(				self			):
       for model_name in LILT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
             UpperCamelCase_					: Tuple            =				LiltModel.from_pretrained(__lowerCAmelCase			)
             self.assertIsNotNone(__lowerCAmelCase			)






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






 """simple docstring"""





 def 	_UpperCAmelCase							(				self			):
       UpperCamelCase_					: List[Any]            =				LiltModel.from_pretrained("""SCUT-DLVCLab/lilt-roberta-en-base"""			).to(__lowerCAmelCase			)

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

       # forward pass
       with torch.no_grad():
             UpperCamelCase_					: List[Any]            =				model(input_ids=__lowerCAmelCase      ,  bbox=__lowerCAmelCase			)

       UpperCamelCase_					: int            =				torch.Size([1, 2, 7_68]			)
       UpperCamelCase_					: Dict            =				torch.tensor(
           [[-0.06_53, 0.09_50, -0.00_61], [-0.05_45, 0.09_26, -0.03_24]]      ,  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'''

import gc
import unittest

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

from diffusers import TransformeraDModel, VQDiffusionPipeline, VQDiffusionScheduler, VQModel
from diffusers.pipelines.vq_diffusion.pipeline_vq_diffusion import LearnedClassifierFreeSamplingEmbeddings
from diffusers.utils import load_numpy, slow, torch_device
from diffusers.utils.testing_utils import require_torch_gpu


UpperCamelCase      					=False




class      A     (   unittest.TestCase    ):






 """simple docstring"""





 def 	_UpperCAmelCase							(				self			):
       # clean up the VRAM after each test
       super().tearDown()
       gc.collect()
       torch.cuda.empty_cache()
 @property
 def 	_UpperCAmelCase							(				self			):
       return 12
 @property
 def 	_UpperCAmelCase							(				self			):
       return 12
 @property
 def 	_UpperCAmelCase							(				self			):
       return 32
 @property
 def 	_UpperCAmelCase							(				self			):
       torch.manual_seed(0			)
       UpperCamelCase_					: Union[str, Any]            =				VQModel(
           block_out_channels=[32, 64]      ,  in_channels=3      ,  out_channels=3      ,  down_block_types=["""DownEncoderBlock2D""", """DownEncoderBlock2D"""]      ,  up_block_types=["""UpDecoderBlock2D""", """UpDecoderBlock2D"""]      ,  latent_channels=3      ,  num_vq_embeddings=self.num_embed      ,  vq_embed_dim=3      ,  )
       return model
 @property
 def 	_UpperCAmelCase							(				self			):
       UpperCamelCase_					: str            =				CLIPTokenizer.from_pretrained("""hf-internal-testing/tiny-random-clip"""			)
       return tokenizer
 @property
 def 	_UpperCAmelCase							(				self			):
       torch.manual_seed(0			)
       UpperCamelCase_					: Any            =				CLIPTextConfig(
           bos_token_id=0      ,  eos_token_id=2      ,  hidden_size=self.text_embedder_hidden_size      ,  intermediate_size=37      ,  layer_norm_eps=1E-05      ,  num_attention_heads=4      ,  num_hidden_layers=5      ,  pad_token_id=1      ,  vocab_size=10_00      ,  )
       return CLIPTextModel(__lowerCAmelCase			)
 @property
 def 	_UpperCAmelCase							(				self			):
       torch.manual_seed(0			)

       UpperCamelCase_					: Optional[Any]            =				12
       UpperCamelCase_					: Optional[int]            =				12

       UpperCamelCase_					: Optional[Any]            =				{
           """attention_bias""": True,
           """cross_attention_dim""": 32,
           """attention_head_dim""": height * width,
           """num_attention_heads""": 1,
           """num_vector_embeds""": self.num_embed,
           """num_embeds_ada_norm""": self.num_embeds_ada_norm,
           """norm_num_groups""": 32,
           """sample_size""": width,
           """activation_fn""": """geglu-approximate""",
       }

       UpperCamelCase_					: Optional[Any]            =				TransformeraDModel(**__lowerCAmelCase			)
       return model
 def 	_UpperCAmelCase							(				self			):
       UpperCamelCase_					: Union[str, Any]            =				"""cpu"""

       UpperCamelCase_					: List[str]            =				self.dummy_vqvae
       UpperCamelCase_					: str            =				self.dummy_text_encoder
       UpperCamelCase_					: Optional[Any]            =				self.dummy_tokenizer
       UpperCamelCase_					: Optional[int]            =				self.dummy_transformer
       UpperCamelCase_					: Optional[Any]            =				VQDiffusionScheduler(self.num_embed			)
       UpperCamelCase_					: Union[str, Any]            =				LearnedClassifierFreeSamplingEmbeddings(learnable=__lowerCAmelCase			)

       UpperCamelCase_					: Tuple            =				VQDiffusionPipeline(
           vqvae=__lowerCAmelCase      ,  text_encoder=__lowerCAmelCase      ,  tokenizer=__lowerCAmelCase      ,  transformer=__lowerCAmelCase      ,  scheduler=__lowerCAmelCase      ,  learned_classifier_free_sampling_embeddings=__lowerCAmelCase      ,  )
       UpperCamelCase_					: Union[str, Any]            =				pipe.to(__lowerCAmelCase			)
       pipe.set_progress_bar_config(disable=__lowerCAmelCase			)

       UpperCamelCase_					: Optional[int]            =				"""teddy bear playing in the pool"""

       UpperCamelCase_					: Union[str, Any]            =				torch.Generator(device=__lowerCAmelCase			).manual_seed(0			)
       UpperCamelCase_					: List[Any]            =				pipe([prompt]      ,  generator=__lowerCAmelCase      ,  num_inference_steps=2      ,  output_type="""np"""			)
       UpperCamelCase_					: Dict            =				output.images

       UpperCamelCase_					: List[str]            =				torch.Generator(device=__lowerCAmelCase			).manual_seed(0			)
       UpperCamelCase_					: Dict            =				pipe(
           [prompt]      ,  generator=__lowerCAmelCase      ,  output_type="""np"""      ,  return_dict=__lowerCAmelCase      ,  num_inference_steps=2			)[0]

       UpperCamelCase_					: Union[str, Any]            =				image[0, -3:, -3:, -1]
       UpperCamelCase_					: Any            =				image_from_tuple[0, -3:, -3:, -1]

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

       UpperCamelCase_					: List[str]            =				np.array([0.65_51, 0.61_68, 0.50_08, 0.56_76, 0.56_59, 0.42_95, 0.60_73, 0.55_99, 0.49_92]			)

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





 def 	_UpperCAmelCase							(				self			):
       UpperCamelCase_					: int            =				"""cpu"""

       UpperCamelCase_					: List[Any]            =				self.dummy_vqvae
       UpperCamelCase_					: Union[str, Any]            =				self.dummy_text_encoder
       UpperCamelCase_					: int            =				self.dummy_tokenizer
       UpperCamelCase_					: Tuple            =				self.dummy_transformer
       UpperCamelCase_					: Tuple            =				VQDiffusionScheduler(self.num_embed			)
       UpperCamelCase_					: List[Any]            =				LearnedClassifierFreeSamplingEmbeddings(
           learnable=__lowerCAmelCase      ,  hidden_size=self.text_embedder_hidden_size      ,  length=tokenizer.model_max_length			)

       UpperCamelCase_					: Optional[Any]            =				VQDiffusionPipeline(
           vqvae=__lowerCAmelCase      ,  text_encoder=__lowerCAmelCase      ,  tokenizer=__lowerCAmelCase      ,  transformer=__lowerCAmelCase      ,  scheduler=__lowerCAmelCase      ,  learned_classifier_free_sampling_embeddings=__lowerCAmelCase      ,  )
       UpperCamelCase_					: List[Any]            =				pipe.to(__lowerCAmelCase			)
       pipe.set_progress_bar_config(disable=__lowerCAmelCase			)

       UpperCamelCase_					: Tuple            =				"""teddy bear playing in the pool"""

       UpperCamelCase_					: List[str]            =				torch.Generator(device=__lowerCAmelCase			).manual_seed(0			)
       UpperCamelCase_					: Tuple            =				pipe([prompt]      ,  generator=__lowerCAmelCase      ,  num_inference_steps=2      ,  output_type="""np"""			)
       UpperCamelCase_					: List[str]            =				output.images

       UpperCamelCase_					: Optional[int]            =				torch.Generator(device=__lowerCAmelCase			).manual_seed(0			)
       UpperCamelCase_					: List[str]            =				pipe(
           [prompt]      ,  generator=__lowerCAmelCase      ,  output_type="""np"""      ,  return_dict=__lowerCAmelCase      ,  num_inference_steps=2			)[0]

       UpperCamelCase_					: Dict            =				image[0, -3:, -3:, -1]
       UpperCamelCase_					: Optional[int]            =				image_from_tuple[0, -3:, -3:, -1]

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

       UpperCamelCase_					: List[Any]            =				np.array([0.66_93, 0.60_75, 0.49_59, 0.57_01, 0.55_83, 0.43_33, 0.61_71, 0.56_84, 0.49_88]			)

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






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






 """simple docstring"""





 def 	_UpperCAmelCase							(				self			):
       # clean up the VRAM after each test
       super().tearDown()
       gc.collect()
       torch.cuda.empty_cache()





 def 	_UpperCAmelCase							(				self			):
       UpperCamelCase_					: List[Any]            =				load_numpy(
           """https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main"""
           """/vq_diffusion/teddy_bear_pool_classifier_free_sampling.npy"""			)

       UpperCamelCase_					: Optional[int]            =				VQDiffusionPipeline.from_pretrained("""microsoft/vq-diffusion-ithq"""			)
       UpperCamelCase_					: str            =				pipeline.to(__lowerCAmelCase			)
       pipeline.set_progress_bar_config(disable=__lowerCAmelCase			)

       # requires GPU generator for gumbel softmax
       # don't use GPU generator in tests though
       UpperCamelCase_					: List[Any]            =				torch.Generator(device=__lowerCAmelCase			).manual_seed(0			)
       UpperCamelCase_					: str            =				pipeline(
           """teddy bear playing in the pool"""      ,  num_images_per_prompt=1      ,  generator=__lowerCAmelCase      ,  output_type="""np"""      ,  )

       UpperCamelCase_					: Union[str, Any]            =				output.images[0]

       assert image.shape == (2_56, 2_56, 3)
       assert np.abs(expected_image - image			).max() < 2.0






