





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.
snake_case_           =     {'LayoutLMv2Config', 'LayoutLMv3Config'}

@is_pipeline_test
class 						SCREAMING_SNAKE_CASE__							(			unittest.TestCase    ):
      A_		:							Union[str, Any]				=       MODEL_FOR_SEQUENCE_CLASSIFICATION_MAPPING
      A_		:							List[str]				=       TF_MODEL_FOR_SEQUENCE_CLASSIFICATION_MAPPING

      if model_mapping is not None:
            A_		:							List[str]				=       {config: model for config, model in model_mapping.items() if config.__name__ not in _TO_SKIP}
      if tf_model_mapping is not None:
            A_		:							Union[str, Any]				=       {
                config: model for config, model in tf_model_mapping.items() if config.__name__ not in _TO_SKIP
            }


      @require_torch
      def   a					(self			: Tuple ):





         """simple docstring"""



         __snake_case							=  pipeline(
             task='''text-classification'''							,     model='''hf-internal-testing/tiny-random-distilbert'''							,     framework='''pt''' )

         __snake_case							=  text_classifier('''This is great !''' )
         self.assertEqual(nested_simplify(SCREAMING_SNAKE_CASE__ )							,     [{'''label''': '''LABEL_0''', '''score''': 0.5_0_4}] )

         __snake_case							=  text_classifier('''This is great !'''							,     top_k=2 )
         self.assertEqual(
             nested_simplify(SCREAMING_SNAKE_CASE__ )							,     [{'''label''': '''LABEL_0''', '''score''': 0.5_0_4}, {'''label''': '''LABEL_1''', '''score''': 0.4_9_6}] )

         __snake_case							=  text_classifier(['''This is great !''', '''This is bad''']							,     top_k=2 )
         self.assertEqual(
             nested_simplify(SCREAMING_SNAKE_CASE__ )							,     [
                 [{'''label''': '''LABEL_0''', '''score''': 0.5_0_4}, {'''label''': '''LABEL_1''', '''score''': 0.4_9_6}],
                 [{'''label''': '''LABEL_0''', '''score''': 0.5_0_4}, {'''label''': '''LABEL_1''', '''score''': 0.4_9_6}],
             ]							,     )

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

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

         __snake_case							=  text_classifier('''This is great !'''							,     return_all_scores=SCREAMING_SNAKE_CASE__ )
         self.assertEqual(
             nested_simplify(SCREAMING_SNAKE_CASE__ )							,     [[{'''label''': '''LABEL_0''', '''score''': 0.5_0_4}, {'''label''': '''LABEL_1''', '''score''': 0.4_9_6}]] )

         __snake_case							=  text_classifier(['''This is great !''', '''Something else''']							,     return_all_scores=SCREAMING_SNAKE_CASE__ )
         self.assertEqual(
             nested_simplify(SCREAMING_SNAKE_CASE__ )							,     [
                 [{'''label''': '''LABEL_0''', '''score''': 0.5_0_4}, {'''label''': '''LABEL_1''', '''score''': 0.4_9_6}],
                 [{'''label''': '''LABEL_0''', '''score''': 0.5_0_4}, {'''label''': '''LABEL_1''', '''score''': 0.4_9_6}],
             ]							,     )

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


      @require_torch
      def   a					(self			: str ):





         """simple docstring"""



         import torch

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

         __snake_case							=  text_classifier('''This is great !''' )
         self.assertEqual(nested_simplify(SCREAMING_SNAKE_CASE__ )							,     [{'''label''': '''LABEL_0''', '''score''': 0.5_0_4}] )


      @require_tf
      def   a					(self			: Optional[Any] ):





         """simple docstring"""



         __snake_case							=  pipeline(
             task='''text-classification'''							,     model='''hf-internal-testing/tiny-random-distilbert'''							,     framework='''tf''' )

         __snake_case							=  text_classifier('''This is great !''' )
         self.assertEqual(nested_simplify(SCREAMING_SNAKE_CASE__ )							,     [{'''label''': '''LABEL_0''', '''score''': 0.5_0_4}] )


      @slow
      @require_torch
      def   a					(self			: List[str] ):





         """simple docstring"""



         __snake_case							=  pipeline('''text-classification''' )

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


      @slow
      @require_tf
      def   a					(self			: List[str] ):





         """simple docstring"""



         __snake_case							=  pipeline('''text-classification'''							,     framework='''tf''' )

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


      def   a					(self			: List[Any]							,     a__			: Any							,     a__			: List[str]							,     a__			: Optional[Any] ):





         """simple docstring"""



         __snake_case							=  TextClassificationPipeline(model=SCREAMING_SNAKE_CASE__							,     tokenizer=SCREAMING_SNAKE_CASE__ )
         return text_classifier, ["HuggingFace is in", "This is another test"]


      def   a					(self			: Optional[int]							,     a__			: Tuple							,     a__			: str ):





         """simple docstring"""



         __snake_case							=  text_classifier.model
         # Small inputs because BartTokenizer tiny has maximum position embeddings = 22
         __snake_case							=  '''HuggingFace is in'''
         __snake_case							=  text_classifier(SCREAMING_SNAKE_CASE__ )

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

         __snake_case							=  ['''HuggingFace is in ''', '''Paris is in France''']
         __snake_case							=  text_classifier(SCREAMING_SNAKE_CASE__ )
         self.assertEqual(
             nested_simplify(SCREAMING_SNAKE_CASE__ )							,     [{'''label''': ANY(SCREAMING_SNAKE_CASE__ ), '''score''': ANY(SCREAMING_SNAKE_CASE__ )}, {'''label''': ANY(SCREAMING_SNAKE_CASE__ ), '''score''': ANY(SCREAMING_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
         __snake_case							=  text_classifier(SCREAMING_SNAKE_CASE__							,     top_k=SCREAMING_SNAKE_CASE__ )
         __snake_case							=  len(model.config.idalabel.values() )
         self.assertEqual(
             nested_simplify(SCREAMING_SNAKE_CASE__ )							,     [[{'''label''': ANY(SCREAMING_SNAKE_CASE__ ), '''score''': ANY(SCREAMING_SNAKE_CASE__ )}] * N, [{'''label''': ANY(SCREAMING_SNAKE_CASE__ ), '''score''': ANY(SCREAMING_SNAKE_CASE__ )}] * N]							,     )

         __snake_case							=  {'''text''': '''HuggingFace is in ''', '''text_pair''': '''Paris is in France'''}
         __snake_case							=  text_classifier(SCREAMING_SNAKE_CASE__ )
         self.assertEqual(
             nested_simplify(SCREAMING_SNAKE_CASE__ )							,     {'''label''': ANY(SCREAMING_SNAKE_CASE__ ), '''score''': ANY(SCREAMING_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.
         __snake_case							=  [['''HuggingFace is in ''', '''Paris is in France''']]
         with self.assertRaises(SCREAMING_SNAKE_CASE__ ):
            text_classifier(SCREAMING_SNAKE_CASE__ )

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






"""simple docstring"""







import torch
import torch.nn as nn
from transformers.modeling_utils import ModuleUtilsMixin
from transformers.models.ta.modeling_ta import TaBlock, TaConfig, TaLayerNorm

from ...configuration_utils import ConfigMixin, register_to_config
from ...models import ModelMixin






class       UpperCamelCase__			(   _lowerCAmelCase       ,		_lowerCAmelCase       ,		_lowerCAmelCase      ):

			"""simple docstring"""


			@register_to_config
			def __init__(      self						,	SCREAMING_SNAKE_CASE__						,	SCREAMING_SNAKE_CASE__						,	SCREAMING_SNAKE_CASE__						,	SCREAMING_SNAKE_CASE__						,	SCREAMING_SNAKE_CASE__						,	SCREAMING_SNAKE_CASE__						,	SCREAMING_SNAKE_CASE__						,	SCREAMING_SNAKE_CASE__						,	SCREAMING_SNAKE_CASE__						,	SCREAMING_SNAKE_CASE__ = False						,	)      ->	Union[str, Any]:
				super().__init__()

				A__											=					nn.Embedding(SCREAMING_SNAKE_CASE__						,	SCREAMING_SNAKE_CASE__		)

				A__											=					nn.Embedding(SCREAMING_SNAKE_CASE__						,	SCREAMING_SNAKE_CASE__		)
				A__											=					False

				A__											=					nn.Dropout(p=SCREAMING_SNAKE_CASE__		)

				A__											=					TaConfig(
				    vocab_size=SCREAMING_SNAKE_CASE__						,	d_model=SCREAMING_SNAKE_CASE__						,	num_heads=SCREAMING_SNAKE_CASE__						,	d_kv=SCREAMING_SNAKE_CASE__						,	d_ff=SCREAMING_SNAKE_CASE__						,	dropout_rate=SCREAMING_SNAKE_CASE__						,	feed_forward_proj=SCREAMING_SNAKE_CASE__						,	is_decoder=SCREAMING_SNAKE_CASE__						,	is_encoder_decoder=SCREAMING_SNAKE_CASE__						,	)

				A__											=					nn.ModuleList()
				for lyr_num in range(SCREAMING_SNAKE_CASE__		):
					A__											=					TaBlock(SCREAMING_SNAKE_CASE__		)
					self.encoders.append(SCREAMING_SNAKE_CASE__		)

				A__											=					TaLayerNorm(SCREAMING_SNAKE_CASE__		)
				A__											=					nn.Dropout(p=SCREAMING_SNAKE_CASE__		)







			def   snake_case__	(      self						,	SCREAMING_SNAKE_CASE__						,	SCREAMING_SNAKE_CASE__		)      ->	Any:
				A__											=					self.token_embedder(SCREAMING_SNAKE_CASE__		)

				A__											=					encoder_input_tokens.shape[1]
				A__											=					torch.arange(SCREAMING_SNAKE_CASE__						,	device=encoder_input_tokens.device		)
				x += self.position_encoding(SCREAMING_SNAKE_CASE__		)

				A__											=					self.dropout_pre(SCREAMING_SNAKE_CASE__		)

				# inverted the attention mask
				A__											=					encoder_input_tokens.size()
				A__											=					self.get_extended_attention_mask(SCREAMING_SNAKE_CASE__						,	SCREAMING_SNAKE_CASE__		)

				for lyr in self.encoders:
					A__											=					lyr(SCREAMING_SNAKE_CASE__						,	SCREAMING_SNAKE_CASE__		)[0]
				A__											=					self.layer_norm(SCREAMING_SNAKE_CASE__		)

				return self.dropout_post(SCREAMING_SNAKE_CASE__		), encoder_inputs_mask




