



'''simple docstring'''




import contextlib
import copy
import random
from typing import Any, Dict, Iterable, Optional, Union

import numpy as np
import torch

from .utils import deprecate, is_transformers_available


if is_transformers_available():
		import transformers


def        lowerCAmelCase_     (			_lowerCamelCase:     int      ):
		random.seed(_UpperCamelCase      )
		np.random.seed(_UpperCamelCase      )
		torch.manual_seed(_UpperCamelCase      )
		torch.cuda.manual_seed_all(_UpperCamelCase      )
		# ^^ safe to call this function even if cuda is not available






class 		_UpperCamelCase						:


			'''simple docstring'''

			def __init__(							self  :				Optional[int]							,  lowerCAmelCase__  :				Iterable[torch.nn.Parameter]							,  lowerCAmelCase__  :				float = 0.99_99							,  lowerCAmelCase__  :				float = 0.0							,  lowerCAmelCase__  :				int = 0							,  lowerCAmelCase__  :				bool = False							,  lowerCAmelCase__  :				Union[float, int] = 1.0							,  lowerCAmelCase__  :				Union[float, int] = 2 / 3							,  lowerCAmelCase__  :				Optional[Any] = None							,  lowerCAmelCase__  :				Dict[str, Any] = None							,  **lowerCAmelCase__  :				Optional[int]							,  ):





					"""simple docstring"""






					if isinstance(UpperCAmelCase__							,  torch.nn.Module		):
							__SCREAMING_SNAKE_CASE :	Dict					 =	(
							    """Passing a `torch.nn.Module` to `ExponentialMovingAverage` is deprecated. """
							    """Please pass the parameters of the module instead."""
							)
							deprecate(
							    """passing a `torch.nn.Module` to `ExponentialMovingAverage`"""							,  """1.0.0"""							,  UpperCAmelCase__							,  standard_warn=UpperCAmelCase__							,  )
							__SCREAMING_SNAKE_CASE :	str					 =	parameters.parameters()

							# set use_ema_warmup to True if a torch.nn.Module is passed for backwards compatibility
							__SCREAMING_SNAKE_CASE :	Tuple					 =	True

					if kwargs.get("""max_value"""							,  UpperCAmelCase__		) is not None:
							__SCREAMING_SNAKE_CASE :	int					 =	"""The `max_value` argument is deprecated. Please use `decay` instead."""
							deprecate("""max_value"""							,  """1.0.0"""							,  UpperCAmelCase__							,  standard_warn=UpperCAmelCase__		)
							__SCREAMING_SNAKE_CASE :	Union[str, Any]					 =	kwargs["""max_value"""]

					if kwargs.get("""min_value"""							,  UpperCAmelCase__		) is not None:
							__SCREAMING_SNAKE_CASE :	Optional[Any]					 =	"""The `min_value` argument is deprecated. Please use `min_decay` instead."""
							deprecate("""min_value"""							,  """1.0.0"""							,  UpperCAmelCase__							,  standard_warn=UpperCAmelCase__		)
							__SCREAMING_SNAKE_CASE :	Any					 =	kwargs["""min_value"""]

					__SCREAMING_SNAKE_CASE :	Dict					 =	list(UpperCAmelCase__		)
					__SCREAMING_SNAKE_CASE :	List[str]					 =	[p.clone().detach() for p in parameters]

					if kwargs.get("""device"""							,  UpperCAmelCase__		) is not None:
							__SCREAMING_SNAKE_CASE :	Optional[int]					 =	"""The `device` argument is deprecated. Please use `to` instead."""
							deprecate("""device"""							,  """1.0.0"""							,  UpperCAmelCase__							,  standard_warn=UpperCAmelCase__		)
							self.to(device=kwargs["""device"""]		)

					__SCREAMING_SNAKE_CASE :	int					 =	None

					__SCREAMING_SNAKE_CASE :	Tuple					 =	decay
					__SCREAMING_SNAKE_CASE :	List[Any]					 =	min_decay
					__SCREAMING_SNAKE_CASE :	int					 =	update_after_step
					__SCREAMING_SNAKE_CASE :	int					 =	use_ema_warmup
					__SCREAMING_SNAKE_CASE :	Union[str, Any]					 =	inv_gamma
					__SCREAMING_SNAKE_CASE :	str					 =	power
					__SCREAMING_SNAKE_CASE :	Dict					 =	0
					__SCREAMING_SNAKE_CASE :	Dict					 =	None  # set in `step()`

					__SCREAMING_SNAKE_CASE :	int					 =	model_cls
					__SCREAMING_SNAKE_CASE :	Optional[int]					 =	model_config

			@classmethod
			def UpperCamelCase__      (							cls  :				Union[str, Any]							,  lowerCAmelCase__  :				str							,  lowerCAmelCase__  :				List[Any]		):





					"""simple docstring"""






					__SCREAMING_SNAKE_CASE ,		__SCREAMING_SNAKE_CASE :	Dict					 =	model_cls.load_config(UpperCAmelCase__							,  return_unused_kwargs=UpperCAmelCase__		)
					__SCREAMING_SNAKE_CASE :	Optional[Any]					 =	model_cls.from_pretrained(UpperCAmelCase__		)

					__SCREAMING_SNAKE_CASE :	Optional[int]					 =	cls(model.parameters()							,  model_cls=UpperCAmelCase__							,  model_config=model.config		)

					ema_model.load_state_dict(UpperCAmelCase__		)
					return ema_model

			def UpperCamelCase__      (							self  :				List[Any]							,  lowerCAmelCase__  :				Optional[Any]		):





					"""simple docstring"""






					if self.model_cls is None:
							raise ValueError("""`save_pretrained` can only be used if `model_cls` was defined at __init__."""		)

					if self.model_config is None:
							raise ValueError("""`save_pretrained` can only be used if `model_config` was defined at __init__."""		)

					__SCREAMING_SNAKE_CASE :	Union[str, Any]					 =	self.model_cls.from_config(self.model_config		)
					__SCREAMING_SNAKE_CASE :	Tuple					 =	self.state_dict()
					state_dict.pop("""shadow_params"""							,  UpperCAmelCase__		)

					model.register_to_config(**UpperCAmelCase__		)
					self.copy_to(model.parameters()		)
					model.save_pretrained(UpperCAmelCase__		)

			def UpperCamelCase__      (							self  :				Optional[Any]							,  lowerCAmelCase__  :				int		):





					"""simple docstring"""






					__SCREAMING_SNAKE_CASE :	Optional[int]					 =	max(0							,  optimization_step - self.update_after_step - 1		)

					if step <= 0:
							return 0.0

					if self.use_ema_warmup:
							__SCREAMING_SNAKE_CASE :	int					 =	1 - (1 + step / self.inv_gamma) ** -self.power
					else:
							__SCREAMING_SNAKE_CASE :	Union[str, Any]					 =	(1 + step) / (1_0 + step)

					__SCREAMING_SNAKE_CASE :	int					 =	min(UpperCAmelCase__							,  self.decay		)
					# make sure decay is not smaller than min_decay
					__SCREAMING_SNAKE_CASE :	Dict					 =	max(UpperCAmelCase__							,  self.min_decay		)
					return cur_decay_value

			@torch.no_grad()
			def UpperCamelCase__      (							self  :				Optional[Any]							,  lowerCAmelCase__  :				Iterable[torch.nn.Parameter]		):





					"""simple docstring"""






					if isinstance(UpperCAmelCase__							,  torch.nn.Module		):
							__SCREAMING_SNAKE_CASE :	str					 =	(
							    """Passing a `torch.nn.Module` to `ExponentialMovingAverage.step` is deprecated. """
							    """Please pass the parameters of the module instead."""
							)
							deprecate(
							    """passing a `torch.nn.Module` to `ExponentialMovingAverage.step`"""							,  """1.0.0"""							,  UpperCAmelCase__							,  standard_warn=UpperCAmelCase__							,  )
							__SCREAMING_SNAKE_CASE :	str					 =	parameters.parameters()

					__SCREAMING_SNAKE_CASE :	Optional[int]					 =	list(UpperCAmelCase__		)

					self.optimization_step += 1

					# Compute the decay factor for the exponential moving average.
					__SCREAMING_SNAKE_CASE :	Optional[Any]					 =	self.get_decay(self.optimization_step		)
					__SCREAMING_SNAKE_CASE :	Dict					 =	decay
					__SCREAMING_SNAKE_CASE :	Dict					 =	1 - decay

					__SCREAMING_SNAKE_CASE :	Dict					 =	contextlib.nullcontext
					if is_transformers_available() and transformers.deepspeed.is_deepspeed_zeroa_enabled():
							import deepspeed

					for s_param, param in zip(self.shadow_params							,  UpperCAmelCase__		):
							if is_transformers_available() and transformers.deepspeed.is_deepspeed_zeroa_enabled():
									__SCREAMING_SNAKE_CASE :	Optional[int]					 =	deepspeed.zero.GatheredParameters(UpperCAmelCase__							,  modifier_rank=UpperCAmelCase__		)

							with context_manager():
									if param.requires_grad:
											s_param.sub_(one_minus_decay * (s_param - param)		)
									else:
											s_param.copy_(UpperCAmelCase__		)

			def UpperCamelCase__      (							self  :				Tuple							,  lowerCAmelCase__  :				Iterable[torch.nn.Parameter]		):





					"""simple docstring"""






					__SCREAMING_SNAKE_CASE :	Union[str, Any]					 =	list(UpperCAmelCase__		)
					for s_param, param in zip(self.shadow_params							,  UpperCAmelCase__		):
							param.data.copy_(s_param.to(param.device		).data		)

			def UpperCamelCase__      (							self  :				int							,  lowerCAmelCase__  :				List[Any]=None							,  lowerCAmelCase__  :				str=None		):





					"""simple docstring"""






					__SCREAMING_SNAKE_CASE :	Union[str, Any]					 =	[
					    p.to(device=UpperCAmelCase__							,  dtype=UpperCAmelCase__		) if p.is_floating_point() else p.to(device=UpperCAmelCase__		)
					    for p in self.shadow_params
					]

			def UpperCamelCase__      (							self  :				Tuple		):





					"""simple docstring"""






					return {
					    "decay": self.decay,
					    "min_decay": self.min_decay,
					    "optimization_step": self.optimization_step,
					    "update_after_step": self.update_after_step,
					    "use_ema_warmup": self.use_ema_warmup,
					    "inv_gamma": self.inv_gamma,
					    "power": self.power,
					    "shadow_params": self.shadow_params,
					}

			def UpperCamelCase__      (							self  :				Union[str, Any]							,  lowerCAmelCase__  :				Iterable[torch.nn.Parameter]		):





					"""simple docstring"""






					__SCREAMING_SNAKE_CASE :	Union[str, Any]					 =	[param.detach().cpu().clone() for param in parameters]

			def UpperCamelCase__      (							self  :				int							,  lowerCAmelCase__  :				Iterable[torch.nn.Parameter]		):





					"""simple docstring"""






					if self.temp_stored_params is None:
							raise RuntimeError("""This ExponentialMovingAverage has no `store()`ed weights """ """to `restore()`"""		)
					for c_param, param in zip(self.temp_stored_params							,  UpperCAmelCase__		):
							param.data.copy_(c_param.data		)

					# Better memory-wise.
					__SCREAMING_SNAKE_CASE :	Optional[int]					 =	None



			def UpperCamelCase__      (							self  :				Optional[Any]							,  lowerCAmelCase__  :				dict		):





					"""simple docstring"""






					__SCREAMING_SNAKE_CASE :	str					 =	copy.deepcopy(UpperCAmelCase__		)

					__SCREAMING_SNAKE_CASE :	Union[str, Any]					 =	state_dict.get("""decay"""							,  self.decay		)
					if self.decay < 0.0 or self.decay > 1.0:
							raise ValueError("""Decay must be between 0 and 1"""		)

					__SCREAMING_SNAKE_CASE :	Optional[int]					 =	state_dict.get("""min_decay"""							,  self.min_decay		)
					if not isinstance(self.min_decay							,  UpperCAmelCase__		):
							raise ValueError("""Invalid min_decay"""		)

					__SCREAMING_SNAKE_CASE :	List[str]					 =	state_dict.get("""optimization_step"""							,  self.optimization_step		)
					if not isinstance(self.optimization_step							,  UpperCAmelCase__		):
							raise ValueError("""Invalid optimization_step"""		)

					__SCREAMING_SNAKE_CASE :	int					 =	state_dict.get("""update_after_step"""							,  self.update_after_step		)
					if not isinstance(self.update_after_step							,  UpperCAmelCase__		):
							raise ValueError("""Invalid update_after_step"""		)

					__SCREAMING_SNAKE_CASE :	Dict					 =	state_dict.get("""use_ema_warmup"""							,  self.use_ema_warmup		)
					if not isinstance(self.use_ema_warmup							,  UpperCAmelCase__		):
							raise ValueError("""Invalid use_ema_warmup"""		)

					__SCREAMING_SNAKE_CASE :	Dict					 =	state_dict.get("""inv_gamma"""							,  self.inv_gamma		)
					if not isinstance(self.inv_gamma							,  (float, int)		):
							raise ValueError("""Invalid inv_gamma"""		)

					__SCREAMING_SNAKE_CASE :	str					 =	state_dict.get("""power"""							,  self.power		)
					if not isinstance(self.power							,  (float, int)		):
							raise ValueError("""Invalid power"""		)

					__SCREAMING_SNAKE_CASE :	Optional[int]					 =	state_dict.get("""shadow_params"""							,  UpperCAmelCase__		)
					if shadow_params is not None:
							__SCREAMING_SNAKE_CASE :	Any					 =	shadow_params
							if not isinstance(self.shadow_params							,  UpperCAmelCase__		):
									raise ValueError("""shadow_params must be a list"""		)
							if not all(isinstance(UpperCAmelCase__							,  torch.Tensor		) for p in self.shadow_params		):
									raise ValueError("""shadow_params must all be Tensors"""		)




