




from typing import List, Union

import numpy as np

from ..tokenization_utils import TruncationStrategy
from ..utils import add_end_docstrings, logging
from .base import PIPELINE_INIT_ARGS, ArgumentHandler, ChunkPipeline


SCREAMING_SNAKE_CASE:     List[Any]									= logging.get_logger(__name__)



class    lowercase_		(SCREAMING_SNAKE_CASE__ ):



						def 	__a					(			self	:   List[str]					,							snake_case__	:   Optional[int]  ):

								"""simple docstring"""

								if isinstance(snake_case__					,							snake_case__  ):
										SCREAMING_SNAKE_CASE_					      =							[label.strip() for label in labels.split(','  ) if label.strip()]
								return labels






						def __call__(			self	:   int					,							snake_case__	:   Tuple					,							snake_case__	:   Any					,							snake_case__	:   Any  ):

								"""simple docstring"""

								if len(snake_case__  ) == 0 or len(snake_case__  ) == 0:
										raise ValueError('You must include at least one label and at least one sequence.'  )
								if hypothesis_template.format(labels[0]  ) == hypothesis_template:
										raise ValueError(
										    (
										        'The provided hypothesis_template "{}" was not able to be formatted with the target labels. '
										        'Make sure the passed template includes formatting syntax such as {{}} where the label should go.'
										    ).format(snake_case__  )  )

								if isinstance(snake_case__					,							snake_case__  ):
										SCREAMING_SNAKE_CASE_					      =							[sequences]

								SCREAMING_SNAKE_CASE_					      =							[]
								for sequence in sequences:
										sequence_pairs.extend([[sequence, hypothesis_template.format(snake_case__  )] for label in labels]  )

								return sequence_pairs, sequences



