

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



							'''simple docstring'''







							def __init__(    self     ,						_lowerCamelCase     ,						_lowerCamelCase=100     ,						_lowerCamelCase=13     ,						_lowerCamelCase=30     ,						_lowerCamelCase=2     ,						_lowerCamelCase=3     ,						_lowerCamelCase=True     ,						_lowerCamelCase=True     ,						_lowerCamelCase=32     ,						_lowerCamelCase=4     ,						_lowerCamelCase=4     ,						_lowerCamelCase=37     ,						_lowerCamelCase="gelu"     ,						_lowerCamelCase=0.1     ,						_lowerCamelCase=0.1     ,						_lowerCamelCase=10     ,						_lowerCamelCase=0.02     ,						_lowerCamelCase=3     ,						_lowerCamelCase=None     ,						_lowerCamelCase=[0, 1, 2, 3]     ,						) ->       Dict:




										'''simple docstring'''
										__lowercase						      =					parent
										__lowercase						      =					100
										__lowercase						      =					batch_size
										__lowercase						      =					image_size
										__lowercase						      =					patch_size
										__lowercase						      =					num_channels
										__lowercase						      =					is_training
										__lowercase						      =					use_labels
										__lowercase						      =					hidden_size
										__lowercase						      =					num_hidden_layers
										__lowercase						      =					num_attention_heads
										__lowercase						      =					intermediate_size
										__lowercase						      =					hidden_act
										__lowercase						      =					hidden_dropout_prob
										__lowercase						      =					attention_probs_dropout_prob
										__lowercase						      =					type_sequence_label_size
										__lowercase						      =					initializer_range
										__lowercase						      =					scope
										__lowercase						      =					out_indices
										__lowercase						      =					num_labels

										# in BeiT, the seq length equals the number of patches + 1 (we add 1 for the [CLS] token)
										__lowercase						      =					(image_size // patch_size) ** 2
										__lowercase						      =					num_patches + 1
							def      SCREAMING_SNAKE_CASE	(    self					) ->       Union[str, Any]:




										'''simple docstring'''
										__lowercase						      =					floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]					)

										__lowercase						      =					None
										__lowercase						      =					None
										if self.use_labels:
													__lowercase						      =					ids_tensor([self.batch_size]     ,						self.type_sequence_label_size					)
													__lowercase						      =					ids_tensor([self.batch_size, self.image_size, self.image_size]     ,						self.num_labels					)

										__lowercase						      =					self.get_config()

										return config, pixel_values, labels, pixel_labels
							def      SCREAMING_SNAKE_CASE	(    self					) ->       str:




										'''simple docstring'''
										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=lowerCamelCase__     ,						initializer_range=self.initializer_range     ,						out_indices=self.out_indices     ,						)
							def      SCREAMING_SNAKE_CASE	(    self     ,						_lowerCamelCase     ,						_lowerCamelCase     ,						_lowerCamelCase     ,						_lowerCamelCase					) ->       Optional[int]:




										'''simple docstring'''
										__lowercase						      =					DataaVecVisionModel(config=lowerCamelCase__					)
										model.to(lowerCamelCase__					)
										model.eval()
										__lowercase						      =					model(lowerCamelCase__					)
										# expected sequence length = num_patches + 1 (we add 1 for the [CLS] token)
										__lowercase						      =					(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      SCREAMING_SNAKE_CASE	(    self     ,						_lowerCamelCase     ,						_lowerCamelCase     ,						_lowerCamelCase     ,						_lowerCamelCase					) ->       str:




										'''simple docstring'''
										__lowercase						      =					self.type_sequence_label_size
										__lowercase						      =					DataaVecVisionForImageClassification(lowerCamelCase__					)
										model.to(lowerCamelCase__					)
										model.eval()
										__lowercase						      =					model(lowerCamelCase__     ,						labels=lowerCamelCase__					)
										self.parent.assertEqual(result.logits.shape     ,						(self.batch_size, self.type_sequence_label_size)					)
							def      SCREAMING_SNAKE_CASE	(    self     ,						_lowerCamelCase     ,						_lowerCamelCase     ,						_lowerCamelCase     ,						_lowerCamelCase					) ->       List[str]:




										'''simple docstring'''
										__lowercase						      =					self.num_labels
										__lowercase						      =					DataaVecVisionForSemanticSegmentation(lowerCamelCase__					)
										model.to(lowerCamelCase__					)
										model.eval()
										__lowercase						      =					model(lowerCamelCase__					)
										self.parent.assertEqual(
										    result.logits.shape     ,						(self.batch_size, self.num_labels, self.image_size * 2, self.image_size * 2)					)
										__lowercase						      =					model(lowerCamelCase__     ,						labels=lowerCamelCase__					)
										self.parent.assertEqual(
										    result.logits.shape     ,						(self.batch_size, self.num_labels, self.image_size * 2, self.image_size * 2)					)





							def      SCREAMING_SNAKE_CASE	(    self					) ->       int:




										'''simple docstring'''
										__lowercase						      =					self.prepare_config_and_inputs()
										__lowercase						      =					config_and_inputs
										__lowercase						      =					{"pixel_values": pixel_values}
										return config, inputs_dict





@require_torch
class 				__a    (   _a				,      _a				,      unittest.TestCase					):



							'''simple docstring'''







							_lowerCamelCase				: Optional[int]					      =      (
							    (DataaVecVisionModel, DataaVecVisionForImageClassification, DataaVecVisionForSemanticSegmentation)
							    if is_torch_available()
							    else ()
							)
							_lowerCamelCase				: Union[str, Any]					      =      (
							    {
							        """feature-extraction""": DataaVecVisionModel,
							        """image-classification""": DataaVecVisionForImageClassification,
							        """image-segmentation""": DataaVecVisionForSemanticSegmentation,
							    }
							    if is_torch_available()
							    else {}
							)

							_lowerCamelCase				: int					      =      False
							_lowerCamelCase				: List[str]					      =      False
							_lowerCamelCase				: Tuple					      =      False
							def      SCREAMING_SNAKE_CASE	(    self					) ->       str:




										'''simple docstring'''
										__lowercase						      =					DataaVecVisionModelTester(self					)
										__lowercase						      =					ConfigTester(
										    self     ,						config_class=lowerCamelCase__     ,						has_text_modality=lowerCamelCase__     ,						hidden_size=37					)
							def      SCREAMING_SNAKE_CASE	(    self					) ->       Dict:




										'''simple docstring'''
										self.config_tester.run_common_tests()
							def      SCREAMING_SNAKE_CASE	(    self					) ->       Dict:




										'''simple docstring'''
										# 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      SCREAMING_SNAKE_CASE	(    self					) ->       Optional[Any]:




										'''simple docstring'''
										pass
							def      SCREAMING_SNAKE_CASE	(    self					) ->       List[Any]:




										'''simple docstring'''
										__lowercase						      =					self.model_tester.prepare_config_and_inputs_for_common()

										for model_class in self.all_model_classes:
													__lowercase						      =					model_class(lowerCamelCase__					)
													self.assertIsInstance(model.get_input_embeddings()     ,						(nn.Module)					)
													__lowercase						      =					model.get_output_embeddings()
													self.assertTrue(x is None or isinstance(lowerCamelCase__     ,						nn.Linear					)					)
							def      SCREAMING_SNAKE_CASE	(    self					) ->       str:




										'''simple docstring'''
										__lowercase						      =					self.model_tester.prepare_config_and_inputs_for_common()

										for model_class in self.all_model_classes:
													__lowercase						      =					model_class(lowerCamelCase__					)
													__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]     ,						lowerCamelCase__					)
							def      SCREAMING_SNAKE_CASE	(    self					) ->       List[str]:




										'''simple docstring'''
										__lowercase						      =					self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_model(*lowerCamelCase__					)
							def      SCREAMING_SNAKE_CASE	(    self					) ->       Optional[int]:




										'''simple docstring'''
										__lowercase						      =					self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_for_image_segmentation(*lowerCamelCase__					)
							def      SCREAMING_SNAKE_CASE	(    self					) ->       List[str]:




										'''simple docstring'''
										if not self.model_tester.is_training:
													return

										__lowercase						      =					self.model_tester.prepare_config_and_inputs_for_common()
										__lowercase						      =					True

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

													__lowercase						      =					model_class(lowerCamelCase__					)
													model.to(lowerCamelCase__					)
													model.train()
													__lowercase						      =					self._prepare_for_class(lowerCamelCase__     ,						lowerCamelCase__     ,						return_labels=lowerCamelCase__					)
													__lowercase						      =					model(**lowerCamelCase__					).loss
													loss.backward()
							def      SCREAMING_SNAKE_CASE	(    self					) ->       Tuple:




										'''simple docstring'''
										__lowercase						      =					self.model_tester.prepare_config_and_inputs_for_common()
										if not self.model_tester.is_training:
													return

										__lowercase						      =					False
										__lowercase						      =					True

										for model_class in self.all_model_classes:
													if model_class in [*get_values(lowerCamelCase__					)] 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						      =					inputs_dict["pixel_values"].shape
																__lowercase						      =					torch.zeros(
																    [self.model_tester.batch_size, height, width]     ,						device=lowerCamelCase__					).long()
													__lowercase						      =					model_class(lowerCamelCase__					)
													model.gradient_checkpointing_enable()
													model.to(lowerCamelCase__					)
													model.train()
													__lowercase						      =					self._prepare_for_class(lowerCamelCase__     ,						lowerCamelCase__     ,						return_labels=lowerCamelCase__					)
													__lowercase						      =					model(**lowerCamelCase__					).loss
													loss.backward()
							def      SCREAMING_SNAKE_CASE	(    self					) ->       Any:




										'''simple docstring'''
										__lowercase						      =					self.model_tester.prepare_config_and_inputs_for_common()

										__lowercase						      =					_config_zero_init(lowerCamelCase__					)
										for model_class in self.all_model_classes:
													__lowercase						      =					model_class(config=lowerCamelCase__					)
													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      SCREAMING_SNAKE_CASE	(    self     ,						_lowerCamelCase     ,						_lowerCamelCase     ,						_lowerCamelCase     ,						_lowerCamelCase=2e-4     ,						_lowerCamelCase="outputs"     ,						_lowerCamelCase=None					) ->       int:




										'''simple docstring'''
										# We override with a slightly higher tol value, as semseg models tend to diverge a bit more
										super().check_pt_tf_outputs(lowerCamelCase__     ,						lowerCamelCase__     ,						lowerCamelCase__     ,						lowerCamelCase__     ,						lowerCamelCase__     ,						lowerCamelCase__					)
							def      SCREAMING_SNAKE_CASE	(    self					) ->       Optional[Any]:




										'''simple docstring'''
										__lowercase						      =					self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_for_image_classification(*lowerCamelCase__					)





							@slow
							def      SCREAMING_SNAKE_CASE	(    self					) ->       Optional[int]:




										'''simple docstring'''
										for model_name in DATA2VEC_VISION_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
													__lowercase						      =					DataaVecVisionModel.from_pretrained(lowerCamelCase__					)
													self.assertIsNotNone(lowerCamelCase__					)





