



from typing import Optional

import torch
import torch.utils.checkpoint
from torch import Tensor, nn
from torch.nn import BCEWithLogitsLoss, CrossEntropyLoss, MSELoss

from ...activations import ACTaFN
from ...modeling_outputs import (
    BackboneOutput,
    BaseModelOutputWithNoAttention,
    BaseModelOutputWithPoolingAndNoAttention,
    ImageClassifierOutputWithNoAttention,
)
from ...modeling_utils import PreTrainedModel
from ...utils import (
    add_code_sample_docstrings,
    add_start_docstrings,
    add_start_docstrings_to_model_forward,
    logging,
    replace_return_docstrings,
)
from ...utils.backbone_utils import BackboneMixin
from .configuration_resnet import ResNetConfig


_snake_case		=  logging.get_logger(__name__)

# General docstring
_snake_case		=  "ResNetConfig"

# Base docstring
_snake_case		=  "microsoft/resnet-50"
_snake_case		=  [1, 2048, 7, 7]

# Image classification docstring
_snake_case		=  "microsoft/resnet-50"
_snake_case		=  "tiger cat"

_snake_case		=  [
    "microsoft/resnet-50",
    # See all resnet models at https://huggingface.co/models?filter=resnet
]



class   UpperCAmelCase_    (       nn.Module):







							def __init__(   self,			__a,			__a,			__a = 3,			__a = 1,			__a = "relu"):

														'''simple docstring'''

														super().__init__()
														_lowerCAmelCase     :							Optional[Any]    =    nn.Convad(
														    __a,			__a,			kernel_size=__a,			stride=__a,			padding=kernel_size // 2,			bias=__a)
														_lowerCAmelCase     :							List[Any]    =    nn.BatchNormad(__a)
														_lowerCAmelCase     :							Optional[Any]    =    ACTaFN[activation] if activation is not None else nn.Identity()







							def    snake_case__      (   self,			__a):

														'''simple docstring'''

														_lowerCAmelCase     :							List[Any]    =    self.convolution(__a)
														_lowerCAmelCase     :							Dict    =    self.normalization(__a)
														_lowerCAmelCase     :							List[Any]    =    self.activation(__a)
														return hidden_state



class   UpperCAmelCase_    (       nn.Module):







							def __init__(   self,			__a):

														'''simple docstring'''

														super().__init__()
														_lowerCAmelCase     :							Optional[int]    =    ResNetConvLayer(
														    config.num_channels,			config.embedding_size,			kernel_size=7,			stride=2,			activation=config.hidden_act)
														_lowerCAmelCase     :							Dict    =    nn.MaxPoolad(kernel_size=3,			stride=2,			padding=1)
														_lowerCAmelCase     :							Tuple    =    config.num_channels







							def    snake_case__      (   self,			__a):

														'''simple docstring'''

														_lowerCAmelCase     :							Optional[Any]    =    pixel_values.shape[1]
														if num_channels != self.num_channels:
																					raise ValueError(
																					    "Make sure that the channel dimension of the pixel values match with the one set in the configuration.")
														_lowerCAmelCase     :							int    =    self.embedder(__a)
														_lowerCAmelCase     :							Tuple    =    self.pooler(__a)
														return embedding



class   UpperCAmelCase_    (       nn.Module):







							def __init__(   self,			__a,			__a,			__a = 2):

														'''simple docstring'''

														super().__init__()
														_lowerCAmelCase     :							Union[str, Any]    =    nn.Convad(__a,			__a,			kernel_size=1,			stride=__a,			bias=__a)
														_lowerCAmelCase     :							int    =    nn.BatchNormad(__a)







							def    snake_case__      (   self,			__a):

														'''simple docstring'''

														_lowerCAmelCase     :							Optional[Any]    =    self.convolution(__a)
														_lowerCAmelCase     :							Dict    =    self.normalization(__a)
														return hidden_state



class   UpperCAmelCase_    (       nn.Module):







							def __init__(   self,			__a,			__a,			__a = 1,			__a = "relu"):

														'''simple docstring'''

														super().__init__()
														_lowerCAmelCase     :							Tuple    =    in_channels != out_channels or stride != 1
														_lowerCAmelCase     :							Tuple    =    (
														    ResNetShortCut(__a,			__a,			stride=__a) if should_apply_shortcut else nn.Identity()
														)
														_lowerCAmelCase     :							str    =    nn.Sequential(
														    ResNetConvLayer(__a,			__a,			stride=__a),			ResNetConvLayer(__a,			__a,			activation=__a),			)
														_lowerCAmelCase     :							Union[str, Any]    =    ACTaFN[activation]







							def    snake_case__      (   self,			__a):

														'''simple docstring'''

														_lowerCAmelCase     :							int    =    hidden_state
														_lowerCAmelCase     :							Dict    =    self.layer(__a)
														_lowerCAmelCase     :							Optional[int]    =    self.shortcut(__a)
														hidden_state += residual
														_lowerCAmelCase     :							Tuple    =    self.activation(__a)
														return hidden_state



class   UpperCAmelCase_    (       nn.Module):







							def __init__(   self,			__a,			__a,			__a = 1,			__a = "relu",			__a = 4):

														'''simple docstring'''

														super().__init__()
														_lowerCAmelCase     :							int    =    in_channels != out_channels or stride != 1
														_lowerCAmelCase     :							str    =    out_channels // reduction
														_lowerCAmelCase     :							Optional[int]    =    (
														    ResNetShortCut(__a,			__a,			stride=__a) if should_apply_shortcut else nn.Identity()
														)
														_lowerCAmelCase     :							Tuple    =    nn.Sequential(
														    ResNetConvLayer(__a,			__a,			kernel_size=1),			ResNetConvLayer(__a,			__a,			stride=__a),			ResNetConvLayer(__a,			__a,			kernel_size=1,			activation=__a),			)
														_lowerCAmelCase     :							Optional[int]    =    ACTaFN[activation]







							def    snake_case__      (   self,			__a):

														'''simple docstring'''

														_lowerCAmelCase     :							Any    =    hidden_state
														_lowerCAmelCase     :							Any    =    self.layer(__a)
														_lowerCAmelCase     :							str    =    self.shortcut(__a)
														hidden_state += residual
														_lowerCAmelCase     :							int    =    self.activation(__a)
														return hidden_state



class   UpperCAmelCase_    (       nn.Module):







							def __init__(   self,			__a,			__a,			__a,			__a = 2,			__a = 2,			):

														'''simple docstring'''

														super().__init__()

														_lowerCAmelCase     :							List[str]    =    ResNetBottleNeckLayer if config.layer_type == "bottleneck" else ResNetBasicLayer

														_lowerCAmelCase     :							Dict    =    nn.Sequential(
														    # downsampling is done in the first layer with stride of 2
														    layer(__a,			__a,			stride=__a,			activation=config.hidden_act),			*[layer(__a,			__a,			activation=config.hidden_act) for _ in range(depth - 1)],			)







							def    snake_case__      (   self,			__a):

														'''simple docstring'''

														_lowerCAmelCase     :							Tuple    =    input
														for layer in self.layers:
																					_lowerCAmelCase     :							Tuple    =    layer(__a)
														return hidden_state



class   UpperCAmelCase_    (       nn.Module):







							def __init__(   self,			__a):

														'''simple docstring'''

														super().__init__()
														_lowerCAmelCase     :							Any    =    nn.ModuleList([])
														# based on `downsample_in_first_stage` the first layer of the first stage may or may not downsample the input
														self.stages.append(
														    ResNetStage(
														        __a,			config.embedding_size,			config.hidden_sizes[0],			stride=2 if config.downsample_in_first_stage else 1,			depth=config.depths[0],			))
														_lowerCAmelCase     :							Union[str, Any]    =    zip(config.hidden_sizes,			config.hidden_sizes[1:])
														for (in_channels, out_channels), depth in zip(__a,			config.depths[1:]):
																					self.stages.append(ResNetStage(__a,			__a,			__a,			depth=__a))







							def    snake_case__      (   self,			__a,			__a = False,			__a = True):

														'''simple docstring'''

														_lowerCAmelCase     :							Optional[int]    =    () if output_hidden_states else None

														for stage_module in self.stages:
																					if output_hidden_states:
																												_lowerCAmelCase     :							Optional[Any]    =    hidden_states + (hidden_state,)

																					_lowerCAmelCase     :							List[str]    =    stage_module(__a)

														if output_hidden_states:
																					_lowerCAmelCase     :							str    =    hidden_states + (hidden_state,)

														if not return_dict:
																					return tuple(v for v in [hidden_state, hidden_states] if v is not None)

														return BaseModelOutputWithNoAttention(
														    last_hidden_state=__a,			hidden_states=__a,			)



class   UpperCAmelCase_    (       a):
							lowerCamelCase__   			=		ResNetConfig
							lowerCamelCase__   			=		'resnet'
							lowerCamelCase__   			=		'pixel_values'
							lowerCamelCase__   			=		True







							def    snake_case__      (   self,			__a):

														'''simple docstring'''

														if isinstance(__a,			nn.Convad):
																					nn.init.kaiming_normal_(module.weight,			mode="fan_out",			nonlinearity="relu")
														elif isinstance(__a,			(nn.BatchNormad, nn.GroupNorm)):
																					nn.init.constant_(module.weight,			1)
																					nn.init.constant_(module.bias,			0)







							def    snake_case__      (   self,			__a,			__a=False):

														'''simple docstring'''

														if isinstance(__a,			__a):
																					_lowerCAmelCase     :							Tuple    =    value



_snake_case		=  R"\n    This model is a PyTorch [torch.nn.Module](https://pytorch.org/docs/stable/nn.html#torch.nn.Module) subclass. Use it\n    as a regular PyTorch Module and refer to the PyTorch documentation for all matter related to general usage and\n    behavior.\n\n    Parameters:\n        config ([`ResNetConfig`]): Model configuration class with all the parameters of the model.\n            Initializing with a config file does not load the weights associated with the model, only the\n            configuration. Check out the [`~PreTrainedModel.from_pretrained`] method to load the model weights.\n"

_snake_case		=  R"\n    Args:\n        pixel_values (`torch.FloatTensor` of shape `(batch_size, num_channels, height, width)`):\n            Pixel values. Pixel values can be obtained using [`AutoImageProcessor`]. See\n            [`ConvNextImageProcessor.__call__`] for details.\n\n        output_hidden_states (`bool`, *optional*):\n            Whether or not to return the hidden states of all layers. See `hidden_states` under returned tensors for\n            more detail.\n        return_dict (`bool`, *optional*):\n            Whether or not to return a [`~utils.ModelOutput`] instead of a plain tuple.\n"



@add_start_docstrings(
    'The bare ResNet model outputting raw features without any specific head on top.'   ,					a   ,					)
class   UpperCAmelCase_    (       a):







							def __init__(   self,			__a):

														'''simple docstring'''

														super().__init__(__a)
														_lowerCAmelCase     :							Tuple    =    config
														_lowerCAmelCase     :							List[str]    =    ResNetEmbeddings(__a)
														_lowerCAmelCase     :							List[str]    =    ResNetEncoder(__a)
														_lowerCAmelCase     :							Any    =    nn.AdaptiveAvgPoolad((1, 1))
														# Initialize weights and apply final processing
														self.post_init()







							@add_start_docstrings_to_model_forward(__a)
							@add_code_sample_docstrings(
							    checkpoint=_CHECKPOINT_FOR_DOC,			output_type=__a,			config_class=_CONFIG_FOR_DOC,			modality="vision",			expected_output=_EXPECTED_OUTPUT_SHAPE,			)
							def    snake_case__      (   self,			__a,			__a = None,			__a = None):

														'''simple docstring'''

														_lowerCAmelCase     :							Union[str, Any]    =    (
														    output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states
														)
														_lowerCAmelCase     :							List[str]    =    return_dict if return_dict is not None else self.config.use_return_dict

														_lowerCAmelCase     :							List[str]    =    self.embedder(__a)

														_lowerCAmelCase     :							int    =    self.encoder(
														    __a,			output_hidden_states=__a,			return_dict=__a)

														_lowerCAmelCase     :							int    =    encoder_outputs[0]

														_lowerCAmelCase     :							List[Any]    =    self.pooler(__a)

														if not return_dict:
																					return (last_hidden_state, pooled_output) + encoder_outputs[1:]

														return BaseModelOutputWithPoolingAndNoAttention(
														    last_hidden_state=__a,			pooler_output=__a,			hidden_states=encoder_outputs.hidden_states,			)



@add_start_docstrings(
    '\n    ResNet Model with an image classification head on top (a linear layer on top of the pooled features), e.g. for\n    ImageNet.\n    '   ,					a   ,					)
class   UpperCAmelCase_    (       a):







							def __init__(   self,			__a):

														'''simple docstring'''

														super().__init__(__a)
														_lowerCAmelCase     :							Tuple    =    config.num_labels
														_lowerCAmelCase     :							str    =    ResNetModel(__a)
														# classification head
														_lowerCAmelCase     :							str    =    nn.Sequential(
														    nn.Flatten(),			nn.Linear(config.hidden_sizes[-1],			config.num_labels) if config.num_labels > 0 else nn.Identity(),			)
														# initialize weights and apply final processing
														self.post_init()







							@add_start_docstrings_to_model_forward(__a)
							@add_code_sample_docstrings(
							    checkpoint=_IMAGE_CLASS_CHECKPOINT,			output_type=__a,			config_class=_CONFIG_FOR_DOC,			expected_output=_IMAGE_CLASS_EXPECTED_OUTPUT,			)
							def    snake_case__      (   self,			__a = None,			__a = None,			__a = None,			__a = None,			):

														'''simple docstring'''

														_lowerCAmelCase     :							List[Any]    =    return_dict if return_dict is not None else self.config.use_return_dict

														_lowerCAmelCase     :							List[Any]    =    self.resnet(__a,			output_hidden_states=__a,			return_dict=__a)

														_lowerCAmelCase     :							Tuple    =    outputs.pooler_output if return_dict else outputs[1]

														_lowerCAmelCase     :							int    =    self.classifier(__a)

														_lowerCAmelCase     :							Optional[Any]    =    None

														if labels is not None:
																					if self.config.problem_type is None:
																												if self.num_labels == 1:
																																			_lowerCAmelCase     :							Optional[int]    =    "regression"
																												elif self.num_labels > 1 and (labels.dtype == torch.long or labels.dtype == torch.int):
																																			_lowerCAmelCase     :							Optional[Any]    =    "single_label_classification"
																												else:
																																			_lowerCAmelCase     :							Tuple    =    "multi_label_classification"
																					if self.config.problem_type == "regression":
																												_lowerCAmelCase     :							Optional[int]    =    MSELoss()
																												if self.num_labels == 1:
																																			_lowerCAmelCase     :							int    =    loss_fct(logits.squeeze(),			labels.squeeze())
																												else:
																																			_lowerCAmelCase     :							Optional[Any]    =    loss_fct(__a,			__a)
																					elif self.config.problem_type == "single_label_classification":
																												_lowerCAmelCase     :							List[str]    =    CrossEntropyLoss()
																												_lowerCAmelCase     :							Any    =    loss_fct(logits.view(-1,			self.num_labels),			labels.view(-1))
																					elif self.config.problem_type == "multi_label_classification":
																												_lowerCAmelCase     :							Any    =    BCEWithLogitsLoss()
																												_lowerCAmelCase     :							str    =    loss_fct(__a,			__a)

														if not return_dict:
																					_lowerCAmelCase     :							Dict    =    (logits,) + outputs[2:]
																					return (loss,) + output if loss is not None else output

														return ImageClassifierOutputWithNoAttention(loss=__a,			logits=__a,			hidden_states=outputs.hidden_states)



@add_start_docstrings(
    '\n    ResNet backbone, to be used with frameworks like DETR and MaskFormer.\n    '   ,					a   ,					)
class   UpperCAmelCase_    (       a   ,					a):







							def __init__(   self,			__a):

														'''simple docstring'''

														super().__init__(__a)
														super()._init_backbone(__a)

														_lowerCAmelCase     :							Dict    =    [config.embedding_size] + config.hidden_sizes
														_lowerCAmelCase     :							List[Any]    =    ResNetEmbeddings(__a)
														_lowerCAmelCase     :							Union[str, Any]    =    ResNetEncoder(__a)

														# initialize weights and apply final processing
														self.post_init()







							@add_start_docstrings_to_model_forward(__a)
							@replace_return_docstrings(output_type=__a,			config_class=_CONFIG_FOR_DOC)
							def    snake_case__      (   self,			__a,			__a = None,			__a = None):

														'''simple docstring'''

														_lowerCAmelCase     :							Tuple    =    return_dict if return_dict is not None else self.config.use_return_dict
														_lowerCAmelCase     :							Optional[Any]    =    (
														    output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states
														)

														_lowerCAmelCase     :							Union[str, Any]    =    self.embedder(__a)

														_lowerCAmelCase     :							Union[str, Any]    =    self.encoder(__a,			output_hidden_states=__a,			return_dict=__a)

														_lowerCAmelCase     :							Tuple    =    outputs.hidden_states

														_lowerCAmelCase     :							Dict    =    ()
														for idx, stage in enumerate(self.stage_names):
																					if stage in self.out_features:
																												feature_maps += (hidden_states[idx],)

														if not return_dict:
																					_lowerCAmelCase     :							str    =    (feature_maps,)
																					if output_hidden_states:
																												output += (outputs.hidden_states,)
																					return output

														return BackboneOutput(
														    feature_maps=__a,			hidden_states=outputs.hidden_states if output_hidden_states else None,			attentions=__a,			)





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







							def __init__(   self,			__a,			__a=13,			__a=7,			__a=True,			__a=True,			__a=True,			__a=True,			__a=99,			__a=24,			__a=2,			__a=6,			__a=37,			__a="gelu",			__a=0.1,			__a=0.1,			__a=512,			__a=16,			__a=2,			__a=0.02,			__a=3,			__a=None,			__a=1000,			):

														'''simple docstring'''

														_lowerCAmelCase     :							Tuple    =    parent
														_lowerCAmelCase     :							List[str]    =    batch_size
														_lowerCAmelCase     :							int    =    seq_length
														_lowerCAmelCase     :							Optional[int]    =    is_training
														_lowerCAmelCase     :							Dict    =    use_input_mask
														_lowerCAmelCase     :							List[str]    =    use_token_type_ids
														_lowerCAmelCase     :							str    =    use_labels
														_lowerCAmelCase     :							Optional[Any]    =    vocab_size
														_lowerCAmelCase     :							Tuple    =    hidden_size
														_lowerCAmelCase     :							List[Any]    =    num_hidden_layers
														_lowerCAmelCase     :							Optional[Any]    =    num_attention_heads
														_lowerCAmelCase     :							Any    =    intermediate_size
														_lowerCAmelCase     :							List[str]    =    hidden_act
														_lowerCAmelCase     :							Union[str, Any]    =    hidden_dropout_prob
														_lowerCAmelCase     :							Any    =    attention_probs_dropout_prob
														_lowerCAmelCase     :							int    =    max_position_embeddings
														_lowerCAmelCase     :							Optional[int]    =    type_vocab_size
														_lowerCAmelCase     :							Optional[Any]    =    type_sequence_label_size
														_lowerCAmelCase     :							List[str]    =    initializer_range
														_lowerCAmelCase     :							List[Any]    =    num_labels
														_lowerCAmelCase     :							Tuple    =    scope
														_lowerCAmelCase     :							str    =    range_bbox







							def    snake_case__      (   self):

														'''simple docstring'''

														_lowerCAmelCase     :							Dict    =    ids_tensor([self.batch_size, self.seq_length],			self.vocab_size)

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

														_lowerCAmelCase     :							Optional[Any]    =    None
														if self.use_input_mask:
																					_lowerCAmelCase     :							Union[str, Any]    =    ids_tensor([self.batch_size, self.seq_length],			vocab_size=2)

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

														_lowerCAmelCase     :							Optional[int]    =    None
														_lowerCAmelCase     :							Optional[Any]    =    None
														if self.use_labels:
																					_lowerCAmelCase     :							Optional[Any]    =    ids_tensor([self.batch_size],			self.type_sequence_label_size)
																					_lowerCAmelCase     :							Any    =    ids_tensor([self.batch_size, self.seq_length],			self.num_labels)

														_lowerCAmelCase     :							Optional[int]    =    self.get_config()

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







							def    snake_case__      (   self):

														'''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    snake_case__      (   self,			__a,			__a,			__a,			__a,			__a,			__a,			__a,			):

														'''simple docstring'''

														_lowerCAmelCase     :							Union[str, Any]    =    LiltModel(config=__a)
														model.to(__a)
														model.eval()
														_lowerCAmelCase     :							Dict    =    model(__a,			bbox=__a,			attention_mask=__a,			token_type_ids=__a)
														_lowerCAmelCase     :							str    =    model(__a,			bbox=__a,			token_type_ids=__a)
														_lowerCAmelCase     :							List[Any]    =    model(__a,			bbox=__a)

														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,			__a,			__a,			__a,			__a,			__a,			__a,			__a,			):

														'''simple docstring'''

														_lowerCAmelCase     :							List[Any]    =    self.num_labels
														_lowerCAmelCase     :							Optional[Any]    =    LiltForTokenClassification(config=__a)
														model.to(__a)
														model.eval()
														_lowerCAmelCase     :							Dict    =    model(
														    __a,			bbox=__a,			attention_mask=__a,			token_type_ids=__a,			labels=__a)
														self.parent.assertEqual(result.logits.shape,			(self.batch_size, self.seq_length, self.num_labels))







							def    snake_case__      (   self,			__a,			__a,			__a,			__a,			__a,			__a,			__a,			):

														'''simple docstring'''

														_lowerCAmelCase     :							Optional[int]    =    LiltForQuestionAnswering(config=__a)
														model.to(__a)
														model.eval()
														_lowerCAmelCase     :							Tuple    =    model(
														    __a,			bbox=__a,			attention_mask=__a,			token_type_ids=__a,			start_positions=__a,			end_positions=__a,			)
														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):

														'''simple docstring'''

														_lowerCAmelCase     :							Optional[Any]    =    self.prepare_config_and_inputs()
														(
														    (
														    _lowerCAmelCase
														)   ,    (
														    _lowerCAmelCase
														)   ,    (
														    _lowerCAmelCase
														)   ,    (
														    _lowerCAmelCase
														)   ,    (
														    _lowerCAmelCase
														)   ,    (
														    _lowerCAmelCase
														)   ,    (
														    _lowerCAmelCase
														)   ,    
														)     :							Dict    =    config_and_inputs
														_lowerCAmelCase     :							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   UpperCAmelCase_    (       a   ,					a   ,					a   ,					unittest.TestCase):
							lowerCamelCase__   			=		(
							    (
							        LiltModel,
							        LiltForSequenceClassification,
							        LiltForTokenClassification,
							        LiltForQuestionAnswering,
							    )
							    if is_torch_available()
							    else ()
							)
							lowerCamelCase__   			=		(
							    {
							        'feature-extraction': LiltModel,
							        'question-answering': LiltForQuestionAnswering,
							        'text-classification': LiltForSequenceClassification,
							        'token-classification': LiltForTokenClassification,
							        'zero-shot': LiltForSequenceClassification,
							    }
							    if is_torch_available()
							    else {}
							)
							lowerCamelCase__   			=		False
							lowerCamelCase__   			=		False







							def    snake_case__      (   self,			__a,			__a,			__a,			__a,			__a):

														'''simple docstring'''

														return True







							def    snake_case__      (   self):

														'''simple docstring'''

														_lowerCAmelCase     :							Optional[Any]    =    LiltModelTester(self)
														_lowerCAmelCase     :							Union[str, Any]    =    ConfigTester(self,			config_class=__a,			hidden_size=37)







							def    snake_case__      (   self):

														'''simple docstring'''

														self.config_tester.run_common_tests()







							def    snake_case__      (   self):

														'''simple docstring'''

														_lowerCAmelCase     :							Dict    =    self.model_tester.prepare_config_and_inputs()
														self.model_tester.create_and_check_model(*__a)







							def    snake_case__      (   self):

														'''simple docstring'''

														_lowerCAmelCase     :							int    =    self.model_tester.prepare_config_and_inputs()
														for type in ["absolute", "relative_key", "relative_key_query"]:
																					_lowerCAmelCase     :							Any    =    type
																					self.model_tester.create_and_check_model(*__a)







							def    snake_case__      (   self):

														'''simple docstring'''

														_lowerCAmelCase     :							int    =    self.model_tester.prepare_config_and_inputs()
														self.model_tester.create_and_check_for_token_classification(*__a)







							def    snake_case__      (   self):

														'''simple docstring'''

														_lowerCAmelCase     :							Optional[int]    =    self.model_tester.prepare_config_and_inputs()
														self.model_tester.create_and_check_for_question_answering(*__a)







							@slow
							def    snake_case__      (   self):

														'''simple docstring'''

														for model_name in LILT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
																					_lowerCAmelCase     :							str    =    LiltModel.from_pretrained(__a)
																					self.assertIsNotNone(__a)



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







							def    snake_case__      (   self):

														'''simple docstring'''

														_lowerCAmelCase     :							Dict    =    LiltModel.from_pretrained("SCUT-DLVCLab/lilt-roberta-en-base").to(__a)

														_lowerCAmelCase     :							Any    =    torch.tensor([[1, 2]],			device=__a)
														_lowerCAmelCase     :							str    =    torch.tensor([[[1, 2, 3, 4], [5, 6, 7, 8]]],			device=__a)

														# forward pass
														with torch.no_grad():
																					_lowerCAmelCase     :							Optional[Any]    =    model(input_ids=__a,			bbox=__a)

														_lowerCAmelCase     :							Optional[int]    =    torch.Size([1, 2, 768])
														_lowerCAmelCase     :							List[str]    =    torch.tensor(
														    [[-0.0_653, 0.0_950, -0.0_061], [-0.0_545, 0.0_926, -0.0_324]],			device=__a,			)

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