@add_end_docstrings(SCREAMING_SNAKE_CASE__ )
class    lowercase_		(SCREAMING_SNAKE_CASE__ ):



						def __init__(			self	:   Optional[Any]					,							snake_case__	:   Optional[int]=ZeroShotClassificationArgumentHandler()					,							*snake_case__	:   List[Any]					,							**snake_case__	:   List[str]  ):

								"""simple docstring"""

								SCREAMING_SNAKE_CASE_					      =							args_parser
								super().__init__(*snake_case__					,							**snake_case__  )
								if self.entailment_id == -1:
										logger.warning(
										    'Failed to determine \'entailment\' label id from the label2id mapping in the model config. Setting to '
										    '-1. Define a descriptive label2id mapping in the model config to ensure correct outputs.'  )



						@property
						def 	__a					(			self	:   int  ):

								"""simple docstring"""

								for label, ind in self.model.config.labelaid.items():
										if label.lower().startswith('entail'  ):
												return ind
								return -1



						def 	__a					(			self	:   Optional[Any]					,							snake_case__	:   int					,							snake_case__	:   Any=True					,							snake_case__	:   Optional[Any]=True					,							snake_case__	:   str=TruncationStrategy.ONLY_FIRST					,							**snake_case__	:   Any  ):

								"""simple docstring"""

								SCREAMING_SNAKE_CASE_					      =							self.framework
								if self.tokenizer.pad_token is None:
										# Override for tokenizers not supporting padding
										logger.error(
										    'Tokenizer was not supporting padding necessary for zero-shot, attempting to use '
										    ' `pad_token=eos_token`'  )
										SCREAMING_SNAKE_CASE_					      =							self.tokenizer.eos_token
								try:
										SCREAMING_SNAKE_CASE_					      =							self.tokenizer(
										    snake_case__					,							add_special_tokens=snake_case__					,							return_tensors=snake_case__					,							padding=snake_case__					,							truncation=snake_case__					,							)
								except Exception as e:
										if "too short" in str(snake_case__  ):
												# tokenizers might yell that we want to truncate
												# to a value that is not even reached by the input.
												# In that case we don't want to truncate.
												# It seems there's not a really better way to catch that
												# exception.

												SCREAMING_SNAKE_CASE_					      =							self.tokenizer(
												    snake_case__					,							add_special_tokens=snake_case__					,							return_tensors=snake_case__					,							padding=snake_case__					,							truncation=TruncationStrategy.DO_NOT_TRUNCATE					,							)
										else:
												raise e

								return inputs



						def 	__a					(			self	:   Any					,							**snake_case__	:   int  ):

								"""simple docstring"""

								if kwargs.get('multi_class'					,							snake_case__  ) is not None:
										SCREAMING_SNAKE_CASE_					      =							kwargs['multi_class']
										logger.warning(
										    'The `multi_class` argument has been deprecated and renamed to `multi_label`. '
										    '`multi_class` will be removed in a future version of Transformers.'  )
								SCREAMING_SNAKE_CASE_					      =							{}
								if "candidate_labels" in kwargs:
										SCREAMING_SNAKE_CASE_					      =							self._args_parser._parse_labels(kwargs['candidate_labels']  )
								if "hypothesis_template" in kwargs:
										SCREAMING_SNAKE_CASE_					      =							kwargs['hypothesis_template']

								SCREAMING_SNAKE_CASE_					      =							{}
								if "multi_label" in kwargs:
										SCREAMING_SNAKE_CASE_					      =							kwargs['multi_label']
								return preprocess_params, {}, postprocess_params



						def __call__(			self	:   Tuple					,							snake_case__	:   Union[str, List[str]]					,							*snake_case__	:   Tuple					,							**snake_case__	:   Any					,							):

								"""simple docstring"""

								if len(snake_case__  ) == 0:
										pass
								elif len(snake_case__  ) == 1 and "candidate_labels" not in kwargs:
										SCREAMING_SNAKE_CASE_					      =							args[0]
								else:
										raise ValueError(f'''Unable to understand extra arguments {args}'''  )

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



						def 	__a					(			self	:   Optional[int]					,							snake_case__	:   str					,							snake_case__	:   Optional[int]=None					,							snake_case__	:   List[Any]="This example is {}."  ):

								"""simple docstring"""

								SCREAMING_SNAKE_CASE_      ,				SCREAMING_SNAKE_CASE_					      =							self._args_parser(snake_case__					,							snake_case__					,							snake_case__  )

								for i, (candidate_label, sequence_pair) in enumerate(zip(snake_case__					,							snake_case__  )  ):
										SCREAMING_SNAKE_CASE_					      =							self._parse_and_tokenize([sequence_pair]  )

										yield {
										    "candidate_label": candidate_label,
										    "sequence": sequences[0],
										    "is_last": i == len(snake_case__  ) - 1,
										    **model_input,
										}



						def 	__a					(			self	:   Optional[Any]					,							snake_case__	:   str  ):

								"""simple docstring"""

								SCREAMING_SNAKE_CASE_					      =							inputs['candidate_label']
								SCREAMING_SNAKE_CASE_					      =							inputs['sequence']
								SCREAMING_SNAKE_CASE_					      =							{k: inputs[k] for k in self.tokenizer.model_input_names}
								SCREAMING_SNAKE_CASE_					      =							self.model(**snake_case__  )

								SCREAMING_SNAKE_CASE_					      =							{
								    'candidate_label': candidate_label,
								    'sequence': sequence,
								    'is_last': inputs['is_last'],
								    **outputs,
								}
								return model_outputs






						def 	__a					(			self	:   List[Any]					,							snake_case__	:   Tuple					,							snake_case__	:   Tuple=False  ):

								"""simple docstring"""

								SCREAMING_SNAKE_CASE_					      =							[outputs['candidate_label'] for outputs in model_outputs]
								SCREAMING_SNAKE_CASE_					      =							[outputs['sequence'] for outputs in model_outputs]
								SCREAMING_SNAKE_CASE_					      =							np.concatenate([output['logits'].numpy() for output in model_outputs]  )
								SCREAMING_SNAKE_CASE_					      =							logits.shape[0]
								SCREAMING_SNAKE_CASE_					      =							len(snake_case__  )
								SCREAMING_SNAKE_CASE_					      =							N // n
								SCREAMING_SNAKE_CASE_					      =							logits.reshape((num_sequences, n, -1)  )

								if multi_label or len(snake_case__  ) == 1:
										# softmax over the entailment vs. contradiction dim for each label independently
										SCREAMING_SNAKE_CASE_					      =							self.entailment_id
										SCREAMING_SNAKE_CASE_					      =							-1 if entailment_id == 0 else 0
										SCREAMING_SNAKE_CASE_					      =							reshaped_outputs[..., [contradiction_id, entailment_id]]
										SCREAMING_SNAKE_CASE_					      =							np.exp(snake_case__  ) / np.exp(snake_case__  ).sum(-1					,							keepdims=snake_case__  )
										SCREAMING_SNAKE_CASE_					      =							scores[..., 1]
								else:
										# softmax the "entailment" logits over all candidate labels
										SCREAMING_SNAKE_CASE_					      =							reshaped_outputs[..., self.entailment_id]
										SCREAMING_SNAKE_CASE_					      =							np.exp(snake_case__  ) / np.exp(snake_case__  ).sum(-1					,							keepdims=snake_case__  )

								SCREAMING_SNAKE_CASE_					      =							list(reversed(scores[0].argsort()  )  )
								return {
								    "sequence": sequences[0],
								    "labels": [candidate_labels[i] for i in top_inds],
								    "scores": scores[0, top_inds].tolist(),
								}





from typing import Mapping

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


SCREAMING_SNAKE_CASE:     List[Any]									= logging.get_logger(__name__)

SCREAMING_SNAKE_CASE:     Optional[Any]									= {
    '''google/umt5-small''': '''https://huggingface.co/google/umt5-small/resolve/main/config.json''',
    # See all umt5 models at https://huggingface.co/models?filter=umt5
}



