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





@is_pipeline_test
class  snake_case     (							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 _lowercase					(self    ):

									"""simple docstring"""







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

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

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

									SCREAMING_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_04}, {'''label''': '''LABEL_1''', '''score''': 0.4_96}],
									        [{'''label''': '''LABEL_0''', '''score''': 0.5_04}, {'''label''': '''LABEL_1''', '''score''': 0.4_96}],
									    ]       ,       )

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

									# Legacy behavior
									SCREAMING_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_04}]    )

									SCREAMING_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_04}, {'''label''': '''LABEL_1''', '''score''': 0.4_96}]]    )

									SCREAMING_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_04}, {'''label''': '''LABEL_1''', '''score''': 0.4_96}],
									        [{'''label''': '''LABEL_0''', '''score''': 0.5_04}, {'''label''': '''LABEL_1''', '''score''': 0.4_96}],
									    ]       ,       )

									SCREAMING_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_04},
									        {'''label''': '''LABEL_0''', '''score''': 0.5_04},
									    ]       ,       )

							@require_torch
							def _lowercase					(self    ):

									"""simple docstring"""







									import torch

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

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

							@require_tf
							def _lowercase					(self    ):

									"""simple docstring"""







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

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

							@slow
							@require_torch
							def _lowercase					(self    ):

									"""simple docstring"""







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

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

							@slow
							@require_tf
							def _lowercase					(self    ):

									"""simple docstring"""







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

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

							def _lowercase					(self       ,       SCREAMING_SNAKE_CASE_       ,       SCREAMING_SNAKE_CASE_       ,       SCREAMING_SNAKE_CASE_    ):

									"""simple docstring"""







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







							def _lowercase					(self       ,       SCREAMING_SNAKE_CASE_       ,       SCREAMING_SNAKE_CASE_    ):

									"""simple docstring"""







									SCREAMING_SNAKE_CASE_						 = text_classifier.model
									# Small inputs because BartTokenizer tiny has maximum position embeddings = 22
									SCREAMING_SNAKE_CASE_						 = '''HuggingFace is in'''
									SCREAMING_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()    )

									SCREAMING_SNAKE_CASE_						 = ['''HuggingFace is in ''', '''Paris is in France''']
									SCREAMING_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
									SCREAMING_SNAKE_CASE_						 = text_classifier(SCREAMING_SNAKE_CASE_       ,       top_k=SCREAMING_SNAKE_CASE_    )
									SCREAMING_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]       ,       )

									SCREAMING_SNAKE_CASE_						 = {'''text''': '''HuggingFace is in ''', '''text_pair''': '''Paris is in France'''}
									SCREAMING_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.
									SCREAMING_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
									SCREAMING_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 os
import socket
from contextlib import contextmanager

import torch

from ..commands.config.default import write_basic_config  # noqa: F401
from ..state import PartialState
from .dataclasses import DistributedType
from .imports import is_deepspeed_available, is_tpu_available
from .transformer_engine import convert_model
from .versions import is_torch_version


if is_deepspeed_available():
						from deepspeed import DeepSpeedEngine

if is_tpu_available(check_device=False):
						import torch_xla.core.xla_model as xm



def        _lowerCamelCase ( __a    ):
		if is_torch_version('''<''',							'''2.0.0'''    ) or not hasattr(__a,							'''_dynamo'''    ):
				return False
		return isinstance(__a,							torch._dynamo.eval_frame.OptimizedModule    )



def        _lowerCamelCase ( __a,							__a = True    ):
		SCREAMING_SNAKE_CASE_						 = (torch.nn.parallel.DistributedDataParallel, torch.nn.DataParallel)

		SCREAMING_SNAKE_CASE_						 = is_compiled_module(__a    )
		if is_compiled:
				SCREAMING_SNAKE_CASE_						 = model
				SCREAMING_SNAKE_CASE_						 = model._orig_mod

		if is_deepspeed_available():
				options += (DeepSpeedEngine,)

		while isinstance(__a,							__a    ):
				SCREAMING_SNAKE_CASE_						 = model.module

		if not keep_fpaa_wrapper:
				SCREAMING_SNAKE_CASE_						 = getattr(__a,							'''forward'''    )
				SCREAMING_SNAKE_CASE_						 = model.__dict__.pop('''_original_forward''',							__a    )
				if original_forward is not None:
						while hasattr(__a,							'''__wrapped__'''    ):
								SCREAMING_SNAKE_CASE_						 = forward.__wrapped__
								if forward == original_forward:
										break
						SCREAMING_SNAKE_CASE_						 = forward
				if getattr(__a,							'''_converted_to_transformer_engine''',							__a    ):
						convert_model(__a,							to_transformer_engine=__a    )

		if is_compiled:
				SCREAMING_SNAKE_CASE_						 = model
				SCREAMING_SNAKE_CASE_						 = compiled_model

		return model



def        _lowerCamelCase ( ):
		PartialState().wait_for_everyone()



def        _lowerCamelCase ( __a,							__a    ):
		if PartialState().distributed_type == DistributedType.TPU:
				xm.save(__a,							__a    )
		elif PartialState().local_process_index == 0:
				torch.save(__a,							__a    )



@contextmanager
def        _lowerCamelCase ( **__a    ):
		for key, value in kwargs.items():
				SCREAMING_SNAKE_CASE_						 = str(__a    )

		yield

		for key in kwargs:
				if key.upper() in os.environ:
						del os.environ[key.upper()]



def        _lowerCamelCase ( __a    ):
		if not hasattr(__a,							'''__qualname__'''    ) and not hasattr(__a,							'''__name__'''    ):
				SCREAMING_SNAKE_CASE_						 = getattr(__a,							'''__class__''',							__a    )
		if hasattr(__a,							'''__qualname__'''    ):
				return obj.__qualname__
		if hasattr(__a,							'''__name__'''    ):
				return obj.__name__
		return str(__a    )



def        _lowerCamelCase ( __a,							__a    ):
		for key, value in source.items():
				if isinstance(__a,							__a    ):
						SCREAMING_SNAKE_CASE_						 = destination.setdefault(__a,							{}    )
						merge_dicts(__a,							__a    )
				else:
						SCREAMING_SNAKE_CASE_						 = value

		return destination



def        _lowerCamelCase ( __a = None    ):
		if port is None:
				SCREAMING_SNAKE_CASE_						 = 29_500
		with socket.socket(socket.AF_INET,							socket.SOCK_STREAM    ) as s:
				return s.connect_ex(('''localhost''', port)    ) == 0