


from ...configuration_utils import PretrainedConfig
from ...utils import logging
from ...utils.backbone_utils import BackboneConfigMixin, get_aligned_output_features_output_indices


_snake_case  :	List[str]			      =       logging.get_logger(__name__)

_snake_case  :	int			      =       {
    "shi-labs/nat-mini-in1k-224": "https://huggingface.co/shi-labs/nat-mini-in1k-224/resolve/main/config.json",
    # See all Nat models at https://huggingface.co/models?filter=nat
}

class        a	(_lowerCAmelCase  ,       _lowerCAmelCase   ):




							"""simple docstring"""


							__UpperCAmelCase					:							Tuple  						=						"nat"

							__UpperCAmelCase					:							Any  						=						{
							    "num_attention_heads": "num_heads",
							    "num_hidden_layers": "num_layers",
							}

							def __init__(							self :						Any					,			lowerCamelCase :						List[Any]=4					,			lowerCamelCase :						str=3					,			lowerCamelCase :						int=64					,			lowerCamelCase :						Union[str, Any]=[3, 4, 6, 5]					,			lowerCamelCase :						Dict=[2, 4, 8, 16]					,			lowerCamelCase :						Tuple=7					,			lowerCamelCase :						Dict=3.0					,			lowerCamelCase :						Tuple=True					,			lowerCamelCase :						Dict=0.0					,			lowerCamelCase :						Union[str, Any]=0.0					,			lowerCamelCase :						int=0.1					,			lowerCamelCase :						Tuple="gelu"					,			lowerCamelCase :						Optional[int]=0.02					,			lowerCamelCase :						List[str]=1E-5					,			lowerCamelCase :						Optional[int]=0.0					,			lowerCamelCase :						Any=None					,			lowerCamelCase :						List[str]=None					,			**lowerCamelCase :						List[str]					,			)			->					Dict:
														super().__init__(**lowerCamelCase       )

														__snake_case	:    Dict											=     patch_size
														__snake_case	:    str											=     num_channels
														__snake_case	:    List[str]											=     embed_dim
														__snake_case	:    Tuple											=     depths
														__snake_case	:    Union[str, Any]											=     len(lowerCamelCase       )
														__snake_case	:    int											=     num_heads
														__snake_case	:    Optional[Any]											=     kernel_size
														__snake_case	:    Dict											=     mlp_ratio
														__snake_case	:    List[str]											=     qkv_bias
														__snake_case	:    Optional[Any]											=     hidden_dropout_prob
														__snake_case	:    Tuple											=     attention_probs_dropout_prob
														__snake_case	:    Tuple											=     drop_path_rate
														__snake_case	:    Optional[Any]											=     hidden_act
														__snake_case	:    Any											=     layer_norm_eps
														__snake_case	:    List[str]											=     initializer_range
														# we set the hidden_size attribute in order to make Nat work with VisionEncoderDecoderModel
														# this indicates the channel dimension after the last stage of the model
														__snake_case	:    Dict											=     int(embed_dim * 2 ** (len(lowerCamelCase       ) - 1)       )
														__snake_case	:    Optional[Any]											=     layer_scale_init_value
														__snake_case	:    List[Any]											=     ["stem"] + [F'stage{idx}' for idx in range(1					,			len(lowerCamelCase       ) + 1       )]
														__snake_case				,    __snake_case	:    List[Any]											=     get_aligned_output_features_output_indices(
														    out_features=lowerCamelCase					,			out_indices=lowerCamelCase					,			stage_names=self.stage_names       )





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  :	Dict			      =       {"LayoutLMv2Config", "LayoutLMv3Config"}

