'''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     a__      :






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





         """simple docstring"""



         __lowerCamelCase		=  parent
         __lowerCamelCase		=  batch_size
         __lowerCamelCase		=  image_size
         __lowerCamelCase		=  patch_size
         __lowerCamelCase		=  num_channels
         __lowerCamelCase		=  is_training
         __lowerCamelCase		=  use_labels
         __lowerCamelCase		=  hidden_size
         __lowerCamelCase		=  num_hidden_layers
         __lowerCamelCase		=  num_attention_heads
         __lowerCamelCase		=  intermediate_size
         __lowerCamelCase		=  hidden_act
         __lowerCamelCase		=  hidden_dropout_prob
         __lowerCamelCase		=  attention_probs_dropout_prob
         __lowerCamelCase		=  type_sequence_label_size
         __lowerCamelCase		=  initializer_range
         __lowerCamelCase		=  scope
         __lowerCamelCase		=  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
         __lowerCamelCase		=  (self.image_size // 32) ** 2
         __lowerCamelCase		=  num_patches + 1






     def    SCREAMING_SNAKE_CASE__   ( self		:		Optional[Any]    ):





         """simple docstring"""



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

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

         __lowerCamelCase		=  self.get_config()

         return config, pixel_values, labels






     def    SCREAMING_SNAKE_CASE__   ( self		:		Dict    ):





         """simple docstring"""



         __lowerCamelCase		=  {
             '''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=a  ,				initializer_range=self.initializer_range  ,				backbone_featmap_shape=self.backbone_featmap_shape  ,				backbone_config=a  ,				)






     def    SCREAMING_SNAKE_CASE__   ( self		:		List[str]  ,				a		:		Any  ,				a		:		Optional[int]  ,				a		:		Tuple    ):





         """simple docstring"""



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






     def    SCREAMING_SNAKE_CASE__   ( self		:		Any  ,				a		:		Dict  ,				a		:		Dict  ,				a		:		Union[str, Any]    ):





         """simple docstring"""



         __lowerCamelCase		=  self.type_sequence_label_size
         __lowerCamelCase		=  ViTHybridForImageClassification(a    )
         model.to(a    )
         model.eval()
         __lowerCamelCase		=  model(a  ,				labels=a    )
         self.parent.assertEqual(result.logits.shape  ,				(self.batch_size, self.type_sequence_label_size)    )






     def    SCREAMING_SNAKE_CASE__   ( self		:		Any    ):





         """simple docstring"""



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




