



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


UpperCamelCase__:     List[str]										=    logging.getLogger(__name__)

class        SCREAMING_SNAKE_CASE(							A__    ):







	"""simple docstring"""






	def   A			(		self    :  str	,    __snake_case    :  Tuple	,    __snake_case    :  int	,    __snake_case    :  List[Any]=None	,    __snake_case    :  List[Any]=None	)    ->	Optional[int]:
								UpperCAmelCase				: Union[str, Any]											=	self.layer[current_layer](__snake_case	,    __snake_case	,    head_mask[current_layer]	)

								UpperCAmelCase				: Optional[Any]											=	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."""							,	A__							,	)
class        SCREAMING_SNAKE_CASE(							A__    ):







	"""simple docstring"""






	def __init__(		self    :  int	,    __snake_case    :  Any	)    ->	Any:
								super().__init__(__snake_case	)

								UpperCAmelCase				: Any											=	BertEncoderWithPabee(__snake_case	)

								self.init_weights()
								UpperCAmelCase				: Optional[Any]											=	0
								UpperCAmelCase				: List[str]											=	0
								UpperCAmelCase				: Tuple											=	0

								UpperCAmelCase				: List[str]											=	0







	def   A			(		self    :  List[str]	,    __snake_case    :  List[str]	)    ->	List[Any]:
								UpperCAmelCase				: Union[str, Any]											=	threshold







	def   A			(		self    :  str	,    __snake_case    :  List[str]	)    ->	Optional[Any]:
								UpperCAmelCase				: List[str]											=	patience







	def   A			(		self    :  Dict	)    ->	str:
								UpperCAmelCase				: List[Any]											=	0
								UpperCAmelCase				: Dict											=	0







	def   A			(		self    :  Optional[Any]	)    ->	Optional[int]:
								UpperCAmelCase				: Dict											=	self.inference_layers_num / self.inference_instances_num
								UpperCAmelCase				: 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   A			(		self    :  Union[str, Any]	,    __snake_case    :  Optional[Any]=None	,    __snake_case    :  str=None	,    __snake_case    :  int=None	,    __snake_case    :  Optional[Any]=None	,    __snake_case    :  Tuple=None	,    __snake_case    :  Tuple=None	,    __snake_case    :  Tuple=None	,    __snake_case    :  Optional[Any]=None	,    __snake_case    :  Union[str, Any]=None	,    __snake_case    :  Optional[int]=None	,    __snake_case    :  Optional[int]=False	,    )    ->	List[str]:

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

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

								if attention_mask is None:
															UpperCAmelCase				: Optional[Any]											=	torch.ones(__snake_case	,    device=__snake_case	)
								if token_type_ids is None:
															UpperCAmelCase				: Tuple											=	torch.zeros(__snake_case	,    dtype=torch.long	,    device=__snake_case	)

								# 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				: torch.Tensor											=	self.get_extended_attention_mask(__snake_case	,    __snake_case	,    __snake_case	)

								# 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							,	UpperCAmelCase							,	UpperCAmelCase				: Any											=	encoder_hidden_states.size()
															UpperCAmelCase				: List[Any]											=	(encoder_batch_size, encoder_sequence_length)
															if encoder_attention_mask is None:
																						UpperCAmelCase				: str											=	torch.ones(__snake_case	,    device=__snake_case	)
															UpperCAmelCase				: Tuple											=	self.invert_attention_mask(__snake_case	)
								else:
															UpperCAmelCase				: List[str]											=	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				: List[Any]											=	self.get_head_mask(__snake_case	,    self.config.num_hidden_layers	)

								UpperCAmelCase				: Any											=	self.embeddings(
								    input_ids=__snake_case	,    position_ids=__snake_case	,    token_type_ids=__snake_case	,    inputs_embeds=__snake_case	)
								UpperCAmelCase				: Optional[int]											=	embedding_output

								if self.training:
															UpperCAmelCase				: str											=	[]
															for i in range(self.config.num_hidden_layers	):
																						UpperCAmelCase				: Optional[int]											=	self.encoder.adaptive_forward(
																						    __snake_case	,    current_layer=__snake_case	,    attention_mask=__snake_case	,    head_mask=__snake_case	)

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

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

																						UpperCAmelCase				: List[str]											=	logits
																						if patient_counter == self.patience:
																													break
															UpperCAmelCase				: List[Any]											=	[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. """							,	A__							,	)
