


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=1_3      , __snake_case=3_0      , __snake_case=2      , __snake_case=3      , __snake_case=True      , __snake_case=True      , __snake_case=3_2      , __snake_case=2      , __snake_case=4      , __snake_case=3_7      , __snake_case="gelu"      , __snake_case=0.1      , __snake_case=0.1      , __snake_case=1_0      , __snake_case=0.02      , __snake_case=3      , __snake_case=None      , ):
         snake_case					=    parent
         snake_case					=    batch_size
         snake_case					=    image_size
         snake_case					=    patch_size
         snake_case					=    num_channels
         snake_case					=    is_training
         snake_case					=    use_labels
         snake_case					=    hidden_size
         snake_case					=    num_hidden_layers
         snake_case					=    num_attention_heads
         snake_case					=    intermediate_size
         snake_case					=    hidden_act
         snake_case					=    hidden_dropout_prob
         snake_case					=    attention_probs_dropout_prob
         snake_case					=    type_sequence_label_size
         snake_case					=    initializer_range
         snake_case					=    scope

         # in ViT, the seq length equals the number of patches + 1 (we add 1 for the [CLS] token)
         snake_case					=    (image_size // patch_size) ** 2
         snake_case					=    num_patches + 1
    def 		a_      (	self   ):
         snake_case					=    floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]   )

         snake_case					=    None
         if self.use_labels:
              snake_case					=    ids_tensor([self.batch_size]      , self.type_sequence_label_size   )

         snake_case					=    self.get_config()

         return config, pixel_values, labels
    def 		a_      (	self   ):
         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 		a_      (	self      , __snake_case      , __snake_case      , __snake_case   ):
         snake_case					=    TFViTModel(config=__snake_case   )
         snake_case					=    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.
         snake_case					=    self.image_size // 2
         snake_case					=    pixel_values[:, :, :image_size, :image_size]
         snake_case					=    model(__snake_case      , interpolate_pos_encoding=__snake_case      , training=__snake_case   )
         snake_case					=    (image_size // self.patch_size) ** 2 + 1
         self.parent.assertEqual(result.last_hidden_state.shape      , (self.batch_size, seq_length, self.hidden_size)   )
    def 		a_      (	self      , __snake_case      , __snake_case      , __snake_case   ):
         snake_case					=    self.type_sequence_label_size
         snake_case					=    TFViTForImageClassification(__snake_case   )
         snake_case					=    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.
         snake_case					=    self.image_size // 2
         snake_case					=    pixel_values[:, :, :image_size, :image_size]
         snake_case					=    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
         snake_case					=    1
         snake_case					=    TFViTForImageClassification(__snake_case   )
         snake_case					=    floats_tensor([self.batch_size, 1, self.image_size, self.image_size]   )
         snake_case					=    model(__snake_case   )
         self.parent.assertEqual(result.logits.shape      , (self.batch_size, self.type_sequence_label_size)   )




    def 		a_      (	self   ):
         snake_case					=    self.prepare_config_and_inputs()
         snake_case			,  snake_case			,  snake_case					=    config_and_inputs
         snake_case					=    {'''pixel_values''': pixel_values}
         return config, inputs_dict






@require_tf
class   A__ (   snake_case__	, snake_case__	, unittest.TestCase     ):







    """simple docstring"""

    __magic_name__        =		(TFViTModel, TFViTForImageClassification) if is_tf_available() else ()
    __magic_name__        =		(
        {'feature-extraction': TFViTModel, 'image-classification': TFViTForImageClassification}
        if is_tf_available()
        else {}
    )

    __magic_name__        =		False
    __magic_name__        =		False
    __magic_name__        =		False
    def 		a_      (	self   ):
         snake_case					=    TFViTModelTester(self   )
         snake_case					=    ConfigTester(self      , config_class=__snake_case      , has_text_modality=__snake_case      , hidden_size=3_7   )
    def 		a_      (	self   ):
         self.config_tester.run_common_tests()
    @unittest.skip(reason='''ViT does not use inputs_embeds'''   )
    def 		a_      (	self   ):
         pass
    @unittest.skip(reason='''ViT does not use inputs_embeds'''   )
    def 		a_      (	self   ):
         pass
    def 		a_      (	self   ):
         snake_case			,  snake_case					=    self.model_tester.prepare_config_and_inputs_for_common()

         for model_class in self.all_model_classes:
              snake_case					=    model_class(__snake_case   )
              self.assertIsInstance(model.get_input_embeddings()      , (tf.keras.layers.Layer)   )
              snake_case					=    model.get_output_embeddings()
              self.assertTrue(x is None or isinstance(__snake_case      , tf.keras.layers.Layer   )   )
    def 		a_      (	self   ):
         snake_case			,  snake_case					=    self.model_tester.prepare_config_and_inputs_for_common()

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

              snake_case					=    ['''pixel_values''']
              self.assertListEqual(arg_names[:1]      , __snake_case   )
    def 		a_      (	self   ):
         snake_case					=    self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_model(*__snake_case   )
    def 		a_      (	self   ):
         snake_case					=    self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_for_image_classification(*__snake_case   )




    @slow
    def 		a_      (	self   ):
         snake_case					=    TFViTModel.from_pretrained('''google/vit-base-patch16-224'''   )
         self.assertIsNotNone(__snake_case   )






def       UpperCAmelCase__						():



     """simple docstring"""






     snake_case					=    Image.open('''./tests/fixtures/tests_samples/COCO/000000039769.png'''    )
     return image

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







    """simple docstring"""

    @cached_property
    def 		a_      (	self   ):
         return ViTImageProcessor.from_pretrained('''google/vit-base-patch16-224'''   ) if is_vision_available() else None




    @slow
    def 		a_      (	self   ):
         snake_case					=    TFViTForImageClassification.from_pretrained('''google/vit-base-patch16-224'''   )

         snake_case					=    self.default_image_processor
         snake_case					=    prepare_img()
         snake_case					=    image_processor(images=__snake_case      , return_tensors='''tf'''   )

         # forward pass
         snake_case					=    model(**__snake_case   )

         # verify the logits
         snake_case					=    tf.TensorShape((1, 1_0_0_0)   )
         self.assertEqual(outputs.logits.shape      , __snake_case   )

         snake_case					=    tf.constant([-0.2744, 0.8215, -0.0836]   )

         tf.debugging.assert_near(outputs.logits[0, :3]      , __snake_case      , atol=1E-4   )








def       UpperCAmelCase__						(UpperCamelCase_							,UpperCamelCase_							,UpperCamelCase_    ):



     """simple docstring"""






     snake_case					=    len(UpperCamelCase_    )
     snake_case					=    [[0] * n for i in range(UpperCamelCase_    )]
     for i in range(UpperCamelCase_    ):
          snake_case					=    y_points[i]

     for i in range(2							,UpperCamelCase_    ):
          for j in range(UpperCamelCase_							,UpperCamelCase_    ):
               snake_case					=    (
                   (xa - x_points[j - i + 1]) * q[j][i - 1]
                   - (xa - x_points[j]) * q[j - 1][i - 1]
               ) / (x_points[j] - x_points[j - i + 1])

     return [q[n - 1][n - 1], q]


if __name__ == "__main__":
 import doctest

 doctest.testmod()




