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

					def    lowercase__      (	self					:     Any   ):
												lowerCamelCase_							  =					ids_tensor([self.batch_size, self.seq_length]   ,					self.vocab_size   )

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

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

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

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

												lowerCamelCase_							  =					self.get_config()

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

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

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

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





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

					def    lowercase__      (	self					:     str   ,					__UpperCamelCase					:     Any   ,					__UpperCamelCase					:     List[Any]   ,					__UpperCamelCase					:     List[Any]   ,					__UpperCamelCase					:     Optional[Any]   ,					__UpperCamelCase					:     Tuple   ):
												return True

					def    lowercase__      (	self					:     Optional[int]   ):
												lowerCamelCase_							  =					LiltModelTester(self   )
												lowerCamelCase_							  =					ConfigTester(self   ,					config_class=__UpperCamelCase   ,					hidden_size=3_7   )

					def    lowercase__      (	self					:     List[Any]   ):
												self.config_tester.run_common_tests()

					def    lowercase__      (	self					:     int   ):
												lowerCamelCase_							  =					self.model_tester.prepare_config_and_inputs()
												self.model_tester.create_and_check_model(*__UpperCamelCase   )

					def    lowercase__      (	self					:     str   ):
												lowerCamelCase_							  =					self.model_tester.prepare_config_and_inputs()
												for type in ["absolute", "relative_key", "relative_key_query"]:
																			lowerCamelCase_							  =					type
																			self.model_tester.create_and_check_model(*__UpperCamelCase   )

					def    lowercase__      (	self					:     Optional[Any]   ):
												lowerCamelCase_							  =					self.model_tester.prepare_config_and_inputs()
												self.model_tester.create_and_check_for_token_classification(*__UpperCamelCase   )

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



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





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

					def    lowercase__      (	self					:     Optional[int]   ):
												lowerCamelCase_							  =					LiltModel.from_pretrained("""SCUT-DLVCLab/lilt-roberta-en-base"""   ).to(__UpperCamelCase   )

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

												# forward pass
												with torch.no_grad():
																			lowerCamelCase_							  =					model(input_ids=__UpperCamelCase   ,					bbox=__UpperCamelCase   )

												lowerCamelCase_							  =					torch.Size([1, 2, 7_6_8]   )
												lowerCamelCase_							  =					torch.tensor(
												    [[-0.0653, 0.0950, -0.0061], [-0.0545, 0.0926, -0.0324]]   ,					device=__UpperCamelCase   ,					)

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







import functools
import operator

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


lowercase        =  logging.get_logger(__name__)

lowercase        =  {
    '''facebook/wav2vec2-base-960h''': '''https://huggingface.co/facebook/wav2vec2-base-960h/resolve/main/config.json''',
    # See all Wav2Vec2 models at https://huggingface.co/models?filter=wav2vec2
}





