'''simple docstring'''


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

snake_case__					:      Dict				      =  logging.get_logger(__name__)






class 					__SCREAMING_SNAKE_CASE						(	enum.Enum	):







  '''simple docstring'''






  lowerCamelCase_		:Optional[Any]						   =		0
  lowerCamelCase_		:Union[str, Any]						   =		1







@add_end_docstrings(lowerCamelCase_	)
class 					__SCREAMING_SNAKE_CASE						(	lowerCamelCase_	):







  '''simple docstring'''






  lowerCamelCase_		:Tuple						   =		'''generated'''


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




       '''simple docstring'''

       super().__init__(*__UpperCAmelCase				,		**__UpperCAmelCase						)

       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 _UpperCamelCase					(			self				,		snake_case_=None				,		snake_case_=None				,		snake_case_=None				,		snake_case_=None				,		snake_case_=None				,		snake_case_=None				,		**snake_case_				,		):




       '''simple docstring'''

       UpperCAmelCase_							:       Union[str, Any]       =				{}
       if truncation is not None:
            UpperCAmelCase_							:       Dict       =				truncation

       UpperCAmelCase_							:       Optional[int]       =				generate_kwargs

       UpperCAmelCase_							:       Optional[Any]       =				{}
       if return_tensors is not None and return_type is None:
            UpperCAmelCase_							:       str       =				ReturnType.TENSORS if return_tensors else ReturnType.TEXT
       if return_type is not None:
            UpperCAmelCase_							:       Tuple       =				return_type

       if clean_up_tokenization_spaces is not None:
            UpperCAmelCase_							:       Optional[Any]       =				clean_up_tokenization_spaces

       if stop_sequence is not None:
            UpperCAmelCase_							:       List[Any]       =				self.tokenizer.encode(__UpperCAmelCase				,		add_special_tokens=__UpperCAmelCase						)
            if len(__UpperCAmelCase						) > 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.'						)
            UpperCAmelCase_							:       Dict       =				stop_sequence_ids[0]

       return preprocess_params, forward_params, postprocess_params


  def _UpperCamelCase					(			self				,		snake_case_				,		snake_case_				,		snake_case_						):




       '''simple docstring'''

       return True


  def _UpperCamelCase					(			self				,		*snake_case_				,		snake_case_						):




       '''simple docstring'''

       UpperCAmelCase_							:       Dict       =				self.model.config.prefix if self.model.config.prefix is not None else ''
       if isinstance(args[0]				,		__UpperCAmelCase						):
            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'						)
            UpperCAmelCase_							:       str       =				([prefix + arg for arg in args[0]],)
            UpperCAmelCase_							:       Dict       =				True

       elif isinstance(args[0]				,		__UpperCAmelCase						):
            UpperCAmelCase_							:       Tuple       =				(prefix + args[0],)
            UpperCAmelCase_							:       List[Any]       =				False
       else:
            raise ValueError(
                F''' `args[0]`: {args[0]} have the wrong format. The should be either of type `str` or type `list`'''						)
       UpperCAmelCase_							:       Dict       =				self.tokenizer(*__UpperCAmelCase				,		padding=__UpperCAmelCase				,		truncation=__UpperCAmelCase				,		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				,		*snake_case_				,		**snake_case_						):




       '''simple docstring'''

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


  def _UpperCamelCase					(			self				,		snake_case_				,		snake_case_=TruncationStrategy.DO_NOT_TRUNCATE				,		**snake_case_						):




       '''simple docstring'''

       UpperCAmelCase_							:       Dict       =				self._parse_and_tokenize(__UpperCAmelCase				,		truncation=__UpperCAmelCase				,		**__UpperCAmelCase						)
       return inputs


  def _UpperCamelCase					(			self				,		snake_case_				,		**snake_case_						):




       '''simple docstring'''

       if self.framework == "pt":
            UpperCAmelCase_       ,      UpperCAmelCase_							:       int       =				model_inputs['input_ids'].shape
       elif self.framework == "tf":
            UpperCAmelCase_       ,      UpperCAmelCase_							:       List[Any]       =				tf.shape(model_inputs['input_ids']						).numpy()

       UpperCAmelCase_							:       Optional[int]       =				generate_kwargs.get('min_length'				,		self.model.config.min_length						)
       UpperCAmelCase_							:       Any       =				generate_kwargs.get('max_length'				,		self.model.config.max_length						)
       self.check_inputs(__UpperCAmelCase				,		generate_kwargs['min_length']				,		generate_kwargs['max_length']						)
       UpperCAmelCase_							:       int       =				self.model.generate(**__UpperCAmelCase				,		**__UpperCAmelCase						)
       UpperCAmelCase_							:       Dict       =				output_ids.shape[0]
       if self.framework == "pt":
            UpperCAmelCase_							:       Tuple       =				output_ids.reshape(__UpperCAmelCase				,		out_b // in_b				,		*output_ids.shape[1:]						)
       elif self.framework == "tf":
            UpperCAmelCase_							:       List[str]       =				tf.reshape(__UpperCAmelCase				,		(in_b, out_b // in_b, *output_ids.shape[1:])						)
       return {"output_ids": output_ids}






  def _UpperCamelCase					(			self				,		snake_case_				,		snake_case_=ReturnType.TEXT				,		snake_case_=False						):




       '''simple docstring'''

       UpperCAmelCase_							:       List[Any]       =				[]
       for output_ids in model_outputs["output_ids"][0]:
            if return_type == ReturnType.TENSORS:
                 UpperCAmelCase_							:       int       =				{F'''{self.return_name}_token_ids''': output_ids}
            elif return_type == ReturnType.TEXT:
                 UpperCAmelCase_							:       Optional[int]       =				{
                     F'''{self.return_name}_text''': self.tokenizer.decode(
                         __UpperCAmelCase				,		skip_special_tokens=__UpperCAmelCase				,		clean_up_tokenization_spaces=__UpperCAmelCase				,		)
                 }
            records.append(__UpperCAmelCase						)
       return records







@add_end_docstrings(lowerCamelCase_	)
class 					__SCREAMING_SNAKE_CASE						(	lowerCamelCase_	):







  '''simple docstring'''






  lowerCamelCase_		:str						   =		'''summary'''


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




       '''simple docstring'''

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






  def _UpperCamelCase					(			self				,		snake_case_				,		snake_case_				,		snake_case_						):




       '''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(lowerCamelCase_	)
class 					__SCREAMING_SNAKE_CASE						(	lowerCamelCase_	):







  '''simple docstring'''






  lowerCamelCase_		:Dict						   =		'''translation'''


  def _UpperCamelCase					(			self				,		snake_case_				,		snake_case_				,		snake_case_						):




       '''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 _UpperCamelCase					(			self				,		*snake_case_				,		snake_case_=TruncationStrategy.DO_NOT_TRUNCATE				,		snake_case_=None				,		snake_case_=None						):




       '''simple docstring'''

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


  def _UpperCamelCase					(			self				,		snake_case_=None				,		snake_case_=None				,		**snake_case_						):




       '''simple docstring'''

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






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




       '''simple docstring'''

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







'''simple docstring'''


from typing import Optional, Union

import torch
from torch import nn

from ...configuration_utils import ConfigMixin, register_to_config
from ...models.modeling_utils import ModelMixin






class 					__SCREAMING_SNAKE_CASE						(	lowerCamelCase_						,     lowerCamelCase_	):







  '''simple docstring'''






  @register_to_config
  def __init__(			self				,		snake_case_ = 7_6_8				,		):




       '''simple docstring'''

       super().__init__()

       UpperCAmelCase_							:       int       =				nn.Parameter(torch.zeros(1				,		snake_case_						)						)
       UpperCAmelCase_							:       str       =				nn.Parameter(torch.ones(1				,		snake_case_						)						)


  def _UpperCamelCase					(			self				,		snake_case_ = None				,		snake_case_ = None				,		):




       '''simple docstring'''

       UpperCAmelCase_							:       int       =				nn.Parameter(self.mean.to(snake_case_						).to(snake_case_						)						)
       UpperCAmelCase_							:       Tuple       =				nn.Parameter(self.std.to(snake_case_						).to(snake_case_						)						)
       return self


  def _UpperCamelCase					(			self				,		snake_case_						):




       '''simple docstring'''

       UpperCAmelCase_							:       Dict       =				(embeds - self.mean) * 1.0 / self.std
       return embeds






  def _UpperCamelCase					(			self				,		snake_case_						):




       '''simple docstring'''

       UpperCAmelCase_							:       List[Any]       =				(embeds * self.std) + self.mean
       return embeds






