


'''simple docstring'''
import inspect
import unittest

from transformers import ViTHybridConfig
from transformers.testing_utils import require_accelerate, require_torch, 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 ViTHybridForImageClassification, ViTHybridImageProcessor, ViTHybridModel
 from transformers.models.vit_hybrid.modeling_vit_hybrid import VIT_HYBRID_PRETRAINED_MODEL_ARCHIVE_LIST


if is_vision_available():
 from PIL import Image

class     _lowerCamelCase       :




 '''simple docstring'''


 def __init__(   self   ,   __lowercase   ,   __lowercase=13   ,   __lowercase=64   ,   __lowercase=2   ,   __lowercase=3   ,   __lowercase=True   ,   __lowercase=True   ,   __lowercase=32   ,   __lowercase=5   ,   __lowercase=4   ,   __lowercase=37   ,   __lowercase="gelu"   ,   __lowercase=0.1   ,   __lowercase=0.1   ,   __lowercase=10   ,   __lowercase=0.0_2   ,   __lowercase=[1, 16, 4, 4]   ,   __lowercase=None   ,   ):

  """simple docstring"""







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

  # in ViT hybrid, the seq length equals the number of patches + 1 (we add 1 for the [CLS] token)
  # the number of patches is based on the feature map of the backbone, which by default uses an output stride
  # of 32, which means that the feature map has a spatial resolution of 1/32 of the input image size
  __A	:		Optional[int]	  =      (self.image_size // 32) ** 2
  __A	:		Optional[int]	  =      num_patches + 1


 def 			snake_case__ (   self    ):

  """simple docstring"""







  __A	:		List[str]	  =      floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]    )

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

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

  return config, pixel_values, labels


 def 			snake_case__ (   self    ):

  """simple docstring"""







  __A	:		List[Any]	  =      {
      'global_padding': 'same',
      'layer_type': 'bottleneck',
      'depths': [3, 4, 9],
      'out_features': ['stage1', 'stage2', 'stage3'],
      'embedding_dynamic_padding': True,
      'hidden_sizes': [4, 8, 16, 32],
      'num_groups': 2,
  }

  return ViTHybridConfig(
      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=UpperCamelCase__   ,   initializer_range=self.initializer_range   ,   backbone_featmap_shape=self.backbone_featmap_shape   ,   backbone_config=UpperCamelCase__   ,   )


 def 			snake_case__ (   self   ,   __lowercase   ,   __lowercase   ,   __lowercase    ):

  """simple docstring"""







  __A	:		List[Any]	  =      ViTHybridModel(config=UpperCamelCase__    )
  model.to(UpperCamelCase__    )
  model.eval()
  __A	:		Optional[int]	  =      model(UpperCamelCase__    )
  self.parent.assertEqual(result.last_hidden_state.shape   ,   (self.batch_size, self.seq_length, self.hidden_size)    )


 def 			snake_case__ (   self   ,   __lowercase   ,   __lowercase   ,   __lowercase    ):

  """simple docstring"""







  __A	:		Tuple	  =      self.type_sequence_label_size
  __A	:		Any	  =      ViTHybridForImageClassification(UpperCamelCase__    )
  model.to(UpperCamelCase__    )
  model.eval()
  __A	:		Any	  =      model(UpperCamelCase__   ,   labels=UpperCamelCase__    )
  self.parent.assertEqual(result.logits.shape   ,   (self.batch_size, self.type_sequence_label_size)    )







 def 			snake_case__ (   self    ):

  """simple docstring"""







  __A	:		Union[str, Any]	  =      self.prepare_config_and_inputs()
  __A  ,__A  ,__A	:		int	  =      config_and_inputs
  __A	:		List[Any]	  =      {'pixel_values': pixel_values}
  return config, inputs_dict


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




 '''simple docstring'''
 __lowercase					:		str     				=    (ViTHybridModel, ViTHybridForImageClassification) if is_torch_available() else ()
 __lowercase					:		Dict     				=    (
     {'''feature-extraction''': ViTHybridModel, '''image-classification''': ViTHybridForImageClassification}
     if is_torch_available()
     else {}
 )
 __lowercase					:		List[Any]     				=    False
 __lowercase					:		Any     				=    False
 __lowercase					:		Optional[Any]     				=    False


 def 			snake_case__ (   self    ):

  """simple docstring"""







  __A	:		Any	  =      ViTHybridModelTester(self    )
  __A	:		str	  =      ConfigTester(self   ,   config_class=UpperCamelCase__   ,   has_text_modality=UpperCamelCase__   ,   hidden_size=37    )


 def 			snake_case__ (   self    ):

  """simple docstring"""







  self.config_tester.run_common_tests()


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

  """simple docstring"""







  pass


 def 			snake_case__ (   self    ):

  """simple docstring"""







  __A  ,__A	:		Dict	  =      self.model_tester.prepare_config_and_inputs_for_common()

  for model_class in self.all_model_classes:
   __A	:		Union[str, Any]	  =      model_class(UpperCamelCase__    )
   self.assertIsInstance(model.get_input_embeddings()   ,   (nn.Module)    )
   __A	:		Union[str, Any]	  =      model.get_output_embeddings()
   self.assertTrue(x is None or isinstance(UpperCamelCase__   ,   nn.Linear    )    )


 def 			snake_case__ (   self    ):

  """simple docstring"""







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

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

   __A	:		List[str]	  =      ['pixel_values']
   self.assertListEqual(arg_names[:1]   ,   UpperCamelCase__    )


 def 			snake_case__ (   self    ):

  """simple docstring"""







  __A	:		Any	  =      self.model_tester.prepare_config_and_inputs()
  self.model_tester.create_and_check_model(*UpperCamelCase__    )


 def 			snake_case__ (   self    ):

  """simple docstring"""







  __A	:		List[Any]	  =      self.model_tester.prepare_config_and_inputs()
  self.model_tester.create_and_check_for_image_classification(*UpperCamelCase__    )


 def 			snake_case__ (   self    ):

  """simple docstring"""







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

  __A	:		str	  =      _config_zero_init(UpperCamelCase__    )
  for model_class in self.all_model_classes:
   __A	:		str	  =      model_class(config=UpperCamelCase__    )
   # Skip the check for the backbone
   for name, module in model.named_modules():
    if module.__class__.__name__ == "ViTHybridPatchEmbeddings":
     __A	:		List[str]	  =      [F"""{name}.{key}""" for key in module.state_dict().keys()]
     break

   for name, param in model.named_parameters():
    if param.requires_grad:
     if name in backbone_params:
      continue
     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"""   ,   )







 @slow
 def 			snake_case__ (   self    ):

  """simple docstring"""







  for model_name in VIT_HYBRID_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
   __A	:		List[str]	  =      ViTHybridModel.from_pretrained(UpperCamelCase__    )
   self.assertIsNotNone(UpperCamelCase__    )


def       _lowercase							(					):
 __A	:		Tuple	  =      Image.open('./tests/fixtures/tests_samples/COCO/000000039769.png'      )
 return image


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




 '''simple docstring'''


 @cached_property
 def 			snake_case__ (   self    ):

  """simple docstring"""







  return (
      ViTHybridImageProcessor.from_pretrained(VIT_HYBRID_PRETRAINED_MODEL_ARCHIVE_LIST[0]    )
      if is_vision_available()
      else None
  )


 @slow
 def 			snake_case__ (   self    ):

  """simple docstring"""







  __A	:		Dict	  =      ViTHybridForImageClassification.from_pretrained(VIT_HYBRID_PRETRAINED_MODEL_ARCHIVE_LIST[0]    ).to(
      UpperCamelCase__    )

  __A	:		List[Any]	  =      self.default_image_processor
  __A	:		Optional[Any]	  =      prepare_img()
  __A	:		Dict	  =      image_processor(images=UpperCamelCase__   ,   return_tensors='pt'    ).to(UpperCamelCase__    )

  # forward pass
  with torch.no_grad():
   __A	:		Dict	  =      model(**UpperCamelCase__    )

  # verify the logits
  __A	:		Optional[int]	  =      torch.Size((1, 1_000)    )
  self.assertEqual(outputs.logits.shape   ,   UpperCamelCase__    )

  __A	:		Any	  =      torch.tensor([-1.9_0_9_0, -0.4_9_9_3, -0.2_3_8_9]    ).to(UpperCamelCase__    )

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







 @slow
 @require_accelerate
 def 			snake_case__ (   self    ):

  """simple docstring"""







  __A	:		Optional[Any]	  =      ViTHybridImageProcessor.from_pretrained('google/vit-hybrid-base-bit-384'    )
  __A	:		Optional[int]	  =      ViTHybridForImageClassification.from_pretrained('google/vit-hybrid-base-bit-384'   ,   device_map='auto'    )

  __A	:		List[Any]	  =      prepare_img()

  __A	:		List[str]	  =      image_processor(images=UpperCamelCase__   ,   return_tensors='pt'    )
  __A	:		int	  =      model(**UpperCamelCase__    )
  __A	:		Union[str, Any]	  =      outputs.logits
  # model predicts one of the 1000 ImageNet classes
  __A	:		Tuple	  =      logits.argmax(-1    ).item()

  self.assertTrue(model.config.idalabel[predicted_class_idx]   ,   'tabby, tabby cat'    )







'''simple docstring'''






import os
from pathlib import Path
from unittest.mock import patch

import pytest
import zstandard as zstd

from datasets.download.download_config import DownloadConfig
from datasets.utils.file_utils import (
    OfflineModeIsEnabled,
    cached_path,
    fsspec_get,
    fsspec_head,
    ftp_get,
    ftp_head,
    get_from_cache,
    http_get,
    http_head,
)


_UpperCAmelCase    :     Optional[int]         =       '''\
    Text data.
    Second line of data.'''

_UpperCAmelCase    :     Optional[Any]         =       '''file'''





@pytest.fixture(scope='session'				)
def  _SCREAMING_SNAKE_CASE		(      __snake_case							:			Any				):
     _A    		=  tmp_path_factory.mktemp('data'				) / (FILE_PATH + '.zstd')
     _A    		=  bytes(__snake_case   ,		'utf-8'				)
     with zstd.open(__snake_case   ,		'wb'				) as f:
          f.write(__snake_case				)
     return path





@pytest.fixture
def  _SCREAMING_SNAKE_CASE		(      __snake_case							:			Union[str, Any]				):
     with open(os.path.join(tmpfs.local_root_dir   ,		__snake_case				)   ,		'w'				) as f:
          f.write(__snake_case				)
     return FILE_PATH





@pytest.mark.parametrize('compression_format'   ,		['gzip', 'xz', 'zstd']				)
def  _SCREAMING_SNAKE_CASE		(      __snake_case							:			Dict   ,		__snake_case							:			str   ,		__snake_case							:			Any   ,		__snake_case							:			Any   ,		__snake_case							:			List[str]   ,		__snake_case							:			List[str]				):
     _A    		=  {'gzip': gz_file, 'xz': xz_file, 'zstd': zstd_path}
     _A    		=  input_paths[compression_format]
     _A    		=  tmp_path / 'cache'
     _A    		=  DownloadConfig(cache_dir=__snake_case   ,		extract_compressed_file=__snake_case				)
     _A    		=  cached_path(__snake_case   ,		download_config=__snake_case				)
     with open(__snake_case				) as f:
          _A    		=  f.read()
     with open(__snake_case				) as f:
          _A    		=  f.read()
     assert extracted_file_content == expected_file_content





@pytest.mark.parametrize('default_extracted'   ,		[True, False]				)
@pytest.mark.parametrize('default_cache_dir'   ,		[True, False]				)
def  _SCREAMING_SNAKE_CASE		(      __snake_case							:			Union[str, Any]   ,		__snake_case							:			List[str]   ,		__snake_case							:			Union[str, Any]   ,		__snake_case							:			int   ,		__snake_case							:			List[str]				):
     _A    		=  'custom_cache'
     _A    		=  'custom_extracted_dir'
     _A    		=  tmp_path / 'custom_extracted_path'
     if default_extracted:
          _A    		=  ('downloads' if default_cache_dir else custom_cache_dir, 'extracted')
     else:
          monkeypatch.setattr('datasets.config.EXTRACTED_DATASETS_DIR'   ,		__snake_case				)
          monkeypatch.setattr('datasets.config.EXTRACTED_DATASETS_PATH'   ,		str(__snake_case				)				)
          _A    		=  custom_extracted_path.parts[-2:] if default_cache_dir else (custom_cache_dir, custom_extracted_dir)

     _A    		=  xz_file
     _A    		=  (
         DownloadConfig(extract_compressed_file=__snake_case				)
         if default_cache_dir
         else DownloadConfig(cache_dir=tmp_path / custom_cache_dir   ,		extract_compressed_file=__snake_case				)
     )
     _A    		=  cached_path(__snake_case   ,		download_config=__snake_case				)
     assert Path(__snake_case				).parent.parts[-2:] == expected





def  _SCREAMING_SNAKE_CASE		(      __snake_case							:			str				):
     # absolute path
     _A    		=  str(Path(__snake_case				).resolve()				)
     assert cached_path(__snake_case				) == text_file
     # relative path
     _A    		=  str(Path(__snake_case				).resolve().relative_to(Path(os.getcwd()				)				)				)
     assert cached_path(__snake_case				) == text_file





def  _SCREAMING_SNAKE_CASE		(      __snake_case							:			List[str]				):
     # absolute path
     _A    		=  str(tmp_path.resolve() / '__missing_file__.txt'				)
     with pytest.raises(__snake_case				):
          cached_path(__snake_case				)
     # relative path
     _A    		=  './__missing_file__.txt'
     with pytest.raises(__snake_case				):
          cached_path(__snake_case				)





def  _SCREAMING_SNAKE_CASE		(      __snake_case							:			int				):
     _A    		=  get_from_cache(F'tmp://{tmpfs_file}'				)
     with open(__snake_case				) as f:
          _A    		=  f.read()
     assert output_file_content == FILE_CONTENT





@patch('datasets.config.HF_DATASETS_OFFLINE'   ,		__snake_case				)
def  _SCREAMING_SNAKE_CASE		(      ):
     with pytest.raises(__snake_case				):
          cached_path('https://huggingface.co'				)





@patch('datasets.config.HF_DATASETS_OFFLINE'   ,		__snake_case				)
def  _SCREAMING_SNAKE_CASE		(      __snake_case							:			Tuple				):
     _A    		=  tmp_path_factory.mktemp('data'				) / 'file.html'
     with pytest.raises(__snake_case				):
          http_get('https://huggingface.co'   ,		temp_file=__snake_case				)
     with pytest.raises(__snake_case				):
          http_head('https://huggingface.co'				)





@patch('datasets.config.HF_DATASETS_OFFLINE'   ,		__snake_case				)
def  _SCREAMING_SNAKE_CASE		(      __snake_case							:			Optional[int]				):
     _A    		=  tmp_path_factory.mktemp('data'				) / 'file.html'
     with pytest.raises(__snake_case				):
          ftp_get('ftp://huggingface.co'   ,		temp_file=__snake_case				)
     with pytest.raises(__snake_case				):
          ftp_head('ftp://huggingface.co'				)





@patch('datasets.config.HF_DATASETS_OFFLINE'   ,		__snake_case				)
def  _SCREAMING_SNAKE_CASE		(      __snake_case							:			List[str]				):
     _A    		=  tmp_path_factory.mktemp('data'				) / 'file.html'
     with pytest.raises(__snake_case				):
          fsspec_get('s3://huggingface.co'   ,		temp_file=__snake_case				)
     with pytest.raises(__snake_case				):
          fsspec_head('s3://huggingface.co'				)



