



import copy
import inspect
import unittest

from transformers import AutoBackbone
from transformers.configuration_utils import PretrainedConfig
from transformers.testing_utils import require_timm, require_torch, torch_device
from transformers.utils.import_utils import is_torch_available

from ...test_backbone_common import BackboneTesterMixin
from ...test_configuration_common import ConfigTester
from ...test_modeling_common import ModelTesterMixin, floats_tensor


if is_torch_available():
							import torch

							from transformers import TimmBackbone, TimmBackboneConfig

from ...test_pipeline_mixin import PipelineTesterMixin
class 				_snake_case			:




				def __init__(						self					:						Dict,					__lowercase					:						Tuple,					__lowercase					:						Tuple=None,					__lowercase					:						str=None,					__lowercase					:						List[str]=None,					__lowercase					:						Dict="resnet50",					__lowercase					:						Tuple=3,					__lowercase					:						Optional[int]=32,					__lowercase					:						List[str]=3,					__lowercase					:						Dict=True,					__lowercase					:						List[str]=True,					):
							lowercase__					  =				parent
							lowercase__					  =				out_indices if out_indices is not None else [4]
							lowercase__					  =				stage_names
							lowercase__					  =				out_features
							lowercase__					  =				backbone
							lowercase__					  =				batch_size
							lowercase__					  =				image_size
							lowercase__					  =				num_channels
							lowercase__					  =				use_pretrained_backbone
							lowercase__					  =				is_training




				def 							A__      (						self					:						Optional[Any]						):
							lowercase__					  =				floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]						)
							lowercase__					  =				self.get_config()

							return config, pixel_values




				def 							A__      (						self					:						int						):
							return TimmBackboneConfig(
							    image_size=self.image_size,					num_channels=self.num_channels,					out_features=self.out_features,					out_indices=self.out_indices,					stage_names=self.stage_names,					use_pretrained_backbone=self.use_pretrained_backbone,					backbone=self.backbone,					)




				def 							A__      (						self					:						str,					__lowercase					:						Dict,					__lowercase					:						Tuple						):
							lowercase__					  =				TimmBackbone(config=__lowercase						)
							model.to(__lowercase						)
							model.eval()
							with torch.no_grad():
										lowercase__					  =				model(__lowercase						)
							self.parent.assertEqual(
							    result.feature_map[-1].shape,					(self.batch_size, model.channels[-1], 14, 14),					)




				def 							A__      (						self					:						Any						):
							lowercase__					  =				self.prepare_config_and_inputs()
							lowercase__  ,       lowercase__					  =				config_and_inputs
							lowercase__					  =				{"pixel_values": pixel_values}
							return config, inputs_dict
