

'''simple docstring'''
import unittest

from transformers import (
    MODEL_FOR_SEQUENCE_CLASSIFICATION_MAPPING,
    TF_MODEL_FOR_SEQUENCE_CLASSIFICATION_MAPPING,
    TextClassificationPipeline,
    pipeline,
)
from transformers.testing_utils import is_pipeline_test, nested_simplify, require_tf, require_torch, slow

from .test_pipelines_common import ANY


# These 2 model types require different inputs than those of the usual text models.
_UpperCAmelCase  :		int  =		{'''LayoutLMv2Config''', '''LayoutLMv3Config'''}

@is_pipeline_test
class 						__magic_name__   (							unittest.TestCase ):
 UpperCamelCase__     =       MODEL_FOR_SEQUENCE_CLASSIFICATION_MAPPING
 UpperCamelCase__     =       TF_MODEL_FOR_SEQUENCE_CLASSIFICATION_MAPPING

 if model_mapping is not None:
  UpperCamelCase__     =       {config: model for config, model in model_mapping.items() if config.__name__ not in _TO_SKIP}
 if tf_model_mapping is not None:
  UpperCamelCase__     =       {
      config: model for config, model in tf_model_mapping.items() if config.__name__ not in _TO_SKIP
  }







 @require_torch
 def 						_A(       self   ):
   lowercase									=pipeline(
       task='''text-classification'''  , model='''hf-internal-testing/tiny-random-distilbert'''  , framework='''pt'''   )

   lowercase									=text_classifier('''This is great !'''   )
   self.assertEqual(nested_simplify(snake_case_   )  , [{'''label''': '''LABEL_0''', '''score''': 0.5_04}]   )

   lowercase									=text_classifier('''This is great !'''  , top_k=2   )
   self.assertEqual(
       nested_simplify(snake_case_   )  , [{'''label''': '''LABEL_0''', '''score''': 0.5_04}, {'''label''': '''LABEL_1''', '''score''': 0.4_96}]   )

   lowercase									=text_classifier(['''This is great !''', '''This is bad''']  , top_k=2   )
   self.assertEqual(
       nested_simplify(snake_case_   )  , [
           [{'''label''': '''LABEL_0''', '''score''': 0.5_04}, {'''label''': '''LABEL_1''', '''score''': 0.4_96}],
           [{'''label''': '''LABEL_0''', '''score''': 0.5_04}, {'''label''': '''LABEL_1''', '''score''': 0.4_96}],
       ]  , )

   lowercase									=text_classifier('''This is great !'''  , top_k=1   )
   self.assertEqual(nested_simplify(snake_case_   )  , [{'''label''': '''LABEL_0''', '''score''': 0.5_04}]   )

   # Legacy behavior
   lowercase									=text_classifier('''This is great !'''  , return_all_scores=snake_case_   )
   self.assertEqual(nested_simplify(snake_case_   )  , [{'''label''': '''LABEL_0''', '''score''': 0.5_04}]   )

   lowercase									=text_classifier('''This is great !'''  , return_all_scores=snake_case_   )
   self.assertEqual(
       nested_simplify(snake_case_   )  , [[{'''label''': '''LABEL_0''', '''score''': 0.5_04}, {'''label''': '''LABEL_1''', '''score''': 0.4_96}]]   )

   lowercase									=text_classifier(['''This is great !''', '''Something else''']  , return_all_scores=snake_case_   )
   self.assertEqual(
       nested_simplify(snake_case_   )  , [
           [{'''label''': '''LABEL_0''', '''score''': 0.5_04}, {'''label''': '''LABEL_1''', '''score''': 0.4_96}],
           [{'''label''': '''LABEL_0''', '''score''': 0.5_04}, {'''label''': '''LABEL_1''', '''score''': 0.4_96}],
       ]  , )

   lowercase									=text_classifier(['''This is great !''', '''Something else''']  , return_all_scores=snake_case_   )
   self.assertEqual(
       nested_simplify(snake_case_   )  , [
           {'''label''': '''LABEL_0''', '''score''': 0.5_04},
           {'''label''': '''LABEL_0''', '''score''': 0.5_04},
       ]  , )







 @require_torch
 def 						_A(       self   ):
   import torch

   lowercase									=pipeline(
       task='''text-classification'''  , model='''hf-internal-testing/tiny-random-distilbert'''  , framework='''pt'''  , device=torch.device('''cpu'''   )  , )

   lowercase									=text_classifier('''This is great !'''   )
   self.assertEqual(nested_simplify(snake_case_   )  , [{'''label''': '''LABEL_0''', '''score''': 0.5_04}]   )







 @require_tf
 def 						_A(       self   ):
   lowercase									=pipeline(
       task='''text-classification'''  , model='''hf-internal-testing/tiny-random-distilbert'''  , framework='''tf'''   )

   lowercase									=text_classifier('''This is great !'''   )
   self.assertEqual(nested_simplify(snake_case_   )  , [{'''label''': '''LABEL_0''', '''score''': 0.5_04}]   )







 @slow
 @require_torch
 def 						_A(       self   ):
   lowercase									=pipeline('''text-classification'''   )

   lowercase									=text_classifier('''This is great !'''   )
   self.assertEqual(nested_simplify(snake_case_   )  , [{'''label''': '''POSITIVE''', '''score''': 1.0}]   )
   lowercase									=text_classifier('''This is bad !'''   )
   self.assertEqual(nested_simplify(snake_case_   )  , [{'''label''': '''NEGATIVE''', '''score''': 1.0}]   )
   lowercase									=text_classifier('''Birds are a type of animal'''   )
   self.assertEqual(nested_simplify(snake_case_   )  , [{'''label''': '''POSITIVE''', '''score''': 0.9_88}]   )







 @slow
 @require_tf
 def 						_A(       self   ):
   lowercase									=pipeline('''text-classification'''  , framework='''tf'''   )

   lowercase									=text_classifier('''This is great !'''   )
   self.assertEqual(nested_simplify(snake_case_   )  , [{'''label''': '''POSITIVE''', '''score''': 1.0}]   )
   lowercase									=text_classifier('''This is bad !'''   )
   self.assertEqual(nested_simplify(snake_case_   )  , [{'''label''': '''NEGATIVE''', '''score''': 1.0}]   )
   lowercase									=text_classifier('''Birds are a type of animal'''   )
   self.assertEqual(nested_simplify(snake_case_   )  , [{'''label''': '''POSITIVE''', '''score''': 0.9_88}]   )







 def 						_A(       self  , snake_case_  , snake_case_  , snake_case_   ):
   lowercase									=TextClassificationPipeline(model=snake_case_  , tokenizer=snake_case_   )
   return text_classifier, ["HuggingFace is in", "This is another test"]







 def 						_A(       self  , snake_case_  , snake_case_   ):
   lowercase									=text_classifier.model
   # Small inputs because BartTokenizer tiny has maximum position embeddings = 22
   lowercase									='''HuggingFace is in'''
   lowercase									=text_classifier(snake_case_   )

   self.assertEqual(nested_simplify(snake_case_   )  , [{'''label''': ANY(snake_case_   ), '''score''': ANY(snake_case_   )}]   )
   self.assertTrue(outputs[0]['''label'''] in model.config.idalabel.values()   )

   lowercase									=['''HuggingFace is in ''', '''Paris is in France''']
   lowercase									=text_classifier(snake_case_   )
   self.assertEqual(
       nested_simplify(snake_case_   )  , [{'''label''': ANY(snake_case_   ), '''score''': ANY(snake_case_   )}, {'''label''': ANY(snake_case_   ), '''score''': ANY(snake_case_   )}]  , )
   self.assertTrue(outputs[0]['''label'''] in model.config.idalabel.values()   )
   self.assertTrue(outputs[1]['''label'''] in model.config.idalabel.values()   )

   # Forcing to get all results with `top_k=None`
   # This is NOT the legacy format
   lowercase									=text_classifier(snake_case_  , top_k=snake_case_   )
   lowercase									=len(model.config.idalabel.values()   )
   self.assertEqual(
       nested_simplify(snake_case_   )  , [[{'''label''': ANY(snake_case_   ), '''score''': ANY(snake_case_   )}] * N, [{'''label''': ANY(snake_case_   ), '''score''': ANY(snake_case_   )}] * N]  , )

   lowercase									={'''text''': '''HuggingFace is in ''', '''text_pair''': '''Paris is in France'''}
   lowercase									=text_classifier(snake_case_   )
   self.assertEqual(
       nested_simplify(snake_case_   )  , {'''label''': ANY(snake_case_   ), '''score''': ANY(snake_case_   )}  , )
   self.assertTrue(outputs['''label'''] in model.config.idalabel.values()   )

   # This might be used a text pair, but tokenizer + pipe interaction
   # makes it hard to understand that it's not using the pair properly
   # https://github.com/huggingface/transformers/issues/17305
   # We disabled this usage instead as it was outputting wrong outputs.
   lowercase									=[['''HuggingFace is in ''', '''Paris is in France''']]
   with self.assertRaises(snake_case_   ):
     text_classifier(snake_case_   )

   # This used to be valid for doing text pairs
   # We're keeping it working because of backward compatibility
   lowercase									=text_classifier([[['''HuggingFace is in ''', '''Paris is in France''']]]   )
   self.assertEqual(
       nested_simplify(snake_case_   )  , [{'''label''': ANY(snake_case_   ), '''score''': ANY(snake_case_   )}]  , )
   self.assertTrue(outputs[0]['''label'''] in model.config.idalabel.values()   )








