


'''simple docstring'''

import inspect
import unittest

from transformers import MobileNetVaConfig
from transformers.testing_utils import 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, floats_tensor, ids_tensor
from ...test_pipeline_mixin import PipelineTesterMixin


if is_torch_available():
       import torch

       from transformers import MobileNetVaForImageClassification, MobileNetVaModel
       from transformers.models.mobilenet_va.modeling_mobilenet_va import MOBILENET_V1_PRETRAINED_MODEL_ARCHIVE_LIST


if is_vision_available():
       from PIL import Image

       from transformers import MobileNetVaImageProcessor





class   snake_case					(					lowerCAmelCase__    ):





  """simple docstring"""




  def 						snake_case	(					self  ):







   """simple docstring"""
   lowerCamelCase_  				=						self.config_class(**self.inputs_dict  )
   self.parent.assertTrue(hasattr(_lowerCamelCase    ,						"tf_padding"  )  )
   self.parent.assertTrue(hasattr(_lowerCamelCase    ,						"depth_multiplier"  )  )





class   snake_case					:





  """simple docstring"""




  def __init__(					self    ,						UpperCamelCase    ,						UpperCamelCase=13    ,						UpperCamelCase=3    ,						UpperCamelCase=32    ,						UpperCamelCase=0.25    ,						UpperCamelCase=8    ,						UpperCamelCase=True    ,						UpperCamelCase=1024    ,						UpperCamelCase=32    ,						UpperCamelCase="relu6"    ,						UpperCamelCase=0.1    ,						UpperCamelCase=0.02    ,						UpperCamelCase=True    ,						UpperCamelCase=True    ,						UpperCamelCase=10    ,						UpperCamelCase=None    ,						):







   """simple docstring"""
   lowerCamelCase_  				=						parent
   lowerCamelCase_  				=						batch_size
   lowerCamelCase_  				=						num_channels
   lowerCamelCase_  				=						image_size
   lowerCamelCase_  				=						depth_multiplier
   lowerCamelCase_  				=						min_depth
   lowerCamelCase_  				=						tf_padding
   lowerCamelCase_  				=						int(last_hidden_size * depth_multiplier  )
   lowerCamelCase_  				=						output_stride
   lowerCamelCase_  				=						hidden_act
   lowerCamelCase_  				=						classifier_dropout_prob
   lowerCamelCase_  				=						use_labels
   lowerCamelCase_  				=						is_training
   lowerCamelCase_  				=						num_labels
   lowerCamelCase_  				=						initializer_range
   lowerCamelCase_  				=						scope






  def 						snake_case	(					self  ):







   """simple docstring"""
   lowerCamelCase_  				=						floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]  )

   lowerCamelCase_  				=						None
   lowerCamelCase_  				=						None
   if self.use_labels:
    lowerCamelCase_  				=						ids_tensor([self.batch_size]    ,						self.num_labels  )
    lowerCamelCase_  				=						ids_tensor([self.batch_size, self.image_size, self.image_size]    ,						self.num_labels  )

   lowerCamelCase_  				=						self.get_config()

   return config, pixel_values, labels, pixel_labels






  def 						snake_case	(					self  ):







   """simple docstring"""
   return MobileNetVaConfig(
       num_channels=self.num_channels    ,						image_size=self.image_size    ,						depth_multiplier=self.depth_multiplier    ,						min_depth=self.min_depth    ,						tf_padding=self.tf_padding    ,						hidden_act=self.hidden_act    ,						classifier_dropout_prob=self.classifier_dropout_prob    ,						initializer_range=self.initializer_range    ,						)






  def 						snake_case	(					self    ,						UpperCamelCase    ,						UpperCamelCase    ,						UpperCamelCase    ,						UpperCamelCase  ):







   """simple docstring"""
   lowerCamelCase_  				=						MobileNetVaModel(config=_lowerCamelCase  )
   model.to(_lowerCamelCase  )
   model.eval()
   lowerCamelCase_  				=						model(_lowerCamelCase  )
   self.parent.assertEqual(
       result.last_hidden_state.shape    ,						(
           self.batch_size,
           self.last_hidden_size,
           self.image_size // self.output_stride,
           self.image_size // self.output_stride,
       )    ,						)






  def 						snake_case	(					self    ,						UpperCamelCase    ,						UpperCamelCase    ,						UpperCamelCase    ,						UpperCamelCase  ):







   """simple docstring"""
   lowerCamelCase_  				=						self.num_labels
   lowerCamelCase_  				=						MobileNetVaForImageClassification(_lowerCamelCase  )
   model.to(_lowerCamelCase  )
   model.eval()
   lowerCamelCase_  				=						model(_lowerCamelCase    ,						labels=_lowerCamelCase  )
   self.parent.assertEqual(result.logits.shape    ,						(self.batch_size, self.num_labels)  )







  def 						snake_case	(					self  ):







   """simple docstring"""
   lowerCamelCase_  				=						self.prepare_config_and_inputs()
   lowerCamelCase_				,lowerCamelCase_				,lowerCamelCase_				,lowerCamelCase_  				=						config_and_inputs
   lowerCamelCase_  				=						{"pixel_values": pixel_values}
   return config, inputs_dict





