



'''simple docstring'''

import unittest

from transformers import AutoTokenizer, FalconConfig, is_torch_available
from transformers.testing_utils import require_torch, slow, torch_device

from ...generation.test_utils import GenerationTesterMixin
from ...test_configuration_common import ConfigTester
from ...test_modeling_common import ModelTesterMixin, ids_tensor, random_attention_mask
from ...test_pipeline_mixin import PipelineTesterMixin


if is_torch_available():
			import torch

			from transformers import (
			    FalconForCausalLM,
			    FalconForQuestionAnswering,
			    FalconForSequenceClassification,
			    FalconForTokenClassification,
			    FalconModel,
			)

class      __magic_name__						:







							def __init__(							self :	Tuple     ,_UpperCAmelCase :	int     ,_UpperCAmelCase :	Dict=3     ,_UpperCAmelCase :	Tuple=7     ,_UpperCAmelCase :	List[Any]=True     ,_UpperCAmelCase :	Optional[Any]=True     ,_UpperCAmelCase :	Tuple=False     ,_UpperCAmelCase :	Optional[int]=True     ,_UpperCAmelCase :	Dict=99     ,_UpperCAmelCase :	List[Any]=32     ,_UpperCAmelCase :	Optional[int]=5     ,_UpperCAmelCase :	Any=4     ,_UpperCAmelCase :	Union[str, Any]=37     ,_UpperCAmelCase :	List[Any]="gelu"     ,_UpperCAmelCase :	Tuple=0.1     ,_UpperCAmelCase :	Tuple=0.1     ,_UpperCAmelCase :	List[Any]=512     ,_UpperCAmelCase :	Optional[int]=16     ,_UpperCAmelCase :	Tuple=2     ,_UpperCAmelCase :	Optional[int]=0.02     ,_UpperCAmelCase :	Any=3     ,_UpperCAmelCase :	List[Any]=4     ,_UpperCAmelCase :	Dict=None     ,):
									_a					:				Tuple				=							parent
									_a					:				int				=							batch_size
									_a					:				List[str]				=							seq_length
									_a					:				int				=							is_training
									_a					:				Optional[int]				=							use_input_mask
									_a					:				Tuple				=							use_token_type_ids
									_a					:				Optional[Any]				=							use_labels
									_a					:				List[str]				=							vocab_size
									_a					:				Optional[Any]				=							hidden_size
									_a					:				Any				=							num_hidden_layers
									_a					:				List[str]				=							num_attention_heads
									_a					:				int				=							intermediate_size
									_a					:				Any				=							hidden_act
									_a					:				List[Any]				=							hidden_dropout_prob
									_a					:				Dict				=							attention_probs_dropout_prob
									_a					:				List[str]				=							max_position_embeddings
									_a					:				Union[str, Any]				=							type_vocab_size
									_a					:				Optional[Any]				=							type_sequence_label_size
									_a					:				Tuple				=							initializer_range
									_a					:				Optional[int]				=							num_labels
									_a					:				Tuple				=							num_choices
									_a					:				Optional[Any]				=							scope







							def 							__lowercase      (							self :	Any		):
									_a					:				Optional[int]				=							ids_tensor([self.batch_size, self.seq_length]     ,self.vocab_size		)

									_a					:				Optional[Any]				=							None
									if self.use_input_mask:
											_a					:				List[str]				=							random_attention_mask([self.batch_size, self.seq_length]		)

									_a					:				Union[str, Any]				=							None

									_a					:				int				=							None
									_a					:				List[str]				=							None
									_a					:				Union[str, Any]				=							None
									if self.use_labels:
											_a					:				Union[str, Any]				=							ids_tensor([self.batch_size]     ,self.type_sequence_label_size		)
											_a					:				Optional[int]				=							ids_tensor([self.batch_size, self.seq_length]     ,self.num_labels		)
											_a					:				str				=							ids_tensor([self.batch_size]     ,self.num_choices		)

									_a					:				Tuple				=							self.get_config()

									return config, input_ids, token_type_ids, input_mask, sequence_labels, token_labels, choice_labels







							def 							__lowercase      (							self :	List[str]		):
									return FalconConfig(
									    vocab_size=self.vocab_size     ,hidden_size=self.hidden_size     ,num_hidden_layers=self.num_hidden_layers     ,num_attention_heads=self.num_attention_heads     ,intermediate_size=self.intermediate_size     ,hidden_act=self.hidden_act     ,hidden_dropout_prob=self.hidden_dropout_prob     ,attention_probs_dropout_prob=self.attention_probs_dropout_prob     ,max_position_embeddings=self.max_position_embeddings     ,type_vocab_size=self.type_vocab_size     ,is_decoder=__A     ,initializer_range=self.initializer_range     ,pad_token_id=1     ,new_decoder_architecture=__A     ,)







							def 							__lowercase      (							self :	Any     ,_UpperCAmelCase :	Any     ,_UpperCAmelCase :	Optional[int]     ,_UpperCAmelCase :	Tuple     ,_UpperCAmelCase :	List[Any]     ,_UpperCAmelCase :	Optional[int]     ,_UpperCAmelCase :	Optional[int]     ,_UpperCAmelCase :	List[str]		):
									_a					:				List[str]				=							FalconModel(config=__A		)
									model.to(__A		)
									model.eval()
									_a					:				List[Any]				=							model(__A     ,attention_mask=__A		)
									_a					:				str				=							model(__A		)
									self.parent.assertEqual(result.last_hidden_state.shape     ,(self.batch_size, self.seq_length, self.hidden_size)		)







							def 							__lowercase      (							self :	Optional[int]     ,_UpperCAmelCase :	Optional[int]     ,_UpperCAmelCase :	Union[str, Any]     ,_UpperCAmelCase :	Dict     ,_UpperCAmelCase :	int     ,_UpperCAmelCase :	Any     ,_UpperCAmelCase :	int     ,_UpperCAmelCase :	Optional[int]     ,_UpperCAmelCase :	str     ,_UpperCAmelCase :	Union[str, Any]     ,):
									_a					:				List[str]				=							True
									_a					:				List[str]				=							FalconModel(__A		)
									model.to(__A		)
									model.eval()
									_a					:				int				=							model(
									    __A     ,attention_mask=__A     ,encoder_hidden_states=__A     ,encoder_attention_mask=__A     ,)
									_a					:				Optional[Any]				=							model(
									    __A     ,attention_mask=__A     ,encoder_hidden_states=__A     ,)
									_a					:				List[str]				=							model(__A     ,attention_mask=__A		)
									self.parent.assertEqual(result.last_hidden_state.shape     ,(self.batch_size, self.seq_length, self.hidden_size)		)







							def 							__lowercase      (							self :	str     ,_UpperCAmelCase :	Union[str, Any]     ,_UpperCAmelCase :	str     ,_UpperCAmelCase :	Optional[Any]     ,_UpperCAmelCase :	List[str]     ,_UpperCAmelCase :	Optional[int]     ,_UpperCAmelCase :	List[Any]     ,_UpperCAmelCase :	Union[str, Any]     ,_UpperCAmelCase :	Union[str, Any]     ,_UpperCAmelCase :	int     ,):
									_a					:				Optional[int]				=							FalconForCausalLM(config=__A		)
									model.to(__A		)
									model.eval()
									_a					:				Optional[Any]				=							model(__A     ,attention_mask=__A     ,labels=__A		)
									self.parent.assertEqual(result.logits.shape     ,(self.batch_size, self.seq_length, self.vocab_size)		)







							def 							__lowercase      (							self :	List[str]     ,_UpperCAmelCase :	Any     ,_UpperCAmelCase :	Optional[Any]     ,_UpperCAmelCase :	Union[str, Any]     ,_UpperCAmelCase :	Dict     ,_UpperCAmelCase :	Optional[Any]     ,_UpperCAmelCase :	Any     ,_UpperCAmelCase :	Tuple     ,_UpperCAmelCase :	str     ,_UpperCAmelCase :	str     ,):
									_a					:				Optional[Any]				=							True
									_a					:				str				=							True
									_a					:				List[str]				=							FalconForCausalLM(config=__A		)
									model.to(__A		)
									model.eval()

									# first forward pass
									_a					:				int				=							model(
									    __A     ,attention_mask=__A     ,encoder_hidden_states=__A     ,encoder_attention_mask=__A     ,use_cache=__A     ,)
									_a					:				Optional[Any]				=							outputs.past_key_values

									# create hypothetical multiple next token and extent to next_input_ids
									_a					:				Dict				=							ids_tensor((self.batch_size, 3)     ,config.vocab_size		)
									_a					:				List[str]				=							ids_tensor((self.batch_size, 3)     ,vocab_size=2		)

									# append to next input_ids and
									_a					:				Tuple				=							torch.cat([input_ids, next_tokens]     ,dim=-1		)
									_a					:				Optional[int]				=							torch.cat([input_mask, next_mask]     ,dim=-1		)

									_a					:				int				=							model(
									    __A     ,attention_mask=__A     ,encoder_hidden_states=__A     ,encoder_attention_mask=__A     ,output_hidden_states=__A     ,)["""hidden_states"""][0]
									_a					:				Dict				=							model(
									    __A     ,attention_mask=__A     ,encoder_hidden_states=__A     ,encoder_attention_mask=__A     ,past_key_values=__A     ,output_hidden_states=__A     ,)["""hidden_states"""][0]

									# select random slice
									_a					:				List[Any]				=							ids_tensor((1,)     ,output_from_past.shape[-1]		).item()
									_a					:				Optional[Any]				=							output_from_no_past[:, -3:, random_slice_idx].detach()
									_a					:				List[str]				=							output_from_past[:, :, random_slice_idx].detach()

									self.parent.assertTrue(output_from_past_slice.shape[1] == next_tokens.shape[1]		)

									# test that outputs are equal for slice
									self.parent.assertTrue(torch.allclose(__A     ,__A     ,atol=1E-3		)		)







							def 							__lowercase      (							self :	Any		):
									_a					:				Optional[int]				=							self.prepare_config_and_inputs()
									(
									    _a	
									)					:				Optional[Any]				=							config_and_inputs
									_a					:				str				=							{"""input_ids""": input_ids, """attention_mask""": input_mask}
									return config, inputs_dict

