





"""simple docstring"""
from typing import List, Union

import numpy as np

from ..tokenization_utils import TruncationStrategy
from ..utils import add_end_docstrings, logging
from .base import PIPELINE_INIT_ARGS, ArgumentHandler, ChunkPipeline


lowerCamelCase_       =		logging.get_logger(__name__)






class  _SCREAMING_SNAKE_CASE(       __UpperCamelCase  ):




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





													"""simple docstring"""






													if isinstance(SCREAMING_SNAKE_CASE__		,SCREAMING_SNAKE_CASE__							):
																			__SCREAMING_SNAKE_CASE       :Optional[int]		    =  [label.strip() for label in labels.split(''','''							) if label.strip()]
													return labels




							def __call__(						self		,SCREAMING_SNAKE_CASE__		,SCREAMING_SNAKE_CASE__		,SCREAMING_SNAKE_CASE__							)      ->				Optional[Any]:





													"""simple docstring"""






													if len(SCREAMING_SNAKE_CASE__							) == 0 or len(SCREAMING_SNAKE_CASE__							) == 0:
																			raise ValueError('''You must include at least one label and at least one sequence.'''							)
													if hypothesis_template.format(labels[0]							) == hypothesis_template:
																			raise ValueError(
																			    (
																			        '''The provided hypothesis_template "{}" was not able to be formatted with the target labels. '''
																			        '''Make sure the passed template includes formatting syntax such as {{}} where the label should go.'''
																			    ).format(SCREAMING_SNAKE_CASE__							)							)

													if isinstance(SCREAMING_SNAKE_CASE__		,SCREAMING_SNAKE_CASE__							):
																			__SCREAMING_SNAKE_CASE       :int		    =  [sequences]

													__SCREAMING_SNAKE_CASE       :Optional[int]		    =  []
													for sequence in sequences:
																			sequence_pairs.extend([[sequence, hypothesis_template.format(SCREAMING_SNAKE_CASE__							)] for label in labels]							)

													return sequence_pairs, sequences






