






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


_UpperCamelCase											=			logging.getLogger(__name__)



class _A  (    a__    ):





		def 							__A       (     self ,       __UpperCAmelCase ,       __UpperCAmelCase ,       __UpperCAmelCase=None ,       __UpperCAmelCase=None				)       ->     int:

					'''simple docstring'''
					if current_layer == 0:
								__UpperCAmelCase   :      Dict										=     self.embedding_hidden_mapping_in(SCREAMING_SNAKE_CASE_				)
					else:
								__UpperCAmelCase   :      Optional[Any]										=     hidden_states[0]

					__UpperCAmelCase   :      List[str]										=     int(self.config.num_hidden_layers / self.config.num_hidden_groups				)

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

					__UpperCAmelCase   :      str										=     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] ,       )
					__UpperCAmelCase   :      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."	,				a__	,				)
class _A  (    a__    ):





		def __init__(     self ,       __UpperCAmelCase				)       ->     Tuple:

					'''simple docstring'''
					super().__init__(SCREAMING_SNAKE_CASE_				)

					__UpperCAmelCase   :      Any										=     AlbertTransformerWithPabee(SCREAMING_SNAKE_CASE_				)

					self.init_weights()
					__UpperCAmelCase   :      List[str]										=     0
					__UpperCAmelCase   :      Optional[int]										=     0
					__UpperCAmelCase   :      List[str]										=     0

					__UpperCAmelCase   :      Dict										=     0





		def 							__A       (     self ,       __UpperCAmelCase				)       ->     Dict:

					'''simple docstring'''
					__UpperCAmelCase   :      List[str]										=     threshold





		def 							__A       (     self ,       __UpperCAmelCase				)       ->     Optional[int]:

					'''simple docstring'''
					__UpperCAmelCase   :      Optional[int]										=     patience





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

					'''simple docstring'''
					__UpperCAmelCase   :      Tuple										=     0
					__UpperCAmelCase   :      Optional[Any]										=     0





		def 							__A       (     self				)       ->     List[Any]:

					'''simple docstring'''
					__UpperCAmelCase   :      Union[str, Any]										=     self.inference_layers_num / self.inference_instances_num
					__UpperCAmelCase   :      Union[str, Any]										=     (
					    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 ,       __UpperCAmelCase=None ,       __UpperCAmelCase=None ,       __UpperCAmelCase=None ,       __UpperCAmelCase=None ,       __UpperCAmelCase=None ,       __UpperCAmelCase=None ,       __UpperCAmelCase=None ,       __UpperCAmelCase=None ,       __UpperCAmelCase=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:
								__UpperCAmelCase   :      str										=     input_ids.size()
					elif inputs_embeds is not None:
								__UpperCAmelCase   :      Any										=     inputs_embeds.size()[:-1]
					else:
								raise ValueError("""You have to specify either input_ids or inputs_embeds"""				)

					__UpperCAmelCase   :      Tuple										=     input_ids.device if input_ids is not None else inputs_embeds.device

					if attention_mask is None:
								__UpperCAmelCase   :      Optional[int]										=     torch.ones(SCREAMING_SNAKE_CASE_ ,       device=SCREAMING_SNAKE_CASE_				)
					if token_type_ids is None:
								__UpperCAmelCase   :      List[Any]										=     torch.zeros(SCREAMING_SNAKE_CASE_ ,       dtype=torch.long ,       device=SCREAMING_SNAKE_CASE_				)

					__UpperCAmelCase   :      Optional[Any]										=     attention_mask.unsqueeze(1				).unsqueeze(2				)
					__UpperCAmelCase   :      int										=     extended_attention_mask.to(dtype=self.dtype				)  # fp16 compatibility
					__UpperCAmelCase   :      Tuple										=     (1.0 - extended_attention_mask) * -10_000.0
					__UpperCAmelCase   :      Any										=     self.get_head_mask(SCREAMING_SNAKE_CASE_ ,       self.config.num_hidden_layers				)

					__UpperCAmelCase   :      Tuple										=     self.embeddings(
					    SCREAMING_SNAKE_CASE_ ,       position_ids=SCREAMING_SNAKE_CASE_ ,       token_type_ids=SCREAMING_SNAKE_CASE_ ,       inputs_embeds=SCREAMING_SNAKE_CASE_				)
					__UpperCAmelCase   :      Optional[int]										=     embedding_output

					if self.training:
								__UpperCAmelCase   :      List[Any]										=     []
								for i in range(self.config.num_hidden_layers				):
											__UpperCAmelCase   :      Tuple										=     self.encoder.adaptive_forward(
											    SCREAMING_SNAKE_CASE_ ,       current_layer=SCREAMING_SNAKE_CASE_ ,       attention_mask=SCREAMING_SNAKE_CASE_ ,       head_mask=SCREAMING_SNAKE_CASE_ ,       )

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

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

											__UpperCAmelCase   :      Dict										=     logits
											if patient_counter == self.patience:
														break
								__UpperCAmelCase   :      Tuple										=     [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 _A  (    a__    ):





		def __init__(     self ,       __UpperCAmelCase				)       ->     Optional[Any]:

					'''simple docstring'''
					super().__init__(SCREAMING_SNAKE_CASE_				)
					__UpperCAmelCase   :      str										=     config.num_labels

					__UpperCAmelCase   :      List[str]										=     AlbertModelWithPabee(SCREAMING_SNAKE_CASE_				)
					__UpperCAmelCase   :      Union[str, Any]										=     nn.Dropout(config.classifier_dropout_prob				)
					__UpperCAmelCase   :      Tuple										=     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 ,       __UpperCAmelCase=None ,       __UpperCAmelCase=None ,       __UpperCAmelCase=None ,       __UpperCAmelCase=None ,       __UpperCAmelCase=None ,       __UpperCAmelCase=None ,       __UpperCAmelCase=None ,       )       ->     Any:

					'''simple docstring'''
					__UpperCAmelCase   :      Tuple										=     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 ,       )

					__UpperCAmelCase   :      Any										=     (logits[-1],)

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

					return outputs









'''simple docstring'''
from .chunk_utils import chunk_layer
from .data_transforms import make_atomaa_masks
from .feats import atomaa_to_atomaa, frames_and_literature_positions_to_atomaa_pos, torsion_angles_to_frames
from .loss import compute_predicted_aligned_error, compute_tm
from .protein import Protein as OFProtein
from .protein import to_pdb
from .rigid_utils import Rigid, Rotation
from .tensor_utils import dict_multimap, flatten_final_dims, permute_final_dims




