



"""simple docstring"""







import csv
import dataclasses
import json
from dataclasses import dataclass
from typing import List, Optional, Union

from ...utils import is_tf_available, is_torch_available, logging


UpperCAmelCase:  str			     =				logging.get_logger(__name__)





@dataclass
class   UpperCamelCase	:




	"""simple docstring"""
	SCREAMING_SNAKE_CASE_							:       str
	SCREAMING_SNAKE_CASE_							:       str
	SCREAMING_SNAKE_CASE_							:       Optional[str]						       =  None
	SCREAMING_SNAKE_CASE_							:       Optional[str]						       =  None






	def    lowerCamelCase__					(     self   ):
			return json.dumps(dataclasses.asdict(self   )				,indent=2   ) + "\n"







@dataclass(frozen=snake_case       )
class   UpperCamelCase	:




	"""simple docstring"""
	SCREAMING_SNAKE_CASE_							:       List[int]
	SCREAMING_SNAKE_CASE_							:       Optional[List[int]]						       =  None
	SCREAMING_SNAKE_CASE_							:       Optional[List[int]]						       =  None
	SCREAMING_SNAKE_CASE_							:       Optional[Union[int, float]]						       =  None






	def    lowerCamelCase__					(     self   ):
			return json.dumps(dataclasses.asdict(self   )   ) + "\n"







class   UpperCamelCase	:




	"""simple docstring"""






	def    lowerCamelCase__					(     self				,UpperCAmelCase_   ):
			raise NotImplementedError()






	def    lowerCamelCase__					(     self				,UpperCAmelCase_   ):
			raise NotImplementedError()






	def    lowerCamelCase__					(     self				,UpperCAmelCase_   ):
			raise NotImplementedError()






	def    lowerCamelCase__					(     self				,UpperCAmelCase_   ):
			raise NotImplementedError()






	def    lowerCamelCase__					(     self   ):
			raise NotImplementedError()






	def    lowerCamelCase__					(     self				,UpperCAmelCase_   ):
			if len(self.get_labels()   ) > 1:
					_lowercase				: Optional[int]     =   self.get_labels()[int(example.label   )]
			return example






	@classmethod
	def    lowerCamelCase__					(     cls				,UpperCAmelCase_				,UpperCAmelCase_=None   ):
			with open(UpperCAmelCase_				,"""r"""				,encoding="""utf-8-sig"""   ) as f:
					return list(csv.reader(UpperCAmelCase_				,delimiter="""\t"""				,quotechar=UpperCAmelCase_   )   )







