


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 	__lowercase    :





							"""simple docstring"""







							def __init__(      self			,						lowerCAmelCase__			,						lowerCAmelCase__=1_3			,						lowerCAmelCase__=3_0			,						lowerCAmelCase__=2			,						lowerCAmelCase__=3			,						lowerCAmelCase__=True			,						lowerCAmelCase__=True			,						lowerCAmelCase__=3_2			,						lowerCAmelCase__=5			,						lowerCAmelCase__=4			,						lowerCAmelCase__=3_7			,						lowerCAmelCase__="gelu"			,						lowerCAmelCase__=0.1			,						lowerCAmelCase__=0.1			,						lowerCAmelCase__=1_0			,						lowerCAmelCase__=0.02			,						lowerCAmelCase__=None			,						lowerCAmelCase__=2			,						):





										"""simple docstring"""




										SCREAMING_SNAKE_CASE_		:     List[Any]					=					parent
										SCREAMING_SNAKE_CASE_		:     Dict					=					batch_size
										SCREAMING_SNAKE_CASE_		:     Optional[int]					=					image_size
										SCREAMING_SNAKE_CASE_		:     Tuple					=					patch_size
										SCREAMING_SNAKE_CASE_		:     str					=					num_channels
										SCREAMING_SNAKE_CASE_		:     Optional[int]					=					is_training
										SCREAMING_SNAKE_CASE_		:     Optional[int]					=					use_labels
										SCREAMING_SNAKE_CASE_		:     Tuple					=					hidden_size
										SCREAMING_SNAKE_CASE_		:     List[Any]					=					num_hidden_layers
										SCREAMING_SNAKE_CASE_		:     List[Any]					=					num_attention_heads
										SCREAMING_SNAKE_CASE_		:     Any					=					intermediate_size
										SCREAMING_SNAKE_CASE_		:     str					=					hidden_act
										SCREAMING_SNAKE_CASE_		:     str					=					hidden_dropout_prob
										SCREAMING_SNAKE_CASE_		:     Any					=					attention_probs_dropout_prob
										SCREAMING_SNAKE_CASE_		:     Optional[Any]					=					type_sequence_label_size
										SCREAMING_SNAKE_CASE_		:     Dict					=					initializer_range
										SCREAMING_SNAKE_CASE_		:     List[Any]					=					scope
										SCREAMING_SNAKE_CASE_		:     List[Any]					=					encoder_stride

										# in ViT, the seq length equals the number of patches + 1 (we add 1 for the [CLS] token)
										SCREAMING_SNAKE_CASE_		:     Dict					=					(image_size // patch_size) ** 2
										SCREAMING_SNAKE_CASE_		:     Union[str, Any]					=					num_patches + 1






							def 					UpperCamelCase__     (      self       ):





										"""simple docstring"""




										SCREAMING_SNAKE_CASE_		:     Dict					=					floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]       )

										SCREAMING_SNAKE_CASE_		:     Optional[Any]					=					None
										if self.use_labels:
													SCREAMING_SNAKE_CASE_		:     Union[str, Any]					=					ids_tensor([self.batch_size]			,						self.type_sequence_label_size       )

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

										return config, pixel_values, labels






							def 					UpperCamelCase__     (      self       ):





										"""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			,						lowerCAmelCase__			,						lowerCAmelCase__			,						lowerCAmelCase__       ):





										"""simple docstring"""




										SCREAMING_SNAKE_CASE_		:     Any					=					ViTModel(config=lowerCAmelCase__       )
										model.to(lowerCAmelCase__       )
										model.eval()
										SCREAMING_SNAKE_CASE_		:     str					=					model(lowerCAmelCase__       )
										self.parent.assertEqual(result.last_hidden_state.shape			,						(self.batch_size, self.seq_length, self.hidden_size)       )






							def 					UpperCamelCase__     (      self			,						lowerCAmelCase__			,						lowerCAmelCase__			,						lowerCAmelCase__       ):





										"""simple docstring"""




										SCREAMING_SNAKE_CASE_		:     Dict					=					ViTForMaskedImageModeling(config=lowerCAmelCase__       )
										model.to(lowerCAmelCase__       )
										model.eval()
										SCREAMING_SNAKE_CASE_		:     Union[str, Any]					=					model(lowerCAmelCase__       )
										self.parent.assertEqual(
										    result.reconstruction.shape			,						(self.batch_size, self.num_channels, self.image_size, self.image_size)       )

										# test greyscale images
										SCREAMING_SNAKE_CASE_		:     Optional[int]					=					1
										SCREAMING_SNAKE_CASE_		:     Dict					=					ViTForMaskedImageModeling(lowerCAmelCase__       )
										model.to(lowerCAmelCase__       )
										model.eval()

										SCREAMING_SNAKE_CASE_		:     Tuple					=					floats_tensor([self.batch_size, 1, self.image_size, self.image_size]       )
										SCREAMING_SNAKE_CASE_		:     List[Any]					=					model(lowerCAmelCase__       )
										self.parent.assertEqual(result.reconstruction.shape			,						(self.batch_size, 1, self.image_size, self.image_size)       )






							def 					UpperCamelCase__     (      self			,						lowerCAmelCase__			,						lowerCAmelCase__			,						lowerCAmelCase__       ):





										"""simple docstring"""




										SCREAMING_SNAKE_CASE_		:     Tuple					=					self.type_sequence_label_size
										SCREAMING_SNAKE_CASE_		:     List[Any]					=					ViTForImageClassification(lowerCAmelCase__       )
										model.to(lowerCAmelCase__       )
										model.eval()
										SCREAMING_SNAKE_CASE_		:     List[str]					=					model(lowerCAmelCase__			,						labels=lowerCAmelCase__       )
										self.parent.assertEqual(result.logits.shape			,						(self.batch_size, self.type_sequence_label_size)       )

										# test greyscale images
										SCREAMING_SNAKE_CASE_		:     Union[str, Any]					=					1
										SCREAMING_SNAKE_CASE_		:     Any					=					ViTForImageClassification(lowerCAmelCase__       )
										model.to(lowerCAmelCase__       )
										model.eval()

										SCREAMING_SNAKE_CASE_		:     str					=					floats_tensor([self.batch_size, 1, self.image_size, self.image_size]       )
										SCREAMING_SNAKE_CASE_		:     Any					=					model(lowerCAmelCase__       )
										self.parent.assertEqual(result.logits.shape			,						(self.batch_size, self.type_sequence_label_size)       )






							def 					UpperCamelCase__     (      self       ):





										"""simple docstring"""




										SCREAMING_SNAKE_CASE_		:     Tuple					=					self.prepare_config_and_inputs()
										(
										    (
										    SCREAMING_SNAKE_CASE_
										)		,      (
										    SCREAMING_SNAKE_CASE_
										)		,      (
										    SCREAMING_SNAKE_CASE_
										)		,      
										)		:     List[str]					=					config_and_inputs
										SCREAMING_SNAKE_CASE_		:     Dict					=					{'pixel_values': pixel_values}
										return config, inputs_dict







@require_torch
class 	__lowercase    (__SCREAMING_SNAKE_CASE	,       __SCREAMING_SNAKE_CASE	,       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       ):





										"""simple docstring"""




										SCREAMING_SNAKE_CASE_		:     List[str]					=					ViTModelTester(self       )
										SCREAMING_SNAKE_CASE_		:     int					=					ConfigTester(self			,						config_class=lowerCAmelCase__			,						has_text_modality=lowerCAmelCase__			,						hidden_size=3_7       )






							def 					UpperCamelCase__     (      self       ):





										"""simple docstring"""




										self.config_tester.run_common_tests()






							@unittest.skip(reason='ViT does not use inputs_embeds'       )
							def 					UpperCamelCase__     (      self       ):





										"""simple docstring"""




										pass






							def 					UpperCamelCase__     (      self       ):





										"""simple docstring"""




										SCREAMING_SNAKE_CASE_		,      SCREAMING_SNAKE_CASE_		:     Any					=					self.model_tester.prepare_config_and_inputs_for_common()

										for model_class in self.all_model_classes:
													SCREAMING_SNAKE_CASE_		:     Dict					=					model_class(lowerCAmelCase__       )
													self.assertIsInstance(model.get_input_embeddings()			,						(nn.Module)       )
													SCREAMING_SNAKE_CASE_		:     List[Any]					=					model.get_output_embeddings()
													self.assertTrue(x is None or isinstance(lowerCAmelCase__			,						nn.Linear       )       )






							def 					UpperCamelCase__     (      self       ):





										"""simple docstring"""




										SCREAMING_SNAKE_CASE_		,      SCREAMING_SNAKE_CASE_		:     Any					=					self.model_tester.prepare_config_and_inputs_for_common()

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

													SCREAMING_SNAKE_CASE_		:     int					=					['pixel_values']
													self.assertListEqual(arg_names[:1]			,						lowerCAmelCase__       )






							def 					UpperCamelCase__     (      self       ):





										"""simple docstring"""




										SCREAMING_SNAKE_CASE_		:     Optional[int]					=					self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_model(*lowerCAmelCase__       )






							def 					UpperCamelCase__     (      self       ):





										"""simple docstring"""




										SCREAMING_SNAKE_CASE_		:     Optional[Any]					=					self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_for_masked_image_modeling(*lowerCAmelCase__       )






							def 					UpperCamelCase__     (      self       ):





										"""simple docstring"""




										SCREAMING_SNAKE_CASE_		:     Union[str, Any]					=					self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_for_image_classification(*lowerCAmelCase__       )






							@slow
							def 					UpperCamelCase__     (      self       ):





										"""simple docstring"""




										for model_name in VIT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
													SCREAMING_SNAKE_CASE_		:     Optional[int]					=					ViTModel.from_pretrained(lowerCAmelCase__       )
													self.assertIsNotNone(lowerCAmelCase__       )







def 						a__			(				):
			SCREAMING_SNAKE_CASE_		:     Optional[Any]					=					Image.open('./tests/fixtures/tests_samples/COCO/000000039769.png'				)
			return image







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





							"""simple docstring"""







							@cached_property
							def 					UpperCamelCase__     (      self       ):





										"""simple docstring"""




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






							@slow
							def 					UpperCamelCase__     (      self       ):





										"""simple docstring"""




										SCREAMING_SNAKE_CASE_		:     int					=					ViTForImageClassification.from_pretrained('google/vit-base-patch16-224'       ).to(lowerCAmelCase__       )

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

										# forward pass
										with torch.no_grad():
													SCREAMING_SNAKE_CASE_		:     Union[str, Any]					=					model(**lowerCAmelCase__       )

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

										SCREAMING_SNAKE_CASE_		:     Optional[Any]					=					torch.tensor([-0.2_744, 0.8_215, -0.0_836]       ).to(lowerCAmelCase__       )

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






							@slow
							def 					UpperCamelCase__     (      self       ):





										"""simple docstring"""




										SCREAMING_SNAKE_CASE_		:     int					=					ViTModel.from_pretrained('facebook/dino-vits8'       ).to(lowerCAmelCase__       )

										SCREAMING_SNAKE_CASE_		:     Any					=					ViTImageProcessor.from_pretrained('facebook/dino-vits8'			,						size=4_8_0       )
										SCREAMING_SNAKE_CASE_		:     Optional[int]					=					prepare_img()
										SCREAMING_SNAKE_CASE_		:     Dict					=					image_processor(images=lowerCAmelCase__			,						return_tensors='pt'       )
										SCREAMING_SNAKE_CASE_		:     Union[str, Any]					=					inputs.pixel_values.to(lowerCAmelCase__       )

										# forward pass
										with torch.no_grad():
													SCREAMING_SNAKE_CASE_		:     Optional[int]					=					model(lowerCAmelCase__			,						interpolate_pos_encoding=lowerCAmelCase__       )

										# verify the logits
										SCREAMING_SNAKE_CASE_		:     Optional[int]					=					torch.Size((1, 3_6_0_1, 3_8_4)       )
										self.assertEqual(outputs.last_hidden_state.shape			,						lowerCAmelCase__       )

										SCREAMING_SNAKE_CASE_		:     str					=					torch.tensor(
										    [[4.2_340, 4.3_906, -6.6_692], [4.5_463, 1.8_928, -6.7_257], [4.4_429, 0.8_496, -5.8_585]]       ).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       ):





										"""simple docstring"""




										SCREAMING_SNAKE_CASE_		:     Optional[Any]					=					ViTModel.from_pretrained('facebook/dino-vits8'			,						torch_dtype=torch.floataa			,						device_map='auto'       )
										SCREAMING_SNAKE_CASE_		:     Tuple					=					self.default_image_processor

										SCREAMING_SNAKE_CASE_		:     Tuple					=					prepare_img()
										SCREAMING_SNAKE_CASE_		:     List[Any]					=					image_processor(images=lowerCAmelCase__			,						return_tensors='pt'       )
										SCREAMING_SNAKE_CASE_		:     Optional[int]					=					inputs.pixel_values.to(lowerCAmelCase__       )

										# forward pass to make sure inference works in fp16
										with torch.no_grad():
													SCREAMING_SNAKE_CASE_		:     Union[str, Any]					=					model(lowerCAmelCase__       )













import importlib
import os

import fsspec
import pytest
from fsspec import register_implementation
from fsspec.registry import _registry as _fsspec_registry

from datasets.filesystems import COMPRESSION_FILESYSTEMS, HfFileSystem, extract_path_from_uri, is_remote_filesystem

from .utils import require_lza, require_zstandard






def 	UpperCamelCase	(							snake_case__  : int	):




     '''simple docstring'''






     assert "mock" in _fsspec_registry
     assert "bz2" in _fsspec_registry






def 	UpperCamelCase	(							):




     '''simple docstring'''






     assert "mock" not in _fsspec_registry
     assert "bz2" in _fsspec_registry






def 	UpperCamelCase	(							):




     '''simple docstring'''






     __snake_case      :Tuple 							=    """mock-s3-bucket"""
     __snake_case      :Optional[Any] 							=    f'''s3://{mock_bucket}'''
     __snake_case      :Optional[int] 							=    extract_path_from_uri(snake_case__	)
     assert dataset_path.startswith("""s3://"""	) is False

     __snake_case      :Union[str, Any] 							=    """./local/path"""
     __snake_case      :List[str] 							=    extract_path_from_uri(snake_case__	)
     assert dataset_path == new_dataset_path






