


import inspect
import unittest

from transformers import ViTHybridConfig
from transformers.testing_utils import require_accelerate, require_torch, require_vision, slow, torch_device
from transformers.utils import cached_property, is_torch_available, is_vision_available

from ...test_configuration_common import ConfigTester
from ...test_modeling_common import ModelTesterMixin, _config_zero_init, floats_tensor, ids_tensor
from ...test_pipeline_mixin import PipelineTesterMixin


if is_torch_available():
	import torch
	from torch import nn

	from transformers import ViTHybridForImageClassification, ViTHybridImageProcessor, ViTHybridModel
	from transformers.models.vit_hybrid.modeling_vit_hybrid import VIT_HYBRID_PRETRAINED_MODEL_ARCHIVE_LIST


if is_vision_available():
	from PIL import Image

class        a	:




							"""simple docstring"""


							def __init__(							self :						Optional[int]					,			lowerCamelCase :						str					,			lowerCamelCase :						str=13					,			lowerCamelCase :						Any=64					,			lowerCamelCase :						List[str]=2					,			lowerCamelCase :						Dict=3					,			lowerCamelCase :						List[Any]=True					,			lowerCamelCase :						Optional[Any]=True					,			lowerCamelCase :						Optional[int]=32					,			lowerCamelCase :						List[str]=5					,			lowerCamelCase :						int=4					,			lowerCamelCase :						Dict=37					,			lowerCamelCase :						List[Any]="gelu"					,			lowerCamelCase :						Any=0.1					,			lowerCamelCase :						Optional[Any]=0.1					,			lowerCamelCase :						int=10					,			lowerCamelCase :						Optional[int]=0.02					,			lowerCamelCase :						Dict=[1, 16, 4, 4]					,			lowerCamelCase :						Optional[int]=None					,			)			->					Tuple:
														__snake_case	:    Any											=     parent
														__snake_case	:    List[str]											=     batch_size
														__snake_case	:    Tuple											=     image_size
														__snake_case	:    List[Any]											=     patch_size
														__snake_case	:    Union[str, Any]											=     num_channels
														__snake_case	:    str											=     is_training
														__snake_case	:    Any											=     use_labels
														__snake_case	:    List[str]											=     hidden_size
														__snake_case	:    Dict											=     num_hidden_layers
														__snake_case	:    Any											=     num_attention_heads
														__snake_case	:    Dict											=     intermediate_size
														__snake_case	:    int											=     hidden_act
														__snake_case	:    Any											=     hidden_dropout_prob
														__snake_case	:    Any											=     attention_probs_dropout_prob
														__snake_case	:    List[Any]											=     type_sequence_label_size
														__snake_case	:    Optional[int]											=     initializer_range
														__snake_case	:    int											=     scope
														__snake_case	:    int											=     backbone_featmap_shape

														# in ViT hybrid, the seq length equals the number of patches + 1 (we add 1 for the [CLS] token)
														# the number of patches is based on the feature map of the backbone, which by default uses an output stride
														# of 32, which means that the feature map has a spatial resolution of 1/32 of the input image size
														__snake_case	:    str											=     (self.image_size // 32) ** 2
														__snake_case	:    Union[str, Any]											=     num_patches + 1

							def 					__snake_case      (							self :						Union[str, Any]       )			->					Any:
														__snake_case	:    Optional[int]											=     floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]       )

														__snake_case	:    Union[str, Any]											=     None
														if self.use_labels:
																					__snake_case	:    Tuple											=     ids_tensor([self.batch_size]					,			self.type_sequence_label_size       )

														__snake_case	:    List[Any]											=     self.get_config()

														return config, pixel_values, labels

							def 					__snake_case      (							self :						int       )			->					Optional[Any]:
														__snake_case	:    List[Any]											=     {
														    "global_padding": "same",
														    "layer_type": "bottleneck",
														    "depths": [3, 4, 9],
														    "out_features": ["stage1", "stage2", "stage3"],
														    "embedding_dynamic_padding": True,
														    "hidden_sizes": [4, 8, 16, 32],
														    "num_groups": 2,
														}

														return ViTHybridConfig(
														    image_size=self.image_size					,			patch_size=self.patch_size					,			num_channels=self.num_channels					,			hidden_size=self.hidden_size					,			num_hidden_layers=self.num_hidden_layers					,			num_attention_heads=self.num_attention_heads					,			intermediate_size=self.intermediate_size					,			hidden_act=self.hidden_act					,			hidden_dropout_prob=self.hidden_dropout_prob					,			attention_probs_dropout_prob=self.attention_probs_dropout_prob					,			is_decoder=__UpperCamelCase					,			initializer_range=self.initializer_range					,			backbone_featmap_shape=self.backbone_featmap_shape					,			backbone_config=__UpperCamelCase					,			)

							def 					__snake_case      (							self :						int					,			lowerCamelCase :						List[Any]					,			lowerCamelCase :						List[Any]					,			lowerCamelCase :						Union[str, Any]       )			->					Union[str, Any]:
														__snake_case	:    Optional[Any]											=     ViTHybridModel(config=__UpperCamelCase       )
														model.to(__UpperCamelCase       )
														model.eval()
														__snake_case	:    Optional[Any]											=     model(__UpperCamelCase       )
														self.parent.assertEqual(result.last_hidden_state.shape					,			(self.batch_size, self.seq_length, self.hidden_size)       )

							def 					__snake_case      (							self :						Any					,			lowerCamelCase :						str					,			lowerCamelCase :						int					,			lowerCamelCase :						Dict       )			->					Dict:
														__snake_case	:    str											=     self.type_sequence_label_size
														__snake_case	:    Dict											=     ViTHybridForImageClassification(__UpperCamelCase       )
														model.to(__UpperCamelCase       )
														model.eval()
														__snake_case	:    int											=     model(__UpperCamelCase					,			labels=__UpperCamelCase       )
														self.parent.assertEqual(result.logits.shape					,			(self.batch_size, self.type_sequence_label_size)       )






							def 					__snake_case      (							self :						int       )			->					str:
														__snake_case	:    Dict											=     self.prepare_config_and_inputs()
														__snake_case				,    __snake_case				,    __snake_case	:    Dict											=     config_and_inputs
														__snake_case	:    List[Any]											=     {"pixel_values": pixel_values}
														return config, inputs_dict

