






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


A_			  =    logging.getLogger(__name__)
class 						__lowerCAmelCase   (			UpperCAmelCase       ):







  '''simple docstring'''


  def 					UpperCamelCase__   (      self:		Optional[Any]				,			UpperCamelCase_:		Any				,			UpperCamelCase_:		List[Any]				,			UpperCamelCase_:		Tuple=None				,			UpperCamelCase_:		Dict=None					):
     if current_layer == 0:
        UpperCamelCase_     =self.embedding_hidden_mapping_in(UpperCamelCase_					)
     else:
        UpperCamelCase_     =hidden_states[0]

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

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

     UpperCamelCase_     =self.albert_layer_groups[group_idx](
         UpperCamelCase_				,			UpperCamelCase_				,			head_mask[group_idx * layers_per_group : (group_idx + 1) * layers_per_group]				,			)
     UpperCamelCase_     =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 						__lowerCAmelCase   (			UpperCAmelCase       ):







  '''simple docstring'''


  def __init__(      self:		Tuple				,			UpperCamelCase_:		Union[str, Any]					):
     super().__init__(UpperCamelCase_					)

     UpperCamelCase_     =AlbertTransformerWithPabee(UpperCamelCase_					)

     self.init_weights()
     UpperCamelCase_     =0
     UpperCamelCase_     =0
     UpperCamelCase_     =0

     UpperCamelCase_     =0
  def 					UpperCamelCase__   (      self:		Optional[Any]				,			UpperCamelCase_:		Union[str, Any]					):
     UpperCamelCase_     =threshold
  def 					UpperCamelCase__   (      self:		Any				,			UpperCamelCase_:		str					):
     UpperCamelCase_     =patience
  def 					UpperCamelCase__   (      self:		int					):
     UpperCamelCase_     =0
     UpperCamelCase_     =0
  def 					UpperCamelCase__   (      self:		str					):
     UpperCamelCase_     =self.inference_layers_num / self.inference_instances_num
     UpperCamelCase_     =(
         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 					UpperCamelCase__   (      self:		int				,			UpperCamelCase_:		List[Any]=None				,			UpperCamelCase_:		List[str]=None				,			UpperCamelCase_:		List[Any]=None				,			UpperCamelCase_:		Dict=None				,			UpperCamelCase_:		Optional[int]=None				,			UpperCamelCase_:		str=None				,			UpperCamelCase_:		str=None				,			UpperCamelCase_:		int=None				,			UpperCamelCase_:		Union[str, Any]=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_     =input_ids.size()
     elif inputs_embeds is not None:
        UpperCamelCase_     =inputs_embeds.size()[:-1]
     else:
        raise ValueError("You have to specify either input_ids or inputs_embeds"					)

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

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

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

     UpperCamelCase_     =self.embeddings(
         UpperCamelCase_				,			position_ids=UpperCamelCase_				,			token_type_ids=UpperCamelCase_				,			inputs_embeds=UpperCamelCase_					)
     UpperCamelCase_     =embedding_output

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

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

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

           UpperCamelCase_     =logits
           if patient_counter == self.patience:
              break
        UpperCamelCase_     =[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 						__lowerCAmelCase   (			UpperCAmelCase       ):







  '''simple docstring'''


  def __init__(      self:		str				,			UpperCamelCase_:		Any					):
     super().__init__(UpperCamelCase_					)
     UpperCamelCase_     =config.num_labels

     UpperCamelCase_     =AlbertModelWithPabee(UpperCamelCase_					)
     UpperCamelCase_     =nn.Dropout(config.classifier_dropout_prob					)
     UpperCamelCase_     =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 					UpperCamelCase__   (      self:		Optional[int]				,			UpperCamelCase_:		Any=None				,			UpperCamelCase_:		Tuple=None				,			UpperCamelCase_:		Dict=None				,			UpperCamelCase_:		int=None				,			UpperCamelCase_:		List[str]=None				,			UpperCamelCase_:		Optional[Any]=None				,			UpperCamelCase_:		Optional[Any]=None				,			):
     UpperCamelCase_     =self.albert(
         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_     =(logits[-1],)

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

     return outputs








"""simple docstring"""






from datetime import date

import requests

A_			  =    "https://api.carbonintensity.org.uk/intensity"




def 						_UpperCamelCase			(  ):
   UpperCamelCase_     =requests.get(A			).json()["data"][0]
   return last_half_hour["intensity"]["actual"]




def 						_UpperCamelCase			(  A    ,       A			):
   return requests.get(f"""{BASE_URL}/{start}/{end}"""			).json()["data"]


if __name__ == "__main__":
    for entry in fetch_from_to(start=date(2020, 10, 1), end=date(2020, 10, 3)):
        print("from {from} to {to}: {intensity[actual]}".format(**entry))
    print(f'''{fetch_last_half_hour() = }''')
