




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

__lowerCAmelCase		:				Any       			=		logging.get_logger(__name__)




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



				SCREAMING_SNAKE_CASE_       :							Dict							 =					0
				SCREAMING_SNAKE_CASE_       :							Optional[Any]							 =					1




@add_end_docstrings(_UpperCamelCase   )
class     snake_case__							(_UpperCamelCase   ):
				"""simple docstring"""



				SCREAMING_SNAKE_CASE_       :							int							 =					"""generated"""





				def __init__(      self				:     Dict					,      *__lowerCamelCase				:     List[Any]					,      **__lowerCamelCase				:     Union[str, Any]							)					->     List[Any]:
							super().__init__(*__lowerCamelCase					,      **__lowerCamelCase							)

							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				:     List[Any]					,      __lowerCamelCase				:     List[str]=None					,      __lowerCamelCase				:     List[str]=None					,      __lowerCamelCase				:     Any=None					,      __lowerCamelCase				:     Union[str, Any]=None					,      __lowerCamelCase				:     Union[str, Any]=None					,      __lowerCamelCase				:     str=None					,      **__lowerCamelCase				:     str					,      )					->     Tuple:
							a    =			{}
							if truncation is not None:
										a    =			truncation

							a    =			generate_kwargs

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

							if clean_up_tokenization_spaces is not None:
										a    =			clean_up_tokenization_spaces

							if stop_sequence is not None:
										a    =			self.tokenizer.encode(__lowerCamelCase					,      add_special_tokens=__lowerCamelCase							)
										if len(__lowerCamelCase							) > 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    =			stop_sequence_ids[0]

							return preprocess_params, forward_params, postprocess_params





				def 							__UpperCAmelCase							(      self				:     List[Any]					,      __lowerCamelCase				:     int					,      __lowerCamelCase				:     int					,      __lowerCamelCase				:     int							)					->     Union[str, Any]:
							return True





				def 							__UpperCAmelCase							(      self				:     Optional[int]					,      *__lowerCamelCase				:     str					,      __lowerCamelCase				:     Optional[int]							)					->     Union[str, Any]:
							a    =			self.model.config.prefix if self.model.config.prefix is not None else ""
							if isinstance(args[0]					,      __lowerCamelCase							):
										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    =			([prefix + arg for arg in args[0]],)
										a    =			True

							elif isinstance(args[0]					,      __lowerCamelCase							):
										a    =			(prefix + args[0],)
										a    =			False
							else:
										raise ValueError(
										    f""" `args[0]`: {args[0]} have the wrong format. The should be either of type `str` or type `list`"""							)
							a    =			self.tokenizer(*__lowerCamelCase					,      padding=__lowerCamelCase					,      truncation=__lowerCamelCase					,      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				:     Any					,      *__lowerCamelCase				:     List[Any]					,      **__lowerCamelCase				:     int							)					->     Optional[int]:
							a    =			super().__call__(*__lowerCamelCase					,      **__lowerCamelCase							)
							if (
							    isinstance(args[0]					,      __lowerCamelCase							)
							    and all(isinstance(__lowerCamelCase					,      __lowerCamelCase							) for el in args[0]							)
							    and all(len(__lowerCamelCase							) == 1 for res in result							)
							):
										return [res[0] for res in result]
							return result





				def 							__UpperCAmelCase							(      self				:     str					,      __lowerCamelCase				:     Any					,      __lowerCamelCase				:     List[Any]=TruncationStrategy.DO_NOT_TRUNCATE					,      **__lowerCamelCase				:     str							)					->     Optional[int]:
							a    =			self._parse_and_tokenize(__lowerCamelCase					,      truncation=__lowerCamelCase					,      **__lowerCamelCase							)
							return inputs





				def 							__UpperCAmelCase							(      self				:     List[Any]					,      __lowerCamelCase				:     Optional[int]					,      **__lowerCamelCase				:     Dict							)					->     Optional[int]:
							if self.framework == "pt":
										a    ,		a    =			model_inputs["input_ids"].shape
							elif self.framework == "tf":
										a    ,		a    =			tf.shape(model_inputs["input_ids"]							).numpy()

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





				def 							__UpperCAmelCase							(      self				:     Optional[int]					,      __lowerCamelCase				:     List[Any]					,      __lowerCamelCase				:     List[Any]=ReturnType.TEXT					,      __lowerCamelCase				:     List[Any]=False							)					->     int:
							a    =			[]
							for output_ids in model_outputs["output_ids"][0]:
										if return_type == ReturnType.TENSORS:
													a    =			{f"""{self.return_name}_token_ids""": output_ids}
										elif return_type == ReturnType.TEXT:
													a    =			{
													    f"""{self.return_name}_text""": self.tokenizer.decode(
													        __lowerCamelCase					,      skip_special_tokens=__lowerCamelCase					,      clean_up_tokenization_spaces=__lowerCamelCase					,      )
													}
										records.append(__lowerCamelCase							)
							return records




