




"""simple docstring"""







import inspect
import unittest
import warnings

from transformers import DeiTConfig
from transformers.models.auto import get_values
from transformers.testing_utils import (
    require_accelerate,
    require_torch,
    require_torch_gpu,
    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, floats_tensor, ids_tensor
from ...test_pipeline_mixin import PipelineTesterMixin


if is_torch_available():
		import torch
		from torch import nn

		from transformers import (
		    MODEL_FOR_IMAGE_CLASSIFICATION_MAPPING,
		    MODEL_FOR_SEQUENCE_CLASSIFICATION_MAPPING,
		    MODEL_MAPPING,
		    DeiTForImageClassification,
		    DeiTForImageClassificationWithTeacher,
		    DeiTForMaskedImageModeling,
		    DeiTModel,
		)
		from transformers.models.deit.modeling_deit import DEIT_PRETRAINED_MODEL_ARCHIVE_LIST


if is_vision_available():
		from PIL import Image

		from transformers import DeiTImageProcessor






class 					SCREAMING_SNAKE_CASE__    :


						def __init__(							self :	List[Any]  ,  lowerCAmelCase :	int  ,  lowerCAmelCase :	Union[str, Any]=13  ,  lowerCAmelCase :	List[Any]=30  ,  lowerCAmelCase :	List[str]=2  ,  lowerCAmelCase :	Optional[int]=3  ,  lowerCAmelCase :	List[str]=True  ,  lowerCAmelCase :	str=True  ,  lowerCAmelCase :	int=32  ,  lowerCAmelCase :	List[Any]=5  ,  lowerCAmelCase :	List[Any]=4  ,  lowerCAmelCase :	List[str]=37  ,  lowerCAmelCase :	int="gelu"  ,  lowerCAmelCase :	List[str]=0.1  ,  lowerCAmelCase :	Union[str, Any]=0.1  ,  lowerCAmelCase :	Optional[int]=10  ,  lowerCAmelCase :	Any=0.02  ,  lowerCAmelCase :	Optional[Any]=3  ,  lowerCAmelCase :	Any=None  ,  lowerCAmelCase :	Union[str, Any]=2  ,  ):
										lowerCAmelCase              = parent
										lowerCAmelCase              = batch_size
										lowerCAmelCase              = image_size
										lowerCAmelCase              = patch_size
										lowerCAmelCase              = num_channels
										lowerCAmelCase              = is_training
										lowerCAmelCase              = use_labels
										lowerCAmelCase              = hidden_size
										lowerCAmelCase              = num_hidden_layers
										lowerCAmelCase              = num_attention_heads
										lowerCAmelCase              = intermediate_size
										lowerCAmelCase              = hidden_act
										lowerCAmelCase              = hidden_dropout_prob
										lowerCAmelCase              = attention_probs_dropout_prob
										lowerCAmelCase              = type_sequence_label_size
										lowerCAmelCase              = initializer_range
										lowerCAmelCase              = scope
										lowerCAmelCase              = encoder_stride

										# in DeiT, the seq length equals the number of patches + 2 (we add 2 for the [CLS] and distilation tokens)
										lowerCAmelCase              = (image_size // patch_size) ** 2
										lowerCAmelCase              = num_patches + 2


						def      __lowercase       (							self :	Optional[Any]    ):
										lowerCAmelCase              = floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]    )

										lowerCAmelCase              = None
										if self.use_labels:
														lowerCAmelCase              = ids_tensor([self.batch_size]  ,  self.type_sequence_label_size    )

										lowerCAmelCase              = self.get_config()

										return config, pixel_values, labels


						def      __lowercase       (							self :	Dict    ):
										return DeiTConfig(
										    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=lowerCAmelCase  ,  initializer_range=self.initializer_range  ,  encoder_stride=self.encoder_stride  ,  )


						def      __lowercase       (							self :	int  ,  lowerCAmelCase :	Any  ,  lowerCAmelCase :	str  ,  lowerCAmelCase :	List[str]    ):
										lowerCAmelCase              = DeiTModel(config=lowerCAmelCase    )
										model.to(lowerCAmelCase    )
										model.eval()
										lowerCAmelCase              = model(lowerCAmelCase    )
										self.parent.assertEqual(result.last_hidden_state.shape  ,  (self.batch_size, self.seq_length, self.hidden_size)    )


						def      __lowercase       (							self :	Tuple  ,  lowerCAmelCase :	int  ,  lowerCAmelCase :	Optional[int]  ,  lowerCAmelCase :	int    ):
										lowerCAmelCase              = DeiTForMaskedImageModeling(config=lowerCAmelCase    )
										model.to(lowerCAmelCase    )
										model.eval()
										lowerCAmelCase              = model(lowerCAmelCase    )
										self.parent.assertEqual(
										    result.reconstruction.shape  ,  (self.batch_size, self.num_channels, self.image_size, self.image_size)    )

										# test greyscale images
										lowerCAmelCase              = 1
										lowerCAmelCase              = DeiTForMaskedImageModeling(lowerCAmelCase    )
										model.to(lowerCAmelCase    )
										model.eval()

										lowerCAmelCase              = floats_tensor([self.batch_size, 1, self.image_size, self.image_size]    )
										lowerCAmelCase              = model(lowerCAmelCase    )
										self.parent.assertEqual(result.reconstruction.shape  ,  (self.batch_size, 1, self.image_size, self.image_size)    )


						def      __lowercase       (							self :	List[Any]  ,  lowerCAmelCase :	Tuple  ,  lowerCAmelCase :	Any  ,  lowerCAmelCase :	str    ):
										lowerCAmelCase              = self.type_sequence_label_size
										lowerCAmelCase              = DeiTForImageClassification(lowerCAmelCase    )
										model.to(lowerCAmelCase    )
										model.eval()
										lowerCAmelCase              = model(lowerCAmelCase  ,  labels=lowerCAmelCase    )
										self.parent.assertEqual(result.logits.shape  ,  (self.batch_size, self.type_sequence_label_size)    )

										# test greyscale images
										lowerCAmelCase              = 1
										lowerCAmelCase              = DeiTForImageClassification(lowerCAmelCase    )
										model.to(lowerCAmelCase    )
										model.eval()

										lowerCAmelCase              = floats_tensor([self.batch_size, 1, self.image_size, self.image_size]    )
										lowerCAmelCase              = model(lowerCAmelCase  ,  labels=lowerCAmelCase    )
										self.parent.assertEqual(result.logits.shape  ,  (self.batch_size, self.type_sequence_label_size)    )




						def      __lowercase       (							self :	List[str]    ):
										lowerCAmelCase              = self.prepare_config_and_inputs()
										(
										    (
										    lowerCAmelCase		
										)       ,  (
										    lowerCAmelCase		
										)       ,  (
										    lowerCAmelCase		
										)       ,  		
										)              = config_and_inputs
										lowerCAmelCase              = {"""pixel_values""": pixel_values}
										return config, inputs_dict