def     lowerCAmelCase__ (		)	->int:
			__lowercase						      =					Image.open("./tests/fixtures/tests_samples/COCO/000000039769.png"			)
			return image







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



							'''simple docstring'''







							@cached_property
							def      SCREAMING_SNAKE_CASE	(    self					) ->       Tuple:




										'''simple docstring'''
										return (
										    BeitImageProcessor.from_pretrained("facebook/data2vec-vision-base-ft1k"					) if is_vision_available() else None
										)





							@slow
							def      SCREAMING_SNAKE_CASE	(    self					) ->       str:




										'''simple docstring'''
										__lowercase						      =					DataaVecVisionForImageClassification.from_pretrained("facebook/data2vec-vision-base-ft1k"					).to(
										    lowerCamelCase__					)

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

										# forward pass
										with torch.no_grad():
													__lowercase						      =					model(**lowerCamelCase__					)
										__lowercase						      =					outputs.logits

										# verify the logits
										__lowercase						      =					torch.Size((1, 1_000)					)
										self.assertEqual(logits.shape     ,						lowerCamelCase__					)

										__lowercase						      =					torch.tensor([0.3277, -0.1395, 0.0911]					).to(lowerCamelCase__					)

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

										__lowercase						      =					[model.config.labelaid[i] for i in ["remote control, remote", "tabby, tabby cat"]]
										self.assertEqual(logits[0].topk(2					).indices.cpu().tolist()     ,						lowerCamelCase__					)







