



'''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		)		)




'''simple docstring'''




# coding=utf-8
# Copyright 2020 The HuggingFace Inc. team.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

# this script dumps information about the environment

import os
import sys

import transformers


UpperCamelCase__ :			Tuple												=				'''3'''

print('''Python version:''', sys.version)
print('''transformers version:''', transformers.__version__)

try:
						import torch

						print('''Torch version:''', torch.__version__)
						print('''Cuda available:''', torch.cuda.is_available())
						print('''Cuda version:''', torch.version.cuda)
						print('''CuDNN version:''', torch.backends.cudnn.version())
						print('''Number of GPUs available:''', torch.cuda.device_count())
						print('''NCCL version:''', torch.cuda.nccl.version())
except ImportError:
						print('''Torch version:''', None)

try:
						import deepspeed

						print('''DeepSpeed version:''', deepspeed.__version__)
except ImportError:
						print('''DeepSpeed version:''', None)

try:
						import tensorflow as tf

						print('''TensorFlow version:''', tf.__version__)
						print('''TF GPUs available:''', bool(tf.config.list_physical_devices('''GPU''')))
						print('''Number of TF GPUs available:''', len(tf.config.list_physical_devices('''GPU''')))
except ImportError:
						print('''TensorFlow version:''', None)