
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


SCREAMING_SNAKE_CASE__							=    logging.get_logger(__name__)
@dataclass
class      A__       :
							lowerCAmelCase__		:						str
							lowerCAmelCase__		:						str
							lowerCAmelCase__		:						Optional[str]         =							None
							lowerCAmelCase__		:						Optional[str]         =							None





							def       a__			(			self  :       Tuple    )						->		Optional[int]:



											"""simple docstring"""


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






@dataclass(frozen=lowerCAmelCase__    )
class      A__       :
							lowerCAmelCase__		:						List[int]
							lowerCAmelCase__		:						Optional[List[int]]         =							None
							lowerCAmelCase__		:						Optional[List[int]]         =							None
							lowerCAmelCase__		:						Optional[Union[int, float]]         =							None





							def       a__			(			self  :       List[str]    )						->		Any:



											"""simple docstring"""


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






class      A__       :





							def       a__			(			self  :       Tuple					,			_UpperCAmelCase  :       Union[str, Any]    )						->		Dict:



											"""simple docstring"""


											raise NotImplementedError()





							def       a__			(			self  :       int					,			_UpperCAmelCase  :       int    )						->		List[str]:



											"""simple docstring"""


											raise NotImplementedError()





							def       a__			(			self  :       Optional[int]					,			_UpperCAmelCase  :       str    )						->		Dict:



											"""simple docstring"""


											raise NotImplementedError()





							def       a__			(			self  :       Optional[Any]					,			_UpperCAmelCase  :       Optional[int]    )						->		Dict:



											"""simple docstring"""


											raise NotImplementedError()





							def       a__			(			self  :       str    )						->		str:



											"""simple docstring"""


											raise NotImplementedError()





							def       a__			(			self  :       Union[str, Any]					,			_UpperCAmelCase  :       List[Any]    )						->		Any:



											"""simple docstring"""


											if len(self.get_labels()    ) > 1:
															__lowercase								= self.get_labels()[int(example.label    )]
											return example





							@classmethod
							def       a__			(			cls  :       Optional[Any]					,			_UpperCAmelCase  :       Tuple					,			_UpperCAmelCase  :       str=None    )						->		str:



											"""simple docstring"""


											with open(_UpperCAmelCase					,			'r'					,			encoding='utf-8-sig'    ) as f:
															return list(csv.reader(_UpperCAmelCase					,			delimiter='\t'					,			quotechar=_UpperCAmelCase    )    )






