

import copy
import inspect
import unittest

from transformers import AutoBackbone
from transformers.configuration_utils import PretrainedConfig
from transformers.testing_utils import require_timm, require_torch, torch_device
from transformers.utils.import_utils import is_torch_available

from ...test_backbone_common import BackboneTesterMixin
from ...test_configuration_common import ConfigTester
from ...test_modeling_common import ModelTesterMixin, floats_tensor


if is_torch_available():
     import torch

     from transformers import TimmBackbone, TimmBackboneConfig

from ...test_pipeline_mixin import PipelineTesterMixin





class __SCREAMING_SNAKE_CASE     :






    def __init__(       self,		_a,		_a=None,		_a=None,		_a=None,		_a="resnet50",		_a=3,		_a=32,		_a=3,		_a=True,		_a=True,		)   ->					Dict:
       __SCREAMING_SNAKE_CASE       =				parent
       __SCREAMING_SNAKE_CASE       =				out_indices if out_indices is not None else [4]
       __SCREAMING_SNAKE_CASE       =				stage_names
       __SCREAMING_SNAKE_CASE       =				out_features
       __SCREAMING_SNAKE_CASE       =				backbone
       __SCREAMING_SNAKE_CASE       =				batch_size
       __SCREAMING_SNAKE_CASE       =				image_size
       __SCREAMING_SNAKE_CASE       =				num_channels
       __SCREAMING_SNAKE_CASE       =				use_pretrained_backbone
       __SCREAMING_SNAKE_CASE       =				is_training






    def 						__lowerCAmelCase    (       self )   ->					List[Any]:
       __SCREAMING_SNAKE_CASE       =				floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size] )
       __SCREAMING_SNAKE_CASE       =				self.get_config()

       return config, pixel_values






    def 						__lowerCAmelCase    (       self )   ->					int:
       return TimmBackboneConfig(
           image_size=self.image_size,		num_channels=self.num_channels,		out_features=self.out_features,		out_indices=self.out_indices,		stage_names=self.stage_names,		use_pretrained_backbone=self.use_pretrained_backbone,		backbone=self.backbone,		)






    def 						__lowerCAmelCase    (       self,		_a,		_a )   ->					Optional[Any]:
       __SCREAMING_SNAKE_CASE       =				TimmBackbone(config=lowercase__ )
       model.to(lowercase__ )
       model.eval()
       with torch.no_grad():
          __SCREAMING_SNAKE_CASE       =				model(lowercase__ )
       self.parent.assertEqual(
           result.feature_map[-1].shape,		(self.batch_size, model.channels[-1], 14, 14),		)






    def 						__lowerCAmelCase    (       self )   ->					Dict:
       __SCREAMING_SNAKE_CASE       =				self.prepare_config_and_inputs()
       __SCREAMING_SNAKE_CASE       =				config_and_inputs
       __SCREAMING_SNAKE_CASE       =				{'''pixel_values''': pixel_values}
       return config, inputs_dict






