



import inspect
import unittest

from transformers import RegNetConfig, 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.regnet.modeling_flax_regnet import FlaxRegNetForImageClassification, FlaxRegNetModel

if is_vision_available():
  from PIL import Image

  from transformers import AutoImageProcessor



class  __lowerCAmelCase     (	unittest.TestCase   ):




 def __init__(						self	,				__UpperCAmelCase	,				__UpperCAmelCase=3	,				__UpperCAmelCase=32	,				__UpperCAmelCase=3	,				__UpperCAmelCase=10	,				__UpperCAmelCase=[10, 20, 30, 40]	,				__UpperCAmelCase=[1, 1, 2, 1]	,				__UpperCAmelCase=True	,				__UpperCAmelCase=True	,				__UpperCAmelCase="relu"	,				__UpperCAmelCase=3	,				__UpperCAmelCase=None	,				):



    '''simple docstring'''
    __lowerCamelCase													=    parent
    __lowerCamelCase													=    batch_size
    __lowerCamelCase													=    image_size
    __lowerCamelCase													=    num_channels
    __lowerCamelCase													=    embeddings_size
    __lowerCamelCase													=    hidden_sizes
    __lowerCamelCase													=    depths
    __lowerCamelCase													=    is_training
    __lowerCamelCase													=    use_labels
    __lowerCamelCase													=    hidden_act
    __lowerCamelCase													=    num_labels
    __lowerCamelCase													=    scope
    __lowerCamelCase													=    len(__UpperCAmelCase				)




 def        lowerCamelCase      (						self				):



    '''simple docstring'''
    __lowerCamelCase													=    floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]				)

    __lowerCamelCase													=    self.get_config()

    return config, pixel_values




 def        lowerCamelCase      (						self				):



    '''simple docstring'''
    return RegNetConfig(
        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        lowerCamelCase      (						self	,				__UpperCAmelCase	,				__UpperCAmelCase				):



    '''simple docstring'''
    __lowerCamelCase													=    FlaxRegNetModel(config=__UpperCAmelCase				)
    __lowerCamelCase													=    model(__UpperCAmelCase				)

    # 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        lowerCamelCase      (						self	,				__UpperCAmelCase	,				__UpperCAmelCase				):



    '''simple docstring'''
    __lowerCamelCase													=    self.num_labels
    __lowerCamelCase													=    FlaxRegNetForImageClassification(config=__UpperCAmelCase				)
    __lowerCamelCase													=    model(__UpperCAmelCase				)
    self.parent.assertEqual(result.logits.shape	,				(self.batch_size, self.num_labels)				)




 def        lowerCamelCase      (						self				):



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



@require_flax
class  __lowerCAmelCase     (	lowerCAmelCase__  ,	unittest.TestCase   ):
 lowerCAmelCase__       					=  (FlaxRegNetModel, FlaxRegNetForImageClassification) if is_flax_available() else ()

 lowerCAmelCase__       					=  False
 lowerCAmelCase__       					=  False
 lowerCAmelCase__       					=  False




 def        lowerCamelCase      (						self				):



    '''simple docstring'''
    __lowerCamelCase													=    FlaxRegNetModelTester(self				)
    __lowerCamelCase													=    ConfigTester(self	,				config_class=__UpperCAmelCase	,				has_text_modality=__UpperCAmelCase				)




 def        lowerCamelCase      (						self				):



    '''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        lowerCamelCase      (						self				):



    '''simple docstring'''
    return




 def        lowerCamelCase      (						self				):



    '''simple docstring'''
    __lowerCamelCase													=    self.model_tester.prepare_config_and_inputs()
    self.model_tester.create_and_check_model(*__UpperCAmelCase				)




 def        lowerCamelCase      (						self				):



    '''simple docstring'''
    __lowerCamelCase													=    self.model_tester.prepare_config_and_inputs()
    self.model_tester.create_and_check_for_image_classification(*__UpperCAmelCase				)




 @unittest.skip(reason='''RegNet does not use inputs_embeds'''				)
 def        lowerCamelCase      (						self				):



    '''simple docstring'''
    pass




 @unittest.skip(reason='''RegNet does not support input and output embeddings'''				)
 def        lowerCamelCase      (						self				):



    '''simple docstring'''
    pass




 def        lowerCamelCase      (						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(__UpperCAmelCase				)
       __lowerCamelCase													=    inspect.signature(model.__call__				)
       # signature.parameters is an OrderedDict => so arg_names order is deterministic
       __lowerCamelCase													=    [*signature.parameters.keys()]

       __lowerCamelCase													=    ['''pixel_values''']
       self.assertListEqual(arg_names[:1]	,				__UpperCAmelCase				)




 def        lowerCamelCase      (						self				):



    '''simple docstring'''
    def check_hidden_states_output(__UpperCAmelCase	,				__UpperCAmelCase	,				__UpperCAmelCase				):
       __lowerCamelCase													=    model_class(__UpperCAmelCase				)

       __lowerCamelCase													=    model(**self._prepare_for_class(__UpperCAmelCase	,				__UpperCAmelCase				)				)

       __lowerCamelCase													=    outputs.encoder_hidden_states if config.is_encoder_decoder else outputs.hidden_states

       __lowerCamelCase													=    self.model_tester.num_stages
       self.assertEqual(len(__UpperCAmelCase				)	,				expected_num_stages + 1				)

    __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(__UpperCAmelCase	,				__UpperCAmelCase	,				__UpperCAmelCase				)

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

       check_hidden_states_output(__UpperCAmelCase	,				__UpperCAmelCase	,				__UpperCAmelCase				)




 def        lowerCamelCase      (						self				):



    '''simple docstring'''
    __lowerCamelCase							,__lowerCamelCase													=    self.model_tester.prepare_config_and_inputs_for_common()

    for model_class in self.all_model_classes:
       with self.subTest(model_class.__name__				):
          __lowerCamelCase													=    self._prepare_for_class(__UpperCAmelCase	,				__UpperCAmelCase				)
          __lowerCamelCase													=    model_class(__UpperCAmelCase				)

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

          with self.subTest('''JIT Enabled'''				):
             __lowerCamelCase													=    model_jitted(**__UpperCAmelCase				).to_tuple()

          with self.subTest('''JIT Disabled'''				):
             with jax.disable_jit():
                __lowerCamelCase													=    model_jitted(**__UpperCAmelCase				).to_tuple()

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





def 		a__			(		):
   __lowerCamelCase													=    Image.open('''./tests/fixtures/tests_samples/COCO/000000039769.png'''							)
   return image





@require_flax
class  __lowerCAmelCase     (	unittest.TestCase   ):




 @cached_property
 def        lowerCamelCase      (						self				):



    '''simple docstring'''
    return AutoImageProcessor.from_pretrained('''facebook/regnet-y-040'''				) if is_vision_available() else None




 @slow
 def        lowerCamelCase      (						self				):



    '''simple docstring'''
    __lowerCamelCase													=    FlaxRegNetForImageClassification.from_pretrained('''facebook/regnet-y-040'''				)

    __lowerCamelCase													=    self.default_image_processor
    __lowerCamelCase													=    prepare_img()
    __lowerCamelCase													=    image_processor(images=__UpperCAmelCase	,				return_tensors='''np'''				)

    __lowerCamelCase													=    model(**__UpperCAmelCase				)

    # verify the logits
    __lowerCamelCase													=    (1, 1000)
    self.assertEqual(outputs.logits.shape	,				__UpperCAmelCase				)

    __lowerCamelCase													=    jnp.array([-0.4_180, -1.5_051, -3.4_836]				)

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











from ...configuration_utils import PretrainedConfig
from ...utils import logging


a_			=					logging.get_logger(__name__)

a_			=					{
    """unc-nlp/lxmert-base-uncased""": """https://huggingface.co/unc-nlp/lxmert-base-uncased/resolve/main/config.json""",
}



class  __lowerCAmelCase     (	lowerCAmelCase__   ):
 lowerCAmelCase__       					=  """lxmert"""
 lowerCAmelCase__       					=  {}




 def __init__(						self	,				__UpperCAmelCase=30522	,				__UpperCAmelCase=768	,				__UpperCAmelCase=12	,				__UpperCAmelCase=9500	,				__UpperCAmelCase=1600	,				__UpperCAmelCase=400	,				__UpperCAmelCase=3072	,				__UpperCAmelCase="gelu"	,				__UpperCAmelCase=0.1	,				__UpperCAmelCase=0.1	,				__UpperCAmelCase=512	,				__UpperCAmelCase=2	,				__UpperCAmelCase=0.02	,				__UpperCAmelCase=1E-1_2	,				__UpperCAmelCase=9	,				__UpperCAmelCase=5	,				__UpperCAmelCase=5	,				__UpperCAmelCase=2048	,				__UpperCAmelCase=4	,				__UpperCAmelCase=6.67	,				__UpperCAmelCase=True	,				__UpperCAmelCase=True	,				__UpperCAmelCase=True	,				__UpperCAmelCase=True	,				__UpperCAmelCase=True	,				__UpperCAmelCase=True	,				__UpperCAmelCase=True	,				**__UpperCAmelCase	,				):



    '''simple docstring'''
    __lowerCamelCase													=    vocab_size
    __lowerCamelCase													=    hidden_size
    __lowerCamelCase													=    num_attention_heads
    __lowerCamelCase													=    hidden_act
    __lowerCamelCase													=    intermediate_size
    __lowerCamelCase													=    hidden_dropout_prob
    __lowerCamelCase													=    attention_probs_dropout_prob
    __lowerCamelCase													=    max_position_embeddings
    __lowerCamelCase													=    type_vocab_size
    __lowerCamelCase													=    initializer_range
    __lowerCamelCase													=    layer_norm_eps
    __lowerCamelCase													=    num_qa_labels
    __lowerCamelCase													=    num_object_labels
    __lowerCamelCase													=    num_attr_labels
    __lowerCamelCase													=    l_layers
    __lowerCamelCase													=    x_layers
    __lowerCamelCase													=    r_layers
    __lowerCamelCase													=    visual_feat_dim
    __lowerCamelCase													=    visual_pos_dim
    __lowerCamelCase													=    visual_loss_normalizer
    __lowerCamelCase													=    task_matched
    __lowerCamelCase													=    task_mask_lm
    __lowerCamelCase													=    task_obj_predict
    __lowerCamelCase													=    task_qa
    __lowerCamelCase													=    visual_obj_loss
    __lowerCamelCase													=    visual_attr_loss
    __lowerCamelCase													=    visual_feat_loss
    __lowerCamelCase													=    {'''vision''': r_layers, '''cross_encoder''': x_layers, '''language''': l_layers}
    super().__init__(**__UpperCAmelCase				)