@require_torch
class      __magic_name__						(   A__      ,						A__      ,						A__      ,						unittest.TestCase       ):
							lowerCAmelCase	:   Optional[int]           = (
							    (
							        FalconModel,
							        FalconForCausalLM,
							        FalconForSequenceClassification,
							        FalconForTokenClassification,
							        FalconForQuestionAnswering,
							    )
							    if is_torch_available()
							    else ()
							)
							lowerCAmelCase	:   str           = (FalconForCausalLM,) if is_torch_available() else ()
							lowerCAmelCase	:   Union[str, Any]           = (
							    {
							        "feature-extraction": FalconModel,
							        "text-classification": FalconForSequenceClassification,
							        "text-generation": FalconForCausalLM,
							        "question-answering": FalconForQuestionAnswering,
							        "token-classification": FalconForTokenClassification,
							        "zero-shot": FalconForSequenceClassification,
							    }
							    if is_torch_available()
							    else {}
							)
							lowerCAmelCase	:   int           = False
							lowerCAmelCase	:   int           = False







							def 							__lowercase      (							self :	int		):
									_a					:				List[Any]				=							FalconModelTester(self		)
									_a					:				str				=							ConfigTester(self     ,config_class=__A     ,hidden_size=37		)







							def 							__lowercase      (							self :	Optional[int]		):
									self.config_tester.run_common_tests()







							def 							__lowercase      (							self :	Tuple		):
									_a					:				Optional[int]				=							self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_model(*__A		)







							def 							__lowercase      (							self :	int		):
									_a					:				Dict				=							self.model_tester.prepare_config_and_inputs()
									for alibi in [True, False]:
											_a					:				int				=							alibi
											self.model_tester.create_and_check_model(__A     ,*__A		)







							def 							__lowercase      (							self :	Optional[int]		):
									_a					:				Tuple				=							self.model_tester.prepare_config_and_inputs_for_common()
									_a					:				Optional[Any]				=							3
									_a					:				Any				=							input_dict["""input_ids"""]
									_a					:				int				=							input_ids.ne(1		).to(__A		)
									_a					:				List[Any]				=							ids_tensor([self.model_tester.batch_size]     ,self.model_tester.type_sequence_label_size		)
									_a					:				Dict				=							FalconForSequenceClassification(__A		)
									model.to(__A		)
									model.eval()
									_a					:				Any				=							model(__A     ,attention_mask=__A     ,labels=__A		)
									self.assertEqual(result.logits.shape     ,(self.model_tester.batch_size, self.model_tester.num_labels)		)







							def 							__lowercase      (							self :	Optional[Any]		):
									_a					:				Any				=							self.model_tester.prepare_config_and_inputs_for_common()
									_a					:				List[str]				=							3
									_a					:				Union[str, Any]				=							"""single_label_classification"""
									_a					:				int				=							input_dict["""input_ids"""]
									_a					:				Tuple				=							input_ids.ne(1		).to(__A		)
									_a					:				int				=							ids_tensor([self.model_tester.batch_size]     ,self.model_tester.type_sequence_label_size		)
									_a					:				Any				=							FalconForSequenceClassification(__A		)
									model.to(__A		)
									model.eval()
									_a					:				int				=							model(__A     ,attention_mask=__A     ,labels=__A		)
									self.assertEqual(result.logits.shape     ,(self.model_tester.batch_size, self.model_tester.num_labels)		)







							def 							__lowercase      (							self :	int		):
									_a					:				Optional[Any]				=							self.model_tester.prepare_config_and_inputs_for_common()
									_a					:				Optional[Any]				=							input_dict["""input_ids"""]
									_a					:				Any				=							FalconForCausalLM(__A		)
									model.to(__A		)
									model.eval()
									_a					:				Optional[int]				=							model(__A     ,use_cache=__A		)
									_a					:				Union[str, Any]				=							input_ids.shape[0]
									_a					:				Tuple				=							model._convert_to_rw_cache(result.past_key_values		)
									_a					:				Optional[int]				=							model._convert_cache_to_standard_format(__A     ,__A		)
									for layer in range(len(__A		)		):
											for tensor_idx in range(2		):
													self.assertTrue(rw_cache[layer][tensor_idx].ndim == 3		)
													self.assertTrue(result.past_key_values[layer][tensor_idx].ndim == 4		)
													self.assertTrue(
													    torch.all(result.past_key_values[layer][tensor_idx] == standard_cache[layer][tensor_idx]		)		)







							def 							__lowercase      (							self :	Any		):
									_a					:				Optional[Any]				=							self.model_tester.prepare_config_and_inputs_for_common()
									_a					:				Optional[Any]				=							3
									_a					:				Optional[int]				=							"""multi_label_classification"""
									_a					:				List[str]				=							input_dict["""input_ids"""]
									_a					:				Optional[int]				=							input_ids.ne(1		).to(__A		)
									_a					:				Optional[int]				=							ids_tensor(
									    [self.model_tester.batch_size, config.num_labels]     ,self.model_tester.type_sequence_label_size		).to(torch.float		)
									_a					:				Tuple				=							FalconForSequenceClassification(__A		)
									model.to(__A		)
									model.eval()
									_a					:				Optional[Any]				=							model(__A     ,attention_mask=__A     ,labels=__A		)
									self.assertEqual(result.logits.shape     ,(self.model_tester.batch_size, self.model_tester.num_labels)		)







							def 							__lowercase      (							self :	Tuple		):
									# Falcon can have different numbers of KV-heads than the number of query heads, so we need
									# to override this test to use the right head counts.
									for model_class in self.all_generative_model_classes:
											_a					:				Union[str, Any]				=							self.model_tester.prepare_config_and_inputs_for_common()

											# If it doesn't support cache, pass the test
											if not hasattr(__A     ,'use_cache'		):
													return

											_a					:				Optional[Any]				=							model_class(__A		).to(__A		)
											if "use_cache" not in inputs:
													_a					:				Any				=							True
											_a					:				List[str]				=							model(**__A		)

											# If "past_key_values" is not returned, pass the test (e.g. RWKV uses a different cache name and format)
											if "past_key_values" not in outputs:
													return

											_a					:				int				=							(
											    getattr(__A     ,'decoder_layers'     ,__A		)
											    or getattr(__A     ,'num_decoder_layers'     ,__A		)
											    or config.num_hidden_layers
											)
											_a					:				Optional[int]				=							getattr(__A     ,'num_kv_heads'     ,config.num_attention_heads		)
											_a					:				int				=							getattr(__A     ,'d_model'     ,config.hidden_size		)
											_a					:				Tuple				=							embed_dim // num_attention_heads

											_a					:				List[str]				=							outputs["""past_key_values"""]
											self.assertEqual(len(__A		)     ,__A		)

											_a					:				List[str]				=							inputs["""input_ids"""].shape
											for i in range(__A		):
													if config.new_decoder_architecture:
															_a					:				Tuple				=							config.num_attention_heads
													elif config.multi_query:
															_a					:				List[Any]				=							1
													self.assertEqual(len(past_kv[0]		)     ,2		)  # K V for the decoder = 2
													self.assertEqual(
													    past_kv[i][0].shape     ,(batch_size, num_attention_heads, seq_length, per_head_embed_dim)		)
													self.assertEqual(
													    past_kv[i][1].shape     ,(batch_size, num_attention_heads, seq_length, per_head_embed_dim)		)