@require_torch
@require_timm
class __SCREAMING_SNAKE_CASE     (	_UpperCamelCase			,						_UpperCamelCase			,						_UpperCamelCase			,						unittest.TestCase		):
    SCREAMING_SNAKE_CASE__      =(TimmBackbone,) if is_torch_available() else ()
    SCREAMING_SNAKE_CASE__      ={'''feature-extraction''': TimmBackbone} if is_torch_available() else {}
    SCREAMING_SNAKE_CASE__      =False
    SCREAMING_SNAKE_CASE__      =False
    SCREAMING_SNAKE_CASE__      =False
    SCREAMING_SNAKE_CASE__      =False






    def 						__lowerCAmelCase    (       self )   ->					Union[str, Any]:
       __SCREAMING_SNAKE_CASE       =				TimmBackboneModelTester(self )
       __SCREAMING_SNAKE_CASE       =				ConfigTester(self,		config_class=lowercase__,		has_text_modality=lowercase__ )






    def 						__lowerCAmelCase    (       self )   ->					List[Any]:
       self.config_tester.create_and_test_config_to_json_string()
       self.config_tester.create_and_test_config_to_json_file()
       self.config_tester.create_and_test_config_from_and_save_pretrained()
       self.config_tester.create_and_test_config_with_num_labels()
       self.config_tester.check_config_can_be_init_without_params()
       self.config_tester.check_config_arguments_init()






    def 						__lowerCAmelCase    (       self )   ->					int:
       __SCREAMING_SNAKE_CASE       =				'''resnet18'''
       __SCREAMING_SNAKE_CASE       =				'''microsoft/resnet-18'''

       __SCREAMING_SNAKE_CASE       =				AutoBackbone.from_pretrained(lowercase__,		use_timm_backbone=lowercase__ )
       __SCREAMING_SNAKE_CASE       =				AutoBackbone.from_pretrained(lowercase__ )

       self.assertEqual(len(timm_model.out_features ),		len(transformers_model.out_features ) )
       self.assertEqual(len(timm_model.stage_names ),		len(transformers_model.stage_names ) )
       self.assertEqual(timm_model.channels,		transformers_model.channels )
       # Out indices are set to the last layer by default. For timm models, we don't know
       # the number of layers in advance, so we set it to (-1,), whereas for transformers
       # models, we set it to [len(stage_names) - 1] (kept for backward compatibility).
       self.assertEqual(timm_model.out_indices,		(-1,) )
       self.assertEqual(transformers_model.out_indices,		[len(timm_model.stage_names ) - 1] )

       __SCREAMING_SNAKE_CASE       =				AutoBackbone.from_pretrained(lowercase__,		use_timm_backbone=lowercase__,		out_indices=[1, 2, 3] )
       __SCREAMING_SNAKE_CASE       =				AutoBackbone.from_pretrained(lowercase__,		out_indices=[1, 2, 3] )

       self.assertEqual(timm_model.out_indices,		transformers_model.out_indices )
       self.assertEqual(len(timm_model.out_features ),		len(transformers_model.out_features ) )
       self.assertEqual(timm_model.channels,		transformers_model.channels )






    @unittest.skip("TimmBackbone doesn\'t support feed forward chunking" )
    def 						__lowerCAmelCase    (       self )   ->					Optional[Any]:
       pass






    @unittest.skip("TimmBackbone doesn\'t have num_hidden_layers attribute" )
    def 						__lowerCAmelCase    (       self )   ->					Optional[Any]:
       pass






    @unittest.skip("TimmBackbone initialization is managed on the timm side" )
    def 						__lowerCAmelCase    (       self )   ->					Optional[Any]:
       pass






    @unittest.skip("TimmBackbone models doesn\'t have inputs_embeds" )
    def 						__lowerCAmelCase    (       self )   ->					Optional[Any]:
       pass






    @unittest.skip("TimmBackbone models doesn\'t have inputs_embeds" )
    def 						__lowerCAmelCase    (       self )   ->					int:
       pass






    @unittest.skip("TimmBackbone model cannot be created without specifying a backbone checkpoint" )
    def 						__lowerCAmelCase    (       self )   ->					int:
       pass






    @unittest.skip("Only checkpoints on timm can be loaded into TimmBackbone" )
    def 						__lowerCAmelCase    (       self )   ->					str:
       pass






    @unittest.skip("model weights aren\'t tied in TimmBackbone." )
    def 						__lowerCAmelCase    (       self )   ->					List[Any]:
       pass






    @unittest.skip("model weights aren\'t tied in TimmBackbone." )
    def 						__lowerCAmelCase    (       self )   ->					Any:
       pass






    @unittest.skip("Only checkpoints on timm can be loaded into TimmBackbone" )
    def 						__lowerCAmelCase    (       self )   ->					List[str]:
       pass






    @unittest.skip("Only checkpoints on timm can be loaded into TimmBackbone" )
    def 						__lowerCAmelCase    (       self )   ->					str:
       pass






    @unittest.skip("TimmBackbone doesn\'t have hidden size info in its configuration." )
    def 						__lowerCAmelCase    (       self )   ->					List[Any]:
       pass






    @unittest.skip("TimmBackbone doesn\'t support output_attentions." )
    def 						__lowerCAmelCase    (       self )   ->					Optional[Any]:
       pass






    @unittest.skip("Safetensors is not supported by timm." )
    def 						__lowerCAmelCase    (       self )   ->					Union[str, Any]:
       pass






    @unittest.skip("Will be fixed soon by reducing the size of the model used for common tests." )
    def 						__lowerCAmelCase    (       self )   ->					List[str]:
       pass






    def 						__lowerCAmelCase    (       self )   ->					int:
       __SCREAMING_SNAKE_CASE       =				self.model_tester.prepare_config_and_inputs_for_common()

       for model_class in self.all_model_classes:
          __SCREAMING_SNAKE_CASE       =				model_class(lowercase__ )
          __SCREAMING_SNAKE_CASE       =				inspect.signature(model.forward )
          # signature.parameters is an OrderedDict => so arg_names order is deterministic
          __SCREAMING_SNAKE_CASE       =				[*signature.parameters.keys()]

          __SCREAMING_SNAKE_CASE       =				['''pixel_values''']
          self.assertListEqual(arg_names[:1],		lowercase__ )






    def 						__lowerCAmelCase    (       self )   ->					str:
       __SCREAMING_SNAKE_CASE       =				self.model_tester.prepare_config_and_inputs_for_common()
       __SCREAMING_SNAKE_CASE       =				True
       __SCREAMING_SNAKE_CASE       =				self.has_attentions

       # no need to test all models as different heads yield the same functionality
       __SCREAMING_SNAKE_CASE       =				self.all_model_classes[0]
       __SCREAMING_SNAKE_CASE       =				model_class(lowercase__ )
       model.to(lowercase__ )

       __SCREAMING_SNAKE_CASE       =				self._prepare_for_class(lowercase__,		lowercase__ )
       __SCREAMING_SNAKE_CASE       =				model(**lowercase__ )
       __SCREAMING_SNAKE_CASE       =				outputs[0][-1]

       # Encoder-/Decoder-only models
       __SCREAMING_SNAKE_CASE       =				outputs.hidden_states[0]
       hidden_states.retain_grad()

       if self.has_attentions:
          __SCREAMING_SNAKE_CASE       =				outputs.attentions[0]
          attentions.retain_grad()

       output.flatten()[0].backward(retain_graph=lowercase__ )

       self.assertIsNotNone(hidden_states.grad )

       if self.has_attentions:
          self.assertIsNotNone(attentions.grad )






    def 						__lowerCAmelCase    (       self )   ->					Any:
       __SCREAMING_SNAKE_CASE       =				self.model_tester.prepare_config_and_inputs_for_common()

       for model_class in self.all_model_classes:
          __SCREAMING_SNAKE_CASE       =				model_class(lowercase__ )
          model.to(lowercase__ )
          model.eval()
          __SCREAMING_SNAKE_CASE       =				model(**lowercase__ )

          self.assertEqual(len(result.feature_maps ),		len(config.out_indices ) )
          self.assertEqual(len(model.channels ),		len(config.out_indices ) )

          # Check output of last stage is taken if out_features=None, out_indices=None
          __SCREAMING_SNAKE_CASE       =				copy.deepcopy(lowercase__ )
          __SCREAMING_SNAKE_CASE       =				None
          __SCREAMING_SNAKE_CASE       =				model_class(lowercase__ )
          model.to(lowercase__ )
          model.eval()
          __SCREAMING_SNAKE_CASE       =				model(**lowercase__ )

          self.assertEqual(len(result.feature_maps ),		1 )
          self.assertEqual(len(model.channels ),		1 )

          # Check backbone can be initialized with fresh weights
          __SCREAMING_SNAKE_CASE       =				copy.deepcopy(lowercase__ )
          __SCREAMING_SNAKE_CASE       =				False
          __SCREAMING_SNAKE_CASE       =				model_class(lowercase__ )
          model.to(lowercase__ )
          model.eval()
          __SCREAMING_SNAKE_CASE       =				model(**lowercase__ )





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				,     lowercase__				,     lowercase__=1_3				,     lowercase__=7				,     lowercase__=True				,     lowercase__=True				,     lowercase__=True				,     lowercase__=True				,     lowercase__=9_9				,     lowercase__=2_4				,     lowercase__=2				,     lowercase__=6				,     lowercase__=3_7				,     lowercase__="gelu"				,     lowercase__=0.1				,     lowercase__=0.1				,     lowercase__=5_1_2				,     lowercase__=1_6				,     lowercase__=2				,     lowercase__=0.0_2				,     lowercase__=3				,     lowercase__=None				,     lowercase__=1_0_0_0				,     ):
							__UpperCAmelCase			:							int	    =	parent
							__UpperCAmelCase			:							Optional[int]	    =	batch_size
							__UpperCAmelCase			:							Any	    =	seq_length
							__UpperCAmelCase			:							Dict	    =	is_training
							__UpperCAmelCase			:							str	    =	use_input_mask
							__UpperCAmelCase			:							Dict	    =	use_token_type_ids
							__UpperCAmelCase			:							Tuple	    =	use_labels
							__UpperCAmelCase			:							Union[str, Any]	    =	vocab_size
							__UpperCAmelCase			:							Tuple	    =	hidden_size
							__UpperCAmelCase			:							Dict	    =	num_hidden_layers
							__UpperCAmelCase			:							Optional[Any]	    =	num_attention_heads
							__UpperCAmelCase			:							Union[str, Any]	    =	intermediate_size
							__UpperCAmelCase			:							Optional[int]	    =	hidden_act
							__UpperCAmelCase			:							int	    =	hidden_dropout_prob
							__UpperCAmelCase			:							List[Any]	    =	attention_probs_dropout_prob
							__UpperCAmelCase			:							List[Any]	    =	max_position_embeddings
							__UpperCAmelCase			:							Optional[Any]	    =	type_vocab_size
							__UpperCAmelCase			:							Optional[Any]	    =	type_sequence_label_size
							__UpperCAmelCase			:							str	    =	initializer_range
							__UpperCAmelCase			:							str	    =	num_labels
							__UpperCAmelCase			:							str	    =	scope
							__UpperCAmelCase			:							Union[str, Any]	    =	range_bbox


	def  A(    self):
							__UpperCAmelCase			:							Union[str, Any]	    =	ids_tensor([self.batch_size, self.seq_length]				,     self.vocab_size)

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

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

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

							__UpperCAmelCase			:							List[str]	    =	None
							__UpperCAmelCase			:							Union[str, Any]	    =	None
							if self.use_labels:
													__UpperCAmelCase			:							Dict	    =	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			:							int	    =	self.get_config()

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


	def  A(    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  A(    self				,     lowercase__				,     lowercase__				,     lowercase__				,     lowercase__				,     lowercase__				,     lowercase__				,     lowercase__				,     ):
							__UpperCAmelCase			:							List[Any]	    =	LiltModel(config=lowercase__)
							model.to(lowercase__)
							model.eval()
							__UpperCAmelCase			:							List[Any]	    =	model(lowercase__				,     bbox=lowercase__				,     attention_mask=lowercase__				,     token_type_ids=lowercase__)
							__UpperCAmelCase			:							Optional[int]	    =	model(lowercase__				,     bbox=lowercase__				,     token_type_ids=lowercase__)
							__UpperCAmelCase			:							Optional[int]	    =	model(lowercase__				,     bbox=lowercase__)

							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  A(    self				,     lowercase__				,     lowercase__				,     lowercase__				,     lowercase__				,     lowercase__				,     lowercase__				,     lowercase__				,     ):
							__UpperCAmelCase			:							Optional[int]	    =	self.num_labels
							__UpperCAmelCase			:							Dict	    =	LiltForTokenClassification(config=lowercase__)
							model.to(lowercase__)
							model.eval()
							__UpperCAmelCase			:							Dict	    =	model(
							    lowercase__				,     bbox=lowercase__				,     attention_mask=lowercase__				,     token_type_ids=lowercase__				,     labels=lowercase__)
							self.parent.assertEqual(result.logits.shape				,     (self.batch_size, self.seq_length, self.num_labels))


	def  A(    self				,     lowercase__				,     lowercase__				,     lowercase__				,     lowercase__				,     lowercase__				,     lowercase__				,     lowercase__				,     ):
							__UpperCAmelCase			:							Dict	    =	LiltForQuestionAnswering(config=lowercase__)
							model.to(lowercase__)
							model.eval()
							__UpperCAmelCase			:							List[Any]	    =	model(
							    lowercase__				,     bbox=lowercase__				,     attention_mask=lowercase__				,     token_type_ids=lowercase__				,     start_positions=lowercase__				,     end_positions=lowercase__				,     )
							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  A(    self):
							__UpperCAmelCase			:							Union[str, Any]	    =	self.prepare_config_and_inputs()
							(
							    (
							    __UpperCAmelCase
							)    ,      (
							    __UpperCAmelCase
							)    ,      (
							    __UpperCAmelCase
							)    ,      (
							    __UpperCAmelCase
							)    ,      (
							    __UpperCAmelCase
							)    ,      (
							    __UpperCAmelCase
							)    ,      (
							    __UpperCAmelCase
							)    ,      
							)			:							Any	    =	config_and_inputs
							__UpperCAmelCase			:							int	    =	{
							    '''input_ids''': input_ids,
							    '''bbox''': bbox,
							    '''token_type_ids''': token_type_ids,
							    '''attention_mask''': input_mask,
							}
							return config, inputs_dict






@require_torch
class 							lowerCamelCase   (  _UpperCamelCase							,						_UpperCamelCase							,						_UpperCamelCase							,						unittest.TestCase ):
	_lowerCAmelCase     :		str										=						(
	    (
	        LiltModel,
	        LiltForSequenceClassification,
	        LiltForTokenClassification,
	        LiltForQuestionAnswering,
	    )
	    if is_torch_available()
	    else ()
	)
	_lowerCAmelCase     :		str										=						(
	    {
	        '''feature-extraction''': LiltModel,
	        '''question-answering''': LiltForQuestionAnswering,
	        '''text-classification''': LiltForSequenceClassification,
	        '''token-classification''': LiltForTokenClassification,
	        '''zero-shot''': LiltForSequenceClassification,
	    }
	    if is_torch_available()
	    else {}
	)
	_lowerCAmelCase     :		Any										=						False
	_lowerCAmelCase     :		List[Any]										=						False


	def  A(    self				,     lowercase__				,     lowercase__				,     lowercase__				,     lowercase__				,     lowercase__):
							return True


	def  A(    self):
							__UpperCAmelCase			:							Tuple	    =	LiltModelTester(self)
							__UpperCAmelCase			:							List[Any]	    =	ConfigTester(self				,     config_class=lowercase__				,     hidden_size=3_7)


	def  A(    self):
							self.config_tester.run_common_tests()


	def  A(    self):
							__UpperCAmelCase			:							Union[str, Any]	    =	self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_model(*lowercase__)


	def  A(    self):
							__UpperCAmelCase			:							int	    =	self.model_tester.prepare_config_and_inputs()
							for type in ["absolute", "relative_key", "relative_key_query"]:
													__UpperCAmelCase			:							Any	    =	type
													self.model_tester.create_and_check_model(*lowercase__)


	def  A(    self):
							__UpperCAmelCase			:							Dict	    =	self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_for_token_classification(*lowercase__)


	def  A(    self):
							__UpperCAmelCase			:							Optional[int]	    =	self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_for_question_answering(*lowercase__)


	@slow
	def  A(    self):
							for model_name in LILT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
													__UpperCAmelCase			:							Optional[Any]	    =	LiltModel.from_pretrained(lowercase__)
													self.assertIsNotNone(lowercase__)






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


	def  A(    self):
							__UpperCAmelCase			:							Any	    =	LiltModel.from_pretrained('''SCUT-DLVCLab/lilt-roberta-en-base''').to(lowercase__)

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

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

							__UpperCAmelCase			:							List[Any]	    =	torch.Size([1, 2, 7_6_8])
							__UpperCAmelCase			:							Union[str, Any]	    =	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=lowercase__				,     )

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