class        SCREAMING_SNAKE_CASE(							A__    ):







	"""simple docstring"""






	def __init__(		self    :  str	,    __snake_case    :  str	)    ->	Optional[Any]:
								super().__init__(__snake_case	)
								UpperCAmelCase				: Optional[int]											=	config.num_labels

								UpperCAmelCase				: Optional[Any]											=	BertModelWithPabee(__snake_case	)
								UpperCAmelCase				: Tuple											=	nn.Dropout(config.hidden_dropout_prob	)
								UpperCAmelCase				: Dict											=	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   A			(		self    :  Any	,    __snake_case    :  Dict=None	,    __snake_case    :  str=None	,    __snake_case    :  List[Any]=None	,    __snake_case    :  List[Any]=None	,    __snake_case    :  Union[str, Any]=None	,    __snake_case    :  int=None	,    __snake_case    :  str=None	,    )    ->	Union[str, Any]:
								UpperCAmelCase				: Optional[int]											=	self.bert(
								    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	,    )

								UpperCAmelCase				: List[str]											=	(logits[-1],)

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

								return outputs










'''simple docstring'''






import argparse
import logging
import os

import datasets
import tensorflow as tf

from transformers import AutoTokenizer


UpperCamelCase__:     List[Any]										=    logging.getLogger(__name__)


def 				snake_case_  (      )   ->     int:
							UpperCAmelCase				: int											=	argparse.ArgumentParser(
							    description='''Prepare TFRecord shards from pre-tokenized samples of the wikitext dataset.'''						)
							parser.add_argument(
							    '''--dataset_name'''     ,   type=_lowerCAmelCase     ,   default='''wikitext'''     ,   help='''Name of the training. Explore datasets at: hf.co/datasets.'''     ,   )
							parser.add_argument(
							    '''--dataset_config'''     ,   type=_lowerCAmelCase     ,   default='''wikitext-103-raw-v1'''     ,   help='''Configuration name of the dataset.'''						)
							parser.add_argument(
							    '''--tokenizer_name_or_path'''     ,   type=_lowerCAmelCase     ,   default='''sayakpaul/unigram-tokenizer-wikitext'''     ,   help='''Tokenizer identifier. Can be a local filepath or a Hub identifier.'''     ,   )
							parser.add_argument(
							    '''--shard_size'''     ,   type=_lowerCAmelCase     ,   default=1000     ,   help='''Number of entries to go in a single shard.'''     ,   )
							parser.add_argument('''--split'''     ,   type=_lowerCAmelCase     ,   default='''train'''     ,   choices=['''train''', '''test''', '''validation''']						)
							parser.add_argument(
							    '''--limit'''     ,   default=_lowerCAmelCase     ,   type=_lowerCAmelCase     ,   help='''Limit the number of shards (used for debugging).'''     ,   )
							parser.add_argument(
							    '''--max_length'''     ,   type=_lowerCAmelCase     ,   default=512     ,   help='''Maximum sequence length. For training on TPUs, it helps to have a maximum'''
							    ''' sequence length that is a multiple of 8.'''     ,   )
							parser.add_argument(
							    '''--output_dir'''     ,   default='''tf-tpu'''     ,   type=_lowerCAmelCase     ,   help='''Output directory where the TFRecord shards will be saved. If the'''
							    ''' path is appended with `gs://` (\'gs://tf-tpu\', for example) then the TFRecord'''
							    ''' shards will be directly saved to a Google Cloud Storage bucket.'''     ,   )

							UpperCAmelCase				: List[str]											=	parser.parse_args()
							return args


def 				snake_case_  (      _lowerCAmelCase						:		Union[str, Any]						)   ->     Optional[int]:
							def fn(_lowerCAmelCase						:		Tuple						):
														return tokenizer(examples['''text''']						)

							return fn


def 				snake_case_  (      _lowerCAmelCase						:		int						)   ->     Dict:
							UpperCAmelCase				: Optional[Any]											=	[]
							for i in range(len(tokenized_data['''input_ids''']						)						):
														UpperCAmelCase				: Optional[Any]											=	{
														    '''input_ids''': tf.train.Feature(intaa_list=tf.train.IntaaList(value=tokenized_data['''input_ids'''][i]						)						),
														    '''attention_mask''': tf.train.Feature(
														        intaa_list=tf.train.IntaaList(value=tokenized_data['''attention_mask'''][i]						)						),
														}
														UpperCAmelCase				: Optional[Any]											=	tf.train.Features(feature=_lowerCAmelCase						)
														UpperCAmelCase				: Union[str, Any]											=	tf.train.Example(features=_lowerCAmelCase						)
														UpperCAmelCase				: Any											=	example.SerializeToString()
														records.append(_lowerCAmelCase						)
							return records


