



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.bert.modeling_bert import (
    BERT_INPUTS_DOCSTRING,
    BERT_START_DOCSTRING,
    BertEncoder,
    BertModel,
    BertPreTrainedModel,
)


UpperCAmelCase__     = logging.getLogger(__name__)







class     __lowerCAmelCase					(    UpperCamelCase_		):



			def 					_lowerCamelCase (		self     :						Any				,	A     :						List[str]				,	A     :						Dict				,	A     :						int=None				,	A     :						Optional[Any]=None)     ->					Dict:



						"""simple docstring"""






						_UpperCAmelCase	 =     self.layer[current_layer](_a				,	_a				,	head_mask[current_layer])

						_UpperCAmelCase	 =     layer_outputs[0]

						return hidden_states







@add_start_docstrings(
    '''The bare Bert Model transformer with PABEE outputting raw hidden-states without any specific head on top.'''       ,		UpperCamelCase_       ,		)
class     __lowerCAmelCase					(    UpperCamelCase_		):



			def __init__(		self     :						Tuple				,	A     :						Optional[int])     ->					Dict:



						"""simple docstring"""






						super().__init__(_a)

						_UpperCAmelCase	 =     BertEncoderWithPabee(_a)

						self.init_weights()
						_UpperCAmelCase	 =     0
						_UpperCAmelCase	 =     0
						_UpperCAmelCase	 =     0

						_UpperCAmelCase	 =     0



			def 					_lowerCamelCase (		self     :						Union[str, Any]				,	A     :						int)     ->					Dict:



						"""simple docstring"""






						_UpperCAmelCase	 =     threshold



			def 					_lowerCamelCase (		self     :						List[Any]				,	A     :						List[str])     ->					Union[str, Any]:



						"""simple docstring"""






						_UpperCAmelCase	 =     patience



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



						"""simple docstring"""






						_UpperCAmelCase	 =     0
						_UpperCAmelCase	 =     0



			def 					_lowerCamelCase (		self     :						Dict)     ->					Tuple:



						"""simple docstring"""






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

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

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

						# We can provide a self-attention mask of dimensions [batch_size, from_seq_length, to_seq_length]
						# ourselves in which case we just need to make it broadcastable to all heads.
						_UpperCAmelCase	 =     self.get_extended_attention_mask(_a				,	_a				,	_a)

						# If a 2D ou 3D attention mask is provided for the cross-attention
						# we need to make broadcastable to [batch_size, num_heads, seq_length, seq_length]
						if self.config.is_decoder and encoder_hidden_states is not None:
									_UpperCAmelCase	 =     encoder_hidden_states.size()
									_UpperCAmelCase	 =     (encoder_batch_size, encoder_sequence_length)
									if encoder_attention_mask is None:
												_UpperCAmelCase	 =     torch.ones(_a				,	device=_a)
									_UpperCAmelCase	 =     self.invert_attention_mask(_a)
						else:
									_UpperCAmelCase	 =     None

						# Prepare head mask if needed
						# 1.0 in head_mask indicate we keep the head
						# attention_probs has shape bsz x n_heads x N x N
						# input head_mask has shape [num_heads] or [num_hidden_layers x num_heads]
						# and head_mask is converted to shape [num_hidden_layers x batch x num_heads x seq_length x seq_length]
						_UpperCAmelCase	 =     self.get_head_mask(_a				,	self.config.num_hidden_layers)

						_UpperCAmelCase	 =     self.embeddings(
						    input_ids=_a				,	position_ids=_a				,	token_type_ids=_a				,	inputs_embeds=_a)
						_UpperCAmelCase	 =     embedding_output

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

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

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

												_UpperCAmelCase	 =     logits
												if patient_counter == self.patience:
															break
									_UpperCAmelCase	 =     [patient_result]
									self.inference_layers_num += calculated_layer_num
									self.inference_instances_num += 1

						return res







@add_start_docstrings(
    '''Bert 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. '''       ,		UpperCamelCase_       ,		)
class     __lowerCAmelCase					(    UpperCamelCase_		):



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



						"""simple docstring"""






						super().__init__(_a)
						_UpperCAmelCase	 =     config.num_labels

						_UpperCAmelCase	 =     BertModelWithPabee(_a)
						_UpperCAmelCase	 =     nn.Dropout(config.hidden_dropout_prob)
						_UpperCAmelCase	 =     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     :						str				,	A     :						List[Any]=None				,	A     :						Union[str, Any]=None				,	A     :						Dict=None				,	A     :						Dict=None				,	A     :						Any=None				,	A     :						Dict=None				,	A     :						Optional[Any]=None				,	)     ->					List[str]:



						"""simple docstring"""






						_UpperCAmelCase	 =     self.bert(
						    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				,	)

						_UpperCAmelCase	 =     (logits[-1],)

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

						return outputs





import os
from shutil import copyfile
from typing import List, Optional, Tuple

