





"""simple docstring"""






import argparse

import torch

from transformers import (
    SpeechTaConfig,
    SpeechTaFeatureExtractor,
    SpeechTaForSpeechToSpeech,
    SpeechTaForSpeechToText,
    SpeechTaForTextToSpeech,
    SpeechTaProcessor,
    SpeechTaTokenizer,
    logging,
)
from transformers.tokenization_utils import AddedToken


logging.set_verbosity_info()
__UpperCAmelCase						=			logging.get_logger('''transformers.models.speecht5''')

__UpperCAmelCase						=			{
    '''speech_encoder_prenet.layer_norm''': '''speecht5.encoder.prenet.feature_projection.layer_norm''',
    '''speech_encoder_prenet.post_extract_proj''': '''speecht5.encoder.prenet.feature_projection.projection''',
    '''speech_encoder_prenet.pos_conv.0''': '''speecht5.encoder.prenet.pos_conv_embed.conv''',
    '''speech_encoder_prenet.mask_emb''': '''speecht5.encoder.prenet.masked_spec_embed''',
}
__UpperCAmelCase						=			{
    '''text_encoder_prenet.encoder_prenet.0''': '''speecht5.encoder.prenet.embed_tokens''',
    '''text_encoder_prenet.encoder_prenet.1.alpha''': '''speecht5.encoder.prenet.encode_positions.alpha''',
}
__UpperCAmelCase						=			{
    '''speech_decoder_prenet.decoder_prenet.0.0.prenet.0.0''': '''speecht5.decoder.prenet.layers.0''',
    '''speech_decoder_prenet.decoder_prenet.0.0.prenet.1.0''': '''speecht5.decoder.prenet.layers.1''',
    '''speech_decoder_prenet.decoder_prenet.0.1''': '''speecht5.decoder.prenet.final_layer''',
    '''speech_decoder_prenet.decoder_prenet.1.alpha''': '''speecht5.decoder.prenet.encode_positions.alpha''',
    '''speech_decoder_prenet.spkembs_layer.0''': '''speecht5.decoder.prenet.speaker_embeds_layer''',
}
__UpperCAmelCase						=			{
    '''speech_decoder_postnet.feat_out''': '''speech_decoder_postnet.feat_out''',
    '''speech_decoder_postnet.prob_out''': '''speech_decoder_postnet.prob_out''',
    '''speech_decoder_postnet.postnet.postnet.0.0''': '''speech_decoder_postnet.layers.0.conv''',
    '''speech_decoder_postnet.postnet.postnet.0.1''': '''speech_decoder_postnet.layers.0.batch_norm''',
    '''speech_decoder_postnet.postnet.postnet.1.0''': '''speech_decoder_postnet.layers.1.conv''',
    '''speech_decoder_postnet.postnet.postnet.1.1''': '''speech_decoder_postnet.layers.1.batch_norm''',
    '''speech_decoder_postnet.postnet.postnet.2.0''': '''speech_decoder_postnet.layers.2.conv''',
    '''speech_decoder_postnet.postnet.postnet.2.1''': '''speech_decoder_postnet.layers.2.batch_norm''',
    '''speech_decoder_postnet.postnet.postnet.3.0''': '''speech_decoder_postnet.layers.3.conv''',
    '''speech_decoder_postnet.postnet.postnet.3.1''': '''speech_decoder_postnet.layers.3.batch_norm''',
    '''speech_decoder_postnet.postnet.postnet.4.0''': '''speech_decoder_postnet.layers.4.conv''',
    '''speech_decoder_postnet.postnet.postnet.4.1''': '''speech_decoder_postnet.layers.4.batch_norm''',
}
__UpperCAmelCase						=			{
    '''text_decoder_prenet.embed_tokens''': '''speecht5.decoder.prenet.embed_tokens''',
}
__UpperCAmelCase						=			{
    '''text_decoder_postnet.output_projection''': '''text_decoder_postnet.lm_head''',
}
__UpperCAmelCase						=			{
    '''encoder.layers.*.self_attn.k_proj''': '''speecht5.encoder.wrapped_encoder.layers.*.attention.k_proj''',
    '''encoder.layers.*.self_attn.v_proj''': '''speecht5.encoder.wrapped_encoder.layers.*.attention.v_proj''',
    '''encoder.layers.*.self_attn.q_proj''': '''speecht5.encoder.wrapped_encoder.layers.*.attention.q_proj''',
    '''encoder.layers.*.self_attn.out_proj''': '''speecht5.encoder.wrapped_encoder.layers.*.attention.out_proj''',
    '''encoder.layers.*.self_attn_layer_norm''': '''speecht5.encoder.wrapped_encoder.layers.*.layer_norm''',
    '''encoder.layers.*.fc1''': '''speecht5.encoder.wrapped_encoder.layers.*.feed_forward.intermediate_dense''',
    '''encoder.layers.*.fc2''': '''speecht5.encoder.wrapped_encoder.layers.*.feed_forward.output_dense''',
    '''encoder.layers.*.final_layer_norm''': '''speecht5.encoder.wrapped_encoder.layers.*.final_layer_norm''',
    '''encoder.layer_norm''': '''speecht5.encoder.wrapped_encoder.layer_norm''',
    '''encoder.pos_emb.pe_k''': '''speecht5.encoder.wrapped_encoder.embed_positions.pe_k''',
}
__UpperCAmelCase						=			{
    '''decoder.layers.*.self_attn.k_proj''': '''speecht5.decoder.wrapped_decoder.layers.*.self_attn.k_proj''',
    '''decoder.layers.*.self_attn.v_proj''': '''speecht5.decoder.wrapped_decoder.layers.*.self_attn.v_proj''',
    '''decoder.layers.*.self_attn.q_proj''': '''speecht5.decoder.wrapped_decoder.layers.*.self_attn.q_proj''',
    '''decoder.layers.*.self_attn.out_proj''': '''speecht5.decoder.wrapped_decoder.layers.*.self_attn.out_proj''',
    '''decoder.layers.*.self_attn_layer_norm''': '''speecht5.decoder.wrapped_decoder.layers.*.self_attn_layer_norm''',
    '''decoder.layers.*.encoder_attn.k_proj''': '''speecht5.decoder.wrapped_decoder.layers.*.encoder_attn.k_proj''',
    '''decoder.layers.*.encoder_attn.v_proj''': '''speecht5.decoder.wrapped_decoder.layers.*.encoder_attn.v_proj''',
    '''decoder.layers.*.encoder_attn.q_proj''': '''speecht5.decoder.wrapped_decoder.layers.*.encoder_attn.q_proj''',
    '''decoder.layers.*.encoder_attn.out_proj''': '''speecht5.decoder.wrapped_decoder.layers.*.encoder_attn.out_proj''',
    '''decoder.layers.*.encoder_attn_layer_norm''': '''speecht5.decoder.wrapped_decoder.layers.*.encoder_attn_layer_norm''',
    '''decoder.layers.*.fc1''': '''speecht5.decoder.wrapped_decoder.layers.*.feed_forward.intermediate_dense''',
    '''decoder.layers.*.fc2''': '''speecht5.decoder.wrapped_decoder.layers.*.feed_forward.output_dense''',
    '''decoder.layers.*.final_layer_norm''': '''speecht5.decoder.wrapped_decoder.layers.*.final_layer_norm''',
}
__UpperCAmelCase						=			{
    **MAPPING_SPEECH_ENCODER_PRENET,
    **MAPPING_ENCODER,
    **MAPPING_DECODER,
    **MAPPING_TEXT_DECODER_PRENET,
    **MAPPING_TEXT_DECODER_POSTNET,
}
__UpperCAmelCase						=			{
    **MAPPING_TEXT_ENCODER_PRENET,
    **MAPPING_ENCODER,
    **MAPPING_DECODER,
    **MAPPING_SPEECH_DECODER_PRENET,
    **MAPPING_SPEECH_DECODER_POSTNET,
}
__UpperCAmelCase						=			{
    **MAPPING_SPEECH_ENCODER_PRENET,
    **MAPPING_ENCODER,
    **MAPPING_DECODER,
    **MAPPING_SPEECH_DECODER_PRENET,
    **MAPPING_SPEECH_DECODER_POSTNET,
}
__UpperCAmelCase						=			[]
__UpperCAmelCase						=			[
    '''encoder.version''',
    '''encoder.layers.*.norm_k.weight''',
    '''encoder.layers.*.norm_k.bias''',
    '''decoder.version''',
    '''decoder.layers.*.norm_k.weight''',
    '''decoder.layers.*.norm_k.bias''',
    '''decoder.pos_emb.pe_k''',
    '''speech_encoder_prenet.embed_positions._float_tensor''',
    '''text_decoder_prenet.embed_positions._float_tensor''',
]
__UpperCAmelCase						=			IGNORE_KEYS + [
    '''encoder.proj''',
    '''text_encoder_prenet.*''',
    '''speech_decoder_prenet.*''',
    '''speech_decoder_postnet.*''',
]
__UpperCAmelCase						=			IGNORE_KEYS + [
    '''encoder.proj''',
    '''speech_encoder_prenet.*''',
    '''text_decoder_prenet.*''',
    '''text_decoder_postnet.*''',
]
__UpperCAmelCase						=			IGNORE_KEYS + [
    '''encoder.proj''',
    '''text_encoder_prenet.*''',
    '''text_decoder_prenet.*''',
    '''text_decoder_postnet.*''',
]






