


"""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 					lowerCAmelCase	:




		'''simple docstring'''







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

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







		def 							__A						(	self				)     ->							Tuple:
								SCREAMING_SNAKE_CASE													=					floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]				)

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

								SCREAMING_SNAKE_CASE													=					self.get_config()

								return config, pixel_values, labels







		def 							__A						(	self				)     ->							str:
								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 							__A						(	self						,       lowerCAmelCase__						,       lowerCAmelCase__						,       lowerCAmelCase__				)     ->							Dict:
								SCREAMING_SNAKE_CASE													=					DeiTModel(config=lowerCAmelCase__				)
								model.to(lowerCAmelCase__				)
								model.eval()
								SCREAMING_SNAKE_CASE													=					model(lowerCAmelCase__				)
								self.parent.assertEqual(result.last_hidden_state.shape						,       (self.batch_size, self.seq_length, self.hidden_size)				)







		def 							__A						(	self						,       lowerCAmelCase__						,       lowerCAmelCase__						,       lowerCAmelCase__				)     ->							Tuple:
								SCREAMING_SNAKE_CASE													=					DeiTForMaskedImageModeling(config=lowerCAmelCase__				)
								model.to(lowerCAmelCase__				)
								model.eval()
								SCREAMING_SNAKE_CASE													=					model(lowerCAmelCase__				)
								self.parent.assertEqual(
								    result.reconstruction.shape						,       (self.batch_size, self.num_channels, self.image_size, self.image_size)				)

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

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







		def 							__A						(	self						,       lowerCAmelCase__						,       lowerCAmelCase__						,       lowerCAmelCase__				)     ->							Any:
								SCREAMING_SNAKE_CASE													=					self.type_sequence_label_size
								SCREAMING_SNAKE_CASE													=					DeiTForImageClassification(lowerCAmelCase__				)
								model.to(lowerCAmelCase__				)
								model.eval()
								SCREAMING_SNAKE_CASE													=					model(lowerCAmelCase__						,       labels=lowerCAmelCase__				)
								self.parent.assertEqual(result.logits.shape						,       (self.batch_size, self.type_sequence_label_size)				)

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

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







		def 							__A						(	self				)     ->							Optional[int]:
								SCREAMING_SNAKE_CASE													=					self.prepare_config_and_inputs()
								(
								    (
								    SCREAMING_SNAKE_CASE       
								)							,				(
								    SCREAMING_SNAKE_CASE       
								)							,				(
								    SCREAMING_SNAKE_CASE       
								)							,				       
								)													=					config_and_inputs
								SCREAMING_SNAKE_CASE													=					{'pixel_values': pixel_values}
								return config, inputs_dict





@require_torch
class 					lowerCAmelCase	(					snake_case_	,    snake_case_	,    unittest.TestCase    ):




		'''simple docstring'''







		SCREAMING_SNAKE_CASE_						:     List[str]								=     (
		    (
		        DeiTModel,
		        DeiTForImageClassification,
		        DeiTForImageClassificationWithTeacher,
		        DeiTForMaskedImageModeling,
		    )
		    if is_torch_available()
		    else ()
		)
		SCREAMING_SNAKE_CASE_						:     int								=     (
		    {
		        """feature-extraction""": DeiTModel,
		        """image-classification""": (DeiTForImageClassification, DeiTForImageClassificationWithTeacher),
		    }
		    if is_torch_available()
		    else {}
		)

		SCREAMING_SNAKE_CASE_						:     Optional[int]								=     False
		SCREAMING_SNAKE_CASE_						:     List[Any]								=     False
		SCREAMING_SNAKE_CASE_						:     List[Any]								=     False







		def 							__A						(	self				)     ->							int:
								SCREAMING_SNAKE_CASE													=					DeiTModelTester(self				)
								SCREAMING_SNAKE_CASE													=					ConfigTester(self						,       config_class=lowerCAmelCase__						,       has_text_modality=lowerCAmelCase__						,       hidden_size=37				)







		def 							__A						(	self				)     ->							str:
								self.config_tester.run_common_tests()







		@unittest.skip(reason='DeiT does not use inputs_embeds'				)
		def 							__A						(	self				)     ->							List[Any]:
								pass







		def 							__A						(	self				)     ->							Union[str, Any]:
								SCREAMING_SNAKE_CASE							,				SCREAMING_SNAKE_CASE													=					self.model_tester.prepare_config_and_inputs_for_common()

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







		def 							__A						(	self				)     ->							List[str]:
								SCREAMING_SNAKE_CASE							,				SCREAMING_SNAKE_CASE													=					self.model_tester.prepare_config_and_inputs_for_common()

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

														SCREAMING_SNAKE_CASE													=					['pixel_values']
														self.assertListEqual(arg_names[:1]						,       lowerCAmelCase__				)







		def 							__A						(	self				)     ->							int:
								SCREAMING_SNAKE_CASE													=					self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_model(*lowerCAmelCase__				)







		def 							__A						(	self				)     ->							int:
								SCREAMING_SNAKE_CASE													=					self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_for_masked_image_modeling(*lowerCAmelCase__				)







		def 							__A						(	self				)     ->							List[Any]:
								SCREAMING_SNAKE_CASE													=					self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_for_image_classification(*lowerCAmelCase__				)







		def 							__A						(	self						,       lowerCAmelCase__						,       lowerCAmelCase__						,       lowerCAmelCase__=False				)     ->							Dict:
								SCREAMING_SNAKE_CASE													=					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 							__A						(	self				)     ->							Any:
								if not self.model_tester.is_training:
														return

								SCREAMING_SNAKE_CASE							,				SCREAMING_SNAKE_CASE													=					self.model_tester.prepare_config_and_inputs_for_common()
								SCREAMING_SNAKE_CASE													=					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
														SCREAMING_SNAKE_CASE													=					model_class(lowerCAmelCase__				)
														model.to(lowerCAmelCase__				)
														model.train()
														SCREAMING_SNAKE_CASE													=					self._prepare_for_class(lowerCAmelCase__						,       lowerCAmelCase__						,       return_labels=lowerCAmelCase__				)
														SCREAMING_SNAKE_CASE													=					model(**lowerCAmelCase__				).loss
														loss.backward()







		def 							__A						(	self				)     ->							str:
								SCREAMING_SNAKE_CASE							,				SCREAMING_SNAKE_CASE													=					self.model_tester.prepare_config_and_inputs_for_common()
								if not self.model_tester.is_training:
														return

								SCREAMING_SNAKE_CASE													=					False
								SCREAMING_SNAKE_CASE													=					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
														SCREAMING_SNAKE_CASE													=					model_class(lowerCAmelCase__				)
														model.gradient_checkpointing_enable()
														model.to(lowerCAmelCase__				)
														model.train()
														SCREAMING_SNAKE_CASE													=					self._prepare_for_class(lowerCAmelCase__						,       lowerCAmelCase__						,       return_labels=lowerCAmelCase__				)
														SCREAMING_SNAKE_CASE													=					model(**lowerCAmelCase__				).loss
														loss.backward()







		def 							__A						(	self				)     ->							List[Any]:
								SCREAMING_SNAKE_CASE							,				SCREAMING_SNAKE_CASE													=					self.model_tester.prepare_config_and_inputs_for_common()

								SCREAMING_SNAKE_CASE													=					[
								    {'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"]}'				):
																										SCREAMING_SNAKE_CASE													=					problem_type['title']
																										SCREAMING_SNAKE_CASE													=					problem_type['num_labels']

																										SCREAMING_SNAKE_CASE													=					model_class(lowerCAmelCase__				)
																										model.to(lowerCAmelCase__				)
																										model.train()

																										SCREAMING_SNAKE_CASE													=					self._prepare_for_class(lowerCAmelCase__						,       lowerCAmelCase__						,       return_labels=lowerCAmelCase__				)

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

																										SCREAMING_SNAKE_CASE													=					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:
																																SCREAMING_SNAKE_CASE													=					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 							__A						(	self				)     ->							Optional[int]:
								for model_name in DEIT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
														SCREAMING_SNAKE_CASE													=					DeiTModel.from_pretrained(lowerCAmelCase__				)
														self.assertIsNotNone(lowerCAmelCase__				)





def     lowercase			()   ->						int:
						SCREAMING_SNAKE_CASE													=					Image.open('./tests/fixtures/tests_samples/COCO/000000039769.png'  )
						return image


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




		'''simple docstring'''







		@cached_property
		def 							__A						(	self				)     ->							List[str]:
								return (
								    DeiTImageProcessor.from_pretrained('facebook/deit-base-distilled-patch16-224'				)
								    if is_vision_available()
								    else None
								)







		@slow
		def 							__A						(	self				)     ->							Any:
								SCREAMING_SNAKE_CASE													=					DeiTForImageClassificationWithTeacher.from_pretrained('facebook/deit-base-distilled-patch16-224'				).to(
								    lowerCAmelCase__				)

								SCREAMING_SNAKE_CASE													=					self.default_image_processor
								SCREAMING_SNAKE_CASE													=					prepare_img()
								SCREAMING_SNAKE_CASE													=					image_processor(images=lowerCAmelCase__						,       return_tensors='pt'				).to(lowerCAmelCase__				)

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

								# verify the logits
								SCREAMING_SNAKE_CASE													=					torch.Size((1, 1_000)				)
								self.assertEqual(outputs.logits.shape						,       lowerCAmelCase__				)

								SCREAMING_SNAKE_CASE													=					torch.tensor([-1.02_66, 0.19_12, -1.28_61]				).to(lowerCAmelCase__				)

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







		@slow
		@require_accelerate
		@require_torch_gpu
		def 							__A						(	self				)     ->							Union[str, Any]:
								SCREAMING_SNAKE_CASE													=					DeiTModel.from_pretrained(
								    'facebook/deit-base-distilled-patch16-224'						,       torch_dtype=torch.floataa						,       device_map='auto'				)
								SCREAMING_SNAKE_CASE													=					self.default_image_processor

								SCREAMING_SNAKE_CASE													=					prepare_img()
								SCREAMING_SNAKE_CASE													=					image_processor(images=lowerCAmelCase__						,       return_tensors='pt'				)
								SCREAMING_SNAKE_CASE													=					inputs.pixel_values.to(lowerCAmelCase__				)

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









'''simple docstring'''

import argparse
import shlex

import runhouse as rh


if __name__ == "__main__":
      # Refer to https://runhouse-docs.readthedocs-hosted.com/en/latest/api/python/cluster.html#hardware-setup for cloud access
      # setup instructions, if using on-demand hardware

      # If user passes --user <user> --host <host> --key_path <key_path> <example> <args>, fill them in as BYO cluster
      # If user passes --instance <instance> --provider <provider> <example> <args>, fill them in as on-demand cluster
      # Throw an error if user passes both BYO and on-demand cluster args
      # Otherwise, use default values
      lowercase_					= argparse.ArgumentParser()
      parser.add_argument("""--user""", type=str, default="""ubuntu""")
      parser.add_argument("""--host""", type=str, default="""localhost""")
      parser.add_argument("""--key_path""", type=str, default=None)
      parser.add_argument("""--instance""", type=str, default="""V100:1""")
      parser.add_argument("""--provider""", type=str, default="""cheapest""")
      parser.add_argument("""--use_spot""", type=bool, default=False)
      parser.add_argument("""--example""", type=str, default="""pytorch/text-generation/run_generation.py""")
      lowercase_				,			lowercase_					= parser.parse_known_args()
      if args.host != "localhost":
            if args.instance != "V100:1" or args.provider != "cheapest":
                  raise ValueError("""Cannot specify both BYO and on-demand cluster args""")
            lowercase_					= rh.cluster(
                name="""rh-cluster""", ips=[args.host], ssh_creds={"""ssh_user""": args.user, """ssh_private_key""": args.key_path}
            )
      else:
            lowercase_					= rh.cluster(
                name="""rh-cluster""", instance_type=args.instance, provider=args.provider, use_spot=args.use_spot
            )
      lowercase_					= args.example.rsplit("""/""", 1)[0]

      # Set up remote environment
      cluster.install_packages(["""pip:./"""])  # Installs transformers from local source
      # Note transformers is copied into the home directory on the remote machine, so we can install from there
      cluster.run([f"""pip install -r transformers/examples/{example_dir}/requirements.txt"""])
      cluster.run(["""pip install torch --upgrade --extra-index-url https://download.pytorch.org/whl/cu117"""])

      # Run example. You can bypass the CLI wrapper and paste your own code here.
      cluster.run([f"""python transformers/examples/{args.example} {" ".join(shlex.quote(arg) for arg in unknown)}"""])

      # Alternatively, we can just import and run a training function (especially if there's no wrapper CLI):
      # from my_script... import train
      # reqs = ['pip:./', 'torch', 'datasets', 'accelerate', 'evaluate', 'tqdm', 'scipy', 'scikit-learn', 'tensorboard']
      # launch_train_gpu = rh.function(fn=train,
      #                                system=gpu,
      #                                reqs=reqs,
      #                                name='train_bert_glue')
      #
      # We can pass in arguments just like we would to a function:
      # launch_train_gpu(num_epochs = 3, lr = 2e-5, seed = 42, batch_size = 16
      #                  stream_logs=True)