@require_torch
class   snake_case					(					lowerCAmelCase__    ,		lowerCAmelCase__    ,		unittest.TestCase    ):





  """simple docstring"""




  _lowerCamelCase						=						(MobileNetVaModel, MobileNetVaForImageClassification) if is_torch_available() else ()
  _lowerCamelCase						=						(
      {"feature-extraction": MobileNetVaModel, "image-classification": MobileNetVaForImageClassification}
      if is_torch_available()
      else {}
  )

  _lowerCamelCase						=						False
  _lowerCamelCase						=						False
  _lowerCamelCase						=						False
  _lowerCamelCase						=						False






  def 						snake_case	(					self  ):







   """simple docstring"""
   lowerCamelCase_  				=						MobileNetVaModelTester(self  )
   lowerCamelCase_  				=						MobileNetVaConfigTester(self    ,						config_class=_lowerCamelCase    ,						has_text_modality=_lowerCamelCase  )






  def 						snake_case	(					self  ):







   """simple docstring"""
   self.config_tester.run_common_tests()






  @unittest.skip(reason="MobileNetV1 does not use inputs_embeds"  )
  def 						snake_case	(					self  ):







   """simple docstring"""
   pass






  @unittest.skip(reason="MobileNetV1 does not support input and output embeddings"  )
  def 						snake_case	(					self  ):







   """simple docstring"""
   pass






  @unittest.skip(reason="MobileNetV1 does not output attentions"  )
  def 						snake_case	(					self  ):







   """simple docstring"""
   pass






  def 						snake_case	(					self  ):







   """simple docstring"""
   lowerCamelCase_				,lowerCamelCase_  				=						self.model_tester.prepare_config_and_inputs_for_common()

   for model_class in self.all_model_classes:
    lowerCamelCase_  				=						model_class(_lowerCamelCase  )
    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]    ,						_lowerCamelCase  )






  def 						snake_case	(					self  ):







   """simple docstring"""
   lowerCamelCase_  				=						self.model_tester.prepare_config_and_inputs()
   self.model_tester.create_and_check_model(*_lowerCamelCase  )






  def 						snake_case	(					self  ):







   """simple docstring"""





   def check_hidden_states_output(UpperCamelCase    ,						UpperCamelCase    ,						UpperCamelCase  ):
    lowerCamelCase_  				=						model_class(_lowerCamelCase  )
    model.to(_lowerCamelCase  )
    model.eval()

    with torch.no_grad():
     lowerCamelCase_  				=						model(**self._prepare_for_class(_lowerCamelCase    ,						_lowerCamelCase  )  )

    lowerCamelCase_  				=						outputs.hidden_states

    lowerCamelCase_  				=						26
    self.assertEqual(len(_lowerCamelCase  )    ,						_lowerCamelCase  )

   lowerCamelCase_				,lowerCamelCase_  				=						self.model_tester.prepare_config_and_inputs_for_common()

   for model_class in self.all_model_classes:
    lowerCamelCase_  				=						True
    check_hidden_states_output(_lowerCamelCase    ,						_lowerCamelCase    ,						_lowerCamelCase  )

    # check that output_hidden_states also work using config
    del inputs_dict["output_hidden_states"]
    lowerCamelCase_  				=						True

    check_hidden_states_output(_lowerCamelCase    ,						_lowerCamelCase    ,						_lowerCamelCase  )






  def 						snake_case	(					self  ):







   """simple docstring"""
   lowerCamelCase_  				=						self.model_tester.prepare_config_and_inputs()
   self.model_tester.create_and_check_for_image_classification(*_lowerCamelCase  )







  @slow
  def 						snake_case	(					self  ):







   """simple docstring"""





   for model_name in MOBILENET_V1_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
    lowerCamelCase_  				=						MobileNetVaModel.from_pretrained(_lowerCamelCase  )
    self.assertIsNotNone(_lowerCamelCase  )





def __snake_case      (   ):
 lowerCamelCase_  				=						Image.open("./tests/fixtures/tests_samples/COCO/000000039769.png"	)
 return image







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





  """simple docstring"""




  @cached_property
  def 						snake_case	(					self  ):







   """simple docstring"""
   return (
       MobileNetVaImageProcessor.from_pretrained("google/mobilenet_v1_1.0_224"  ) if is_vision_available() else None
   )







  @slow
  def 						snake_case	(					self  ):







   """simple docstring"""
   lowerCamelCase_  				=						MobileNetVaForImageClassification.from_pretrained("google/mobilenet_v1_1.0_224"  ).to(_lowerCamelCase  )

   lowerCamelCase_  				=						self.default_image_processor
   lowerCamelCase_  				=						prepare_img()
   lowerCamelCase_  				=						image_processor(images=_lowerCamelCase    ,						return_tensors="pt"  ).to(_lowerCamelCase  )

   # forward pass
   with torch.no_grad():
    lowerCamelCase_  				=						model(**_lowerCamelCase  )

   # verify the logits
   lowerCamelCase_  				=						torch.Size((1, 1001)  )
   self.assertEqual(outputs.logits.shape    ,						_lowerCamelCase  )

   lowerCamelCase_  				=						torch.tensor([-4.1_739, -1.1_233, 3.1_205]  ).to(_lowerCamelCase  )

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







