





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      :       str									=  logging.get_logger(__name__)





class  A__    (							__UpperCAmelCase	):




   """simple docstring"""



   def 							__lowercase		(					self					,  lowercase)			->						List[str]:







          '''simple docstring'''

          if isinstance(lowercase					,  lowercase):
                 a__						:	int =				[label.strip() for label in labels.split(',') if label.strip()]
          return labels

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







          '''simple docstring'''

          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):
                 a__						:	List[Any] =				[sequences]

          a__						:	List[Any] =				[]
          for sequence in sequences:
                 sequence_pairs.extend([[sequence, hypothesis_template.format(lowercase)] for label in labels])

          return sequence_pairs, sequences





@add_end_docstrings(__UpperCAmelCase	)
class  A__    (							__UpperCAmelCase	):




   """simple docstring"""



   def __init__(					self					,  lowercase=ZeroShotClassificationArgumentHandler()					,  *lowercase					,  **lowercase)			->						Dict:







          '''simple docstring'''

          a__						:	List[Any] =				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 							__lowercase		(					self)			->						Any:







          '''simple docstring'''

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

   def 							__lowercase		(					self					,  lowercase					,  lowercase=True					,  lowercase=True					,  lowercase=TruncationStrategy.ONLY_FIRST					,  **lowercase)			->						Optional[int]:







          '''simple docstring'''

          a__						:	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`')
                 a__						:	List[str] =				self.tokenizer.eos_token
          try:
                 a__						:	Optional[int] =				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.

                        a__						:	Any =				self.tokenizer(
                            lowercase					,  add_special_tokens=lowercase					,  return_tensors=lowercase					,  padding=lowercase					,  truncation=TruncationStrategy.DO_NOT_TRUNCATE					,  )
                 else:
                        raise e

          return inputs

   def 							__lowercase		(					self					,  **lowercase)			->						str:







          '''simple docstring'''

          if kwargs.get('multi_class'					,  lowercase) is not None:
                 a__						:	str =				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.')
          a__						:	List[str] =				{}
          if "candidate_labels" in kwargs:
                 a__						:	Union[str, Any] =				self._args_parser._parse_labels(kwargs['candidate_labels'])
          if "hypothesis_template" in kwargs:
                 a__						:	Dict =				kwargs['hypothesis_template']

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

   def __call__(					self					,  lowercase					,  *lowercase					,  **lowercase					,  )			->						Dict:







          '''simple docstring'''

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

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

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







          '''simple docstring'''

          a__ ,							a__						:	Any =				self._args_parser(lowercase					,  lowercase					,  lowercase)

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

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

   def 							__lowercase		(					self					,  lowercase)			->						List[str]:







          '''simple docstring'''

          a__						:	Any =				inputs['candidate_label']
          a__						:	int =				inputs['sequence']
          a__						:	Tuple =				{k: inputs[k] for k in self.tokenizer.model_input_names}
          a__						:	Optional[int] =				self.model(**lowercase)

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

   def 							__lowercase		(					self					,  lowercase					,  lowercase=False)			->						Dict:







          '''simple docstring'''

          a__						:	Dict =				[outputs['candidate_label'] for outputs in model_outputs]
          a__						:	Optional[int] =				[outputs['sequence'] for outputs in model_outputs]
          a__						:	Any =				np.concatenate([output['logits'].numpy() for output in model_outputs])
          a__						:	Optional[Any] =				logits.shape[0]
          a__						:	Dict =				len(lowercase)
          a__						:	List[Any] =				N // n
          a__						:	List[Any] =				logits.reshape((num_sequences, n, -1))

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

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









import os
import tempfile
import unittest
from pathlib import Path

from transformers import AutoConfig, is_tf_available
from transformers.testing_utils import require_tf


if is_tf_available():
    import tensorflow as tf

    from transformers import TensorFlowBenchmark, TensorFlowBenchmarkArguments





@require_tf
class  A__    (							unittest.TestCase	):




   """simple docstring"""



   def 							__lowercase		(					self					,  lowercase)			->						Dict:







          '''simple docstring'''

          for model_result in results.values():
                 for batch_size, sequence_length in zip(model_result['bs']					,  model_result['ss']):
                        a__						:	int =				model_result['result'][batch_size][sequence_length]
                        self.assertIsNotNone(lowercase)

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







          '''simple docstring'''

          a__						:	Tuple =				'sshleifer/tiny-gpt2'
          a__						:	str =				TensorFlowBenchmarkArguments(
              models=[MODEL_ID]					,  training=lowercase					,  inference=lowercase					,  sequence_lengths=[8]					,  batch_sizes=[1]					,  eager_mode=lowercase					,  multi_process=lowercase					,  )
          a__						:	List[Any] =				TensorFlowBenchmark(lowercase)
          a__						:	Any =				benchmark.run()
          self.check_results_dict_not_empty(results.time_inference_result)
          self.check_results_dict_not_empty(results.memory_inference_result)

   def 							__lowercase		(					self)			->						Tuple:







          '''simple docstring'''

          a__						:	Optional[Any] =				'sgugger/tiny-distilbert-classification'
          a__						:	List[str] =				TensorFlowBenchmarkArguments(
              models=[MODEL_ID]					,  training=lowercase					,  inference=lowercase					,  sequence_lengths=[8]					,  batch_sizes=[1]					,  multi_process=lowercase					,  only_pretrain_model=lowercase					,  )
          a__						:	str =				TensorFlowBenchmark(lowercase)
          a__						:	int =				benchmark.run()
          self.check_results_dict_not_empty(results.time_inference_result)
          self.check_results_dict_not_empty(results.memory_inference_result)

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







          '''simple docstring'''

          a__						:	Union[str, Any] =				'sshleifer/tiny-gpt2'
          a__						:	Dict =				TensorFlowBenchmarkArguments(
              models=[MODEL_ID]					,  training=lowercase					,  inference=lowercase					,  sequence_lengths=[8]					,  batch_sizes=[1]					,  multi_process=lowercase					,  )
          a__						:	List[str] =				TensorFlowBenchmark(lowercase)
          a__						:	Tuple =				benchmark.run()
          self.check_results_dict_not_empty(results.time_inference_result)
          self.check_results_dict_not_empty(results.memory_inference_result)

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







          '''simple docstring'''

          a__						:	str =				'sshleifer/tiny-gpt2'
          a__						:	str =				AutoConfig.from_pretrained(lowercase)
          a__						:	Any =				TensorFlowBenchmarkArguments(
              models=[MODEL_ID]					,  training=lowercase					,  inference=lowercase					,  sequence_lengths=[8]					,  batch_sizes=[1]					,  eager_mode=lowercase					,  multi_process=lowercase					,  )
          a__						:	Optional[Any] =				TensorFlowBenchmark(lowercase					,  [config])
          a__						:	Union[str, Any] =				benchmark.run()
          self.check_results_dict_not_empty(results.time_inference_result)
          self.check_results_dict_not_empty(results.memory_inference_result)

   def 							__lowercase		(					self)			->						str:







          '''simple docstring'''

          a__						:	Dict =				'sshleifer/tiny-gpt2'
          a__						:	Union[str, Any] =				AutoConfig.from_pretrained(lowercase)
          a__						:	Optional[int] =				TensorFlowBenchmarkArguments(
              models=[MODEL_ID]					,  training=lowercase					,  inference=lowercase					,  sequence_lengths=[8]					,  batch_sizes=[1]					,  multi_process=lowercase					,  )
          a__						:	str =				TensorFlowBenchmark(lowercase					,  [config])
          a__						:	List[str] =				benchmark.run()
          self.check_results_dict_not_empty(results.time_inference_result)
          self.check_results_dict_not_empty(results.memory_inference_result)

   def 							__lowercase		(					self)			->						Dict:







          '''simple docstring'''

          a__						:	Optional[int] =				'sshleifer/tiny-gpt2'
          a__						:	Any =				TensorFlowBenchmarkArguments(
              models=[MODEL_ID]					,  training=lowercase					,  inference=lowercase					,  sequence_lengths=[8]					,  batch_sizes=[1]					,  multi_process=lowercase					,  )
          a__						:	Tuple =				TensorFlowBenchmark(lowercase)
          a__						:	str =				benchmark.run()
          self.check_results_dict_not_empty(results.time_train_result)
          self.check_results_dict_not_empty(results.memory_train_result)

   def 							__lowercase		(					self)			->						int:







          '''simple docstring'''

          a__						:	Optional[Any] =				'sshleifer/tiny-gpt2'
          a__						:	List[Any] =				AutoConfig.from_pretrained(lowercase)
          a__						:	List[Any] =				TensorFlowBenchmarkArguments(
              models=[MODEL_ID]					,  training=lowercase					,  inference=lowercase					,  sequence_lengths=[8]					,  batch_sizes=[1]					,  multi_process=lowercase					,  )
          a__						:	Optional[Any] =				TensorFlowBenchmark(lowercase					,  [config])
          a__						:	List[Any] =				benchmark.run()
          self.check_results_dict_not_empty(results.time_train_result)
          self.check_results_dict_not_empty(results.memory_train_result)

   def 							__lowercase		(					self)			->						int:







          '''simple docstring'''

          a__						:	Optional[int] =				'patrickvonplaten/t5-tiny-random'
          a__						:	Optional[int] =				AutoConfig.from_pretrained(lowercase)
          a__						:	Any =				TensorFlowBenchmarkArguments(
              models=[MODEL_ID]					,  training=lowercase					,  inference=lowercase					,  sequence_lengths=[8]					,  batch_sizes=[1]					,  multi_process=lowercase					,  )
          a__						:	List[Any] =				TensorFlowBenchmark(lowercase					,  configs=[config])
          a__						:	Union[str, Any] =				benchmark.run()
          self.check_results_dict_not_empty(results.time_inference_result)
          self.check_results_dict_not_empty(results.memory_inference_result)

   @unittest.skipIf(is_tf_available() and len(tf.config.list_physical_devices('GPU')) == 0					,  'Cannot do xla on CPU.')
   def 							__lowercase		(					self)			->						List[Any]:







          '''simple docstring'''

          a__						:	Optional[int] =				'sshleifer/tiny-gpt2'
          a__						:	str =				TensorFlowBenchmarkArguments(
              models=[MODEL_ID]					,  training=lowercase					,  inference=lowercase					,  sequence_lengths=[8]					,  batch_sizes=[1]					,  use_xla=lowercase					,  multi_process=lowercase					,  )
          a__						:	int =				TensorFlowBenchmark(lowercase)
          a__						:	Tuple =				benchmark.run()
          self.check_results_dict_not_empty(results.time_inference_result)
          self.check_results_dict_not_empty(results.memory_inference_result)

   def 							__lowercase		(					self)			->						Dict:







          '''simple docstring'''

          a__						:	str =				'sshleifer/tiny-gpt2'
          with tempfile.TemporaryDirectory() as tmp_dir:
                 a__						:	int =				TensorFlowBenchmarkArguments(
                     models=[MODEL_ID]					,  inference=lowercase					,  save_to_csv=lowercase					,  sequence_lengths=[8]					,  batch_sizes=[1]					,  inference_time_csv_file=os.path.join(lowercase					,  'inf_time.csv')					,  inference_memory_csv_file=os.path.join(lowercase					,  'inf_mem.csv')					,  env_info_csv_file=os.path.join(lowercase					,  'env.csv')					,  multi_process=lowercase					,  )
                 a__						:	List[str] =				TensorFlowBenchmark(lowercase)
                 benchmark.run()
                 self.assertTrue(Path(os.path.join(lowercase					,  'inf_time.csv')).exists())
                 self.assertTrue(Path(os.path.join(lowercase					,  'inf_mem.csv')).exists())
                 self.assertTrue(Path(os.path.join(lowercase					,  'env.csv')).exists())

   def 							__lowercase		(					self)			->						str:







          '''simple docstring'''

          a__						:	Union[str, Any] =				'sshleifer/tiny-gpt2'

          def _check_summary_is_not_empty(lowercase):
                 self.assertTrue(hasattr(lowercase					,  'sequential'))
                 self.assertTrue(hasattr(lowercase					,  'cumulative'))
                 self.assertTrue(hasattr(lowercase					,  'current'))
                 self.assertTrue(hasattr(lowercase					,  'total'))

          with tempfile.TemporaryDirectory() as tmp_dir:
                 a__						:	List[str] =				TensorFlowBenchmarkArguments(
                     models=[MODEL_ID]					,  inference=lowercase					,  sequence_lengths=[8]					,  batch_sizes=[1]					,  log_filename=os.path.join(lowercase					,  'log.txt')					,  log_print=lowercase					,  trace_memory_line_by_line=lowercase					,  eager_mode=lowercase					,  multi_process=lowercase					,  )
                 a__						:	List[str] =				TensorFlowBenchmark(lowercase)
                 a__						:	Any =				benchmark.run()
                 _check_summary_is_not_empty(result.inference_summary)
                 self.assertTrue(Path(os.path.join(lowercase					,  'log.txt')).exists())