class       __A(				UpperCAmelCase       ):
					SCREAMING_SNAKE_CASE			    =    '''wav2vec2'''

					def __init__(	self					:     Tuple   ,					__UpperCamelCase					:     Optional[int]=3_2   ,					__UpperCamelCase					:     List[str]=7_6_8   ,					__UpperCamelCase					:     Tuple=1_2   ,					__UpperCamelCase					:     str=1_2   ,					__UpperCamelCase					:     Any=3_0_7_2   ,					__UpperCamelCase					:     Tuple="gelu"   ,					__UpperCamelCase					:     Tuple=0.1   ,					__UpperCamelCase					:     Any=0.1   ,					__UpperCamelCase					:     List[str]=0.1   ,					__UpperCamelCase					:     List[Any]=0.0   ,					__UpperCamelCase					:     str=0.0   ,					__UpperCamelCase					:     str=0.1   ,					__UpperCamelCase					:     List[Any]=0.1   ,					__UpperCamelCase					:     Dict=0.02   ,					__UpperCamelCase					:     int=1E-5   ,					__UpperCamelCase					:     Dict="group"   ,					__UpperCamelCase					:     Tuple="gelu"   ,					__UpperCamelCase					:     Optional[int]=(5_1_2, 5_1_2, 5_1_2, 5_1_2, 5_1_2, 5_1_2, 5_1_2)   ,					__UpperCamelCase					:     Union[str, Any]=(5, 2, 2, 2, 2, 2, 2)   ,					__UpperCamelCase					:     int=(1_0, 3, 3, 3, 3, 2, 2)   ,					__UpperCamelCase					:     List[Any]=False   ,					__UpperCamelCase					:     Tuple=1_2_8   ,					__UpperCamelCase					:     Any=1_6   ,					__UpperCamelCase					:     List[str]=False   ,					__UpperCamelCase					:     List[str]=True   ,					__UpperCamelCase					:     Dict=0.05   ,					__UpperCamelCase					:     Union[str, Any]=1_0   ,					__UpperCamelCase					:     int=2   ,					__UpperCamelCase					:     List[Any]=0.0   ,					__UpperCamelCase					:     List[Any]=1_0   ,					__UpperCamelCase					:     List[str]=0   ,					__UpperCamelCase					:     Optional[Any]=3_2_0   ,					__UpperCamelCase					:     Optional[int]=2   ,					__UpperCamelCase					:     List[Any]=0.1   ,					__UpperCamelCase					:     int=1_0_0   ,					__UpperCamelCase					:     int=2_5_6   ,					__UpperCamelCase					:     str=2_5_6   ,					__UpperCamelCase					:     Dict=0.1   ,					__UpperCamelCase					:     Union[str, Any]="sum"   ,					__UpperCamelCase					:     Optional[int]=False   ,					__UpperCamelCase					:     Optional[int]=False   ,					__UpperCamelCase					:     Dict=2_5_6   ,					__UpperCamelCase					:     List[Any]=(5_1_2, 5_1_2, 5_1_2, 5_1_2, 1_5_0_0)   ,					__UpperCamelCase					:     int=(5, 3, 3, 1, 1)   ,					__UpperCamelCase					:     Optional[Any]=(1, 2, 3, 1, 1)   ,					__UpperCamelCase					:     Dict=5_1_2   ,					__UpperCamelCase					:     Any=0   ,					__UpperCamelCase					:     Union[str, Any]=1   ,					__UpperCamelCase					:     List[str]=2   ,					__UpperCamelCase					:     List[str]=False   ,					__UpperCamelCase					:     int=3   ,					__UpperCamelCase					:     Dict=2   ,					__UpperCamelCase					:     Optional[Any]=3   ,					__UpperCamelCase					:     Optional[int]=None   ,					__UpperCamelCase					:     Tuple=None   ,					**__UpperCamelCase					:     Tuple   ,					):
												super().__init__(**__UpperCamelCase   ,					pad_token_id=__UpperCamelCase   ,					bos_token_id=__UpperCamelCase   ,					eos_token_id=__UpperCamelCase   )
												lowerCamelCase_							  =					hidden_size
												lowerCamelCase_							  =					feat_extract_norm
												lowerCamelCase_							  =					feat_extract_activation
												lowerCamelCase_							  =					list(__UpperCamelCase   )
												lowerCamelCase_							  =					list(__UpperCamelCase   )
												lowerCamelCase_							  =					list(__UpperCamelCase   )
												lowerCamelCase_							  =					conv_bias
												lowerCamelCase_							  =					num_conv_pos_embeddings
												lowerCamelCase_							  =					num_conv_pos_embedding_groups
												lowerCamelCase_							  =					len(self.conv_dim   )
												lowerCamelCase_							  =					num_hidden_layers
												lowerCamelCase_							  =					intermediate_size
												lowerCamelCase_							  =					hidden_act
												lowerCamelCase_							  =					num_attention_heads
												lowerCamelCase_							  =					hidden_dropout
												lowerCamelCase_							  =					attention_dropout
												lowerCamelCase_							  =					activation_dropout
												lowerCamelCase_							  =					feat_proj_dropout
												lowerCamelCase_							  =					final_dropout
												lowerCamelCase_							  =					layerdrop
												lowerCamelCase_							  =					layer_norm_eps
												lowerCamelCase_							  =					initializer_range
												lowerCamelCase_							  =					vocab_size
												lowerCamelCase_							  =					do_stable_layer_norm
												lowerCamelCase_							  =					use_weighted_layer_sum

												if (
												    (len(self.conv_stride   ) != self.num_feat_extract_layers)
												    or (len(self.conv_kernel   ) != self.num_feat_extract_layers)
												    or (len(self.conv_dim   ) != self.num_feat_extract_layers)
												):
																			raise ValueError(
																			    """Configuration for convolutional layers is incorrect. It is required that `len(config.conv_dim)` =="""
																			    """ `len(config.conv_stride)` == `len(config.conv_kernel)`, but is `len(config.conv_dim) ="""
																			    F''' {len(self.conv_dim   )}`, `len(config.conv_stride) = {len(self.conv_stride   )}`,'''
																			    F''' `len(config.conv_kernel) = {len(self.conv_kernel   )}`.'''   )

												# fine-tuning config parameters for SpecAugment: https://arxiv.org/abs/1904.08779
												lowerCamelCase_							  =					apply_spec_augment
												lowerCamelCase_							  =					mask_time_prob
												lowerCamelCase_							  =					mask_time_length
												lowerCamelCase_							  =					mask_time_min_masks
												lowerCamelCase_							  =					mask_feature_prob
												lowerCamelCase_							  =					mask_feature_length
												lowerCamelCase_							  =					mask_feature_min_masks

												# parameters for pretraining with codevector quantized representations
												lowerCamelCase_							  =					num_codevectors_per_group
												lowerCamelCase_							  =					num_codevector_groups
												lowerCamelCase_							  =					contrastive_logits_temperature
												lowerCamelCase_							  =					feat_quantizer_dropout
												lowerCamelCase_							  =					num_negatives
												lowerCamelCase_							  =					codevector_dim
												lowerCamelCase_							  =					proj_codevector_dim
												lowerCamelCase_							  =					diversity_loss_weight

												# ctc loss
												lowerCamelCase_							  =					ctc_loss_reduction
												lowerCamelCase_							  =					ctc_zero_infinity

												# adapter
												lowerCamelCase_							  =					add_adapter
												lowerCamelCase_							  =					adapter_kernel_size
												lowerCamelCase_							  =					adapter_stride
												lowerCamelCase_							  =					num_adapter_layers
												lowerCamelCase_							  =					output_hidden_size or hidden_size
												lowerCamelCase_							  =					adapter_attn_dim

												# SequenceClassification-specific parameter. Feel free to ignore for other classes.
												lowerCamelCase_							  =					classifier_proj_size

												# XVector-specific parameters. Feel free to ignore for other classes.
												lowerCamelCase_							  =					list(__UpperCamelCase   )
												lowerCamelCase_							  =					list(__UpperCamelCase   )
												lowerCamelCase_							  =					list(__UpperCamelCase   )
												lowerCamelCase_							  =					xvector_output_dim



					@property
					def    lowercase__      (	self					:     Dict   ):
												return functools.reduce(operator.mul   ,					self.conv_stride   ,					1   )