"""simple docstring"""






import os

from packaging import version

from .. import __version__
from .accelerate_utils import apply_forward_hook
from .constants import (
    CONFIG_NAME,
    DEPRECATED_REVISION_ARGS,
    DIFFUSERS_CACHE,
    DIFFUSERS_DYNAMIC_MODULE_NAME,
    FLAX_WEIGHTS_NAME,
    HF_MODULES_CACHE,
    HUGGINGFACE_CO_RESOLVE_ENDPOINT,
    ONNX_EXTERNAL_WEIGHTS_NAME,
    ONNX_WEIGHTS_NAME,
    SAFETENSORS_WEIGHTS_NAME,
    TEXT_ENCODER_ATTN_MODULE,
    WEIGHTS_NAME,
)
from .deprecation_utils import deprecate
from .doc_utils import replace_example_docstring
from .dynamic_modules_utils import get_class_from_dynamic_module
from .hub_utils import (
    HF_HUB_OFFLINE,
    _add_variant,
    _get_model_file,
    extract_commit_hash,
    http_user_agent,
)
from .import_utils import (
    BACKENDS_MAPPING,
    ENV_VARS_TRUE_AND_AUTO_VALUES,
    ENV_VARS_TRUE_VALUES,
    USE_JAX,
    USE_TF,
    USE_TORCH,
    DummyObject,
    OptionalDependencyNotAvailable,
    is_accelerate_available,
    is_accelerate_version,
    is_bsa_available,
    is_flax_available,
    is_ftfy_available,
    is_inflect_available,
    is_invisible_watermark_available,
    is_k_diffusion_available,
    is_k_diffusion_version,
    is_librosa_available,
    is_note_seq_available,
    is_omegaconf_available,
    is_onnx_available,
    is_safetensors_available,
    is_scipy_available,
    is_tensorboard_available,
    is_tf_available,
    is_torch_available,
    is_torch_version,
    is_torchsde_available,
    is_transformers_available,
    is_transformers_version,
    is_unidecode_available,
    is_wandb_available,
    is_xformers_available,
    requires_backends,
)
from .logging import get_logger
from .outputs import BaseOutput
from .pil_utils import PIL_INTERPOLATION, numpy_to_pil, pt_to_pil
from .torch_utils import is_compiled_module, randn_tensor


if is_torch_available():
					from .testing_utils import (
					    floats_tensor,
					    load_hf_numpy,
					    load_image,
					    load_numpy,
					    load_pt,
					    nightly,
					    parse_flag_from_env,
					    print_tensor_test,
					    require_torch_a,
					    require_torch_gpu,
					    skip_mps,
					    slow,
					    torch_all_close,
					    torch_device,
					)
					from .torch_utils import maybe_allow_in_graph

from .testing_utils import export_to_gif, export_to_video


_lowercase							 =							get_logger(__name__)



def     lowerCAmelCase__ (		__magic_name__			)	->Optional[int]:
			if version.parse(__magic_name__			) < version.parse(__magic_name__			):
						if "dev" in min_version:
									__lowercase						      =					(
									    "This example requires a source install from HuggingFace diffusers (see "
									    "`https://huggingface.co/docs/diffusers/installation#install-from-source`),"
									)
						else:
									__lowercase						      =					F'''This example requires a minimum version of {min_version},'''
						error_message += F''' but the version found is {__version__}.\n'''
						raise ImportError(__magic_name__			)





