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


__snake_case  : List[str]		       =    logging.getLogger(__name__)




class       __UpperCAmelCase							(							_UpperCamelCase	):





       '''simple docstring'''



       def    __A			( self       ,	_SCREAMING_SNAKE_CASE       ,	_SCREAMING_SNAKE_CASE       ,	_SCREAMING_SNAKE_CASE=None       ,	_SCREAMING_SNAKE_CASE=None					)						->	Optional[int]:
           if current_layer == 0:
               A_      =  self.embedding_hidden_mapping_in(_SCREAMING_SNAKE_CASE					)
           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](
               _SCREAMING_SNAKE_CASE       ,	_SCREAMING_SNAKE_CASE       ,	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.'		, _UpperCamelCase		, )
class       __UpperCAmelCase							(							_UpperCamelCase	):





       '''simple docstring'''



       def __init__( self       ,	_SCREAMING_SNAKE_CASE					)						->	Any:
           super().__init__(_SCREAMING_SNAKE_CASE					)

           A_      =  AlbertTransformerWithPabee(_SCREAMING_SNAKE_CASE					)

           self.init_weights()
           A_      =  0
           A_      =  0
           A_      =  0

           A_      =  0

       def    __A			( self       ,	_SCREAMING_SNAKE_CASE					)						->	List[Any]:
           A_      =  threshold

       def    __A			( self       ,	_SCREAMING_SNAKE_CASE					)						->	Union[str, Any]:
           A_      =  patience

       def    __A			( self					)						->	List[str]:
           A_      =  0
           A_      =  0

       def    __A			( self					)						->	str:
           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(_SCREAMING_SNAKE_CASE					)

       @add_start_docstrings_to_model_forward(_SCREAMING_SNAKE_CASE					)
       def    __A			( self       ,	_SCREAMING_SNAKE_CASE=None       ,	_SCREAMING_SNAKE_CASE=None       ,	_SCREAMING_SNAKE_CASE=None       ,	_SCREAMING_SNAKE_CASE=None       ,	_SCREAMING_SNAKE_CASE=None       ,	_SCREAMING_SNAKE_CASE=None       ,	_SCREAMING_SNAKE_CASE=None       ,	_SCREAMING_SNAKE_CASE=None       ,	_SCREAMING_SNAKE_CASE=False       ,	)						->	List[Any]:

           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(_SCREAMING_SNAKE_CASE       ,	device=_SCREAMING_SNAKE_CASE					)
           if token_type_ids is None:
               A_      =  torch.zeros(_SCREAMING_SNAKE_CASE       ,	dtype=torch.long       ,	device=_SCREAMING_SNAKE_CASE					)

           A_      =  attention_mask.unsqueeze(1					).unsqueeze(2					)
           A_      =  extended_attention_mask.to(dtype=self.dtype					)  # fp16 compatibility
           A_      =  (1.0 - extended_attention_mask) * -10_000.0
           A_      =  self.get_head_mask(_SCREAMING_SNAKE_CASE       ,	self.config.num_hidden_layers					)

           A_      =  self.embeddings(
               _SCREAMING_SNAKE_CASE       ,	position_ids=_SCREAMING_SNAKE_CASE       ,	token_type_ids=_SCREAMING_SNAKE_CASE       ,	inputs_embeds=_SCREAMING_SNAKE_CASE					)
           A_      =  embedding_output

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

                   A_      =  self.pooler_activation(self.pooler(encoder_outputs[0][:, 0]					)					)
                   A_      =  output_layers[i](output_dropout(_SCREAMING_SNAKE_CASE					)					)
                   res.append(_SCREAMING_SNAKE_CASE					)
           elif self.patience == 0:  # Use all layers for inference
               A_      =  self.encoder(_SCREAMING_SNAKE_CASE       ,	_SCREAMING_SNAKE_CASE       ,	head_mask=_SCREAMING_SNAKE_CASE					)
               A_      =  self.pooler_activation(self.pooler(encoder_outputs[0][:, 0]					)					)
               A_      =  [output_layers[self.config.num_hidden_layers - 1](_SCREAMING_SNAKE_CASE					)]
           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(
                       _SCREAMING_SNAKE_CASE       ,	current_layer=_SCREAMING_SNAKE_CASE       ,	attention_mask=_SCREAMING_SNAKE_CASE       ,	head_mask=_SCREAMING_SNAKE_CASE       ,	)

                   A_      =  self.pooler_activation(self.pooler(encoder_outputs[0][:, 0]					)					)
                   A_      =  output_layers[i](_SCREAMING_SNAKE_CASE					)
                   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(_SCREAMING_SNAKE_CASE					)					):
                           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\n    the pooled output) e.g. for GLUE tasks. '		, _UpperCamelCase		, )
