
'''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_    	=							logging.getLogger(__name__)

class   _snake_case( a_			):






 def 	_UpperCamelCase   (self					:     Optional[Any]						,		a					:     int						,		a					:     List[str]						,		a					:     List[str]=None						,		a					:     List[str]=None						)					->      List[str]:






    """simple docstring"""


    if current_layer == 0:
       A__         =    self.embedding_hidden_mapping_in(_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](
        _snake_case						,		_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.'''					,   a_					,   )
class   _snake_case( a_			):






 def __init__(self					:     Tuple						,		a					:     str						)					->      Optional[int]:






    """simple docstring"""


    super().__init__(_snake_case						)

    A__         =    AlbertTransformerWithPabee(_snake_case						)

    self.init_weights()
    A__         =    0
    A__         =    0
    A__         =    0

    A__         =    0






 def 	_UpperCamelCase   (self					:     int						,		a					:     Optional[int]						)					->      List[str]:






    """simple docstring"""


    A__         =    threshold






 def 	_UpperCamelCase   (self					:     str						,		a					:     Optional[int]						)					->      Any:






    """simple docstring"""


    A__         =    patience






 def 	_UpperCamelCase   (self					:     Optional[int]						)					->      int:






    """simple docstring"""


    A__         =    0
    A__         =    0






 def 	_UpperCamelCase   (self					:     List[str]						)					->      int:






    """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(_snake_case						)






 @add_start_docstrings_to_model_forward(_snake_case						)
 def 	_UpperCamelCase   (self					:     Optional[int]						,		a					:     str=None						,		a					:     Dict=None						,		a					:     List[Any]=None						,		a					:     str=None						,		a					:     int=None						,		a					:     Any=None						,		a					:     str=None						,		a					:     Any=None						,		a					:     Tuple=False						,		)					->      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(_snake_case						,		device=_snake_case						)
    if token_type_ids is None:
       A__         =    torch.zeros(_snake_case						,		dtype=torch.long						,		device=_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) * -1_00_00.0
    A__         =    self.get_head_mask(_snake_case						,		self.config.num_hidden_layers						)

    A__         =    self.embeddings(
        _snake_case						,		position_ids=_snake_case						,		token_type_ids=_snake_case						,		inputs_embeds=_snake_case						)
    A__         =    embedding_output

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

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

          A__         =    self.pooler_activation(self.pooler(encoder_outputs[0][:, 0]						)						)
          A__         =    output_layers[i](_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(_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. '''					,   a_					,   )
class   _snake_case( a_			):






 def __init__(self					:     List[Any]						,		a					:     Any						)					->      Tuple:






    """simple docstring"""


    super().__init__(_snake_case						)
    A__         =    config.num_labels

    A__         =    AlbertModelWithPabee(_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(_snake_case						)
 def 	_UpperCamelCase   (self					:     Optional[int]						,		a					:     int=None						,		a					:     Optional[int]=None						,		a					:     str=None						,		a					:     Any=None						,		a					:     Any=None						,		a					:     Any=None						,		a					:     List[str]=None						,		)					->      Tuple:






    """simple docstring"""


    A__         =    self.albert(
        input_ids=_snake_case						,		attention_mask=_snake_case						,		token_type_ids=_snake_case						,		position_ids=_snake_case						,		head_mask=_snake_case						,		inputs_embeds=_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(_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'''







import hashlib
import importlib.util
import json
import os
import pathlib
from types import ModuleType

import pytest
import requests

lowerCAmelCase_    	=							pathlib.Path.cwd().joinpath('''project_euler''')
lowerCAmelCase_    	=							pathlib.Path.cwd().joinpath(
    '''scripts''', '''project_euler_answers.json'''
)

with open(PROJECT_EULER_ANSWERS_PATH) as file_handle:
    lowerCAmelCase_    	=							json.load(file_handle)







def 	_A			(   UpperCAmelCase	):






   '''simple docstring'''







   A__         =    importlib.util.spec_from_file_location(file_path.name			,str(UpperCAmelCase	)	)
   A__         =    importlib.util.module_from_spec(UpperCAmelCase	)  # type: ignore
   spec.loader.exec_module(UpperCAmelCase	)  # type: ignore
   return module







def 	_A			(   ):






   '''simple docstring'''







   A__         =    []
   for problem_dir_path in PROJECT_EULER_DIR_PATH.iterdir():
      if problem_dir_path.is_file() or problem_dir_path.name.startswith('_'	):
         continue
      for file_path in problem_dir_path.iterdir():
         if file_path.suffix != ".py" or file_path.name.startswith(('_', 'test')	):
            continue
         solution_file_paths.append(UpperCAmelCase	)
   return solution_file_paths







def 	_A			(   ):






   '''simple docstring'''







   with open(os.environ['GITHUB_EVENT_PATH']	) as file:
      A__         =    json.load(UpperCAmelCase	)
   return event["pull_request"]["url"] + "/files"







def 	_A			(   ):






   '''simple docstring'''







   A__         =    []
   A__         =    {
       'Accept': 'application/vnd.github.v3+json',
       'Authorization': 'token ' + os.environ['GITHUB_TOKEN'],
   }
   A__         =    requests.get(get_files_url()			,headers=UpperCAmelCase	).json()
   for file in files:
      A__         =    pathlib.Path.cwd().joinpath(file['filename']	)
      if (
          filepath.suffix != ".py"
          or filepath.name.startswith(('_', 'test')	)
          or not filepath.name.startswith('sol'	)
      ):
         continue
      solution_file_paths.append(UpperCAmelCase	)
   return solution_file_paths







def 	_A			(   ):






   '''simple docstring'''







   if os.environ.get('CI'	) and os.environ.get('GITHUB_EVENT_NAME'	) == "pull_request":
      # Return only if there are any, otherwise default to all solutions
      if filepaths := added_solution_file_path():
         return filepaths
   return all_solution_file_paths()







@pytest.mark.parametrize(
    'solution_path'			,collect_solution_file_paths()			,ids=lambda UpperCAmelCase	: F"""{path.parent.name}/{path.name}"""			,)
def 	_A			(   UpperCAmelCase	):






   '''simple docstring'''







   A__         =    solution_path.parent.name[8:].zfill(3	)
   A__         =    PROBLEM_ANSWERS[problem_number]
   A__         =    convert_path_to_module(UpperCAmelCase	)
   A__         =    str(solution_module.solution()	)  # type: ignore
   A__         =    hashlib.shaaaa(answer.encode()	).hexdigest()
   assert (
       answer == expected
   ), F"""Expected solution to {problem_number} to have hash {expected}, got {answer}"""


