





import inspect
import unittest

from transformers import ViTConfig
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 ViTForImageClassification, ViTForMaskedImageModeling, ViTModel
		from transformers.models.vit.modeling_vit import VIT_PRETRAINED_MODEL_ARCHIVE_LIST


if is_vision_available():
		from PIL import Image

		from transformers import ViTImageProcessor

class 		snake_case_   :







	'''simple docstring'''




	def __init__(						self							:     List[Any]					,      __lowerCamelCase							:     int					,      __lowerCamelCase							:     List[str]=13					,      __lowerCamelCase							:     Optional[int]=30					,      __lowerCamelCase							:     Any=2					,      __lowerCamelCase							:     str=3					,      __lowerCamelCase							:     Tuple=True					,      __lowerCamelCase							:     int=True					,      __lowerCamelCase							:     str=32					,      __lowerCamelCase							:     Dict=5					,      __lowerCamelCase							:     List[Any]=4					,      __lowerCamelCase							:     Union[str, Any]=37					,      __lowerCamelCase							:     Optional[Any]="gelu"					,      __lowerCamelCase							:     Dict=0.1					,      __lowerCamelCase							:     Optional[Any]=0.1					,      __lowerCamelCase							:     Any=10					,      __lowerCamelCase							:     List[Any]=0.02					,      __lowerCamelCase							:     Tuple=None					,      __lowerCamelCase							:     Union[str, Any]=2					,      )       -> Dict:

			'''simple docstring'''




			__lowercase											=     parent
			__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											=     encoder_stride

			# in ViT, 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    UpperCAmelCase			(						self							:     Optional[int]   )       -> List[Any]:

			'''simple docstring'''




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

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

			__lowercase											=     self.get_config()

			return config, pixel_values, labels




	def    UpperCAmelCase			(						self							:     List[Any]   )       -> Optional[Any]:

			'''simple docstring'''




			return ViTConfig(
			    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    UpperCAmelCase			(						self							:     Tuple					,      __lowerCamelCase							:     str					,      __lowerCamelCase							:     Union[str, Any]					,      __lowerCamelCase							:     Dict   )       -> List[Any]:

			'''simple docstring'''




			__lowercase											=     ViTModel(config=__lowerCamelCase   )
			model.to(__lowerCamelCase   )
			model.eval()
			__lowercase											=     model(__lowerCamelCase   )
			self.parent.assertEqual(result.last_hidden_state.shape					,      (self.batch_size, self.seq_length, self.hidden_size)   )




	def    UpperCAmelCase			(						self							:     Optional[Any]					,      __lowerCamelCase							:     int					,      __lowerCamelCase							:     Tuple					,      __lowerCamelCase							:     Optional[Any]   )       -> str:

			'''simple docstring'''




			__lowercase											=     ViTForMaskedImageModeling(config=__lowerCamelCase   )
			model.to(__lowerCamelCase   )
			model.eval()
			__lowercase											=     model(__lowerCamelCase   )
			self.parent.assertEqual(
			    result.reconstruction.shape					,      (self.batch_size, self.num_channels, self.image_size, self.image_size)   )

			# test greyscale images
			__lowercase											=     1
			__lowercase											=     ViTForMaskedImageModeling(__lowerCamelCase   )
			model.to(__lowerCamelCase   )
			model.eval()

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




	def    UpperCAmelCase			(						self							:     Optional[int]					,      __lowerCamelCase							:     Optional[int]					,      __lowerCamelCase							:     Union[str, Any]					,      __lowerCamelCase							:     Any   )       -> str:

			'''simple docstring'''




			__lowercase											=     self.type_sequence_label_size
			__lowercase											=     ViTForImageClassification(__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)   )

			# test greyscale images
			__lowercase											=     1
			__lowercase											=     ViTForImageClassification(__lowerCamelCase   )
			model.to(__lowerCamelCase   )
			model.eval()

			__lowercase											=     floats_tensor([self.batch_size, 1, self.image_size, self.image_size]   )
			__lowercase											=     model(__lowerCamelCase   )
			self.parent.assertEqual(result.logits.shape					,      (self.batch_size, self.type_sequence_label_size)   )






	def    UpperCAmelCase			(						self							:     List[str]   )       -> List[Any]:

			'''simple docstring'''




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





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







	'''simple docstring'''
	__UpperCamelCase   =		(
	    (
	        ViTModel,
	        ViTForImageClassification,
	        ViTForMaskedImageModeling,
	    )
	    if is_torch_available()
	    else ()
	)
	__UpperCamelCase   =		(
	    {'''feature-extraction''': ViTModel, '''image-classification''': ViTForImageClassification}
	    if is_torch_available()
	    else {}
	)
	__UpperCamelCase   =		True

	__UpperCamelCase   =		False
	__UpperCamelCase   =		False
	__UpperCamelCase   =		False




	def    UpperCAmelCase			(						self							:     int   )       -> List[str]:

			'''simple docstring'''




			__lowercase											=     ViTModelTester(self   )
			__lowercase											=     ConfigTester(self					,      config_class=__lowerCamelCase					,      has_text_modality=__lowerCamelCase					,      hidden_size=37   )




	def    UpperCAmelCase			(						self							:     List[Any]   )       -> Tuple:

			'''simple docstring'''




			self.config_tester.run_common_tests()




	@unittest.skip(reason='ViT does not use inputs_embeds'   )
	def    UpperCAmelCase			(						self							:     Optional[Any]   )       -> List[Any]:

			'''simple docstring'''




			pass




	def    UpperCAmelCase			(						self							:     Union[str, Any]   )       -> int:

			'''simple docstring'''




			__lowercase						,      __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    UpperCAmelCase			(						self							:     int   )       -> List[str]:

			'''simple docstring'''




			__lowercase						,      __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    UpperCAmelCase			(						self							:     Any   )       -> Optional[Any]:

			'''simple docstring'''




			__lowercase											=     self.model_tester.prepare_config_and_inputs()
			self.model_tester.create_and_check_model(*__lowerCamelCase   )




	def    UpperCAmelCase			(						self							:     str   )       -> Any:

			'''simple docstring'''




			__lowercase											=     self.model_tester.prepare_config_and_inputs()
			self.model_tester.create_and_check_for_masked_image_modeling(*__lowerCamelCase   )




	def    UpperCAmelCase			(						self							:     Union[str, Any]   )       -> str:

			'''simple docstring'''




			__lowercase											=     self.model_tester.prepare_config_and_inputs()
			self.model_tester.create_and_check_for_image_classification(*__lowerCamelCase   )






	@slow
	def    UpperCAmelCase			(						self							:     Optional[Any]   )       -> str:

			'''simple docstring'''




			for model_name in VIT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
					__lowercase											=     ViTModel.from_pretrained(__lowerCamelCase   )
					self.assertIsNotNone(__lowerCamelCase   )





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


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







	'''simple docstring'''




	@cached_property
	def    UpperCAmelCase			(						self							:     Optional[int]   )       -> Optional[int]:

			'''simple docstring'''




			return ViTImageProcessor.from_pretrained('google/vit-base-patch16-224'   ) if is_vision_available() else None




	@slow
	def    UpperCAmelCase			(						self							:     List[Any]   )       -> Tuple:

			'''simple docstring'''




			__lowercase											=     ViTForImageClassification.from_pretrained('google/vit-base-patch16-224'   ).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   )

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

			__lowercase											=     torch.tensor([-0.2744, 0.8215, -0.0836]   ).to(__lowerCamelCase   )

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




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

			'''simple docstring'''




			__lowercase											=     ViTModel.from_pretrained('facebook/dino-vits8'   ).to(__lowerCamelCase   )

			__lowercase											=     ViTImageProcessor.from_pretrained('facebook/dino-vits8'					,      size=480   )
			__lowercase											=     prepare_img()
			__lowercase											=     image_processor(images=__lowerCamelCase					,      return_tensors='pt'   )
			__lowercase											=     inputs.pixel_values.to(__lowerCamelCase   )

			# forward pass
			with torch.no_grad():
					__lowercase											=     model(__lowerCamelCase					,      interpolate_pos_encoding=__lowerCamelCase   )

			# verify the logits
			__lowercase											=     torch.Size((1, 3_601, 384)   )
			self.assertEqual(outputs.last_hidden_state.shape					,      __lowerCamelCase   )

			__lowercase											=     torch.tensor(
			    [[4.2340, 4.3906, -6.6692], [4.5463, 1.8928, -6.7257], [4.4429, 0.8496, -5.8585]]   ).to(__lowerCamelCase   )

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






	@slow
	@require_accelerate
	@require_torch_gpu
	def    UpperCAmelCase			(						self							:     Optional[int]   )       -> Any:

			'''simple docstring'''




			__lowercase											=     ViTModel.from_pretrained('facebook/dino-vits8'					,      torch_dtype=torch.floataa					,      device_map='auto'   )
			__lowercase											=     self.default_image_processor

			__lowercase											=     prepare_img()
			__lowercase											=     image_processor(images=__lowerCamelCase					,      return_tensors='pt'   )
			__lowercase											=     inputs.pixel_values.to(__lowerCamelCase   )

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