@add_end_docstrings(__UpperCamelCase  )
class  _SCREAMING_SNAKE_CASE(       __UpperCamelCase  ):




							def __init__(						self		,SCREAMING_SNAKE_CASE__=ZeroShotClassificationArgumentHandler()		,*SCREAMING_SNAKE_CASE__		,**SCREAMING_SNAKE_CASE__							)      ->				List[Any]:





													"""simple docstring"""






													__SCREAMING_SNAKE_CASE       :Optional[int]		    =  args_parser
													super().__init__(*SCREAMING_SNAKE_CASE__		,**SCREAMING_SNAKE_CASE__							)
													if self.entailment_id == -1:
																			logger.warning(
																			    '''Failed to determine \'entailment\' label id from the label2id mapping in the model config. Setting to '''
																			    '''-1. Define a descriptive label2id mapping in the model config to ensure correct outputs.'''							)




							@property
							def    _UpperCamelCase					(						self							)      ->				Dict:





													"""simple docstring"""






													for label, ind in self.model.config.labelaid.items():
																			if label.lower().startswith('''entail'''							):
																									return ind
													return -1




							def    _UpperCamelCase					(						self		,SCREAMING_SNAKE_CASE__		,SCREAMING_SNAKE_CASE__=True		,SCREAMING_SNAKE_CASE__=True		,SCREAMING_SNAKE_CASE__=TruncationStrategy.ONLY_FIRST		,**SCREAMING_SNAKE_CASE__							)      ->				List[Any]:





													"""simple docstring"""






													__SCREAMING_SNAKE_CASE       :Union[str, Any]		    =  self.framework
													if self.tokenizer.pad_token is None:
																			# Override for tokenizers not supporting padding
																			logger.error(
																			    '''Tokenizer was not supporting padding necessary for zero-shot, attempting to use '''
																			    ''' `pad_token=eos_token`'''							)
																			__SCREAMING_SNAKE_CASE       :Optional[int]		    =  self.tokenizer.eos_token
													try:
																			__SCREAMING_SNAKE_CASE       :Optional[int]		    =  self.tokenizer(
																			    SCREAMING_SNAKE_CASE__		,add_special_tokens=SCREAMING_SNAKE_CASE__		,return_tensors=SCREAMING_SNAKE_CASE__		,padding=SCREAMING_SNAKE_CASE__		,truncation=SCREAMING_SNAKE_CASE__		,)
													except Exception as e:
																			if "too short" in str(SCREAMING_SNAKE_CASE__							):
																									# tokenizers might yell that we want to truncate
																									# to a value that is not even reached by the input.
																									# In that case we don't want to truncate.
																									# It seems there's not a really better way to catch that
																									# exception.

																									__SCREAMING_SNAKE_CASE       :Union[str, Any]		    =  self.tokenizer(
																									    SCREAMING_SNAKE_CASE__		,add_special_tokens=SCREAMING_SNAKE_CASE__		,return_tensors=SCREAMING_SNAKE_CASE__		,padding=SCREAMING_SNAKE_CASE__		,truncation=TruncationStrategy.DO_NOT_TRUNCATE		,)
																			else:
																									raise e

													return inputs




							def    _UpperCamelCase					(						self		,**SCREAMING_SNAKE_CASE__							)      ->				Dict:





													"""simple docstring"""






													if kwargs.get('''multi_class'''		,SCREAMING_SNAKE_CASE__							) is not None:
																			__SCREAMING_SNAKE_CASE       :Optional[Any]		    =  kwargs['multi_class']
																			logger.warning(
																			    '''The `multi_class` argument has been deprecated and renamed to `multi_label`. '''
																			    '''`multi_class` will be removed in a future version of Transformers.'''							)
													__SCREAMING_SNAKE_CASE       :Dict		    =  {}
													if "candidate_labels" in kwargs:
																			__SCREAMING_SNAKE_CASE       :Optional[int]		    =  self._args_parser._parse_labels(kwargs['''candidate_labels''']							)
													if "hypothesis_template" in kwargs:
																			__SCREAMING_SNAKE_CASE       :List[str]		    =  kwargs['hypothesis_template']

													__SCREAMING_SNAKE_CASE       :List[Any]		    =  {}
													if "multi_label" in kwargs:
																			__SCREAMING_SNAKE_CASE       :Union[str, Any]		    =  kwargs['multi_label']
													return preprocess_params, {}, postprocess_params




							def __call__(						self		,SCREAMING_SNAKE_CASE__		,*SCREAMING_SNAKE_CASE__		,**SCREAMING_SNAKE_CASE__		,)      ->				List[str]:





													"""simple docstring"""






													if len(SCREAMING_SNAKE_CASE__							) == 0:
																			pass
													elif len(SCREAMING_SNAKE_CASE__							) == 1 and "candidate_labels" not in kwargs:
																			__SCREAMING_SNAKE_CASE       :int		    =  args[0]
													else:
																			raise ValueError(f'''Unable to understand extra arguments {args}'''							)

													return super().__call__(SCREAMING_SNAKE_CASE__		,**SCREAMING_SNAKE_CASE__							)




							def    _UpperCamelCase					(						self		,SCREAMING_SNAKE_CASE__		,SCREAMING_SNAKE_CASE__=None		,SCREAMING_SNAKE_CASE__="This example is {}."							)      ->				Optional[int]:





													"""simple docstring"""






													__SCREAMING_SNAKE_CASE       :Any		    =  self._args_parser(SCREAMING_SNAKE_CASE__		,SCREAMING_SNAKE_CASE__		,SCREAMING_SNAKE_CASE__							)

													for i, (candidate_label, sequence_pair) in enumerate(zip(SCREAMING_SNAKE_CASE__		,SCREAMING_SNAKE_CASE__							)							):
																			__SCREAMING_SNAKE_CASE       :Dict		    =  self._parse_and_tokenize([sequence_pair]							)

																			yield {
																			    "candidate_label": candidate_label,
																			    "sequence": sequences[0],
																			    "is_last": i == len(SCREAMING_SNAKE_CASE__							) - 1,
																			    **model_input,
																			}




							def    _UpperCamelCase					(						self		,SCREAMING_SNAKE_CASE__							)      ->				List[str]:





													"""simple docstring"""






													__SCREAMING_SNAKE_CASE       :List[Any]		    =  inputs['candidate_label']
													__SCREAMING_SNAKE_CASE       :Dict		    =  inputs['sequence']
													__SCREAMING_SNAKE_CASE       :int		    =  {k: inputs[k] for k in self.tokenizer.model_input_names}
													__SCREAMING_SNAKE_CASE       :Optional[Any]		    =  self.model(**SCREAMING_SNAKE_CASE__							)

													__SCREAMING_SNAKE_CASE       :Any		    =  {
													    'candidate_label': candidate_label,
													    'sequence': sequence,
													    'is_last': inputs['is_last'],
													    **outputs,
													}
													return model_outputs




							def    _UpperCamelCase					(						self		,SCREAMING_SNAKE_CASE__		,SCREAMING_SNAKE_CASE__=False							)      ->				List[Any]:





													"""simple docstring"""






													__SCREAMING_SNAKE_CASE       :Any		    =  [outputs['candidate_label'] for outputs in model_outputs]
													__SCREAMING_SNAKE_CASE       :List[str]		    =  [outputs['sequence'] for outputs in model_outputs]
													__SCREAMING_SNAKE_CASE       :Tuple		    =  np.concatenate([output['''logits'''].numpy() for output in model_outputs]							)
													__SCREAMING_SNAKE_CASE       :Dict		    =  logits.shape[0]
													__SCREAMING_SNAKE_CASE       :Dict		    =  len(SCREAMING_SNAKE_CASE__							)
													__SCREAMING_SNAKE_CASE       :Union[str, Any]		    =  N // n
													__SCREAMING_SNAKE_CASE       :Any		    =  logits.reshape((num_sequences, n, -1)							)

													if multi_label or len(SCREAMING_SNAKE_CASE__							) == 1:
																			# softmax over the entailment vs. contradiction dim for each label independently
																			__SCREAMING_SNAKE_CASE       :str		    =  self.entailment_id
																			__SCREAMING_SNAKE_CASE       :List[str]		    =  -1 if entailment_id == 0 else 0
																			__SCREAMING_SNAKE_CASE       :List[str]		    =  reshaped_outputs[..., [contradiction_id, entailment_id]]
																			__SCREAMING_SNAKE_CASE       :Optional[Any]		    =  np.exp(SCREAMING_SNAKE_CASE__							) / np.exp(SCREAMING_SNAKE_CASE__							).sum(-1		,keepdims=SCREAMING_SNAKE_CASE__							)
																			__SCREAMING_SNAKE_CASE       :Optional[Any]		    =  scores[..., 1]
													else:
																			# softmax the "entailment" logits over all candidate labels
																			__SCREAMING_SNAKE_CASE       :int		    =  reshaped_outputs[..., self.entailment_id]
																			__SCREAMING_SNAKE_CASE       :Tuple		    =  np.exp(SCREAMING_SNAKE_CASE__							) / np.exp(SCREAMING_SNAKE_CASE__							).sum(-1		,keepdims=SCREAMING_SNAKE_CASE__							)

													__SCREAMING_SNAKE_CASE       :List[str]		    =  list(reversed(scores[0].argsort()							)							)
													return {
													    "sequence": sequences[0],
													    "labels": [candidate_labels[i] for i in top_inds],
													    "scores": scores[0, top_inds].tolist(),
													}






