



import enum
import warnings

from ..tokenization_utils import TruncationStrategy
from ..utils import add_end_docstrings, is_tf_available, is_torch_available, logging
from .base import PIPELINE_INIT_ARGS, Pipeline


if is_tf_available():
     import tensorflow as tf

     from ..models.auto.modeling_tf_auto import TF_MODEL_FOR_SEQ_TO_SEQ_CAUSAL_LM_MAPPING

if is_torch_available():
     from ..models.auto.modeling_auto import MODEL_FOR_SEQ_TO_SEQ_CAUSAL_LM_MAPPING

a_    :  Dict						  =		logging.get_logger(__name__)





class  lowerCamelCase__	(    enum.Enum):
     """simple docstring"""



     _A      =      0
     _A      =      1





@add_end_docstrings(UpperCAmelCase_)
class  lowerCamelCase__	(    UpperCAmelCase_):
     """simple docstring"""



     _A      =      'generated'







     def __init__(self					,      *__a					,      **__a     ):






           '''simple docstring'''



           super().__init__(*__a					,      **__a     )

           self.check_model_type(
               TF_MODEL_FOR_SEQ_TO_SEQ_CAUSAL_LM_MAPPING
               if self.framework == "tf"
               else MODEL_FOR_SEQ_TO_SEQ_CAUSAL_LM_MAPPING     )







     def     _a   (self					,      __a=None					,      __a=None					,      __a=None					,      __a=None					,      __a=None					,      __a=None					,      **__a					,      ):






           '''simple docstring'''



           lowerCamelCase             =		{}
           if truncation is not None:
                 lowerCamelCase             =		truncation

           lowerCamelCase             =		generate_kwargs

           lowerCamelCase             =		{}
           if return_tensors is not None and return_type is None:
                 lowerCamelCase             =		ReturnType.TENSORS if return_tensors else ReturnType.TEXT
           if return_type is not None:
                 lowerCamelCase             =		return_type

           if clean_up_tokenization_spaces is not None:
                 lowerCamelCase             =		clean_up_tokenization_spaces

           if stop_sequence is not None:
                 lowerCamelCase             =		self.tokenizer.encode(__a					,      add_special_tokens=__a     )
                 if len(__a     ) > 1:
                       warnings.warn(
                           "Stopping on a multiple token sequence is not yet supported on transformers. The first token of"
                           " the stop sequence will be used as the stop sequence string in the interim."     )
                 lowerCamelCase             =		stop_sequence_ids[0]

           return preprocess_params, forward_params, postprocess_params







     def     _a   (self					,      __a					,      __a					,      __a     ):






           '''simple docstring'''



           return True







     def     _a   (self					,      *__a					,      __a     ):






           '''simple docstring'''



           lowerCamelCase             =		self.model.config.prefix if self.model.config.prefix is not None else ""
           if isinstance(args[0]					,      __a     ):
                 if self.tokenizer.pad_token_id is None:
                       raise ValueError("Please make sure that the tokenizer has a pad_token_id when using a batch input"     )
                 lowerCamelCase             =		([prefix + arg for arg in args[0]],)
                 lowerCamelCase             =		True

           elif isinstance(args[0]					,      __a     ):
                 lowerCamelCase             =		(prefix + args[0],)
                 lowerCamelCase             =		False
           else:
                 raise ValueError(
                     F""" `args[0]`: {args[0]} have the wrong format. The should be either of type `str` or type `list`"""     )
           lowerCamelCase             =		self.tokenizer(*__a					,      padding=__a					,      truncation=__a					,      return_tensors=self.framework     )
           # This is produced by tokenizers but is an invalid generate kwargs
           if "token_type_ids" in inputs:
                 del inputs["token_type_ids"]
           return inputs







     def __call__(self					,      *__a					,      **__a     ):






           '''simple docstring'''



           lowerCamelCase             =		super().__call__(*__a					,      **__a     )
           if (
               isinstance(args[0]					,      __a     )
               and all(isinstance(__a					,      __a     ) for el in args[0]     )
               and all(len(__a     ) == 1 for res in result     )
           ):
                 return [res[0] for res in result]
           return result







     def     _a   (self					,      __a					,      __a=TruncationStrategy.DO_NOT_TRUNCATE					,      **__a     ):






           '''simple docstring'''



           lowerCamelCase             =		self._parse_and_tokenize(__a					,      truncation=__a					,      **__a     )
           return inputs







     def     _a   (self					,      __a					,      **__a     ):






           '''simple docstring'''



           if self.framework == "pt":
                 lowerCamelCase      ,				lowerCamelCase             =		model_inputs["input_ids"].shape
           elif self.framework == "tf":
                 lowerCamelCase      ,				lowerCamelCase             =		tf.shape(model_inputs["input_ids"]     ).numpy()

           lowerCamelCase             =		generate_kwargs.get("min_length"					,      self.model.config.min_length     )
           lowerCamelCase             =		generate_kwargs.get("max_length"					,      self.model.config.max_length     )
           self.check_inputs(__a					,      generate_kwargs["min_length"]					,      generate_kwargs["max_length"]     )
           lowerCamelCase             =		self.model.generate(**__a					,      **__a     )
           lowerCamelCase             =		output_ids.shape[0]
           if self.framework == "pt":
                 lowerCamelCase             =		output_ids.reshape(__a					,      out_b // in_b					,      *output_ids.shape[1:]     )
           elif self.framework == "tf":
                 lowerCamelCase             =		tf.reshape(__a					,      (in_b, out_b // in_b, *output_ids.shape[1:])     )
           return {"output_ids": output_ids}







     def     _a   (self					,      __a					,      __a=ReturnType.TEXT					,      __a=False     ):






           '''simple docstring'''



           lowerCamelCase             =		[]
           for output_ids in model_outputs["output_ids"][0]:
                 if return_type == ReturnType.TENSORS:
                       lowerCamelCase             =		{F"""{self.return_name}_token_ids""": output_ids}
                 elif return_type == ReturnType.TEXT:
                       lowerCamelCase             =		{
                           F"""{self.return_name}_text""": self.tokenizer.decode(
                               __a					,      skip_special_tokens=__a					,      clean_up_tokenization_spaces=__a					,      )
                       }
                 records.append(__a     )
           return records





@add_end_docstrings(UpperCAmelCase_)
class  lowerCamelCase__	(    UpperCAmelCase_):
     """simple docstring"""



     _A      =      'summary'







     def __call__(self					,      *__a					,      **__a     ):






           '''simple docstring'''



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







     def     _a   (self					,      __a					,      __a					,      __a     ):






           '''simple docstring'''



           if max_length < min_length:
                 logger.warning(F"""Your min_length={min_length} must be inferior than your max_length={max_length}."""     )

           if input_length < max_length:
                 logger.warning(
                     F"""Your max_length is set to {max_length}, but your input_length is only {input_length}. Since this is """
                     "a summarization task, where outputs shorter than the input are typically wanted, you might "
                     F"""consider decreasing max_length manually, e.g. summarizer('...', max_length={input_length//2})"""     )





@add_end_docstrings(UpperCAmelCase_)
class  lowerCamelCase__	(    UpperCAmelCase_):
     """simple docstring"""



     _A      =      'translation'







     def     _a   (self					,      __a					,      __a					,      __a     ):






           '''simple docstring'''



           if input_length > 0.9 * max_length:
                 logger.warning(
                     F"""Your input_length: {input_length} is bigger than 0.9 * max_length: {max_length}. You might consider """
                     "increasing your max_length manually, e.g. translator('...', max_length=400)"     )
           return True







     def     _a   (self					,      *__a					,      __a=TruncationStrategy.DO_NOT_TRUNCATE					,      __a=None					,      __a=None     ):






           '''simple docstring'''



           if getattr(self.tokenizer					,      "_build_translation_inputs"					,      __a     ):
                 return self.tokenizer._build_translation_inputs(
                     *__a					,      return_tensors=self.framework					,      truncation=__a					,      src_lang=__a					,      tgt_lang=__a     )
           else:
                 return super()._parse_and_tokenize(*__a					,      truncation=__a     )







     def     _a   (self					,      __a=None					,      __a=None					,      **__a     ):






           '''simple docstring'''



           lowerCamelCase      ,				lowerCamelCase      ,				lowerCamelCase             =		super()._sanitize_parameters(**__a     )
           if src_lang is not None:
                 lowerCamelCase             =		src_lang
           if tgt_lang is not None:
                 lowerCamelCase             =		tgt_lang
           if src_lang is None and tgt_lang is None:
                 # Backward compatibility, direct arguments use is preferred.
                 lowerCamelCase             =		kwargs.get("task"					,      self.task     )
                 lowerCamelCase             =		task.split("_"     )
                 if task and len(__a     ) == 4:
                       # translation, XX, to YY
                       lowerCamelCase             =		items[1]
                       lowerCamelCase             =		items[3]
           return preprocess_params, forward_params, postprocess_params







     def __call__(self					,      *__a					,      **__a     ):






           '''simple docstring'''



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




from dataclasses import dataclass, field
from typing import Tuple

from ..utils import cached_property, is_tf_available, logging, requires_backends
from .benchmark_args_utils import BenchmarkArguments


if is_tf_available():
     import tensorflow as tf


a_    :  Any						  =		logging.get_logger(__name__)





@dataclass
class  lowerCamelCase__	(    UpperCAmelCase_):
     """simple docstring"""



     _A      =      [
         'no_inference',
         'no_cuda',
         'no_tpu',
         'no_speed',
         'no_memory',
         'no_env_print',
         'no_multi_process',
     ]







     def __init__(self					,      **__a     ):






           '''simple docstring'''



           for deprecated_arg in self.deprecated_args:
                 if deprecated_arg in kwargs:
                       lowerCamelCase             =		deprecated_arg[3:]
                       lowerCamelCase             =		not kwargs.pop(__a     )
                       logger.warning(
                           F"""{deprecated_arg} is depreciated. Please use --no-{positive_arg} or"""
                           F""" {positive_arg}={kwargs[positive_arg]}"""     )
           lowerCamelCase             =		kwargs.pop("tpu_name"					,      self.tpu_name     )
           lowerCamelCase             =		kwargs.pop("device_idx"					,      self.device_idx     )
           lowerCamelCase             =		kwargs.pop("eager_mode"					,      self.eager_mode     )
           lowerCamelCase             =		kwargs.pop("use_xla"					,      self.use_xla     )
           super().__init__(**__a     )

     _A      =      field(
         default=UpperCAmelCase_							,     metadata={'help': 'Name of TPU'}							,     )
     _A      =      field(
         default=0							,     metadata={'help': 'CPU / GPU device index. Defaults to 0.'}							,     )
     _A      =      field(default=UpperCAmelCase_							,     metadata={'help': 'Benchmark models in eager model.'})
     _A      =      field(
         default=UpperCAmelCase_							,     metadata={
             'help': 'Benchmark models using XLA JIT compilation. Note that `eager_model` has to be set to `False`.'
         }							,     )







     @cached_property
     def     _a   (self     ):






           '''simple docstring'''



           requires_backends(self					,      ["tf"]     )
           lowerCamelCase             =		None
           if self.tpu:
                 try:
                       if self.tpu_name:
                             lowerCamelCase             =		tf.distribute.cluster_resolver.TPUClusterResolver(self.tpu_name     )
                       else:
                             lowerCamelCase             =		tf.distribute.cluster_resolver.TPUClusterResolver()
                 except ValueError:
                       lowerCamelCase             =		None
           return tpu







     @cached_property
     def     _a   (self     ):






           '''simple docstring'''



           requires_backends(self					,      ["tf"]     )
           if self.is_tpu:
                 tf.config.experimental_connect_to_cluster(self._setup_tpu     )
                 tf.tpu.experimental.initialize_tpu_system(self._setup_tpu     )

                 lowerCamelCase             =		tf.distribute.TPUStrategy(self._setup_tpu     )
           else:
                 # currently no multi gpu is allowed
                 if self.is_gpu:
                       # TODO: Currently only single GPU is supported
                       tf.config.set_visible_devices(self.gpu_list[self.device_idx]					,      "GPU"     )
                       lowerCamelCase             =		tf.distribute.OneDeviceStrategy(device=F"""/gpu:{self.device_idx}"""     )
                 else:
                       tf.config.set_visible_devices([]					,      "GPU"     )  # disable GPU
                       lowerCamelCase             =		tf.distribute.OneDeviceStrategy(device=F"""/cpu:{self.device_idx}"""     )

           return strategy







     @property
     def     _a   (self     ):






           '''simple docstring'''



           requires_backends(self					,      ["tf"]     )
           return self._setup_tpu is not None







     @property
     def     _a   (self     ):






           '''simple docstring'''



           requires_backends(self					,      ["tf"]     )
           return self._setup_strategy







     @property
     def     _a   (self     ):






           '''simple docstring'''



           requires_backends(self					,      ["tf"]     )
           return tf.config.list_physical_devices("GPU"     )







     @property
     def     _a   (self     ):






           '''simple docstring'''



           requires_backends(self					,      ["tf"]     )
           if self.cuda:
                 return len(self.gpu_list     )
           return 0







     @property
     def     _a   (self     ):






           '''simple docstring'''



           return self.n_gpu > 0