@require_torch
@require_timm
class 				_snake_case			(					lowercase__    ,	lowercase__    ,	lowercase__    ,	unittest.TestCase):
				UpperCamelCase__				:					Tuple     		=(TimmBackbone,) if is_torch_available() else ()
				UpperCamelCase__				:					str     		={"""feature-extraction""": TimmBackbone} if is_torch_available() else {}
				UpperCamelCase__				:					str     		=False
				UpperCamelCase__				:					Tuple     		=False
				UpperCamelCase__				:					Union[str, Any]     		=False
				UpperCamelCase__				:					Any     		=False




				def 							A__      (						self					:						List[Any]						):
							lowercase__					  =				TimmBackboneModelTester(self						)
							lowercase__					  =				ConfigTester(self,					config_class=__lowercase,					has_text_modality=__lowercase						)




				def 							A__      (						self					:						Union[str, Any]						):
							self.config_tester.create_and_test_config_to_json_string()
							self.config_tester.create_and_test_config_to_json_file()
							self.config_tester.create_and_test_config_from_and_save_pretrained()
							self.config_tester.create_and_test_config_with_num_labels()
							self.config_tester.check_config_can_be_init_without_params()
							self.config_tester.check_config_arguments_init()




				def 							A__      (						self					:						Optional[int]						):
							lowercase__					  =				"resnet18"
							lowercase__					  =				"microsoft/resnet-18"

							lowercase__					  =				AutoBackbone.from_pretrained(__lowercase,					use_timm_backbone=__lowercase						)
							lowercase__					  =				AutoBackbone.from_pretrained(__lowercase						)

							self.assertEqual(len(timm_model.out_features						),					len(transformers_model.out_features						)						)
							self.assertEqual(len(timm_model.stage_names						),					len(transformers_model.stage_names						)						)
							self.assertEqual(timm_model.channels,					transformers_model.channels						)
							# Out indices are set to the last layer by default. For timm models, we don't know
							# the number of layers in advance, so we set it to (-1,), whereas for transformers
							# models, we set it to [len(stage_names) - 1] (kept for backward compatibility).
							self.assertEqual(timm_model.out_indices,					(-1,)						)
							self.assertEqual(transformers_model.out_indices,					[len(timm_model.stage_names						) - 1]						)

							lowercase__					  =				AutoBackbone.from_pretrained(__lowercase,					use_timm_backbone=__lowercase,					out_indices=[1, 2, 3]						)
							lowercase__					  =				AutoBackbone.from_pretrained(__lowercase,					out_indices=[1, 2, 3]						)

							self.assertEqual(timm_model.out_indices,					transformers_model.out_indices						)
							self.assertEqual(len(timm_model.out_features						),					len(transformers_model.out_features						)						)
							self.assertEqual(timm_model.channels,					transformers_model.channels						)




				@unittest.skip("TimmBackbone doesn't support feed forward chunking"						)
				def 							A__      (						self					:						Optional[Any]						):
							pass




				@unittest.skip("TimmBackbone doesn't have num_hidden_layers attribute"						)
				def 							A__      (						self					:						Optional[int]						):
							pass




				@unittest.skip("TimmBackbone initialization is managed on the timm side"						)
				def 							A__      (						self					:						Optional[int]						):
							pass




				@unittest.skip("TimmBackbone models doesn't have inputs_embeds"						)
				def 							A__      (						self					:						Optional[Any]						):
							pass




				@unittest.skip("TimmBackbone models doesn't have inputs_embeds"						)
				def 							A__      (						self					:						Dict						):
							pass




				@unittest.skip("TimmBackbone model cannot be created without specifying a backbone checkpoint"						)
				def 							A__      (						self					:						Optional[int]						):
							pass




				@unittest.skip("Only checkpoints on timm can be loaded into TimmBackbone"						)
				def 							A__      (						self					:						Dict						):
							pass




				@unittest.skip("model weights aren't tied in TimmBackbone."						)
				def 							A__      (						self					:						Union[str, Any]						):
							pass




				@unittest.skip("model weights aren't tied in TimmBackbone."						)
				def 							A__      (						self					:						List[Any]						):
							pass




				@unittest.skip("Only checkpoints on timm can be loaded into TimmBackbone"						)
				def 							A__      (						self					:						str						):
							pass




				@unittest.skip("Only checkpoints on timm can be loaded into TimmBackbone"						)
				def 							A__      (						self					:						List[str]						):
							pass




				@unittest.skip("TimmBackbone doesn't have hidden size info in its configuration."						)
				def 							A__      (						self					:						Any						):
							pass




				@unittest.skip("TimmBackbone doesn't support output_attentions."						)
				def 							A__      (						self					:						Dict						):
							pass




				@unittest.skip("Safetensors is not supported by timm."						)
				def 							A__      (						self					:						Optional[int]						):
							pass




				@unittest.skip("Will be fixed soon by reducing the size of the model used for common tests."						)
				def 							A__      (						self					:						Dict						):
							pass




				def 							A__      (						self					:						Tuple						):
							lowercase__  ,       lowercase__					  =				self.model_tester.prepare_config_and_inputs_for_common()

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

										lowercase__					  =				["pixel_values"]
										self.assertListEqual(arg_names[:1],					__lowercase						)




				def 							A__      (						self					:						Optional[Any]						):
							lowercase__  ,       lowercase__					  =				self.model_tester.prepare_config_and_inputs_for_common()
							lowercase__					  =				True
							lowercase__					  =				self.has_attentions

							# no need to test all models as different heads yield the same functionality
							lowercase__					  =				self.all_model_classes[0]
							lowercase__					  =				model_class(__lowercase						)
							model.to(__lowercase						)

							lowercase__					  =				self._prepare_for_class(__lowercase,					__lowercase						)
							lowercase__					  =				model(**__lowercase						)
							lowercase__					  =				outputs[0][-1]

							# Encoder-/Decoder-only models
							lowercase__					  =				outputs.hidden_states[0]
							hidden_states.retain_grad()

							if self.has_attentions:
										lowercase__					  =				outputs.attentions[0]
										attentions.retain_grad()

							output.flatten()[0].backward(retain_graph=__lowercase						)

							self.assertIsNotNone(hidden_states.grad						)

							if self.has_attentions:
										self.assertIsNotNone(attentions.grad						)




				def 							A__      (						self					:						List[str]						):
							lowercase__  ,       lowercase__					  =				self.model_tester.prepare_config_and_inputs_for_common()

							for model_class in self.all_model_classes:
										lowercase__					  =				model_class(__lowercase						)
										model.to(__lowercase						)
										model.eval()
										lowercase__					  =				model(**__lowercase						)

										self.assertEqual(len(result.feature_maps						),					len(config.out_indices						)						)
										self.assertEqual(len(model.channels						),					len(config.out_indices						)						)

										# Check output of last stage is taken if out_features=None, out_indices=None
										lowercase__					  =				copy.deepcopy(__lowercase						)
										lowercase__					  =				None
										lowercase__					  =				model_class(__lowercase						)
										model.to(__lowercase						)
										model.eval()
										lowercase__					  =				model(**__lowercase						)

										self.assertEqual(len(result.feature_maps						),					1						)
										self.assertEqual(len(model.channels						),					1						)

										# Check backbone can be initialized with fresh weights
										lowercase__					  =				copy.deepcopy(__lowercase						)
										lowercase__					  =				False
										lowercase__					  =				model_class(__lowercase						)
										model.to(__lowercase						)
										model.eval()
										lowercase__					  =				model(**__lowercase						)