class   UpperCamelCase	(     snake_case       ):




	"""simple docstring"""






	def __init__(     self				,UpperCAmelCase_=None				,UpperCAmelCase_=None				,UpperCAmelCase_="classification"				,UpperCAmelCase_=False   ):
			_lowercase				: int     =   [] if labels is None else labels
			_lowercase				: int     =   [] if examples is None else examples
			_lowercase				: Union[str, Any]     =   mode
			_lowercase				: Optional[Any]     =   verbose






	def __len__(     self   ):
			return len(self.examples   )






	def __getitem__(     self				,UpperCAmelCase_   ):
			if isinstance(UpperCAmelCase_				,UpperCAmelCase_   ):
					return SingleSentenceClassificationProcessor(labels=self.labels				,examples=self.examples[idx]   )
			return self.examples[idx]






	@classmethod
	def    lowerCamelCase__					(     cls				,UpperCAmelCase_				,UpperCAmelCase_=""				,UpperCAmelCase_=0				,UpperCAmelCase_=1				,UpperCAmelCase_=None				,UpperCAmelCase_=False				,**UpperCAmelCase_   ):
			_lowercase				: int     =   cls(**UpperCAmelCase_   )
			processor.add_examples_from_csv(
			    UpperCAmelCase_				,split_name=UpperCAmelCase_				,column_label=UpperCAmelCase_				,column_text=UpperCAmelCase_				,column_id=UpperCAmelCase_				,skip_first_row=UpperCAmelCase_				,overwrite_labels=UpperCAmelCase_				,overwrite_examples=UpperCAmelCase_				,)
			return processor






	@classmethod
	def    lowerCamelCase__					(     cls				,UpperCAmelCase_				,UpperCAmelCase_=None				,**UpperCAmelCase_   ):
			_lowercase				: int     =   cls(**UpperCAmelCase_   )
			processor.add_examples(UpperCAmelCase_				,labels=UpperCAmelCase_   )
			return processor






	def    lowerCamelCase__					(     self				,UpperCAmelCase_				,UpperCAmelCase_=""				,UpperCAmelCase_=0				,UpperCAmelCase_=1				,UpperCAmelCase_=None				,UpperCAmelCase_=False				,UpperCAmelCase_=False				,UpperCAmelCase_=False				,):
			_lowercase				: Dict     =   self._read_tsv(UpperCAmelCase_   )
			if skip_first_row:
					_lowercase				: Union[str, Any]     =   lines[1:]
			_lowercase				: int     =   []
			_lowercase				: Optional[int]     =   []
			_lowercase				: int     =   []
			for i, line in enumerate(UpperCAmelCase_   ):
					texts.append(line[column_text]   )
					labels.append(line[column_label]   )
					if column_id is not None:
							ids.append(line[column_id]   )
					else:
							_lowercase				: Tuple     =   f"""{split_name}-{i}""" if split_name else str(UpperCAmelCase_   )
							ids.append(UpperCAmelCase_   )

			return self.add_examples(
			    UpperCAmelCase_				,UpperCAmelCase_				,UpperCAmelCase_				,overwrite_labels=UpperCAmelCase_				,overwrite_examples=UpperCAmelCase_   )






	def    lowerCamelCase__					(     self				,UpperCAmelCase_				,UpperCAmelCase_=None				,UpperCAmelCase_=None				,UpperCAmelCase_=False				,UpperCAmelCase_=False   ):
			if labels is not None and len(UpperCAmelCase_   ) != len(UpperCAmelCase_   ):
					raise ValueError(
					    f"""Text and labels have mismatched lengths {len(UpperCAmelCase_   )} and {len(UpperCAmelCase_   )}"""   )
			if ids is not None and len(UpperCAmelCase_   ) != len(UpperCAmelCase_   ):
					raise ValueError(f"""Text and ids have mismatched lengths {len(UpperCAmelCase_   )} and {len(UpperCAmelCase_   )}"""   )
			if ids is None:
					_lowercase				: Any     =   [None] * len(UpperCAmelCase_   )
			if labels is None:
					_lowercase				: List[str]     =   [None] * len(UpperCAmelCase_   )
			_lowercase				: Union[str, Any]     =   []
			_lowercase				: int     =   set()
			for text_or_text_and_label, label, guid in zip(UpperCAmelCase_				,UpperCAmelCase_				,UpperCAmelCase_   ):
					if isinstance(UpperCAmelCase_				,(tuple, list)   ) and label is None:
							_lowercase     ,  _lowercase				: Dict     =   text_or_text_and_label
					else:
							_lowercase				: Optional[int]     =   text_or_text_and_label
					added_labels.add(UpperCAmelCase_   )
					examples.append(InputExample(guid=UpperCAmelCase_				,text_a=UpperCAmelCase_				,text_b=UpperCAmelCase_				,label=UpperCAmelCase_   )   )

			# Update examples
			if overwrite_examples:
					_lowercase				: Any     =   examples
			else:
					self.examples.extend(UpperCAmelCase_   )

			# Update labels
			if overwrite_labels:
					_lowercase				: List[str]     =   list(UpperCAmelCase_   )
			else:
					_lowercase				: Optional[int]     =   list(set(self.labels   ).union(UpperCAmelCase_   )   )

			return self.examples






	def    lowerCamelCase__					(     self				,UpperCAmelCase_				,UpperCAmelCase_=None				,UpperCAmelCase_=False				,UpperCAmelCase_=0				,UpperCAmelCase_=True				,UpperCAmelCase_=None				,):
			if max_length is None:
					_lowercase				: Any     =   tokenizer.max_len

			_lowercase				: List[Any]     =   {label: i for i, label in enumerate(self.labels   )}

			_lowercase				: Union[str, Any]     =   []
			for ex_index, example in enumerate(self.examples   ):
					if ex_index % 1_00_00 == 0:
							logger.info(f"""Tokenizing example {ex_index}"""   )

					_lowercase				: Optional[int]     =   tokenizer.encode(
					    example.text_a				,add_special_tokens=UpperCAmelCase_				,max_length=min(UpperCAmelCase_				,tokenizer.max_len   )				,)
					all_input_ids.append(UpperCAmelCase_   )

			_lowercase				: str     =   max(len(UpperCAmelCase_   ) for input_ids in all_input_ids   )

			_lowercase				: Optional[Any]     =   []
			for ex_index, (input_ids, example) in enumerate(zip(UpperCAmelCase_				,self.examples   )   ):
					if ex_index % 1_00_00 == 0:
							logger.info(f"""Writing example {ex_index}/{len(self.examples   )}"""   )
					# The mask has 1 for real tokens and 0 for padding tokens. Only real
					# tokens are attended to.
					_lowercase				: Any     =   [1 if mask_padding_with_zero else 0] * len(UpperCAmelCase_   )

					# Zero-pad up to the sequence length.
					_lowercase				: Dict     =   batch_length - len(UpperCAmelCase_   )
					if pad_on_left:
							_lowercase				: Dict     =   ([pad_token] * padding_length) + input_ids
							_lowercase				: Any     =   ([0 if mask_padding_with_zero else 1] * padding_length) + attention_mask
					else:
							_lowercase				: Optional[Any]     =   input_ids + ([pad_token] * padding_length)
							_lowercase				: int     =   attention_mask + ([0 if mask_padding_with_zero else 1] * padding_length)

					if len(UpperCAmelCase_   ) != batch_length:
							raise ValueError(f"""Error with input length {len(UpperCAmelCase_   )} vs {batch_length}"""   )
					if len(UpperCAmelCase_   ) != batch_length:
							raise ValueError(f"""Error with input length {len(UpperCAmelCase_   )} vs {batch_length}"""   )

					if self.mode == "classification":
							_lowercase				: Dict     =   label_map[example.label]
					elif self.mode == "regression":
							_lowercase				: Optional[int]     =   float(example.label   )
					else:
							raise ValueError(self.mode   )

					if ex_index < 5 and self.verbose:
							logger.info("""*** Example ***"""   )
							logger.info(f"""guid: {example.guid}"""   )
							logger.info(f"""input_ids: {" ".join([str(UpperCAmelCase_   ) for x in input_ids]   )}"""   )
							logger.info(f"""attention_mask: {" ".join([str(UpperCAmelCase_   ) for x in attention_mask]   )}"""   )
							logger.info(f"""label: {example.label} (id = {label})"""   )

					features.append(InputFeatures(input_ids=UpperCAmelCase_				,attention_mask=UpperCAmelCase_				,label=UpperCAmelCase_   )   )

			if return_tensors is None:
					return features
			elif return_tensors == "tf":
					if not is_tf_available():
							raise RuntimeError("""return_tensors set to 'tf' but TensorFlow 2.0 can't be imported"""   )
					import tensorflow as tf

					def gen():
							for ex in features:
									yield ({"input_ids": ex.input_ids, "attention_mask": ex.attention_mask}, ex.label)

					_lowercase				: Tuple     =   tf.data.Dataset.from_generator(
					    UpperCAmelCase_				,({"""input_ids""": tf.intaa, """attention_mask""": tf.intaa}, tf.intaa)				,({"""input_ids""": tf.TensorShape([None]   ), """attention_mask""": tf.TensorShape([None]   )}, tf.TensorShape([]   ))				,)
					return dataset
			elif return_tensors == "pt":
					if not is_torch_available():
							raise RuntimeError("""return_tensors set to 'pt' but PyTorch can't be imported"""   )
					import torch
					from torch.utils.data import TensorDataset

					_lowercase				: int     =   torch.tensor([f.input_ids for f in features]				,dtype=torch.long   )
					_lowercase				: str     =   torch.tensor([f.attention_mask for f in features]				,dtype=torch.long   )
					if self.mode == "classification":
							_lowercase				: Tuple     =   torch.tensor([f.label for f in features]				,dtype=torch.long   )
					elif self.mode == "regression":
							_lowercase				: Any     =   torch.tensor([f.label for f in features]				,dtype=torch.float   )

					_lowercase				: Any     =   TensorDataset(UpperCAmelCase_				,UpperCAmelCase_				,UpperCAmelCase_   )
					return dataset
			else:
					raise ValueError("""return_tensors should be one of 'tf' or 'pt'"""   )