class      A__       (						lowerCAmelCase__    ):





							def __init__(			self  :       Optional[int]					,			_UpperCAmelCase  :       Optional[Any]=None					,			_UpperCAmelCase  :       Optional[int]=None					,			_UpperCAmelCase  :       Tuple="classification"					,			_UpperCAmelCase  :       Optional[int]=False    )						->		int:



											"""simple docstring"""


											__lowercase								= [] if labels is None else labels
											__lowercase								= [] if examples is None else examples
											__lowercase								= mode
											__lowercase								= verbose





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



											"""simple docstring"""


											return len(self.examples    )





							def __getitem__(			self  :       int					,			_UpperCAmelCase  :       Dict    )						->		Optional[int]:



											"""simple docstring"""


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





							@classmethod
							def       a__			(			cls  :       Tuple					,			_UpperCAmelCase  :       List[Any]					,			_UpperCAmelCase  :       str=""					,			_UpperCAmelCase  :       Optional[Any]=0					,			_UpperCAmelCase  :       Optional[Any]=1					,			_UpperCAmelCase  :       Optional[int]=None					,			_UpperCAmelCase  :       Tuple=False					,			**_UpperCAmelCase  :       List[Any]    )						->		Optional[int]:



											"""simple docstring"""


											__lowercase								= 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       a__			(			cls  :       List[str]					,			_UpperCAmelCase  :       int					,			_UpperCAmelCase  :       str=None					,			**_UpperCAmelCase  :       Dict    )						->		str:



											"""simple docstring"""


											__lowercase								= cls(**_UpperCAmelCase    )
											processor.add_examples(_UpperCAmelCase					,			labels=_UpperCAmelCase    )
											return processor





							def       a__			(			self  :       str					,			_UpperCAmelCase  :       Any					,			_UpperCAmelCase  :       Any=""					,			_UpperCAmelCase  :       Tuple=0					,			_UpperCAmelCase  :       List[str]=1					,			_UpperCAmelCase  :       Dict=None					,			_UpperCAmelCase  :       List[Any]=False					,			_UpperCAmelCase  :       Dict=False					,			_UpperCAmelCase  :       Optional[Any]=False					,			)						->		Optional[Any]:



											"""simple docstring"""


											__lowercase								= self._read_tsv(_UpperCAmelCase    )
											if skip_first_row:
															__lowercase								= lines[1:]
											__lowercase								= []
											__lowercase								= []
											__lowercase								= []
											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								= 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       a__			(			self  :       int					,			_UpperCAmelCase  :       Optional[Any]					,			_UpperCAmelCase  :       int=None					,			_UpperCAmelCase  :       str=None					,			_UpperCAmelCase  :       Optional[Any]=False					,			_UpperCAmelCase  :       Tuple=False    )						->		int:



											"""simple docstring"""


											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								= [None] * len(_UpperCAmelCase    )
											if labels is None:
															__lowercase								= [None] * len(_UpperCAmelCase    )
											__lowercase								= []
											__lowercase								= 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								= text_or_text_and_label
															else:
																			__lowercase								= 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								= examples
											else:
															self.examples.extend(_UpperCAmelCase    )

											# Update labels
											if overwrite_labels:
															__lowercase								= list(_UpperCAmelCase    )
											else:
															__lowercase								= list(set(self.labels    ).union(_UpperCAmelCase    )    )

											return self.examples





							def       a__			(			self  :       Dict					,			_UpperCAmelCase  :       int					,			_UpperCAmelCase  :       Union[str, Any]=None					,			_UpperCAmelCase  :       Optional[int]=False					,			_UpperCAmelCase  :       Tuple=0					,			_UpperCAmelCase  :       Optional[int]=True					,			_UpperCAmelCase  :       List[str]=None					,			)						->		List[Any]:



											"""simple docstring"""


											if max_length is None:
															__lowercase								= tokenizer.max_len

											__lowercase								= {label: i for i, label in enumerate(self.labels    )}

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

															__lowercase								= tokenizer.encode(
															    example.text_a					,			add_special_tokens=_UpperCAmelCase					,			max_length=min(_UpperCAmelCase					,			tokenizer.max_len    )					,			)
															all_input_ids.append(_UpperCAmelCase    )

											__lowercase								= max(len(_UpperCAmelCase    ) for input_ids in all_input_ids    )

											__lowercase								= []
											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								= [1 if mask_padding_with_zero else 0] * len(_UpperCAmelCase    )

															# Zero-pad up to the sequence length.
															__lowercase								= batch_length - len(_UpperCAmelCase    )
															if pad_on_left:
																			__lowercase								= ([pad_token] * padding_length) + input_ids
																			__lowercase								= ([0 if mask_padding_with_zero else 1] * padding_length) + attention_mask
															else:
																			__lowercase								= input_ids + ([pad_token] * padding_length)
																			__lowercase								= 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								= label_map[example.label]
															elif self.mode == "regression":
																			__lowercase								= 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								= 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								= torch.tensor([f.input_ids for f in features]					,			dtype=torch.long    )
															__lowercase								= torch.tensor([f.attention_mask for f in features]					,			dtype=torch.long    )
															if self.mode == "classification":
																			__lowercase								= torch.tensor([f.label for f in features]					,			dtype=torch.long    )
															elif self.mode == "regression":
																			__lowercase								= torch.tensor([f.label for f in features]					,			dtype=torch.float    )

															__lowercase								= TensorDataset(_UpperCAmelCase					,			_UpperCAmelCase					,			_UpperCAmelCase    )
															return dataset
											else:
															raise ValueError('return_tensors should be one of \'tf\' or \'pt\''    )







import inspect
import warnings

import torch

from .state import AcceleratorState, GradientState
from .utils import DistributedType, honor_type, is_tpu_available


if is_tpu_available(check_device=False):
							import torch_xla.core.xla_model as xm




def 							__SCREAMING_SNAKE_CASE       (		SCREAMING_SNAKE_CASE	:				int   ,							SCREAMING_SNAKE_CASE	:				int      )					->						Optional[Any]:
				if isinstance(SCREAMING_SNAKE_CASE   ,							(list, tuple)      ):
								return honor_type(SCREAMING_SNAKE_CASE   ,							(move_to_device(SCREAMING_SNAKE_CASE   ,							SCREAMING_SNAKE_CASE      ) for t in state)      )
				elif isinstance(SCREAMING_SNAKE_CASE   ,							SCREAMING_SNAKE_CASE      ):
								return type(SCREAMING_SNAKE_CASE      )({k: move_to_device(SCREAMING_SNAKE_CASE   ,							SCREAMING_SNAKE_CASE      ) for k, v in state.items()}      )
				elif isinstance(SCREAMING_SNAKE_CASE   ,							torch.Tensor      ):
								return state.to(SCREAMING_SNAKE_CASE      )
				return state