import unittest

import numpy as np
import requests

from transformers.testing_utils import require_torch, require_vision
from transformers.utils import is_torch_available, is_vision_available

from ...test_image_processing_common import ImageProcessingSavingTestMixin, prepare_image_inputs


if is_torch_available():
							import torch

							from transformers.pytorch_utils import is_torch_greater_or_equal_than_1_11
else:
							lowercase_								=     False

if is_vision_available():
							from PIL import Image

							from transformers import PixaStructImageProcessor
class 				_snake_case			(					unittest.TestCase):




				def __init__(						self					:						List[Any],					__lowercase					:						int,					__lowercase					:						Optional[int]=7,					__lowercase					:						List[str]=3,					__lowercase					:						Tuple=18,					__lowercase					:						List[Any]=30,					__lowercase					:						Tuple=400,					__lowercase					:						Any=None,					__lowercase					:						Optional[int]=True,					__lowercase					:						List[str]=True,					__lowercase					:						Union[str, Any]=None,					):
							lowercase__					  =				size if size is not None else {"height": 20, "width": 20}
							lowercase__					  =				parent
							lowercase__					  =				batch_size
							lowercase__					  =				num_channels
							lowercase__					  =				image_size
							lowercase__					  =				min_resolution
							lowercase__					  =				max_resolution
							lowercase__					  =				size
							lowercase__					  =				do_normalize
							lowercase__					  =				do_convert_rgb
							lowercase__					  =				[512, 1024, 2048, 4096]
							lowercase__					  =				patch_size if patch_size is not None else {"height": 16, "width": 16}




				def 							A__      (						self					:						List[str]						):
							return {"do_normalize": self.do_normalize, "do_convert_rgb": self.do_convert_rgb}




				def 							A__      (						self					:						Any						):
							lowercase__					  =				"https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/transformers/tasks/australia.jpg"
							lowercase__					  =				Image.open(requests.get(__lowercase,					stream=__lowercase						).raw						).convert("RGB"						)
							return raw_image
@unittest.skipIf(
    not is_torch_greater_or_equal_than_1_11    ,	reason="""`Pix2StructImageProcessor` requires `torch>=1.11.0`."""    ,	)
