


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


snake_case    :   Optional[Any]	    =					logging.getLogger(__name__)







class 		_snake_case  (				_snake_case							):




 def 	SCREAMING_SNAKE_CASE__    (		self ,  _lowerCamelCase ,  _lowerCamelCase ,  _lowerCamelCase=None ,  _lowerCamelCase=None   ):
       a						:Optional[int]										=	self.layer[current_layer](_lowerCamelCase ,  _lowerCamelCase ,  head_mask[current_layer]   )

       a						: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.'						,   _snake_case						,   )
class 		_snake_case  (				_snake_case							):




 def __init__(		self ,  _lowerCamelCase   ):
       super().__init__(_lowerCamelCase   )

       a						:int										=	BertEncoderWithPabee(_lowerCamelCase   )

       self.init_weights()
       a						:Union[str, Any]										=	0
       a						:Dict										=	0
       a						:Dict										=	0

       a						:Dict										=	0




 def 	SCREAMING_SNAKE_CASE__    (		self ,  _lowerCamelCase   ):
       a						:Dict										=	threshold




 def 	SCREAMING_SNAKE_CASE__    (		self ,  _lowerCamelCase   ):
       a						:Tuple										=	patience




 def 	SCREAMING_SNAKE_CASE__    (		self   ):
       a						:Any										=	0
       a						:Tuple										=	0




 def 	SCREAMING_SNAKE_CASE__    (		self   ):
       a						:Optional[int]										=	self.inference_layers_num / self.inference_instances_num
       a						:Dict										=	(
           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(_lowerCamelCase   )




 @add_start_docstrings_to_model_forward(_lowerCamelCase   )
 def 	SCREAMING_SNAKE_CASE__    (		self ,  _lowerCamelCase=None ,  _lowerCamelCase=None ,  _lowerCamelCase=None ,  _lowerCamelCase=None ,  _lowerCamelCase=None ,  _lowerCamelCase=None ,  _lowerCamelCase=None ,  _lowerCamelCase=None ,  _lowerCamelCase=None ,  _lowerCamelCase=None ,  _lowerCamelCase=False ,  ):

       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:
             a						:Dict										=	input_ids.size()
       elif inputs_embeds is not None:
             a						:Any										=	inputs_embeds.size()[:-1]
       else:
             raise ValueError('''You have to specify either input_ids or inputs_embeds'''   )

       a						:Optional[int]										=	input_ids.device if input_ids is not None else inputs_embeds.device

       if attention_mask is None:
             a						:List[str]										=	torch.ones(_lowerCamelCase ,  device=_lowerCamelCase   )
       if token_type_ids is None:
             a						:int										=	torch.zeros(_lowerCamelCase ,  dtype=torch.long ,  device=_lowerCamelCase   )

       # 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.
       a						:torch.Tensor										=	self.get_extended_attention_mask(_lowerCamelCase ,  _lowerCamelCase ,  _lowerCamelCase   )

       # 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:
             a							,    a							,    a						:Optional[int]										=	encoder_hidden_states.size()
             a						:int										=	(encoder_batch_size, encoder_sequence_length)
             if encoder_attention_mask is None:
                   a						:Union[str, Any]										=	torch.ones(_lowerCamelCase ,  device=_lowerCamelCase   )
             a						:List[str]										=	self.invert_attention_mask(_lowerCamelCase   )
       else:
             a						:Tuple										=	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]
       a						:Optional[Any]										=	self.get_head_mask(_lowerCamelCase ,  self.config.num_hidden_layers   )

       a						:Optional[Any]										=	self.embeddings(
           input_ids=_lowerCamelCase ,  position_ids=_lowerCamelCase ,  token_type_ids=_lowerCamelCase ,  inputs_embeds=_lowerCamelCase   )
       a						:Optional[int]										=	embedding_output

       if self.training:
             a						:List[str]										=	[]
             for i in range(self.config.num_hidden_layers   ):
                   a						:List[str]										=	self.encoder.adaptive_forward(
                       _lowerCamelCase ,  current_layer=_lowerCamelCase ,  attention_mask=_lowerCamelCase ,  head_mask=_lowerCamelCase   )

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

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

                   a						:Tuple										=	logits
                   if patient_counter == self.patience:
                         break
             a						:Union[str, 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. '						,   _snake_case						,   )
class 		_snake_case  (				_snake_case							):




 def __init__(		self ,  _lowerCamelCase   ):
       super().__init__(_lowerCamelCase   )
       a						:Union[str, Any]										=	config.num_labels

       a						:Union[str, Any]										=	BertModelWithPabee(_lowerCamelCase   )
       a						:List[str]										=	nn.Dropout(config.hidden_dropout_prob   )
       a						:Union[str, Any]										=	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(_lowerCamelCase   )
 def 	SCREAMING_SNAKE_CASE__    (		self ,  _lowerCamelCase=None ,  _lowerCamelCase=None ,  _lowerCamelCase=None ,  _lowerCamelCase=None ,  _lowerCamelCase=None ,  _lowerCamelCase=None ,  _lowerCamelCase=None ,  ):
       a						:int										=	self.bert(
           input_ids=_lowerCamelCase ,  attention_mask=_lowerCamelCase ,  token_type_ids=_lowerCamelCase ,  position_ids=_lowerCamelCase ,  head_mask=_lowerCamelCase ,  inputs_embeds=_lowerCamelCase ,  output_dropout=self.dropout ,  output_layers=self.classifiers ,  regression=self.num_labels == 1 ,  )

       a						:Optional[int]										=	(logits[-1],)

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

       return outputs









from typing import List, Union

from ..utils import (
    add_end_docstrings,
    is_tf_available,
    is_torch_available,
    is_vision_available,
    logging,
    requires_backends,
)
from .base import PIPELINE_INIT_ARGS, Pipeline


if is_vision_available():
     from PIL import Image

     from ..image_utils import load_image

if is_tf_available():
     import tensorflow as tf

     from ..models.auto.modeling_tf_auto import TF_MODEL_FOR_IMAGE_CLASSIFICATION_MAPPING
     from ..tf_utils import stable_softmax

if is_torch_available():
     from ..models.auto.modeling_auto import MODEL_FOR_IMAGE_CLASSIFICATION_MAPPING

snake_case    :   str	    =					logging.get_logger(__name__)







@add_end_docstrings(_snake_case							)
class 		_snake_case  (				_snake_case							):




 def __init__(		self ,  *_lowerCamelCase ,  **_lowerCamelCase   ):
       super().__init__(*_lowerCamelCase ,  **_lowerCamelCase   )
       requires_backends(self ,  '''vision'''   )
       self.check_model_type(
           TF_MODEL_FOR_IMAGE_CLASSIFICATION_MAPPING
           if self.framework == '''tf'''
           else MODEL_FOR_IMAGE_CLASSIFICATION_MAPPING   )




 def 	SCREAMING_SNAKE_CASE__    (		self ,  _lowerCamelCase=None   ):
       a						:Tuple										=	{}
       if top_k is not None:
             a						:int										=	top_k
       return {}, {}, postprocess_params




 def __call__(		self ,  _lowerCamelCase ,  **_lowerCamelCase   ):
       return super().__call__(_lowerCamelCase ,  **_lowerCamelCase   )




 def 	SCREAMING_SNAKE_CASE__    (		self ,  _lowerCamelCase   ):
       a						:str										=	load_image(_lowerCamelCase   )
       a						:Any										=	self.image_processor(images=_lowerCamelCase ,  return_tensors=self.framework   )
       return model_inputs




 def 	SCREAMING_SNAKE_CASE__    (		self ,  _lowerCamelCase   ):
       a						:List[str]										=	self.model(**_lowerCamelCase   )
       return model_outputs




 def 	SCREAMING_SNAKE_CASE__    (		self ,  _lowerCamelCase ,  _lowerCamelCase=5   ):
       if top_k > self.model.config.num_labels:
             a						:List[Any]										=	self.model.config.num_labels

       if self.framework == "pt":
             a						:int										=	model_outputs.logits.softmax(-1   )[0]
             a							,    a						:Union[str, Any]										=	probs.topk(_lowerCamelCase   )
       elif self.framework == "tf":
             a						:Optional[Any]										=	stable_softmax(model_outputs.logits ,  axis=-1   )[0]
             a						:Union[str, Any]										=	tf.math.top_k(_lowerCamelCase ,  k=_lowerCamelCase   )
             a							,    a						:Optional[int]										=	topk.values.numpy(), topk.indices.numpy()
       else:
             raise ValueError(F'''Unsupported framework: {self.framework}'''   )

       a						:Optional[int]										=	scores.tolist()
       a						:str										=	ids.tolist()
       return [{"score": score, "label": self.model.config.idalabel[_id]} for score, _id in zip(_lowerCamelCase ,  _lowerCamelCase   )]





