




"""simple docstring"""





import logging

import torch
from torch import nn
from torch.nn import CrossEntropyLoss, MSELoss

from transformers.file_utils import add_start_docstrings, add_start_docstrings_to_model_forward
from transformers.models.bert.modeling_bert import (
    BERT_INPUTS_DOCSTRING,
    BERT_START_DOCSTRING,
    BertEncoder,
    BertModel,
    BertPreTrainedModel,
)


A   =       logging.getLogger(__name__)



class      a__				(	__magic_name__   ):


			def       a_     (      self      :    int      ,  UpperCamelCase_      :    List[Any]      ,  UpperCamelCase_      :    Tuple      ,  UpperCamelCase_      :    Optional[Any]=None      ,  UpperCamelCase_      :    List[str]=None):


						"""simple docstring"""




						__UpperCAmelCase	: List[Any]		=					self.layer[current_layer](UpperCamelCase_      ,  UpperCamelCase_      ,  head_mask[current_layer])

						__UpperCAmelCase	: List[Any]		=					layer_outputs[0]

						return hidden_states






@add_start_docstrings(
    "The bare Bert Model transformer with PABEE outputting raw hidden-states without any specific head on top."							,    __magic_name__							,    )
class      a__				(	__magic_name__   ):


			def __init__(      self      :    int      ,  UpperCamelCase_      :    Union[str, Any]):


						"""simple docstring"""




						super().__init__(UpperCamelCase_)

						__UpperCAmelCase	: Union[str, Any]		=					BertEncoderWithPabee(UpperCamelCase_)

						self.init_weights()
						__UpperCAmelCase	: Any		=					0
						__UpperCAmelCase	: Optional[int]		=					0
						__UpperCAmelCase	: Union[str, Any]		=					0

						__UpperCAmelCase	: Union[str, Any]		=					0


			def       a_     (      self      :    Union[str, Any]      ,  UpperCamelCase_      :    str):


						"""simple docstring"""




						__UpperCAmelCase	: int		=					threshold


			def       a_     (      self      :    Union[str, Any]      ,  UpperCamelCase_      :    List[str]):


						"""simple docstring"""




						__UpperCAmelCase	: Optional[Any]		=					patience


			def       a_     (      self      :    int):


						"""simple docstring"""




						__UpperCAmelCase	: List[Any]		=					0
						__UpperCAmelCase	: Union[str, Any]		=					0


			def       a_     (      self      :    Union[str, Any]):


						"""simple docstring"""




						__UpperCAmelCase	: Any		=					self.inference_layers_num / self.inference_instances_num
						__UpperCAmelCase	: str		=					(
						    F"*** Patience = {self.patience} Avg. Inference Layers = {avg_inf_layers:.2f} Speed Up ="
						    F" {1 - avg_inf_layers / self.config.num_hidden_layers:.2f} ***"
						)
						print(UpperCamelCase_)


			@add_start_docstrings_to_model_forward(UpperCamelCase_)
			def       a_     (      self      :    Optional[int]      ,  UpperCamelCase_      :    Tuple=None      ,  UpperCamelCase_      :    int=None      ,  UpperCamelCase_      :    List[Any]=None      ,  UpperCamelCase_      :    Optional[int]=None      ,  UpperCamelCase_      :    List[Any]=None      ,  UpperCamelCase_      :    List[Any]=None      ,  UpperCamelCase_      :    Any=None      ,  UpperCamelCase_      :    Optional[Any]=None      ,  UpperCamelCase_      :    Tuple=None      ,  UpperCamelCase_      :    int=None      ,  UpperCamelCase_      :    Union[str, Any]=False      ,  ):


						"""simple docstring"""




						if input_ids is not None and inputs_embeds is not None:
									raise ValueError("You cannot specify both input_ids and inputs_embeds at the same time")
						elif input_ids is not None:
									__UpperCAmelCase	: Tuple		=					input_ids.size()
						elif inputs_embeds is not None:
									__UpperCAmelCase	: Optional[Any]		=					inputs_embeds.size()[:-1]
						else:
									raise ValueError("You have to specify either input_ids or inputs_embeds")

						__UpperCAmelCase	: int		=					input_ids.device if input_ids is not None else inputs_embeds.device

						if attention_mask is None:
									__UpperCAmelCase	: Union[str, Any]		=					torch.ones(UpperCamelCase_      ,  device=UpperCamelCase_)
						if token_type_ids is None:
									__UpperCAmelCase	: str		=					torch.zeros(UpperCamelCase_      ,  dtype=torch.long      ,  device=UpperCamelCase_)

						# We can provide a self-attention mask of dimensions [batch_size, from_seq_length, to_seq_length]
						# ourselves in which case we just need to make it broadcastable to all heads.
						__UpperCAmelCase	: torch.Tensor		=					self.get_extended_attention_mask(UpperCamelCase_      ,  UpperCamelCase_      ,  UpperCamelCase_)

						# If a 2D ou 3D attention mask is provided for the cross-attention
						# we need to make broadcastable to [batch_size, num_heads, seq_length, seq_length]
						if self.config.is_decoder and encoder_hidden_states is not None:
									__UpperCAmelCase	,     __UpperCAmelCase	,     __UpperCAmelCase	: str		=					encoder_hidden_states.size()
									__UpperCAmelCase	: Optional[Any]		=					(encoder_batch_size, encoder_sequence_length)
									if encoder_attention_mask is None:
												__UpperCAmelCase	: str		=					torch.ones(UpperCamelCase_      ,  device=UpperCamelCase_)
									__UpperCAmelCase	: Union[str, Any]		=					self.invert_attention_mask(UpperCamelCase_)
						else:
									__UpperCAmelCase	: int		=					None

						# Prepare head mask if needed
						# 1.0 in head_mask indicate we keep the head
						# attention_probs has shape bsz x n_heads x N x N
						# input head_mask has shape [num_heads] or [num_hidden_layers x num_heads]
						# and head_mask is converted to shape [num_hidden_layers x batch x num_heads x seq_length x seq_length]
						__UpperCAmelCase	: Any		=					self.get_head_mask(UpperCamelCase_      ,  self.config.num_hidden_layers)

						__UpperCAmelCase	: Tuple		=					self.embeddings(
						    input_ids=UpperCamelCase_      ,  position_ids=UpperCamelCase_      ,  token_type_ids=UpperCamelCase_      ,  inputs_embeds=UpperCamelCase_)
						__UpperCAmelCase	: Union[str, Any]		=					embedding_output

						if self.training:
									__UpperCAmelCase	: Dict		=					[]
									for i in range(self.config.num_hidden_layers):
												__UpperCAmelCase	: Union[str, Any]		=					self.encoder.adaptive_forward(
												    UpperCamelCase_      ,  current_layer=UpperCamelCase_      ,  attention_mask=UpperCamelCase_      ,  head_mask=UpperCamelCase_)

												__UpperCAmelCase	: List[Any]		=					self.pooler(UpperCamelCase_)
												__UpperCAmelCase	: Dict		=					output_layers[i](output_dropout(UpperCamelCase_))
												res.append(UpperCamelCase_)
						elif self.patience == 0:  # Use all layers for inference
									__UpperCAmelCase	: Union[str, Any]		=					self.encoder(
									    UpperCamelCase_      ,  attention_mask=UpperCamelCase_      ,  head_mask=UpperCamelCase_      ,  encoder_hidden_states=UpperCamelCase_      ,  encoder_attention_mask=UpperCamelCase_      ,  )
									__UpperCAmelCase	: Dict		=					self.pooler(encoder_outputs[0])
									__UpperCAmelCase	: Any		=					[output_layers[self.config.num_hidden_layers - 1](UpperCamelCase_)]
						else:
									__UpperCAmelCase	: List[str]		=					0
									__UpperCAmelCase	: Any		=					None
									__UpperCAmelCase	: Optional[int]		=					0
									for i in range(self.config.num_hidden_layers):
												calculated_layer_num += 1
												__UpperCAmelCase	: str		=					self.encoder.adaptive_forward(
												    UpperCamelCase_      ,  current_layer=UpperCamelCase_      ,  attention_mask=UpperCamelCase_      ,  head_mask=UpperCamelCase_)

												__UpperCAmelCase	: int		=					self.pooler(UpperCamelCase_)
												__UpperCAmelCase	: List[Any]		=					output_layers[i](UpperCamelCase_)
												if regression:
															__UpperCAmelCase	: Optional[Any]		=					logits.detach()
															if patient_result is not None:
																		__UpperCAmelCase	: Any		=					patient_result.detach()
															if (patient_result is not None) and torch.abs(patient_result - labels) < self.regression_threshold:
																		patient_counter += 1
															else:
																		__UpperCAmelCase	: List[str]		=					0
												else:
															__UpperCAmelCase	: Optional[int]		=					logits.detach().argmax(dim=1)
															if patient_result is not None:
																		__UpperCAmelCase	: Any		=					patient_result.detach().argmax(dim=1)
															if (patient_result is not None) and torch.all(labels.eq(UpperCamelCase_)):
																		patient_counter += 1
															else:
																		__UpperCAmelCase	: int		=					0

												__UpperCAmelCase	: Union[str, Any]		=					logits
												if patient_counter == self.patience:
															break
									__UpperCAmelCase	: List[Any]		=					[patient_result]
									self.inference_layers_num += calculated_layer_num
									self.inference_instances_num += 1

						return res