def      lowercase__ (					lowerCAmelCase__   :			Tuple							,			lowerCAmelCase__   :			Any							,			lowerCAmelCase__   :			Union[str, Any]							,			lowerCAmelCase__   :			List[str]							,			lowerCAmelCase__   :			Tuple			)    ->			Tuple:



			'''simple docstring'''
			for attribute in key.split("."			):
						a__  :	int		 =			getattr(lowerCAmelCase__							,			lowerCAmelCase__			)

			if weight_type is not None:
						a__  :	Dict		 =			getattr(lowerCAmelCase__							,			lowerCAmelCase__			).shape
			else:
						a__  :	Dict		 =			hf_pointer.shape

			if hf_shape != value.shape:
						raise ValueError(
						    F"Shape of hf {key + '.' + weight_type if weight_type is not None else ''} is {hf_shape}, but should be"
						    F" {value.shape} for {full_name}"			)

			if weight_type == "weight":
						a__  :	Union[str, Any]		 =			value
			elif weight_type == "weight_g":
						a__  :	int		 =			value
			elif weight_type == "weight_v":
						a__  :	List[str]		 =			value
			elif weight_type == "bias":
						a__  :	int		 =			value
			elif weight_type == "running_mean":
						a__  :	Tuple		 =			value
			elif weight_type == "running_var":
						a__  :	List[str]		 =			value
			elif weight_type == "num_batches_tracked":
						a__  :	Optional[Any]		 =			value
			else:
						a__  :	Tuple		 =			value

			logger.info(F"{key + ('.' + weight_type if weight_type is not None else '')} was initialized from {full_name}."			)






