




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


_SCREAMING_SNAKE_CASE							:     Union[str, Any]   	=				logging.getLogger(__name__)




class 				_snake_case	(			lowercase_							):




			def      lowerCAmelCase__       (      self       ,							a__       ,							a__       ,							a__=None       ,							a__=None							)       ->       int:

										'''simple docstring'''




										if current_layer == 0:
																	snake_case_			      =   self.embedding_hidden_mapping_in(a__							)
										else:
																	snake_case_			      =   hidden_states[0]

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

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

										snake_case_			      =   self.albert_layer_groups[group_idx](
										    a__       ,							a__       ,							head_mask[group_idx * layers_per_group : (group_idx + 1) * layers_per_group]       ,							)
										snake_case_			      =   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 				_snake_case	(			lowercase_							):




			def __init__(      self       ,							a__							)       ->       Dict:

										'''simple docstring'''




										super().__init__(a__							)

										snake_case_			      =   AlbertTransformerWithPabee(a__							)

										self.init_weights()
										snake_case_			      =   0
										snake_case_			      =   0
										snake_case_			      =   0

										snake_case_			      =   0




			def      lowerCAmelCase__       (      self       ,							a__							)       ->       Optional[int]:

										'''simple docstring'''




										snake_case_			      =   threshold




			def      lowerCAmelCase__       (      self       ,							a__							)       ->       List[Any]:

										'''simple docstring'''




										snake_case_			      =   patience




			def      lowerCAmelCase__       (      self							)       ->       Dict:

										'''simple docstring'''




										snake_case_			      =   0
										snake_case_			      =   0




			def      lowerCAmelCase__       (      self							)       ->       Union[str, Any]:

										'''simple docstring'''




										snake_case_			      =   self.inference_layers_num / self.inference_instances_num
										snake_case_			      =   (
										    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      lowerCAmelCase__       (      self       ,							a__=None       ,							a__=None       ,							a__=None       ,							a__=None       ,							a__=None       ,							a__=None       ,							a__=None       ,							a__=None       ,							a__=False       ,							)       ->       int:

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

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

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

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

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

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

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

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

																								snake_case_			      =   logits
																								if patient_counter == self.patience:
																															break
																	snake_case_			      =   [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. "		,		lowercase_		,		)
class 				_snake_case	(			lowercase_							):




			def __init__(      self       ,							a__							)       ->       Dict:

										'''simple docstring'''




										super().__init__(a__							)
										snake_case_			      =   config.num_labels

										snake_case_			      =   AlbertModelWithPabee(a__							)
										snake_case_			      =   nn.Dropout(config.classifier_dropout_prob							)
										snake_case_			      =   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      lowerCAmelCase__       (      self       ,							a__=None       ,							a__=None       ,							a__=None       ,							a__=None       ,							a__=None       ,							a__=None       ,							a__=None       ,							)       ->       Optional[Any]:

										'''simple docstring'''




										snake_case_			      =   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       ,							)

										snake_case_			      =   (logits[-1],)

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

										return outputs










'''simple docstring'''





import heapq


def   UpperCamelCase_(			snake_case							: List[Any]							,       snake_case							: List[str]							,       snake_case							: Dict  ):

							'''simple docstring'''






							snake_case_			      =   [(0, start)]  # cost from start node,end node
							snake_case_			      =   set()
							while heap:
														((snake_case_)      ,   (snake_case_))			      =   heapq.heappop(snake_case  )
														if u in visited:
																					continue
														visited.add(snake_case  )
														if u == end:
																					return cost
														for v, c in graph[u]:
																					if v in visited:
																												continue
																					snake_case_			      =   cost + c
																					heapq.heappush(snake_case							,       (next_item, v)  )
							return -1


_SCREAMING_SNAKE_CASE							:     Dict   	=				{
    "A": [["B", 2], ["C", 5]],
    "B": [["A", 2], ["D", 3], ["E", 1], ["F", 1]],
    "C": [["A", 5], ["F", 3]],
    "D": [["B", 3]],
    "E": [["B", 4], ["F", 3]],
    "F": [["C", 3], ["E", 3]],
}
_SCREAMING_SNAKE_CASE							:     Optional[int]   	=				{
    "B": [["C", 1]],
    "C": [["D", 1]],
    "D": [["F", 1]],
    "E": [["B", 1], ["F", 3]],
    "F": [],
}
_SCREAMING_SNAKE_CASE							:     Any   	=				{
    "B": [["C", 1]],
    "C": [["D", 1]],
    "D": [["F", 1]],
    "E": [["B", 1], ["G", 2]],
    "F": [],
    "G": [["F", 1]],
}

_SCREAMING_SNAKE_CASE							:     Tuple   	=				dijkstra(G, "E", "C")
print(short_distance)  # E -- 3 --> F -- 3 --> C == 6

_SCREAMING_SNAKE_CASE							:     str   	=				dijkstra(Ga, "E", "F")
print(short_distance)  # E -- 3 --> F == 3

_SCREAMING_SNAKE_CASE							:     List[Any]   	=				dijkstra(Ga, "E", "F")
print(short_distance)  # E -- 2 --> G -- 1 --> F == 3

if __name__ == "__main__":
		import doctest

		doctest.testmod()