@add_start_docstrings(
    "Bert Model transformer with PABEE and a sequence classification/regression head on top (a linear layer on top of\n    the pooled output) e.g. for GLUE tasks. "							,    __magic_name__							,    )
class      a__				(	__magic_name__   ):


			def __init__(      self      :    Any      ,  UpperCamelCase_      :    List[Any]):


						"""simple docstring"""




						super().__init__(UpperCamelCase_)
						__UpperCAmelCase	: Optional[Any]		=					config.num_labels

						__UpperCAmelCase	: Optional[Any]		=					BertModelWithPabee(UpperCamelCase_)
						__UpperCAmelCase	: Dict		=					nn.Dropout(config.hidden_dropout_prob)
						__UpperCAmelCase	: List[str]		=					nn.ModuleList(
						    [nn.Linear(config.hidden_size      ,  self.config.num_labels) for _ in range(config.num_hidden_layers)])

						self.init_weights()


			@add_start_docstrings_to_model_forward(UpperCamelCase_)
			def       a_     (      self      :    Union[str, Any]      ,  UpperCamelCase_      :    int=None      ,  UpperCamelCase_      :    Tuple=None      ,  UpperCamelCase_      :    str=None      ,  UpperCamelCase_      :    Optional[Any]=None      ,  UpperCamelCase_      :    Union[str, Any]=None      ,  UpperCamelCase_      :    Dict=None      ,  UpperCamelCase_      :    Optional[int]=None      ,  ):


						"""simple docstring"""




						__UpperCAmelCase	: Optional[int]		=					self.bert(
						    input_ids=UpperCamelCase_      ,  attention_mask=UpperCamelCase_      ,  token_type_ids=UpperCamelCase_      ,  position_ids=UpperCamelCase_      ,  head_mask=UpperCamelCase_      ,  inputs_embeds=UpperCamelCase_      ,  output_dropout=self.dropout      ,  output_layers=self.classifiers      ,  regression=self.num_labels == 1      ,  )

						__UpperCAmelCase	: Dict		=					(logits[-1],)

						if labels is not None:
									__UpperCAmelCase	: Dict		=					None
									__UpperCAmelCase	: str		=					0
									for ix, logits_item in enumerate(UpperCamelCase_):
												if self.num_labels == 1:
															#  We are doing regression
															__UpperCAmelCase	: Union[str, Any]		=					MSELoss()
															__UpperCAmelCase	: List[Any]		=					loss_fct(logits_item.view(-1)      ,  labels.view(-1))
												else:
															__UpperCAmelCase	: List[str]		=					CrossEntropyLoss()
															__UpperCAmelCase	: str		=					loss_fct(logits_item.view(-1      ,  self.num_labels)      ,  labels.view(-1))
												if total_loss is None:
															__UpperCAmelCase	: Tuple		=					loss
												else:
															total_loss += loss * (ix + 1)
												total_weights += ix + 1
									__UpperCAmelCase	: Union[str, Any]		=					(total_loss / total_weights,) + outputs

						return outputs








