






"""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 				__SCREAMING_SNAKE_CASE    (					UpperCamelCase		):


							def     _UpperCamelCase							(      self   :    Any      ,     snake_case   :    Union[str, Any]      ,     snake_case   :    Optional[int]      ,     snake_case   :    int=None      ,     snake_case   :    Any=None				):
													'''simple docstring'''





													if current_layer == 0:
																			A__    :	str			=	self.embedding_hidden_mapping_in(snake_case				)
													else:
																			A__    :	Tuple			=	hidden_states[0]

													A__    :	Optional[Any]			=	int(self.config.num_hidden_layers / self.config.num_hidden_groups				)

													# Index of the hidden group
													A__    :	Optional[int]			=	int(current_layer / (self.config.num_hidden_layers / self.config.num_hidden_groups)				)

													A__    :	List[str]			=	self.albert_layer_groups[group_idx](
													    snake_case      ,     snake_case      ,     head_mask[group_idx * layers_per_group : (group_idx + 1) * layers_per_group]      ,     )
													A__    :	str			=	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 				__SCREAMING_SNAKE_CASE    (					UpperCamelCase		):


							def __init__(      self   :    Optional[Any]      ,     snake_case   :    Dict				):
													'''simple docstring'''





													super().__init__(snake_case				)

													A__    :	Optional[Any]			=	AlbertTransformerWithPabee(snake_case				)

													self.init_weights()
													A__    :	int			=	0
													A__    :	Any			=	0
													A__    :	Optional[int]			=	0

													A__    :	Union[str, Any]			=	0


							def     _UpperCamelCase							(      self   :    int      ,     snake_case   :    Tuple				):
													'''simple docstring'''





													A__    :	Optional[int]			=	threshold


							def     _UpperCamelCase							(      self   :    Dict      ,     snake_case   :    str				):
													'''simple docstring'''





													A__    :	int			=	patience


							def     _UpperCamelCase							(      self   :    Tuple				):
													'''simple docstring'''





													A__    :	Tuple			=	0
													A__    :	str			=	0


							def     _UpperCamelCase							(      self   :    int				):
													'''simple docstring'''





													A__    :	List[Any]			=	self.inference_layers_num / self.inference_instances_num
													A__    :	Optional[int]			=	(
													    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   :    Any      ,     snake_case   :    Union[str, Any]=None      ,     snake_case   :    Optional[Any]=None      ,     snake_case   :    Dict=None      ,     snake_case   :    int=None      ,     snake_case   :    Dict=None      ,     snake_case   :    List[Any]=None      ,     snake_case   :    Dict=None      ,     snake_case   :    List[str]=None      ,     snake_case   :    int=False      ,     ):
													'''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__    :	Any			=	input_ids.size()
													elif inputs_embeds is not None:
																			A__    :	List[Any]			=	inputs_embeds.size()[:-1]
													else:
																			raise ValueError("""You have to specify either input_ids or inputs_embeds"""				)

													A__    :	Optional[Any]			=	input_ids.device if input_ids is not None else inputs_embeds.device

													if attention_mask is None:
																			A__    :	Tuple			=	torch.ones(snake_case      ,     device=snake_case				)
													if token_type_ids is None:
																			A__    :	Any			=	torch.zeros(snake_case      ,     dtype=torch.long      ,     device=snake_case				)

													A__    :	Dict			=	attention_mask.unsqueeze(1				).unsqueeze(2				)
													A__    :	str			=	extended_attention_mask.to(dtype=self.dtype				)  # fp16 compatibility
													A__    :	Optional[Any]			=	(1.0 - extended_attention_mask) * -10000.0
													A__    :	Optional[int]			=	self.get_head_mask(snake_case      ,     self.config.num_hidden_layers				)

													A__    :	List[Any]			=	self.embeddings(
													    snake_case      ,     position_ids=snake_case      ,     token_type_ids=snake_case      ,     inputs_embeds=snake_case				)
													A__    :	List[Any]			=	embedding_output

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

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

																									A__    :	List[str]			=	self.pooler_activation(self.pooler(encoder_outputs[0][:, 0]				)				)
																									A__    :	Dict			=	output_layers[i](snake_case				)
																									if regression:
																															A__    :	int			=	logits.detach()
																															if patient_result is not None:
																																					A__    :	Optional[Any]			=	patient_result.detach()
																															if (patient_result is not None) and torch.abs(patient_result - labels				) < self.regression_threshold:
																																					patient_counter += 1
																															else:
																																					A__    :	Any			=	0
																									else:
																															A__    :	Tuple			=	logits.detach().argmax(dim=1				)
																															if patient_result is not None:
																																					A__    :	Optional[int]			=	patient_result.detach().argmax(dim=1				)
																															if (patient_result is not None) and torch.all(labels.eq(snake_case				)				):
																																					patient_counter += 1
																															else:
																																					A__    :	List[str]			=	0

																									A__    :	Union[str, Any]			=	logits
																									if patient_counter == self.patience:
																															break
																			A__    :	str			=	[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 				__SCREAMING_SNAKE_CASE    (					UpperCamelCase		):


							def __init__(      self   :    Dict      ,     snake_case   :    Union[str, Any]				):
													'''simple docstring'''





													super().__init__(snake_case				)
													A__    :	Union[str, Any]			=	config.num_labels

													A__    :	List[Any]			=	AlbertModelWithPabee(snake_case				)
													A__    :	Union[str, Any]			=	nn.Dropout(config.classifier_dropout_prob				)
													A__    :	List[str]			=	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]      ,     snake_case   :    Tuple=None      ,     snake_case   :    int=None      ,     snake_case   :    Tuple=None      ,     snake_case   :    Union[str, Any]=None      ,     snake_case   :    int=None      ,     snake_case   :    int=None      ,     snake_case   :    Optional[int]=None      ,     ):
													'''simple docstring'''





													A__    :	List[Any]			=	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__    :	Dict			=	(logits[-1],)

													if labels is not None:
																			A__    :	List[Any]			=	None
																			A__    :	Dict			=	0
																			for ix, logits_item in enumerate(snake_case				):
																									if self.num_labels == 1:
																															#  We are doing regression
																															A__    :	Dict			=	MSELoss()
																															A__    :	Optional[Any]			=	loss_fct(logits_item.view(-1				)      ,     labels.view(-1				)				)
																									else:
																															A__    :	Any			=	CrossEntropyLoss()
																															A__    :	Optional[Any]			=	loss_fct(logits_item.view(-1      ,     self.num_labels				)      ,     labels.view(-1				)				)
																									if total_loss is None:
																															A__    :	Union[str, Any]			=	loss
																									else:
																															total_loss += loss * (ix + 1)
																									total_weights += ix + 1
																			A__    :	List[str]			=	(total_loss / total_weights,) + outputs

													return outputs














"""simple docstring"""



import unittest

from transformers import load_tool

from .test_tools_common import ToolTesterMixin


A_     =				'''
Hugging Face was founded in 2016 by French entrepreneurs Clément Delangue, Julien Chaumond, and Thomas Wolf originally as a company that developed a chatbot app targeted at teenagers.[2] After open-sourcing the model behind the chatbot, the company pivoted to focus on being a platform for machine learning.

In March 2021, Hugging Face raised $40 million in a Series B funding round.[3]

On April 28, 2021, the company launched the BigScience Research Workshop in collaboration with several other research groups to release an open large language model.[4] In 2022, the workshop concluded with the announcement of BLOOM, a multilingual large language model with 176 billion parameters.[5]
'''






class 				__SCREAMING_SNAKE_CASE    (					unittest.TestCase							,		UpperCamelCase		):


							def     _UpperCamelCase							(      self   :    int				):
													'''simple docstring'''





													A__    :	Any			=	load_tool("""summarization"""				)
													self.tool.setup()
													A__    :	List[Any]			=	load_tool("""summarization"""      ,     remote=snake_case				)


							def     _UpperCamelCase							(      self   :    Any				):
													'''simple docstring'''





													A__    :	List[Any]			=	self.tool(snake_case				)
													self.assertEqual(
													    snake_case      ,     """Hugging Face was founded in 2016 by French entrepreneurs Clément Delangue, Julien Chaumond, and Thomas Wolf. In March 2021, Hugging Face raised $40 million in a Series B funding round. On April 28, 2021, the company launched the BigScience Research Workshop in collaboration with several other research groups to release an open large language model. In 2022, the workshop concluded with the announcement of BLOOM."""      ,     )


							def     _UpperCamelCase							(      self   :    Optional[int]				):
													'''simple docstring'''





													A__    :	List[str]			=	self.remote_tool(snake_case				)
													self.assertEqual(
													    snake_case      ,     """Hugging Face was founded in 2016 by French entrepreneurs Clément Delangue, Julien Chaumond, and Thomas Wolf. In March 2021, Hugging Face raised $40 million in a Series B funding round. On April 28, 2021, the company launched the BigScience Research Workshop in collaboration with several other research groups to release an open large language model. In 2022, the workshop concluded with the announcement of BLOOM."""      ,     )


							def     _UpperCamelCase							(      self   :    Dict				):
													'''simple docstring'''





													A__    :	List[Any]			=	self.tool(text=snake_case				)
													self.assertEqual(
													    snake_case      ,     """Hugging Face was founded in 2016 by French entrepreneurs Clément Delangue, Julien Chaumond, and Thomas Wolf. In March 2021, Hugging Face raised $40 million in a Series B funding round. On April 28, 2021, the company launched the BigScience Research Workshop in collaboration with several other research groups to release an open large language model. In 2022, the workshop concluded with the announcement of BLOOM."""      ,     )


							def     _UpperCamelCase							(      self   :    Union[str, Any]				):
													'''simple docstring'''





													A__    :	Optional[int]			=	self.remote_tool(text=snake_case				)
													self.assertEqual(
													    snake_case      ,     """Hugging Face was founded in 2016 by French entrepreneurs Clément Delangue, Julien Chaumond, and Thomas Wolf. In March 2021, Hugging Face raised $40 million in a Series B funding round. On April 28, 2021, the company launched the BigScience Research Workshop in collaboration with several other research groups to release an open large language model. In 2022, the workshop concluded with the announcement of BLOOM."""      ,     )