@require_torch
class        a	(_lowerCAmelCase  ,       _lowerCAmelCase  ,       unittest.TestCase   ):




							"""simple docstring"""


							__UpperCAmelCase					:							Dict  						=						(ViTHybridModel, ViTHybridForImageClassification) if is_torch_available() else ()
							__UpperCAmelCase					:							Union[str, Any]  						=						(
							    {"feature-extraction": ViTHybridModel, "image-classification": ViTHybridForImageClassification}
							    if is_torch_available()
							    else {}
							)
							__UpperCAmelCase					:							Any  						=						False
							__UpperCAmelCase					:							List[str]  						=						False
							__UpperCAmelCase					:							Optional[Any]  						=						False

							def 					__snake_case      (							self :						List[str]       )			->					Any:
														__snake_case	:    Any											=     ViTHybridModelTester(self       )
														__snake_case	:    Tuple											=     ConfigTester(self					,			config_class=__UpperCamelCase					,			has_text_modality=__UpperCamelCase					,			hidden_size=37       )

							def 					__snake_case      (							self :						Tuple       )			->					Optional[Any]:
														self.config_tester.run_common_tests()

							@unittest.skip(reason="ViT does not use inputs_embeds"       )
							def 					__snake_case      (							self :						Any       )			->					Any:
														pass

							def 					__snake_case      (							self :						Tuple       )			->					int:
														__snake_case				,    __snake_case	:    Optional[Any]											=     self.model_tester.prepare_config_and_inputs_for_common()

														for model_class in self.all_model_classes:
																					__snake_case	:    int											=     model_class(__UpperCamelCase       )
																					self.assertIsInstance(model.get_input_embeddings()					,			(nn.Module)       )
																					__snake_case	:    List[str]											=     model.get_output_embeddings()
																					self.assertTrue(x is None or isinstance(__UpperCamelCase					,			nn.Linear       )       )

							def 					__snake_case      (							self :						Optional[int]       )			->					Any:
														__snake_case				,    __snake_case	:    Dict											=     self.model_tester.prepare_config_and_inputs_for_common()

														for model_class in self.all_model_classes:
																					__snake_case	:    Optional[int]											=     model_class(__UpperCamelCase       )
																					__snake_case	:    int											=     inspect.signature(model.forward       )
																					# signature.parameters is an OrderedDict => so arg_names order is deterministic
																					__snake_case	:    Optional[Any]											=     [*signature.parameters.keys()]

																					__snake_case	:    int											=     ["pixel_values"]
																					self.assertListEqual(arg_names[:1]					,			__UpperCamelCase       )

							def 					__snake_case      (							self :						int       )			->					int:
														__snake_case	:    Tuple											=     self.model_tester.prepare_config_and_inputs()
														self.model_tester.create_and_check_model(*__UpperCamelCase       )

							def 					__snake_case      (							self :						Any       )			->					Optional[Any]:
														__snake_case	:    Optional[Any]											=     self.model_tester.prepare_config_and_inputs()
														self.model_tester.create_and_check_for_image_classification(*__UpperCamelCase       )

							def 					__snake_case      (							self :						str       )			->					Tuple:
														__snake_case				,    __snake_case	:    str											=     self.model_tester.prepare_config_and_inputs_for_common()

														__snake_case	:    int											=     _config_zero_init(__UpperCamelCase       )
														for model_class in self.all_model_classes:
																					__snake_case	:    Tuple											=     model_class(config=__UpperCamelCase       )
																					# Skip the check for the backbone
																					for name, module in model.named_modules():
																												if module.__class__.__name__ == "ViTHybridPatchEmbeddings":
																																			__snake_case	:    Dict											=     [F'{name}.{key}' for key in module.state_dict().keys()]
																																			break

																					for name, param in model.named_parameters():
																												if param.requires_grad:
																																			if name in backbone_params:
																																										continue
																																			self.assertIn(
																																			    ((param.data.mean() * 1E9).round() / 1E9).item()					,			[0.0, 1.0]					,			msg=F'Parameter {name} of model {model_class} seems not properly initialized'					,			)






							@slow
							def 					__snake_case      (							self :						List[str]       )			->					Optional[Any]:
														for model_name in VIT_HYBRID_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
																					__snake_case	:    Optional[Any]											=     ViTHybridModel.from_pretrained(__UpperCamelCase       )
																					self.assertIsNotNone(__UpperCamelCase       )