'''simple docstring'''




import inspect
import unittest
from math import floor

from transformers import CvtConfig
from transformers.file_utils import cached_property, is_torch_available, is_vision_available
from transformers.testing_utils import require_torch, require_vision, slow, torch_device

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 transformers import CvtForImageClassification, CvtModel
						from transformers.models.cvt.modeling_cvt import CVT_PRETRAINED_MODEL_ARCHIVE_LIST


if is_vision_available():
						from PIL import Image

						from transformers import AutoImageProcessor






class 		_UpperCamelCase						(							lowerCamelCase__					):


			'''simple docstring'''

			def UpperCamelCase__      (							self  :				Any		):





					"""simple docstring"""






					__SCREAMING_SNAKE_CASE :	int					 =	self.config_class(**self.inputs_dict		)
					self.parent.assertTrue(hasattr(lowerCAmelCase__							,  """embed_dim"""		)		)
					self.parent.assertTrue(hasattr(lowerCAmelCase__							,  """num_heads"""		)		)






class 		_UpperCamelCase						:


			'''simple docstring'''

			def __init__(							self  :				str							,  lowerCAmelCase__  :				int							,  lowerCAmelCase__  :				int=1_3							,  lowerCAmelCase__  :				Union[str, Any]=6_4							,  lowerCAmelCase__  :				Optional[Any]=3							,  lowerCAmelCase__  :				str=[1_6, 4_8, 9_6]							,  lowerCAmelCase__  :				Any=[1, 3, 6]							,  lowerCAmelCase__  :				Any=[1, 2, 1_0]							,  lowerCAmelCase__  :				Union[str, Any]=[7, 3, 3]							,  lowerCAmelCase__  :				Tuple=[4, 2, 2]							,  lowerCAmelCase__  :				Union[str, Any]=[2, 1, 1]							,  lowerCAmelCase__  :				Dict=[2, 2, 2]							,  lowerCAmelCase__  :				Optional[int]=[False, False, True]							,  lowerCAmelCase__  :				List[Any]=[0.0, 0.0, 0.0]							,  lowerCAmelCase__  :				List[Any]=0.02							,  lowerCAmelCase__  :				List[str]=1E-12							,  lowerCAmelCase__  :				Any=True							,  lowerCAmelCase__  :				Union[str, Any]=True							,  lowerCAmelCase__  :				Optional[Any]=2							,  ):





					"""simple docstring"""






					__SCREAMING_SNAKE_CASE :	int					 =	parent
					__SCREAMING_SNAKE_CASE :	Optional[Any]					 =	batch_size
					__SCREAMING_SNAKE_CASE :	List[Any]					 =	image_size
					__SCREAMING_SNAKE_CASE :	Any					 =	patch_sizes
					__SCREAMING_SNAKE_CASE :	List[Any]					 =	patch_stride
					__SCREAMING_SNAKE_CASE :	List[Any]					 =	patch_padding
					__SCREAMING_SNAKE_CASE :	Union[str, Any]					 =	is_training
					__SCREAMING_SNAKE_CASE :	Dict					 =	use_labels
					__SCREAMING_SNAKE_CASE :	int					 =	num_labels
					__SCREAMING_SNAKE_CASE :	Optional[int]					 =	num_channels
					__SCREAMING_SNAKE_CASE :	List[str]					 =	embed_dim
					__SCREAMING_SNAKE_CASE :	Optional[Any]					 =	num_heads
					__SCREAMING_SNAKE_CASE :	Optional[int]					 =	stride_kv
					__SCREAMING_SNAKE_CASE :	Union[str, Any]					 =	depth
					__SCREAMING_SNAKE_CASE :	Optional[Any]					 =	cls_token
					__SCREAMING_SNAKE_CASE :	List[Any]					 =	attention_drop_rate
					__SCREAMING_SNAKE_CASE :	str					 =	initializer_range
					__SCREAMING_SNAKE_CASE :	List[Any]					 =	layer_norm_eps

			def UpperCamelCase__      (							self  :				Dict		):





					"""simple docstring"""






					__SCREAMING_SNAKE_CASE :	Union[str, Any]					 =	floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]		)

					__SCREAMING_SNAKE_CASE :	Dict					 =	None
					if self.use_labels:
							__SCREAMING_SNAKE_CASE :	int					 =	ids_tensor([self.batch_size]							,  self.num_labels		)

					__SCREAMING_SNAKE_CASE :	Any					 =	self.get_config()
					return config, pixel_values, labels

			def UpperCamelCase__      (							self  :				str		):





					"""simple docstring"""






					return CvtConfig(
					    image_size=self.image_size							,  num_labels=self.num_labels							,  num_channels=self.num_channels							,  embed_dim=self.embed_dim							,  num_heads=self.num_heads							,  patch_sizes=self.patch_sizes							,  patch_padding=self.patch_padding							,  patch_stride=self.patch_stride							,  stride_kv=self.stride_kv							,  depth=self.depth							,  cls_token=self.cls_token							,  attention_drop_rate=self.attention_drop_rate							,  initializer_range=self.initializer_range							,  )

			def UpperCamelCase__      (							self  :				Union[str, Any]							,  lowerCAmelCase__  :				int							,  lowerCAmelCase__  :				str							,  lowerCAmelCase__  :				Optional[int]		):





					"""simple docstring"""






					__SCREAMING_SNAKE_CASE :	Union[str, Any]					 =	CvtModel(config=lowerCAmelCase__		)
					model.to(lowerCAmelCase__		)
					model.eval()
					__SCREAMING_SNAKE_CASE :	int					 =	model(lowerCAmelCase__		)
					__SCREAMING_SNAKE_CASE :	Optional[Any]					 =	(self.image_size, self.image_size)
					__SCREAMING_SNAKE_CASE ,		__SCREAMING_SNAKE_CASE :	Optional[int]					 =	image_size[0], image_size[1]
					for i in range(len(self.depth		)		):
							__SCREAMING_SNAKE_CASE :	Optional[Any]					 =	floor(((height + 2 * self.patch_padding[i] - self.patch_sizes[i]) / self.patch_stride[i]) + 1		)
							__SCREAMING_SNAKE_CASE :	Any					 =	floor(((width + 2 * self.patch_padding[i] - self.patch_sizes[i]) / self.patch_stride[i]) + 1		)
					self.parent.assertEqual(result.last_hidden_state.shape							,  (self.batch_size, self.embed_dim[-1], height, width)		)

			def UpperCamelCase__      (							self  :				int							,  lowerCAmelCase__  :				str							,  lowerCAmelCase__  :				Tuple							,  lowerCAmelCase__  :				Any		):





					"""simple docstring"""






					__SCREAMING_SNAKE_CASE :	Union[str, Any]					 =	self.num_labels
					__SCREAMING_SNAKE_CASE :	Optional[int]					 =	CvtForImageClassification(lowerCAmelCase__		)
					model.to(lowerCAmelCase__		)
					model.eval()
					__SCREAMING_SNAKE_CASE :	str					 =	model(lowerCAmelCase__							,  labels=lowerCAmelCase__		)
					self.parent.assertEqual(result.logits.shape							,  (self.batch_size, self.num_labels)		)



			def UpperCamelCase__      (							self  :				Tuple		):





					"""simple docstring"""






					__SCREAMING_SNAKE_CASE :	Optional[Any]					 =	self.prepare_config_and_inputs()
					__SCREAMING_SNAKE_CASE ,		__SCREAMING_SNAKE_CASE ,		__SCREAMING_SNAKE_CASE :	Any					 =	config_and_inputs
					__SCREAMING_SNAKE_CASE :	Optional[Any]					 =	{"""pixel_values""": pixel_values}
					return config, inputs_dict






