
import inspect
import unittest

import numpy as np

from transformers import ViTConfig, is_flax_available
from transformers.testing_utils import require_flax, slow

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


if is_flax_available():
    import jax

    from transformers.models.vit.modeling_flax_vit import FlaxViTForImageClassification, FlaxViTModel




class      __A(    unittest.TestCase			):

  """simple docstring"""







  def __init__(self	,      SCREAMING_SNAKE_CASE_	,      SCREAMING_SNAKE_CASE_=13	,      SCREAMING_SNAKE_CASE_=30	,      SCREAMING_SNAKE_CASE_=2	,      SCREAMING_SNAKE_CASE_=3	,      SCREAMING_SNAKE_CASE_=True	,      SCREAMING_SNAKE_CASE_=True	,      SCREAMING_SNAKE_CASE_=32	,      SCREAMING_SNAKE_CASE_=5	,      SCREAMING_SNAKE_CASE_=4	,      SCREAMING_SNAKE_CASE_=37	,      SCREAMING_SNAKE_CASE_="gelu"	,      SCREAMING_SNAKE_CASE_=0.1	,      SCREAMING_SNAKE_CASE_=0.1	,      SCREAMING_SNAKE_CASE_=10	,      SCREAMING_SNAKE_CASE_=0.02	,      ):
       UpperCamelCase__				=    parent
       UpperCamelCase__				=    batch_size
       UpperCamelCase__				=    image_size
       UpperCamelCase__				=    patch_size
       UpperCamelCase__				=    num_channels
       UpperCamelCase__				=    is_training
       UpperCamelCase__				=    use_labels
       UpperCamelCase__				=    hidden_size
       UpperCamelCase__				=    num_hidden_layers
       UpperCamelCase__				=    num_attention_heads
       UpperCamelCase__				=    intermediate_size
       UpperCamelCase__				=    hidden_act
       UpperCamelCase__				=    hidden_dropout_prob
       UpperCamelCase__				=    attention_probs_dropout_prob
       UpperCamelCase__				=    type_sequence_label_size
       UpperCamelCase__				=    initializer_range

       # in ViT, the seq length equals the number of patches + 1 (we add 1 for the [CLS] token)
       UpperCamelCase__				=    (image_size // patch_size) ** 2
       UpperCamelCase__				=    num_patches + 1







  def 	UpperCAmelCase_ (self						):
       UpperCamelCase__				=    floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]						)

       UpperCamelCase__				=    ViTConfig(
           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=SCREAMING_SNAKE_CASE_	,      initializer_range=self.initializer_range	,      )

       return config, pixel_values







  def 	UpperCAmelCase_ (self	,      SCREAMING_SNAKE_CASE_	,      SCREAMING_SNAKE_CASE_						):
       UpperCamelCase__				=    FlaxViTModel(config=SCREAMING_SNAKE_CASE_						)
       UpperCamelCase__				=    model(SCREAMING_SNAKE_CASE_						)
       # expected sequence length = num_patches + 1 (we add 1 for the [CLS] token)
       UpperCamelCase__				=    (self.image_size, self.image_size)
       UpperCamelCase__				=    (self.patch_size, self.patch_size)
       UpperCamelCase__				=    (image_size[1] // patch_size[1]) * (image_size[0] // patch_size[0])
       self.parent.assertEqual(result.last_hidden_state.shape	,      (self.batch_size, num_patches + 1, self.hidden_size)						)







  def 	UpperCAmelCase_ (self	,      SCREAMING_SNAKE_CASE_	,      SCREAMING_SNAKE_CASE_						):
       UpperCamelCase__				=    self.type_sequence_label_size
       UpperCamelCase__				=    FlaxViTForImageClassification(config=SCREAMING_SNAKE_CASE_						)
       UpperCamelCase__				=    model(SCREAMING_SNAKE_CASE_						)
       self.parent.assertEqual(result.logits.shape	,      (self.batch_size, self.type_sequence_label_size)						)

       # test greyscale images
       UpperCamelCase__				=    1
       UpperCamelCase__				=    FlaxViTForImageClassification(SCREAMING_SNAKE_CASE_						)

       UpperCamelCase__				=    floats_tensor([self.batch_size, 1, self.image_size, self.image_size]						)
       UpperCamelCase__				=    model(SCREAMING_SNAKE_CASE_						)







  def 	UpperCAmelCase_ (self						):
       UpperCamelCase__				=    self.prepare_config_and_inputs()
       (
           (
           UpperCamelCase__
       )		,	(
           UpperCamelCase__
       )		,	
       )				=    config_and_inputs
       UpperCamelCase__				=    {"""pixel_values""": pixel_values}
       return config, inputs_dict






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

  """simple docstring"""
  SCREAMING_SNAKE_CASE__         =      (FlaxViTModel, FlaxViTForImageClassification) if is_flax_available() else ()







  def 	UpperCAmelCase_ (self						):
       UpperCamelCase__				=    FlaxViTModelTester(self						)
       UpperCamelCase__				=    ConfigTester(self	,      config_class=SCREAMING_SNAKE_CASE_	,      has_text_modality=SCREAMING_SNAKE_CASE_	,      hidden_size=37						)







  def 	UpperCAmelCase_ (self						):
       self.config_tester.run_common_tests()







  def 	UpperCAmelCase_ (self						):
       UpperCamelCase__				=    self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_model(*SCREAMING_SNAKE_CASE_						)







  def 	UpperCAmelCase_ (self						):
       UpperCamelCase__				=    self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_for_image_classification(*SCREAMING_SNAKE_CASE_						)







  def 	UpperCAmelCase_ (self						):
       UpperCamelCase__		,	UpperCamelCase__				=    self.model_tester.prepare_config_and_inputs_for_common()

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

            UpperCamelCase__				=    ["""pixel_values"""]
            self.assertListEqual(arg_names[:1]	,      SCREAMING_SNAKE_CASE_						)







  def 	UpperCAmelCase_ (self						):
       UpperCamelCase__		,	UpperCamelCase__				=    self.model_tester.prepare_config_and_inputs_for_common()

       for model_class in self.all_model_classes:
            with self.subTest(model_class.__name__						):
                 UpperCamelCase__				=    self._prepare_for_class(SCREAMING_SNAKE_CASE_	,      SCREAMING_SNAKE_CASE_						)
                 UpperCamelCase__				=    model_class(SCREAMING_SNAKE_CASE_						)

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

                 with self.subTest("""JIT Enabled"""						):
                      UpperCamelCase__				=    model_jitted(**SCREAMING_SNAKE_CASE_						).to_tuple()

                 with self.subTest("""JIT Disabled"""						):
                      with jax.disable_jit():
                           UpperCamelCase__				=    model_jitted(**SCREAMING_SNAKE_CASE_						).to_tuple()

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







  @slow
  def 	UpperCAmelCase_ (self						):
       for model_class_name in self.all_model_classes:
            UpperCamelCase__				=    model_class_name.from_pretrained("""google/vit-base-patch16-224"""						)
            UpperCamelCase__				=    model(np.ones((1, 3, 2_24, 2_24)						)						)
            self.assertIsNotNone(SCREAMING_SNAKE_CASE_						)







import importlib
import inspect
import os
import re


# All paths are set with the intent you should run this script from the root of the repo with the command
# python utils/check_config_docstrings.py
lowerCamelCase_						     =					'''src/transformers'''


# This is to make sure the transformers module imported is the one in the repo.
lowerCamelCase_						     =					importlib.util.spec_from_file_location(
    '''transformers''',
    os.path.join(PATH_TO_TRANSFORMERS, '''__init__.py'''),
    submodule_search_locations=[PATH_TO_TRANSFORMERS],
)
lowerCamelCase_						     =					spec.loader.load_module()

lowerCamelCase_						     =					transformers.models.auto.configuration_auto.CONFIG_MAPPING

# Regex pattern used to find the checkpoint mentioned in the docstring of `config_class`.
# For example, `[bert-base-uncased](https://huggingface.co/bert-base-uncased)`
lowerCamelCase_						     =					re.compile('''\[(.+?)\]\((https://huggingface\.co/.+?)\)''')


lowerCamelCase_						     =					{
    '''CLIPConfigMixin''',
    '''DecisionTransformerConfigMixin''',
    '''EncoderDecoderConfigMixin''',
    '''RagConfigMixin''',
    '''SpeechEncoderDecoderConfigMixin''',
    '''VisionEncoderDecoderConfigMixin''',
    '''VisionTextDualEncoderConfigMixin''',
}






def 						__magic_name__			(   ):



     '''simple docstring'''




     UpperCamelCase__				=    []

     for config_class in list(CONFIG_MAPPING.values()						):
          UpperCamelCase__				=    False

          # source code of `config_class`
          UpperCamelCase__				=    inspect.getsource(__a						)
          UpperCamelCase__				=    _re_checkpoint.findall(__a						)

          for checkpoint in checkpoints:
               # Each `checkpoint` is a tuple of a checkpoint name and a checkpoint link.
               # For example, `('bert-base-uncased', 'https://huggingface.co/bert-base-uncased')`
               UpperCamelCase__		,	UpperCamelCase__				=    checkpoint

               # verify the checkpoint name corresponds to the checkpoint link
               UpperCamelCase__				=    f"https://huggingface.co/{ckpt_name}"
               if ckpt_link == ckpt_link_from_name:
                    UpperCamelCase__				=    True
                    break

          UpperCamelCase__				=    config_class.__name__
          if not checkpoint_found and name not in CONFIG_CLASSES_TO_IGNORE_FOR_DOCSTRING_CHECKPOINT_CHECK:
               configs_without_checkpoint.append(__a						)

     if len(__a						) > 0:
          UpperCamelCase__				=    """\n""".join(sorted(__a						)						)
          raise ValueError(f"The following configurations don't contain any valid checkpoint:\n{message}"						)


if __name__ == "__main__":
    check_config_docstrings_have_checkpoints()





