




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







class 						lowercase							(   enum.Enum		):
     _a							 =						0
     _a							 =						1







@add_end_docstrings(UpperCamelCase__		)
class 						lowercase							(   UpperCamelCase__		):
     _a							 =						"generated"

     def __init__(  self		,     *_a		,     **_a				)						->							Optional[int]:
           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		,     )						->							str:
           _A							:     Dict			=						{}
           if truncation is not None:
                 _A							:     Union[str, Any]			=						truncation

           _A							:     Optional[Any]			=						generate_kwargs

           _A							:     List[str]			=						{}
           if return_tensors is not None and return_type is None:
                 _A							:     Any			=						ReturnType.TENSORS if return_tensors else ReturnType.TEXT
           if return_type is not None:
                 _A							:     Union[str, Any]			=						return_type

           if clean_up_tokenization_spaces is not None:
                 _A							:     Tuple			=						clean_up_tokenization_spaces

           if stop_sequence is not None:
                 _A							:     str			=						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."""				)
                 _A							:     Any			=						stop_sequence_ids[0]

           return preprocess_params, forward_params, postprocess_params

     def 					a__	(  self		,     _a		,     _a		,     _a				)						->							str:
           return True

     def 					a__	(  self		,     *_a		,     _a				)						->							List[Any]:
           _A							:     Dict			=						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"""				)
                 _A							:     Optional[int]			=						([prefix + arg for arg in args[0]],)
                 _A							:     Optional[Any]			=						True

           elif isinstance(args[0]		,     _a				):
                 _A							:     Any			=						(prefix + args[0],)
                 _A							:     str			=						False
           else:
                 raise ValueError(
                     F''' `args[0]`: {args[0]} have the wrong format. The should be either of type `str` or type `list`'''				)
           _A							:     Optional[Any]			=						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				)						->							Optional[Any]:
           _A							:     int			=						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				)						->							Tuple:
           _A							:     Optional[Any]			=						self._parse_and_tokenize(_a		,     truncation=_a		,     **_a				)
           return inputs

     def 					a__	(  self		,     _a		,     **_a				)						->							Optional[int]:
           if self.framework == "pt":
                 _A			,   _A							:     Tuple			=						model_inputs["""input_ids"""].shape
           elif self.framework == "tf":
                 _A			,   _A							:     Dict			=						tf.shape(model_inputs["""input_ids"""]				).numpy()

           _A							:     List[Any]			=						generate_kwargs.get("""min_length"""		,     self.model.config.min_length				)
           _A							:     Union[str, Any]			=						generate_kwargs.get("""max_length"""		,     self.model.config.max_length				)
           self.check_inputs(_a		,     generate_kwargs["""min_length"""]		,     generate_kwargs["""max_length"""]				)
           _A							:     str			=						self.model.generate(**_a		,     **_a				)
           _A							:     Optional[int]			=						output_ids.shape[0]
           if self.framework == "pt":
                 _A							:     Dict			=						output_ids.reshape(_a		,     out_b // in_b		,     *output_ids.shape[1:]				)
           elif self.framework == "tf":
                 _A							:     List[str]			=						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				)						->							List[Any]:
           _A							:     Union[str, Any]			=						[]
           for output_ids in model_outputs["output_ids"][0]:
                 if return_type == ReturnType.TENSORS:
                       _A							:     int			=						{F'''{self.return_name}_token_ids''': output_ids}
                 elif return_type == ReturnType.TEXT:
                       _A							:     Optional[int]			=						{
                           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 						lowercase							(   UpperCamelCase__		):
     _a							 =						"summary"

     def __call__(  self		,     *_a		,     **_a				)						->							str:
           return super().__call__(*_a		,     **_a				)



     def 					a__	(  self		,     _a		,     _a		,     _a				)						->							bool:
           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 						lowercase							(   UpperCamelCase__		):
     _a							 =						"translation"

     def 					a__	(  self		,     _a		,     _a		,     _a				)						->							Optional[int]:
           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				)						->							Optional[int]:
           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				)						->							List[Any]:
           _A			,   _A			,   _A							:     str			=						super()._sanitize_parameters(**_a				)
           if src_lang is not None:
                 _A							:     Optional[Any]			=						src_lang
           if tgt_lang is not None:
                 _A							:     Any			=						tgt_lang
           if src_lang is None and tgt_lang is None:
                 # Backward compatibility, direct arguments use is preferred.
                 _A							:     Any			=						kwargs.get("""task"""		,     self.task				)
                 _A							:     Union[str, Any]			=						task.split("""_"""				)
                 if task and len(_a				) == 4:
                       # translation, XX, to YY
                       _A							:     int			=						items[1]
                       _A							:     Dict			=						items[3]
           return preprocess_params, forward_params, postprocess_params



     def __call__(  self		,     *_a		,     **_a				)						->							Union[str, Any]:
           return super().__call__(*_a		,     **_a				)