"""simple docstring"""
import baseaa
import io
import json
import os
from copy import deepcopy

from ..optimizer import AcceleratedOptimizer
from ..scheduler import AcceleratedScheduler






class  _SCREAMING_SNAKE_CASE:




       def __init__(						self		,SCREAMING_SNAKE_CASE__							)      ->				int:





             """simple docstring"""






             if isinstance(SCREAMING_SNAKE_CASE__		,SCREAMING_SNAKE_CASE__							):
                   # Don't modify user's data should they want to reuse it (e.g. in tests), because once we
                   # modified it, it will not be accepted here again, since `auto` values would have been overridden
                   __SCREAMING_SNAKE_CASE       :Optional[int]		    =  deepcopy(SCREAMING_SNAKE_CASE__							)
             elif os.path.exists(SCREAMING_SNAKE_CASE__							):
                   with io.open(SCREAMING_SNAKE_CASE__		,'''r'''		,encoding='''utf-8'''							) as f:
                         __SCREAMING_SNAKE_CASE       :Dict		    =  json.load(SCREAMING_SNAKE_CASE__							)
             else:
                   try:
                         __SCREAMING_SNAKE_CASE       :str		    =  baseaa.urlsafe_baadecode(SCREAMING_SNAKE_CASE__							).decode('''utf-8'''							)
                         __SCREAMING_SNAKE_CASE       :Union[str, Any]		    =  json.loads(SCREAMING_SNAKE_CASE__							)
                   except (UnicodeDecodeError, AttributeError, ValueError):
                         raise ValueError(
                             f'''Expected a string path to an existing deepspeed config, or a dictionary, or a base64 encoded string. Received: {config_file_or_dict}'''							)

             __SCREAMING_SNAKE_CASE       :Optional[int]		    =  config

             self.set_stage_and_offload()




       def    _UpperCamelCase					(						self							)      ->				Any:





             """simple docstring"""






             __SCREAMING_SNAKE_CASE       :Union[str, Any]		    =  self.get_value('''zero_optimization.stage'''		,-1							)

             # offload
             __SCREAMING_SNAKE_CASE       :Any		    =  False
             if self.is_zeroa() or self.is_zeroa():
                   __SCREAMING_SNAKE_CASE       :Optional[Any]		    =  set(['''cpu''', '''nvme''']							)
                   __SCREAMING_SNAKE_CASE       :Optional[Any]		    =  set(
                       [
                           self.get_value('''zero_optimization.offload_optimizer.device'''							),
                           self.get_value('''zero_optimization.offload_param.device'''							),
                       ]							)
                   if len(offload_devices & offload_devices_valid							) > 0:
                         __SCREAMING_SNAKE_CASE       :int		    =  True




       def    _UpperCamelCase					(						self		,SCREAMING_SNAKE_CASE__							)      ->				Tuple:





             """simple docstring"""






             __SCREAMING_SNAKE_CASE       :Any		    =  self.config

             # find the config node of interest if it exists
             __SCREAMING_SNAKE_CASE       :Union[str, Any]		    =  ds_key_long.split('''.'''							)
             __SCREAMING_SNAKE_CASE       :List[str]		    =  nodes.pop()
             for node in nodes:
                   __SCREAMING_SNAKE_CASE       :Dict		    =  config.get(SCREAMING_SNAKE_CASE__							)
                   if config is None:
                         return None, ds_key

             return config, ds_key




       def    _UpperCamelCase					(						self		,SCREAMING_SNAKE_CASE__		,SCREAMING_SNAKE_CASE__=None							)      ->				Tuple:





             """simple docstring"""






             __SCREAMING_SNAKE_CASE    ,__SCREAMING_SNAKE_CASE       :Optional[Any]		    =  self.find_config_node(SCREAMING_SNAKE_CASE__							)
             if config is None:
                   return default
             return config.get(SCREAMING_SNAKE_CASE__		,SCREAMING_SNAKE_CASE__							)




       def    _UpperCamelCase					(						self		,SCREAMING_SNAKE_CASE__		,SCREAMING_SNAKE_CASE__=False							)      ->				int:





             """simple docstring"""






             __SCREAMING_SNAKE_CASE       :Dict		    =  self.config

             # find the config node of interest if it exists
             __SCREAMING_SNAKE_CASE       :str		    =  ds_key_long.split('''.'''							)
             for node in nodes:
                   __SCREAMING_SNAKE_CASE       :Any		    =  config
                   __SCREAMING_SNAKE_CASE       :int		    =  config.get(SCREAMING_SNAKE_CASE__							)
                   if config is None:
                         if must_exist:
                               raise ValueError(f'''Can\'t find {ds_key_long} entry in the config: {self.config}'''							)
                         else:
                               return

        # if found remove it
             if parent_config is not None:
                   parent_config.pop(SCREAMING_SNAKE_CASE__							)




       def    _UpperCamelCase					(						self		,SCREAMING_SNAKE_CASE__							)      ->				Optional[int]:





             """simple docstring"""






             __SCREAMING_SNAKE_CASE       :int		    =  self.get_value(SCREAMING_SNAKE_CASE__							)
             return False if value is None else bool(SCREAMING_SNAKE_CASE__							)




       def    _UpperCamelCase					(						self		,SCREAMING_SNAKE_CASE__							)      ->				Optional[Any]:





             """simple docstring"""






             __SCREAMING_SNAKE_CASE       :str		    =  self.get_value(SCREAMING_SNAKE_CASE__							)
             return False if value is None else not bool(SCREAMING_SNAKE_CASE__							)




       def    _UpperCamelCase					(						self							)      ->				List[str]:





             """simple docstring"""






             return self._stage == 2




       def    _UpperCamelCase					(						self							)      ->				List[Any]:





             """simple docstring"""






             return self._stage == 3




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





             """simple docstring"""






             return self._offload






