



"""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    SCREAMING_SNAKE_CASE__ :

    def __init__(				self			:				int ,  lowerCAmelCase_			:				str ,  lowerCAmelCase_			:				Tuple=1_3 ,  lowerCAmelCase_			:				Union[str, Any]=6_4 ,  lowerCAmelCase_			:				List[Any]=2 ,  lowerCAmelCase_			:				Optional[int]=3 ,  lowerCAmelCase_			:				int=True ,  lowerCAmelCase_			:				List[str]=True ,  lowerCAmelCase_			:				Dict=3_2 ,  lowerCAmelCase_			:				str=5 ,  lowerCAmelCase_			:				List[str]=4 ,  lowerCAmelCase_			:				Tuple=3_7 ,  lowerCAmelCase_			:				List[Any]="gelu" ,  lowerCAmelCase_			:				List[Any]=0.1 ,  lowerCAmelCase_			:				str=0.1 ,  lowerCAmelCase_			:				Union[str, Any]=1_0 ,  lowerCAmelCase_			:				Optional[Any]=0.02 ,  lowerCAmelCase_			:				Optional[int]=[1, 1_6, 4, 4] ,  lowerCAmelCase_			:				List[str]=None ,  ):







      """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_								= 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
      lowercase_								= (self.image_size // 3_2) ** 2
      lowercase_								= num_patches + 1

    def 					_UpperCAmelCase	(				self			:				Union[str, 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]):







      """simple docstring"""







      lowercase_								= {
          """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 					_UpperCAmelCase	(				self			:				List[str] ,  lowerCAmelCase_			:				Any ,  lowerCAmelCase_			:				int ,  lowerCAmelCase_			:				Optional[int]):







      """simple docstring"""







      lowercase_								= ViTHybridModel(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			:				List[Any] ,  lowerCAmelCase_			:				List[str] ,  lowerCAmelCase_			:				Optional[int] ,  lowerCAmelCase_			:				Optional[Any]):







      """simple docstring"""







      lowercase_								= self.type_sequence_label_size
      lowercase_								= ViTHybridForImageClassification(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))




    def 					_UpperCAmelCase	(				self			:				List[str]):







      """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    SCREAMING_SNAKE_CASE__ (							__UpperCAmelCase      ,  __UpperCAmelCase      ,  unittest.TestCase   ):
    lowercase__      =   (ViTHybridModel, ViTHybridForImageClassification) if is_torch_available() else ()
    lowercase__      =   (
        {"feature-extraction": ViTHybridModel, "image-classification": ViTHybridForImageClassification}
        if is_torch_available()
        else {}
    )
    lowercase__      =   False
    lowercase__      =   False
    lowercase__      =   False

    def 					_UpperCAmelCase	(				self			:				List[str]):







      """simple docstring"""







      lowercase_								= ViTHybridModelTester(self)
      lowercase_								= ConfigTester(self ,  config_class=lowerCAmelCase_ ,  has_text_modality=lowerCAmelCase_ ,  hidden_size=3_7)

    def 					_UpperCAmelCase	(				self			:				int):







      """simple docstring"""







      self.config_tester.run_common_tests()

    @unittest.skip(reason="""ViT does not use inputs_embeds""")
    def 					_UpperCAmelCase	(				self			:				Any):







      """simple docstring"""







      pass

    def 					_UpperCAmelCase	(				self			:				Optional[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			:				Dict):







      """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			:				Optional[int]):







      """simple docstring"""







      lowercase_								= self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_model(*lowerCAmelCase_)

    def 					_UpperCAmelCase	(				self			:				str):







      """simple docstring"""







      lowercase_								= self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_for_image_classification(*lowerCAmelCase_)

    def 					_UpperCAmelCase	(				self			:				int):







      """simple docstring"""







      lowercase_					,    lowercase_								= self.model_tester.prepare_config_and_inputs_for_common()

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







      """simple docstring"""







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







def 			_SCREAMING_SNAKE_CASE						() ->						Tuple:







  '''simple docstring'''


  lowercase_								= Image.open("""./tests/fixtures/tests_samples/COCO/000000039769.png"""							)
  return image


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

    @cached_property
    def 					_UpperCAmelCase	(				self			:				Any):







      """simple docstring"""







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

    @slow
    def 					_UpperCAmelCase	(				self			:				str):







      """simple docstring"""







      lowercase_								= ViTHybridForImageClassification.from_pretrained(VIT_HYBRID_PRETRAINED_MODEL_ARCHIVE_LIST[0]).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_0_0_0))
      self.assertEqual(outputs.logits.shape ,  lowerCAmelCase_)

      lowercase_								= torch.tensor([-1.9_090, -0.4_993, -0.2_389]).to(lowerCAmelCase_)

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




    @slow
    @require_accelerate
    def 					_UpperCAmelCase	(				self			:				Optional[int]):







      """simple docstring"""







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

      lowercase_								= prepare_img()

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

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






"""simple docstring"""
import numpy as np
from sklearn.datasets import fetch_california_housing
from sklearn.metrics import mean_absolute_error, mean_squared_error
from sklearn.model_selection import train_test_split
from xgboost import XGBRegressor


def 			_SCREAMING_SNAKE_CASE						(__lowerCAmelCase							) ->						tuple:







  '''simple docstring'''


  return (data["data"], data["target"])


def 			_SCREAMING_SNAKE_CASE						(__lowerCAmelCase ,  __lowerCAmelCase ,  __lowerCAmelCase							) ->						np.ndarray:







  '''simple docstring'''


  lowercase_								= XGBRegressor(verbosity=0 ,  random_state=42							)
  xgb.fit(__lowerCAmelCase ,  __lowerCAmelCase							)
  # Predict target for test data
  lowercase_								= xgb.predict(__lowerCAmelCase							)
  lowercase_								= predictions.reshape(len(__lowerCAmelCase							) ,  1							)
  return predictions


def 			_SCREAMING_SNAKE_CASE						() ->						None:







  '''simple docstring'''


  lowercase_								= fetch_california_housing()
  lowercase_					,    lowercase_								= data_handling(__lowerCAmelCase							)
  lowercase_					,    lowercase_					,    lowercase_					,    lowercase_								= train_test_split(
      __lowerCAmelCase ,  __lowerCAmelCase ,  test_size=0.25 ,  random_state=1							)
  lowercase_								= xgboost(__lowerCAmelCase ,  __lowerCAmelCase ,  __lowerCAmelCase							)
  # Error printing
  print(F'''Mean Absolute Error : {mean_absolute_error(__lowerCAmelCase ,  __lowerCAmelCase							)}'''							)
  print(F'''Mean Square Error  : {mean_squared_error(__lowerCAmelCase ,  __lowerCAmelCase							)}'''							)


if __name__ == "__main__":
       import doctest

       doctest.testmod(verbose=True)
       main()