def      lowercase__ (					lowerCAmelCase__   :			Union[str, Any]							,			lowerCAmelCase__   :			Any			)    ->			List[str]:



			'''simple docstring'''
			for key in ignore_keys:
						if key.endswith(".*"			):
									if name.startswith(key[:-1]			):
												return True
						elif ".*." in key:
									a__ ,	a__  :	int		 =			key.split(".*."			)
									if prefix in name and suffix in name:
												return True
						elif key in name:
									return True
			return False






def      lowercase__ (					lowerCAmelCase__   :			Union[str, Any]							,			lowerCAmelCase__   :			str							,			lowerCAmelCase__   :			Union[str, Any]			)    ->			Dict:



			'''simple docstring'''
			a__  :	Optional[int]		 =			[]

			if task == "s2t":
						a__  :	int		 =			hf_model.speechta.encoder.prenet.feature_encoder
						a__  :	List[Any]		 =			MAPPING_S2T
						a__  :	Tuple		 =			IGNORE_KEYS_S2T
			elif task == "t2s":
						a__  :	str		 =			None
						a__  :	List[str]		 =			MAPPING_T2S
						a__  :	str		 =			IGNORE_KEYS_T2S
			elif task == "s2s":
						a__  :	int		 =			hf_model.speechta.encoder.prenet.feature_encoder
						a__  :	Optional[int]		 =			MAPPING_S2S
						a__  :	List[str]		 =			IGNORE_KEYS_S2S
			else:
						raise ValueError(F"Unsupported task: {task}"			)

			for name, value in fairseq_dict.items():
						if should_ignore(lowerCAmelCase__							,			lowerCAmelCase__			):
									logger.info(F"{name} was ignored"			)
									continue

						a__  :	Union[str, Any]		 =			False
						if "conv_layers" in name:
									load_conv_layer(
									    lowerCAmelCase__							,			lowerCAmelCase__							,			lowerCAmelCase__							,			lowerCAmelCase__							,			hf_model.config.feat_extract_norm == "group"							,			)
									a__  :	List[Any]		 =			True
						else:
									for key, mapped_key in MAPPING.items():
												# mapped_key = "speecht5." + mapped_key if mapped_key not in TOP_LEVEL_KEYS else mapped_key

												if "*" in key:
															a__ ,	a__  :	Union[str, Any]		 =			key.split(".*."			)
															if prefix in name and suffix in name:
																		a__  :	Optional[Any]		 =			suffix

                # if key in name or key.split("w2v_model.")[-1] == name.split(".")[0]:
												if key in name:
															a__  :	Tuple		 =			True
															if "*" in mapped_key:
																		a__  :	Optional[Any]		 =			name.split(lowerCAmelCase__			)[0].split("."			)[-2]
																		a__  :	Optional[Any]		 =			mapped_key.replace("*"							,			lowerCAmelCase__			)
															if "weight_g" in name:
																		a__  :	List[str]		 =			"weight_g"
															elif "weight_v" in name:
																		a__  :	List[Any]		 =			"weight_v"
															elif "bias" in name:
																		a__  :	Any		 =			"bias"
															elif "weight" in name:
																		a__  :	Any		 =			"weight"
															elif "running_mean" in name:
																		a__  :	Optional[Any]		 =			"running_mean"
															elif "running_var" in name:
																		a__  :	Optional[int]		 =			"running_var"
															elif "num_batches_tracked" in name:
																		a__  :	Optional[int]		 =			"num_batches_tracked"
															else:
																		a__  :	int		 =			None
															set_recursively(lowerCAmelCase__							,			lowerCAmelCase__							,			lowerCAmelCase__							,			lowerCAmelCase__							,			lowerCAmelCase__			)
												continue
						if not is_used:
									unused_weights.append(lowerCAmelCase__			)

			logger.warning(F"Unused weights: {unused_weights}"			)