class  _SCREAMING_SNAKE_CASE:




       def __init__(						self		,SCREAMING_SNAKE_CASE__							)      ->				Optional[Any]:





             """simple docstring"""






             __SCREAMING_SNAKE_CASE       :Optional[int]		    =  engine




       def    _UpperCamelCase					(						self		,SCREAMING_SNAKE_CASE__		,**SCREAMING_SNAKE_CASE__							)      ->				List[Any]:





             """simple docstring"""






             self.engine.backward(SCREAMING_SNAKE_CASE__		,**SCREAMING_SNAKE_CASE__							)

             # Deepspeed's `engine.step` performs the following operations:
             # - gradient accumulation check
             # - gradient clipping
             # - optimizer step
             # - zero grad
             # - checking overflow
             # - lr_scheduler step (only if engine.lr_scheduler is not None)
             self.engine.step()
             # and this plugin overrides the above calls with no-ops when Accelerate runs under
             # Deepspeed, but allows normal functionality for non-Deepspeed cases thus enabling a simple
             # training loop that works transparently under many training regimes.






class  _SCREAMING_SNAKE_CASE(       A  ):




       def __init__(						self		,SCREAMING_SNAKE_CASE__							)      ->				str:





             """simple docstring"""






             super().__init__(SCREAMING_SNAKE_CASE__		,device_placement=SCREAMING_SNAKE_CASE__		,scaler=SCREAMING_SNAKE_CASE__							)
             __SCREAMING_SNAKE_CASE       :int		    =  hasattr(self.optimizer		,'''overflow'''							)




       def    _UpperCamelCase					(						self		,SCREAMING_SNAKE_CASE__=None							)      ->				Union[str, Any]:





             """simple docstring"""






             pass  # `accelerator.backward(loss)` is doing that automatically. Therefore, its implementation is not needed




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





             """simple docstring"""






             pass  # `accelerator.backward(loss)` is doing that automatically. Therefore, its implementation is not needed




       @property
       def    _UpperCamelCase					(						self							)      ->				Dict:





             """simple docstring"""






             if self.__has_overflow__:
                   return self.optimizer.overflow
             return False