def 				snake_case_  (      _lowerCAmelCase						:		List[str]						)   ->     List[str]:
							UpperCAmelCase				: List[str]											=	datasets.load_dataset(args.dataset_name     ,   args.dataset_config     ,   split=args.split						)

							if args.limit is not None:
														UpperCAmelCase				: List[Any]											=	min(len(_lowerCAmelCase						)     ,   args.limit						)
														UpperCAmelCase				: Dict											=	dataset.select(range(_lowerCAmelCase						)						)
														print(f"""Limiting the dataset to {args.limit} entries."""						)

							UpperCAmelCase				: List[Any]											=	AutoTokenizer.from_pretrained(args.tokenizer_name_or_path						)

							# Handle output directory creation.
							# For serializing into a Google Cloud Storage Bucket, one needs to first
							# create a bucket.
							if "gs" not in args.output_dir:
														if not os.path.exists(args.output_dir						):
																					os.makedirs(args.output_dir						)
														UpperCAmelCase				: List[Any]											=	os.path.join(args.output_dir     ,   args.split						)
														if not os.path.exists(_lowerCAmelCase						):
																					os.makedirs(_lowerCAmelCase						)
							else:
														UpperCAmelCase				: Tuple											=	os.path.join(args.output_dir     ,   args.split						)

							# Tokenize the whole dataset at once.
							UpperCAmelCase				: Optional[Any]											=	tokenize_function(_lowerCAmelCase						)
							UpperCAmelCase				: int											=	dataset.map(_lowerCAmelCase     ,   batched=_lowerCAmelCase     ,   num_proc=4     ,   remove_columns=['''text''']						)

							# We need to concatenate all our texts together, and then split the result
							# into chunks of a fixed size, which we will call block_size. To do this, we
							# will use the map method again, with the option batched=True. When we use batched=True,
							# the function we pass to map() will be passed multiple inputs at once, allowing us
							# to group them into more or fewer examples than we had in the input.
							# This allows us to create our new fixed-length samples. The advantage of this
							# method is that we don't lose a whole lot of content from the dataset compared to the
							# case where we simply tokenize with a pre-defined max_length.

							def group_texts(_lowerCAmelCase						:		List[str]						):
														# Concatenate all texts.
														UpperCAmelCase				: Optional[Any]											=	{k: sum(examples[k]     ,   []						) for k in examples.keys()}
														UpperCAmelCase				: Optional[Any]											=	len(concatenated_examples[list(examples.keys()						)[0]]						)
														# We drop the small remainder, though you could add padding instead if the model supports it
														# In this, as in all things, we advise you to follow your heart 🫀
														UpperCAmelCase				: Any											=	(total_length // args.max_length) * args.max_length
														# Split by chunks of max_len.
														UpperCAmelCase				: List[str]											=	{
														    k: [t[i : i + args.max_length] for i in range(0     ,   _lowerCAmelCase     ,   args.max_length						)]
														    for k, t in concatenated_examples.items()
														}
														return result

							UpperCAmelCase				: str											=	dataset_tokenized.map(_lowerCAmelCase     ,   batched=_lowerCAmelCase     ,   batch_size=1000     ,   num_proc=4						)

							UpperCAmelCase				: Union[str, Any]											=	0
							UpperCAmelCase				: Dict											=	0
							for shard in range(0     ,   len(_lowerCAmelCase						)     ,   args.shard_size						):
														UpperCAmelCase				: Union[str, Any]											=	grouped_dataset[shard : shard + args.shard_size]
														UpperCAmelCase				: Optional[int]											=	len(dataset_snapshot['''input_ids''']						)
														UpperCAmelCase				: Dict											=	os.path.join(_lowerCAmelCase     ,   f"""dataset-{shard_count}-{records_containing}.tfrecord"""						)
														UpperCAmelCase				: List[Any]											=	get_serialized_examples(_lowerCAmelCase						)

														with tf.io.TFRecordWriter(_lowerCAmelCase						) as out_file:
																					for i in range(len(_lowerCAmelCase						)						):
																												UpperCAmelCase				: Any											=	serialized_examples[i]
																												out_file.write(_lowerCAmelCase						)
																					print('''Wrote file {} containing {} records'''.format(_lowerCAmelCase     ,   _lowerCAmelCase						)						)

														shard_count += 1
														total_records += records_containing

							with open(f"""split-{args.split}-records-count.txt"""     ,   '''w'''						) as f:
														print(f"""Total {args.split} records: {total_records}"""     ,   file=_lowerCAmelCase						)


if __name__ == "__main__":
						UpperCamelCase__:     List[Any]										=    parse_args()
						main(args)