@require_torch
class      __magic_name__						(   unittest.TestCase       ):







							@slow
							def 							__lowercase      (							self :	int		):
									_a					:				Tuple				=							AutoTokenizer.from_pretrained('Rocketknight1/falcon-rw-1b'		)
									_a					:				int				=							FalconForCausalLM.from_pretrained('Rocketknight1/falcon-rw-1b'		)
									model.eval()
									model.to(__A		)
									_a					:				List[Any]				=							tokenizer('My favorite food is'     ,return_tensors='pt'		).to(__A		)

									_a					:				List[str]				=							(
									    """My favorite food is pizza. I love it so much that I have a pizza party every year for my birthday."""
									)

									_a					:				int				=							model.generate(**__A     ,do_sample=__A     ,max_new_tokens=19		)
									_a					:				List[str]				=							tokenizer.batch_decode(__A		)[0]

									self.assertEqual(__A     ,__A		)







							@slow
							def 							__lowercase      (							self :	int		):
									# The big models are way too big for the CI, so we use tiny random models that resemble their
									# architectures but with much smaller and fewer layers
									for repo in ["Rocketknight1/tiny-random-falcon-7b", "Rocketknight1/tiny-random-falcon-40b"]:
											_a					:				Union[str, Any]				=							AutoTokenizer.from_pretrained(__A		)
											_a					:				str				=							FalconForCausalLM.from_pretrained(__A		)
											model.eval()
											model.to(__A		)
											_a					:				List[str]				=							tokenizer('My favorite food is'     ,return_tensors='pt'		).to(__A		)

											# We just test that these run without errors - the models are randomly initialized
											# and so the actual text outputs will be garbage
											model.generate(**__A     ,do_sample=__A     ,max_new_tokens=4		)
											model.generate(**__A     ,do_sample=__A     ,max_new_tokens=4		)
											model.generate(**__A     ,num_beams=2     ,max_new_tokens=4		)







							@slow
							def 							__lowercase      (							self :	Dict		):
									# The big models are way too big for the CI, so we use tiny random models that resemble their
									# architectures but with much smaller and fewer layers
									with torch.no_grad():
											for repo in [
											    "Rocketknight1/falcon-rw-1b",
											    "Rocketknight1/tiny-random-falcon-7b",
											    "Rocketknight1/tiny-random-falcon-40b",
											]:
													_a					:				Optional[int]				=							AutoTokenizer.from_pretrained(__A		)
													_a					:				Dict				=							FalconForCausalLM.from_pretrained(__A		)
													model.eval()
													model.to(device=__A		)
													_a					:				Tuple				=							tokenizer('My favorite food is'     ,return_tensors='pt'		).to(__A		)

													# Test results are the same with and without cache
													_a					:				List[Any]				=							model.generate(**__A     ,do_sample=__A     ,max_new_tokens=20     ,use_cache=__A		)
													_a					:				Dict				=							model.generate(**__A     ,do_sample=__A     ,max_new_tokens=20     ,use_cache=__A		)
													self.assertTrue((outputs_cache - outputs_no_cache).sum().item() == 0		)









