



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							:

			def __init__(self,					SCREAMING_SNAKE_CASE_,					SCREAMING_SNAKE_CASE_=13,					SCREAMING_SNAKE_CASE_=7,					SCREAMING_SNAKE_CASE_=True,					SCREAMING_SNAKE_CASE_=True,					SCREAMING_SNAKE_CASE_=True,					SCREAMING_SNAKE_CASE_=True,					SCREAMING_SNAKE_CASE_=99,					SCREAMING_SNAKE_CASE_=24,					SCREAMING_SNAKE_CASE_=2,					SCREAMING_SNAKE_CASE_=6,					SCREAMING_SNAKE_CASE_=37,					SCREAMING_SNAKE_CASE_="gelu",					SCREAMING_SNAKE_CASE_=0.1,					SCREAMING_SNAKE_CASE_=0.1,					SCREAMING_SNAKE_CASE_=512,					SCREAMING_SNAKE_CASE_=16,					SCREAMING_SNAKE_CASE_=2,					SCREAMING_SNAKE_CASE_=0.0_2,					SCREAMING_SNAKE_CASE_=3,					SCREAMING_SNAKE_CASE_=None,					SCREAMING_SNAKE_CASE_=1000,					)    ->					Optional[Any]:
						UpperCAmelCase_:						Dict					=			parent
						UpperCAmelCase_:						Optional[Any]					=			batch_size
						UpperCAmelCase_:						str					=			seq_length
						UpperCAmelCase_:						List[str]					=			is_training
						UpperCAmelCase_:						Optional[Any]					=			use_input_mask
						UpperCAmelCase_:						str					=			use_token_type_ids
						UpperCAmelCase_:						List[Any]					=			use_labels
						UpperCAmelCase_:						Optional[int]					=			vocab_size
						UpperCAmelCase_:						Tuple					=			hidden_size
						UpperCAmelCase_:						List[Any]					=			num_hidden_layers
						UpperCAmelCase_:						Union[str, Any]					=			num_attention_heads
						UpperCAmelCase_:						Union[str, Any]					=			intermediate_size
						UpperCAmelCase_:						Tuple					=			hidden_act
						UpperCAmelCase_:						Union[str, Any]					=			hidden_dropout_prob
						UpperCAmelCase_:						int					=			attention_probs_dropout_prob
						UpperCAmelCase_:						Tuple					=			max_position_embeddings
						UpperCAmelCase_:						Dict					=			type_vocab_size
						UpperCAmelCase_:						Union[str, Any]					=			type_sequence_label_size
						UpperCAmelCase_:						List[Any]					=			initializer_range
						UpperCAmelCase_:						Tuple					=			num_labels
						UpperCAmelCase_:						Dict					=			scope
						UpperCAmelCase_:						Any					=			range_bbox

			def 			__snake_case    (self )    ->					int:
						UpperCAmelCase_:						int					=			ids_tensor([self.batch_size, self.seq_length],					self.vocab_size )

						UpperCAmelCase_:						List[str]					=			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_:						List[str]					=			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_:						Optional[Any]					=			t

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

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

						UpperCAmelCase_:						Any					=			None
						UpperCAmelCase_:						str					=			None
						if self.use_labels:
									UpperCAmelCase_:						List[Any]					=			ids_tensor([self.batch_size],					self.type_sequence_label_size )
									UpperCAmelCase_:						Dict					=			ids_tensor([self.batch_size, self.seq_length],					self.num_labels )

						UpperCAmelCase_:						Tuple					=			self.get_config()

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

			def 			__snake_case    (self )    ->					Any:
						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,					SCREAMING_SNAKE_CASE_,					SCREAMING_SNAKE_CASE_,					SCREAMING_SNAKE_CASE_,					SCREAMING_SNAKE_CASE_,					SCREAMING_SNAKE_CASE_,					SCREAMING_SNAKE_CASE_,					SCREAMING_SNAKE_CASE_,					)    ->					int:
						UpperCAmelCase_:						Dict					=			LiltModel(config=lowerCAmelCase_ )
						model.to(lowerCAmelCase_ )
						model.eval()
						UpperCAmelCase_:						List[str]					=			model(lowerCAmelCase_,					bbox=lowerCAmelCase_,					attention_mask=lowerCAmelCase_,					token_type_ids=lowerCAmelCase_ )
						UpperCAmelCase_:						Optional[int]					=			model(lowerCAmelCase_,					bbox=lowerCAmelCase_,					token_type_ids=lowerCAmelCase_ )
						UpperCAmelCase_:						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 			__snake_case    (self,					SCREAMING_SNAKE_CASE_,					SCREAMING_SNAKE_CASE_,					SCREAMING_SNAKE_CASE_,					SCREAMING_SNAKE_CASE_,					SCREAMING_SNAKE_CASE_,					SCREAMING_SNAKE_CASE_,					SCREAMING_SNAKE_CASE_,					)    ->					int:
						UpperCAmelCase_:						int					=			self.num_labels
						UpperCAmelCase_:						List[Any]					=			LiltForTokenClassification(config=lowerCAmelCase_ )
						model.to(lowerCAmelCase_ )
						model.eval()
						UpperCAmelCase_:						List[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 			__snake_case    (self,					SCREAMING_SNAKE_CASE_,					SCREAMING_SNAKE_CASE_,					SCREAMING_SNAKE_CASE_,					SCREAMING_SNAKE_CASE_,					SCREAMING_SNAKE_CASE_,					SCREAMING_SNAKE_CASE_,					SCREAMING_SNAKE_CASE_,					)    ->					str:
						UpperCAmelCase_:						List[str]					=			LiltForQuestionAnswering(config=lowerCAmelCase_ )
						model.to(lowerCAmelCase_ )
						model.eval()
						UpperCAmelCase_:						Dict					=			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 			__snake_case    (self )    ->					Any:
						UpperCAmelCase_:						List[str]					=			self.prepare_config_and_inputs()
						(
						    (
						    UpperCAmelCase_  
						)	,				(
						    UpperCAmelCase_  
						)	,				(
						    UpperCAmelCase_  
						)	,				(
						    UpperCAmelCase_  
						)	,				(
						    UpperCAmelCase_  
						)	,				(
						    UpperCAmelCase_  
						)	,				(
						    UpperCAmelCase_  
						)	,				  
						):						List[Any]					=			config_and_inputs
						UpperCAmelCase_:						List[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							(					__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 			__snake_case    (self,					SCREAMING_SNAKE_CASE_,					SCREAMING_SNAKE_CASE_,					SCREAMING_SNAKE_CASE_,					SCREAMING_SNAKE_CASE_,					SCREAMING_SNAKE_CASE_ )    ->					Dict:
						return True

			def 			__snake_case    (self )    ->					Any:
						UpperCAmelCase_:						str					=			LiltModelTester(self )
						UpperCAmelCase_:						List[Any]					=			ConfigTester(self,					config_class=lowerCAmelCase_,					hidden_size=37 )

			def 			__snake_case    (self )    ->					List[Any]:
						self.config_tester.run_common_tests()

			def 			__snake_case    (self )    ->					Tuple:
						UpperCAmelCase_:						Optional[int]					=			self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_model(*lowerCAmelCase_ )

			def 			__snake_case    (self )    ->					Optional[int]:
						UpperCAmelCase_:						Any					=			self.model_tester.prepare_config_and_inputs()
						for type in ["absolute", "relative_key", "relative_key_query"]:
									UpperCAmelCase_:						Dict					=			type
									self.model_tester.create_and_check_model(*lowerCAmelCase_ )

			def 			__snake_case    (self )    ->					Optional[int]:
						UpperCAmelCase_:						Any					=			self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_for_token_classification(*lowerCAmelCase_ )

			def 			__snake_case    (self )    ->					str:
						UpperCAmelCase_:						int					=			self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_for_question_answering(*lowerCAmelCase_ )

			@slow
			def 			__snake_case    (self )    ->					List[Any]:
						for model_name in LILT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
									UpperCAmelCase_:						List[str]					=			LiltModel.from_pretrained(lowerCAmelCase_ )
									self.assertIsNotNone(lowerCAmelCase_ )







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

			def 			__snake_case    (self )    ->					int:
						UpperCAmelCase_:						Dict					=			LiltModel.from_pretrained("""SCUT-DLVCLab/lilt-roberta-en-base""" ).to(lowerCAmelCase_ )

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

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

						UpperCAmelCase_:						str					=			torch.Size([1, 2, 768] )
						UpperCAmelCase_:						str					=			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=lowerCAmelCase_,					)

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







import copy
import os
from typing import Union

from ...configuration_utils import PretrainedConfig
from ...utils import logging


_SCREAMING_SNAKE_CASE										=						logging.get_logger(__name__)

_SCREAMING_SNAKE_CASE										=						{
    'BridgeTower/bridgetower-base': 'https://huggingface.co/BridgeTower/bridgetower-base/blob/main/config.json',
    'BridgeTower/bridgetower-base-itm-mlm': (
        'https://huggingface.co/BridgeTower/bridgetower-base-itm-mlm/blob/main/config.json'
    ),
}



class 				a							(			__lowerCAmelCase       ):







 """simple docstring"""






 lowerCamelCase	:Optional[Any]    			=		'''bridgetower_vision_model'''



 def __init__(					self		,				lowerCAmelCase_=7_68		,				lowerCAmelCase_=12		,				lowerCAmelCase_=3		,				lowerCAmelCase_=16		,				lowerCAmelCase_=2_88		,				lowerCAmelCase_=1		,				lowerCAmelCase_=1E-05		,				lowerCAmelCase_=False		,				lowerCAmelCase_=True		,				lowerCAmelCase_=False		,				**lowerCAmelCase_		,				)  ->					Any:
        super().__init__(**lowerCAmelCase_							)
        _A        =  hidden_size
        _A        =  num_hidden_layers
        _A        =  num_channels
        _A        =  patch_size
        _A        =  image_size
        _A        =  initializer_factor
        _A        =  layer_norm_eps
        _A        =  stop_gradient
        _A        =  share_layernorm
        _A        =  remove_last_layer



 @classmethod
 def      UpperCAmelCase							(					cls		,				lowerCAmelCase_		,				**lowerCAmelCase_							)  ->					"PretrainedConfig":
        _A   ,  _A        =  cls.get_config_dict(lowerCAmelCase_		,				**lowerCAmelCase_							)

        if config_dict.get("""model_type"""							) == "bridgetower":
               _A        =  config_dict["""text_config"""]

        if "model_type" in config_dict and hasattr(cls		,				"""model_type"""							) and config_dict["model_type"] != cls.model_type:
               logger.warning(
                   F'''You are using a model of type {config_dict['model_type']} to instantiate a model of type '''
                   F'''{cls.model_type}. This is not supported for all configurations of models and can yield errors.'''							)

        return cls.from_dict(lowerCAmelCase_		,				**lowerCAmelCase_							)



class 				a							(			__lowerCAmelCase       ):







 """simple docstring"""






 lowerCamelCase	:Union[str, Any]    			=		'''bridgetower_text_model'''



 def __init__(					self		,				lowerCAmelCase_=5_02_65		,				lowerCAmelCase_=7_68		,				lowerCAmelCase_=12		,				lowerCAmelCase_=12		,				lowerCAmelCase_=1		,				lowerCAmelCase_=30_72		,				lowerCAmelCase_="gelu"		,				lowerCAmelCase_=0.1		,				lowerCAmelCase_=0.1		,				lowerCAmelCase_=5_14		,				lowerCAmelCase_=1		,				lowerCAmelCase_=1E-05		,				lowerCAmelCase_=1		,				lowerCAmelCase_=0		,				lowerCAmelCase_=2		,				lowerCAmelCase_="absolute"		,				lowerCAmelCase_=True		,				**lowerCAmelCase_		,				)  ->					Optional[Any]:
        super().__init__(**lowerCAmelCase_							)

        _A        =  vocab_size
        _A        =  hidden_size
        _A        =  num_hidden_layers
        _A        =  num_attention_heads
        _A        =  hidden_act
        _A        =  initializer_factor
        _A        =  intermediate_size
        _A        =  hidden_dropout_prob
        _A        =  attention_probs_dropout_prob
        _A        =  max_position_embeddings
        _A        =  type_vocab_size
        _A        =  layer_norm_eps
        _A        =  position_embedding_type
        _A        =  use_cache
        _A        =  pad_token_id
        _A        =  bos_token_id
        _A        =  eos_token_id



 @classmethod
 def      UpperCAmelCase							(					cls		,				lowerCAmelCase_		,				**lowerCAmelCase_							)  ->					"PretrainedConfig":
        _A   ,  _A        =  cls.get_config_dict(lowerCAmelCase_		,				**lowerCAmelCase_							)

        if config_dict.get("""model_type"""							) == "bridgetower":
               _A        =  config_dict["""text_config"""]

        if "model_type" in config_dict and hasattr(cls		,				"""model_type"""							) and config_dict["model_type"] != cls.model_type:
               logger.warning(
                   F'''You are using a model of type {config_dict['model_type']} to instantiate a model of type '''
                   F'''{cls.model_type}. This is not supported for all configurations of models and can yield errors.'''							)

        return cls.from_dict(lowerCAmelCase_		,				**lowerCAmelCase_							)



class 				a							(			__lowerCAmelCase       ):







 """simple docstring"""






 lowerCamelCase	:Optional[int]    			=		'''bridgetower'''



 def __init__(					self		,				lowerCAmelCase_=True		,				lowerCAmelCase_="gelu"		,				lowerCAmelCase_=7_68		,				lowerCAmelCase_=1		,				lowerCAmelCase_=1E-05		,				lowerCAmelCase_=False		,				lowerCAmelCase_="add"		,				lowerCAmelCase_=12		,				lowerCAmelCase_=6		,				lowerCAmelCase_=False		,				lowerCAmelCase_=False		,				lowerCAmelCase_=None		,				lowerCAmelCase_=None		,				**lowerCAmelCase_		,				)  ->					int:
        # TODO: remove this once the Hub files are updated.
        _A        =  kwargs.pop("""text_config_dict"""		,				lowerCAmelCase_							)
        _A        =  kwargs.pop("""vision_config_dict"""		,				lowerCAmelCase_							)

        super().__init__(**lowerCAmelCase_							)
        _A        =  share_cross_modal_transformer_layers
        _A        =  hidden_act
        _A        =  hidden_size
        _A        =  initializer_factor
        _A        =  layer_norm_eps
        _A        =  share_link_tower_layers
        _A        =  link_tower_type
        _A        =  num_attention_heads
        _A        =  num_hidden_layers
        _A        =  tie_word_embeddings
        _A        =  init_layernorm_from_vision_encoder

        if text_config is None:
               _A        =  {}
               logger.info("""`text_config` is `None`. Initializing the `BridgeTowerTextConfig` with default values."""							)

        if vision_config is None:
               _A        =  {}
               logger.info("""`vision_config` is `None`. Initializing the `BridgeTowerVisionConfig` with default values."""							)

        _A        =  BridgeTowerTextConfig(**lowerCAmelCase_							)
        _A        =  BridgeTowerVisionConfig(**lowerCAmelCase_							)



 @classmethod
 def      UpperCAmelCase							(					cls		,				lowerCAmelCase_		,				lowerCAmelCase_		,				**lowerCAmelCase_							)  ->					Any:
        return cls(text_config=text_config.to_dict()		,				vision_config=vision_config.to_dict()		,				**lowerCAmelCase_							)



 def      UpperCAmelCase							(					self							)  ->					Union[str, Any]:
        _A        =  copy.deepcopy(self.__dict__							)
        _A        =  self.text_config.to_dict()
        _A        =  self.vision_config.to_dict()
        _A        =  self.__class__.model_type
        return output