class    lowercase_		(SCREAMING_SNAKE_CASE__ ):
						lowerCAmelCase__			      ="umt5"
						lowerCAmelCase__			      =["past_key_values"]



						def __init__(			self	:   Optional[Any]					,							snake_case__	:   Union[str, Any]=25_01_12					,							snake_case__	:   Optional[int]=5_12					,							snake_case__	:   Optional[Any]=64					,							snake_case__	:   str=10_24					,							snake_case__	:   Dict=8					,							snake_case__	:   Union[str, Any]=None					,							snake_case__	:   List[Any]=6					,							snake_case__	:   List[Any]=32					,							snake_case__	:   List[Any]=1_28					,							snake_case__	:   Optional[int]=0.1					,							snake_case__	:   List[str]=1e-6					,							snake_case__	:   Optional[Any]=1.0					,							snake_case__	:   Optional[int]="gated-gelu"					,							snake_case__	:   Any=True					,							snake_case__	:   List[Any]=True					,							snake_case__	:   List[str]="T5Tokenizer"					,							snake_case__	:   List[str]=True					,							snake_case__	:   Tuple=0					,							snake_case__	:   Optional[int]=1					,							snake_case__	:   List[str]=0					,							**snake_case__	:   Any					,							):

								"""simple docstring"""

								super().__init__(
								    is_encoder_decoder=snake_case__					,							tokenizer_class=snake_case__					,							tie_word_embeddings=snake_case__					,							pad_token_id=snake_case__					,							eos_token_id=snake_case__					,							decoder_start_token_id=snake_case__					,							**snake_case__					,							)
								SCREAMING_SNAKE_CASE_					      =							vocab_size
								SCREAMING_SNAKE_CASE_					      =							d_model
								SCREAMING_SNAKE_CASE_					      =							d_kv
								SCREAMING_SNAKE_CASE_					      =							d_ff
								SCREAMING_SNAKE_CASE_					      =							num_layers
								SCREAMING_SNAKE_CASE_					      =							(
								    num_decoder_layers if num_decoder_layers is not None else self.num_layers
								)  # default = symmetry
								SCREAMING_SNAKE_CASE_					      =							num_heads
								SCREAMING_SNAKE_CASE_					      =							relative_attention_num_buckets
								SCREAMING_SNAKE_CASE_					      =							relative_attention_max_distance
								SCREAMING_SNAKE_CASE_					      =							dropout_rate
								SCREAMING_SNAKE_CASE_					      =							layer_norm_epsilon
								SCREAMING_SNAKE_CASE_					      =							initializer_factor
								SCREAMING_SNAKE_CASE_					      =							feed_forward_proj
								SCREAMING_SNAKE_CASE_					      =							use_cache

								SCREAMING_SNAKE_CASE_					      =							self.feed_forward_proj.split('-'  )
								SCREAMING_SNAKE_CASE_					      =							act_info[-1]
								SCREAMING_SNAKE_CASE_					      =							act_info[0] == 'gated'

								if len(snake_case__  ) > 1 and act_info[0] != "gated" or len(snake_case__  ) > 2:
										raise ValueError(
										    f'''`feed_forward_proj`: {feed_forward_proj} is not a valid activation function of the dense layer.'''
										    'Please make sure `feed_forward_proj` is of the format `gated-{ACT_FN}` or `{ACT_FN}`, e.g. '
										    '\'gated-gelu\' or \'relu\''  )

								if feed_forward_proj == "gated-gelu":
										SCREAMING_SNAKE_CASE_					      =							'gelu_new'



						@property
						def 	__a					(			self	:   Optional[Any]  ):

								"""simple docstring"""

								return self.d_model



						@property
						def 	__a					(			self	:   Union[str, Any]  ):

								"""simple docstring"""

								return self.num_heads






						@property
						def 	__a					(			self	:   Optional[Any]  ):

								"""simple docstring"""

								return self.num_layers



class    lowercase_		(SCREAMING_SNAKE_CASE__ ):



						@property
						# Copied from transformers.models.t5.configuration_t5.T5OnnxConfig.inputs
						def 	__a					(			self	:   int  ):

								"""simple docstring"""

								SCREAMING_SNAKE_CASE_					      =							{
								    'input_ids': {0: 'batch', 1: 'encoder_sequence'},
								    'attention_mask': {0: 'batch', 1: 'encoder_sequence'},
								}
								if self.use_past:
										SCREAMING_SNAKE_CASE_					      =							'past_encoder_sequence + sequence'
										SCREAMING_SNAKE_CASE_					      =							{0: 'batch'}
										SCREAMING_SNAKE_CASE_					      =							{0: 'batch', 1: 'past_decoder_sequence + sequence'}
								else:
										SCREAMING_SNAKE_CASE_					      =							{0: 'batch', 1: 'decoder_sequence'}
										SCREAMING_SNAKE_CASE_					      =							{0: 'batch', 1: 'decoder_sequence'}

								if self.use_past:
										self.fill_with_past_key_values_(snake_case__					,							direction='inputs'  )

								return common_inputs



						@property
						# Copied from transformers.models.t5.configuration_t5.T5OnnxConfig.default_onnx_opset
						def 	__a					(			self	:   Union[str, Any]  ):

								"""simple docstring"""

								return 13






						@property
						def 	__a					(			self	:   Dict  ):

								"""simple docstring"""

								return 5e-4