"""simple docstring"""





from typing import Dict, List, Optional, Union

import numpy as np

from ...image_processing_utils import BaseImageProcessor, BatchFeature, get_size_dict
from ...image_transforms import (
    center_crop,
    get_resize_output_image_size,
    normalize,
    rescale,
    resize,
    to_channel_dimension_format,
)
from ...image_utils import (
    IMAGENET_STANDARD_MEAN,
    IMAGENET_STANDARD_STD,
    ChannelDimension,
    ImageInput,
    PILImageResampling,
    is_valid_image,
    to_numpy_array,
    valid_images,
)
from ...utils import TensorType, is_vision_available, logging


if is_vision_available():
					import PIL


A   =       logging.get_logger(__name__)

def 					_UpperCamelCase    (			UpperCamelCase							)			->			List[List[ImageInput]]:


			"""simple docstring"""
			if isinstance(UpperCamelCase				,       (list, tuple)							) and isinstance(videos[0]				,       (list, tuple)							) and is_valid_image(videos[0][0]							):
						return videos

			elif isinstance(UpperCamelCase				,       (list, tuple)							) and is_valid_image(videos[0]							):
						return [videos]

			elif is_valid_image(UpperCamelCase							):
						return [[videos]]

			raise ValueError(f"Could not make batched video from {videos}"							)