def 	UpperCamelCase	(							snake_case__  : str	):




     '''simple docstring'''






     __snake_case      :List[str] 							=    is_remote_filesystem(snake_case__	)
     assert is_remote is True

     __snake_case      :Any 							=    fsspec.filesystem("""file"""	)

     __snake_case      :Optional[Any] 							=    is_remote_filesystem(snake_case__	)
     assert is_remote is False






@pytest.mark.parametrize("""compression_fs_class"""						,snake_case__	)
def 	UpperCamelCase	(							snake_case__  : Optional[Any]						,snake_case__  : List[str]						,snake_case__  : int						,snake_case__  : List[Any]						,snake_case__  : Tuple						,snake_case__  : str						,snake_case__  : Optional[Any]	):




     '''simple docstring'''






     __snake_case      :Optional[int] 							=    {"""gzip""": gz_file, """xz""": xz_file, """zstd""": zstd_file, """bz2""": bza_file, """lz4""": lza_file}
     __snake_case      :Optional[Any] 							=    input_paths[compression_fs_class.protocol]
     if input_path is None:
          __snake_case      :Optional[int] 							=    f'''for \'{compression_fs_class.protocol}\' compression protocol, '''
          if compression_fs_class.protocol == "lz4":
               reason += require_lza.kwargs["reason"]
          elif compression_fs_class.protocol == "zstd":
               reason += require_zstandard.kwargs["reason"]
          pytest.skip(snake_case__	)
     __snake_case      :Optional[Any] 							=    fsspec.filesystem(compression_fs_class.protocol						,fo=snake_case__	)
     assert isinstance(snake_case__						,snake_case__	)
     __snake_case      :Dict 							=    os.path.basename(snake_case__	)
     __snake_case      :Dict 							=    expected_filename[: expected_filename.rindex("""."""	)]
     assert fs.glob("""*"""	) == [expected_filename]
     with fs.open(snake_case__						,"""r"""						,encoding="""utf-8"""	) as f, open(snake_case__						,encoding="""utf-8"""	) as expected_file:
          assert f.read() == expected_file.read()






