


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 							lowerCamelCase   :


	def __init__(    self				,     lowercase__				,     lowercase__=None				,     lowercase__=None				,     lowercase__=None				,     lowercase__="resnet50"				,     lowercase__=3				,     lowercase__=3_2				,     lowercase__=3				,     lowercase__=True				,     lowercase__=True				,     ):
							__UpperCAmelCase			:							List[str]	    =	parent
							__UpperCAmelCase			:							Dict	    =	out_indices if out_indices is not None else [4]
							__UpperCAmelCase			:							List[Any]	    =	stage_names
							__UpperCAmelCase			:							int	    =	out_features
							__UpperCAmelCase			:							Union[str, Any]	    =	backbone
							__UpperCAmelCase			:							Optional[Any]	    =	batch_size
							__UpperCAmelCase			:							Optional[Any]	    =	image_size
							__UpperCAmelCase			:							Optional[int]	    =	num_channels
							__UpperCAmelCase			:							List[Any]	    =	use_pretrained_backbone
							__UpperCAmelCase			:							Dict	    =	is_training


	def  A(    self):
							__UpperCAmelCase			:							Optional[Any]	    =	floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size])
							__UpperCAmelCase			:							Tuple	    =	self.get_config()

							return config, pixel_values


	def  A(    self):
							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  A(    self				,     lowercase__				,     lowercase__):
							__UpperCAmelCase			:							Tuple	    =	TimmBackbone(config=lowercase__)
							model.to(lowercase__)
							model.eval()
							with torch.no_grad():
													__UpperCAmelCase			:							Union[str, Any]	    =	model(lowercase__)
							self.parent.assertEqual(
							    result.feature_map[-1].shape				,     (self.batch_size, model.channels[-1], 1_4, 1_4)				,     )


	def  A(    self):
							__UpperCAmelCase			:							Dict	    =	self.prepare_config_and_inputs()
							__UpperCAmelCase    ,      __UpperCAmelCase			:							List[Any]	    =	config_and_inputs
							__UpperCAmelCase			:							List[Any]	    =	{'''pixel_values''': pixel_values}
							return config, inputs_dict






@require_torch
@require_timm
class 							lowerCamelCase   (  _UpperCamelCase							,						_UpperCamelCase							,						_UpperCamelCase							,						unittest.TestCase ):
	_lowerCAmelCase     :		Dict										=						(TimmBackbone,) if is_torch_available() else ()
	_lowerCAmelCase     :		str										=						{'''feature-extraction''': TimmBackbone} if is_torch_available() else {}
	_lowerCAmelCase     :		List[str]										=						False
	_lowerCAmelCase     :		str										=						False
	_lowerCAmelCase     :		List[Any]										=						False
	_lowerCAmelCase     :		List[str]										=						False


	def  A(    self):
							__UpperCAmelCase			:							List[Any]	    =	TimmBackboneModelTester(self)
							__UpperCAmelCase			:							int	    =	ConfigTester(self				,     config_class=lowercase__				,     has_text_modality=lowercase__)


	def  A(    self):
							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  A(    self):
							__UpperCAmelCase			:							int	    =	'''resnet18'''
							__UpperCAmelCase			:							List[str]	    =	'''microsoft/resnet-18'''

							__UpperCAmelCase			:							Any	    =	AutoBackbone.from_pretrained(lowercase__				,     use_timm_backbone=lowercase__)
							__UpperCAmelCase			:							Union[str, Any]	    =	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])

							__UpperCAmelCase			:							Union[str, Any]	    =	AutoBackbone.from_pretrained(lowercase__				,     use_timm_backbone=lowercase__				,     out_indices=[1, 2, 3])
							__UpperCAmelCase			:							Any	    =	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  A(    self):
							pass


	@unittest.skip('''TimmBackbone doesn\'t have num_hidden_layers attribute''')
	def  A(    self):
							pass


	@unittest.skip('''TimmBackbone initialization is managed on the timm side''')
	def  A(    self):
							pass


	@unittest.skip('''TimmBackbone models doesn\'t have inputs_embeds''')
	def  A(    self):
							pass


	@unittest.skip('''TimmBackbone models doesn\'t have inputs_embeds''')
	def  A(    self):
							pass


	@unittest.skip('''TimmBackbone model cannot be created without specifying a backbone checkpoint''')
	def  A(    self):
							pass


	@unittest.skip('''Only checkpoints on timm can be loaded into TimmBackbone''')
	def  A(    self):
							pass


	@unittest.skip('''model weights aren\'t tied in TimmBackbone.''')
	def  A(    self):
							pass


	@unittest.skip('''model weights aren\'t tied in TimmBackbone.''')
	def  A(    self):
							pass


	@unittest.skip('''Only checkpoints on timm can be loaded into TimmBackbone''')
	def  A(    self):
							pass


	@unittest.skip('''Only checkpoints on timm can be loaded into TimmBackbone''')
	def  A(    self):
							pass


	@unittest.skip('''TimmBackbone doesn\'t have hidden size info in its configuration.''')
	def  A(    self):
							pass


	@unittest.skip('''TimmBackbone doesn\'t support output_attentions.''')
	def  A(    self):
							pass


	@unittest.skip('''Safetensors is not supported by timm.''')
	def  A(    self):
							pass


	@unittest.skip('''Will be fixed soon by reducing the size of the model used for common tests.''')
	def  A(    self):
							pass


	def  A(    self):
							__UpperCAmelCase    ,      __UpperCAmelCase			:							Optional[int]	    =	self.model_tester.prepare_config_and_inputs_for_common()

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

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


	def  A(    self):
							__UpperCAmelCase    ,      __UpperCAmelCase			:							str	    =	self.model_tester.prepare_config_and_inputs_for_common()
							__UpperCAmelCase			:							Tuple	    =	True
							__UpperCAmelCase			:							Optional[int]	    =	self.has_attentions

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

							__UpperCAmelCase			:							Tuple	    =	self._prepare_for_class(lowercase__				,     lowercase__)
							__UpperCAmelCase			:							Optional[int]	    =	model(**lowercase__)
							__UpperCAmelCase			:							List[str]	    =	outputs[0][-1]

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

							if self.has_attentions:
													__UpperCAmelCase			:							Optional[int]	    =	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  A(    self):
							__UpperCAmelCase    ,      __UpperCAmelCase			:							Dict	    =	self.model_tester.prepare_config_and_inputs_for_common()

							for model_class in self.all_model_classes:
													__UpperCAmelCase			:							Optional[Any]	    =	model_class(lowercase__)
													model.to(lowercase__)
													model.eval()
													__UpperCAmelCase			:							Union[str, Any]	    =	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
													__UpperCAmelCase			:							List[str]	    =	copy.deepcopy(lowercase__)
													__UpperCAmelCase			:							str	    =	None
													__UpperCAmelCase			:							Optional[int]	    =	model_class(lowercase__)
													model.to(lowercase__)
													model.eval()
													__UpperCAmelCase			:							Optional[Any]	    =	model(**lowercase__)

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

													# Check backbone can be initialized with fresh weights
													__UpperCAmelCase			:							Tuple	    =	copy.deepcopy(lowercase__)
													__UpperCAmelCase			:							Optional[int]	    =	False
													__UpperCAmelCase			:							Optional[int]	    =	model_class(lowercase__)
													model.to(lowercase__)
													model.eval()
													__UpperCAmelCase			:							List[Any]	    =	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))