"""simple docstring"""



import timeit
from typing import Callable, Optional

from ..configuration_utils import PretrainedConfig
from ..models.auto.modeling_auto import MODEL_MAPPING, MODEL_WITH_LM_HEAD_MAPPING
from ..utils import is_pyanvml_available, is_torch_available, logging
from .benchmark_utils import (
    Benchmark,
    Memory,
    MemorySummary,
    measure_peak_memory_cpu,
    start_memory_tracing,
    stop_memory_tracing,
)


if is_torch_available():
						import torch

						from .benchmark_args import PyTorchBenchmarkArguments


if is_pyanvml_available():
						import pyanvml.pyanvml as nvml


_snake_case							:   str	  =       logging.get_logger(__name__)




class 							__lowercase(							_UpperCamelCase	):
						lowerCAmelCase_		:				PyTorchBenchmarkArguments
						lowerCAmelCase_		:				PretrainedConfig
						lowerCAmelCase_		:				str=							"PyTorch"






						@property
						def 		lowerCAmelCase (       self)      ->	List[Any]:




											"""simple docstring"""





											return torch.__version__






						def 		lowerCAmelCase (       self     ,    _SCREAMING_SNAKE_CASE     ,    _SCREAMING_SNAKE_CASE     ,    _SCREAMING_SNAKE_CASE)      ->	float:




											"""simple docstring"""





											lowerCamelCase__					=self._prepare_inference_func(_SCREAMING_SNAKE_CASE     ,    _SCREAMING_SNAKE_CASE     ,    _SCREAMING_SNAKE_CASE)
											return self._measure_speed(_inference)






						def 		lowerCAmelCase (       self     ,    _SCREAMING_SNAKE_CASE     ,    _SCREAMING_SNAKE_CASE     ,    _SCREAMING_SNAKE_CASE)      ->	[Memory, Optional[MemorySummary]]:




											"""simple docstring"""





											lowerCamelCase__					=self._prepare_inference_func(_SCREAMING_SNAKE_CASE     ,    _SCREAMING_SNAKE_CASE     ,    _SCREAMING_SNAKE_CASE)
											return self._measure_memory(_inference)






						def 		lowerCAmelCase (       self     ,    _SCREAMING_SNAKE_CASE     ,    _SCREAMING_SNAKE_CASE     ,    _SCREAMING_SNAKE_CASE)      ->	float:




											"""simple docstring"""





											lowerCamelCase__					=self._prepare_train_func(_SCREAMING_SNAKE_CASE     ,    _SCREAMING_SNAKE_CASE     ,    _SCREAMING_SNAKE_CASE)
											return self._measure_speed(_train)






						def 		lowerCAmelCase (       self     ,    _SCREAMING_SNAKE_CASE     ,    _SCREAMING_SNAKE_CASE     ,    _SCREAMING_SNAKE_CASE)      ->	[Memory, Optional[MemorySummary]]:




											"""simple docstring"""





											lowerCamelCase__					=self._prepare_train_func(_SCREAMING_SNAKE_CASE     ,    _SCREAMING_SNAKE_CASE     ,    _SCREAMING_SNAKE_CASE)
											return self._measure_memory(_train)






						def 		lowerCAmelCase (       self     ,    _SCREAMING_SNAKE_CASE     ,    _SCREAMING_SNAKE_CASE     ,    _SCREAMING_SNAKE_CASE)      ->	Callable[[], None]:




											"""simple docstring"""





											lowerCamelCase__					=self.config_dict[model_name]

											if self.args.torchscript:
																lowerCamelCase__					=True

											lowerCamelCase__					=(
											    hasattr(_SCREAMING_SNAKE_CASE     ,    'architectures')
											    and isinstance(config.architectures     ,    _SCREAMING_SNAKE_CASE)
											    and len(config.architectures) > 0
											)
											if not self.args.only_pretrain_model and has_model_class_in_config:
																try:
																					lowerCamelCase__					=config.architectures[0]
																					lowerCamelCase__					=__import__('transformers'     ,    fromlist=[model_class])
																					lowerCamelCase__					=getattr(_SCREAMING_SNAKE_CASE     ,    _SCREAMING_SNAKE_CASE)
																					lowerCamelCase__					=model_cls(_SCREAMING_SNAKE_CASE)
																except ImportError:
																					raise ImportError(
																					    F"{model_class} does not exist. If you just want to test the pretrained model, you might want to"
																					    ' set `--only_pretrain_model` or `args.only_pretrain_model=True`.')
											else:
																lowerCamelCase__					=MODEL_MAPPING[config.__class__](_SCREAMING_SNAKE_CASE)

											model.eval()
											model.to(self.args.device)

											# encoder-decoder has vocab size saved differently
											lowerCamelCase__					=config.vocab_size if hasattr(_SCREAMING_SNAKE_CASE     ,    'vocab_size') else config.encoder.vocab_size
											lowerCamelCase__					=torch.randint(_SCREAMING_SNAKE_CASE     ,    (batch_size, sequence_length)     ,    dtype=torch.long     ,    device=self.args.device)

											if self.args.fpaa:
																logger.info('Running training in Mixed Precision...')
																if not self.args.is_gpu:
																					raise ValueError('Mixed precision is possible only for GPU.')
																# amp seems to have memory leaks so that memory usage
																# is measured using .half() for now https://github.com/NVIDIA/apex/issues/439
																model.half()

											if self.args.torchscript:
																with torch.no_grad():
																					lowerCamelCase__					=torch.jit.trace(_SCREAMING_SNAKE_CASE     ,    _SCREAMING_SNAKE_CASE)
											else:
																lowerCamelCase__					=model

											def encoder_decoder_forward():
																with torch.no_grad():
																					lowerCamelCase__					=inference_model(_SCREAMING_SNAKE_CASE     ,    decoder_input_ids=_SCREAMING_SNAKE_CASE)
																return outputs

											def encoder_forward():
																with torch.no_grad():
																					lowerCamelCase__					=inference_model(_SCREAMING_SNAKE_CASE)
																return outputs

											lowerCamelCase__					=encoder_decoder_forward if config.is_encoder_decoder else encoder_forward
											return _forward






						def 		lowerCAmelCase (       self     ,    _SCREAMING_SNAKE_CASE     ,    _SCREAMING_SNAKE_CASE     ,    _SCREAMING_SNAKE_CASE)      ->	Callable[[], None]:




											"""simple docstring"""





											lowerCamelCase__					=self.config_dict[model_name]

											lowerCamelCase__					=(
											    hasattr(_SCREAMING_SNAKE_CASE     ,    'architectures')
											    and isinstance(config.architectures     ,    _SCREAMING_SNAKE_CASE)
											    and len(config.architectures) > 0
											)
											if not self.args.only_pretrain_model and has_model_class_in_config:
																try:
																					lowerCamelCase__					=config.architectures[0]
																					lowerCamelCase__					=__import__('transformers'     ,    fromlist=[model_class])
																					lowerCamelCase__					=getattr(_SCREAMING_SNAKE_CASE     ,    _SCREAMING_SNAKE_CASE)
																					lowerCamelCase__					=model_cls(_SCREAMING_SNAKE_CASE)
																except ImportError:
																					raise ImportError(
																					    F"{model_class} does not exist. If you just want to test the pretrained model, you might want to"
																					    ' set `--only_pretrain_model` or `args.only_pretrain_model=True`.')
											else:
																lowerCamelCase__					=MODEL_WITH_LM_HEAD_MAPPING[config.__class__](_SCREAMING_SNAKE_CASE)

											if self.args.torchscript:
																raise NotImplementedError('Training for torchscript is currently not implemented')
											else:
																lowerCamelCase__					=model

											model.train()
											model.to(self.args.device)

											# encoder-decoder has vocab size saved differently
											lowerCamelCase__					=config.vocab_size if hasattr(_SCREAMING_SNAKE_CASE     ,    'vocab_size') else config.encoder.vocab_size
											lowerCamelCase__					=torch.randint(_SCREAMING_SNAKE_CASE     ,    (batch_size, sequence_length)     ,    dtype=torch.long     ,    device=self.args.device)

											if self.args.fpaa:
																logger.info('Running training in Mixed Precision...')
																if not self.args.is_gpu:
																					raise ValueError('Mixed precision is possible only for GPU.')

																# amp seems to have memory leaks so that memory usage
																# is measured using .half() for now https://github.com/NVIDIA/apex/issues/439
																model.half()

											def compute_loss_and_backprob_encoder():
																lowerCamelCase__					=train_model(_SCREAMING_SNAKE_CASE     ,    labels=_SCREAMING_SNAKE_CASE)[0]
																loss.backward()
																return loss

											def compute_loss_and_backprob_encoder_decoder():
																lowerCamelCase__					=train_model(_SCREAMING_SNAKE_CASE     ,    decoder_input_ids=_SCREAMING_SNAKE_CASE     ,    labels=_SCREAMING_SNAKE_CASE)[0]
																loss.backward()
																return loss

											lowerCamelCase__					=(
											    compute_loss_and_backprob_encoder_decoder
											    if config.is_encoder_decoder
											    else compute_loss_and_backprob_encoder
											)
											return _train






						def 		lowerCAmelCase (       self     ,    _SCREAMING_SNAKE_CASE)      ->	float:




											"""simple docstring"""





											try:
																if self.args.is_tpu or self.args.torchscript:
																					# run additional 10 times to stabilize compilation for tpu and torchscript
																					logger.info('Do inference on TPU or torchscript. Running model 5 times to stabilize compilation')
																					timeit.repeat(
																					    _SCREAMING_SNAKE_CASE     ,    repeat=1     ,    number=5     ,    )

																# as written in https://docs.python.org/2/library/timeit.html#timeit.Timer.repeat, min should be taken rather than the average
																lowerCamelCase__					=timeit.repeat(
																    _SCREAMING_SNAKE_CASE     ,    repeat=self.args.repeat     ,    number=10     ,    )

																if self.args.is_tpu and self.args.torch_xla_tpu_print_metrics:
																					import torch_xla.debug.metrics as met

																					self.print_fn(met.metrics_report())

																return min(_SCREAMING_SNAKE_CASE) / 10.0
											except RuntimeError as e:
																self.print_fn(F"Doesn't fit on GPU. {e}")
																return "N/A"






						def 		lowerCAmelCase (       self     ,    _SCREAMING_SNAKE_CASE)      ->	[Memory, MemorySummary]:




											"""simple docstring"""





											try:
																if self.args.trace_memory_line_by_line:
																					lowerCamelCase__					=start_memory_tracing('transformers')

																if self.args.is_tpu:
																					# tpu
																					raise NotImplementedError(
																					    'Memory Benchmarking is currently not implemented for TPU. Please disable memory benchmarking with'
																					    ' `--no-memory` or `args.memory=False`')
																elif self.args.is_gpu:
																					if not is_pyanvml_available():
																										logger.warning(
																										    'py3nvml not installed, we won\'t log GPU memory usage. '
																										    'Install py3nvml (pip install py3nvml) to log information about GPU.')
																										lowerCamelCase__					='N/A'
																					else:
																										logger.info(
																										    'Measuring total GPU usage on GPU device. Make sure to not have additional processes running'
																										    ' on the same GPU.')
																										# init nvml
																										nvml.nvmlInit()
																										func()
																										lowerCamelCase__					=nvml.nvmlDeviceGetHandleByIndex(self.args.device_idx)
																										lowerCamelCase__					=nvml.nvmlDeviceGetMemoryInfo(_SCREAMING_SNAKE_CASE)
																										lowerCamelCase__					=meminfo.used
																										lowerCamelCase__					=Memory(_SCREAMING_SNAKE_CASE)
																										# shutdown nvml
																										nvml.nvmlShutdown()
																else:
																					# cpu
																					lowerCamelCase__					=measure_peak_memory_cpu(_SCREAMING_SNAKE_CASE)
																					lowerCamelCase__					=Memory(_SCREAMING_SNAKE_CASE) if isinstance(_SCREAMING_SNAKE_CASE     ,    _SCREAMING_SNAKE_CASE) else memory_bytes

																if self.args.trace_memory_line_by_line:
																					lowerCamelCase__					=stop_memory_tracing(_SCREAMING_SNAKE_CASE)
																else:
																					lowerCamelCase__					=None

																return memory, summary
											except RuntimeError as e:
																self.print_fn(F"Doesn't fit on GPU. {e}")
																return "N/A", None