import warnings

from ...configuration_utils import PretrainedConfig
from ...utils import logging


_SCREAMING_SNAKE_CASE   =  logging.get_logger(__name__)

_SCREAMING_SNAKE_CASE   =  {
    "RUCAIBox/mvp": "https://huggingface.co/RUCAIBox/mvp/resolve/main/config.json",
}






class 					SCREAMING_SNAKE_CASE_  (       _a ):
       """simple docstring"""


       __lowerCAmelCase    :		List[str]					='''mvp'''
       __lowerCAmelCase    :		List[Any]					=['''past_key_values''']
       __lowerCAmelCase    :		Optional[Any]					={'''num_attention_heads''': '''encoder_attention_heads''', '''hidden_size''': '''d_model'''}





       def __init__(				self	:Any,      snake_case	:Any=5_0267,      snake_case	:List[str]=1024,      snake_case	:List[str]=12,      snake_case	:List[Any]=4096,      snake_case	:List[Any]=16,      snake_case	:Optional[int]=12,      snake_case	:Optional[int]=4096,      snake_case	:List[str]=16,      snake_case	:Optional[Any]=0.0,      snake_case	:Tuple=0.0,      snake_case	:str="gelu",      snake_case	:int=1024,      snake_case	:Any=0.1,      snake_case	:Dict=0.0,      snake_case	:Optional[Any]=0.0,      snake_case	:int=0.0_2,      snake_case	:Union[str, Any]=0.0,      snake_case	:Tuple=False,      snake_case	:Optional[int]=True,      snake_case	:Any=1,      snake_case	:Union[str, Any]=0,      snake_case	:Optional[Any]=2,      snake_case	:Union[str, Any]=True,      snake_case	:Tuple=2,      snake_case	:Any=2,      snake_case	:List[Any]=False,      snake_case	:Optional[int]=100,      snake_case	:Union[str, Any]=800,      **snake_case	:Tuple,      ):



          """simple docstring"""

          _lowercase              =vocab_size
          _lowercase              =max_position_embeddings
          _lowercase              =d_model
          _lowercase              =encoder_ffn_dim
          _lowercase              =encoder_layers
          _lowercase              =encoder_attention_heads
          _lowercase              =decoder_ffn_dim
          _lowercase              =decoder_layers
          _lowercase              =decoder_attention_heads
          _lowercase              =dropout
          _lowercase              =attention_dropout
          _lowercase              =activation_dropout
          _lowercase              =activation_function
          _lowercase              =init_std
          _lowercase              =encoder_layerdrop
          _lowercase              =decoder_layerdrop
          _lowercase              =classifier_dropout
          _lowercase              =use_cache
          _lowercase              =encoder_layers
          _lowercase              =scale_embedding  # scale factor will be sqrt(d_model) if True
          _lowercase              =use_prompt
          _lowercase              =prompt_length
          _lowercase              =prompt_mid_dim

          super().__init__(
              pad_token_id=snake_case,      bos_token_id=snake_case,      eos_token_id=snake_case,      is_encoder_decoder=snake_case,      decoder_start_token_id=snake_case,      forced_eos_token_id=snake_case,      **snake_case,      )

          if self.forced_bos_token_id is None and kwargs.get('force_bos_token_to_be_generated',      snake_case):
             _lowercase              =self.bos_token_id
             warnings.warn(
                 f'''Please make sure the config includes `forced_bos_token_id={self.bos_token_id}` in future versions. '''
                 'The config can simply be saved and uploaded again to be fixed.')