def      lowercase__ (					lowerCAmelCase__   :			Optional[int]							,			lowerCAmelCase__   :			Any							,			lowerCAmelCase__   :			List[str]							,			lowerCAmelCase__   :			Optional[int]							,			lowerCAmelCase__   :			Union[str, Any]			)    ->			Optional[Any]:



			'''simple docstring'''
			a__  :	Optional[Any]		 =			full_name.split("conv_layers."			)[-1]
			a__  :	List[Any]		 =			name.split("."			)
			a__  :	Dict		 =			int(items[0]			)
			a__  :	int		 =			int(items[1]			)

			if type_id == 0:
						if "bias" in name:
									if value.shape != feature_extractor.conv_layers[layer_id].conv.bias.data.shape:
												raise ValueError(
												    F"{full_name} has size {value.shape}, but"
												    F" {feature_extractor.conv_layers[layer_id].conv.bias.data.shape} was found."			)
									a__  :	List[Any]		 =			value
									logger.info(F"Feat extract conv layer {layer_id} was initialized from {full_name}."			)
						elif "weight" in name:
									if value.shape != feature_extractor.conv_layers[layer_id].conv.weight.data.shape:
												raise ValueError(
												    F"{full_name} has size {value.shape}, but"
												    F" {feature_extractor.conv_layers[layer_id].conv.weight.data.shape} was found."			)
									a__  :	Union[str, Any]		 =			value
									logger.info(F"Feat extract conv layer {layer_id} was initialized from {full_name}."			)
			elif (type_id == 2 and not use_group_norm) or (type_id == 2 and layer_id == 0 and use_group_norm):
						if "bias" in name:
									if value.shape != feature_extractor.conv_layers[layer_id].layer_norm.bias.data.shape:
												raise ValueError(
												    F"{full_name} has size {value.shape}, but"
												    F" {feature_extractor.conv_layers[layer_id].layer_norm.bias.data.shape} was found."			)
									a__  :	Optional[int]		 =			value
									logger.info(F"Feat extract layer norm weight of layer {layer_id} was initialized from {full_name}."			)
						elif "weight" in name:
									if value.shape != feature_extractor.conv_layers[layer_id].layer_norm.weight.data.shape:
												raise ValueError(
												    F"{full_name} has size {value.shape}, but"
												    F" {feature_extractor.conv_layers[layer_id].layer_norm.weight.data.shape} was found."			)
									a__  :	List[Any]		 =			value
									logger.info(F"Feat extract layer norm weight of layer {layer_id} was initialized from {full_name}."			)
			else:
						unused_weights.append(lowerCAmelCase__			)






