





import inspect
import unittest

from transformers import ResNetConfig
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_backbone_common import BackboneTesterMixin
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 torch import nn

      from transformers import ResNetBackbone, ResNetForImageClassification, ResNetModel
      from transformers.models.resnet.modeling_resnet import RESNET_PRETRAINED_MODEL_ARCHIVE_LIST


if is_vision_available():
      from PIL import Image

      from transformers import AutoImageProcessor



class 				_lowerCAmelCase			:


  '''simple docstring'''







  def __init__(					self					:    Optional[Any]					, UpperCamelCase					:    List[str]					, UpperCamelCase					:    List[Any]=3					, UpperCamelCase					:    Optional[int]=32					, UpperCamelCase					:    Dict=3					, UpperCamelCase					:    Tuple=10					, UpperCamelCase					:    List[str]=[10, 20, 30, 40]					, UpperCamelCase					:    Optional[Any]=[1, 1, 2, 1]					, UpperCamelCase					:    Any=True					, UpperCamelCase					:    int=True					, UpperCamelCase					:    Dict="relu"					, UpperCamelCase					:    Optional[Any]=3					, UpperCamelCase					:    Any=None					, UpperCamelCase					:    Optional[Any]=["stage2", "stage3", "stage4"]					, UpperCamelCase					:    Tuple=[2, 3, 4]					, ):


    '''simple docstring'''





    _snake_case      : Optional[int]														=		parent
    _snake_case      : Dict														=		batch_size
    _snake_case      : int														=		image_size
    _snake_case      : Any														=		num_channels
    _snake_case      : str														=		embeddings_size
    _snake_case      : Union[str, Any]														=		hidden_sizes
    _snake_case      : List[Any]														=		depths
    _snake_case      : Optional[Any]														=		is_training
    _snake_case      : Any														=		use_labels
    _snake_case      : Tuple														=		hidden_act
    _snake_case      : List[str]														=		num_labels
    _snake_case      : Union[str, Any]														=		scope
    _snake_case      : Tuple														=		len(UpperCamelCase							)
    _snake_case      : List[str]														=		out_features
    _snake_case      : Dict														=		out_indices







  def UpperCamelCase_       (					self					:    Union[str, Any]							):


    '''simple docstring'''





    _snake_case      : List[Any]														=		floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]							)

    _snake_case      : List[str]														=		None
    if self.use_labels:
      _snake_case      : Union[str, Any]														=		ids_tensor([self.batch_size]					, self.num_labels							)

    _snake_case      : List[str]														=		self.get_config()

    return config, pixel_values, labels







  def UpperCamelCase_       (					self					:    Tuple							):


    '''simple docstring'''





    return ResNetConfig(
        num_channels=self.num_channels					, embeddings_size=self.embeddings_size					, hidden_sizes=self.hidden_sizes					, depths=self.depths					, hidden_act=self.hidden_act					, num_labels=self.num_labels					, out_features=self.out_features					, out_indices=self.out_indices					, )







  def UpperCamelCase_       (					self					:    Optional[int]					, UpperCamelCase					:    str					, UpperCamelCase					:    Any					, UpperCamelCase					:    Dict							):


    '''simple docstring'''





    _snake_case      : Dict														=		ResNetModel(config=UpperCamelCase							)
    model.to(UpperCamelCase							)
    model.eval()
    _snake_case      : int														=		model(UpperCamelCase							)
    # expected last hidden states: B, C, H // 32, W // 32
    self.parent.assertEqual(
        result.last_hidden_state.shape					, (self.batch_size, self.hidden_sizes[-1], self.image_size // 32, self.image_size // 32)					, )







  def UpperCamelCase_       (					self					:    Union[str, Any]					, UpperCamelCase					:    Any					, UpperCamelCase					:    Optional[int]					, UpperCamelCase					:    List[str]							):


    '''simple docstring'''





    _snake_case      : int														=		self.num_labels
    _snake_case      : Dict														=		ResNetForImageClassification(UpperCamelCase							)
    model.to(UpperCamelCase							)
    model.eval()
    _snake_case      : str														=		model(UpperCamelCase					, labels=UpperCamelCase							)
    self.parent.assertEqual(result.logits.shape					, (self.batch_size, self.num_labels)							)







  def UpperCamelCase_       (					self					:    List[str]					, UpperCamelCase					:    Tuple					, UpperCamelCase					:    Any					, UpperCamelCase					:    Optional[Any]							):


    '''simple docstring'''





    _snake_case      : List[str]														=		ResNetBackbone(config=UpperCamelCase							)
    model.to(UpperCamelCase							)
    model.eval()
    _snake_case      : int														=		model(UpperCamelCase							)

    # verify feature maps
    self.parent.assertEqual(len(result.feature_maps							)					, len(config.out_features							)							)
    self.parent.assertListEqual(list(result.feature_maps[0].shape							)					, [self.batch_size, self.hidden_sizes[1], 4, 4]							)

    # verify channels
    self.parent.assertEqual(len(model.channels							)					, len(config.out_features							)							)
    self.parent.assertListEqual(model.channels					, config.hidden_sizes[1:]							)

    # verify backbone works with out_features=None
    _snake_case      : Tuple														=		None
    _snake_case      : List[str]														=		ResNetBackbone(config=UpperCamelCase							)
    model.to(UpperCamelCase							)
    model.eval()
    _snake_case      : int														=		model(UpperCamelCase							)

    # verify feature maps
    self.parent.assertEqual(len(result.feature_maps							)					, 1							)
    self.parent.assertListEqual(list(result.feature_maps[0].shape							)					, [self.batch_size, self.hidden_sizes[-1], 1, 1]							)

    # verify channels
    self.parent.assertEqual(len(model.channels							)					, 1							)
    self.parent.assertListEqual(model.channels					, [config.hidden_sizes[-1]]							)







  def UpperCamelCase_       (					self					:    Dict							):


    '''simple docstring'''





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




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


  '''simple docstring'''
  a_							:				List[str]					    =(
      (
          ResNetModel,
          ResNetForImageClassification,
          ResNetBackbone,
      )
      if is_torch_available()
      else ()
  )
  a_							:				str					    =(
      {"""feature-extraction""": ResNetModel, """image-classification""": ResNetForImageClassification}
      if is_torch_available()
      else {}
  )

  a_							:				List[Any]					    =True
  a_							:				Union[str, Any]					    =False
  a_							:				Dict					    =False
  a_							:				Dict					    =False
  a_							:				List[str]					    =False







  def UpperCamelCase_       (					self					:    int							):


    '''simple docstring'''





    _snake_case      : Dict														=		ResNetModelTester(self							)
    _snake_case      : int														=		ConfigTester(self					, config_class=UpperCamelCase					, has_text_modality=UpperCamelCase							)







  def UpperCamelCase_       (					self					:    Tuple							):


    '''simple docstring'''





    self.create_and_test_config_common_properties()
    self.config_tester.create_and_test_config_to_json_string()
    self.config_tester.create_and_test_config_to_json_file()
    self.config_tester.create_and_test_config_from_and_save_pretrained()
    self.config_tester.create_and_test_config_with_num_labels()
    self.config_tester.check_config_can_be_init_without_params()
    self.config_tester.check_config_arguments_init()







  def UpperCamelCase_       (					self					:    int							):


    '''simple docstring'''





    return







  @unittest.skip(reason='ResNet does not use inputs_embeds'							)
  def UpperCamelCase_       (					self					:    int							):


    '''simple docstring'''





    pass







  @unittest.skip(reason='ResNet does not support input and output embeddings'							)
  def UpperCamelCase_       (					self					:    Tuple							):


    '''simple docstring'''





    pass







  def UpperCamelCase_       (					self					:    Optional[Any]							):


    '''simple docstring'''





    _snake_case							,    _snake_case      : List[str]														=		self.model_tester.prepare_config_and_inputs_for_common()

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

      _snake_case      : Dict														=		['pixel_values']
      self.assertListEqual(arg_names[:1]					, UpperCamelCase							)







  def UpperCamelCase_       (					self					:    Any							):


    '''simple docstring'''





    _snake_case      : Tuple														=		self.model_tester.prepare_config_and_inputs()
    self.model_tester.create_and_check_model(*UpperCamelCase							)







  def UpperCamelCase_       (					self					:    int							):


    '''simple docstring'''





    _snake_case      : Dict														=		self.model_tester.prepare_config_and_inputs()
    self.model_tester.create_and_check_backbone(*UpperCamelCase							)







  def UpperCamelCase_       (					self					:    Optional[Any]							):


    '''simple docstring'''





    _snake_case							,    _snake_case      : str														=		self.model_tester.prepare_config_and_inputs_for_common()

    for model_class in self.all_model_classes:
      _snake_case      : Any														=		model_class(config=UpperCamelCase							)
      for name, module in model.named_modules():
        if isinstance(UpperCamelCase					, (nn.BatchNormad, nn.GroupNorm)							):
          self.assertTrue(
              torch.all(module.weight == 1							)					, msg=f"""Parameter {name} of model {model_class} seems not properly initialized"""					, )
          self.assertTrue(
              torch.all(module.bias == 0							)					, msg=f"""Parameter {name} of model {model_class} seems not properly initialized"""					, )







  def UpperCamelCase_       (					self					:    List[str]							):


    '''simple docstring'''





    def check_hidden_states_output(UpperCamelCase					:    Optional[int]					, UpperCamelCase					:    Tuple					, UpperCamelCase					:    Optional[int]							):
      _snake_case      : List[Any]														=		model_class(UpperCamelCase							)
      model.to(UpperCamelCase							)
      model.eval()

      with torch.no_grad():
        _snake_case      : int														=		model(**self._prepare_for_class(UpperCamelCase					, UpperCamelCase							)							)

      _snake_case      : int														=		outputs.encoder_hidden_states if config.is_encoder_decoder else outputs.hidden_states

      _snake_case      : Any														=		self.model_tester.num_stages
      self.assertEqual(len(UpperCamelCase							)					, expected_num_stages + 1							)

      # ResNet's feature maps are of shape (batch_size, num_channels, height, width)
      self.assertListEqual(
          list(hidden_states[0].shape[-2:]							)					, [self.model_tester.image_size // 4, self.model_tester.image_size // 4]					, )

    _snake_case							,    _snake_case      : Any														=		self.model_tester.prepare_config_and_inputs_for_common()
    _snake_case      : List[str]														=		['basic', 'bottleneck']
    for model_class in self.all_model_classes:
      for layer_type in layers_type:
        _snake_case      : List[str]														=		layer_type
        _snake_case      : List[Any]														=		True
        check_hidden_states_output(UpperCamelCase					, UpperCamelCase					, UpperCamelCase							)

        # check that output_hidden_states also work using config
        del inputs_dict["output_hidden_states"]
        _snake_case      : str														=		True

        check_hidden_states_output(UpperCamelCase					, UpperCamelCase					, UpperCamelCase							)







  @unittest.skip(reason='ResNet does not use feedforward chunking'							)
  def UpperCamelCase_       (					self					:    int							):


    '''simple docstring'''





    pass







  def UpperCamelCase_       (					self					:    Dict							):


    '''simple docstring'''





    _snake_case      : Optional[Any]														=		self.model_tester.prepare_config_and_inputs()
    self.model_tester.create_and_check_for_image_classification(*UpperCamelCase							)







  @slow
  def UpperCamelCase_       (					self					:    Optional[Any]							):


    '''simple docstring'''





    for model_name in RESNET_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
      _snake_case      : Dict														=		ResNetModel.from_pretrained(UpperCamelCase							)
      self.assertIsNotNone(UpperCamelCase							)




def 			lowerCamelCase_		(					)->						Tuple:
  _snake_case      : 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 UpperCamelCase_       (					self					:    Union[str, Any]							):


    '''simple docstring'''





    return (
        AutoImageProcessor.from_pretrained(RESNET_PRETRAINED_MODEL_ARCHIVE_LIST[0]							)
        if is_vision_available()
        else None
    )







  @slow
  def UpperCamelCase_       (					self					:    Optional[Any]							):


    '''simple docstring'''





    _snake_case      : str														=		ResNetForImageClassification.from_pretrained(RESNET_PRETRAINED_MODEL_ARCHIVE_LIST[0]							).to(UpperCamelCase							)

    _snake_case      : Optional[Any]														=		self.default_image_processor
    _snake_case      : Optional[int]														=		prepare_img()
    _snake_case      : List[str]														=		image_processor(images=UpperCamelCase					, return_tensors='pt'							).to(UpperCamelCase							)

    # forward pass
    with torch.no_grad():
      _snake_case      : Tuple														=		model(**UpperCamelCase							)

    # verify the logits
    _snake_case      : Union[str, Any]														=		torch.Size((1, 10_00)							)
    self.assertEqual(outputs.logits.shape					, UpperCamelCase							)

    _snake_case      : Any														=		torch.tensor([-11.10_69, -9.78_77, -8.37_77]							).to(UpperCamelCase							)

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




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


  '''simple docstring'''
  a_							:				Dict					    =(ResNetBackbone,) if is_torch_available() else ()
  a_							:				List[Any]					    =False
  a_							:				Union[str, Any]					    =ResNetConfig







  def UpperCamelCase_       (					self					:    int							):


    '''simple docstring'''





    _snake_case      : Tuple														=		ResNetModelTester(self							)











from collections import OrderedDict
from typing import Mapping

from ...configuration_utils import PretrainedConfig
from ...onnx import OnnxConfig
from ...utils import logging


lowerCAmelCase_        =			logging.get_logger(__name__)

lowerCAmelCase_        =			{
    """google/mobilebert-uncased""": """https://huggingface.co/google/mobilebert-uncased/resolve/main/config.json"""
}



class 				_lowerCAmelCase			(	UpperCAmelCase_							):


  '''simple docstring'''
  a_							:				List[Any]					    =MOBILEBERT_PRETRAINED_CONFIG_ARCHIVE_MAP
  a_							:				Union[str, Any]					    ="""mobilebert"""







  def __init__(					self					:    int					, UpperCamelCase					:    int=3_05_22					, UpperCamelCase					:    Any=5_12					, UpperCamelCase					:    Any=24					, UpperCamelCase					:    List[str]=4					, UpperCamelCase					:    Any=5_12					, UpperCamelCase					:    str="relu"					, UpperCamelCase					:    Dict=0.0					, UpperCamelCase					:    int=0.1					, UpperCamelCase					:    Optional[Any]=5_12					, UpperCamelCase					:    str=2					, UpperCamelCase					:    Tuple=0.02					, UpperCamelCase					:    Union[str, Any]=1e-1_2					, UpperCamelCase					:    Optional[Any]=0					, UpperCamelCase					:    Union[str, Any]=1_28					, UpperCamelCase					:    Tuple=True					, UpperCamelCase					:    int=True					, UpperCamelCase					:    List[Any]=1_28					, UpperCamelCase					:    Dict=False					, UpperCamelCase					:    List[str]=True					, UpperCamelCase					:    Optional[Any]=4					, UpperCamelCase					:    int="no_norm"					, UpperCamelCase					:    Tuple=True					, UpperCamelCase					:    str=None					, **UpperCamelCase					:    str					, ):


    '''simple docstring'''





    super().__init__(pad_token_id=UpperCamelCase					, **UpperCamelCase							)

    _snake_case      : Union[str, Any]														=		vocab_size
    _snake_case      : Any														=		hidden_size
    _snake_case      : Tuple														=		num_hidden_layers
    _snake_case      : Any														=		num_attention_heads
    _snake_case      : Optional[Any]														=		hidden_act
    _snake_case      : List[Any]														=		intermediate_size
    _snake_case      : List[Any]														=		hidden_dropout_prob
    _snake_case      : Optional[int]														=		attention_probs_dropout_prob
    _snake_case      : int														=		max_position_embeddings
    _snake_case      : Optional[int]														=		type_vocab_size
    _snake_case      : Any														=		initializer_range
    _snake_case      : Optional[int]														=		layer_norm_eps
    _snake_case      : Tuple														=		embedding_size
    _snake_case      : int														=		trigram_input
    _snake_case      : Optional[int]														=		use_bottleneck
    _snake_case      : List[Any]														=		intra_bottleneck_size
    _snake_case      : Any														=		use_bottleneck_attention
    _snake_case      : str														=		key_query_shared_bottleneck
    _snake_case      : List[str]														=		num_feedforward_networks
    _snake_case      : int														=		normalization_type
    _snake_case      : str														=		classifier_activation

    if self.use_bottleneck:
      _snake_case      : Any														=		intra_bottleneck_size
    else:
      _snake_case      : Union[str, Any]														=		hidden_size

    _snake_case      : Dict														=		classifier_dropout




class 				_lowerCAmelCase			(	UpperCAmelCase_							):


  '''simple docstring'''







  @property
  def UpperCamelCase_       (					self					:    List[Any]							):


    '''simple docstring'''





    if self.task == "multiple-choice":
      _snake_case      : str														=		{0: 'batch', 1: 'choice', 2: 'sequence'}
    else:
      _snake_case      : Dict														=		{0: 'batch', 1: 'sequence'}
    return OrderedDict(
        [
            ('input_ids', dynamic_axis),
            ('attention_mask', dynamic_axis),
            ('token_type_ids', dynamic_axis),
        ]							)