@require_torch
class 		_UpperCamelCase						(							lowerCamelCase__					,      lowerCamelCase__					,      unittest.TestCase					):


			'''simple docstring'''

			_A   :	Optional[int]    					=	(CvtModel, CvtForImageClassification) if is_torch_available() else ()
			_A   :	Optional[Any]    					=	(
			    {'''feature-extraction''': CvtModel, '''image-classification''': CvtForImageClassification}
			    if is_torch_available()
			    else {}
			)

			_A   :	Dict    					=	False
			_A   :	Union[str, Any]    					=	False
			_A   :	str    					=	False
			_A   :	List[str]    					=	False
			_A   :	Optional[int]    					=	False

			def UpperCamelCase__      (							self  :				Dict		):





					"""simple docstring"""






					__SCREAMING_SNAKE_CASE :	str					 =	CvtModelTester(self		)
					__SCREAMING_SNAKE_CASE :	Optional[Any]					 =	ConfigTester(self							,  config_class=lowerCAmelCase__							,  has_text_modality=lowerCAmelCase__							,  hidden_size=3_7		)

			def UpperCamelCase__      (							self  :				Union[str, Any]		):





					"""simple docstring"""






					self.create_and_test_config_common_properties()
					self.config_tester.create_and_test_config_to_json_string()
					self.config_tester.create_and_test_config_to_json_file()
					self.config_tester.create_and_test_config_from_and_save_pretrained()
					self.config_tester.create_and_test_config_with_num_labels()
					self.config_tester.check_config_can_be_init_without_params()
					self.config_tester.check_config_arguments_init()

			def UpperCamelCase__      (							self  :				Dict		):





					"""simple docstring"""






					return

			@unittest.skip(reason="""Cvt does not output attentions"""		)
			def UpperCamelCase__      (							self  :				str		):





					"""simple docstring"""






					pass

			@unittest.skip(reason="""Cvt does not use inputs_embeds"""		)
			def UpperCamelCase__      (							self  :				Union[str, Any]		):





					"""simple docstring"""






					pass

			@unittest.skip(reason="""Cvt does not support input and output embeddings"""		)
			def UpperCamelCase__      (							self  :				Optional[Any]		):





					"""simple docstring"""






					pass

			def UpperCamelCase__      (							self  :				Dict		):





					"""simple docstring"""






					__SCREAMING_SNAKE_CASE ,		__SCREAMING_SNAKE_CASE :	Optional[int]					 =	self.model_tester.prepare_config_and_inputs_for_common()

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

							__SCREAMING_SNAKE_CASE :	str					 =	["""pixel_values"""]
							self.assertListEqual(arg_names[:1]							,  lowerCAmelCase__		)

			def UpperCamelCase__      (							self  :				List[Any]		):





					"""simple docstring"""






					__SCREAMING_SNAKE_CASE :	Tuple					 =	self.model_tester.prepare_config_and_inputs()
					self.model_tester.create_and_check_model(*lowerCAmelCase__		)

			def UpperCamelCase__      (							self  :				Dict		):





					"""simple docstring"""






					def check_hidden_states_output(lowerCAmelCase__  :				Any							,  lowerCAmelCase__  :				Any							,  lowerCAmelCase__  :				Union[str, Any]		):
							__SCREAMING_SNAKE_CASE :	List[str]					 =	model_class(lowerCAmelCase__		)
							model.to(lowerCAmelCase__		)
							model.eval()

							with torch.no_grad():
									__SCREAMING_SNAKE_CASE :	Dict					 =	model(**self._prepare_for_class(lowerCAmelCase__							,  lowerCAmelCase__		)		)

							__SCREAMING_SNAKE_CASE :	Optional[int]					 =	outputs.hidden_states

							__SCREAMING_SNAKE_CASE :	Optional[Any]					 =	len(self.model_tester.depth		)
							self.assertEqual(len(lowerCAmelCase__		)							,  lowerCAmelCase__		)

							# verify the first hidden states (first block)
							self.assertListEqual(
							    list(hidden_states[0].shape[-3:]		)							,  [
							        self.model_tester.embed_dim[0],
							        self.model_tester.image_size // 4,
							        self.model_tester.image_size // 4,
							    ]							,  )

					__SCREAMING_SNAKE_CASE ,		__SCREAMING_SNAKE_CASE :	Optional[int]					 =	self.model_tester.prepare_config_and_inputs_for_common()

					for model_class in self.all_model_classes:
							__SCREAMING_SNAKE_CASE :	List[Any]					 =	True
							check_hidden_states_output(lowerCAmelCase__							,  lowerCAmelCase__							,  lowerCAmelCase__		)

							# check that output_hidden_states also work using config
							del inputs_dict["output_hidden_states"]
							__SCREAMING_SNAKE_CASE :	Optional[Any]					 =	True

							check_hidden_states_output(lowerCAmelCase__							,  lowerCAmelCase__							,  lowerCAmelCase__		)

			def UpperCamelCase__      (							self  :				Optional[int]		):





					"""simple docstring"""






					__SCREAMING_SNAKE_CASE :	str					 =	self.model_tester.prepare_config_and_inputs()
					self.model_tester.create_and_check_for_image_classification(*lowerCAmelCase__		)

			@unittest.skip("""Will be fixed soon by reducing the size of the model used for common tests."""		)
			def UpperCamelCase__      (							self  :				Tuple		):





					"""simple docstring"""






					pass



			@slow
			def UpperCamelCase__      (							self  :				List[str]		):





					"""simple docstring"""






					for model_name in CVT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
							__SCREAMING_SNAKE_CASE :	Optional[Any]					 =	CvtModel.from_pretrained(lowerCAmelCase__		)
							self.assertIsNotNone(lowerCAmelCase__		)