@is_pipeline_test
class        a	(unittest.TestCase   ):




							"""simple docstring"""


							__UpperCAmelCase					:							Union[str, Any]  						=						MODEL_FOR_SEQUENCE_CLASSIFICATION_MAPPING
							__UpperCAmelCase					:							Optional[int]  						=						TF_MODEL_FOR_SEQUENCE_CLASSIFICATION_MAPPING

							if model_mapping is not None:
														__UpperCAmelCase					:							Optional[int]  						=						{config: model for config, model in model_mapping.items() if config.__name__ not in _TO_SKIP}
							if tf_model_mapping is not None:
														__UpperCAmelCase					:							Any  						=						{
														    config: model for config, model in tf_model_mapping.items() if config.__name__ not in _TO_SKIP
														}

							@require_torch
							def 					__snake_case      (							self :						str       )			->					str:
														__snake_case	:    Dict											=     pipeline(
														    task="text-classification"					,			model="hf-internal-testing/tiny-random-distilbert"					,			framework="pt"       )

														__snake_case	:    Optional[Any]											=     text_classifier("This is great !"       )
														self.assertEqual(nested_simplify(lowerCamelCase       )					,			[{"label": "LABEL_0", "score": 0.5_04}]       )

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

														__snake_case	:    int											=     text_classifier(["This is great !", "This is bad"]					,			top_k=2       )
														self.assertEqual(
														    nested_simplify(lowerCamelCase       )					,			[
														        [{"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}],
														    ]					,			)

														__snake_case	:    List[str]											=     text_classifier("This is great !"					,			top_k=1       )
														self.assertEqual(nested_simplify(lowerCamelCase       )					,			[{"label": "LABEL_0", "score": 0.5_04}]       )

														# Legacy behavior
														__snake_case	:    Dict											=     text_classifier("This is great !"					,			return_all_scores=lowerCamelCase       )
														self.assertEqual(nested_simplify(lowerCamelCase       )					,			[{"label": "LABEL_0", "score": 0.5_04}]       )

														__snake_case	:    Any											=     text_classifier("This is great !"					,			return_all_scores=lowerCamelCase       )
														self.assertEqual(
														    nested_simplify(lowerCamelCase       )					,			[[{"label": "LABEL_0", "score": 0.5_04}, {"label": "LABEL_1", "score": 0.4_96}]]       )

														__snake_case	:    Tuple											=     text_classifier(["This is great !", "Something else"]					,			return_all_scores=lowerCamelCase       )
														self.assertEqual(
														    nested_simplify(lowerCamelCase       )					,			[
														        [{"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}],
														    ]					,			)

														__snake_case	:    Tuple											=     text_classifier(["This is great !", "Something else"]					,			return_all_scores=lowerCamelCase       )
														self.assertEqual(
														    nested_simplify(lowerCamelCase       )					,			[
														        {"label": "LABEL_0", "score": 0.5_04},
														        {"label": "LABEL_0", "score": 0.5_04},
														    ]					,			)

							@require_torch
							def 					__snake_case      (							self :						Optional[int]       )			->					List[Any]:
														import torch

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

														__snake_case	:    Optional[int]											=     text_classifier("This is great !"       )
														self.assertEqual(nested_simplify(lowerCamelCase       )					,			[{"label": "LABEL_0", "score": 0.5_04}]       )

							@require_tf
							def 					__snake_case      (							self :						Any       )			->					Tuple:
														__snake_case	:    List[Any]											=     pipeline(
														    task="text-classification"					,			model="hf-internal-testing/tiny-random-distilbert"					,			framework="tf"       )

														__snake_case	:    str											=     text_classifier("This is great !"       )
														self.assertEqual(nested_simplify(lowerCamelCase       )					,			[{"label": "LABEL_0", "score": 0.5_04}]       )

							@slow
							@require_torch
							def 					__snake_case      (							self :						int       )			->					int:
														__snake_case	:    Dict											=     pipeline("text-classification"       )

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

							@slow
							@require_tf
							def 					__snake_case      (							self :						List[Any]       )			->					str:
														__snake_case	:    Optional[Any]											=     pipeline("text-classification"					,			framework="tf"       )

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

							def 					__snake_case      (							self :						str					,			lowerCamelCase :						Tuple					,			lowerCamelCase :						Any					,			lowerCamelCase :						Union[str, Any]       )			->					List[Any]:
														__snake_case	:    Union[str, Any]											=     TextClassificationPipeline(model=lowerCamelCase					,			tokenizer=lowerCamelCase       )
														return text_classifier, ["HuggingFace is in", "This is another test"]






							def 					__snake_case      (							self :						List[str]					,			lowerCamelCase :						Optional[Any]					,			lowerCamelCase :						Tuple       )			->					str:
														__snake_case	:    Tuple											=     text_classifier.model
														# Small inputs because BartTokenizer tiny has maximum position embeddings = 22
														__snake_case	:    Any											=     "HuggingFace is in"
														__snake_case	:    Tuple											=     text_classifier(lowerCamelCase       )

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

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

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

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