@require_torch
class 					SCREAMING_SNAKE_CASE__    (    snake_case_  , snake_case_  , unittest.TestCase    ):
						_a			  =  (
						    (
						        DeiTModel,
						        DeiTForImageClassification,
						        DeiTForImageClassificationWithTeacher,
						        DeiTForMaskedImageModeling,
						    )
						    if is_torch_available()
						    else ()
						)
						_a			  =  (
						    {
						        'feature-extraction': DeiTModel,
						        'image-classification': (DeiTForImageClassification, DeiTForImageClassificationWithTeacher),
						    }
						    if is_torch_available()
						    else {}
						)

						_a			  =  False
						_a			  =  False
						_a			  =  False


						def      __lowercase       (							self :	int    ):
										lowerCAmelCase              = DeiTModelTester(self    )
										lowerCAmelCase              = ConfigTester(self  ,  config_class=lowerCAmelCase  ,  has_text_modality=lowerCAmelCase  ,  hidden_size=37    )


						def      __lowercase       (							self :	int    ):
										self.config_tester.run_common_tests()


						@unittest.skip(reason="""DeiT does not use inputs_embeds"""    )
						def      __lowercase       (							self :	str    ):
										pass


						def      __lowercase       (							self :	List[str]    ):
										lowerCAmelCase       ,  lowerCAmelCase              = self.model_tester.prepare_config_and_inputs_for_common()

										for model_class in self.all_model_classes:
														lowerCAmelCase              = model_class(lowerCAmelCase    )
														self.assertIsInstance(model.get_input_embeddings()  ,  (nn.Module)    )
														lowerCAmelCase              = model.get_output_embeddings()
														self.assertTrue(x is None or isinstance(lowerCAmelCase  ,  nn.Linear    )    )


						def      __lowercase       (							self :	int    ):
										lowerCAmelCase       ,  lowerCAmelCase              = self.model_tester.prepare_config_and_inputs_for_common()

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

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


						def      __lowercase       (							self :	Dict    ):
										lowerCAmelCase              = self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_model(*lowerCAmelCase    )


						def      __lowercase       (							self :	Any    ):
										lowerCAmelCase              = self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_for_masked_image_modeling(*lowerCAmelCase    )


						def      __lowercase       (							self :	Dict    ):
										lowerCAmelCase              = self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_for_image_classification(*lowerCAmelCase    )


						def      __lowercase       (							self :	int  ,  lowerCAmelCase :	Union[str, Any]  ,  lowerCAmelCase :	Optional[int]  ,  lowerCAmelCase :	Union[str, Any]=False    ):
										lowerCAmelCase              = super()._prepare_for_class(lowerCAmelCase  ,  lowerCAmelCase  ,  return_labels=lowerCAmelCase    )

										if return_labels:
														if model_class.__name__ == "DeiTForImageClassificationWithTeacher":
																		del inputs_dict["labels"]

										return inputs_dict


						def      __lowercase       (							self :	List[str]    ):
										if not self.model_tester.is_training:
														return

										lowerCAmelCase       ,  lowerCAmelCase              = self.model_tester.prepare_config_and_inputs_for_common()
										lowerCAmelCase              = True

										for model_class in self.all_model_classes:
														# DeiTForImageClassificationWithTeacher supports inference-only
														if (
														    model_class in get_values(lowerCAmelCase    )
														    or model_class.__name__ == "DeiTForImageClassificationWithTeacher"
														):
																		continue
														lowerCAmelCase              = model_class(lowerCAmelCase    )
														model.to(lowerCAmelCase    )
														model.train()
														lowerCAmelCase              = self._prepare_for_class(lowerCAmelCase  ,  lowerCAmelCase  ,  return_labels=lowerCAmelCase    )
														lowerCAmelCase              = model(**lowerCAmelCase    ).loss
														loss.backward()


						def      __lowercase       (							self :	Optional[Any]    ):
										lowerCAmelCase       ,  lowerCAmelCase              = self.model_tester.prepare_config_and_inputs_for_common()
										if not self.model_tester.is_training:
														return

										lowerCAmelCase              = False
										lowerCAmelCase              = True

										for model_class in self.all_model_classes:
														if model_class in get_values(lowerCAmelCase    ) or not model_class.supports_gradient_checkpointing:
																		continue
														# DeiTForImageClassificationWithTeacher supports inference-only
														if model_class.__name__ == "DeiTForImageClassificationWithTeacher":
																		continue
														lowerCAmelCase              = model_class(lowerCAmelCase    )
														model.gradient_checkpointing_enable()
														model.to(lowerCAmelCase    )
														model.train()
														lowerCAmelCase              = self._prepare_for_class(lowerCAmelCase  ,  lowerCAmelCase  ,  return_labels=lowerCAmelCase    )
														lowerCAmelCase              = model(**lowerCAmelCase    ).loss
														loss.backward()


						def      __lowercase       (							self :	Tuple    ):
										lowerCAmelCase       ,  lowerCAmelCase              = self.model_tester.prepare_config_and_inputs_for_common()

										lowerCAmelCase              = [
										    {"""title""": """multi_label_classification""", """num_labels""": 2, """dtype""": torch.float},
										    {"""title""": """single_label_classification""", """num_labels""": 1, """dtype""": torch.long},
										    {"""title""": """regression""", """num_labels""": 1, """dtype""": torch.float},
										]

										for model_class in self.all_model_classes:
														if (
														    model_class
														    not in [
														        *get_values(lowerCAmelCase    ),
														        *get_values(lowerCAmelCase    ),
														    ]
														    or model_class.__name__ == "DeiTForImageClassificationWithTeacher"
														):
																		continue

														for problem_type in problem_types:
																		with self.subTest(msg=f'''Testing {model_class} with {problem_type['title']}'''    ):
																						lowerCAmelCase              = problem_type["""title"""]
																						lowerCAmelCase              = problem_type["""num_labels"""]

																						lowerCAmelCase              = model_class(lowerCAmelCase    )
																						model.to(lowerCAmelCase    )
																						model.train()

																						lowerCAmelCase              = self._prepare_for_class(lowerCAmelCase  ,  lowerCAmelCase  ,  return_labels=lowerCAmelCase    )

																						if problem_type["num_labels"] > 1:
																										lowerCAmelCase              = inputs["""labels"""].unsqueeze(1    ).repeat(1  ,  problem_type["""num_labels"""]    )

																						lowerCAmelCase              = inputs["""labels"""].to(problem_type["""dtype"""]    )

																						# This tests that we do not trigger the warning form PyTorch "Using a target size that is different
																						# to the input size. This will likely lead to incorrect results due to broadcasting. Please ensure
																						# they have the same size." which is a symptom something in wrong for the regression problem.
																						# See https://github.com/huggingface/transformers/issues/11780
																						with warnings.catch_warnings(record=lowerCAmelCase    ) as warning_list:
																										lowerCAmelCase              = model(**lowerCAmelCase    ).loss
																						for w in warning_list:
																										if "Using a target size that is different to the input size" in str(w.message    ):
																														raise ValueError(
																														    f'''Something is going wrong in the regression problem: intercepted {w.message}'''    )

																						loss.backward()




						@slow
						def      __lowercase       (							self :	Dict    ):
										for model_name in DEIT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
														lowerCAmelCase              = DeiTModel.from_pretrained(lowerCAmelCase    )
														self.assertIsNotNone(lowerCAmelCase    )