import json
import os
from pathlib import Path

import pytest

from datasets.download.download_config import DownloadConfig
from datasets.download.download_manager import DownloadManager
from datasets.utils.file_utils import hash_url_to_filename


SCREAMING_SNAKE_CASE_   :      Union[str, Any]			=   '''http://www.mocksite.com/file1.txt'''
SCREAMING_SNAKE_CASE_   :      str			=   '''"text": ["foo", "foo"]'''
SCREAMING_SNAKE_CASE_   :      Optional[int]			=   '''6d8ce9aa78a471c7477201efbeabd3bb01ac2e7d100a6dc024ba1608361f90a8'''

class 		snake_case_   :







	'''simple docstring'''
	__UpperCamelCase   =		2_00
	__UpperCamelCase   =		{'''Content-Length''': '''100'''}
	__UpperCamelCase   =		{}




	def    UpperCAmelCase			(						self							:     str					,      **__lowerCamelCase							:     Optional[int]   )       -> str:

			'''simple docstring'''




			return [bytes(__lowerCamelCase					,      'utf-8'   )]





def  SCREAMING_SNAKE_CASE				(      *snake_case						,   **snake_case							)		->    int:
		return MockResponse()



@pytest.mark.parametrize('urls_type'						,   [str, list, dict]							)
def  SCREAMING_SNAKE_CASE				(      snake_case						,   snake_case						,   snake_case							)		->    Optional[Any]:
		import requests

		monkeypatch.setattr(snake_case						,   'request'						,   snake_case							)

		__lowercase											=     URL
		if issubclass(snake_case						,   snake_case							):
				__lowercase											=     url
		elif issubclass(snake_case						,   snake_case							):
				__lowercase											=     [url]
		elif issubclass(snake_case						,   snake_case							):
				__lowercase											=     {'train': url}
		__lowercase											=     'dummy'
		__lowercase											=     'downloads'
		__lowercase											=     tmp_path
		__lowercase											=     DownloadConfig(
		    cache_dir=os.path.join(snake_case						,   snake_case							)						,   use_etag=snake_case						,   )
		__lowercase											=     DownloadManager(dataset_name=snake_case						,   download_config=snake_case							)
		__lowercase											=     dl_manager.download(snake_case							)
		__lowercase											=     urls
		for downloaded_paths in [downloaded_paths]:
				if isinstance(snake_case						,   snake_case							):
						__lowercase											=     [downloaded_paths]
						__lowercase											=     [urls]
				elif isinstance(snake_case						,   snake_case							):
						assert "train" in downloaded_paths.keys()
						__lowercase											=     downloaded_paths.values()
						__lowercase											=     urls.values()
				assert downloaded_paths
				for downloaded_path, input_url in zip(snake_case						,   snake_case							):
						assert downloaded_path == dl_manager.downloaded_paths[input_url]
						__lowercase											=     Path(snake_case							)
						__lowercase											=     downloaded_path.parts
						assert parts[-1] == HASH
						assert parts[-2] == cache_subdir
						assert downloaded_path.exists()
						__lowercase											=     downloaded_path.read_text()
						assert content == CONTENT
						__lowercase											=     downloaded_path.with_suffix('.json'							)
						assert metadata_downloaded_path.exists()
						__lowercase											=     json.loads(metadata_downloaded_path.read_text()							)
						assert metadata_content == {"url": URL, "etag": None}