'''simple docstring'''

from dataclasses import dataclass
from typing import Optional, Tuple

import torch
from torch import nn
from transformers import RobertaPreTrainedModel, XLMRobertaConfig, XLMRobertaModel
from transformers.utils import ModelOutput





@dataclass
class   snake_case					(					lowercase    ):





  """simple docstring"""




  _lowerCamelCase						=						None
  _lowerCamelCase						=						None
  _lowerCamelCase						=						None
  _lowerCamelCase						=						None





class   snake_case					(					lowercase    ):





  """simple docstring"""




  def __init__(					self    ,						UpperCamelCase=1    ,						UpperCamelCase=0    ,						UpperCamelCase=2    ,						UpperCamelCase=512    ,						UpperCamelCase="cls"    ,						UpperCamelCase=False    ,						UpperCamelCase=True    ,						**UpperCamelCase    ,						):







   """simple docstring"""
   super().__init__(pad_token_id=UpperCamelCase    ,						bos_token_id=UpperCamelCase    ,						eos_token_id=UpperCamelCase    ,						**UpperCamelCase  )
   lowerCamelCase_  				=						project_dim
   lowerCamelCase_  				=						pooler_fn
   lowerCamelCase_  				=						learn_encoder
   lowerCamelCase_  				=						use_attention_mask





class   snake_case					(					lowercase    ):





  """simple docstring"""




  _lowerCamelCase						=						[r"pooler", r"logit_scale"]
  _lowerCamelCase						=						[r"position_ids", r"predictions.decoder.bias"]
  _lowerCamelCase						=						"roberta"
  _lowerCamelCase						=						RobertaSeriesConfig






  def __init__(					self    ,						UpperCamelCase  ):







   """simple docstring"""
   super().__init__(UpperCamelCase  )
   lowerCamelCase_  				=						XLMRobertaModel(UpperCamelCase  )
   lowerCamelCase_  				=						nn.Linear(config.hidden_size    ,						config.project_dim  )
   lowerCamelCase_  				=						getattr(UpperCamelCase    ,						"has_pre_transformation"    ,						UpperCamelCase  )
   if self.has_pre_transformation:
    lowerCamelCase_  				=						nn.Linear(config.hidden_size    ,						config.project_dim  )
    lowerCamelCase_  				=						nn.LayerNorm(config.hidden_size    ,						eps=config.layer_norm_eps  )
   self.post_init()







  def 						snake_case	(					self    ,						UpperCamelCase = None    ,						UpperCamelCase = None    ,						UpperCamelCase = None    ,						UpperCamelCase = None    ,						UpperCamelCase = None    ,						UpperCamelCase = None    ,						UpperCamelCase = None    ,						UpperCamelCase = None    ,						UpperCamelCase = None    ,						UpperCamelCase = None    ,						UpperCamelCase = None    ,						):







   """simple docstring"""
   lowerCamelCase_  				=						return_dict if return_dict is not None else self.config.use_return_dict

   lowerCamelCase_  				=						self.base_model(
       input_ids=UpperCamelCase    ,						attention_mask=UpperCamelCase    ,						token_type_ids=UpperCamelCase    ,						position_ids=UpperCamelCase    ,						head_mask=UpperCamelCase    ,						inputs_embeds=UpperCamelCase    ,						encoder_hidden_states=UpperCamelCase    ,						encoder_attention_mask=UpperCamelCase    ,						output_attentions=UpperCamelCase    ,						output_hidden_states=True if self.has_pre_transformation else output_hidden_states    ,						return_dict=UpperCamelCase    ,						)

   if self.has_pre_transformation:
    lowerCamelCase_  				=						outputs["hidden_states"][-2]
    lowerCamelCase_  				=						self.pre_LN(UpperCamelCase  )
    lowerCamelCase_  				=						self.transformation_pre(UpperCamelCase  )

    return TransformationModelOutput(
        projection_state=UpperCamelCase    ,						last_hidden_state=outputs.last_hidden_state    ,						hidden_states=outputs.hidden_states    ,						attentions=outputs.attentions    ,						)
   else:
    lowerCamelCase_  				=						self.transformation(outputs.last_hidden_state  )
    return TransformationModelOutput(
        projection_state=UpperCamelCase    ,						last_hidden_state=outputs.last_hidden_state    ,						hidden_states=outputs.hidden_states    ,						attentions=outputs.attentions    ,						)



