





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


A_												=     logging.get_logger(__name__)

class SCREAMING_SNAKE_CASE_ (					lowercase_   ):





					"""simple docstring"""


					def 					__magic_name__ ( self	,  _lowerCAmelCase	):
										if isinstance(_lowerCAmelCase	,  _lowerCAmelCase	):
															lowerCamelCase__       =       [label.strip() for label in labels.split(","	) if label.strip()]
										return labels






					def __call__( self	,  _lowerCAmelCase	,  _lowerCAmelCase	,  _lowerCAmelCase	):
										if len(_lowerCAmelCase	) == 0 or len(_lowerCAmelCase	) == 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(_lowerCAmelCase	)	)

										if isinstance(_lowerCAmelCase	,  _lowerCAmelCase	):
															lowerCamelCase__       =       [sequences]

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

										return sequence_pairs, sequences

@add_end_docstrings(lowercase_   )
class SCREAMING_SNAKE_CASE_ (					lowercase_   ):





					"""simple docstring"""


					def __init__( self	,  _lowerCAmelCase=ZeroShotClassificationArgumentHandler()	,  *_lowerCAmelCase	,  **_lowerCAmelCase	):
										lowerCamelCase__       =       args_parser
										super().__init__(*_lowerCAmelCase	,  **_lowerCAmelCase	)
										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 					__magic_name__ ( self	):
										for label, ind in self.model.config.labelaid.items():
															if label.lower().startswith("entail"	):
																				return ind
										return -1





					def 					__magic_name__ ( self	,  _lowerCAmelCase	,  _lowerCAmelCase=True	,  _lowerCAmelCase=True	,  _lowerCAmelCase=TruncationStrategy.ONLY_FIRST	,  **_lowerCAmelCase	):
										lowerCamelCase__       =       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`"	)
															lowerCamelCase__       =       self.tokenizer.eos_token
										try:
															lowerCamelCase__       =       self.tokenizer(
															    _lowerCAmelCase	,  add_special_tokens=_lowerCAmelCase	,  return_tensors=_lowerCAmelCase	,  padding=_lowerCAmelCase	,  truncation=_lowerCAmelCase	,  )
										except Exception as e:
															if "too short" in str(_lowerCAmelCase	):
																				# 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.

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

										return inputs





					def 					__magic_name__ ( self	,  **_lowerCAmelCase	):
										if kwargs.get("multi_class"	,  _lowerCAmelCase	) is not None:
															lowerCamelCase__       =       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."	)
										lowerCamelCase__       =       {}
										if "candidate_labels" in kwargs:
															lowerCamelCase__       =       self._args_parser._parse_labels(kwargs["candidate_labels"]	)
										if "hypothesis_template" in kwargs:
															lowerCamelCase__       =       kwargs["hypothesis_template"]

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





					def __call__( self	,  _lowerCAmelCase	,  *_lowerCAmelCase	,  **_lowerCAmelCase	,  ):
										if len(_lowerCAmelCase	) == 0:
															pass
										elif len(_lowerCAmelCase	) == 1 and "candidate_labels" not in kwargs:
															lowerCamelCase__       =       args[0]
										else:
															raise ValueError(F"Unable to understand extra arguments {args}"	)

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





					def 					__magic_name__ ( self	,  _lowerCAmelCase	,  _lowerCAmelCase=None	,  _lowerCAmelCase="This example is {}."	):
										lowerCamelCase__    ,		lowerCamelCase__       =       self._args_parser(_lowerCAmelCase	,  _lowerCAmelCase	,  _lowerCAmelCase	)

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

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





					def 					__magic_name__ ( self	,  _lowerCAmelCase	):
										lowerCamelCase__       =       inputs["candidate_label"]
										lowerCamelCase__       =       inputs["sequence"]
										lowerCamelCase__       =       {k: inputs[k] for k in self.tokenizer.model_input_names}
										lowerCamelCase__       =       self.model(**_lowerCAmelCase	)

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






					def 					__magic_name__ ( self	,  _lowerCAmelCase	,  _lowerCAmelCase=False	):
										lowerCamelCase__       =       [outputs["candidate_label"] for outputs in model_outputs]
										lowerCamelCase__       =       [outputs["sequence"] for outputs in model_outputs]
										lowerCamelCase__       =       np.concatenate([output["logits"].numpy() for output in model_outputs]	)
										lowerCamelCase__       =       logits.shape[0]
										lowerCamelCase__       =       len(_lowerCAmelCase	)
										lowerCamelCase__       =       N // n
										lowerCamelCase__       =       logits.reshape((num_sequences, n, -1)	)

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

										lowerCamelCase__       =       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 TYPE_CHECKING, Any, List, Mapping, Optional, Union

from ...configuration_utils import PretrainedConfig
from ...onnx import OnnxConfig
from ...utils import TensorType, logging


if TYPE_CHECKING:
						from ...onnx.config import PatchingSpec
						from ...tokenization_utils_base import PreTrainedTokenizerBase


A_												=     logging.get_logger(__name__)

A_												=     {
    "allenai/longformer-base-4096": "https://huggingface.co/allenai/longformer-base-4096/resolve/main/config.json",
    "allenai/longformer-large-4096": "https://huggingface.co/allenai/longformer-large-4096/resolve/main/config.json",
    "allenai/longformer-large-4096-finetuned-triviaqa": (
        "https://huggingface.co/allenai/longformer-large-4096-finetuned-triviaqa/resolve/main/config.json"
    ),
    "allenai/longformer-base-4096-extra.pos.embd.only": (
        "https://huggingface.co/allenai/longformer-base-4096-extra.pos.embd.only/resolve/main/config.json"
    ),
    "allenai/longformer-large-4096-extra.pos.embd.only": (
        "https://huggingface.co/allenai/longformer-large-4096-extra.pos.embd.only/resolve/main/config.json"
    ),
}

class SCREAMING_SNAKE_CASE_ (					lowercase_   ):





					"""simple docstring"""


					A__				 =    "longformer"





					def __init__( self	,  _lowerCAmelCase = 512	,  _lowerCAmelCase = 2	,  _lowerCAmelCase = 1	,  _lowerCAmelCase = 0	,  _lowerCAmelCase = 2	,  _lowerCAmelCase = 3_0522	,  _lowerCAmelCase = 768	,  _lowerCAmelCase = 12	,  _lowerCAmelCase = 12	,  _lowerCAmelCase = 3072	,  _lowerCAmelCase = "gelu"	,  _lowerCAmelCase = 0.1	,  _lowerCAmelCase = 0.1	,  _lowerCAmelCase = 512	,  _lowerCAmelCase = 2	,  _lowerCAmelCase = 0.02	,  _lowerCAmelCase = 1E-12	,  _lowerCAmelCase = False	,  **_lowerCAmelCase	,  ):
										super().__init__(pad_token_id=_lowerCAmelCase	,  **_lowerCAmelCase	)

										lowerCamelCase__       =       attention_window
										lowerCamelCase__       =       sep_token_id
										lowerCamelCase__       =       bos_token_id
										lowerCamelCase__       =       eos_token_id
										lowerCamelCase__       =       vocab_size
										lowerCamelCase__       =       hidden_size
										lowerCamelCase__       =       num_hidden_layers
										lowerCamelCase__       =       num_attention_heads
										lowerCamelCase__       =       hidden_act
										lowerCamelCase__       =       intermediate_size
										lowerCamelCase__       =       hidden_dropout_prob
										lowerCamelCase__       =       attention_probs_dropout_prob
										lowerCamelCase__       =       max_position_embeddings
										lowerCamelCase__       =       type_vocab_size
										lowerCamelCase__       =       initializer_range
										lowerCamelCase__       =       layer_norm_eps
										lowerCamelCase__       =       onnx_export

class SCREAMING_SNAKE_CASE_ (					lowercase_   ):





					"""simple docstring"""


					def __init__( self	,  _lowerCAmelCase	,  _lowerCAmelCase = "default"	,  _lowerCAmelCase = None	):
										super().__init__(_lowerCAmelCase	,  _lowerCAmelCase	,  _lowerCAmelCase	)
										lowerCamelCase__       =       True





					@property
					def 					__magic_name__ ( self	):
										if self.task == "multiple-choice":
															lowerCamelCase__       =       {0: "batch", 1: "choice", 2: "sequence"}
										else:
															lowerCamelCase__       =       {0: "batch", 1: "sequence"}
										return OrderedDict(
										    [
										        ("input_ids", dynamic_axis),
										        ("attention_mask", dynamic_axis),
										        ("global_attention_mask", dynamic_axis),
										    ]	)





					@property
					def 					__magic_name__ ( self	):
										lowerCamelCase__       =       super().outputs
										if self.task == "default":
															lowerCamelCase__       =       {0: "batch"}
										return outputs





					@property
					def 					__magic_name__ ( self	):
										return 1E-4





					@property
					def 					__magic_name__ ( self	):
										# needs to be >= 14 to support tril operator
										return max(super().default_onnx_opset	,  14	)






					def 					__magic_name__ ( self	,  _lowerCAmelCase	,  _lowerCAmelCase = -1	,  _lowerCAmelCase = -1	,  _lowerCAmelCase = False	,  _lowerCAmelCase = None	,  ):
										lowerCamelCase__       =       super().generate_dummy_inputs(
										    preprocessor=_lowerCAmelCase	,  batch_size=_lowerCAmelCase	,  seq_length=_lowerCAmelCase	,  is_pair=_lowerCAmelCase	,  framework=_lowerCAmelCase	)
										import torch

										# for some reason, replacing this code by inputs["global_attention_mask"] = torch.randint(2, inputs["input_ids"].shape, dtype=torch.int64)
										# makes the export fail randomly
										lowerCamelCase__       =       torch.zeros_like(inputs["input_ids"]	)
										# make every second token global
										lowerCamelCase__       =       1

										return inputs

