'''simple docstring'''






from __future__ import annotations

import inspect
import unittest

from transformers import ViTConfig
from transformers.testing_utils import require_tf, require_vision, slow
from transformers.utils import cached_property, is_tf_available, is_vision_available

from ...test_configuration_common import ConfigTester
from ...test_modeling_tf_common import TFModelTesterMixin, floats_tensor, ids_tensor
from ...test_pipeline_mixin import PipelineTesterMixin


if is_tf_available():
    import tensorflow as tf

    from transformers import TFViTForImageClassification, TFViTModel


if is_vision_available():
    from PIL import Image

    from transformers import ViTImageProcessor




class    a :



  """simple docstring"""







  def __init__(							self   ,    snake_case_   ,    snake_case_=13   ,    snake_case_=30   ,    snake_case_=2   ,    snake_case_=3   ,    snake_case_=True   ,    snake_case_=True   ,    snake_case_=32   ,    snake_case_=2   ,    snake_case_=4   ,    snake_case_=37   ,    snake_case_="gelu"   ,    snake_case_=0.1   ,    snake_case_=0.1   ,    snake_case_=10   ,    snake_case_=0.0_2   ,    snake_case_=3   ,    snake_case_=None   ,    ):

    '''simple docstring'''







    __UpperCAmelCase:					Optional[int]		= parent
    __UpperCAmelCase:					str		= batch_size
    __UpperCAmelCase:					Union[str, Any]		= image_size
    __UpperCAmelCase:					Dict		= patch_size
    __UpperCAmelCase:					List[str]		= num_channels
    __UpperCAmelCase:					List[Any]		= is_training
    __UpperCAmelCase:					Any		= use_labels
    __UpperCAmelCase:					str		= hidden_size
    __UpperCAmelCase:					Optional[Any]		= num_hidden_layers
    __UpperCAmelCase:					List[Any]		= num_attention_heads
    __UpperCAmelCase:					Any		= intermediate_size
    __UpperCAmelCase:					Optional[Any]		= hidden_act
    __UpperCAmelCase:					List[str]		= hidden_dropout_prob
    __UpperCAmelCase:					Tuple		= attention_probs_dropout_prob
    __UpperCAmelCase:					Union[str, Any]		= type_sequence_label_size
    __UpperCAmelCase:					Union[str, Any]		= initializer_range
    __UpperCAmelCase:					Optional[int]		= scope

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







  def    lowercase_  (							self					):

    '''simple docstring'''







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

    __UpperCAmelCase:					Any		= None
    if self.use_labels:
      __UpperCAmelCase:					Dict		= ids_tensor([self.batch_size]   ,    self.type_sequence_label_size					)

    __UpperCAmelCase:					str		= self.get_config()

    return config, pixel_values, labels







  def    lowercase_  (							self					):

    '''simple docstring'''







    return 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=snake_case_   ,    initializer_range=self.initializer_range   ,    )







  def    lowercase_  (							self   ,    snake_case_   ,    snake_case_   ,    snake_case_					):

    '''simple docstring'''







    __UpperCAmelCase:					Dict		= TFViTModel(config=snake_case_					)
    __UpperCAmelCase:					int		= model(snake_case_   ,    training=snake_case_					)
    self.parent.assertEqual(result.last_hidden_state.shape   ,    (self.batch_size, self.seq_length, self.hidden_size)					)

    # Test with an image with different size than the one specified in config.
    __UpperCAmelCase:					Dict		= self.image_size // 2
    __UpperCAmelCase:					Optional[Any]		= pixel_values[:, :, :image_size, :image_size]
    __UpperCAmelCase:					Union[str, Any]		= model(snake_case_   ,    interpolate_pos_encoding=snake_case_   ,    training=snake_case_					)
    __UpperCAmelCase:					str		= (image_size // self.patch_size) ** 2 + 1
    self.parent.assertEqual(result.last_hidden_state.shape   ,    (self.batch_size, seq_length, self.hidden_size)					)







  def    lowercase_  (							self   ,    snake_case_   ,    snake_case_   ,    snake_case_					):

    '''simple docstring'''







    __UpperCAmelCase:					str		= self.type_sequence_label_size
    __UpperCAmelCase:					Optional[Any]		= TFViTForImageClassification(snake_case_					)
    __UpperCAmelCase:					Tuple		= model(snake_case_   ,    labels=snake_case_   ,    training=snake_case_					)
    self.parent.assertEqual(result.logits.shape   ,    (self.batch_size, self.type_sequence_label_size)					)

    # Test with an image with different size than the one specified in config.
    __UpperCAmelCase:					List[Any]		= self.image_size // 2
    __UpperCAmelCase:					Union[str, Any]		= pixel_values[:, :, :image_size, :image_size]
    __UpperCAmelCase:					int		= model(snake_case_   ,    interpolate_pos_encoding=snake_case_   ,    training=snake_case_					)
    self.parent.assertEqual(result.logits.shape   ,    (self.batch_size, self.type_sequence_label_size)					)

    # test greyscale images
    __UpperCAmelCase:					List[Any]		= 1
    __UpperCAmelCase:					Tuple		= TFViTForImageClassification(snake_case_					)
    __UpperCAmelCase:					Optional[int]		= floats_tensor([self.batch_size, 1, self.image_size, self.image_size]					)
    __UpperCAmelCase:					Optional[int]		= model(snake_case_					)
    self.parent.assertEqual(result.logits.shape   ,    (self.batch_size, self.type_sequence_label_size)					)







  def    lowercase_  (							self					):

    '''simple docstring'''







    __UpperCAmelCase:					Dict		= self.prepare_config_and_inputs()
    __UpperCAmelCase, __UpperCAmelCase, __UpperCAmelCase:					Tuple		= config_and_inputs
    __UpperCAmelCase:					Dict		= {"""pixel_values""": pixel_values}
    return config, inputs_dict




@require_tf
class    a (       __lowerCAmelCase						,  __lowerCAmelCase						,  unittest.TestCase				):



  """simple docstring"""
  __lowerCAmelCase				      =					(TFViTModel, TFViTForImageClassification) if is_tf_available() else ()
  __lowerCAmelCase				      =					(
      {"""feature-extraction""": TFViTModel, """image-classification""": TFViTForImageClassification}
      if is_tf_available()
      else {}
  )

  __lowerCAmelCase				      =					False
  __lowerCAmelCase				      =					False
  __lowerCAmelCase				      =					False







  def    lowercase_  (							self					):

    '''simple docstring'''







    __UpperCAmelCase:					List[str]		= TFViTModelTester(self					)
    __UpperCAmelCase:					Any		= ConfigTester(self   ,    config_class=snake_case_   ,    has_text_modality=snake_case_   ,    hidden_size=37					)







  def    lowercase_  (							self					):

    '''simple docstring'''







    self.config_tester.run_common_tests()







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

    '''simple docstring'''







    pass







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

    '''simple docstring'''







    pass







  def    lowercase_  (							self					):

    '''simple docstring'''







    __UpperCAmelCase, __UpperCAmelCase:					int		= self.model_tester.prepare_config_and_inputs_for_common()

    for model_class in self.all_model_classes:
      __UpperCAmelCase:					str		= model_class(snake_case_					)
      self.assertIsInstance(model.get_input_embeddings()   ,    (tf.keras.layers.Layer)					)
      __UpperCAmelCase:					Dict		= model.get_output_embeddings()
      self.assertTrue(x is None or isinstance(snake_case_   ,    tf.keras.layers.Layer					)					)







  def    lowercase_  (							self					):

    '''simple docstring'''







    __UpperCAmelCase, __UpperCAmelCase:					Dict		= self.model_tester.prepare_config_and_inputs_for_common()

    for model_class in self.all_model_classes:
      __UpperCAmelCase:					int		= model_class(snake_case_					)
      __UpperCAmelCase:					str		= inspect.signature(model.call					)
      # signature.parameters is an OrderedDict => so arg_names order is deterministic
      __UpperCAmelCase:					Tuple		= [*signature.parameters.keys()]

      __UpperCAmelCase:					Tuple		= ["""pixel_values"""]
      self.assertListEqual(arg_names[:1]   ,    snake_case_					)







  def    lowercase_  (							self					):

    '''simple docstring'''







    __UpperCAmelCase:					str		= self.model_tester.prepare_config_and_inputs()
    self.model_tester.create_and_check_model(*snake_case_					)







  def    lowercase_  (							self					):

    '''simple docstring'''







    __UpperCAmelCase:					str		= self.model_tester.prepare_config_and_inputs()
    self.model_tester.create_and_check_for_image_classification(*snake_case_					)







  @slow
  def    lowercase_  (							self					):

    '''simple docstring'''







    __UpperCAmelCase:					int		= TFViTModel.from_pretrained("""google/vit-base-patch16-224"""					)
    self.assertIsNotNone(snake_case_					)






def  UpperCamelCase__					(					)							->	Optional[int]:
  __UpperCAmelCase:					Optional[Any]		= Image.open("""./tests/fixtures/tests_samples/COCO/000000039769.png"""     )
  return image







@require_tf
@require_vision
class    a (       unittest.TestCase				):



  """simple docstring"""







  @cached_property
  def    lowercase_  (							self					):

    '''simple docstring'''







    return ViTImageProcessor.from_pretrained("""google/vit-base-patch16-224"""					) if is_vision_available() else None







  @slow
  def    lowercase_  (							self					):

    '''simple docstring'''







    __UpperCAmelCase:					Tuple		= TFViTForImageClassification.from_pretrained("""google/vit-base-patch16-224"""					)

    __UpperCAmelCase:					Union[str, Any]		= self.default_image_processor
    __UpperCAmelCase:					str		= prepare_img()
    __UpperCAmelCase:					Dict		= image_processor(images=snake_case_   ,    return_tensors="""tf"""					)

    # forward pass
    __UpperCAmelCase:					Optional[Any]		= model(**snake_case_					)

    # verify the logits
    __UpperCAmelCase:					Optional[Any]		= tf.TensorShape((1, 1000)					)
    self.assertEqual(outputs.logits.shape   ,    snake_case_					)

    __UpperCAmelCase:					int		= tf.constant([-0.2_7_4_4, 0.8_2_1_5, -0.0_8_3_6]					)

    tf.debugging.assert_near(outputs.logits[0, :3]   ,    snake_case_   ,    atol=1e-4					)
'''simple docstring'''






import unittest

import numpy as np

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

from ...test_modeling_flax_common import FlaxModelTesterMixin, ids_tensor, random_attention_mask


if is_flax_available():
    import jax.numpy as jnp

    from transformers.models.albert.modeling_flax_albert import (
        FlaxAlbertForMaskedLM,
        FlaxAlbertForMultipleChoice,
        FlaxAlbertForPreTraining,
        FlaxAlbertForQuestionAnswering,
        FlaxAlbertForSequenceClassification,
        FlaxAlbertForTokenClassification,
        FlaxAlbertModel,
    )




class    a (       unittest.TestCase				):



  """simple docstring"""







  def __init__(							self   ,    snake_case_   ,    snake_case_=13   ,    snake_case_=7   ,    snake_case_=True   ,    snake_case_=True   ,    snake_case_=True   ,    snake_case_=True   ,    snake_case_=99   ,    snake_case_=32   ,    snake_case_=5   ,    snake_case_=4   ,    snake_case_=37   ,    snake_case_="gelu"   ,    snake_case_=0.1   ,    snake_case_=0.1   ,    snake_case_=512   ,    snake_case_=16   ,    snake_case_=2   ,    snake_case_=0.0_2   ,    snake_case_=4   ,    ):

    '''simple docstring'''







    __UpperCAmelCase:					Optional[Any]		= parent
    __UpperCAmelCase:					List[str]		= batch_size
    __UpperCAmelCase:					Optional[int]		= seq_length
    __UpperCAmelCase:					Optional[Any]		= is_training
    __UpperCAmelCase:					Any		= use_attention_mask
    __UpperCAmelCase:					List[str]		= use_token_type_ids
    __UpperCAmelCase:					List[str]		= use_labels
    __UpperCAmelCase:					List[str]		= vocab_size
    __UpperCAmelCase:					Optional[Any]		= hidden_size
    __UpperCAmelCase:					List[Any]		= num_hidden_layers
    __UpperCAmelCase:					List[Any]		= num_attention_heads
    __UpperCAmelCase:					Tuple		= intermediate_size
    __UpperCAmelCase:					Dict		= hidden_act
    __UpperCAmelCase:					Dict		= hidden_dropout_prob
    __UpperCAmelCase:					Tuple		= attention_probs_dropout_prob
    __UpperCAmelCase:					List[str]		= max_position_embeddings
    __UpperCAmelCase:					List[Any]		= type_vocab_size
    __UpperCAmelCase:					List[Any]		= type_sequence_label_size
    __UpperCAmelCase:					List[str]		= initializer_range
    __UpperCAmelCase:					List[Any]		= num_choices







  def    lowercase_  (							self					):

    '''simple docstring'''







    __UpperCAmelCase:					int		= ids_tensor([self.batch_size, self.seq_length]   ,    self.vocab_size					)

    __UpperCAmelCase:					str		= None
    if self.use_attention_mask:
      __UpperCAmelCase:					Any		= random_attention_mask([self.batch_size, self.seq_length]					)

    __UpperCAmelCase:					List[Any]		= None
    if self.use_token_type_ids:
      __UpperCAmelCase:					Optional[int]		= ids_tensor([self.batch_size, self.seq_length]   ,    self.type_vocab_size					)

    __UpperCAmelCase:					List[Any]		= AlbertConfig(
        vocab_size=self.vocab_size   ,    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   ,    max_position_embeddings=self.max_position_embeddings   ,    type_vocab_size=self.type_vocab_size   ,    is_decoder=snake_case_   ,    initializer_range=self.initializer_range   ,    )

    return config, input_ids, token_type_ids, attention_mask







  def    lowercase_  (							self					):

    '''simple docstring'''







    __UpperCAmelCase:					Tuple		= self.prepare_config_and_inputs()
    __UpperCAmelCase, __UpperCAmelCase, __UpperCAmelCase, __UpperCAmelCase:					List[str]		= config_and_inputs
    __UpperCAmelCase:					List[Any]		= {"""input_ids""": input_ids, """token_type_ids""": token_type_ids, """attention_mask""": attention_mask}
    return config, inputs_dict




@require_flax
class    a (       __lowerCAmelCase						,  unittest.TestCase				):



  """simple docstring"""
  __lowerCAmelCase				      =					(
      (
          FlaxAlbertModel,
          FlaxAlbertForPreTraining,
          FlaxAlbertForMaskedLM,
          FlaxAlbertForMultipleChoice,
          FlaxAlbertForQuestionAnswering,
          FlaxAlbertForSequenceClassification,
          FlaxAlbertForTokenClassification,
          FlaxAlbertForQuestionAnswering,
      )
      if is_flax_available()
      else ()
  )







  def    lowercase_  (							self					):

    '''simple docstring'''







    __UpperCAmelCase:					Union[str, Any]		= FlaxAlbertModelTester(self					)







  @slow
  def    lowercase_  (							self					):

    '''simple docstring'''







    for model_class_name in self.all_model_classes:
      __UpperCAmelCase:					str		= model_class_name.from_pretrained("""albert-base-v2"""					)
      __UpperCAmelCase:					int		= model(np.ones((1, 1)					)					)
      self.assertIsNotNone(snake_case_					)




@require_flax
class    a (       unittest.TestCase				):



  """simple docstring"""







  @slow
  def    lowercase_  (							self					):

    '''simple docstring'''







    __UpperCAmelCase:					str		= FlaxAlbertModel.from_pretrained("""albert-base-v2"""					)
    __UpperCAmelCase:					Dict		= np.array([[0, 345, 232, 328, 740, 140, 1695, 69, 6078, 1588, 2]]					)
    __UpperCAmelCase:					int		= np.array([[0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]]					)
    __UpperCAmelCase:					Optional[int]		= model(snake_case_   ,    attention_mask=snake_case_					)[0]
    __UpperCAmelCase:					Optional[int]		= (1, 11, 768)
    self.assertEqual(output.shape   ,    snake_case_					)
    __UpperCAmelCase:					str		= np.array(
        [[[-0.6_5_1_3, 1.5_0_3_5, -0.2_7_6_6], [-0.6_5_1_5, 1.5_0_4_6, -0.2_7_8_0], [-0.6_5_1_2, 1.5_0_4_9, -0.2_7_8_4]]]					)

    self.assertTrue(jnp.allclose(output[:, 1:4, 1:4]   ,    snake_case_   ,    atol=1e-4					)					)