






'''simple docstring'''


import inspect
import unittest

from transformers import ResNetConfig, is_flax_available
from transformers.testing_utils import require_flax, slow
from transformers.utils import cached_property, is_vision_available

from ...test_configuration_common import ConfigTester
from ...test_modeling_flax_common import FlaxModelTesterMixin, floats_tensor


if is_flax_available():
  import jax
  import jax.numpy as jnp

  from transformers.models.resnet.modeling_flax_resnet import FlaxResNetForImageClassification, FlaxResNetModel

if is_vision_available():
  from PIL import Image

  from transformers import AutoImageProcessor




class 							lowerCAmelCase					(							unittest.TestCase   ):







   """simple docstring"""







   def __init__( self   ,   _A   ,   _A=3   ,   _A=32   ,   _A=3   ,   _A=10   ,   _A=[10, 20, 30, 40]   ,   _A=[1, 1, 2, 1]   ,   _A=True   ,   _A=True   ,   _A="relu"   ,   _A=3   ,   _A=None   ,   )    ->   Dict:
    __a    :	int									=						parent
    __a    :	List[Any]									=						batch_size
    __a    :	Dict									=						image_size
    __a    :	Any									=						num_channels
    __a    :	Dict									=						embeddings_size
    __a    :	Union[str, Any]									=						hidden_sizes
    __a    :	Dict									=						depths
    __a    :	Optional[int]									=						is_training
    __a    :	Tuple									=						use_labels
    __a    :	Union[str, Any]									=						hidden_act
    __a    :	Any									=						num_labels
    __a    :	int									=						scope
    __a    :	Any									=						len(_lowerCAmelCase			)



   def    __magic_name__    ( self			)    ->   Optional[int]:
    __a    :	Tuple									=						floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]			)

    __a    :	Tuple									=						self.get_config()

    return config, pixel_values



   def    __magic_name__    ( self			)    ->   str:
    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   ,   image_size=self.image_size   ,   )



   def    __magic_name__    ( self   ,   _A   ,   _A			)    ->   List[str]:
    __a    :	List[Any]									=						FlaxResNetModel(config=_lowerCAmelCase			)
    __a    :	Dict									=						model(_lowerCAmelCase			)

    # Output shape (b, c, h, w)
    self.parent.assertEqual(
        result.last_hidden_state.shape   ,   (self.batch_size, self.hidden_sizes[-1], self.image_size // 32, self.image_size // 32)   ,   )



   def    __magic_name__    ( self   ,   _A   ,   _A			)    ->   Optional[Any]:
    __a    :	Optional[Any]									=						self.num_labels
    __a    :	Optional[Any]									=						FlaxResNetForImageClassification(config=_lowerCAmelCase			)
    __a    :	Optional[Any]									=						model(_lowerCAmelCase			)
    self.parent.assertEqual(result.logits.shape   ,   (self.batch_size, self.num_labels)			)



   def    __magic_name__    ( self			)    ->   Optional[Any]:
    __a    :	str									=						self.prepare_config_and_inputs()
    __a    :	Union[str, Any]									=						config_and_inputs
    __a    :	List[str]									=						{"""pixel_values""": pixel_values}
    return config, inputs_dict




@require_flax
class 							lowerCAmelCase					(							__lowerCamelCase							,      unittest.TestCase   ):







   """simple docstring"""







   _A						=   (FlaxResNetModel, FlaxResNetForImageClassification) if is_flax_available() else ()

   _A						=   False
   _A						=   False
   _A						=   False



   def    __magic_name__    ( self			)    ->   List[Any]:
    __a    :	Optional[Any]									=						FlaxResNetModelTester(self			)
    __a    :	Optional[int]									=						ConfigTester(self   ,   config_class=_lowerCAmelCase   ,   has_text_modality=_lowerCAmelCase			)



   def    __magic_name__    ( self			)    ->   int:
    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    __magic_name__    ( self			)    ->   Any:
    return



   def    __magic_name__    ( self			)    ->   int:
    __a    :	Optional[Any]									=						self.model_tester.prepare_config_and_inputs()
    self.model_tester.create_and_check_model(*_lowerCAmelCase			)



   def    __magic_name__    ( self			)    ->   str:
    __a    :	List[str]									=						self.model_tester.prepare_config_and_inputs()
    self.model_tester.create_and_check_for_image_classification(*_lowerCAmelCase			)



   @unittest.skip(reason='ResNet does not use inputs_embeds'			)
   def    __magic_name__    ( self			)    ->   Optional[int]:
    pass



   @unittest.skip(reason='ResNet does not support input and output embeddings'			)
   def    __magic_name__    ( self			)    ->   Dict:
    pass



   def    __magic_name__    ( self			)    ->   Dict:
    __a    :	List[Any]									=						self.model_tester.prepare_config_and_inputs_for_common()

    for model_class in self.all_model_classes:
     __a    :	Any									=						model_class(_lowerCAmelCase			)
     __a    :	str									=						inspect.signature(model.__call__			)
     # signature.parameters is an OrderedDict => so arg_names order is deterministic
     __a    :	Tuple									=						[*signature.parameters.keys()]

     __a    :	Optional[Any]									=						["""pixel_values"""]
     self.assertListEqual(arg_names[:1]   ,   _lowerCAmelCase			)



   def    __magic_name__    ( self			)    ->   Optional[int]:
    def check_hidden_states_output(_A   ,   _A   ,   _A			):
     __a    :	Union[str, Any]									=						model_class(_lowerCAmelCase			)

     __a    :	List[str]									=						model(**self._prepare_for_class(_lowerCAmelCase   ,   _lowerCAmelCase			)			)

     __a    :	str									=						outputs.encoder_hidden_states if config.is_encoder_decoder else outputs.hidden_states

     __a    :	str									=						self.model_tester.num_stages
     self.assertEqual(len(_lowerCAmelCase			)   ,   expected_num_stages + 1			)



   @unittest.skip(reason='ResNet does not use feedforward chunking'			)
   def    __magic_name__    ( self			)    ->   Dict:
    pass



   def    __magic_name__    ( self			)    ->   Dict:
    __a    :	Tuple									=						self.model_tester.prepare_config_and_inputs_for_common()

    for model_class in self.all_model_classes:
     with self.subTest(model_class.__name__			):
      __a    :	List[Any]									=						self._prepare_for_class(_lowerCAmelCase   ,   _lowerCAmelCase			)
      __a    :	Union[str, Any]									=						model_class(_lowerCAmelCase			)

      @jax.jit
      def model_jitted(_A   ,   **_A			):
       return model(pixel_values=_lowerCAmelCase   ,   **_lowerCAmelCase			)

      with self.subTest('JIT Enabled'			):
       __a    :	Union[str, Any]									=						model_jitted(**_lowerCAmelCase			).to_tuple()

      with self.subTest('JIT Disabled'			):
       with jax.disable_jit():
        __a    :	Any									=						model_jitted(**_lowerCAmelCase			).to_tuple()

      self.assertEqual(len(_lowerCAmelCase			)   ,   len(_lowerCAmelCase			)			)
      for jitted_output, output in zip(_lowerCAmelCase   ,   _lowerCAmelCase			):
       self.assertEqual(jitted_output.shape   ,   output.shape			)






def   lowerCAmelCase__			(					):
 __a    :	Tuple									=						Image.open('./tests/fixtures/tests_samples/COCO/000000039769.png'		)
 return image




@require_flax
class 							lowerCAmelCase					(							unittest.TestCase   ):







   """simple docstring"""







   @cached_property
   def    __magic_name__    ( self			)    ->   int:
    return AutoImageProcessor.from_pretrained('microsoft/resnet-50'			) if is_vision_available() else None



   @slow
   def    __magic_name__    ( self			)    ->   Tuple:
    __a    :	Optional[Any]									=						FlaxResNetForImageClassification.from_pretrained('microsoft/resnet-50'			)

    __a    :	Tuple									=						self.default_image_processor
    __a    :	Any									=						prepare_img()
    __a    :	Union[str, Any]									=						image_processor(images=_lowerCAmelCase   ,   return_tensors='np'			)

    __a    :	Any									=						model(**_lowerCAmelCase			)

    # verify the logits
    __a    :	Tuple									=						(1, 1000)
    self.assertEqual(outputs.logits.shape   ,   _lowerCAmelCase			)

    __a    :	Tuple									=						jnp.array([-11.1069, -9.7_877, -8.3_777]			)

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












'''simple docstring'''


from collections import OrderedDict
from typing import Mapping

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


SCREAMING_SNAKE_CASE_				      =							logging.get_logger(__name__)

SCREAMING_SNAKE_CASE_				      =							{
    "squeezebert/squeezebert-uncased": (
        "https://huggingface.co/squeezebert/squeezebert-uncased/resolve/main/config.json"
    ),
    "squeezebert/squeezebert-mnli": "https://huggingface.co/squeezebert/squeezebert-mnli/resolve/main/config.json",
    "squeezebert/squeezebert-mnli-headless": (
        "https://huggingface.co/squeezebert/squeezebert-mnli-headless/resolve/main/config.json"
    ),
}




class 							lowerCAmelCase					(							lowerCamelCase__   ):







   """simple docstring"""







   _A						=   SQUEEZEBERT_PRETRAINED_CONFIG_ARCHIVE_MAP
   _A						=   'squeezebert'



   def __init__( self   ,   _A=30522   ,   _A=768   ,   _A=12   ,   _A=12   ,   _A=3072   ,   _A="gelu"   ,   _A=0.1   ,   _A=0.1   ,   _A=512   ,   _A=2   ,   _A=0.02   ,   _A=1E-1_2   ,   _A=0   ,   _A=768   ,   _A=4   ,   _A=4   ,   _A=4   ,   _A=1   ,   _A=4   ,   _A=4   ,   **_A   ,   )    ->   List[Any]:
    super().__init__(pad_token_id=_A   ,   **_A			)

    __a    :	Optional[int]									=						vocab_size
    __a    :	Optional[int]									=						hidden_size
    __a    :	int									=						num_hidden_layers
    __a    :	int									=						num_attention_heads
    __a    :	Tuple									=						hidden_act
    __a    :	Optional[Any]									=						intermediate_size
    __a    :	Any									=						hidden_dropout_prob
    __a    :	List[Any]									=						attention_probs_dropout_prob
    __a    :	List[str]									=						max_position_embeddings
    __a    :	Tuple									=						type_vocab_size
    __a    :	Tuple									=						initializer_range
    __a    :	List[str]									=						layer_norm_eps
    __a    :	str									=						embedding_size
    __a    :	Any									=						q_groups
    __a    :	Any									=						k_groups
    __a    :	Union[str, Any]									=						v_groups
    __a    :	List[str]									=						post_attention_groups
    __a    :	Dict									=						intermediate_groups
    __a    :	Optional[Any]									=						output_groups




class 							lowerCAmelCase					(							lowerCamelCase__   ):







   """simple docstring"""







   @property
   def    __magic_name__    ( self			)    ->   Mapping[str, Mapping[int, str]]:
    if self.task == "multiple-choice":
     __a    :	Tuple									=						{0: 'batch', 1: 'choice', 2: 'sequence'}
    else:
     __a    :	List[Any]									=						{0: 'batch', 1: 'sequence'}
    return OrderedDict(
        [
            ('input_ids', dynamic_axis),
            ('attention_mask', dynamic_axis),
            ('token_type_ids', dynamic_axis),
        ]			)




