




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.albert.modeling_albert import (
    ALBERT_INPUTS_DOCSTRING,
    ALBERT_START_DOCSTRING,
    AlbertModel,
    AlbertPreTrainedModel,
    AlbertTransformer,
)


SCREAMING_SNAKE_CASE_						:					Tuple   =       logging.getLogger(__name__)







class 		__UpperCAmelCase(		lowercase__ ):





     """simple docstring"""



     def 				__SCREAMING_SNAKE_CASE		(      self :       Optional[Any]						,				A :       Any						,				A :       Union[str, Any]						,				A :       Union[str, Any]=None						,				A :       List[Any]=None    )			->			Optional[int]:






         """simple docstring"""
         if current_layer == 0:
             a__      =       self.embedding_hidden_mapping_in(A    )
         else:
             a__      =       hidden_states[0]

         a__      =       int(self.config.num_hidden_layers / self.config.num_hidden_groups    )

         # Index of the hidden group
         a__      =       int(current_layer / (self.config.num_hidden_layers / self.config.num_hidden_groups)    )

         a__      =       self.albert_layer_groups[group_idx](
             A						,				A						,				head_mask[group_idx * layers_per_group : (group_idx + 1) * layers_per_group]						,				)
         a__      =       layer_group_output[0]

         return (hidden_states,)







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





     """simple docstring"""



     def __init__(      self :       List[str]						,				A :       Dict    )			->			Optional[int]:






         """simple docstring"""
         super().__init__(A    )

         a__      =       AlbertTransformerWithPabee(A    )

         self.init_weights()
         a__      =       0
         a__      =       0
         a__      =       0

         a__      =       0

     def 				__SCREAMING_SNAKE_CASE		(      self :       Optional[int]						,				A :       Optional[int]    )			->			int:






         """simple docstring"""
         a__      =       threshold

     def 				__SCREAMING_SNAKE_CASE		(      self :       List[Any]						,				A :       Union[str, Any]    )			->			Any:






         """simple docstring"""
         a__      =       patience

     def 				__SCREAMING_SNAKE_CASE		(      self :       str    )			->			List[Any]:






         """simple docstring"""
         a__      =       0
         a__      =       0

     def 				__SCREAMING_SNAKE_CASE		(      self :       Tuple    )			->			List[str]:






         """simple docstring"""
         a__      =       self.inference_layers_num / self.inference_instances_num
         a__      =       (
             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(A    )






     @add_start_docstrings_to_model_forward(A    )
     def 				__SCREAMING_SNAKE_CASE		(      self :       Union[str, Any]						,				A :       Dict=None						,				A :       Optional[int]=None						,				A :       List[str]=None						,				A :       Dict=None						,				A :       str=None						,				A :       Optional[int]=None						,				A :       List[Any]=None						,				A :       Optional[Any]=None						,				A :       str=False						,				)			->			List[str]:






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

         a__      =       input_ids.device if input_ids is not None else inputs_embeds.device

         if attention_mask is None:
             a__      =       torch.ones(A						,				device=A    )
         if token_type_ids is None:
             a__      =       torch.zeros(A						,				dtype=torch.long						,				device=A    )

         a__      =       attention_mask.unsqueeze(1    ).unsqueeze(2    )
         a__      =       extended_attention_mask.to(dtype=self.dtype    )  # fp16 compatibility
         a__      =       (1.0 - extended_attention_mask) * -1_00_00.0
         a__      =       self.get_head_mask(A						,				self.config.num_hidden_layers    )

         a__      =       self.embeddings(
             A						,				position_ids=A						,				token_type_ids=A						,				inputs_embeds=A    )
         a__      =       embedding_output

         if self.training:
             a__      =       []
             for i in range(self.config.num_hidden_layers    ):
                 a__      =       self.encoder.adaptive_forward(
                     A						,				current_layer=A						,				attention_mask=A						,				head_mask=A						,				)

                 a__      =       self.pooler_activation(self.pooler(encoder_outputs[0][:, 0]    )    )
                 a__      =       output_layers[i](output_dropout(A    )    )
                 res.append(A    )
         elif self.patience == 0:  # Use all layers for inference
             a__      =       self.encoder(A						,				A						,				head_mask=A    )
             a__      =       self.pooler_activation(self.pooler(encoder_outputs[0][:, 0]    )    )
             a__      =       [output_layers[self.config.num_hidden_layers - 1](A    )]
         else:
             a__      =       0
             a__      =       None
             a__      =       0
             for i in range(self.config.num_hidden_layers    ):
                 calculated_layer_num += 1
                 a__      =       self.encoder.adaptive_forward(
                     A						,				current_layer=A						,				attention_mask=A						,				head_mask=A						,				)

                 a__      =       self.pooler_activation(self.pooler(encoder_outputs[0][:, 0]    )    )
                 a__      =       output_layers[i](A    )
                 if regression:
                     a__      =       logits.detach()
                     if patient_result is not None:
                         a__      =       patient_result.detach()
                     if (patient_result is not None) and torch.abs(patient_result - labels    ) < self.regression_threshold:
                         patient_counter += 1
                     else:
                         a__      =       0
                 else:
                     a__      =       logits.detach().argmax(dim=1    )
                     if patient_result is not None:
                         a__      =       patient_result.detach().argmax(dim=1    )
                     if (patient_result is not None) and torch.all(labels.eq(A    )    ):
                         patient_counter += 1
                     else:
                         a__      =       0

                 a__      =       logits
                 if patient_counter == self.patience:
                     break
             a__      =       [patient_result]
             self.inference_layers_num += calculated_layer_num
             self.inference_instances_num += 1

         return res







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





     """simple docstring"""



     def __init__(      self :       List[Any]						,				A :       Optional[int]    )			->			Any:






         """simple docstring"""
         super().__init__(A    )
         a__      =       config.num_labels

         a__      =       AlbertModelWithPabee(A    )
         a__      =       nn.Dropout(config.classifier_dropout_prob    )
         a__      =       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(A    )
     def 				__SCREAMING_SNAKE_CASE		(      self :       Optional[int]						,				A :       Union[str, Any]=None						,				A :       Optional[Any]=None						,				A :       List[str]=None						,				A :       Optional[int]=None						,				A :       List[str]=None						,				A :       int=None						,				A :       str=None						,				)			->			Optional[Any]:






         """simple docstring"""
         a__      =       self.albert(
             input_ids=A						,				attention_mask=A						,				token_type_ids=A						,				position_ids=A						,				head_mask=A						,				inputs_embeds=A						,				output_dropout=self.dropout						,				output_layers=self.classifiers						,				regression=self.num_labels == 1						,				)

         a__      =       (logits[-1],)

         if labels is not None:
             a__      =       None
             a__      =       0
             for ix, logits_item in enumerate(A    ):
                 if self.num_labels == 1:
                     #  We are doing regression
                     a__      =       MSELoss()
                     a__      =       loss_fct(logits_item.view(-1    )						,				labels.view(-1    )    )
                 else:
                     a__      =       CrossEntropyLoss()
                     a__      =       loss_fct(logits_item.view(-1						,				self.num_labels    )						,				labels.view(-1    )    )
                 if total_loss is None:
                     a__      =       loss
                 else:
                     total_loss += loss * (ix + 1)
                 total_weights += ix + 1
             a__      =       (total_loss / total_weights,) + outputs

         return outputs












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, normalize, rescale, resize, to_channel_dimension_format
from ...image_utils import (
    IMAGENET_STANDARD_MEAN,
    IMAGENET_STANDARD_STD,
    ChannelDimension,
    ImageInput,
    PILImageResampling,
    make_list_of_images,
    to_numpy_array,
    valid_images,
)
from ...utils import TensorType, is_vision_available, logging


if is_vision_available():
 import PIL


SCREAMING_SNAKE_CASE_						:					Any			   =       logging.get_logger(__name__)







class 		__UpperCAmelCase(		lowercase__ ):





     """simple docstring"""



     __UpperCAmelCase	: Optional[Any]						=    ["""pixel_values"""]

     def __init__(      self :       int						,				A :       bool = True						,				A :       Dict[str, int] = None						,				A :       PILImageResampling = PIL.Image.NEAREST						,				A :       bool = False						,				A :       Dict[str, int] = None						,				A :       Union[int, float] = 1 / 2_55						,				A :       bool = False						,				A :       bool = True						,				A :       bool = True						,				A :       Optional[Union[float, List[float]]] = None						,				A :       Optional[Union[float, List[float]]] = None						,				A :       bool = True						,				**A :       Optional[int]						,				)			->			None:






         """simple docstring"""
         super().__init__(**A    )
         a__      =       size if size is not None else {"""height""": 3_46, """width""": 3_46}
         a__      =       get_size_dict(A    )
         a__      =       crop_size if crop_size is not None else {"""height""": 2_89, """width""": 2_89}
         a__      =       get_size_dict(A						,				param_name="""crop_size"""    )

         a__      =       do_resize
         a__      =       size
         a__      =       resample
         a__      =       do_center_crop
         a__      =       crop_size
         a__      =       do_rescale
         a__      =       rescale_factor
         a__      =       rescale_offset
         a__      =       do_normalize
         a__      =       image_mean if image_mean is not None else IMAGENET_STANDARD_MEAN
         a__      =       image_std if image_std is not None else IMAGENET_STANDARD_STD
         a__      =       include_top

     def 				__SCREAMING_SNAKE_CASE		(      self :       Optional[Any]						,				A :       np.ndarray						,				A :       Dict[str, int]						,				A :       PILImageResampling = PIL.Image.NEAREST						,				A :       Optional[Union[str, ChannelDimension]] = None						,				**A :       Dict						,				)			->			np.ndarray:






         """simple docstring"""
         a__      =       get_size_dict(A    )
         if "height" not in size or "width" not in size:
             raise ValueError(f"""The size dictionary must have keys 'height' and 'width'. Got {size.keys()}"""    )
         return resize(
             A						,				size=(size["""height"""], size["""width"""])						,				resample=A						,				data_format=A						,				**A    )

     def 				__SCREAMING_SNAKE_CASE		(      self :       Tuple						,				A :       np.ndarray						,				A :       Dict[str, int]						,				A :       Optional[Union[str, ChannelDimension]] = None						,				**A :       Optional[Any]						,				)			->			np.ndarray:






         """simple docstring"""
         a__      =       get_size_dict(A    )
         if "height" not in size or "width" not in size:
             raise ValueError(f"""The size dictionary must have keys 'height' and 'width'. Got {size.keys()}"""    )
         return center_crop(A						,				size=(size["""height"""], size["""width"""])						,				data_format=A						,				**A    )

     def 				__SCREAMING_SNAKE_CASE		(      self :       Dict						,				A :       np.ndarray						,				A :       Union[int, float]						,				A :       bool = True						,				A :       Optional[Union[str, ChannelDimension]] = None						,				**A :       List[str]						,				)			->			Any:






         """simple docstring"""
         if offset:
             a__      =       (image - 1_2_7.5) * scale
             if data_format is not None:
                 a__      =       to_channel_dimension_format(A						,				A    )
             a__      =       rescaled_image.astype(np.floataa    )
         else:
             a__      =       rescale(A						,				scale=A						,				data_format=A						,				**A    )
         return rescaled_image

     def 				__SCREAMING_SNAKE_CASE		(      self :       Optional[int]						,				A :       np.ndarray						,				A :       Union[float, List[float]]						,				A :       Union[float, List[float]]						,				A :       Optional[Union[str, ChannelDimension]] = None						,				**A :       str						,				)			->			np.ndarray:






         """simple docstring"""
         return normalize(A						,				mean=A						,				std=A						,				data_format=A						,				**A    )






     def 				__SCREAMING_SNAKE_CASE		(      self :       Union[str, Any]						,				A :       ImageInput						,				A :       bool = None						,				A :       Dict[str, int] = None						,				A :       Any=None						,				A :       bool = None						,				A :       Dict[str, int] = None						,				A :       bool = None						,				A :       float = None						,				A :       bool = None						,				A :       bool = None						,				A :       Optional[Union[float, List[float]]] = None						,				A :       Optional[Union[float, List[float]]] = None						,				A :       bool = None						,				A :       Optional[Union[str, TensorType]] = None						,				A :       ChannelDimension = ChannelDimension.FIRST						,				**A :       Tuple						,				)			->			PIL.Image.Image:






         """simple docstring"""
         a__      =       do_resize if do_resize is not None else self.do_resize
         a__      =       resample if resample is not None else self.resample
         a__      =       do_center_crop if do_center_crop is not None else self.do_center_crop
         a__      =       do_rescale if do_rescale is not None else self.do_rescale
         a__      =       rescale_factor if rescale_factor is not None else self.rescale_factor
         a__      =       rescale_offset if rescale_offset is not None else self.rescale_offset
         a__      =       do_normalize if do_normalize is not None else self.do_normalize
         a__      =       image_mean if image_mean is not None else self.image_mean
         a__      =       image_std if image_std is not None else self.image_std
         a__      =       include_top if include_top is not None else self.include_top

         a__      =       size if size is not None else self.size
         a__      =       get_size_dict(A    )
         a__      =       crop_size if crop_size is not None else self.crop_size
         a__      =       get_size_dict(A						,				param_name="""crop_size"""    )

         a__      =       make_list_of_images(A    )

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

         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.
         a__      =       [to_numpy_array(A    ) for image in images]

         if do_resize:
             a__      =       [self.resize(image=A						,				size=A						,				resample=A    ) for image in images]

         if do_center_crop:
             a__      =       [self.center_crop(image=A						,				size=A    ) for image in images]

         if do_rescale:
             a__      =       [self.rescale(image=A						,				scale=A						,				offset=A    ) for image in images]

         if do_normalize:
             a__      =       [self.normalize(image=A						,				mean=A						,				std=A    ) for image in images]

         if include_top:
             a__      =       [self.normalize(image=A						,				mean=[0, 0, 0]						,				std=A    ) for image in images]

         a__      =       [to_channel_dimension_format(A						,				A    ) for image in images]

         a__      =       {"""pixel_values""": images}
         return BatchFeature(data=A						,				tensor_type=A    )






