
'''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


lowercase     :							Union[str, Any]       						=     logging.get_logger(__name__)





class 					A							(				__snake_case		):





     def 							__lowerCAmelCase				(				self		,     SCREAMING_SNAKE_CASE      )					->							int:






         """simple docstring"""






         if isinstance(SCREAMING_SNAKE_CASE		,     SCREAMING_SNAKE_CASE      ):
             A			:  Tuple   =		[label.strip() for label in labels.split(''','''      ) if label.strip()]
         return labels





     def __call__(				self		,     SCREAMING_SNAKE_CASE		,     SCREAMING_SNAKE_CASE		,     SCREAMING_SNAKE_CASE      )					->							List[Any]:






         """simple docstring"""






         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      ):
             A			:  Dict   =		[sequences]

         A			:  Dict   =		[]
         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(__snake_case		)
class 					A							(				__snake_case		):





     def __init__(				self		,     SCREAMING_SNAKE_CASE=ZeroShotClassificationArgumentHandler()		,     *SCREAMING_SNAKE_CASE		,     **SCREAMING_SNAKE_CASE      )					->							int:






         """simple docstring"""






         A			:  Any   =		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 							__lowerCAmelCase				(				self      )					->							Union[str, Any]:






         """simple docstring"""






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





     def 							__lowerCAmelCase				(				self		,     SCREAMING_SNAKE_CASE		,     SCREAMING_SNAKE_CASE=True		,     SCREAMING_SNAKE_CASE=True		,     SCREAMING_SNAKE_CASE=TruncationStrategy.ONLY_FIRST		,     **SCREAMING_SNAKE_CASE      )					->							str:






         """simple docstring"""






         A			:  Optional[Any]   =		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			:  int   =		self.tokenizer.eos_token
         try:
             A			:  Optional[int]   =		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.

                 A			:  Dict   =		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 							__lowerCAmelCase				(				self		,     **SCREAMING_SNAKE_CASE      )					->							Optional[Any]:






         """simple docstring"""






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

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





     def __call__(				self		,     SCREAMING_SNAKE_CASE		,     *SCREAMING_SNAKE_CASE		,     **SCREAMING_SNAKE_CASE		,     )					->							int:






         """simple docstring"""






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

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





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






         """simple docstring"""






         A,    A			:  Optional[Any]   =		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      )      ):
             A			:  str   =		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 							__lowerCAmelCase				(				self		,     SCREAMING_SNAKE_CASE      )					->							Any:






         """simple docstring"""






         A			:  Optional[int]   =		inputs['''candidate_label''']
         A			:  int   =		inputs['''sequence''']
         A			:  Dict   =		{k: inputs[k] for k in self.tokenizer.model_input_names}
         A			:  str   =		self.model(**SCREAMING_SNAKE_CASE      )

         A			:  Union[str, Any]   =		{
             '''candidate_label''': candidate_label,
             '''sequence''': sequence,
             '''is_last''': inputs['''is_last'''],
             **outputs,
         }
         return model_outputs





     def 							__lowerCAmelCase				(				self		,     SCREAMING_SNAKE_CASE		,     SCREAMING_SNAKE_CASE=False      )					->							Optional[int]:






         """simple docstring"""






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

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






'''simple docstring'''




import json
import os
import unittest

from transformers import DebertaTokenizer, DebertaTokenizerFast
from transformers.models.deberta.tokenization_deberta import VOCAB_FILES_NAMES
from transformers.testing_utils import slow

from ...test_tokenization_common import TokenizerTesterMixin





class 					A							(				__snake_case       ,     unittest.TestCase		):
     __magic_name__          =						DebertaTokenizer
     __magic_name__          =						True
     __magic_name__          =						DebertaTokenizerFast





     def 							__lowerCAmelCase				(				self      )					->							Tuple:






         """simple docstring"""






         super().setUp()

         # Adapted from Sennrich et al. 2015 and https://github.com/rsennrich/subword-nmt
         A			:  Optional[int]   =		[
             '''l''',
             '''o''',
             '''w''',
             '''e''',
             '''r''',
             '''s''',
             '''t''',
             '''i''',
             '''d''',
             '''n''',
             '''\u0120''',
             '''\u0120l''',
             '''\u0120n''',
             '''\u0120lo''',
             '''\u0120low''',
             '''er''',
             '''\u0120lowest''',
             '''\u0120newer''',
             '''\u0120wider''',
             '''[UNK]''',
         ]
         A			:  List[str]   =		dict(zip(SCREAMING_SNAKE_CASE		,     range(len(SCREAMING_SNAKE_CASE      )      )      )      )
         A			:  List[Any]   =		['''#version: 0.2''', '''\u0120 l''', '''\u0120l o''', '''\u0120lo w''', '''e r''', '''''']
         A			:  Tuple   =		{'''unk_token''': '''[UNK]'''}

         A			:  Optional[int]   =		os.path.join(self.tmpdirname		,     VOCAB_FILES_NAMES['''vocab_file''']      )
         A			:  str   =		os.path.join(self.tmpdirname		,     VOCAB_FILES_NAMES['''merges_file''']      )
         with open(self.vocab_file		,     '''w'''		,     encoding='''utf-8'''      ) as fp:
             fp.write(json.dumps(SCREAMING_SNAKE_CASE      ) + '''\n'''      )
         with open(self.merges_file		,     '''w'''		,     encoding='''utf-8'''      ) as fp:
             fp.write('''\n'''.join(SCREAMING_SNAKE_CASE      )      )





     def 							__lowerCAmelCase				(				self		,     **SCREAMING_SNAKE_CASE      )					->							Optional[Any]:






         """simple docstring"""






         kwargs.update(self.special_tokens_map      )
         return self.tokenizer_class.from_pretrained(self.tmpdirname		,     **SCREAMING_SNAKE_CASE      )





     def 							__lowerCAmelCase				(				self		,     SCREAMING_SNAKE_CASE      )					->							str:






         """simple docstring"""






         A			:  Union[str, Any]   =		'''lower newer'''
         A			:  List[Any]   =		'''lower newer'''
         return input_text, output_text





     def 							__lowerCAmelCase				(				self      )					->							str:






         """simple docstring"""






         A			:  str   =		self.get_tokenizer()
         A			:  List[str]   =		'''lower newer'''
         A			:  int   =		['''l''', '''o''', '''w''', '''er''', '''\u0120''', '''n''', '''e''', '''w''', '''er''']
         A			:  Any   =		tokenizer.tokenize(SCREAMING_SNAKE_CASE      )
         self.assertListEqual(SCREAMING_SNAKE_CASE		,     SCREAMING_SNAKE_CASE      )

         A			:  str   =		tokens + [tokenizer.unk_token]
         A			:  str   =		[0, 1, 2, 15, 10, 9, 3, 2, 15, 19]
         self.assertListEqual(tokenizer.convert_tokens_to_ids(SCREAMING_SNAKE_CASE      )		,     SCREAMING_SNAKE_CASE      )





     def 							__lowerCAmelCase				(				self      )					->							str:






         """simple docstring"""






         A			:  List[Any]   =		self.get_tokenizer()
         A			:  Tuple   =		tokenizer('''Hello'''		,     '''World'''      )
         A			:  str   =		[0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1]
         self.assertListEqual(tokd['''token_type_ids''']		,     SCREAMING_SNAKE_CASE      )





     @slow
     def 							__lowerCAmelCase				(				self      )					->							int:






         """simple docstring"""






         A			:  Union[str, Any]   =		self.tokenizer_class.from_pretrained('''microsoft/deberta-base'''      )

         A			:  Union[str, Any]   =		tokenizer.encode('''sequence builders'''		,     add_special_tokens=SCREAMING_SNAKE_CASE      )
         A			:  Any   =		tokenizer.encode('''multi-sequence build'''		,     add_special_tokens=SCREAMING_SNAKE_CASE      )

         A			:  Tuple   =		tokenizer.encode(
             '''sequence builders'''		,     add_special_tokens=SCREAMING_SNAKE_CASE		,     add_prefix_space=SCREAMING_SNAKE_CASE      )
         A			:  Union[str, Any]   =		tokenizer.encode(
             '''sequence builders'''		,     '''multi-sequence build'''		,     add_special_tokens=SCREAMING_SNAKE_CASE		,     add_prefix_space=SCREAMING_SNAKE_CASE      )

         A			:  List[str]   =		tokenizer.build_inputs_with_special_tokens(SCREAMING_SNAKE_CASE      )
         A			:  Union[str, Any]   =		tokenizer.build_inputs_with_special_tokens(SCREAMING_SNAKE_CASE		,     SCREAMING_SNAKE_CASE      )

         assert encoded_sentence == encoded_text_from_decode
         assert encoded_pair == encoded_pair_from_decode





     @slow
     def 							__lowerCAmelCase				(				self      )					->							int:






         """simple docstring"""






         A			:  Any   =		[self.tokenizer_class]
         if self.test_rust_tokenizer:
             tokenizer_classes.append(self.rust_tokenizer_class      )

         for tokenizer_class in tokenizer_classes:
             A			:  List[str]   =		tokenizer_class.from_pretrained('''microsoft/deberta-base'''      )

             A			:  Dict   =		[
                 '''ALBERT: A Lite BERT for Self-supervised Learning of Language Representations''',
                 '''ALBERT incorporates two parameter reduction techniques''',
                 '''The first one is a factorized embedding parameterization. By decomposing the large vocabulary'''
                 ''' embedding matrix into two small matrices, we separate the size of the hidden layers from the size of'''
                 ''' vocabulary embedding.''',
             ]

             A			:  List[Any]   =		tokenizer(SCREAMING_SNAKE_CASE		,     padding=SCREAMING_SNAKE_CASE      )
             A			:  List[str]   =		[tokenizer.decode(SCREAMING_SNAKE_CASE		,     skip_special_tokens=SCREAMING_SNAKE_CASE      ) for seq in encoding['''input_ids''']]

             # fmt: off
             A			:  List[str]   =		{
                 '''input_ids''': [
                     [1, 2118, 11126, 565, 35, 83, 25191, 163, 18854, 13, 12156, 12, 16101, 25376, 13807, 9, 22205, 27893, 1635, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
                     [1, 2118, 11126, 565, 24536, 80, 43797, 4878, 7373, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
                     [1, 133, 78, 65, 16, 10, 3724, 1538, 33183, 11303, 43797, 1938, 4, 870, 24165, 29105, 5, 739, 32644, 33183, 11303, 36173, 88, 80, 650, 7821, 45940, 6, 52, 2559, 5, 1836, 9, 5, 7397, 13171, 31, 5, 1836, 9, 32644, 33183, 11303, 4, 2]
                 ],
                 '''token_type_ids''': [
                     [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
                     [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
                     [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
                 ],
                 '''attention_mask''': [
                     [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
                     [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
                     [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]
                 ]
             }
             # fmt: on

             A			:  Any   =		[
                 '''ALBERT: A Lite BERT for Self-supervised Learning of Language Representations''',
                 '''ALBERT incorporates two parameter reduction techniques''',
                 '''The first one is a factorized embedding parameterization. By decomposing the large vocabulary'''
                 ''' embedding matrix into two small matrices, we separate the size of the hidden layers from the size of'''
                 ''' vocabulary embedding.''',
             ]

             self.assertDictEqual(encoding.data		,     SCREAMING_SNAKE_CASE      )

             for expected, decoded in zip(SCREAMING_SNAKE_CASE		,     SCREAMING_SNAKE_CASE      ):
                 self.assertEqual(SCREAMING_SNAKE_CASE		,     SCREAMING_SNAKE_CASE      )