'''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

__lowerCAmelCase						      =       logging.get_logger(__name__)

class      __magic_name__						(   enum.Enum       ):
							lowerCAmelCase	:   Union[str, Any]           = 0
							lowerCAmelCase	:   Dict           = 1

@add_end_docstrings(_UpperCamelCase       )
class      __magic_name__						(   _UpperCamelCase       ):
							lowerCAmelCase	:   int           = 'generated'







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

									_a					:				List[str]				=							generate_kwargs

									_a					:				Optional[Any]				=							{}
									if return_tensors is not None and return_type is None:
											_a					:				Optional[int]				=							ReturnType.TENSORS if return_tensors else ReturnType.TEXT
									if return_type is not None:
											_a					:				List[Any]				=							return_type

									if clean_up_tokenization_spaces is not None:
											_a					:				Optional[int]				=							clean_up_tokenization_spaces

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

									return preprocess_params, forward_params, postprocess_params







							def 							__lowercase      (							self :	Any     ,_UpperCAmelCase :	int     ,_UpperCAmelCase :	int     ,_UpperCAmelCase :	int		):
									return True







							def 							__lowercase      (							self :	str     ,*_UpperCAmelCase :	Any     ,_UpperCAmelCase :	Union[str, Any]		):
									_a					:				Optional[Any]				=							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'		)
											_a					:				Optional[Any]				=							([prefix + arg for arg in args[0]],)
											_a					:				Any				=							True

									elif isinstance(args[0]     ,_UpperCAmelCase		):
											_a					:				List[str]				=							(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(*_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 :	Optional[int]     ,*_UpperCAmelCase :	List[Any]     ,**_UpperCAmelCase :	Any		):
									_a					:				Tuple				=							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 							__lowercase      (							self :	Tuple     ,_UpperCAmelCase :	Union[str, Any]     ,_UpperCAmelCase :	List[str]=TruncationStrategy.DO_NOT_TRUNCATE     ,**_UpperCAmelCase :	int		):
									_a					:				Dict				=							self._parse_and_tokenize(_UpperCAmelCase     ,truncation=_UpperCAmelCase     ,**_UpperCAmelCase		)
									return inputs







							def 							__lowercase      (							self :	Any     ,_UpperCAmelCase :	str     ,**_UpperCAmelCase :	Tuple		):
									if self.framework == "pt":
											_a	, _a					:				int				=							model_inputs['input_ids'].shape
									elif self.framework == "tf":
											_a	, _a					:				Dict				=							tf.shape(model_inputs['input_ids']		).numpy()

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







							def 							__lowercase      (							self :	Dict     ,_UpperCAmelCase :	Tuple     ,_UpperCAmelCase :	Any=ReturnType.TEXT     ,_UpperCAmelCase :	Dict=False		):
									_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					:				str				=							{
													    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(_UpperCamelCase       )
class      __magic_name__						(   _UpperCamelCase       ):
							lowerCAmelCase	:   Any           = 'summary'







							def __call__(							self :	Any     ,*_UpperCAmelCase :	Union[str, Any]     ,**_UpperCAmelCase :	List[str]		):
									return super().__call__(*_UpperCAmelCase     ,**_UpperCAmelCase		)







							def 							__lowercase      (							self :	Optional[Any]     ,_UpperCAmelCase :	int     ,_UpperCAmelCase :	int     ,_UpperCAmelCase :	int		):
									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      __magic_name__						(   _UpperCamelCase       ):
							lowerCAmelCase	:   List[Any]           = 'translation'







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







							def __call__(							self :	Optional[Any]     ,*_UpperCAmelCase :	Tuple     ,**_UpperCAmelCase :	Optional[int]		):
									return super().__call__(*_UpperCAmelCase     ,**_UpperCAmelCase		)