@add_end_docstrings(_UpperCamelCase   )
class     snake_case__							(_UpperCamelCase   ):
				"""simple docstring"""



				SCREAMING_SNAKE_CASE_       :							Union[str, Any]							 =					"""summary"""





				def __call__(      self				:     Dict					,      *__lowerCamelCase				:     List[Any]					,      **__lowerCamelCase				:     str							)					->     Optional[Any]:
							return super().__call__(*__lowerCamelCase					,      **__lowerCamelCase							)





				def 							__UpperCAmelCase							(      self				:     Tuple					,      __lowerCamelCase				:     int					,      __lowerCamelCase				:     int					,      __lowerCamelCase				:     int							)					->     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     snake_case__							(_UpperCamelCase   ):
				"""simple docstring"""



				SCREAMING_SNAKE_CASE_       :							Dict							 =					"""translation"""





				def 							__UpperCAmelCase							(      self				:     Optional[int]					,      __lowerCamelCase				:     int					,      __lowerCamelCase				:     int					,      __lowerCamelCase				:     int							)					->     Dict:
							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				:     Union[str, Any]					,      *__lowerCamelCase				:     Optional[int]					,      __lowerCamelCase				:     Any=TruncationStrategy.DO_NOT_TRUNCATE					,      __lowerCamelCase				:     Optional[int]=None					,      __lowerCamelCase				:     str=None							)					->     Optional[Any]:
							if getattr(self.tokenizer					,      "_build_translation_inputs"					,      __lowerCamelCase							):
										return self.tokenizer._build_translation_inputs(
										    *__lowerCamelCase					,      return_tensors=self.framework					,      truncation=__lowerCamelCase					,      src_lang=__lowerCamelCase					,      tgt_lang=__lowerCamelCase							)
							else:
										return super()._parse_and_tokenize(*__lowerCamelCase					,      truncation=__lowerCamelCase							)





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





				def __call__(      self				:     List[str]					,      *__lowerCamelCase				:     int					,      **__lowerCamelCase				:     List[str]							)					->     Dict:
							return super().__call__(*__lowerCamelCase					,      **__lowerCamelCase							)












from ...configuration_utils import PretrainedConfig
from ...utils import logging


snake_case_ : int    		=       logging.get_logger(__name__)

snake_case_ : str    		=       {}







class __snake_case			(			a		):
   UpperCAmelCase__  :    str          =  '''llama'''
   UpperCAmelCase__  :    Dict          =  ['''past_key_values''']







   def __init__(      self		:						str					,						_snake_case		:						List[str]=32000					,						_snake_case		:						int=4096					,						_snake_case		:						List[str]=11008					,						_snake_case		:						Optional[int]=32					,						_snake_case		:						List[Any]=32					,						_snake_case		:						Tuple=None					,						_snake_case		:						int="silu"					,						_snake_case		:						List[Any]=2048					,						_snake_case		:						List[str]=0.0_2					,						_snake_case		:						Any=1e-6					,						_snake_case		:						List[str]=True					,						_snake_case		:						Optional[Any]=0					,						_snake_case		:						Dict=1					,						_snake_case		:						List[Any]=2					,						_snake_case		:						str=1					,						_snake_case		:						Union[str, Any]=False					,						_snake_case		:						str=None					,						**_snake_case		:						List[Any]					,						):

        """simple docstring"""


        UpperCAmelCase_          =							vocab_size
        UpperCAmelCase_          =							max_position_embeddings
        UpperCAmelCase_          =							hidden_size
        UpperCAmelCase_          =							intermediate_size
        UpperCAmelCase_          =							num_hidden_layers
        UpperCAmelCase_          =							num_attention_heads

        # for backward compatibility
        if num_key_value_heads is None:
             UpperCAmelCase_          =							num_attention_heads

        UpperCAmelCase_          =							num_key_value_heads
        UpperCAmelCase_          =							hidden_act
        UpperCAmelCase_          =							initializer_range
        UpperCAmelCase_          =							rms_norm_eps
        UpperCAmelCase_          =							pretraining_tp
        UpperCAmelCase_          =							use_cache
        UpperCAmelCase_          =							rope_scaling
        self._rope_scaling_validation()

        super().__init__(
            pad_token_id=_snake_case					,						bos_token_id=_snake_case					,						eos_token_id=_snake_case					,						tie_word_embeddings=_snake_case					,						**_snake_case					,						)







   def lowerCamelCase						(      self		:						Optional[int]):

        """simple docstring"""


        if self.rope_scaling is None:
             return

        if not isinstance(self.rope_scaling					,						_snake_case) or len(self.rope_scaling) != 2:
             raise ValueError(
                 '''`rope_scaling` must be a dictionary with with two fields, `name` and `factor`, '''
                 F"""got {self.rope_scaling}""")
        UpperCAmelCase_          =							self.rope_scaling.get('''type'''					,						_snake_case)
        UpperCAmelCase_          =							self.rope_scaling.get('''factor'''					,						_snake_case)
        if rope_scaling_type is None or rope_scaling_type not in ["linear", "dynamic"]:
             raise ValueError(
                 F"""`rope_scaling`'s name field must be one of ['linear', 'dynamic'], got {rope_scaling_type}""")
        if rope_scaling_factor is None or not isinstance(_snake_case					,						_snake_case) or rope_scaling_factor <= 1.0:
             raise ValueError(F"""`rope_scaling`'s factor field must be an float > 1, got {rope_scaling_factor}""")