class  _SCREAMING_SNAKE_CASE(       A  ):




       def __init__(						self		,SCREAMING_SNAKE_CASE__		,SCREAMING_SNAKE_CASE__							)      ->				Optional[int]:





             """simple docstring"""






             super().__init__(SCREAMING_SNAKE_CASE__		,SCREAMING_SNAKE_CASE__							)




       def    _UpperCamelCase					(						self							)      ->				Optional[int]:





             """simple docstring"""






             pass  # `accelerator.backward(loss)` is doing that automatically. Therefore, its implementation is not needed






class  _SCREAMING_SNAKE_CASE:




       def __init__(						self		,SCREAMING_SNAKE_CASE__		,SCREAMING_SNAKE_CASE__=0.0_0_1		,SCREAMING_SNAKE_CASE__=0		,**SCREAMING_SNAKE_CASE__							)      ->				List[str]:





             """simple docstring"""






             __SCREAMING_SNAKE_CASE       :Optional[Any]		    =  params
             __SCREAMING_SNAKE_CASE       :str		    =  lr
             __SCREAMING_SNAKE_CASE       :str		    =  weight_decay
             __SCREAMING_SNAKE_CASE       :int		    =  kwargs






class  _SCREAMING_SNAKE_CASE:




       def __init__(						self		,SCREAMING_SNAKE_CASE__		,SCREAMING_SNAKE_CASE__=None		,SCREAMING_SNAKE_CASE__=0		,**SCREAMING_SNAKE_CASE__							)      ->				str:





             """simple docstring"""






             __SCREAMING_SNAKE_CASE       :Dict		    =  optimizer
             __SCREAMING_SNAKE_CASE       :Union[str, Any]		    =  total_num_steps
             __SCREAMING_SNAKE_CASE       :Any		    =  warmup_num_steps
             __SCREAMING_SNAKE_CASE       :int		    =  kwargs