@require_torch
@require_vision
class 				_snake_case			(					lowercase__    ,	unittest.TestCase):
				UpperCamelCase__				:					Any     		=PixaStructImageProcessor if is_vision_available() else None




				def 							A__      (						self					:						Any						):
							lowercase__					  =				PixaStructImageProcessingTester(self						)




				@property
				def 							A__      (						self					:						Union[str, Any]						):
							return self.image_processor_tester.prepare_image_processor_dict()




				def 							A__      (						self					:						Optional[Any]						):
							lowercase__					  =				self.image_processing_class(**self.image_processor_dict						)
							self.assertTrue(hasattr(__lowercase,					"do_normalize"						)						)
							self.assertTrue(hasattr(__lowercase,					"do_convert_rgb"						)						)




				def 							A__      (						self					:						Optional[int]						):
							lowercase__					  =				self.image_processor_tester.prepare_dummy_image()

							lowercase__					  =				self.image_processing_class(**self.image_processor_dict						)
							lowercase__					  =				2048

							lowercase__					  =				image_processor(__lowercase,					return_tensors="pt",					max_patches=__lowercase						)
							self.assertTrue(torch.allclose(inputs.flattened_patches.mean(),					torch.tensor(0.0606						),					atol=1e-3,					rtol=1e-3						)						)




				def 							A__      (						self					:						Union[str, Any]						):
							# Initialize image_processor
							lowercase__					  =				self.image_processing_class(**self.image_processor_dict						)
							# create random PIL images
							lowercase__					  =				prepare_image_inputs(self.image_processor_tester,					equal_resolution=__lowercase						)
							for image in image_inputs:
										self.assertIsInstance(__lowercase,					Image.Image						)

							# Test not batched input
							lowercase__					  =				(
							    (self.image_processor_tester.patch_size["height"] * self.image_processor_tester.patch_size["width"])
							    * self.image_processor_tester.num_channels
							) + 2

							for max_patch in self.image_processor_tester.max_patches:
										# Test not batched input
										lowercase__					  =				image_processor(
										    image_inputs[0],					return_tensors="pt",					max_patches=__lowercase						).flattened_patches
										self.assertEqual(
										    encoded_images.shape,					(1, max_patch, expected_hidden_dim),					)

										# Test batched
										lowercase__					  =				image_processor(
										    __lowercase,					return_tensors="pt",					max_patches=__lowercase						).flattened_patches
										self.assertEqual(
										    encoded_images.shape,					(self.image_processor_tester.batch_size, max_patch, expected_hidden_dim),					)




				def 							A__      (						self					:						int						):
							# Initialize image_processor
							lowercase__					  =				self.image_processing_class(**self.image_processor_dict						)
							# create random PIL images
							lowercase__					  =				prepare_image_inputs(self.image_processor_tester,					equal_resolution=__lowercase						)
							for image in image_inputs:
										self.assertIsInstance(__lowercase,					Image.Image						)

							# Test not batched input
							lowercase__					  =				(
							    (self.image_processor_tester.patch_size["height"] * self.image_processor_tester.patch_size["width"])
							    * self.image_processor_tester.num_channels
							) + 2

							lowercase__					  =				True

							for max_patch in self.image_processor_tester.max_patches:
										# Test not batched input
										with self.assertRaises(__lowercase						):
													lowercase__					  =				image_processor(
													    image_inputs[0],					return_tensors="pt",					max_patches=__lowercase						).flattened_patches

										lowercase__					  =				"Hello"

										lowercase__					  =				image_processor(
										    image_inputs[0],					return_tensors="pt",					max_patches=__lowercase,					header_text=__lowercase						).flattened_patches
										self.assertEqual(
										    encoded_images.shape,					(1, max_patch, expected_hidden_dim),					)

										# Test batched
										lowercase__					  =				image_processor(
										    __lowercase,					return_tensors="pt",					max_patches=__lowercase,					header_text=__lowercase						).flattened_patches
										self.assertEqual(
										    encoded_images.shape,					(self.image_processor_tester.batch_size, max_patch, expected_hidden_dim),					)




				def 							A__      (						self					:						Tuple						):
							# Initialize image_processor
							lowercase__					  =				self.image_processing_class(**self.image_processor_dict						)
							# create random numpy tensors
							lowercase__					  =				prepare_image_inputs(self.image_processor_tester,					equal_resolution=__lowercase,					numpify=__lowercase						)
							for image in image_inputs:
										self.assertIsInstance(__lowercase,					np.ndarray						)

							lowercase__					  =				(
							    (self.image_processor_tester.patch_size["height"] * self.image_processor_tester.patch_size["width"])
							    * self.image_processor_tester.num_channels
							) + 2

							for max_patch in self.image_processor_tester.max_patches:
										# Test not batched input
										lowercase__					  =				image_processor(
										    image_inputs[0],					return_tensors="pt",					max_patches=__lowercase						).flattened_patches
										self.assertEqual(
										    encoded_images.shape,					(1, max_patch, expected_hidden_dim),					)

										# Test batched
										lowercase__					  =				image_processor(
										    __lowercase,					return_tensors="pt",					max_patches=__lowercase						).flattened_patches
										self.assertEqual(
										    encoded_images.shape,					(self.image_processor_tester.batch_size, max_patch, expected_hidden_dim),					)




				def 							A__      (						self					:						Any						):
							# Initialize image_processor
							lowercase__					  =				self.image_processing_class(**self.image_processor_dict						)
							# create random PyTorch tensors
							lowercase__					  =				prepare_image_inputs(self.image_processor_tester,					equal_resolution=__lowercase,					torchify=__lowercase						)
							for image in image_inputs:
										self.assertIsInstance(__lowercase,					torch.Tensor						)

							# Test not batched input
							lowercase__					  =				(
							    (self.image_processor_tester.patch_size["height"] * self.image_processor_tester.patch_size["width"])
							    * self.image_processor_tester.num_channels
							) + 2

							for max_patch in self.image_processor_tester.max_patches:
										# Test not batched input
										lowercase__					  =				image_processor(
										    image_inputs[0],					return_tensors="pt",					max_patches=__lowercase						).flattened_patches
										self.assertEqual(
										    encoded_images.shape,					(1, max_patch, expected_hidden_dim),					)

										# Test batched
										lowercase__					  =				image_processor(
										    __lowercase,					return_tensors="pt",					max_patches=__lowercase						).flattened_patches
										self.assertEqual(
										    encoded_images.shape,					(self.image_processor_tester.batch_size, max_patch, expected_hidden_dim),					)