@pytest.mark.parametrize("""protocol"""						,["""zip""", """gzip"""]	)
def 	UpperCamelCase	(							snake_case__  : List[Any]						,snake_case__  : List[Any]						,snake_case__  : Dict	):




     '''simple docstring'''






     __snake_case      :Optional[int] 							=    {"""zip""": zip_jsonl_path, """gzip""": jsonl_gz_path}
     __snake_case      :List[str] 							=    compressed_file_paths[protocol]
     __snake_case      :Optional[int] 							=    """dataset.jsonl"""
     __snake_case      :Optional[int] 							=    f'''{protocol}://{member_file_path}::{compressed_file_path}'''
     __snake_case							,			*__snake_case      :Optional[Any] 							=    fsspec.get_fs_token_paths(snake_case__	)
     assert fs.isfile(snake_case__	)
     assert not fs.isfile("""non_existing_""" + member_file_path	)






@pytest.mark.integration
def 	UpperCamelCase	(							snake_case__  : Optional[int]						,snake_case__  : Dict						,snake_case__  : List[str]						,snake_case__  : Dict	):




     '''simple docstring'''






     __snake_case      :List[Any] 							=    hf_api.dataset_info(snake_case__						,token=snake_case__	)
     __snake_case      :List[str] 							=    HfFileSystem(repo_info=snake_case__						,token=snake_case__	)
     assert sorted(hffs.glob("""*"""	)	) == [".gitattributes", "data"]
     assert hffs.isdir("""data"""	)
     assert hffs.isfile(""".gitattributes"""	) and hffs.isfile("""data/text_data.txt"""	)
     with open(snake_case__	) as f:
          assert hffs.open("""data/text_data.txt"""						,"""r"""	).read() == f.read()






def 	UpperCamelCase	(							):




     '''simple docstring'''






     __snake_case      :str 							=    """bz2"""

     # Import module
     import datasets.filesystems

     # Overwrite protocol and reload
     register_implementation(snake_case__						,snake_case__						,clobber=snake_case__	)
     with pytest.warns(snake_case__	) as warning_info:
          importlib.reload(datasets.filesystems	)

     assert len(snake_case__	) == 1
     assert (
         str(warning_info[0].message	)
         == f'''A filesystem protocol was already set for {protocol} and will be overwritten.'''
     )


