code: 
  
  
  
  """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.albert.modeling_albert import (
      ALBERT_INPUTS_DOCSTRING,
      ALBERT_START_DOCSTRING,
      AlbertModel,
      AlbertPreTrainedModel,
      AlbertTransformer,
  )
  
  
  _lowerCAmelCase       :      int							=							logging.getLogger(__name__)
  
  
  
  
  
  
  class 			UpperCAmelCase_						(       _UpperCamelCase		):
  
  
  
        def 		snake_case_      (  self  :			int  ,			A  :			Union[str, Any]  ,			A  :			int  ,			A  :			Tuple=None  ,			A  :			Any=None		):
             if current_layer == 0:
                  _UpperCAmelCase    :			str              =					self.embedding_hidden_mapping_in(A		)
             else:
                  _UpperCAmelCase    :			str              =					hidden_states[0]
  
             _UpperCAmelCase    :			Dict              =					int(self.config.num_hidden_layers / self.config.num_hidden_groups		)
  
             # Index of the hidden group
             _UpperCAmelCase    :			Any              =					int(current_layer / (self.config.num_hidden_layers / self.config.num_hidden_groups)		)
  
             _UpperCAmelCase    :			Optional[int]              =					self.albert_layer_groups[group_idx](
                 A  ,			A  ,			head_mask[group_idx * layers_per_group : (group_idx + 1) * layers_per_group]  ,			)
             _UpperCAmelCase    :			List[str]              =					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.' ,    _UpperCamelCase ,    )
  class 			UpperCAmelCase_						(       _UpperCamelCase		):
  
  
  
        def __init__(  self  :			Optional[Any]  ,			A  :			Optional[int]		):
             super().__init__(A		)
  
             _UpperCAmelCase    :			int              =					AlbertTransformerWithPabee(A		)
  
             self.init_weights()
             _UpperCAmelCase    :			Dict              =					0
             _UpperCAmelCase    :			List[Any]              =					0
             _UpperCAmelCase    :			List[str]              =					0
  
             _UpperCAmelCase    :			Dict              =					0
  
  
  
        def 		snake_case_      (  self  :			int  ,			A  :			Optional[Any]		):
             _UpperCAmelCase    :			int              =					threshold
  
  
  
        def 		snake_case_      (  self  :			List[str]  ,			A  :			List[Any]		):
             _UpperCAmelCase    :			List[str]              =					patience
  
  
  
        def 		snake_case_      (  self  :			int		):
             _UpperCAmelCase    :			List[str]              =					0
             _UpperCAmelCase    :			Optional[int]              =					0
  
  
  
        def 		snake_case_      (  self  :			str		):
             _UpperCAmelCase    :			Any              =					self.inference_layers_num / self.inference_instances_num
             _UpperCAmelCase    :			Any              =					(
                 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 		snake_case_      (  self  :			Dict  ,			A  :			Optional[int]=None  ,			A  :			List[str]=None  ,			A  :			Any=None  ,			A  :			Any=None  ,			A  :			List[str]=None  ,			A  :			int=None  ,			A  :			str=None  ,			A  :			List[Any]=None  ,			A  :			str=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:
                  _UpperCAmelCase    :			Tuple              =					input_ids.size()
             elif inputs_embeds is not None:
                  _UpperCAmelCase    :			int              =					inputs_embeds.size()[:-1]
             else:
                  raise ValueError("You have to specify either input_ids or inputs_embeds"		)
  
             _UpperCAmelCase    :			Any              =					input_ids.device if input_ids is not None else inputs_embeds.device
  
             if attention_mask is None:
                  _UpperCAmelCase    :			Tuple              =					torch.ones(A  ,			device=A		)
             if token_type_ids is None:
                  _UpperCAmelCase    :			List[Any]              =					torch.zeros(A  ,			dtype=torch.long  ,			device=A		)
  
             _UpperCAmelCase    :			List[str]              =					attention_mask.unsqueeze(1		).unsqueeze(2		)
             _UpperCAmelCase    :			Optional[int]              =					extended_attention_mask.to(dtype=self.dtype		)  # fp16 compatibility
             _UpperCAmelCase    :			Optional[int]              =					(1.0 - extended_attention_mask) * -10_000.0
             _UpperCAmelCase    :			int              =					self.get_head_mask(A  ,			self.config.num_hidden_layers		)
  
             _UpperCAmelCase    :			Union[str, Any]              =					self.embeddings(
                 A  ,			position_ids=A  ,			token_type_ids=A  ,			inputs_embeds=A		)
             _UpperCAmelCase    :			str              =					embedding_output
  
             if self.training:
                  _UpperCAmelCase    :			List[Any]              =					[]
                  for i in range(self.config.num_hidden_layers		):
                       _UpperCAmelCase    :			str              =					self.encoder.adaptive_forward(
                           A  ,			current_layer=A  ,			attention_mask=A  ,			head_mask=A  ,			)
  
                       _UpperCAmelCase    :			str              =					self.pooler_activation(self.pooler(encoder_outputs[0][:, 0]		)		)
                       _UpperCAmelCase    :			int              =					output_layers[i](output_dropout(A		)		)
                       res.append(A		)
             elif self.patience == 0:  # Use all layers for inference
                  _UpperCAmelCase    :			List[Any]              =					self.encoder(A  ,			A  ,			head_mask=A		)
                  _UpperCAmelCase    :			str              =					self.pooler_activation(self.pooler(encoder_outputs[0][:, 0]		)		)
                  _UpperCAmelCase    :			Optional[int]              =					[output_layers[self.config.num_hidden_layers - 1](A		)]
             else:
                  _UpperCAmelCase    :			int              =					0
                  _UpperCAmelCase    :			List[Any]              =					None
                  _UpperCAmelCase    :			str              =					0
                  for i in range(self.config.num_hidden_layers		):
                       calculated_layer_num += 1
                       _UpperCAmelCase    :			Any              =					self.encoder.adaptive_forward(
                           A  ,			current_layer=A  ,			attention_mask=A  ,			head_mask=A  ,			)
  
                       _UpperCAmelCase    :			Dict              =					self.pooler_activation(self.pooler(encoder_outputs[0][:, 0]		)		)
                       _UpperCAmelCase    :			List[Any]              =					output_layers[i](A		)
                       if regression:
                            _UpperCAmelCase    :			Optional[int]              =					logits.detach()
                            if patient_result is not None:
                                 _UpperCAmelCase    :			Union[str, Any]              =					patient_result.detach()
                            if (patient_result is not None) and torch.abs(patient_result - labels		) < self.regression_threshold:
                                 patient_counter += 1
                            else:
                                 _UpperCAmelCase    :			Dict              =					0
                       else:
                            _UpperCAmelCase    :			List[Any]              =					logits.detach().argmax(dim=1		)
                            if patient_result is not None:
                                 _UpperCAmelCase    :			List[str]              =					patient_result.detach().argmax(dim=1		)
                            if (patient_result is not None) and torch.all(labels.eq(A		)		):
                                 patient_counter += 1
                            else:
                                 _UpperCAmelCase    :			Dict              =					0
  
                       _UpperCAmelCase    :			Tuple              =					logits
                       if patient_counter == self.patience:
                            break
                  _UpperCAmelCase    :			Tuple              =					[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\n    the pooled output) e.g. for GLUE tasks. ' ,    _UpperCamelCase ,    )
  class 			UpperCAmelCase_						(       _UpperCamelCase		):
  
  
  
        def __init__(  self  :			Optional[Any]  ,			A  :			int		):
             super().__init__(A		)
             _UpperCAmelCase    :			int              =					config.num_labels
  
             _UpperCAmelCase    :			Union[str, Any]              =					AlbertModelWithPabee(A		)
             _UpperCAmelCase    :			Any              =					nn.Dropout(config.classifier_dropout_prob		)
             _UpperCAmelCase    :			Optional[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(A		)
        def 		snake_case_      (  self  :			int  ,			A  :			Tuple=None  ,			A  :			Union[str, Any]=None  ,			A  :			Optional[int]=None  ,			A  :			Any=None  ,			A  :			Dict=None  ,			A  :			int=None  ,			A  :			Optional[Any]=None  ,			):
             _UpperCAmelCase    :			Optional[int]              =					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  ,			)
  
             _UpperCAmelCase    :			List[str]              =					(logits[-1],)
  
             if labels is not None:
                  _UpperCAmelCase    :			Tuple              =					None
                  _UpperCAmelCase    :			Any              =					0
                  for ix, logits_item in enumerate(A		):
                       if self.num_labels == 1:
                            #  We are doing regression
                            _UpperCAmelCase    :			List[str]              =					MSELoss()
                            _UpperCAmelCase    :			Tuple              =					loss_fct(logits_item.view(-1		)  ,			labels.view(-1		)		)
                       else:
                            _UpperCAmelCase    :			Dict              =					CrossEntropyLoss()
                            _UpperCAmelCase    :			Optional[Any]              =					loss_fct(logits_item.view(-1  ,			self.num_labels		)  ,			labels.view(-1		)		)
                       if total_loss is None:
                            _UpperCAmelCase    :			Union[str, Any]              =					loss
                       else:
                            total_loss += loss * (ix + 1)
                       total_weights += ix + 1
                  _UpperCAmelCase    :			int              =					(total_loss / total_weights,) + outputs
  
             return outputs
  
style_context: 
  
  
  
  """simple docstring"""
  
  
  
  
  def 		__snake_case		(   SCREAMING_SNAKE_CASE__			:		list       )						->     bool:
  
  
  
  
  
       '''simple docstring'''
  
       if not isinstance(SCREAMING_SNAKE_CASE__		,						SCREAMING_SNAKE_CASE__       ):
            raise ValueError("Input series is not valid, valid series - [1, 2/3, 2]"       )
       if len(SCREAMING_SNAKE_CASE__       ) == 0:
            raise ValueError("Input list must be a non empty list"       )
       if len(SCREAMING_SNAKE_CASE__       ) == 1 and series[0] != 0:
            return True
       _UpperCAmelCase    :			Tuple              =					[]
       _UpperCAmelCase    :			Any              =					len(SCREAMING_SNAKE_CASE__       )
       for i in range(0		,						SCREAMING_SNAKE_CASE__       ):
            if series[i] == 0:
                 raise ValueError("Input series cannot have 0 as an element"       )
            rec_series.append(1 / series[i]       )
       _UpperCAmelCase    :			Dict              =					rec_series[1] - rec_series[0]
       for index in range(2		,						SCREAMING_SNAKE_CASE__       ):
            if rec_series[index] - rec_series[index - 1] != common_diff:
                 return False
       return True
  
  def 		__snake_case		(   SCREAMING_SNAKE_CASE__			:		list       )						->     float:
  
  
  
  
  
       '''simple docstring'''
  
       if not isinstance(SCREAMING_SNAKE_CASE__		,						SCREAMING_SNAKE_CASE__       ):
            raise ValueError("Input series is not valid, valid series - [2, 4, 6]"       )
       if len(SCREAMING_SNAKE_CASE__       ) == 0:
            raise ValueError("Input list must be a non empty list"       )
       _UpperCAmelCase    :			Any              =					0
       for val in series:
            answer += 1 / val
       return len(SCREAMING_SNAKE_CASE__       ) / answer
  
  
  if __name__ == "__main__":
   import doctest
  
   doctest.testmod()
  