@unittest.skipIf(
    not is_torch_greater_or_equal_than_1_11    ,	reason="""`Pix2StructImageProcessor` requires `torch>=1.11.0`."""    ,	)
@require_torch
@require_vision
class 				_snake_case			(					lowercase__    ,	unittest.TestCase):
				UpperCamelCase__				:					Optional[int]     		=PixaStructImageProcessor if is_vision_available() else None




				def 							A__      (						self					:						Optional[int]						):
							lowercase__					  =				PixaStructImageProcessingTester(self,					num_channels=4						)
							lowercase__					  =				3




				@property
				def 							A__      (						self					:						Union[str, Any]						):
							return self.image_processor_tester.prepare_image_processor_dict()




				def 							A__      (						self					:						Dict						):
							lowercase__					  =				self.image_processing_class(**self.image_processor_dict						)
							self.assertTrue(hasattr(__lowercase,					"do_normalize"						)						)
							self.assertTrue(hasattr(__lowercase,					"do_convert_rgb"						)						)




				def 							A__      (						self					:						Union[str, Any]						):
							# Initialize image_processor
							lowercase__					  =				self.image_processing_class(**self.image_processor_dict						)
							# create random PIL images
							lowercase__					  =				prepare_image_inputs(self.image_processor_tester,					equal_resolution=__lowercase						)
							for image in image_inputs:
										self.assertIsInstance(__lowercase,					Image.Image						)

							# Test not batched input
							lowercase__					  =				(
							    (self.image_processor_tester.patch_size["height"] * self.image_processor_tester.patch_size["width"])
							    * (self.image_processor_tester.num_channels - 1)
							) + 2

							for max_patch in self.image_processor_tester.max_patches:
										# Test not batched input
										lowercase__					  =				image_processor(
										    image_inputs[0],					return_tensors="pt",					max_patches=__lowercase						).flattened_patches
										self.assertEqual(
										    encoded_images.shape,					(1, max_patch, expected_hidden_dim),					)

										# Test batched
										lowercase__					  =				image_processor(
										    __lowercase,					return_tensors="pt",					max_patches=__lowercase						).flattened_patches
										self.assertEqual(
										    encoded_images.shape,					(self.image_processor_tester.batch_size, max_patch, expected_hidden_dim),					)