def        lowerCAmelCase_     (			):
		__SCREAMING_SNAKE_CASE :	str					 =	Image.open("""./tests/fixtures/tests_samples/COCO/000000039769.png"""      )
		return image






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


			'''simple docstring'''

			@cached_property
			def UpperCamelCase__      (							self  :				List[Any]		):





					"""simple docstring"""






					return AutoImageProcessor.from_pretrained(CVT_PRETRAINED_MODEL_ARCHIVE_LIST[0]		)



			@slow
			def UpperCamelCase__      (							self  :				List[str]		):





					"""simple docstring"""






					__SCREAMING_SNAKE_CASE :	int					 =	CvtForImageClassification.from_pretrained(CVT_PRETRAINED_MODEL_ARCHIVE_LIST[0]		).to(lowerCAmelCase__		)

					__SCREAMING_SNAKE_CASE :	List[Any]					 =	self.default_image_processor
					__SCREAMING_SNAKE_CASE :	List[Any]					 =	prepare_img()
					__SCREAMING_SNAKE_CASE :	int					 =	image_processor(images=lowerCAmelCase__							,  return_tensors="""pt"""		).to(lowerCAmelCase__		)

					# forward pass
					with torch.no_grad():
							__SCREAMING_SNAKE_CASE :	Optional[int]					 =	model(**lowerCAmelCase__		)

					# verify the logits
					__SCREAMING_SNAKE_CASE :	str					 =	torch.Size((1, 1_0_0_0)		)
					self.assertEqual(outputs.logits.shape							,  lowerCAmelCase__		)

					__SCREAMING_SNAKE_CASE :	Optional[Any]					 =	torch.tensor([0.92_85, 0.90_15, -0.31_50]		).to(lowerCAmelCase__		)

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