@torch.no_grad()
def      lowercase__ (					lowerCAmelCase__   :			Union[str, Any]							,			lowerCAmelCase__   :			Union[str, Any]							,			lowerCAmelCase__   :			List[str]							,			lowerCAmelCase__   :			str=None							,			lowerCAmelCase__   :			Optional[Any]=None							,			lowerCAmelCase__   :			Dict=None							,			)    ->			Tuple:



			'''simple docstring'''
			if config_path is not None:
						a__  :	Any		 =			SpeechTaConfig.from_pretrained(lowerCAmelCase__			)
			else:
						a__  :	Any		 =			SpeechTaConfig()

			if task == "s2t":
						a__  :	int		 =			config.max_text_positions
						a__  :	Dict		 =			SpeechTaForSpeechToText(lowerCAmelCase__			)
			elif task == "t2s":
						a__  :	Any		 =			1_8_7_6
						a__  :	Optional[int]		 =			6_0_0
						a__  :	Optional[int]		 =			config.max_speech_positions
						a__  :	Any		 =			SpeechTaForTextToSpeech(lowerCAmelCase__			)
			elif task == "s2s":
						a__  :	Dict		 =			1_8_7_6
						a__  :	int		 =			config.max_speech_positions
						a__  :	str		 =			SpeechTaForSpeechToSpeech(lowerCAmelCase__			)
			else:
						raise ValueError(F"Unknown task name: {task}"			)

			if vocab_path:
						a__  :	Tuple		 =			SpeechTaTokenizer(lowerCAmelCase__							,			model_max_length=config.max_text_positions			)

						# Mask token behaves like a normal word, i.e. include the space before it
						a__  :	Dict		 =			AddedToken("<mask>"							,			lstrip=lowerCAmelCase__							,			rstrip=lowerCAmelCase__			)
						a__  :	Optional[Any]		 =			mask_token
						tokenizer.add_special_tokens({"mask_token": mask_token}			)
						tokenizer.add_tokens(["<ctc_blank>"]			)

			a__  :	Dict		 =			SpeechTaFeatureExtractor()
			a__  :	Optional[Any]		 =			SpeechTaProcessor(tokenizer=lowerCAmelCase__							,			feature_extractor=lowerCAmelCase__			)
			processor.save_pretrained(lowerCAmelCase__			)

			a__  :	Optional[int]		 =			torch.load(lowerCAmelCase__			)
			recursively_load_weights(fairseq_checkpoint["model"]							,			lowerCAmelCase__							,			lowerCAmelCase__			)

			model.save_pretrained(lowerCAmelCase__			)

			if repo_id:
						print("Pushing to the hub..."			)
						processor.push_to_hub(lowerCAmelCase__			)
						model.push_to_hub(lowerCAmelCase__			)