@pytest.mark.parametrize('paths_type'						,   [str, list, dict]							)
def  SCREAMING_SNAKE_CASE				(      snake_case						,   snake_case						,   snake_case							)		->    Union[str, Any]:
		__lowercase											=     str(snake_case							)
		if issubclass(snake_case						,   snake_case							):
				__lowercase											=     filename
		elif issubclass(snake_case						,   snake_case							):
				__lowercase											=     [filename]
		elif issubclass(snake_case						,   snake_case							):
				__lowercase											=     {'train': filename}
		__lowercase											=     'dummy'
		__lowercase											=     xz_file.parent
		__lowercase											=     'extracted'
		__lowercase											=     DownloadConfig(
		    cache_dir=snake_case						,   use_etag=snake_case						,   )
		__lowercase											=     DownloadManager(dataset_name=snake_case						,   download_config=snake_case							)
		__lowercase											=     dl_manager.extract(snake_case							)
		__lowercase											=     paths
		for extracted_paths in [extracted_paths]:
				if isinstance(snake_case						,   snake_case							):
						__lowercase											=     [extracted_paths]
						__lowercase											=     [paths]
				elif isinstance(snake_case						,   snake_case							):
						assert "train" in extracted_paths.keys()
						__lowercase											=     extracted_paths.values()
						__lowercase											=     paths.values()
				assert extracted_paths
				for extracted_path, input_path in zip(snake_case						,   snake_case							):
						assert extracted_path == dl_manager.extracted_paths[input_path]
						__lowercase											=     Path(snake_case							)
						__lowercase											=     extracted_path.parts
						assert parts[-1] == hash_url_to_filename(snake_case						,   etag=snake_case							)
						assert parts[-2] == extracted_subdir
						assert extracted_path.exists()
						__lowercase											=     extracted_path.read_text()
						__lowercase											=     text_file.read_text()
						assert extracted_file_content == expected_file_content