def 							lowercase    ()   ->     List[Any]:


				'''simple docstring'''






				lowerCAmelCase              = Image.open("""./tests/fixtures/tests_samples/COCO/000000039769.png"""       )
				return image






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


						@cached_property
						def      __lowercase       (							self :	Tuple    ):
										return (
										    DeiTImageProcessor.from_pretrained("""facebook/deit-base-distilled-patch16-224"""    )
										    if is_vision_available()
										    else None
										)


						@slow
						def      __lowercase       (							self :	str    ):
										lowerCAmelCase              = DeiTForImageClassificationWithTeacher.from_pretrained("""facebook/deit-base-distilled-patch16-224"""    ).to(
										    lowerCAmelCase    )

										lowerCAmelCase              = self.default_image_processor
										lowerCAmelCase              = prepare_img()
										lowerCAmelCase              = image_processor(images=lowerCAmelCase  ,  return_tensors="""pt"""    ).to(lowerCAmelCase    )

										# forward pass
										with torch.no_grad():
														lowerCAmelCase              = model(**lowerCAmelCase    )

										# verify the logits
										lowerCAmelCase              = torch.Size((1, 1000)    )
										self.assertEqual(outputs.logits.shape  ,  lowerCAmelCase    )

										lowerCAmelCase              = torch.tensor([-1.0266, 0.1912, -1.2861]    ).to(lowerCAmelCase    )

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




						@slow
						@require_accelerate
						@require_torch_gpu
						def      __lowercase       (							self :	Optional[int]    ):
										lowerCAmelCase              = DeiTModel.from_pretrained(
										    """facebook/deit-base-distilled-patch16-224"""  ,  torch_dtype=torch.floataa  ,  device_map="""auto"""    )
										lowerCAmelCase              = self.default_image_processor

										lowerCAmelCase              = prepare_img()
										lowerCAmelCase              = image_processor(images=lowerCAmelCase  ,  return_tensors="""pt"""    )
										lowerCAmelCase              = inputs.pixel_values.to(lowerCAmelCase    )

										# forward pass to make sure inference works in fp16
										with torch.no_grad():
														lowerCAmelCase              = model(lowerCAmelCase    )