if __name__ == "__main__":
		__UpperCAmelCase						=			argparse.ArgumentParser()
		parser.add_argument(
		    '''--task''',
		    default='''s2t''',
		    type=str,
		    help='''Type of the SpeechT5 model you\'d like to convert. Should be one of \'s2t\', \'t2s\', \'s2s\'.''',
		)
		parser.add_argument('''--checkpoint_path''', required=True, default=None, type=str, help='''Path to fairseq checkpoint''')
		parser.add_argument('''--vocab_path''', default=None, type=str, help='''Path to SentencePiece model''')
		parser.add_argument('''--config_path''', default=None, type=str, help='''Path to hf config.json of model to convert''')
		parser.add_argument(
		    '''--pytorch_dump_folder_path''', required=True, default=None, type=str, help='''Path to the output PyTorch model.'''
		)
		parser.add_argument(
		    '''--push_to_hub''', default=None, type=str, help='''Where to upload the converted model on the 🤗 hub.'''
		)

		__UpperCAmelCase						=			parser.parse_args()
		convert_speechta_checkpoint(
		    args.task,
		    args.checkpoint_path,
		    args.pytorch_dump_folder_path,
		    args.config_path,
		    args.vocab_path,
		    args.push_to_hub,
		)






"""simple docstring"""






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


__UpperCAmelCase						=			logging.get_logger(__name__)



class 					__UpperCAmelCase							(						_UpperCamelCase  ):
				def       UpperCAmelCase      (		self    :			int	, a_    :			List[Any]    )	->	Optional[Any]:



							'''simple docstring'''



							if isinstance(a_	, a_    ):
										a__  :	Any		 =			[label.strip() for label in labels.split(","    ) if label.strip()]
							return labels






				def __call__(		self    :			Union[str, Any]	, a_    :			Tuple	, a_    :			Optional[Any]	, a_    :			List[str]    )	->	Optional[Any]:



							'''simple docstring'''



							if len(a_    ) == 0 or len(a_    ) == 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(a_    )    )

							if isinstance(a_	, a_    ):
										a__  :	str		 =			[sequences]

							a__  :	Optional[int]		 =			[]
							for sequence in sequences:
										sequence_pairs.extend([[sequence, hypothesis_template.format(a_    )] for label in labels]    )

							return sequence_pairs, sequences



