






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  ,      _lowerCamelCase  ,      _lowerCamelCase=1_3  ,      _lowerCamelCase=6_4  ,      _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.0_2  ,      _lowerCamelCase=[1, 1_6, 4, 4]  ,      _lowerCamelCase=None  ,      ):
         UpperCamelCase_: List[str]   				=    parent
         UpperCamelCase_: Dict   				=    batch_size
         UpperCamelCase_: str   				=    image_size
         UpperCamelCase_: Tuple   				=    patch_size
         UpperCamelCase_: List[Any]   				=    num_channels
         UpperCamelCase_: Union[str, Any]   				=    is_training
         UpperCamelCase_: str   				=    use_labels
         UpperCamelCase_: str   				=    hidden_size
         UpperCamelCase_: Union[str, Any]   				=    num_hidden_layers
         UpperCamelCase_: Any   				=    num_attention_heads
         UpperCamelCase_: Any   				=    intermediate_size
         UpperCamelCase_: Optional[Any]   				=    hidden_act
         UpperCamelCase_: Any   				=    hidden_dropout_prob
         UpperCamelCase_: Any   				=    attention_probs_dropout_prob
         UpperCamelCase_: str   				=    type_sequence_label_size
         UpperCamelCase_: str   				=    initializer_range
         UpperCamelCase_: Optional[Any]   				=    scope
         UpperCamelCase_: List[str]   				=    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
         UpperCamelCase_: Union[str, Any]   				=    (self.image_size // 3_2) ** 2
         UpperCamelCase_: int   				=    num_patches + 1





       def       _a   ( self							):
         UpperCamelCase_: List[str]   				=    floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]							)

         UpperCamelCase_: int   				=    None
         if self.use_labels:
           UpperCamelCase_: Any   				=    ids_tensor([self.batch_size]  ,      self.type_sequence_label_size							)

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

         return config, pixel_values, labels





       def       _a   ( self							):
         UpperCamelCase_: int   				=    {
             'global_padding': 'same',
             'layer_type': 'bottleneck',
             'depths': [3, 4, 9],
             'out_features': ['stage1', 'stage2', 'stage3'],
             'embedding_dynamic_padding': True,
             'hidden_sizes': [4, 8, 1_6, 3_2],
             '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=_lowerCamelCase  ,      initializer_range=self.initializer_range  ,      backbone_featmap_shape=self.backbone_featmap_shape  ,      backbone_config=_lowerCamelCase  ,      )





       def       _a   ( self  ,      _lowerCamelCase  ,      _lowerCamelCase  ,      _lowerCamelCase							):
         UpperCamelCase_: Optional[Any]   				=    ViTHybridModel(config=_lowerCamelCase							)
         model.to(_lowerCamelCase							)
         model.eval()
         UpperCamelCase_: List[Any]   				=    model(_lowerCamelCase							)
         self.parent.assertEqual(result.last_hidden_state.shape  ,      (self.batch_size, self.seq_length, self.hidden_size)							)





       def       _a   ( self  ,      _lowerCamelCase  ,      _lowerCamelCase  ,      _lowerCamelCase							):
         UpperCamelCase_: List[str]   				=    self.type_sequence_label_size
         UpperCamelCase_: Dict   				=    ViTHybridForImageClassification(_lowerCamelCase							)
         model.to(_lowerCamelCase							)
         model.eval()
         UpperCamelCase_: List[str]   				=    model(_lowerCamelCase  ,      labels=_lowerCamelCase							)
         self.parent.assertEqual(result.logits.shape  ,      (self.batch_size, self.type_sequence_label_size)							)





       def       _a   ( self							):
         UpperCamelCase_: List[str]   				=    self.prepare_config_and_inputs()
         UpperCamelCase_				,UpperCamelCase_				,UpperCamelCase_: Optional[int]   				=    config_and_inputs
         UpperCamelCase_: int   				=    {'pixel_values': pixel_values}
         return config, inputs_dict







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





       """simple docstring"""







       a    :		List[Any]									=(ViTHybridModel, ViTHybridForImageClassification) if is_torch_available() else ()
       a    :		str									=(
           {'''feature-extraction''': ViTHybridModel, '''image-classification''': ViTHybridForImageClassification}
           if is_torch_available()
           else {}
       )
       a    :		Optional[int]									=False
       a    :		Tuple									=False
       a    :		int									=False





       def       _a   ( self							):
         UpperCamelCase_: Dict   				=    ViTHybridModelTester(self							)
         UpperCamelCase_: int   				=    ConfigTester(self  ,      config_class=_lowerCamelCase  ,      has_text_modality=_lowerCamelCase  ,      hidden_size=3_7							)





       def       _a   ( self							):
         self.config_tester.run_common_tests()





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





       def       _a   ( self							):
         UpperCamelCase_				,UpperCamelCase_: Union[str, Any]   				=    self.model_tester.prepare_config_and_inputs_for_common()

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





       def       _a   ( self							):
         UpperCamelCase_				,UpperCamelCase_: Optional[Any]   				=    self.model_tester.prepare_config_and_inputs_for_common()

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

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





       def       _a   ( self							):
         UpperCamelCase_: List[Any]   				=    self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_model(*_lowerCamelCase							)





       def       _a   ( self							):
         UpperCamelCase_: str   				=    self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_for_image_classification(*_lowerCamelCase							)





       def       _a   ( self							):
         UpperCamelCase_				,UpperCamelCase_: int   				=    self.model_tester.prepare_config_and_inputs_for_common()

         UpperCamelCase_: List[Any]   				=    _config_zero_init(_lowerCamelCase							)
         for model_class in self.all_model_classes:
           UpperCamelCase_: Optional[Any]   				=    model_class(config=_lowerCamelCase							)
           # Skip the check for the backbone
           for name, module in model.named_modules():
             if module.__class__.__name__ == "ViTHybridPatchEmbeddings":
               UpperCamelCase_: 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       _a   ( self							):
         for model_name in VIT_HYBRID_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
           UpperCamelCase_: str   				=    ViTHybridModel.from_pretrained(_lowerCamelCase							)
           self.assertIsNotNone(_lowerCamelCase							)







def   snake_case ()	->    List[str]:
  UpperCamelCase_: Any   				=    Image.open('./tests/fixtures/tests_samples/COCO/000000039769.png'  )
  return image




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





       """simple docstring"""







       @cached_property
       def       _a   ( self							):
         return (
             ViTHybridImageProcessor.from_pretrained(VIT_HYBRID_PRETRAINED_MODEL_ARCHIVE_LIST[0]							)
             if is_vision_available()
             else None
         )





       @slow
       def       _a   ( self							):
         UpperCamelCase_: Union[str, Any]   				=    ViTHybridForImageClassification.from_pretrained(VIT_HYBRID_PRETRAINED_MODEL_ARCHIVE_LIST[0]							).to(
             _lowerCamelCase							)

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

         # forward pass
         with torch.no_grad():
           UpperCamelCase_: Optional[Any]   				=    model(**_lowerCamelCase							)

         # verify the logits
         UpperCamelCase_: Union[str, Any]   				=    torch.Size((1, 1_0_0_0)							)
         self.assertEqual(outputs.logits.shape  ,      _lowerCamelCase							)

         UpperCamelCase_: int   				=    torch.tensor([-1.9_0_9_0, -0.4_9_9_3, -0.2_3_8_9]							).to(_lowerCamelCase							)

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





       @slow
       @require_accelerate
       def       _a   ( self							):
         UpperCamelCase_: Dict   				=    ViTHybridImageProcessor.from_pretrained('google/vit-hybrid-base-bit-384'							)
         UpperCamelCase_: Optional[int]   				=    ViTHybridForImageClassification.from_pretrained('google/vit-hybrid-base-bit-384'  ,      device_map='auto'							)

         UpperCamelCase_: Dict   				=    prepare_img()

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

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







import os
import sys
import tempfile

import torch

from .state import AcceleratorState
from .utils import PrecisionType, PrepareForLaunch, is_mps_available, patch_environment

def   snake_case (UpperCAmelCase__     ,   UpperCAmelCase__=()     ,   UpperCAmelCase__=None     ,   UpperCAmelCase__="no"     ,   UpperCAmelCase__="29500"  )	->    List[Any]:
  UpperCamelCase_: Any   				=    False
  UpperCamelCase_: List[str]   				=    False
  if any(key.startswith('KAGGLE'  ) for key in os.environ.keys()  ):
    UpperCamelCase_: List[Any]   				=    True
  elif "IPython" in sys.modules:
    UpperCamelCase_: List[Any]   				=    'google.colab' in str(sys.modules['IPython'].get_ipython()  )

  try:
    UpperCamelCase_: Optional[int]   				=    PrecisionType(mixed_precision.lower()  )
  except ValueError:
    raise ValueError(
        F'''Unknown mixed_precision mode: {args.mixed_precision.lower()}. Choose between {PrecisionType.list()}.'''  )

  if (in_colab or in_kaggle) and (os.environ.get('TPU_NAME'     ,   UpperCAmelCase__  ) is not None):
    # TPU launch
    import torch_xla.distributed.xla_multiprocessing as xmp

    if len(AcceleratorState._shared_state  ) > 0:
      raise ValueError(
          'To train on TPU in Colab or Kaggle Kernel, the `Accelerator` should only be initialized inside '
          'your training function. Restart your notebook and make sure no cells initializes an '
          '`Accelerator`.'  )
    if num_processes is None:
      UpperCamelCase_: List[str]   				=    8

    UpperCamelCase_: str   				=    PrepareForLaunch(UpperCAmelCase__     ,   distributed_type='TPU'  )
    print(F'''Launching a training on {num_processes} TPU cores.'''  )
    xmp.spawn(UpperCAmelCase__     ,   args=UpperCAmelCase__     ,   nprocs=UpperCAmelCase__     ,   start_method='fork'  )
  elif in_colab:
    # No need for a distributed launch otherwise as it's either CPU or one GPU.
    if torch.cuda.is_available():
      print('Launching training on one GPU.'  )
    else:
      print('Launching training on one CPU.'  )
    function(*UpperCAmelCase__  )
  else:
    if num_processes is None:
      raise ValueError(
          'You have to specify the number of GPUs you would like to use, add `num_processes=...` to your call.'  )

    if num_processes > 1:
      # Multi-GPU launch
      from torch.multiprocessing import start_processes
      from torch.multiprocessing.spawn import ProcessRaisedException

      if len(AcceleratorState._shared_state  ) > 0:
        raise ValueError(
            'To launch a multi-GPU training from your notebook, the `Accelerator` should only be initialized '
            'inside your training function. Restart your notebook and make sure no cells initializes an '
            '`Accelerator`.'  )

      if torch.cuda.is_initialized():
        raise ValueError(
            'To launch a multi-GPU training from your notebook, you need to avoid running any instruction '
            'using `torch.cuda` in any cell. Restart your notebook and make sure no cells use any CUDA '
            'function.'  )

      # torch.distributed will expect a few environment variable to be here. We set the ones common to each
      # process here (the other ones will be set be the launcher).
      with patch_environment(
          world_size=UpperCAmelCase__     ,   master_addr='127.0.01'     ,   master_port=UpperCAmelCase__     ,   mixed_precision=UpperCAmelCase__  ):
        UpperCamelCase_: str   				=    PrepareForLaunch(UpperCAmelCase__     ,   distributed_type='MULTI_GPU'  )
        print(F'''Launching training on {num_processes} GPUs.'''  )
        try:
          start_processes(UpperCAmelCase__     ,   args=UpperCAmelCase__     ,   nprocs=UpperCAmelCase__     ,   start_method='fork'  )
        except ProcessRaisedException as e:
          if "Cannot re-initialize CUDA in forked subprocess" in e.args[0]:
            raise RuntimeError(
                'CUDA has been initialized before the `notebook_launcher` could create a forked subprocess. '
                'This likely stems from an outside import causing issues once the `notebook_launcher()` is called. '
                'Please review your imports and test them when running the `notebook_launcher()` to identify '
                'which one is problematic.'  ) from e

    else:
      # No need for a distributed launch otherwise as it's either CPU, GPU or MPS.
      if is_mps_available():
        UpperCamelCase_: Tuple   				=    '1'
        print('Launching training on MPS.'  )
      elif torch.cuda.is_available():
        print('Launching training on one GPU.'  )
      else:
        print('Launching training on CPU.'  )
      function(*UpperCAmelCase__  )

def   snake_case (UpperCAmelCase__     ,   UpperCAmelCase__=()     ,   UpperCAmelCase__=2  )	->    Optional[int]:
  from torch.multiprocessing import start_processes

  with tempfile.NamedTemporaryFile() as tmp_file:
    # torch.distributed will expect a few environment variable to be here. We set the ones common to each
    # process here (the other ones will be set be the launcher).
    with patch_environment(
        world_size=UpperCAmelCase__     ,   master_addr='127.0.01'     ,   master_port='29500'     ,   accelerate_mixed_precision='no'     ,   accelerate_debug_rdv_file=tmp_file.name     ,   accelerate_use_cpu='yes'     ,   ):
      UpperCamelCase_: str   				=    PrepareForLaunch(UpperCAmelCase__     ,   debug=UpperCAmelCase__  )
      start_processes(UpperCAmelCase__     ,   args=UpperCAmelCase__     ,   nprocs=UpperCAmelCase__     ,   start_method='fork'  )