class      a__				(	__magic_name__   ):
			lowercase_    =	["pixel_values"]


			def __init__(      self      :    Tuple      ,  UpperCamelCase_      :    bool = True      ,  UpperCamelCase_      :    Dict[str, int] = None      ,  UpperCamelCase_      :    PILImageResampling = PILImageResampling.BILINEAR      ,  UpperCamelCase_      :    bool = True      ,  UpperCamelCase_      :    Dict[str, int] = None      ,  UpperCamelCase_      :    bool = True      ,  UpperCamelCase_      :    Union[int, float] = 1 / 255      ,  UpperCamelCase_      :    bool = True      ,  UpperCamelCase_      :    Optional[Union[float, List[float]]] = None      ,  UpperCamelCase_      :    Optional[Union[float, List[float]]] = None      ,  **UpperCamelCase_      :    List[str]      ,  ):


						"""simple docstring"""




						super().__init__(**UpperCamelCase_)
						__UpperCAmelCase	: Dict		=					size if size is not None else {"shortest_edge": 224}
						__UpperCAmelCase	: Union[str, Any]		=					get_size_dict(UpperCamelCase_      ,  default_to_square=UpperCamelCase_)
						__UpperCAmelCase	: Optional[int]		=					crop_size if crop_size is not None else {"height": 224, "width": 224}
						__UpperCAmelCase	: List[str]		=					get_size_dict(UpperCamelCase_      ,  param_name="crop_size")

						__UpperCAmelCase	: Optional[Any]		=					do_resize
						__UpperCAmelCase	: Union[str, Any]		=					size
						__UpperCAmelCase	: str		=					do_center_crop
						__UpperCAmelCase	: str		=					crop_size
						__UpperCAmelCase	: Any		=					resample
						__UpperCAmelCase	: List[Any]		=					do_rescale
						__UpperCAmelCase	: Union[str, Any]		=					rescale_factor
						__UpperCAmelCase	: List[Any]		=					do_normalize
						__UpperCAmelCase	: Dict		=					image_mean if image_mean is not None else IMAGENET_STANDARD_MEAN
						__UpperCAmelCase	: Tuple		=					image_std if image_std is not None else IMAGENET_STANDARD_STD


			def       a_     (      self      :    int      ,  UpperCamelCase_      :    np.ndarray      ,  UpperCamelCase_      :    Dict[str, int]      ,  UpperCamelCase_      :    PILImageResampling = PILImageResampling.BILINEAR      ,  UpperCamelCase_      :    Optional[Union[str, ChannelDimension]] = None      ,  **UpperCamelCase_      :    List[Any]      ,  ):


						"""simple docstring"""




						__UpperCAmelCase	: Optional[Any]		=					get_size_dict(UpperCamelCase_      ,  default_to_square=UpperCamelCase_)
						if "shortest_edge" in size:
									__UpperCAmelCase	: Dict		=					get_resize_output_image_size(UpperCamelCase_      ,  size["shortest_edge"]      ,  default_to_square=UpperCamelCase_)
						elif "height" in size and "width" in size:
									__UpperCAmelCase	: Dict		=					(size["height"], size["width"])
						else:
									raise ValueError(F"Size must have 'height' and 'width' or 'shortest_edge' as keys. Got {size.keys()}")
						return resize(UpperCamelCase_      ,  size=UpperCamelCase_      ,  resample=UpperCamelCase_      ,  data_format=UpperCamelCase_      ,  **UpperCamelCase_)


			def       a_     (      self      :    List[str]      ,  UpperCamelCase_      :    np.ndarray      ,  UpperCamelCase_      :    Dict[str, int]      ,  UpperCamelCase_      :    Optional[Union[str, ChannelDimension]] = None      ,  **UpperCamelCase_      :    Tuple      ,  ):


						"""simple docstring"""




						__UpperCAmelCase	: int		=					get_size_dict(UpperCamelCase_)
						if "height" not in size or "width" not in size:
									raise ValueError(F"Size must have 'height' and 'width' as keys. Got {size.keys()}")
						return center_crop(UpperCamelCase_      ,  size=(size["height"], size["width"])      ,  data_format=UpperCamelCase_      ,  **UpperCamelCase_)


			def       a_     (      self      :    Optional[int]      ,  UpperCamelCase_      :    np.ndarray      ,  UpperCamelCase_      :    Union[int, float]      ,  UpperCamelCase_      :    Optional[Union[str, ChannelDimension]] = None      ,  **UpperCamelCase_      :    Optional[int]      ,  ):


						"""simple docstring"""




						return rescale(UpperCamelCase_      ,  scale=UpperCamelCase_      ,  data_format=UpperCamelCase_      ,  **UpperCamelCase_)


			def       a_     (      self      :    List[Any]      ,  UpperCamelCase_      :    np.ndarray      ,  UpperCamelCase_      :    Union[float, List[float]]      ,  UpperCamelCase_      :    Union[float, List[float]]      ,  UpperCamelCase_      :    Optional[Union[str, ChannelDimension]] = None      ,  **UpperCamelCase_      :    Union[str, Any]      ,  ):


						"""simple docstring"""




						return normalize(UpperCamelCase_      ,  mean=UpperCamelCase_      ,  std=UpperCamelCase_      ,  data_format=UpperCamelCase_      ,  **UpperCamelCase_)


			def       a_     (      self      :    List[str]      ,  UpperCamelCase_      :    ImageInput      ,  UpperCamelCase_      :    bool = None      ,  UpperCamelCase_      :    Dict[str, int] = None      ,  UpperCamelCase_      :    PILImageResampling = None      ,  UpperCamelCase_      :    bool = None      ,  UpperCamelCase_      :    Dict[str, int] = None      ,  UpperCamelCase_      :    bool = None      ,  UpperCamelCase_      :    float = None      ,  UpperCamelCase_      :    bool = None      ,  UpperCamelCase_      :    Optional[Union[float, List[float]]] = None      ,  UpperCamelCase_      :    Optional[Union[float, List[float]]] = None      ,  UpperCamelCase_      :    Optional[ChannelDimension] = ChannelDimension.FIRST      ,  ):


						"""simple docstring"""




						if do_resize and size is None or resample is None:
									raise ValueError("Size and resample must be specified if do_resize is True.")

						if do_center_crop and crop_size is None:
									raise ValueError("Crop size must be specified if do_center_crop is True.")

						if do_rescale and rescale_factor is None:
									raise ValueError("Rescale factor must be specified if do_rescale is True.")

						if do_normalize and (image_mean is None or image_std is None):
									raise ValueError("Image mean and std must be specified if do_normalize is True.")

						# All transformations expect numpy arrays.
						__UpperCAmelCase	: List[str]		=					to_numpy_array(UpperCamelCase_)

						if do_resize:
									__UpperCAmelCase	: Tuple		=					self.resize(image=UpperCamelCase_      ,  size=UpperCamelCase_      ,  resample=UpperCamelCase_)

						if do_center_crop:
									__UpperCAmelCase	: str		=					self.center_crop(UpperCamelCase_      ,  size=UpperCamelCase_)

						if do_rescale:
									__UpperCAmelCase	: Union[str, Any]		=					self.rescale(image=UpperCamelCase_      ,  scale=UpperCamelCase_)

						if do_normalize:
									__UpperCAmelCase	: Optional[int]		=					self.normalize(image=UpperCamelCase_      ,  mean=UpperCamelCase_      ,  std=UpperCamelCase_)

						__UpperCAmelCase	: Any		=					to_channel_dimension_format(UpperCamelCase_      ,  UpperCamelCase_)
						return image


			def       a_     (      self      :    List[Any]      ,  UpperCamelCase_      :    ImageInput      ,  UpperCamelCase_      :    bool = None      ,  UpperCamelCase_      :    Dict[str, int] = None      ,  UpperCamelCase_      :    PILImageResampling = None      ,  UpperCamelCase_      :    bool = None      ,  UpperCamelCase_      :    Dict[str, int] = None      ,  UpperCamelCase_      :    bool = None      ,  UpperCamelCase_      :    float = None      ,  UpperCamelCase_      :    bool = None      ,  UpperCamelCase_      :    Optional[Union[float, List[float]]] = None      ,  UpperCamelCase_      :    Optional[Union[float, List[float]]] = None      ,  UpperCamelCase_      :    Optional[Union[str, TensorType]] = None      ,  UpperCamelCase_      :    ChannelDimension = ChannelDimension.FIRST      ,  **UpperCamelCase_      :    Optional[int]      ,  ):


						"""simple docstring"""




						__UpperCAmelCase	: Optional[Any]		=					do_resize if do_resize is not None else self.do_resize
						__UpperCAmelCase	: Optional[Any]		=					resample if resample is not None else self.resample
						__UpperCAmelCase	: Any		=					do_center_crop if do_center_crop is not None else self.do_center_crop
						__UpperCAmelCase	: str		=					do_rescale if do_rescale is not None else self.do_rescale
						__UpperCAmelCase	: str		=					rescale_factor if rescale_factor is not None else self.rescale_factor
						__UpperCAmelCase	: Tuple		=					do_normalize if do_normalize is not None else self.do_normalize
						__UpperCAmelCase	: str		=					image_mean if image_mean is not None else self.image_mean
						__UpperCAmelCase	: Optional[Any]		=					image_std if image_std is not None else self.image_std

						__UpperCAmelCase	: Tuple		=					size if size is not None else self.size
						__UpperCAmelCase	: str		=					get_size_dict(UpperCamelCase_      ,  default_to_square=UpperCamelCase_)
						__UpperCAmelCase	: Any		=					crop_size if crop_size is not None else self.crop_size
						__UpperCAmelCase	: int		=					get_size_dict(UpperCamelCase_      ,  param_name="crop_size")

						if not valid_images(UpperCamelCase_):
									raise ValueError(
									    "Invalid image type. Must be of type PIL.Image.Image, numpy.ndarray, "
									    "torch.Tensor, tf.Tensor or jax.ndarray.")

						__UpperCAmelCase	: Dict		=					make_batched(UpperCamelCase_)

						__UpperCAmelCase	: List[str]		=					[
						    [
						        self._preprocess_image(
						            image=UpperCamelCase_      ,  do_resize=UpperCamelCase_      ,  size=UpperCamelCase_      ,  resample=UpperCamelCase_      ,  do_center_crop=UpperCamelCase_      ,  crop_size=UpperCamelCase_      ,  do_rescale=UpperCamelCase_      ,  rescale_factor=UpperCamelCase_      ,  do_normalize=UpperCamelCase_      ,  image_mean=UpperCamelCase_      ,  image_std=UpperCamelCase_      ,  data_format=UpperCamelCase_      ,  )
						        for img in video
						    ]
						    for video in videos
						]

						__UpperCAmelCase	: int		=					{"pixel_values": videos}
						return BatchFeature(data=UpperCamelCase_      ,  tensor_type=UpperCamelCase_)