from ...tokenization_utils import AddedToken
from ...tokenization_utils_fast import PreTrainedTokenizerFast
from ...utils import is_sentencepiece_available, logging


if is_sentencepiece_available():
							from .tokenization_xlnet import XLNetTokenizer
else:
							UpperCAmelCase__     = None


UpperCAmelCase__     = logging.get_logger(__name__)

UpperCAmelCase__     = {"vocab_file": "spiece.model", "tokenizer_file": "tokenizer.json"}

UpperCAmelCase__     = {
    "vocab_file": {
        "xlnet-base-cased": "https://huggingface.co/xlnet-base-cased/resolve/main/spiece.model",
        "xlnet-large-cased": "https://huggingface.co/xlnet-large-cased/resolve/main/spiece.model",
    },
    "tokenizer_file": {
        "xlnet-base-cased": "https://huggingface.co/xlnet-base-cased/resolve/main/tokenizer.json",
        "xlnet-large-cased": "https://huggingface.co/xlnet-large-cased/resolve/main/tokenizer.json",
    },
}

UpperCAmelCase__     = {
    "xlnet-base-cased": None,
    "xlnet-large-cased": None,
}

UpperCAmelCase__     = "▁"

# Segments (not really needed)
UpperCAmelCase__     = 0
UpperCAmelCase__     = 1
UpperCAmelCase__     = 2
UpperCAmelCase__     = 3
UpperCAmelCase__     = 4







class     __lowerCAmelCase					(    A		):
			UpperCamelCase           =  VOCAB_FILES_NAMES
			UpperCamelCase           =  PRETRAINED_VOCAB_FILES_MAP
			UpperCamelCase           =  PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES
			UpperCamelCase           =  '''left'''
			UpperCamelCase           =  XLNetTokenizer



			def __init__(		self     :						Any				,	A     :						Union[str, Any]=None				,	A     :						str=None				,	A     :						Tuple=False				,	A     :						Tuple=True				,	A     :						Any=False				,	A     :						List[str]="<s>"				,	A     :						List[str]="</s>"				,	A     :						Optional[int]="<unk>"				,	A     :						Tuple="<sep>"				,	A     :						str="<pad>"				,	A     :						Dict="<cls>"				,	A     :						Dict="<mask>"				,	A     :						Optional[Any]=["<eop>", "<eod>"]				,	**A     :						Optional[Any]				,	)     ->					str:



						"""simple docstring"""






						_UpperCAmelCase	 =     AddedToken(A				,	lstrip=A				,	rstrip=A) if isinstance(A				,	A) else mask_token

						super().__init__(
						    vocab_file=A				,	tokenizer_file=A				,	do_lower_case=A				,	remove_space=A				,	keep_accents=A				,	bos_token=A				,	eos_token=A				,	unk_token=A				,	sep_token=A				,	pad_token=A				,	cls_token=A				,	mask_token=A				,	additional_special_tokens=A				,	**A				,	)

						_UpperCAmelCase	 =     3
						_UpperCAmelCase	 =     do_lower_case
						_UpperCAmelCase	 =     remove_space
						_UpperCAmelCase	 =     keep_accents
						_UpperCAmelCase	 =     vocab_file
						_UpperCAmelCase	 =     False if not self.vocab_file else True



			def 					_lowerCamelCase (		self     :						Tuple				,	A     :						List[int]				,	A     :						Optional[List[int]] = None)     ->					List[int]:



						"""simple docstring"""






						_UpperCAmelCase	 =     [self.sep_token_id]
						_UpperCAmelCase	 =     [self.cls_token_id]
						if token_ids_a is None:
									return token_ids_a + sep + cls
						return token_ids_a + sep + token_ids_a + sep + cls



			def 					_lowerCamelCase (		self     :						Tuple				,	A     :						List[int]				,	A     :						Optional[List[int]] = None)     ->					List[int]:



						"""simple docstring"""






						_UpperCAmelCase	 =     [self.sep_token_id]
						_UpperCAmelCase	 =     [2]

						if token_ids_a is None:
									return len(token_ids_a + sep) * [0] + cls_segment_id
						return len(token_ids_a + sep) * [0] + len(token_ids_a + sep) * [1] + cls_segment_id



			def 					_lowerCamelCase (		self     :						List[str]				,	A     :						str				,	A     :						Optional[str] = None)     ->					Tuple[str]:



						"""simple docstring"""






						if not self.can_save_slow_tokenizer:
									raise ValueError(
									    'Your fast tokenizer does not have the necessary information to save the vocabulary for a slow '
									    'tokenizer.')

						if not os.path.isdir(A):
									logger.error(F"Vocabulary path ({save_directory}) should be a directory")
									return
						_UpperCAmelCase	 =     os.path.join(
						    A				,	(filename_prefix + '-' if filename_prefix else '') + VOCAB_FILES_NAMES['vocab_file'])

						if os.path.abspath(self.vocab_file) != os.path.abspath(A):
									copyfile(self.vocab_file				,	A)

						return (out_vocab_file,)