class      A__       (						torch.optim.Optimizer    ):





							def __init__(			self  :       Optional[Any]					,			_UpperCAmelCase  :       Optional[int]					,			_UpperCAmelCase  :       Optional[int]=True					,			_UpperCAmelCase  :       int=None    )						->		Optional[Any]:



											"""simple docstring"""


											__lowercase								= optimizer
											__lowercase								= scaler
											__lowercase								= AcceleratorState()
											__lowercase								= GradientState()
											__lowercase								= device_placement
											__lowercase								= False
											__lowercase								= None

											# Handle device placement
											if device_placement:
															__lowercase								= self.optimizer.state_dict()
															if self.accelerator_state.distributed_type == DistributedType.TPU:
																			xm.send_cpu_data_to_device(_UpperCAmelCase					,			self.accelerator_state.device    )
															else:
																			__lowercase								= move_to_device(_UpperCAmelCase					,			self.accelerator_state.device    )
															self.optimizer.load_state_dict(_UpperCAmelCase    )





							@property
							def       a__			(			self  :       Union[str, Any]    )						->		Union[str, Any]:



											"""simple docstring"""


											return self.optimizer.state





							@state.setter
							def       a__			(			self  :       Union[str, Any]					,			_UpperCAmelCase  :       int    )						->		Optional[Any]:



											"""simple docstring"""


											__lowercase								= state





							@property
							def       a__			(			self  :       str    )						->		int:



											"""simple docstring"""


											return self.optimizer.param_groups





							@param_groups.setter
							def       a__			(			self  :       Tuple					,			_UpperCAmelCase  :       List[Any]    )						->		int:



											"""simple docstring"""


											__lowercase								= param_groups





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



											"""simple docstring"""


											return self.optimizer.defaults





							@defaults.setter
							def       a__			(			self  :       List[str]					,			_UpperCAmelCase  :       int    )						->		Any:



											"""simple docstring"""


											__lowercase								= defaults





							def       a__			(			self  :       List[str]					,			_UpperCAmelCase  :       List[Any]    )						->		Any:



											"""simple docstring"""


											self.optimizer.add_param_group(_UpperCAmelCase    )





							def       a__			(			self  :       int					,			_UpperCAmelCase  :       Dict    )						->		Optional[int]:



											"""simple docstring"""


											if self.accelerator_state.distributed_type == DistributedType.TPU and self.device_placement:
															xm.send_cpu_data_to_device(_UpperCAmelCase					,			self.accelerator_state.device    )
											self.optimizer.load_state_dict(_UpperCAmelCase    )





							def       a__			(			self  :       str    )						->		Any:



											"""simple docstring"""


											return self.optimizer.state_dict()





							def       a__			(			self  :       Dict					,			_UpperCAmelCase  :       List[str]=None    )						->		Tuple:



											"""simple docstring"""


											if self.gradient_state.sync_gradients:
															__lowercase								= 'set_to_none' in inspect.signature(self.optimizer.zero_grad    ).parameters
															if accept_arg:
																			if set_to_none is None:
																							__lowercase								= False
																			self.optimizer.zero_grad(set_to_none=_UpperCAmelCase    )
															else:
																			if set_to_none is not None:
																							raise ValueError('`set_to_none` for Optimizer.zero_grad` is not supported by this optimizer.'    )
																			self.optimizer.zero_grad()





							def       a__			(			self  :       List[str]					,			_UpperCAmelCase  :       Optional[Any]=None    )						->		int:



											"""simple docstring"""


											if self.gradient_state.sync_gradients:
															if self.accelerator_state.distributed_type == DistributedType.TPU:
																			__lowercase								= {'closure': closure} if closure is not None else {}
																			xm.optimizer_step(self.optimizer					,			optimizer_args=_UpperCAmelCase    )
															elif self.scaler is not None:
																			__lowercase								= False
																			if self._last_scale is None:
																							# `get_scale` is an async operation requiring full synchronization
																							# on CPU and GPUs before finishing. As a result, we store away
																							# the prior one to reduce the call overhead
																							__lowercase								= self.scaler.get_scale()
																							__lowercase								= True
																			self.scaler.step(self.optimizer					,			_UpperCAmelCase    )
																			self.scaler.update()
																			__lowercase								= self.scaler.get_scale()
																			if not new_scale:
																							# If we reduced the loss scale, it means the optimizer step was skipped because of gradient overflow.
																							__lowercase								= scale_after < self._last_scale
																			__lowercase								= scale_after

															else:
																			self.optimizer.step(_UpperCAmelCase    )





							def       a__			(			self  :       Optional[int]					,			_UpperCAmelCase  :       int    )						->		Tuple:



											"""simple docstring"""


											for param_group in self.optimizer.param_groups:
															__lowercase								= [parameters_map.get(_UpperCAmelCase					,			_UpperCAmelCase    ) for p in param_group['params']]





							@property
							def       a__			(			self  :       Optional[Any]    )						->		Union[str, Any]:



											"""simple docstring"""


											warnings.warn(
											    'The `is_overflow` property is deprecated and will be removed in version 1.0 of Accelerate use '
											    '`optimizer.step_was_skipped` instead.'					,			_UpperCAmelCase					,			)
											return self._is_overflow





							@property
							def       a__			(			self  :       Tuple    )						->		Optional[int]:



											"""simple docstring"""


											return self._is_overflow





							def __getstate__(			self  :       Optional[int]    )						->		str:



											"""simple docstring"""


											return self.__dict__.copy()





							def __setstate__(			self  :       List[Any]					,			_UpperCAmelCase  :       Optional[int]    )						->		Any:



											"""simple docstring"""


											self.__dict__.update(_UpperCAmelCase    )





