






'''simple docstring'''

import inspect
import unittest

from transformers import DataaVecVisionConfig
from transformers.models.auto import get_values
from transformers.testing_utils import require_torch, require_torch_multi_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, _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 (
						    MODEL_MAPPING,
						    DataaVecVisionForImageClassification,
						    DataaVecVisionForSemanticSegmentation,
						    DataaVecVisionModel,
						)
						from transformers.models.dataavec.modeling_dataavec_vision import DATA2VEC_VISION_PRETRAINED_MODEL_ARCHIVE_LIST


if is_vision_available():
						from PIL import Image

						from transformers import BeitImageProcessor






class 				__magic_name__  :







	"""simple docstring"""



	def __init__(				self	,   _lowercase	,   _lowercase=100	,   _lowercase=13	,   _lowercase=30	,   _lowercase=2	,   _lowercase=3	,   _lowercase=True	,   _lowercase=True	,   _lowercase=32	,   _lowercase=4	,   _lowercase=4	,   _lowercase=37	,   _lowercase="gelu"	,   _lowercase=0.1	,   _lowercase=0.1	,   _lowercase=10	,   _lowercase=0.02	,   _lowercase=3	,   _lowercase=None	,   _lowercase=[0, 1, 2, 3]	,   )    ->	Tuple:
							lowercase_   : Optional[int]   	=      parent
							lowercase_   : List[Any]   	=      100
							lowercase_   : Union[str, Any]   	=      batch_size
							lowercase_   : Tuple   	=      image_size
							lowercase_   : Optional[int]   	=      patch_size
							lowercase_   : Optional[Any]   	=      num_channels
							lowercase_   : int   	=      is_training
							lowercase_   : Any   	=      use_labels
							lowercase_   : Dict   	=      hidden_size
							lowercase_   : int   	=      num_hidden_layers
							lowercase_   : Optional[int]   	=      num_attention_heads
							lowercase_   : Dict   	=      intermediate_size
							lowercase_   : List[Any]   	=      hidden_act
							lowercase_   : str   	=      hidden_dropout_prob
							lowercase_   : Tuple   	=      attention_probs_dropout_prob
							lowercase_   : int   	=      type_sequence_label_size
							lowercase_   : Optional[Any]   	=      initializer_range
							lowercase_   : str   	=      scope
							lowercase_   : Optional[int]   	=      out_indices
							lowercase_   : Union[str, Any]   	=      num_labels

							# in BeiT, the seq length equals the number of patches + 1 (we add 1 for the [CLS] token)
							lowercase_   : Any   	=      (image_size // patch_size) ** 2
							lowercase_   : List[Any]   	=      num_patches + 1
	def 							lowerCamelCase__		(				self							)    ->	int:
							lowercase_   : Any   	=      floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]							)

							lowercase_   : int   	=      None
							lowercase_   : Any   	=      None
							if self.use_labels:
													lowercase_   : Optional[int]   	=      ids_tensor([self.batch_size]	,   self.type_sequence_label_size							)
													lowercase_   : str   	=      ids_tensor([self.batch_size, self.image_size, self.image_size]	,   self.num_labels							)

							lowercase_   : Optional[Any]   	=      self.get_config()

							return config, pixel_values, labels, pixel_labels
	def 							lowerCamelCase__		(				self							)    ->	Optional[int]:
							return DataaVecVisionConfig(
							    vocab_size=self.vocab_size	,   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=_lowercase	,   initializer_range=self.initializer_range	,   out_indices=self.out_indices	,   )
	def 							lowerCamelCase__		(				self	,   _lowercase	,   _lowercase	,   _lowercase	,   _lowercase							)    ->	List[str]:
							lowercase_   : Any   	=      DataaVecVisionModel(config=_lowercase							)
							model.to(_lowercase							)
							model.eval()
							lowercase_   : str   	=      model(_lowercase							)
							# expected sequence length = num_patches + 1 (we add 1 for the [CLS] token)
							lowercase_   : Optional[int]   	=      (self.image_size // self.patch_size) ** 2
							self.parent.assertEqual(result.last_hidden_state.shape	,   (self.batch_size, num_patches + 1, self.hidden_size)							)
	def 							lowerCamelCase__		(				self	,   _lowercase	,   _lowercase	,   _lowercase	,   _lowercase							)    ->	Any:
							lowercase_   : Tuple   	=      self.type_sequence_label_size
							lowercase_   : int   	=      DataaVecVisionForImageClassification(_lowercase							)
							model.to(_lowercase							)
							model.eval()
							lowercase_   : Optional[int]   	=      model(_lowercase	,   labels=_lowercase							)
							self.parent.assertEqual(result.logits.shape	,   (self.batch_size, self.type_sequence_label_size)							)
	def 							lowerCamelCase__		(				self	,   _lowercase	,   _lowercase	,   _lowercase	,   _lowercase							)    ->	int:
							lowercase_   : Dict   	=      self.num_labels
							lowercase_   : Dict   	=      DataaVecVisionForSemanticSegmentation(_lowercase							)
							model.to(_lowercase							)
							model.eval()
							lowercase_   : str   	=      model(_lowercase							)
							self.parent.assertEqual(
							    result.logits.shape	,   (self.batch_size, self.num_labels, self.image_size * 2, self.image_size * 2)							)
							lowercase_   : int   	=      model(_lowercase	,   labels=_lowercase							)
							self.parent.assertEqual(
							    result.logits.shape	,   (self.batch_size, self.num_labels, self.image_size * 2, self.image_size * 2)							)







	def 							lowerCamelCase__		(				self							)    ->	Union[str, Any]:
							lowercase_   : Optional[int]   	=      self.prepare_config_and_inputs()
							lowercase_	,   lowercase_	,   lowercase_	,   lowercase_   : Union[str, Any]   	=      config_and_inputs
							lowercase_   : Optional[Any]   	=      {'pixel_values': pixel_values}
							return config, inputs_dict






@require_torch
class 				__magic_name__  (						UpperCAmelCase_,    UpperCAmelCase_,    unittest.TestCase					):







	"""simple docstring"""



	SCREAMING_SNAKE_CASE_       :   Dict   =	(
	    (DataaVecVisionModel, DataaVecVisionForImageClassification, DataaVecVisionForSemanticSegmentation)
	    if is_torch_available()
	    else ()
	)
	SCREAMING_SNAKE_CASE_       :   Optional[Any]   =	(
	    {
	        'feature-extraction': DataaVecVisionModel,
	        'image-classification': DataaVecVisionForImageClassification,
	        'image-segmentation': DataaVecVisionForSemanticSegmentation,
	    }
	    if is_torch_available()
	    else {}
	)

	SCREAMING_SNAKE_CASE_       :   int   =	False
	SCREAMING_SNAKE_CASE_       :   Optional[Any]   =	False
	SCREAMING_SNAKE_CASE_       :   Tuple   =	False
	def 							lowerCamelCase__		(				self							)    ->	List[Any]:
							lowercase_   : Any   	=      DataaVecVisionModelTester(self							)
							lowercase_   : Optional[Any]   	=      ConfigTester(
							    self	,   config_class=_lowercase	,   has_text_modality=_lowercase	,   hidden_size=37							)
	def 							lowerCamelCase__		(				self							)    ->	Optional[Any]:
							self.config_tester.run_common_tests()
	def 							lowerCamelCase__		(				self							)    ->	Any:
							# Data2VecVision does not use inputs_embeds
							pass
	@require_torch_multi_gpu
	@unittest.skip(
	    reason='Data2VecVision has some layers using `add_module` which doesn\'t work well with `nn.DataParallel`'							)
	def 							lowerCamelCase__		(				self							)    ->	Tuple:
							pass
	def 							lowerCamelCase__		(				self							)    ->	List[str]:
							lowercase_	,   lowercase_   : str   	=      self.model_tester.prepare_config_and_inputs_for_common()

							for model_class in self.all_model_classes:
													lowercase_   : int   	=      model_class(_lowercase							)
													self.assertIsInstance(model.get_input_embeddings()	,   (nn.Module)							)
													lowercase_   : Optional[Any]   	=      model.get_output_embeddings()
													self.assertTrue(x is None or isinstance(_lowercase	,   nn.Linear							)							)
	def 							lowerCamelCase__		(				self							)    ->	Optional[int]:
							lowercase_	,   lowercase_   : Union[str, Any]   	=      self.model_tester.prepare_config_and_inputs_for_common()

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

													lowercase_   : Optional[Any]   	=      ['pixel_values']
													self.assertListEqual(arg_names[:1]	,   _lowercase							)
	def 							lowerCamelCase__		(				self							)    ->	Union[str, Any]:
							lowercase_   : str   	=      self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_model(*_lowercase							)
	def 							lowerCamelCase__		(				self							)    ->	str:
							lowercase_   : Dict   	=      self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_for_image_segmentation(*_lowercase							)
	def 							lowerCamelCase__		(				self							)    ->	List[str]:
							if not self.model_tester.is_training:
													return

							lowercase_	,   lowercase_   : Optional[Any]   	=      self.model_tester.prepare_config_and_inputs_for_common()
							lowercase_   : str   	=      True

							for model_class in self.all_model_classes:
													if model_class in [*get_values(_lowercase							)]:
																			continue

													lowercase_   : Dict   	=      model_class(_lowercase							)
													model.to(_lowercase							)
													model.train()
													lowercase_   : int   	=      self._prepare_for_class(_lowercase	,   _lowercase	,   return_labels=_lowercase							)
													lowercase_   : Optional[int]   	=      model(**_lowercase							).loss
													loss.backward()
	def 							lowerCamelCase__		(				self							)    ->	Union[str, Any]:
							lowercase_	,   lowercase_   : Optional[int]   	=      self.model_tester.prepare_config_and_inputs_for_common()
							if not self.model_tester.is_training:
													return

							lowercase_   : Union[str, Any]   	=      False
							lowercase_   : Tuple   	=      True

							for model_class in self.all_model_classes:
													if model_class in [*get_values(_lowercase							)] or not model_class.supports_gradient_checkpointing:
																			continue
													# TODO: remove the following 3 lines once we have a MODEL_FOR_SEMANTIC_SEGMENTATION_MAPPING
													# this can then be incorporated into _prepare_for_class in test_modeling_common.py
													elif model_class.__name__ == "Data2VecVisionForSemanticSegmentation":
																			lowercase_	,   lowercase_	,   lowercase_	,   lowercase_   : Tuple   	=      inputs_dict['pixel_values'].shape
																			lowercase_   : Optional[Any]   	=      torch.zeros(
																			    [self.model_tester.batch_size, height, width]	,   device=_lowercase							).long()
													lowercase_   : str   	=      model_class(_lowercase							)
													model.gradient_checkpointing_enable()
													model.to(_lowercase							)
													model.train()
													lowercase_   : List[Any]   	=      self._prepare_for_class(_lowercase	,   _lowercase	,   return_labels=_lowercase							)
													lowercase_   : Dict   	=      model(**_lowercase							).loss
													loss.backward()
	def 							lowerCamelCase__		(				self							)    ->	List[str]:
							lowercase_	,   lowercase_   : List[str]   	=      self.model_tester.prepare_config_and_inputs_for_common()

							lowercase_   : List[str]   	=      _config_zero_init(_lowercase							)
							for model_class in self.all_model_classes:
													lowercase_   : Optional[Any]   	=      model_class(config=_lowercase							)
													for name, param in model.named_parameters():
																			# we skip lambda parameters as these require special initial values
																			# determined by config.layer_scale_init_value
																			if "lambda" in name:
																									continue
																			if param.requires_grad:
																									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"	,   )
	def 							lowerCamelCase__		(				self	,   _lowercase	,   _lowercase	,   _lowercase	,   _lowercase=2E-4	,   _lowercase="outputs"	,   _lowercase=None							)    ->	Tuple:
							# We override with a slightly higher tol value, as semseg models tend to diverge a bit more
							super().check_pt_tf_outputs(_lowercase	,   _lowercase	,   _lowercase	,   _lowercase	,   _lowercase	,   _lowercase							)
	def 							lowerCamelCase__		(				self							)    ->	List[Any]:
							lowercase_   : Optional[Any]   	=      self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_for_image_classification(*_lowercase							)







	@slow
	def 							lowerCamelCase__		(				self							)    ->	Dict:
							for model_name in DATA2VEC_VISION_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
													lowercase_   : int   	=      DataaVecVisionModel.from_pretrained(_lowercase							)
													self.assertIsNotNone(_lowercase							)






def 						_UpperCAmelCase     (	)      ->      str:





						"""simple docstring"""
						lowercase_   : int   	=      Image.open('./tests/fixtures/tests_samples/COCO/000000039769.png'				)
						return image






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







	"""simple docstring"""



	@cached_property
	def 							lowerCamelCase__		(				self							)    ->	str:
							return (
							    BeitImageProcessor.from_pretrained('facebook/data2vec-vision-base-ft1k'							) if is_vision_available() else None
							)







	@slow
	def 							lowerCamelCase__		(				self							)    ->	Union[str, Any]:
							lowercase_   : Any   	=      DataaVecVisionForImageClassification.from_pretrained('facebook/data2vec-vision-base-ft1k'							).to(
							    _lowercase							)

							lowercase_   : str   	=      self.default_image_processor
							lowercase_   : List[Any]   	=      prepare_img()
							lowercase_   : List[Any]   	=      image_processor(images=_lowercase	,   return_tensors='pt'							).to(_lowercase							)

							# forward pass
							with torch.no_grad():
													lowercase_   : List[Any]   	=      model(**_lowercase							)
							lowercase_   : List[Any]   	=      outputs.logits

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

							lowercase_   : Optional[Any]   	=      torch.tensor([0.32_77, -0.13_95, 0.09_11]							).to(_lowercase							)

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

							lowercase_   : Optional[int]   	=      [model.config.labelaid[i] for i in ['remote control, remote', 'tabby, tabby cat']]
							self.assertEqual(logits[0].topk(2							).indices.cpu().tolist()	,   _lowercase							)








'''simple docstring'''

import importlib
import sys
from argparse import REMAINDER, ArgumentParser
from pathlib import Path

import torch_xla.distributed.xla_multiprocessing as xmp






def 						_UpperCAmelCase     (	)      ->      Optional[Any]:





						"""simple docstring"""
						lowercase_   : List[str]   	=      ArgumentParser(
						    description=(
						        'PyTorch TPU distributed training launch helper utility that will spawn up multiple distributed processes'
						    )				)

						# Optional arguments for the launch helper
						parser.add_argument('--num_cores'    ,      type=a    ,      default=1    ,      help='Number of TPU cores to use (1 or 8).'				)

						# positional
						parser.add_argument(
						    'training_script'    ,      type=a    ,      help=(
						        'The full path to the single TPU training '
						        'program/script to be launched in parallel, '
						        'followed by all the arguments for the '
						        'training script'
						    )    ,      )

						# rest from the training program
						parser.add_argument('training_script_args'    ,      nargs=a				)

						return parser.parse_args()






def 						_UpperCAmelCase     (	)      ->      str:





						"""simple docstring"""
						lowercase_   : Optional[int]   	=      parse_args()

						# Import training_script as a module.
						lowercase_   : str   	=      Path(args.training_script				)
						sys.path.append(str(script_fpath.parent.resolve()				)				)
						lowercase_   : int   	=      script_fpath.stem
						lowercase_   : List[Any]   	=      importlib.import_module(a				)

						# Patch sys.argv
						lowercase_   : List[str]   	=      [args.training_script] + args.training_script_args + ['--tpu_num_cores', str(args.num_cores				)]

						xmp.spawn(mod._mp_fn    ,      args=()    ,      nprocs=args.num_cores				)


if __name__ == "__main__":
						main()
