



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


lowerCAmelCase__					  =  logging.get_logger(__name__)

class 				snake_case__(_UpperCamelCase      ):

					"""simple docstring"""






					def 			snake_case					(							self :     str				,       SCREAMING_SNAKE_CASE :     List[str]					):
									if isinstance(SCREAMING_SNAKE_CASE				,       SCREAMING_SNAKE_CASE					):
													lowercase__					:   str													=					[label.strip() for label in labels.split(","					) if label.strip()]
									return labels







					def __call__(							self :     Tuple				,       SCREAMING_SNAKE_CASE :     str				,       SCREAMING_SNAKE_CASE :     Union[str, Any]				,       SCREAMING_SNAKE_CASE :     Optional[int]					):
									if len(SCREAMING_SNAKE_CASE					) == 0 or len(SCREAMING_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(SCREAMING_SNAKE_CASE					)					)

									if isinstance(SCREAMING_SNAKE_CASE				,       SCREAMING_SNAKE_CASE					):
													lowercase__					:   List[str]													=					[sequences]

									lowercase__					:   Optional[Any]													=					[]
									for sequence in sequences:
													sequence_pairs.extend([[sequence, hypothesis_template.format(SCREAMING_SNAKE_CASE					)] for label in labels]					)

									return sequence_pairs, sequences






@add_end_docstrings(_UpperCamelCase      )
class 				snake_case__(_UpperCamelCase      ):

					"""simple docstring"""






					def __init__(							self :     Optional[int]				,       SCREAMING_SNAKE_CASE :     Optional[Any]=ZeroShotClassificationArgumentHandler()				,       *SCREAMING_SNAKE_CASE :     Union[str, Any]				,       **SCREAMING_SNAKE_CASE :     Union[str, Any]					):
									lowercase__					:   Tuple													=					args_parser
									super().__init__(*SCREAMING_SNAKE_CASE				,       **SCREAMING_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 			snake_case					(							self :     Dict					):
									for label, ind in self.model.config.labelaid.items():
													if label.lower().startswith("entail"					):
																	return ind
									return -1







					def 			snake_case					(							self :     List[str]				,       SCREAMING_SNAKE_CASE :     Tuple				,       SCREAMING_SNAKE_CASE :     List[str]=True				,       SCREAMING_SNAKE_CASE :     Any=True				,       SCREAMING_SNAKE_CASE :     List[Any]=TruncationStrategy.ONLY_FIRST				,       **SCREAMING_SNAKE_CASE :     Optional[int]					):
									lowercase__					:   int													=					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`"					)
													lowercase__					:   Optional[int]													=					self.tokenizer.eos_token
									try:
													lowercase__					:   str													=					self.tokenizer(
													    SCREAMING_SNAKE_CASE				,       add_special_tokens=SCREAMING_SNAKE_CASE				,       return_tensors=SCREAMING_SNAKE_CASE				,       padding=SCREAMING_SNAKE_CASE				,       truncation=SCREAMING_SNAKE_CASE				,       )
									except Exception as e:
													if "too short" in str(SCREAMING_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.

																	lowercase__					:   List[Any]													=					self.tokenizer(
																	    SCREAMING_SNAKE_CASE				,       add_special_tokens=SCREAMING_SNAKE_CASE				,       return_tensors=SCREAMING_SNAKE_CASE				,       padding=SCREAMING_SNAKE_CASE				,       truncation=TruncationStrategy.DO_NOT_TRUNCATE				,       )
													else:
																	raise e

									return inputs







					def 			snake_case					(							self :     Union[str, Any]				,       **SCREAMING_SNAKE_CASE :     int					):
									if kwargs.get("multi_class"				,       SCREAMING_SNAKE_CASE					) is not None:
													lowercase__					:   int													=					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."					)
									lowercase__					:   int													=					{}
									if "candidate_labels" in kwargs:
													lowercase__					:   Optional[int]													=					self._args_parser._parse_labels(kwargs["candidate_labels"]					)
									if "hypothesis_template" in kwargs:
													lowercase__					:   Dict													=					kwargs["hypothesis_template"]

									lowercase__					:   str													=					{}
									if "multi_label" in kwargs:
													lowercase__					:   str													=					kwargs["multi_label"]
									return preprocess_params, {}, postprocess_params







					def __call__(							self :     Union[str, Any]				,       SCREAMING_SNAKE_CASE :     Union[str, List[str]]				,       *SCREAMING_SNAKE_CASE :     Dict				,       **SCREAMING_SNAKE_CASE :     List[Any]				,       ):
									if len(SCREAMING_SNAKE_CASE					) == 0:
													pass
									elif len(SCREAMING_SNAKE_CASE					) == 1 and "candidate_labels" not in kwargs:
													lowercase__					:   List[str]													=					args[0]
									else:
													raise ValueError(f"""Unable to understand extra arguments {args}"""					)

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







					def 			snake_case					(							self :     List[Any]				,       SCREAMING_SNAKE_CASE :     str				,       SCREAMING_SNAKE_CASE :     Dict=None				,       SCREAMING_SNAKE_CASE :     Tuple="This example is {}."					):
									lowercase__							,							lowercase__					:   Tuple													=					self._args_parser(SCREAMING_SNAKE_CASE				,       SCREAMING_SNAKE_CASE				,       SCREAMING_SNAKE_CASE					)

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

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







					def 			snake_case					(							self :     Optional[int]				,       SCREAMING_SNAKE_CASE :     Optional[int]					):
									lowercase__					:   Optional[int]													=					inputs["candidate_label"]
									lowercase__					:   str													=					inputs["sequence"]
									lowercase__					:   Any													=					{k: inputs[k] for k in self.tokenizer.model_input_names}
									lowercase__					:   Dict													=					self.model(**SCREAMING_SNAKE_CASE					)

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







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

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

									lowercase__					:   Optional[int]													=					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 Union

import fire
import torch
from tqdm import tqdm


def  __lowerCamelCase (       lowerCamelCase__					,			lowerCamelCase__ = "cpu"					,			lowerCamelCase__ = None						):

				"""simple docstring"""


				lowercase__					:   Any													=					torch.load(lowerCamelCase__					,			map_location=lowerCamelCase__						)
				for k, v in tqdm(state_dict.items()						):
								if not isinstance(lowerCamelCase__					,			torch.Tensor						):
												raise TypeError("FP16 conversion only works on paths that are saved state dicts, like pytorch_model.bin"						)
								lowercase__					:   int													=					v.half()
				if save_path is None:  # overwrite src_path
								lowercase__					:   Optional[Any]													=					src_path
				torch.save(lowerCamelCase__					,			lowerCamelCase__						)


if __name__ == "__main__":
				fire.Fire(convert)