class       __UpperCAmelCase							(							_UpperCamelCase	):





       '''simple docstring'''



       def __init__( self       ,	_SCREAMING_SNAKE_CASE					)						->	Union[str, Any]:
           super().__init__(_SCREAMING_SNAKE_CASE					)
           A_      =  config.num_labels

           A_      =  AlbertModelWithPabee(_SCREAMING_SNAKE_CASE					)
           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(_SCREAMING_SNAKE_CASE					)
       def    __A			( self       ,	_SCREAMING_SNAKE_CASE=None       ,	_SCREAMING_SNAKE_CASE=None       ,	_SCREAMING_SNAKE_CASE=None       ,	_SCREAMING_SNAKE_CASE=None       ,	_SCREAMING_SNAKE_CASE=None       ,	_SCREAMING_SNAKE_CASE=None       ,	_SCREAMING_SNAKE_CASE=None       ,	)						->	str:
           A_      =  self.albert(
               input_ids=_SCREAMING_SNAKE_CASE       ,	attention_mask=_SCREAMING_SNAKE_CASE       ,	token_type_ids=_SCREAMING_SNAKE_CASE       ,	position_ids=_SCREAMING_SNAKE_CASE       ,	head_mask=_SCREAMING_SNAKE_CASE       ,	inputs_embeds=_SCREAMING_SNAKE_CASE       ,	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(_SCREAMING_SNAKE_CASE					):
                   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







'''simple docstring'''




def 	_UpperCAmelCase       (				_UpperCamelCase       :      list[list[int]],  _UpperCamelCase       :      list[list[int]]  )      -> list[list[int]]:
    A_      =  []
    A_      =  len(_UpperCamelCase  )
    for i in range(_UpperCamelCase  ):
        A_      =  []
        for j in range(_UpperCamelCase  ):
            A_      =  0
            for k in range(_UpperCamelCase  ):
                A_      =  val + matrix_a[i][k] * matrix_b[k][j]
            list_a.append(_UpperCamelCase  )
        matrix_c.append(_UpperCamelCase  )
    return matrix_c





def 	_UpperCAmelCase       (				_UpperCamelCase       :      int  )      -> list[list[int]]:
    return [[int(row == column  ) for column in range(_UpperCamelCase  )] for row in range(_UpperCamelCase  )]





def 	_UpperCAmelCase       (				_UpperCamelCase       :      int  )      -> int:
    if n <= 1:
        return n
    A_      =  identity(2  )
    A_      =  [[1, 1], [1, 0]]
    A_      =  n - 1
    while n > 0:
        if n % 2 == 1:
            A_      =  multiply(_UpperCamelCase,  _UpperCamelCase  )
        A_      =  multiply(_UpperCamelCase,  _UpperCamelCase  )
        A_      =  int(n / 2  )
    return res_matrix[0][0]





def 	_UpperCAmelCase       (				_UpperCamelCase       :      int  )      -> int:
    if n <= 1:
        return n
    A_      =  0
    A_      =  1
    for _ in range(2,  n + 1  ):
        A_  ,A_      =  fiba, fiba + fiba
    return fiba





def 	_UpperCAmelCase       (				)      -> None:
    for ordinal in "0th 1st 2nd 3rd 10th 100th 1000th".split():
        A_      =  int(''''''.join(c for c in ordinal if c in '''0123456789'''  )  )  # 1000th --> 1000
        print(
            F'''{ordinal} fibonacci number using matrix exponentiation is '''
            F'''{nth_fibonacci_matrix(_UpperCamelCase  )} and using bruteforce is '''
            F'''{nth_fibonacci_bruteforce(_UpperCamelCase  )}\n'''  )
    # from timeit import timeit
    # print(timeit("nth_fibonacci_matrix(1000000)",
    #              "from main import nth_fibonacci_matrix", number=5))
    # print(timeit("nth_fibonacci_bruteforce(1000000)",
    #              "from main import nth_fibonacci_bruteforce", number=5))
    # 2.3342058970001744
    # 57.256506615000035


if __name__ == "__main__":
  import doctest

  doctest.testmod()
  main()






