




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  :		Union[str, Any]		     =   logging.getLogger(__name__)







class 				UpperCAmelCase(					snake_case_					):



	"""simple docstring"""






	def    __a     (					self	,				lowerCamelCase	,				lowerCamelCase	,				lowerCamelCase=None	,				lowerCamelCase=None    ) ->      List[str]:





								"""simple docstring"""


								if current_layer == 0:
															lowercase__        =							self.embedding_hidden_mapping_in(lowerCamelCase    )
								else:
															lowercase__        =							hidden_states[0]

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

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

								lowercase__        =							self.albert_layer_groups[group_idx](
								    lowerCamelCase	,				lowerCamelCase	,				head_mask[group_idx * layers_per_group : (group_idx + 1) * layers_per_group]	,				)
								lowercase__        =							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."""   ,     snake_case_   ,     )
class 				UpperCAmelCase(					snake_case_					):



	"""simple docstring"""






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





								"""simple docstring"""


								super().__init__(lowerCamelCase    )

								lowercase__        =							AlbertTransformerWithPabee(lowerCamelCase    )

								self.init_weights()
								lowercase__        =							0
								lowercase__        =							0
								lowercase__        =							0

								lowercase__        =							0

	def    __a     (					self	,				lowerCamelCase    ) ->      Optional[Any]:





								"""simple docstring"""


								lowercase__        =							threshold

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





								"""simple docstring"""


								lowercase__        =							patience

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





								"""simple docstring"""


								lowercase__        =							0
								lowercase__        =							0

	def    __a     (					self    ) ->      List[str]:





								"""simple docstring"""


								lowercase__        =							self.inference_layers_num / self.inference_instances_num
								lowercase__        =							(
								    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(lowerCamelCase    )







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





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

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

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

								lowercase__        =							attention_mask.unsqueeze(1    ).unsqueeze(2    )
								lowercase__        =							extended_attention_mask.to(dtype=self.dtype    )  # fp16 compatibility
								lowercase__        =							(1.0 - extended_attention_mask) * -10000.0
								lowercase__        =							self.get_head_mask(lowerCamelCase	,				self.config.num_hidden_layers    )

								lowercase__        =							self.embeddings(
								    lowerCamelCase	,				position_ids=lowerCamelCase	,				token_type_ids=lowerCamelCase	,				inputs_embeds=lowerCamelCase    )
								lowercase__        =							embedding_output

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

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

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

																						lowercase__        =							logits
																						if patient_counter == self.patience:
																													break
															lowercase__        =							[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
    the pooled output) e.g. for GLUE tasks. """   ,     snake_case_   ,     )
class 				UpperCAmelCase(					snake_case_					):



	"""simple docstring"""






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





								"""simple docstring"""


								super().__init__(lowerCamelCase    )
								lowercase__        =							config.num_labels

								lowercase__        =							AlbertModelWithPabee(lowerCamelCase    )
								lowercase__        =							nn.Dropout(config.classifier_dropout_prob    )
								lowercase__        =							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(lowerCamelCase    )
	def    __a     (					self	,				lowerCamelCase=None	,				lowerCamelCase=None	,				lowerCamelCase=None	,				lowerCamelCase=None	,				lowerCamelCase=None	,				lowerCamelCase=None	,				lowerCamelCase=None	,				) ->      Optional[int]:





								"""simple docstring"""


								lowercase__        =							self.albert(
								    input_ids=lowerCamelCase	,				attention_mask=lowerCamelCase	,				token_type_ids=lowerCamelCase	,				position_ids=lowerCamelCase	,				head_mask=lowerCamelCase	,				inputs_embeds=lowerCamelCase	,				output_dropout=self.dropout	,				output_layers=self.classifiers	,				regression=self.num_labels == 1	,				)

								lowercase__        =							(logits[-1],)

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

								return outputs





def  snake_case_					(		)      ->			int:

							for a in range(3_00    ):
														for b in range(a + 1    ,4_00    ):
																					for c in range(b + 1    ,5_00    ):
																												if (a + b + c) == 10_00 and (a**2) + (b**2) == (c**2):
																																			return a * b * c

							return -1
def  snake_case_					(		)      ->			int:

							for a in range(3_00    ):
														for b in range(4_00    ):
																					lowercase__  :	Dict        =							10_00 - a - b
																					if a < b < c and (a**2) + (b**2) == (c**2):
																												return a * b * c

							return -1
def  snake_case_					(		)      ->			None:
							import timeit

							print(
							    timeit.timeit("solution()"    ,setup="from __main__ import solution"    ,number=10_00    )    )
							print(
							    timeit.timeit(
							        "solution_fast()"    ,setup="from __main__ import solution_fast"    ,number=10_00    )    )


if __name__ == "__main__":
						print(f'{solution() = }')