@require_torch
class     a__      (   UpperCAmelCase__							,   UpperCAmelCase__							,   unittest.TestCase ):
     lowerCamelCase  :     Optional[Any]							=(ViTHybridModel, ViTHybridForImageClassification) if is_torch_available() else ()
     lowerCamelCase  :     Tuple							=(
         {"feature-extraction": ViTHybridModel, "image-classification": ViTHybridForImageClassification}
         if is_torch_available()
         else {}
     )
     lowerCamelCase  :     List[Any]							=False
     lowerCamelCase  :     Tuple							=False
     lowerCamelCase  :     List[str]							=False






     def    SCREAMING_SNAKE_CASE__   ( self		:		Optional[Any]    ):





         """simple docstring"""



         __lowerCamelCase		=  ViTHybridModelTester(self    )
         __lowerCamelCase		=  ConfigTester(self  ,				config_class=a  ,				has_text_modality=a  ,				hidden_size=37    )






     def    SCREAMING_SNAKE_CASE__   ( self		:		str    ):





         """simple docstring"""



         self.config_tester.run_common_tests()






     @unittest.skip(reason='''ViT does not use inputs_embeds'''    )
     def    SCREAMING_SNAKE_CASE__   ( self		:		List[str]    ):





         """simple docstring"""



         pass






     def    SCREAMING_SNAKE_CASE__   ( self		:		Any    ):





         """simple docstring"""



         __lowerCamelCase	,  __lowerCamelCase		=  self.model_tester.prepare_config_and_inputs_for_common()

         for model_class in self.all_model_classes:
             __lowerCamelCase		=  model_class(a    )
             self.assertIsInstance(model.get_input_embeddings()  ,				(nn.Module)    )
             __lowerCamelCase		=  model.get_output_embeddings()
             self.assertTrue(x is None or isinstance(a  ,				nn.Linear    )    )






     def    SCREAMING_SNAKE_CASE__   ( self		:		Any    ):





         """simple docstring"""



         __lowerCamelCase	,  __lowerCamelCase		=  self.model_tester.prepare_config_and_inputs_for_common()

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

             __lowerCamelCase		=  ['''pixel_values''']
             self.assertListEqual(arg_names[:1]  ,				a    )






     def    SCREAMING_SNAKE_CASE__   ( self		:		List[Any]    ):





         """simple docstring"""



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






     def    SCREAMING_SNAKE_CASE__   ( self		:		str    ):





         """simple docstring"""



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






     def    SCREAMING_SNAKE_CASE__   ( self		:		str    ):





         """simple docstring"""



         __lowerCamelCase	,  __lowerCamelCase		=  self.model_tester.prepare_config_and_inputs_for_common()

         __lowerCamelCase		=  _config_zero_init(a    )
         for model_class in self.all_model_classes:
             __lowerCamelCase		=  model_class(config=a    )
             # Skip the check for the backbone
             for name, module in model.named_modules():
                 if module.__class__.__name__ == "ViTHybridPatchEmbeddings":
                     __lowerCamelCase		=  [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    SCREAMING_SNAKE_CASE__   ( self		:		Any    ):





         """simple docstring"""



         for model_name in VIT_HYBRID_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
             __lowerCamelCase		=  ViTHybridModel.from_pretrained(a    )
             self.assertIsNotNone(a    )






def __lowerCAmelCase (							)	->    Optional[int]:
    __lowerCamelCase		=  Image.open('''./tests/fixtures/tests_samples/COCO/000000039769.png'''				)
    return image




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






     @cached_property
     def    SCREAMING_SNAKE_CASE__   ( self		:		Union[str, Any]    ):





         """simple docstring"""



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






     @slow
     def    SCREAMING_SNAKE_CASE__   ( self		:		Any    ):





         """simple docstring"""



         __lowerCamelCase		=  ViTHybridForImageClassification.from_pretrained(VIT_HYBRID_PRETRAINED_MODEL_ARCHIVE_LIST[0]    ).to(
             a    )

         __lowerCamelCase		=  self.default_image_processor
         __lowerCamelCase		=  prepare_img()
         __lowerCamelCase		=  image_processor(images=a  ,				return_tensors='''pt'''    ).to(a    )

         # forward pass
         with torch.no_grad():
             __lowerCamelCase		=  model(**a    )

         # verify the logits
         __lowerCamelCase		=  torch.Size((1, 10_00)    )
         self.assertEqual(outputs.logits.shape  ,				a    )

         __lowerCamelCase		=  torch.tensor([-1.90_90, -0.49_93, -0.23_89]    ).to(a    )

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






     @slow
     @require_accelerate
     def    SCREAMING_SNAKE_CASE__   ( self		:		str    ):





         """simple docstring"""



         __lowerCamelCase		=  ViTHybridImageProcessor.from_pretrained('''google/vit-hybrid-base-bit-384'''    )
         __lowerCamelCase		=  ViTHybridForImageClassification.from_pretrained('''google/vit-hybrid-base-bit-384'''  ,				device_map='''auto'''    )

         __lowerCamelCase		=  prepare_img()

         __lowerCamelCase		=  image_processor(images=a  ,				return_tensors='''pt'''    )
         __lowerCamelCase		=  model(**a    )
         __lowerCamelCase		=  outputs.logits
         # model predicts one of the 1000 ImageNet classes
         __lowerCamelCase		=  logits.argmax(-1    ).item()

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




import inspect
import re
from hashlib import shaaaa
from typing import Dict, List

from .arrow import arrow
from .audiofolder import audiofolder
from .csv import csv
from .imagefolder import imagefolder
from .json import json
from .pandas import pandas
from .parquet import parquet
from .sql import sql  # noqa F401
from .text import text







def        SCREAMING_SNAKE_CASE_						(			__A :			List[str]				)	->		str:

       """simple docstring"""

       a_					:		Tuple							=  []
       for line in lines:
              a_					:		Any							=  re.sub(R'#.*'  ,    ''  ,    __A				)  # remove comments
              if line:
                     filtered_lines.append(__A				)
       a_					:		Tuple							=  '\n'.join(__A				)

       # Make a hash from all this code
       a_					:		Tuple							=  full_str.encode('utf-8'				)
       return shaaaa(__A				).hexdigest()


# get importable module names and hash for caching
UpperCAmelCase_      : List[Any]  					=       {
    'csv': (csv.__name__, _hash_python_lines(inspect.getsource(csv).splitlines())),
    'json': (json.__name__, _hash_python_lines(inspect.getsource(json).splitlines())),
    'pandas': (pandas.__name__, _hash_python_lines(inspect.getsource(pandas).splitlines())),
    'parquet': (parquet.__name__, _hash_python_lines(inspect.getsource(parquet).splitlines())),
    'arrow': (arrow.__name__, _hash_python_lines(inspect.getsource(arrow).splitlines())),
    'text': (text.__name__, _hash_python_lines(inspect.getsource(text).splitlines())),
    'imagefolder': (imagefolder.__name__, _hash_python_lines(inspect.getsource(imagefolder).splitlines())),
    'audiofolder': (audiofolder.__name__, _hash_python_lines(inspect.getsource(audiofolder).splitlines())),
}

# Used to infer the module to use based on the data files extensions
UpperCAmelCase_      : Dict  					=       {
    '.csv': ('csv', {}),
    '.tsv': ('csv', {'sep': '\t'}),
    '.json': ('json', {}),
    '.jsonl': ('json', {}),
    '.parquet': ('parquet', {}),
    '.arrow': ('arrow', {}),
    '.txt': ('text', {}),
}
_EXTENSION_TO_MODULE.update({ext: ('imagefolder', {}) for ext in imagefolder.ImageFolder.EXTENSIONS})
_EXTENSION_TO_MODULE.update({ext.upper(): ('imagefolder', {}) for ext in imagefolder.ImageFolder.EXTENSIONS})
_EXTENSION_TO_MODULE.update({ext: ('audiofolder', {}) for ext in audiofolder.AudioFolder.EXTENSIONS})
_EXTENSION_TO_MODULE.update({ext.upper(): ('audiofolder', {}) for ext in audiofolder.AudioFolder.EXTENSIONS})
UpperCAmelCase_      : Optional[int]  					=       {'imagefolder', 'audiofolder'}

# Used to filter data files based on extensions given a module name
UpperCAmelCase_      : Dict[str, List[str]]  					=       {}
for _ext, (_module, _) in _EXTENSION_TO_MODULE.items():
   _MODULE_TO_EXTENSIONS.setdefault(_module, []).append(_ext)

_MODULE_TO_EXTENSIONS["imagefolder"].append('.zip')
_MODULE_TO_EXTENSIONS["audiofolder"].append('.zip')


