


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


snake_case__			 =						logging.get_logger(__name__)


class 					lowerCAmelCase_ (       _a):




						def  _snake_case					(	self						:   Any   ,     __A						:   Optional[Any]						)      ->Optional[Any]:






									"""simple docstring"""






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






						def __call__(	self						:   str   ,     __A						:   int   ,     __A						:   Optional[int]   ,     __A						:   List[str]						)      ->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__ :Tuple						  =    [sequences]

									a__ :List[str]						  =    []
									for sequence in sequences:
												sequence_pairs.extend([[sequence, hypothesis_template.format(__A						)] for label in labels]						)

									return sequence_pairs, sequences






@add_end_docstrings(_a)
class 					lowerCAmelCase_ (       _a):




						def __init__(	self						:   Optional[Any]   ,     __A						:   List[str]=ZeroShotClassificationArgumentHandler()   ,     *__A						:   List[str]   ,     **__A						:   Any						)      ->Optional[Any]:






									"""simple docstring"""






									a__ :Tuple						  =    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  _snake_case					(	self						:   str						)      ->str:






									"""simple docstring"""






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




						def  _snake_case					(	self						:   List[Any]   ,     __A						:   int   ,     __A						:   List[Any]=True   ,     __A						:   Dict=True   ,     __A						:   Optional[int]=TruncationStrategy.ONLY_FIRST   ,     **__A						:   int						)      ->Optional[int]:






									"""simple docstring"""






									a__ :Optional[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`"						)
												a__ :Optional[int]						  =    self.tokenizer.eos_token
									try:
												a__ :Union[str, Any]						  =    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__ :int						  =    self.tokenizer(
															    __A   ,     add_special_tokens=__A   ,     return_tensors=__A   ,     padding=__A   ,     truncation=TruncationStrategy.DO_NOT_TRUNCATE   ,     )
												else:
															raise e

									return inputs




						def  _snake_case					(	self						:   Optional[int]   ,     **__A						:   str						)      ->Union[str, Any]:






									"""simple docstring"""






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

									a__ :List[Any]						  =    {}
									if "multi_label" in kwargs:
												a__ :Optional[int]						  =    kwargs["multi_label"]
									return preprocess_params, {}, postprocess_params




						def __call__(	self						:   List[Any]   ,     __A						:   Union[str, List[str]]   ,     *__A						:   Dict   ,     **__A						:   Optional[Any]   ,     )      ->List[str]:






									"""simple docstring"""






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

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




						def  _snake_case					(	self						:   List[Any]   ,     __A						:   List[str]   ,     __A						:   List[Any]=None   ,     __A						:   str="This example is {}."						)      ->Union[str, Any]:






									"""simple docstring"""






									a__ :Any						  =    self._args_parser(__A   ,     __A   ,     __A						)

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

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




						def  _snake_case					(	self						:   Optional[int]   ,     __A						:   Optional[Any]						)      ->Optional[Any]:






									"""simple docstring"""






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

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






						def  _snake_case					(	self						:   int   ,     __A						:   Optional[int]   ,     __A						:   Optional[int]=False						)      ->List[str]:






									"""simple docstring"""






									a__ :Dict						  =    [outputs["candidate_label"] for outputs in model_outputs]
									a__ :Tuple						  =    [outputs["sequence"] for outputs in model_outputs]
									a__ :List[Any]						  =    np.concatenate([output["logits"].numpy() for output in model_outputs]						)
									a__ :Optional[Any]						  =    logits.shape[0]
									a__ :Optional[int]						  =    len(__A						)
									a__ :List[str]						  =    N // n
									a__ :Optional[Any]						  =    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__ :Dict						  =    self.entailment_id
												a__ :Tuple						  =    -1 if entailment_id == 0 else 0
												a__ :Any						  =    reshaped_outputs[..., [contradiction_id, entailment_id]]
												a__ :List[str]						  =    np.exp(__A						) / np.exp(__A						).sum(-1   ,     keepdims=__A						)
												a__ :List[str]						  =    scores[..., 1]
									else:
												# softmax the "entailment" logits over all candidate labels
												a__ :int						  =    reshaped_outputs[..., self.entailment_id]
												a__ :Optional[int]						  =    np.exp(__A						) / np.exp(__A						).sum(-1   ,     keepdims=__A						)

									a__ :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 collections import OrderedDict
from typing import Mapping

from ...configuration_utils import PretrainedConfig
from ...onnx import OnnxConfig


snake_case__			 =						{
    '''albert-base-v1''': '''https://huggingface.co/albert-base-v1/resolve/main/config.json''',
    '''albert-large-v1''': '''https://huggingface.co/albert-large-v1/resolve/main/config.json''',
    '''albert-xlarge-v1''': '''https://huggingface.co/albert-xlarge-v1/resolve/main/config.json''',
    '''albert-xxlarge-v1''': '''https://huggingface.co/albert-xxlarge-v1/resolve/main/config.json''',
    '''albert-base-v2''': '''https://huggingface.co/albert-base-v2/resolve/main/config.json''',
    '''albert-large-v2''': '''https://huggingface.co/albert-large-v2/resolve/main/config.json''',
    '''albert-xlarge-v2''': '''https://huggingface.co/albert-xlarge-v2/resolve/main/config.json''',
    '''albert-xxlarge-v2''': '''https://huggingface.co/albert-xxlarge-v2/resolve/main/config.json''',
}


class 					lowerCAmelCase_ (       _a):
						lowerCamelCase_         =     'albert'




						def __init__(	self						:   Any   ,     __A						:   Optional[int]=30000   ,     __A						:   List[str]=128   ,     __A						:   int=4096   ,     __A						:   Any=12   ,     __A						:   Union[str, Any]=1   ,     __A						:   Optional[int]=64   ,     __A						:   Dict=16384   ,     __A						:   List[str]=1   ,     __A						:   Any="gelu_new"   ,     __A						:   List[Any]=0   ,     __A						:   str=0   ,     __A						:   List[str]=512   ,     __A						:   Optional[Any]=2   ,     __A						:   Tuple=0.02   ,     __A						:   int=1E-12   ,     __A						:   str=0.1   ,     __A						:   Optional[Any]="absolute"   ,     __A						:   Tuple=0   ,     __A						:   str=2   ,     __A						:   Union[str, Any]=3   ,     **__A						:   Union[str, Any]   ,     )      ->str:






									"""simple docstring"""






									super().__init__(pad_token_id=__A   ,     bos_token_id=__A   ,     eos_token_id=__A   ,     **__A						)

									a__ :Tuple						  =    vocab_size
									a__ :Dict						  =    embedding_size
									a__ :Union[str, Any]						  =    hidden_size
									a__ :int						  =    num_hidden_layers
									a__ :List[Any]						  =    num_hidden_groups
									a__ :str						  =    num_attention_heads
									a__ :Optional[Any]						  =    inner_group_num
									a__ :Any						  =    hidden_act
									a__ :Optional[int]						  =    intermediate_size
									a__ :Optional[Any]						  =    hidden_dropout_prob
									a__ :Optional[int]						  =    attention_probs_dropout_prob
									a__ :int						  =    max_position_embeddings
									a__ :List[Any]						  =    type_vocab_size
									a__ :Dict						  =    initializer_range
									a__ :Optional[int]						  =    layer_norm_eps
									a__ :Optional[Any]						  =    classifier_dropout_prob
									a__ :Optional[Any]						  =    position_embedding_type






class 					lowerCAmelCase_ (       _a):




						@property
						def  _snake_case					(	self						:   Optional[int]						)      ->Mapping[str, Mapping[int, str]]:






									"""simple docstring"""






									if self.task == "multiple-choice":
												a__ :List[Any]						  =    {0: "batch", 1: "choice", 2: "sequence"}
									else:
												a__ :Dict						  =    {0: "batch", 1: "sequence"}
									return OrderedDict(
									    [
									        ("input_ids", dynamic_axis),
									        ("attention_mask", dynamic_axis),
									        ("token_type_ids", dynamic_axis),
									    ]						)