def  SCREAMING_SNAKE_CASE				(      snake_case						,   snake_case							)		->    int:
		assert path.endswith('.jsonl'							)
		for num_items, line in enumerate(snake_case						,   start=1							):
				__lowercase											=     json.loads(line.decode('utf-8'							)							)
				assert item.keys() == {"col_1", "col_2", "col_3"}
		assert num_items == 4



@pytest.mark.parametrize('archive_jsonl'						,   ['tar_jsonl_path', 'zip_jsonl_path']							)
def  SCREAMING_SNAKE_CASE				(      snake_case						,   snake_case							)		->    int:
		__lowercase											=     request.getfixturevalue(snake_case							)
		__lowercase											=     DownloadManager()
		for num_jsonl, (path, file) in enumerate(dl_manager.iter_archive(snake_case							)						,   start=1							):
				_test_jsonl(snake_case						,   snake_case							)
		assert num_jsonl == 2



@pytest.mark.parametrize('archive_nested_jsonl'						,   ['tar_nested_jsonl_path', 'zip_nested_jsonl_path']							)
def  SCREAMING_SNAKE_CASE				(      snake_case						,   snake_case							)		->    Any:
		__lowercase											=     request.getfixturevalue(snake_case							)
		__lowercase											=     DownloadManager()
		for num_tar, (path, file) in enumerate(dl_manager.iter_archive(snake_case							)						,   start=1							):
				for num_jsonl, (subpath, subfile) in enumerate(dl_manager.iter_archive(snake_case							)						,   start=1							):
						_test_jsonl(snake_case						,   snake_case							)
		assert num_tar == 1
		assert num_jsonl == 2



def  SCREAMING_SNAKE_CASE				(      snake_case							)		->    Dict:
		__lowercase											=     DownloadManager()
		for num_file, file in enumerate(dl_manager.iter_files(snake_case							)						,   start=1							):
				assert os.path.basename(snake_case							) == ("test.txt" if num_file == 1 else "train.txt")
		assert num_file == 2




