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





	'''simple docstring'''



	def __init__(  self	:    Dict			,  lowercase	:    str			,  lowercase	:    List[str]=13			,  lowercase	:    Any=7			,  lowercase	:    Dict=True			,  lowercase	:    str=True			,  lowercase	:    List[Any]=True			,  lowercase	:    Any=True			,  lowercase	:    Tuple=99			,  lowercase	:    str=24			,  lowercase	:    str=2			,  lowercase	:    Any=6			,  lowercase	:    Dict=37			,  lowercase	:    List[str]="gelu"			,  lowercase	:    Dict=0.1			,  lowercase	:    Tuple=0.1			,  lowercase	:    Optional[Any]=512			,  lowercase	:    List[Any]=16			,  lowercase	:    str=2			,  lowercase	:    int=0.02			,  lowercase	:    List[Any]=3			,  lowercase	:    List[Any]=None			,  lowercase	:    int=1_000			,  ):


				'''simple docstring'''


				_snake_case		=			parent
				_snake_case		=			batch_size
				_snake_case		=			seq_length
				_snake_case		=			is_training
				_snake_case		=			use_input_mask
				_snake_case		=			use_token_type_ids
				_snake_case		=			use_labels
				_snake_case		=			vocab_size
				_snake_case		=			hidden_size
				_snake_case		=			num_hidden_layers
				_snake_case		=			num_attention_heads
				_snake_case		=			intermediate_size
				_snake_case		=			hidden_act
				_snake_case		=			hidden_dropout_prob
				_snake_case		=			attention_probs_dropout_prob
				_snake_case		=			max_position_embeddings
				_snake_case		=			type_vocab_size
				_snake_case		=			type_sequence_label_size
				_snake_case		=			initializer_range
				_snake_case		=			num_labels
				_snake_case		=			scope
				_snake_case		=			range_bbox



	def  A							(  self	:    List[Any]	):


				'''simple docstring'''


				_snake_case		=			ids_tensor([self.batch_size, self.seq_length]			,  self.vocab_size	)

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

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

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

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

				_snake_case		=			self.get_config()

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



	def  A							(  self	:    List[str]	):


				'''simple docstring'''


				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	:    str			,  lowercase	:    Tuple			,  lowercase	:    Tuple			,  lowercase	:    str			,  lowercase	:    Any			,  lowercase	:    Union[str, Any]			,  lowercase	:    List[str]			,  lowercase	:    str			,  ):


				'''simple docstring'''


				_snake_case		=			LiltModel(config=lowercase	)
				model.to(lowercase	)
				model.eval()
				_snake_case		=			model(lowercase			,  bbox=lowercase			,  attention_mask=lowercase			,  token_type_ids=lowercase	)
				_snake_case		=			model(lowercase			,  bbox=lowercase			,  token_type_ids=lowercase	)
				_snake_case		=			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	:    List[Any]			,  lowercase	:    int			,  lowercase	:    int			,  lowercase	:    Any			,  lowercase	:    Optional[int]			,  lowercase	:    Union[str, Any]			,  lowercase	:    Optional[Any]			,  lowercase	:    Optional[int]			,  ):


				'''simple docstring'''


				_snake_case		=			self.num_labels
				_snake_case		=			LiltForTokenClassification(config=lowercase	)
				model.to(lowercase	)
				model.eval()
				_snake_case		=			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	:    List[str]			,  lowercase	:    Union[str, Any]			,  lowercase	:    str			,  lowercase	:    Dict			,  lowercase	:    Optional[int]			,  lowercase	:    List[str]			,  lowercase	:    int			,  lowercase	:    int			,  ):


				'''simple docstring'''


				_snake_case		=			LiltForQuestionAnswering(config=lowercase	)
				model.to(lowercase	)
				model.eval()
				_snake_case		=			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	:    Optional[Any]	):


				'''simple docstring'''


				_snake_case		=			self.prepare_config_and_inputs()
				(
				    (
				    _snake_case
				)	,					(
				    _snake_case
				)	,					(
				    _snake_case
				)	,					(
				    _snake_case
				)	,					(
				    _snake_case
				)	,					(
				    _snake_case
				)	,					(
				    _snake_case
				)	,					
				)		=			config_and_inputs
				_snake_case		=			{
				    'input_ids': input_ids,
				    'bbox': bbox,
				    'token_type_ids': token_type_ids,
				    'attention_mask': input_mask,
				}
				return config, inputs_dict




