

'''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


lowercase__         =      logging.get_logger(__name__)




class 	A_			(    _snake_case							):






     '''simple docstring'''



     def        UpperCAmelCase_	(		self			:					Optional[int]					,      lowercase_			:					List[Any]	)						->     Union[str, Any]:
      if isinstance(lowercase_					,      lowercase_	):
       UpperCAmelCase	:     Optional[Any]						 =			[label.strip() for label in labels.split(','	) if label.strip()]
      return labels




     def __call__(		self			:					Union[str, Any]					,      lowercase_			:					Optional[Any]					,      lowercase_			:					Tuple					,      lowercase_			:					str	)						->     Dict:
      if len(lowercase_	) == 0 or len(lowercase_	) == 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(lowercase_	)	)

      if isinstance(lowercase_					,      lowercase_	):
       UpperCAmelCase	:     Union[str, Any]						 =			[sequences]

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

      return sequence_pairs, sequences




@add_end_docstrings(_snake_case							)
class 	A_			(    _snake_case							):






     '''simple docstring'''



     def __init__(		self			:					Tuple					,      lowercase_			:					List[str]=ZeroShotClassificationArgumentHandler()					,      *lowercase_			:					Any					,      **lowercase_			:					List[str]	)						->     Optional[int]:
      UpperCAmelCase	:     Dict						 =			args_parser
      super().__init__(*lowercase_					,      **lowercase_	)
      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			:					Optional[int]	)						->     Any:
      for label, ind in self.model.config.labelaid.items():
       if label.lower().startswith('entail'	):
        return ind
      return -1


     def        UpperCAmelCase_	(		self			:					int					,      lowercase_			:					Optional[Any]					,      lowercase_			:					Dict=True					,      lowercase_			:					Any=True					,      lowercase_			:					Any=TruncationStrategy.ONLY_FIRST					,      **lowercase_			:					List[Any]	)						->     int:
      UpperCAmelCase	:     Optional[int]						 =			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`'	)
       UpperCAmelCase	:     List[Any]						 =			self.tokenizer.eos_token
      try:
       UpperCAmelCase	:     List[str]						 =			self.tokenizer(
           lowercase_					,      add_special_tokens=lowercase_					,      return_tensors=lowercase_					,      padding=lowercase_					,      truncation=lowercase_					,      )
      except Exception as e:
       if "too short" in str(lowercase_	):
        # 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.

        UpperCAmelCase	:     List[Any]						 =			self.tokenizer(
            lowercase_					,      add_special_tokens=lowercase_					,      return_tensors=lowercase_					,      padding=lowercase_					,      truncation=TruncationStrategy.DO_NOT_TRUNCATE					,      )
       else:
        raise e

      return inputs


     def        UpperCAmelCase_	(		self			:					Optional[Any]					,      **lowercase_			:					int	)						->     List[Any]:
      if kwargs.get('multi_class'					,      lowercase_	) is not None:
       UpperCAmelCase	:     List[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.'	)
      UpperCAmelCase	:     Optional[int]						 =			{}
      if "candidate_labels" in kwargs:
       UpperCAmelCase	:     Optional[Any]						 =			self._args_parser._parse_labels(kwargs['candidate_labels']	)
      if "hypothesis_template" in kwargs:
       UpperCAmelCase	:     Optional[Any]						 =			kwargs['hypothesis_template']

      UpperCAmelCase	:     Optional[int]						 =			{}
      if "multi_label" in kwargs:
       UpperCAmelCase	:     Any						 =			kwargs['multi_label']
      return preprocess_params, {}, postprocess_params


     def __call__(		self			:					Any					,      lowercase_			:					Union[str, List[str]]					,      *lowercase_			:					int					,      **lowercase_			:					Optional[Any]					,      )						->     Tuple:
      if len(lowercase_	) == 0:
       pass
      elif len(lowercase_	) == 1 and "candidate_labels" not in kwargs:
       UpperCAmelCase	:     Tuple						 =			args[0]
      else:
       raise ValueError(f"""Unable to understand extra arguments {args}"""	)

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


     def        UpperCAmelCase_	(		self			:					List[str]					,      lowercase_			:					Optional[int]					,      lowercase_			:					Any=None					,      lowercase_			:					Optional[Any]="This example is {}."	)						->     Optional[int]:
      UpperCAmelCase	:     Dict						 =			self._args_parser(lowercase_					,      lowercase_					,      lowercase_	)

      for i, (candidate_label, sequence_pair) in enumerate(zip(lowercase_					,      lowercase_	)	):
       UpperCAmelCase	:     Optional[Any]						 =			self._parse_and_tokenize([sequence_pair]	)

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


     def        UpperCAmelCase_	(		self			:					Any					,      lowercase_			:					Union[str, Any]	)						->     Union[str, Any]:
      UpperCAmelCase	:     Union[str, Any]						 =			inputs['candidate_label']
      UpperCAmelCase	:     str						 =			inputs['sequence']
      UpperCAmelCase	:     Union[str, Any]						 =			{k: inputs[k] for k in self.tokenizer.model_input_names}
      UpperCAmelCase	:     Optional[int]						 =			self.model(**lowercase_	)

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




     def        UpperCAmelCase_	(		self			:					Optional[int]					,      lowercase_			:					Dict					,      lowercase_			:					int=False	)						->     int:
      UpperCAmelCase	:     Union[str, Any]						 =			[outputs['candidate_label'] for outputs in model_outputs]
      UpperCAmelCase	:     Dict						 =			[outputs['sequence'] for outputs in model_outputs]
      UpperCAmelCase	:     str						 =			np.concatenate([output['logits'].numpy() for output in model_outputs]	)
      UpperCAmelCase	:     List[Any]						 =			logits.shape[0]
      UpperCAmelCase	:     Any						 =			len(lowercase_	)
      UpperCAmelCase	:     Any						 =			N // n
      UpperCAmelCase	:     Dict						 =			logits.reshape((num_sequences, n, -1)	)

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

      UpperCAmelCase	:     Tuple						 =			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 inspect
import os
import unittest

import torch

import accelerate
from accelerate import Accelerator
from accelerate.test_utils import execute_subprocess_async, require_multi_gpu
from accelerate.utils import patch_environment




class 	A_			(    unittest.TestCase							):






     '''simple docstring'''



     def        UpperCAmelCase_	(		self			:					str	)						->     Tuple:
      UpperCAmelCase	:     Any						 =			inspect.getfile(accelerate.test_utils	)
      UpperCAmelCase	:     Dict						 =			os.path.sep.join(mod_file.split(os.path.sep	)[:-1] + ['scripts', 'test_script.py']	)
      UpperCAmelCase	:     Dict						 =			os.path.sep.join(
          mod_file.split(os.path.sep	)[:-1] + ['scripts', 'test_distributed_data_loop.py']	)
      UpperCAmelCase	:     List[Any]						 =			os.path.sep.join(mod_file.split(os.path.sep	)[:-1] + ['scripts', 'test_ops.py']	)


     @require_multi_gpu
     def        UpperCAmelCase_	(		self			:					Optional[Any]	)						->     Optional[int]:
      print(f"""Found {torch.cuda.device_count()} devices."""	)
      UpperCAmelCase	:     Dict						 =			['torchrun', f"""--nproc_per_node={torch.cuda.device_count()}""", self.test_file_path]
      with patch_environment(omp_num_threads=1	):
       execute_subprocess_async(lowercase_					,      env=os.environ.copy()	)


     @require_multi_gpu
     def        UpperCAmelCase_	(		self			:					Union[str, Any]	)						->     List[Any]:
      print(f"""Found {torch.cuda.device_count()} devices."""	)
      UpperCAmelCase	:     int						 =			['torchrun', f"""--nproc_per_node={torch.cuda.device_count()}""", self.operation_file_path]
      print(f"""Command: {cmd}"""	)
      with patch_environment(omp_num_threads=1	):
       execute_subprocess_async(lowercase_					,      env=os.environ.copy()	)


     @require_multi_gpu
     def        UpperCAmelCase_	(		self			:					str	)						->     List[Any]:
      UpperCAmelCase	:     Optional[int]						 =			['torchrun', f"""--nproc_per_node={torch.cuda.device_count()}""", inspect.getfile(self.__class__	)]
      with patch_environment(omp_num_threads=1	):
       execute_subprocess_async(lowercase_					,      env=os.environ.copy()	)




     @require_multi_gpu
     def        UpperCAmelCase_	(		self			:					List[str]	)						->     Optional[Any]:
      print(f"""Found {torch.cuda.device_count()} devices, using 2 devices only"""	)
      UpperCAmelCase	:     Dict						 =			['torchrun', f"""--nproc_per_node={torch.cuda.device_count()}""", self.data_loop_file_path]
      with patch_environment(omp_num_threads=1					,      cuda_visible_devices='0,1'	):
       execute_subprocess_async(lowercase_					,      env=os.environ.copy()	)


if __name__ == "__main__":
 lowercase__         =      Accelerator()
 lowercase__         =      (accelerator.state.process_index + 2, 10)
 lowercase__         =      torch.randint(0, 10, shape).to(accelerator.device)

 lowercase__         =      ""

 lowercase__         =      accelerator.pad_across_processes(tensor)
 if tensora.shape[0] != accelerator.state.num_processes + 1:
  error_msg += f"Found shape {tensora.shape} but should have {accelerator.state.num_processes + 1} at dim 0."
 if not torch.equal(tensora[: accelerator.state.process_index + 2], tensor):
  error_msg += "Tensors have different values."
 if not torch.all(tensora[accelerator.state.process_index + 2 :] == 0):
  error_msg += "Padding was not done with the right value (0)."

 lowercase__         =      accelerator.pad_across_processes(tensor, pad_first=True)
 if tensora.shape[0] != accelerator.state.num_processes + 1:
  error_msg += f"Found shape {tensora.shape} but should have {accelerator.state.num_processes + 1} at dim 0."
 lowercase__         =      accelerator.state.num_processes - accelerator.state.process_index - 1
 if not torch.equal(tensora[index:], tensor):
  error_msg += "Tensors have different values."
 if not torch.all(tensora[:index] == 0):
  error_msg += "Padding was not done with the right value (0)."

 # Raise error at the end to make sure we don't stop at the first failure.
 if len(error_msg) > 0:
  raise ValueError(error_msg)