@add_end_docstrings(_UpperCamelCase  )
class 					__UpperCAmelCase							(						_UpperCamelCase  ):
				def __init__(		self    :			str	, a_    :			Optional[Any]=ZeroShotClassificationArgumentHandler()	, *a_    :			Tuple	, **a_    :			str    )	->	Optional[int]:



							'''simple docstring'''



							a__  :	List[Any]		 =			args_parser
							super().__init__(*a_	, **a_    )
							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       UpperCAmelCase      (		self    :			Tuple    )	->	str:



							'''simple docstring'''



							for label, ind in self.model.config.labelaid.items():
										if label.lower().startswith("entail"    ):
													return ind
							return -1
				def       UpperCAmelCase      (		self    :			Optional[int]	, a_    :			List[Any]	, a_    :			int=True	, a_    :			Tuple=True	, a_    :			Tuple=TruncationStrategy.ONLY_FIRST	, **a_    :			Dict    )	->	List[Any]:



							'''simple docstring'''



							a__  :	str		 =			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`"    )
										a__  :	List[str]		 =			self.tokenizer.eos_token
							try:
										a__  :	List[str]		 =			self.tokenizer(
										    a_	, add_special_tokens=a_	, return_tensors=a_	, padding=a_	, truncation=a_	, )
							except Exception as e:
										if "too short" in str(a_    ):
													# 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.

													a__  :	List[str]		 =			self.tokenizer(
													    a_	, add_special_tokens=a_	, return_tensors=a_	, padding=a_	, truncation=TruncationStrategy.DO_NOT_TRUNCATE	, )
										else:
													raise e

							return inputs
				def       UpperCAmelCase      (		self    :			Tuple	, **a_    :			Tuple    )	->	Optional[int]:



							'''simple docstring'''



							if kwargs.get("multi_class"	, a_    ) is not None:
										a__  :	str		 =			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."    )
							a__  :	Tuple		 =			{}
							if "candidate_labels" in kwargs:
										a__  :	Any		 =			self._args_parser._parse_labels(kwargs["candidate_labels"]    )
							if "hypothesis_template" in kwargs:
										a__  :	str		 =			kwargs["hypothesis_template"]

							a__  :	Tuple		 =			{}
							if "multi_label" in kwargs:
										a__  :	Dict		 =			kwargs["multi_label"]
							return preprocess_params, {}, postprocess_params
				def __call__(		self    :			str	, a_    :			Union[str, List[str]]	, *a_    :			List[str]	, **a_    :			List[Any]	, )	->	Tuple:



							'''simple docstring'''



							if len(a_    ) == 0:
										pass
							elif len(a_    ) == 1 and "candidate_labels" not in kwargs:
										a__  :	Any		 =			args[0]
							else:
										raise ValueError(F"Unable to understand extra arguments {args}"    )

							return super().__call__(a_	, **a_    )
				def       UpperCAmelCase      (		self    :			Optional[int]	, a_    :			Tuple	, a_    :			Any=None	, a_    :			Dict="This example is {}."    )	->	Optional[int]:



							'''simple docstring'''



							a__ ,	a__  :	Optional[Any]		 =			self._args_parser(a_	, a_	, a_    )

							for i, (candidate_label, sequence_pair) in enumerate(zip(a_	, a_    )    ):
										a__  :	Union[str, Any]		 =			self._parse_and_tokenize([sequence_pair]    )

										yield {
										    "candidate_label": candidate_label,
										    "sequence": sequences[0],
										    "is_last": i == len(a_    ) - 1,
										    **model_input,
										}
				def       UpperCAmelCase      (		self    :			Optional[int]	, a_    :			Optional[Any]    )	->	List[Any]:



							'''simple docstring'''



							a__  :	Dict		 =			inputs["candidate_label"]
							a__  :	Optional[int]		 =			inputs["sequence"]
							a__  :	Optional[int]		 =			{k: inputs[k] for k in self.tokenizer.model_input_names}
							a__  :	int		 =			self.model(**a_    )

							a__  :	Optional[int]		 =			{
							    "candidate_label": candidate_label,
							    "sequence": sequence,
							    "is_last": inputs["is_last"],
							    **outputs,
							}
							return model_outputs






				def       UpperCAmelCase      (		self    :			Dict	, a_    :			Any	, a_    :			List[str]=False    )	->	Union[str, Any]:



							'''simple docstring'''



							a__  :	int		 =			[outputs["candidate_label"] for outputs in model_outputs]
							a__  :	Optional[int]		 =			[outputs["sequence"] for outputs in model_outputs]
							a__  :	Union[str, Any]		 =			np.concatenate([output["logits"].numpy() for output in model_outputs]    )
							a__  :	List[str]		 =			logits.shape[0]
							a__  :	Optional[int]		 =			len(a_    )
							a__  :	List[str]		 =			N // n
							a__  :	int		 =			logits.reshape((num_sequences, n, -1)    )

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

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