@require_torch
class    SCREAMING_SNAKE_CASE__     (	UpperCAmelCase							,UpperCAmelCase							,UpperCAmelCase							,unittest.TestCase					):





	'''simple docstring'''



	_UpperCAmelCase       :		List[Any]       =					(
	    (
	        LiltModel,
	        LiltForSequenceClassification,
	        LiltForTokenClassification,
	        LiltForQuestionAnswering,
	    )
	    if is_torch_available()
	    else ()
	)
	_UpperCAmelCase       :		List[str]       =					(
	    {
	        "feature-extraction": LiltModel,
	        "question-answering": LiltForQuestionAnswering,
	        "text-classification": LiltForSequenceClassification,
	        "token-classification": LiltForTokenClassification,
	        "zero-shot": LiltForSequenceClassification,
	    }
	    if is_torch_available()
	    else {}
	)
	_UpperCAmelCase       :		Optional[Any]       =					False
	_UpperCAmelCase       :		Union[str, Any]       =					False



	def  A							(  self	:    Dict			,  lowercase	:    Dict			,  lowercase	:    Optional[int]			,  lowercase	:    Optional[int]			,  lowercase	:    List[str]			,  lowercase	:    Tuple	):


				'''simple docstring'''


				return True



	def  A							(  self	:    Optional[Any]	):


				'''simple docstring'''


				_snake_case		=			LiltModelTester(self	)
				_snake_case		=			ConfigTester(self			,  config_class=lowercase			,  hidden_size=37	)



	def  A							(  self	:    Any	):


				'''simple docstring'''


				self.config_tester.run_common_tests()



	def  A							(  self	:    Dict	):


				'''simple docstring'''


				_snake_case		=			self.model_tester.prepare_config_and_inputs()
				self.model_tester.create_and_check_model(*lowercase	)



	def  A							(  self	:    List[Any]	):


				'''simple docstring'''


				_snake_case		=			self.model_tester.prepare_config_and_inputs()
				for type in ["absolute", "relative_key", "relative_key_query"]:
							_snake_case		=			type
							self.model_tester.create_and_check_model(*lowercase	)



	def  A							(  self	:    Any	):


				'''simple docstring'''


				_snake_case		=			self.model_tester.prepare_config_and_inputs()
				self.model_tester.create_and_check_for_token_classification(*lowercase	)



	def  A							(  self	:    Any	):


				'''simple docstring'''


				_snake_case		=			self.model_tester.prepare_config_and_inputs()
				self.model_tester.create_and_check_for_question_answering(*lowercase	)





	@slow
	def  A							(  self	:    Union[str, Any]	):


				'''simple docstring'''


				for model_name in LILT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
							_snake_case		=			LiltModel.from_pretrained(lowercase	)
							self.assertIsNotNone(lowercase	)




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





	'''simple docstring'''



	def  A							(  self	:    Tuple	):


				'''simple docstring'''


				_snake_case		=			LiltModel.from_pretrained('SCUT-DLVCLab/lilt-roberta-en-base'	).to(lowercase	)

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

				# forward pass
				with torch.no_grad():
							_snake_case		=			model(input_ids=lowercase			,  bbox=lowercase	)

				_snake_case		=			torch.Size([1, 2, 768]	)
				_snake_case		=			torch.tensor(
				    [[-0.0653, 0.0950, -0.0061], [-0.0545, 0.0926, -0.0324]]			,  device=lowercase			,  )

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

from typing import Optional

from torch import nn

from .transformer_ad import TransformeraDModel, TransformeraDModelOutput




class    SCREAMING_SNAKE_CASE__     (	nn.Module					):





	'''simple docstring'''



	def __init__(  self	:    Optional[int]			,  lowercase	:    int = 16			,  lowercase	:    int = 88			,  lowercase	:    Optional[int] = None			,  lowercase	:    int = 1			,  lowercase	:    float = 0.0			,  lowercase	:    int = 32			,  lowercase	:    Optional[int] = None			,  lowercase	:    bool = False			,  lowercase	:    Optional[int] = None			,  lowercase	:    Optional[int] = None			,  lowercase	:    str = "geglu"			,  lowercase	:    Optional[int] = None			,  ):


				'''simple docstring'''


				super().__init__()
				_snake_case		=			nn.ModuleList(
				    [
				        TransformeraDModel(
				            num_attention_heads=lowercase			,  attention_head_dim=lowercase			,  in_channels=lowercase			,  num_layers=lowercase			,  dropout=lowercase			,  norm_num_groups=lowercase			,  cross_attention_dim=lowercase			,  attention_bias=lowercase			,  sample_size=lowercase			,  num_vector_embeds=lowercase			,  activation_fn=lowercase			,  num_embeds_ada_norm=lowercase			,  )
				        for _ in range(2	)
				    ]	)

				# Variables that can be set by a pipeline:

				# The ratio of transformer1 to transformer2's output states to be combined during inference
				_snake_case		=			0.5

				# The shape of `encoder_hidden_states` is expected to be
				# `(batch_size, condition_lengths[0]+condition_lengths[1], num_features)`
				_snake_case		=			[77, 257]

				# Which transformer to use to encode which condition.
				# E.g. `(1, 0)` means that we'll use `transformers[1](conditions[0])` and `transformers[0](conditions[1])`
				_snake_case		=			[1, 0]





	def  A							(  self	:    Optional[int]			,  lowercase	:    Optional[int]			,  lowercase	:    List[Any]			,  lowercase	:    List[str]=None			,  lowercase	:    Tuple=None			,  lowercase	:    Dict=None			,  lowercase	:    bool = True			,  ):


				'''simple docstring'''


				_snake_case		=			hidden_states

				_snake_case		=			[]
				_snake_case		=			0
				# attention_mask is not used yet
				for i in range(2	):
							# for each of the two transformers, pass the corresponding condition tokens
							_snake_case		=			encoder_hidden_states[:, tokens_start : tokens_start + self.condition_lengths[i]]
							_snake_case		=			self.transformer_index_for_condition[i]
							_snake_case		=			self.transformers[transformer_index](
							    lowercase			,  encoder_hidden_states=lowercase			,  timestep=lowercase			,  cross_attention_kwargs=lowercase			,  return_dict=lowercase			,  )[0]
							encoded_states.append(encoded_state - input_states	)
							tokens_start += self.condition_lengths[i]

				_snake_case		=			encoded_states[0] * self.mix_ratio + encoded_states[1] * (1 - self.mix_ratio)
				_snake_case		=			output_states + input_states

				if not return_dict:
							return (output_states,)

				return TransformeraDModelOutput(sample=lowercase	)