from collections import OrderedDict
from typing import Mapping

from ...configuration_utils import PretrainedConfig
from ...onnx import OnnxConfig
from ...utils import logging


_snake_case										=       logging.get_logger(__name__)

_snake_case										=       {
    "facebook/xmod-base": "https://huggingface.co/facebook/xmod-base/resolve/main/config.json",
    "facebook/xmod-large-prenorm": "https://huggingface.co/facebook/xmod-large-prenorm/resolve/main/config.json",
    "facebook/xmod-base-13-125k": "https://huggingface.co/facebook/xmod-base-13-125k/resolve/main/config.json",
    "facebook/xmod-base-30-125k": "https://huggingface.co/facebook/xmod-base-30-125k/resolve/main/config.json",
    "facebook/xmod-base-30-195k": "https://huggingface.co/facebook/xmod-base-30-195k/resolve/main/config.json",
    "facebook/xmod-base-60-125k": "https://huggingface.co/facebook/xmod-base-60-125k/resolve/main/config.json",
    "facebook/xmod-base-60-265k": "https://huggingface.co/facebook/xmod-base-60-265k/resolve/main/config.json",
    "facebook/xmod-base-75-125k": "https://huggingface.co/facebook/xmod-base-75-125k/resolve/main/config.json",
    "facebook/xmod-base-75-269k": "https://huggingface.co/facebook/xmod-base-75-269k/resolve/main/config.json",
}







class 						lowercase							(   UpperCamelCase__		):
     _a							 =						"xmod"

     def __init__(  self		,     _a=3_0522		,     _a=768		,     _a=12		,     _a=12		,     _a=3072		,     _a="gelu"		,     _a=0.1		,     _a=0.1		,     _a=512		,     _a=2		,     _a=0.02		,     _a=1e-12		,     _a=1		,     _a=0		,     _a=2		,     _a="absolute"		,     _a=True		,     _a=None		,     _a=False		,     _a=2		,     _a=False		,     _a=True		,     _a=True		,     _a=("en_XX",)		,     _a=None		,     **_a		,     )						->							str:
           super().__init__(pad_token_id=_a		,     bos_token_id=_a		,     eos_token_id=_a		,     **_a				)

           _A							:     Tuple			=						vocab_size
           _A							:     Union[str, Any]			=						hidden_size
           _A							:     Dict			=						num_hidden_layers
           _A							:     Dict			=						num_attention_heads
           _A							:     List[Any]			=						hidden_act
           _A							:     Optional[Any]			=						intermediate_size
           _A							:     Any			=						hidden_dropout_prob
           _A							:     str			=						attention_probs_dropout_prob
           _A							:     Dict			=						max_position_embeddings
           _A							:     Any			=						type_vocab_size
           _A							:     List[Any]			=						initializer_range
           _A							:     int			=						layer_norm_eps
           _A							:     int			=						position_embedding_type
           _A							:     Any			=						use_cache
           _A							:     int			=						classifier_dropout
           _A							:     int			=						pre_norm
           _A							:     Optional[Any]			=						adapter_reduction_factor
           _A							:     List[Any]			=						adapter_layer_norm
           _A							:     Optional[int]			=						adapter_reuse_layer_norm
           _A							:     Any			=						ln_before_adapter
           _A							:     Union[str, Any]			=						list(_a				)
           _A							:     List[Any]			=						default_language







class 						lowercase							(   UpperCamelCase__		):

     @property
     def 					a__	(  self				)						->							Mapping[str, Mapping[int, str]]:
           if self.task == "multiple-choice":
                 _A							:     Dict			=						{0: """batch""", 1: """choice""", 2: """sequence"""}
           else:
                 _A							:     Dict			=						{0: """batch""", 1: """sequence"""}
           return OrderedDict(
               [
                   ("""input_ids""", dynamic_axis),
                   ("""attention_mask""", dynamic_axis),
               ]				)