def        lowerCAmelCase_  (      ):
							__snake_case	:    Tuple											=     Image.open("./tests/fixtures/tests_samples/COCO/000000039769.png"			)
							return image

@require_torch
@require_vision
class        a	(unittest.TestCase   ):




							"""simple docstring"""


							@cached_property
							def 					__snake_case      (							self :						Dict       )			->					Tuple:
														return (
														    ViTHybridImageProcessor.from_pretrained(VIT_HYBRID_PRETRAINED_MODEL_ARCHIVE_LIST[0]       )
														    if is_vision_available()
														    else None
														)

							@slow
							def 					__snake_case      (							self :						int       )			->					List[Any]:
														__snake_case	:    List[Any]											=     ViTHybridForImageClassification.from_pretrained(VIT_HYBRID_PRETRAINED_MODEL_ARCHIVE_LIST[0]       ).to(
														    __UpperCamelCase       )

														__snake_case	:    Any											=     self.default_image_processor
														__snake_case	:    Any											=     prepare_img()
														__snake_case	:    Optional[int]											=     image_processor(images=__UpperCamelCase					,			return_tensors="pt"       ).to(__UpperCamelCase       )

														# forward pass
														with torch.no_grad():
																					__snake_case	:    int											=     model(**__UpperCamelCase       )

														# verify the logits
														__snake_case	:    List[Any]											=     torch.Size((1, 1000)       )
														self.assertEqual(outputs.logits.shape					,			__UpperCamelCase       )

														__snake_case	:    str											=     torch.tensor([-1.90_90, -0.49_93, -0.23_89]       ).to(__UpperCamelCase       )

														self.assertTrue(torch.allclose(outputs.logits[0, :3]					,			__UpperCamelCase					,			atol=1E-4       )       )






							@slow
							@require_accelerate
							def 					__snake_case      (							self :						str       )			->					Optional[Any]:
														__snake_case	:    Optional[int]											=     ViTHybridImageProcessor.from_pretrained("google/vit-hybrid-base-bit-384"       )
														__snake_case	:    str											=     ViTHybridForImageClassification.from_pretrained("google/vit-hybrid-base-bit-384"					,			device_map="auto"       )

														__snake_case	:    Any											=     prepare_img()

														__snake_case	:    Tuple											=     image_processor(images=__UpperCamelCase					,			return_tensors="pt"       )
														__snake_case	:    Tuple											=     model(**__UpperCamelCase       )
														__snake_case	:    Union[str, Any]											=     outputs.logits
														# model predicts one of the 1000 ImageNet classes
														__snake_case	:    List[str]											=     logits.argmax(-1       ).item()

														self.assertTrue(model.config.idalabel[predicted_class_idx]					,			"tabby, tabby cat"       )





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()       )

