




"""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					:      str    ,    lowerCAmelCase					:      Union[str, Any]    ,    lowerCAmelCase					:      Union[str, Any]=13    ,    lowerCAmelCase					:      Tuple=30    ,    lowerCAmelCase					:      Dict=2    ,    lowerCAmelCase					:      Tuple=3    ,    lowerCAmelCase					:      List[str]=True    ,    lowerCAmelCase					:      Optional[Any]=True    ,    lowerCAmelCase					:      List[Any]=32    ,    lowerCAmelCase					:      Union[str, Any]=5    ,    lowerCAmelCase					:      int=4    ,    lowerCAmelCase					:      Optional[Any]=37    ,    lowerCAmelCase					:      Optional[Any]="gelu"    ,    lowerCAmelCase					:      Dict=0.1    ,    lowerCAmelCase					:      Optional[int]=0.1    ,    lowerCAmelCase					:      str=10    ,    lowerCAmelCase					:      Any=0.02    ,    lowerCAmelCase					:      Union[str, Any]=3    ,    lowerCAmelCase					:      str=None    ,    lowerCAmelCase					:      Optional[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					:      List[str]       ):
										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					:      Optional[int]       ):
										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=_UpperCAmelCase    ,    initializer_range=self.initializer_range    ,    encoder_stride=self.encoder_stride    ,    )


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


						def 	__lowercase			(				self					:      Dict    ,    lowerCAmelCase					:      str    ,    lowerCAmelCase					:      Optional[int]    ,    lowerCAmelCase					:      str       ):
										lowerCAmelCase						=					DeiTForMaskedImageModeling(config=_UpperCAmelCase       )
										model.to(_UpperCAmelCase       )
										model.eval()
										lowerCAmelCase						=					model(_UpperCAmelCase       )
										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(_UpperCAmelCase       )
										model.to(_UpperCAmelCase       )
										model.eval()

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


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

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

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




						def 	__lowercase			(				self					:      Any       ):
										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__       (       UpperCAmelCase__    ,					UpperCAmelCase__    ,					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					:      Any       ):
										lowerCAmelCase						=					DeiTModelTester(self       )
										lowerCAmelCase						=					ConfigTester(self    ,    config_class=_UpperCAmelCase    ,    has_text_modality=_UpperCAmelCase    ,    hidden_size=37       )


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


						@unittest.skip(reason="""DeiT does not use inputs_embeds"""       )
						def 	__lowercase			(				self					:      Any       ):
										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(_UpperCAmelCase       )
														self.assertIsInstance(model.get_input_embeddings()    ,    (nn.Module)       )
														lowerCAmelCase						=					model.get_output_embeddings()
														self.assertTrue(x is None or isinstance(_UpperCAmelCase    ,    nn.Linear       )       )


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

										for model_class in self.all_model_classes:
														lowerCAmelCase						=					model_class(_UpperCAmelCase       )
														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]    ,    _UpperCAmelCase       )


						def 	__lowercase			(				self					:      Union[str, Any]       ):
										lowerCAmelCase						=					self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_model(*_UpperCAmelCase       )


						def 	__lowercase			(				self					:      Optional[int]       ):
										lowerCAmelCase						=					self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_for_masked_image_modeling(*_UpperCAmelCase       )


						def 	__lowercase			(				self					:      Optional[Any]       ):
										lowerCAmelCase						=					self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_for_image_classification(*_UpperCAmelCase       )


						def 	__lowercase			(				self					:      Union[str, Any]    ,    lowerCAmelCase					:      Tuple    ,    lowerCAmelCase					:      List[str]    ,    lowerCAmelCase					:      List[Any]=False       ):
										lowerCAmelCase						=					super()._prepare_for_class(_UpperCAmelCase    ,    _UpperCAmelCase    ,    return_labels=_UpperCAmelCase       )

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

										return inputs_dict


						def 	__lowercase			(				self					:      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(_UpperCAmelCase       )
														    or model_class.__name__ == "DeiTForImageClassificationWithTeacher"
														):
																		continue
														lowerCAmelCase						=					model_class(_UpperCAmelCase       )
														model.to(_UpperCAmelCase       )
														model.train()
														lowerCAmelCase						=					self._prepare_for_class(_UpperCAmelCase    ,    _UpperCAmelCase    ,    return_labels=_UpperCAmelCase       )
														lowerCAmelCase						=					model(**_UpperCAmelCase       ).loss
														loss.backward()


						def 	__lowercase			(				self					:      Union[str, 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(_UpperCAmelCase       ) or not model_class.supports_gradient_checkpointing:
																		continue
														# DeiTForImageClassificationWithTeacher supports inference-only
														if model_class.__name__ == "DeiTForImageClassificationWithTeacher":
																		continue
														lowerCAmelCase						=					model_class(_UpperCAmelCase       )
														model.gradient_checkpointing_enable()
														model.to(_UpperCAmelCase       )
														model.train()
														lowerCAmelCase						=					self._prepare_for_class(_UpperCAmelCase    ,    _UpperCAmelCase    ,    return_labels=_UpperCAmelCase       )
														lowerCAmelCase						=					model(**_UpperCAmelCase       ).loss
														loss.backward()


						def 	__lowercase			(				self					:      Union[str, Any]       ):
										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(_UpperCAmelCase       ),
														        *get_values(_UpperCAmelCase       ),
														    ]
														    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(_UpperCAmelCase       )
																						model.to(_UpperCAmelCase       )
																						model.train()

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

																						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=_UpperCAmelCase       ) as warning_list:
																										lowerCAmelCase						=					model(**_UpperCAmelCase       ).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					:      List[str]       ):
										for model_name in DEIT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
														lowerCAmelCase						=					DeiTModel.from_pretrained(_UpperCAmelCase       )
														self.assertIsNotNone(_UpperCAmelCase       )





def 				lowercase       ()  ->	str:


				'''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					:      Union[str, Any]       ):
										return (
										    DeiTImageProcessor.from_pretrained("""facebook/deit-base-distilled-patch16-224"""       )
										    if is_vision_available()
										    else None
										)


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

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

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

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

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

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




						@slow
						@require_accelerate
						@require_torch_gpu
						def 	__lowercase			(				self					:      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=_UpperCAmelCase    ,    return_tensors="""pt"""       )
										lowerCAmelCase						=					inputs.pixel_values.to(_UpperCAmelCase       )

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













import argparse

import torch

from ...utils import logging
from . import AlbertConfig, AlbertForPreTraining, load_tf_weights_in_albert


logging.set_verbosity_info()






def      _snake_case     (__lowercase     ,   __lowercase     ,   __lowercase):
   # Initialise PyTorch model
   UpperCamelCase_						=  AlbertConfig.from_json_file(__lowercase)
   print(f"""Building PyTorch model from configuration: {config}""")
   UpperCamelCase_						=  AlbertForPreTraining(__lowercase)

   # Load weights from tf checkpoint
   load_tf_weights_in_albert(__lowercase     ,   __lowercase     ,   __lowercase)

   # Save pytorch-model
   print(f"""Save PyTorch model to {pytorch_dump_path}""")
   torch.save(model.state_dict()     ,   __lowercase)


if __name__ == "__main__":
 snake_case__   :						Tuple													=   argparse.ArgumentParser()
 # Required parameters
 parser.add_argument(
     """--tf_checkpoint_path""", default=None, type=str, required=True, help="""Path to the TensorFlow checkpoint path."""
 )
 parser.add_argument(
     """--albert_config_file""",
     default=None,
     type=str,
     required=True,
     help=(
         """The config json file corresponding to the pre-trained ALBERT model. \n"""
         """This specifies the model architecture."""
     ),
 )
 parser.add_argument(
     """--pytorch_dump_path""", default=None, type=str, required=True, help="""Path to the output PyTorch model."""
 )
 snake_case__   :						str													=   parser.parse_args()
 convert_tf_checkpoint_to_pytorch(args.tf_checkpoint_path, args.albert_config_file, args.pytorch_dump_path)