import numpy as np
from sklearn.datasets import fetch_california_housing
from sklearn.metrics import mean_absolute_error, mean_squared_error
from sklearn.model_selection import train_test_split
from xgboost import XGBRegressor





def 						__UpperCAmelCase			(     a_):
							return (data["data"], data["target"])





def 						__UpperCAmelCase			(     a_							,     a_							,     a_):
							snake_case_				=							XGBRegressor(verbosity=0							,     random_state=42)
							xgb.fit(a_							,     a_)
							# Predict target for test data
							snake_case_				=							xgb.predict(a_)
							snake_case_				=							predictions.reshape(len(a_)							,     1)
							return predictions





def 						__UpperCAmelCase			(     ):
							snake_case_				=							fetch_california_housing()
							snake_case_			,		snake_case_				=							data_handling(a_)
							snake_case_			,		snake_case_			,		snake_case_			,		snake_case_				=							train_test_split(
							    a_							,     a_							,     test_size=0.25							,     random_state=1)
							snake_case_				=							xgboost(a_							,     a_							,     a_)
							# Error printing
							print(f'''Mean Absolute Error : {mean_absolute_error(a_							,     a_)}''')
							print(f'''Mean Square Error  : {mean_squared_error(a_							,     a_)}''')


if __name__ == "__main__":
	import doctest

	doctest.testmod(verbose=True)
	main()





