




'''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_       				=      logging.get_logger(__name__)

@dataclass
class 						lowerCAmelCase_   :







  '''simple docstring'''







  lowerCAmelCase_				:					str
  lowerCAmelCase_				:					str
  lowerCAmelCase_				:					Optional[str]				     =						None
  lowerCAmelCase_				:					Optional[str]				     =						None


  def 							SCREAMING_SNAKE_CASE__			(						self    :	int ):






   """simple docstring"""






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

@dataclass(frozen=lowerCamelCase_   )
class 						lowerCAmelCase_   :







  '''simple docstring'''







  lowerCAmelCase_				:					List[int]
  lowerCAmelCase_				:					Optional[List[int]]				     =						None
  lowerCAmelCase_				:					Optional[List[int]]				     =						None
  lowerCAmelCase_				:					Optional[Union[int, float]]				     =						None


  def 							SCREAMING_SNAKE_CASE__			(						self    :	Optional[Any] ):






   """simple docstring"""






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

class 						lowerCAmelCase_   :







  '''simple docstring'''







  def 							SCREAMING_SNAKE_CASE__			(						self    :	List[str]						,		_UpperCAmelCase    :	Dict ):






   """simple docstring"""






   raise NotImplementedError()


  def 							SCREAMING_SNAKE_CASE__			(						self    :	List[str]						,		_UpperCAmelCase    :	int ):






   """simple docstring"""






   raise NotImplementedError()


  def 							SCREAMING_SNAKE_CASE__			(						self    :	Tuple						,		_UpperCAmelCase    :	Optional[int] ):






   """simple docstring"""






   raise NotImplementedError()


  def 							SCREAMING_SNAKE_CASE__			(						self    :	int						,		_UpperCAmelCase    :	int ):






   """simple docstring"""






   raise NotImplementedError()


  def 							SCREAMING_SNAKE_CASE__			(						self    :	Optional[int] ):






   """simple docstring"""






   raise NotImplementedError()


  def 							SCREAMING_SNAKE_CASE__			(						self    :	Optional[int]						,		_UpperCAmelCase    :	List[Any] ):






   """simple docstring"""






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



  @classmethod
  def 							SCREAMING_SNAKE_CASE__			(						cls    :	List[Any]						,		_UpperCAmelCase    :	str						,		_UpperCAmelCase    :	str=None ):






   """simple docstring"""






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

class 						lowerCAmelCase_   ( lowerCamelCase_   ):







  '''simple docstring'''







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






   """simple docstring"""






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


  def __len__(						self    :	Optional[Any] ):






   """simple docstring"""






   return len(self.examples )


  def __getitem__(						self    :	List[str]						,		_UpperCAmelCase    :	List[str] ):






   """simple docstring"""






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


  @classmethod
  def 							SCREAMING_SNAKE_CASE__			(						cls    :	int						,		_UpperCAmelCase    :	Union[str, Any]						,		_UpperCAmelCase    :	Any=""						,		_UpperCAmelCase    :	Tuple=0						,		_UpperCAmelCase    :	List[str]=1						,		_UpperCAmelCase    :	Any=None						,		_UpperCAmelCase    :	str=False						,		**_UpperCAmelCase    :	List[str] ):






   """simple docstring"""






   UpperCAmelCase__										=    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 							SCREAMING_SNAKE_CASE__			(						cls    :	Tuple						,		_UpperCAmelCase    :	Union[str, Any]						,		_UpperCAmelCase    :	str=None						,		**_UpperCAmelCase    :	Any ):






   """simple docstring"""






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


  def 							SCREAMING_SNAKE_CASE__			(						self    :	Optional[int]						,		_UpperCAmelCase    :	List[Any]						,		_UpperCAmelCase    :	Dict=""						,		_UpperCAmelCase    :	Union[str, Any]=0						,		_UpperCAmelCase    :	List[Any]=1						,		_UpperCAmelCase    :	int=None						,		_UpperCAmelCase    :	List[str]=False						,		_UpperCAmelCase    :	Any=False						,		_UpperCAmelCase    :	Any=False						,		):






   """simple docstring"""






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






   """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:
    UpperCAmelCase__										=    [None] * len(_UpperCAmelCase )
   if labels is None:
    UpperCAmelCase__										=    [None] * len(_UpperCAmelCase )
   UpperCAmelCase__										=    []
   UpperCAmelCase__										=    set()
   for text_or_text_and_label, label, guid in zip(_UpperCAmelCase						,		_UpperCAmelCase						,		_UpperCAmelCase ):
    if isinstance(_UpperCAmelCase						,		(tuple, list) ) and label is None:
     UpperCAmelCase__							,			UpperCAmelCase__										=    text_or_text_and_label
    else:
     UpperCAmelCase__										=    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:
    UpperCAmelCase__										=    examples
   else:
    self.examples.extend(_UpperCAmelCase )

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

   return self.examples



  def 							SCREAMING_SNAKE_CASE__			(						self    :	List[Any]						,		_UpperCAmelCase    :	str						,		_UpperCAmelCase    :	Any=None						,		_UpperCAmelCase    :	List[str]=False						,		_UpperCAmelCase    :	Tuple=0						,		_UpperCAmelCase    :	List[Any]=True						,		_UpperCAmelCase    :	Dict=None						,		):






   """simple docstring"""






   if max_length is None:
    UpperCAmelCase__										=    tokenizer.max_len

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

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

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

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

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

    # Zero-pad up to the sequence length.
    UpperCAmelCase__										=    batch_length - len(_UpperCAmelCase )
    if pad_on_left:
     UpperCAmelCase__										=    ([pad_token] * padding_length) + input_ids
     UpperCAmelCase__										=    ([0 if mask_padding_with_zero else 1] * padding_length) + attention_mask
    else:
     UpperCAmelCase__										=    input_ids + ([pad_token] * padding_length)
     UpperCAmelCase__										=    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":
     UpperCAmelCase__										=    label_map[example.label]
    elif self.mode == "regression":
     UpperCAmelCase__										=    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)

    UpperCAmelCase__										=    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

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

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






'''simple docstring'''




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    _UpperCamelCase      (				SCREAMING_SNAKE_CASE__			:      Tuple       ,						SCREAMING_SNAKE_CASE__			:      Dict       ):
 '''simple docstring'''





 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 						lowerCAmelCase_   ( torch.optim.Optimizer   ):







  '''simple docstring'''







  def __init__(						self    :	List[str]						,		_UpperCAmelCase    :	Any						,		_UpperCAmelCase    :	Union[str, Any]=True						,		_UpperCAmelCase    :	List[str]=None ):






   """simple docstring"""






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

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


  @property
  def 							SCREAMING_SNAKE_CASE__			(						self    :	Dict ):






   """simple docstring"""






   return self.optimizer.state


  @state.setter
  def 							SCREAMING_SNAKE_CASE__			(						self    :	Optional[int]						,		_UpperCAmelCase    :	List[str] ):






   """simple docstring"""






   UpperCAmelCase__										=    state


  @property
  def 							SCREAMING_SNAKE_CASE__			(						self    :	Any ):






   """simple docstring"""






   return self.optimizer.param_groups


  @param_groups.setter
  def 							SCREAMING_SNAKE_CASE__			(						self    :	Optional[int]						,		_UpperCAmelCase    :	List[Any] ):






   """simple docstring"""






   UpperCAmelCase__										=    param_groups


  @property
  def 							SCREAMING_SNAKE_CASE__			(						self    :	Optional[Any] ):






   """simple docstring"""






   return self.optimizer.defaults


  @defaults.setter
  def 							SCREAMING_SNAKE_CASE__			(						self    :	int						,		_UpperCAmelCase    :	str ):






   """simple docstring"""






   UpperCAmelCase__										=    defaults


  def 							SCREAMING_SNAKE_CASE__			(						self    :	Optional[int]						,		_UpperCAmelCase    :	Union[str, Any] ):






   """simple docstring"""






   self.optimizer.add_param_group(_UpperCAmelCase )


  def 							SCREAMING_SNAKE_CASE__			(						self    :	List[str]						,		_UpperCAmelCase    :	Tuple ):






   """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 							SCREAMING_SNAKE_CASE__			(						self    :	str ):






   """simple docstring"""






   return self.optimizer.state_dict()


  def 							SCREAMING_SNAKE_CASE__			(						self    :	Optional[int]						,		_UpperCAmelCase    :	List[Any]=None ):






   """simple docstring"""






   if self.gradient_state.sync_gradients:
    UpperCAmelCase__										=    """set_to_none""" in inspect.signature(self.optimizer.zero_grad ).parameters
    if accept_arg:
     if set_to_none is None:
      UpperCAmelCase__										=    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 							SCREAMING_SNAKE_CASE__			(						self    :	Any						,		_UpperCAmelCase    :	List[str]=None ):






   """simple docstring"""






   if self.gradient_state.sync_gradients:
    if self.accelerator_state.distributed_type == DistributedType.TPU:
     UpperCAmelCase__										=    {"""closure""": closure} if closure is not None else {}
     xm.optimizer_step(self.optimizer						,		optimizer_args=_UpperCAmelCase )
    elif self.scaler is not None:
     UpperCAmelCase__										=    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
      UpperCAmelCase__										=    self.scaler.get_scale()
      UpperCAmelCase__										=    True
     self.scaler.step(self.optimizer						,		_UpperCAmelCase )
     self.scaler.update()
     UpperCAmelCase__										=    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.
      UpperCAmelCase__										=    scale_after < self._last_scale
     UpperCAmelCase__										=    scale_after

    else:
     self.optimizer.step(_UpperCAmelCase )


  def 							SCREAMING_SNAKE_CASE__			(						self    :	Optional[Any]						,		_UpperCAmelCase    :	str ):






   """simple docstring"""






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


  @property
  def 							SCREAMING_SNAKE_CASE__			(						self    :	str ):






   """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 							SCREAMING_SNAKE_CASE__			(						self    :	Optional[int] ):






   """simple docstring"""






   return self._is_overflow


  def __getstate__(						self    :	Dict ):






   """simple docstring"""






   return self.__dict__.copy()



  def __setstate__(						self    :	Dict						,		_UpperCAmelCase    :	Tuple ):






   """simple docstring"""






   self.__dict__.update(_UpperCAmelCase )
