codingutf8 2022 the huggingface inc team licensed under the apache license version 2 0 the license you may not use this file except in compliance with the license you may obtain a copy of the license at http www apache orglicenseslicense2 0 unless required by applicable law or agreed to in writing software distributed under the license is distributed on an as is basis without warranties or conditions of any kind either express or implied see the license for the specific language governing permissions and limitations under the license testing suite for the tensorflow data2vecvision model from future import annotations import collections abc import inspect import unittest import numpy as np from transformers import data2vecvisionconfig from transformers fileutils import cachedproperty istfavailable isvisionavailable from transformers testingutils import requiretf requirevision slow from testconfigurationcommon import configtester from testmodelingtfcommon import tfmodeltestermixin floatstensor idstensor from testpipelinemixin import pipelinetestermixin if istfavailable import tensorflow as tf from transformers import tfdata2vecvisionforimageclassification tfdata2vecvisionforsemanticsegmentation tfdata2vecvisionmodel from transformers models data2vec modelingtfdata2vecvision import tfdata2vecvisionpretrainedmodelarchivelist if isvisionavailable from pil import image from transformers import beitimageprocessor class tfdata2vecvisionmodeltester def init self parent vocabsize100 batchsize13 imagesize30 patchsize2 numchannels3 istrainingtrue uselabelstrue hiddensize32 numhiddenlayers2 numattentionheads4 intermediatesize37 hiddenactgelu hiddendropoutprob0 1 attentionprobsdropoutprob0 1 typesequencelabelsize10 initializerrange0 02 numlabels3 scopenone outindices0 1 2 3 self parent parent self vocabsize 100 self batchsize batchsize self imagesize imagesize self patchsize patchsize self numchannels numchannels self istraining istraining self uselabels uselabels self hiddensize hiddensize self numhiddenlayers numhiddenlayers self numattentionheads numattentionheads self intermediatesize intermediatesize self hiddenact hiddenact self hiddendropoutprob hiddendropoutprob self attentionprobsdropoutprob attentionprobsdropoutprob self typesequencelabelsize typesequencelabelsize self initializerrange initializerrange self scope scope self outindices outindices self numlabels numlabels def prepareconfigandinputsself pixelvalues floatstensorself batchsize self numchannels self imagesize self imagesize labels none pixellabels none if self uselabels labels idstensorself batchsize self typesequencelabelsize pixellabels idstensorself batchsize self imagesize self imagesize self numlabels config self getconfig return config pixelvalues labels pixellabels def getconfigself return data2vecvisionconfig vocabsizeself vocabsize imagesizeself imagesize patchsizeself patchsize numchannelsself numchannels hiddensizeself hiddensize numhiddenlayersself numhiddenlayers numattentionheadsself numattentionheads intermediatesizeself intermediatesize hiddenactself hiddenact hiddendropoutprobself hiddendropoutprob attentionprobsdropoutprobself attentionprobsdropoutprob isdecoderfalse initializerrangeself initializerrange outindicesself outindices def createandcheckmodelself config pixelvalues labels pixellabels model tfdata2vecvisionmodelconfigconfig result modelpixelvalues trainingfalse expected sequence length numpatches 1 we add 1 for the cls token imagesize self imagesize if isinstanceself imagesize collections abc iterable else self imagesize self imagesize patchsize self patchsize if isinstanceself imagesize collections abc iterable else self patchsize self patchsize numpatches imagesize1 patchsize1 imagesize0 patchsize0 self parent assertequalresult lasthiddenstate shape self batchsize numpatches 1 self hiddensize def createandcheckforimageclassificationself config pixelvalues labels pixellabels config numlabels self typesequencelabelsize model tfdata2vecvisionforimageclassificationconfig result modelpixelvalues labelslabels trainingfalse self parent assertequalresult logits shape self batchsize self typesequencelabelsize def createandcheckforimagesegmentationself config pixelvalues labels pixellabels config numlabels self numlabels model tfdata2vecvisionforsemanticsegmentationconfig result modelpixelvalues trainingfalse self parent assertequal result logits shape self batchsize self numlabels self imagesize 2 self imagesize 2 result modelpixelvalues labelspixellabels self parent assertequal result logits shape self batchsize self numlabels self imagesize 2 self imagesize 2 def prepareconfigandinputsforcommonself configandinputs self prepareconfigandinputs config pixelvalues labels pixellabels configandinputs inputsdict pixelvalues pixelvalues return config inputsdict def prepareconfigandinputsforkerasfitself configandinputs self prepareconfigandinputs config pixelvalues configandinputs inputsdict pixelvalues pixelvalues labels tf zerosself batchsize return config inputsdict requiretf class tfdata2vecvisionmodeltesttfmodeltestermixin pipelinetestermixin unittest testcase allmodelclasses tfdata2vecvisionmodel tfdata2vecvisionforimageclassification tfdata2vecvisionforsemanticsegmentation if istfavailable else pipelinemodelmapping featureextraction tfdata2vecvisionmodel imageclassification tfdata2vecvisionforimageclassification if istfavailable else testpruning false testonnx false testresizeembeddings false testheadmasking false def setupself self modeltester tfdata2vecvisionmodeltesterself self configtester configtester self configclassdata2vecvisionconfig hastextmodalityfalse hiddensize37 def testconfigself self configtester runcommontests unittest skipreasondata2vecvision does not use inputsembeds def testinputsembedsself data2vecvision does not use inputsembeds pass def testmodelcommonattributesself config self modeltester prepareconfigandinputsforcommon for modelclass in self allmodelclasses model modelclassconfig self assertisinstancemodel getinputembeddings tf keras layers layer x model getoutputembeddings self asserttruex is none or isinstancex tf keras layers layer def testforwardsignatureself config self modeltester prepareconfigandinputsforcommon for modelclass in self allmodelclasses model modelclassconfig signature inspect signaturemodel call signature parameters is an ordereddict so argnames order is deterministic argnames signature parameters keys expectedargnames pixelvalues self assertlistequalargnames 1 expectedargnames def testmodelself configandinputs self modeltester prepareconfigandinputs self modeltester createandcheckmodelconfigandinputs def testforimagesegmentationself configandinputs self modeltester prepareconfigandinputs self modeltester createandcheckforimagesegmentationconfigandinputs def testattentionoutputsself config inputsdict self modeltester prepareconfigandinputsforcommon config returndict true in data2vecvision the seqlen equals the number of patches 1 we add 1 for the cls token imagesize self modeltester imagesize if isinstanceself modeltester imagesize collections abc iterable else self modeltester imagesize self modeltester imagesize patchsize self modeltester patchsize if isinstanceself modeltester patchsize collections abc iterable else self modeltester patchsize self modeltester patchsize numpatches imagesize1 patchsize1 imagesize0 patchsize0 seqlen numpatches 1 encoderseqlength getattrself modeltester encoderseqlength seqlen encoderkeylength getattrself modeltester keylength encoderseqlength chunklength getattrself modeltester chunklength none if chunklength is not none and hasattrself modeltester numhashes encoderseqlength encoderseqlength self modeltester numhashes for modelclass in self allmodelclasses inputsdictoutputattentions true inputsdictoutputhiddenstates false config returndict true model modelclassconfig outputs modelself prepareforclassinputsdict modelclass trainingfalse attentions outputs encoderattentions if config isencoderdecoder else outputs attentions self assertequallenattentions self modeltester numhiddenlayers check that outputattentions also work using config del inputsdictoutputattentions config outputattentions true model modelclassconfig outputs modelself prepareforclassinputsdict modelclass trainingfalse attentions outputs encoderattentions if config isencoderdecoder else outputs attentions self assertequallenattentions self modeltester numhiddenlayers self assertlistequal listattentions0 shape3 self modeltester numattentionheads encoderseqlength encoderkeylength outlen lenoutputs check attention is always last and order is fine inputsdictoutputattentions true inputsdictoutputhiddenstates true model modelclassconfig outputs modelself prepareforclassinputsdict modelclass trainingfalse self assertequaloutlen 1 lenoutputs selfattentions outputs attentions self assertequallenselfattentions self modeltester numhiddenlayers self assertlistequal listselfattentions0 shape3 self modeltester numattentionheads encoderseqlength encoderkeylength def testhiddenstatesoutputself def checkhiddenstatesoutputinputsdict config modelclass model modelclassconfig outputs modelself prepareforclassinputsdict modelclass hiddenstates outputs encoderhiddenstates if config isencoderdecoder else outputs hiddenstates expectednumlayers getattr self modeltester expectednumhiddenlayers self modeltester numhiddenlayers 1 self assertequallenhiddenstates expectednumlayers data2vecvision has a different seqlength imagesize self modeltester imagesize if isinstanceself modeltester imagesize collections abc iterable else self modeltester imagesize self modeltester imagesize patchsize self modeltester patchsize if isinstanceself modeltester patchsize collections abc iterable else self modeltester patchsize self modeltester patchsize numpatches imagesize1 patchsize1 imagesize0 patchsize0 seqlength numpatches 1 self assertlistequal listhiddenstates0 shape2 seqlength self modeltester hiddensize config inputsdict self modeltester prepareconfigandinputsforcommon for modelclass in self allmodelclasses inputsdictoutputhiddenstates true checkhiddenstatesoutputinputsdict config modelclass check that outputhiddenstates also work using config del inputsdictoutputhiddenstates config outputhiddenstates true checkhiddenstatesoutputinputsdict config modelclass overriding this method since the base method won t be compatible with data2vecvision slow def testkerasfitself config self modeltester prepareconfigandinputsforcommon for modelclass in self allmodelclasses since tfdata2vecvisionmodel cannot operate with the default fit method if modelclass name tfdata2vecvisionmodel model modelclassconfig if getattrmodel hfcomputeloss none test that model correctly compute the loss with kwargs preparedforclass self modeltester prepareconfigandinputsforkerasfit labelnames labels self assertgreaterlenlabelnames 0 msgno matching label names found labels key val for key val in preparedforclass items if key in labelnames inputsminuslabels key val for key val in preparedforclass items if key not in labelnames self assertgreaterleninputsminuslabels 0 model compileoptimizertf keras optimizers sgd0 0 runeagerlytrue make sure the model fits without crashing regardless of where we pass the labels history1 model fit preparedforclass validationdatapreparedforclass stepsperepoch1 validationsteps1 shufflefalse valloss1 history1 historyvalloss0 history2 model fit inputsminuslabels labels validationdatainputsminuslabels labels stepsperepoch1 validationsteps1 shufflefalse valloss2 history2 historyvalloss0 self asserttruenp allclosevalloss1 valloss2 atol1e2 rtol1e3 def checkpttfoutputsself tfoutputs ptoutputs modelclass tol2e4 nameoutputs attributesnone we override with a slightly higher tol value as semseg models tend to diverge a bit more super checkpttfoutputstfoutputs ptoutputs modelclass tol name attributes overriding this method since the base method won t be compatible with data2vecvision def testlosscomputationself config inputsdict self modeltester prepareconfigandinputsforcommon for modelclass in self allmodelclasses since tfdata2vecvisionmodel won t have labels against which we could compute loss if modelclass name tfdata2vecvisionmodel model modelclassconfig if getattrmodel hfcomputeloss none the number of elements in the loss should be the same as the number of elements in the label preparedforclass self modeltester prepareconfigandinputsforkerasfit addedlabel preparedforclass sortedpreparedforclass keys inputsdict keys reversetrue0 losssize tf sizeaddedlabel test that model correctly compute the loss with kwargs possibleinputnames inputids pixelvalues inputfeatures inputname possibleinputnames intersectionsetpreparedforclass pop modelinput preparedforclass popinputname loss modelmodelinput preparedforclass0 self assertequalloss shape losssize test that model correctly compute the loss with a dict preparedforclass self modeltester prepareconfigandinputsforkerasfit loss modelpreparedforclass0 self assertequalloss shape losssize test that model correctly compute the loss with a tuple labelkeys preparedforclass keys inputsdict keys signature inspect signaturemodel call parameters signaturenames listsignature keys create a dictionary holding the location of the tensors in the tuple tupleindexmapping 0 inputname for labelkey in labelkeys labelkeyindex signaturenames indexlabelkey tupleindexmappinglabelkeyindex labelkey sortedtupleindexmapping sortedtupleindexmapping items initialize a list with their default values update the values and convert to a tuple listinput for name in signaturenames if name kwargs listinput appendsignaturename default for index value in sortedtupleindexmapping listinputindex preparedforclassvalue tupleinput tuplelistinput send to model loss modeltupleinput 10 self assertequalloss shape losssize def testforimageclassificationself configandinputs self modeltester prepareconfigandinputs self modeltester createandcheckforimageclassificationconfigandinputs slow def testmodelfrompretrainedself for modelname in tfdata2vecvisionpretrainedmodelarchivelist 1 model tfdata2vecvisionmodel frompretrainedmodelname self assertisnotnonemodel we will verify our results on an image of cute cats def prepareimg image image open testsfixturestestssamplescoco000000039769 png return image requiretf requirevision class tfdata2vecvisionmodelintegrationtestunittest testcase cachedproperty def defaultimageprocessorself return beitimageprocessor frompretrainedfacebookdata2vecvisionbaseft1k if isvisionavailable else none slow def testinferenceimageclassificationheadimagenet1kself model tfdata2vecvisionforimageclassification frompretrainedfacebookdata2vecvisionbaseft1k imageprocessor self defaultimageprocessor image prepareimg inputs imageprocessorimagesimage returntensorstf forward pass outputs modelinputs logits outputs logits verify the logits expectedshape tf converttotensor1 1000 self assertequallogits shape expectedshape expectedslice tf converttotensor0 3277 0 1395 0 0911 tf debugging assertnearlogits0 3 expectedslice atol1e4 expectedtop2 model config label2idi for i in remote control remote tabby tabby cat self assertequaltf nn topkoutputs logits0 2 indices numpy tolist expectedtop2 coding utf 8 2022 the huggingface inc team licensed under the apache license version 2 0 the license you may not use this file except in compliance with the license you may obtain a copy of the license at http www apache org licenses license 2 0 unless required by applicable law or agreed to in writing software distributed under the license is distributed on an as is basis without warranties or conditions of any kind either express or implied see the license for the specific language governing permissions and limitations under the license testing suite for the tensorflow data2vecvision model expected sequence length num_patches 1 we add 1 for the cls token here we also overwrite some of the tests of test_modeling_common py as data2vecvision does not use input_ids inputs_embeds attention_mask and seq_length data2vecvision does not use inputs_embeds signature parameters is an ordereddict so arg_names order is deterministic in data2vecvision the seq_len equals the number of patches 1 we add 1 for the cls token check that output_attentions also work using config check attention is always last and order is fine data2vecvision has a different seq_length check that output_hidden_states also work using config overriding this method since the base method won t be compatible with data2vecvision since tfdata2vecvisionmodel cannot operate with the default fit method test that model correctly compute the loss with kwargs make sure the model fits without crashing regardless of where we pass the labels we override with a slightly higher tol value as semseg models tend to diverge a bit more overriding this method since the base method won t be compatible with data2vecvision since tfdata2vecvisionmodel won t have labels against which we could compute loss the number of elements in the loss should be the same as the number of elements in the label test that model correctly compute the loss with kwargs test that model correctly compute the loss with a dict test that model correctly compute the loss with a tuple create a dictionary holding the location of the tensors in the tuple initialize a list with their default values update the values and convert to a tuple send to model we will verify our results on an image of cute cats forward pass verify the logits
from __future__ import annotations

import collections.abc
import inspect
import unittest

import numpy as np

from transformers import Data2VecVisionConfig
from transformers.file_utils import cached_property, is_tf_available, is_vision_available
from transformers.testing_utils import require_tf, require_vision, slow

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 (
        TFData2VecVisionForImageClassification,
        TFData2VecVisionForSemanticSegmentation,
        TFData2VecVisionModel,
    )
    from transformers.models.data2vec.modeling_tf_data2vec_vision import (
        TF_DATA2VEC_VISION_PRETRAINED_MODEL_ARCHIVE_LIST,
    )

if is_vision_available():
    from PIL import Image

    from transformers import BeitImageProcessor


class TFData2VecVisionModelTester:
    def __init__(
        self,
        parent,
        vocab_size=100,
        batch_size=13,
        image_size=30,
        patch_size=2,
        num_channels=3,
        is_training=True,
        use_labels=True,
        hidden_size=32,
        num_hidden_layers=2,
        num_attention_heads=4,
        intermediate_size=37,
        hidden_act="gelu",
        hidden_dropout_prob=0.1,
        attention_probs_dropout_prob=0.1,
        type_sequence_label_size=10,
        initializer_range=0.02,
        num_labels=3,
        scope=None,
        out_indices=[0, 1, 2, 3],
    ):
        self.parent = parent
        self.vocab_size = 100
        self.batch_size = batch_size
        self.image_size = image_size
        self.patch_size = patch_size
        self.num_channels = num_channels
        self.is_training = is_training
        self.use_labels = use_labels
        self.hidden_size = hidden_size
        self.num_hidden_layers = num_hidden_layers
        self.num_attention_heads = num_attention_heads
        self.intermediate_size = intermediate_size
        self.hidden_act = hidden_act
        self.hidden_dropout_prob = hidden_dropout_prob
        self.attention_probs_dropout_prob = attention_probs_dropout_prob
        self.type_sequence_label_size = type_sequence_label_size
        self.initializer_range = initializer_range
        self.scope = scope
        self.out_indices = out_indices
        self.num_labels = num_labels

    def prepare_config_and_inputs(self):
        pixel_values = floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size])

        labels = None
        pixel_labels = None
        if self.use_labels:
            labels = ids_tensor([self.batch_size], self.type_sequence_label_size)
            pixel_labels = ids_tensor([self.batch_size, self.image_size, self.image_size], self.num_labels)

        config = self.get_config()

        return config, pixel_values, labels, pixel_labels

    def get_config(self):
        return Data2VecVisionConfig(
            vocab_size=self.vocab_size,
            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=False,
            initializer_range=self.initializer_range,
            out_indices=self.out_indices,
        )

    def create_and_check_model(self, config, pixel_values, labels, pixel_labels):
        model = TFData2VecVisionModel(config=config)
        result = model(pixel_values, training=False)
        
        image_size = (
            self.image_size
            if isinstance(self.image_size, collections.abc.Iterable)
            else (self.image_size, self.image_size)
        )
        patch_size = (
            self.patch_size
            if isinstance(self.image_size, collections.abc.Iterable)
            else (self.patch_size, self.patch_size)
        )
        num_patches = (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 create_and_check_for_image_classification(self, config, pixel_values, labels, pixel_labels):
        config.num_labels = self.type_sequence_label_size
        model = TFData2VecVisionForImageClassification(config)

        result = model(pixel_values, labels=labels, training=False)
        self.parent.assertEqual(result.logits.shape, (self.batch_size, self.type_sequence_label_size))

    def create_and_check_for_image_segmentation(self, config, pixel_values, labels, pixel_labels):
        config.num_labels = self.num_labels
        model = TFData2VecVisionForSemanticSegmentation(config)
        result = model(pixel_values, training=False)
        self.parent.assertEqual(
            result.logits.shape, (self.batch_size, self.num_labels, self.image_size * 2, self.image_size * 2)
        )
        result = model(pixel_values, labels=pixel_labels)
        self.parent.assertEqual(
            result.logits.shape, (self.batch_size, self.num_labels, self.image_size * 2, self.image_size * 2)
        )

    def prepare_config_and_inputs_for_common(self):
        config_and_inputs = self.prepare_config_and_inputs()
        config, pixel_values, labels, pixel_labels = config_and_inputs
        inputs_dict = {"pixel_values": pixel_values}
        return config, inputs_dict

    def prepare_config_and_inputs_for_keras_fit(self):
        config_and_inputs = self.prepare_config_and_inputs()
        config, pixel_values, _, _ = config_and_inputs
        inputs_dict = {"pixel_values": pixel_values, "labels": tf.zeros((self.batch_size))}
        return config, inputs_dict


@require_tf
class TFData2VecVisionModelTest(TFModelTesterMixin, PipelineTesterMixin, unittest.TestCase):
    

    all_model_classes = (
        (TFData2VecVisionModel, TFData2VecVisionForImageClassification, TFData2VecVisionForSemanticSegmentation)
        if is_tf_available()
        else ()
    )
    pipeline_model_mapping = (
        {"feature-extraction": TFData2VecVisionModel, "image-classification": TFData2VecVisionForImageClassification}
        if is_tf_available()
        else {}
    )

    test_pruning = False
    test_onnx = False
    test_resize_embeddings = False
    test_head_masking = False

    def setUp(self):
        self.model_tester = TFData2VecVisionModelTester(self)
        self.config_tester = ConfigTester(
            self, config_class=Data2VecVisionConfig, has_text_modality=False, hidden_size=37
        )

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

    @unittest.skip(reason="Data2VecVision does not use inputs_embeds")
    def test_inputs_embeds(self):
        
        pass

    def test_model_common_attributes(self):
        config, _ = self.model_tester.prepare_config_and_inputs_for_common()

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

    def test_forward_signature(self):
        config, _ = self.model_tester.prepare_config_and_inputs_for_common()

        for model_class in self.all_model_classes:
            model = model_class(config)
            signature = inspect.signature(model.call)
            
            arg_names = [*signature.parameters.keys()]

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

    def test_model(self):
        config_and_inputs = self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_model(*config_and_inputs)

    def test_for_image_segmentation(self):
        config_and_inputs = self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_for_image_segmentation(*config_and_inputs)

    def test_attention_outputs(self):
        config, inputs_dict = self.model_tester.prepare_config_and_inputs_for_common()
        config.return_dict = True

        
        image_size = (
            self.model_tester.image_size
            if isinstance(self.model_tester.image_size, collections.abc.Iterable)
            else (self.model_tester.image_size, self.model_tester.image_size)
        )
        patch_size = (
            self.model_tester.patch_size
            if isinstance(self.model_tester.patch_size, collections.abc.Iterable)
            else (self.model_tester.patch_size, self.model_tester.patch_size)
        )
        num_patches = (image_size[1] // patch_size[1]) * (image_size[0] // patch_size[0])
        seq_len = num_patches + 1
        encoder_seq_length = getattr(self.model_tester, "encoder_seq_length", seq_len)
        encoder_key_length = getattr(self.model_tester, "key_length", encoder_seq_length)
        chunk_length = getattr(self.model_tester, "chunk_length", None)
        if chunk_length is not None and hasattr(self.model_tester, "num_hashes"):
            encoder_seq_length = encoder_seq_length * self.model_tester.num_hashes

        for model_class in self.all_model_classes:
            inputs_dict["output_attentions"] = True
            inputs_dict["output_hidden_states"] = False
            config.return_dict = True
            model = model_class(config)
            outputs = model(**self._prepare_for_class(inputs_dict, model_class), training=False)
            attentions = outputs.encoder_attentions if config.is_encoder_decoder else outputs.attentions
            self.assertEqual(len(attentions), self.model_tester.num_hidden_layers)

            
            del inputs_dict["output_attentions"]
            config.output_attentions = True
            model = model_class(config)
            outputs = model(**self._prepare_for_class(inputs_dict, model_class), training=False)
            attentions = outputs.encoder_attentions if config.is_encoder_decoder else outputs.attentions
            self.assertEqual(len(attentions), self.model_tester.num_hidden_layers)

            self.assertListEqual(
                list(attentions[0].shape[-3:]),
                [self.model_tester.num_attention_heads, encoder_seq_length, encoder_key_length],
            )
            out_len = len(outputs)

            
            inputs_dict["output_attentions"] = True
            inputs_dict["output_hidden_states"] = True
            model = model_class(config)
            outputs = model(**self._prepare_for_class(inputs_dict, model_class), training=False)

            self.assertEqual(out_len + 1, len(outputs))

            self_attentions = outputs.attentions

            self.assertEqual(len(self_attentions), self.model_tester.num_hidden_layers)
            self.assertListEqual(
                list(self_attentions[0].shape[-3:]),
                [self.model_tester.num_attention_heads, encoder_seq_length, encoder_key_length],
            )

    def test_hidden_states_output(self):
        def check_hidden_states_output(inputs_dict, config, model_class):
            model = model_class(config)

            outputs = model(**self._prepare_for_class(inputs_dict, model_class))

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

            expected_num_layers = getattr(
                self.model_tester, "expected_num_hidden_layers", self.model_tester.num_hidden_layers + 1
            )
            self.assertEqual(len(hidden_states), expected_num_layers)

            
            image_size = (
                self.model_tester.image_size
                if isinstance(self.model_tester.image_size, collections.abc.Iterable)
                else (self.model_tester.image_size, self.model_tester.image_size)
            )
            patch_size = (
                self.model_tester.patch_size
                if isinstance(self.model_tester.patch_size, collections.abc.Iterable)
                else (self.model_tester.patch_size, self.model_tester.patch_size)
            )
            num_patches = (image_size[1] // patch_size[1]) * (image_size[0] // patch_size[0])
            seq_length = num_patches + 1

            self.assertListEqual(
                list(hidden_states[0].shape[-2:]),
                [seq_length, self.model_tester.hidden_size],
            )

        config, inputs_dict = self.model_tester.prepare_config_and_inputs_for_common()

        for model_class in self.all_model_classes:
            inputs_dict["output_hidden_states"] = True
            check_hidden_states_output(inputs_dict, config, model_class)

            
            del inputs_dict["output_hidden_states"]
            config.output_hidden_states = True

            check_hidden_states_output(inputs_dict, config, model_class)

    
    @slow
    def test_keras_fit(self):
        config, _ = self.model_tester.prepare_config_and_inputs_for_common()
        for model_class in self.all_model_classes:
            
            if model_class.__name__ != "TFData2VecVisionModel":
                model = model_class(config)
                if getattr(model, "hf_compute_loss", None):
                    
                    _, prepared_for_class = self.model_tester.prepare_config_and_inputs_for_keras_fit()

                    label_names = {"labels"}
                    self.assertGreater(len(label_names), 0, msg="No matching label names found!")
                    labels = {key: val for key, val in prepared_for_class.items() if key in label_names}
                    inputs_minus_labels = {
                        key: val for key, val in prepared_for_class.items() if key not in label_names
                    }
                    self.assertGreater(len(inputs_minus_labels), 0)
                    model.compile(optimizer=tf.keras.optimizers.SGD(0.0), run_eagerly=True)

                    
                    history1 = model.fit(
                        prepared_for_class,
                        validation_data=prepared_for_class,
                        steps_per_epoch=1,
                        validation_steps=1,
                        shuffle=False,
                    )
                    val_loss1 = history1.history["val_loss"][0]
                    history2 = model.fit(
                        inputs_minus_labels,
                        labels,
                        validation_data=(inputs_minus_labels, labels),
                        steps_per_epoch=1,
                        validation_steps=1,
                        shuffle=False,
                    )
                    val_loss2 = history2.history["val_loss"][0]
                    self.assertTrue(np.allclose(val_loss1, val_loss2, atol=1e-2, rtol=1e-3))

    def check_pt_tf_outputs(self, tf_outputs, pt_outputs, model_class, tol=2e-4, name="outputs", attributes=None):
        
        super().check_pt_tf_outputs(tf_outputs, pt_outputs, model_class, tol, name, attributes)

    
    def test_loss_computation(self):
        config, inputs_dict = self.model_tester.prepare_config_and_inputs_for_common()
        for model_class in self.all_model_classes:
            
            
            if model_class.__name__ != "TFData2VecVisionModel":
                model = model_class(config)
                if getattr(model, "hf_compute_loss", None):
                    
                    _, prepared_for_class = self.model_tester.prepare_config_and_inputs_for_keras_fit()
                    added_label = prepared_for_class[
                        sorted(prepared_for_class.keys() - inputs_dict.keys(), reverse=True)[0]
                    ]
                    loss_size = tf.size(added_label)

                    
                    possible_input_names = {"input_ids", "pixel_values", "input_features"}
                    input_name = possible_input_names.intersection(set(prepared_for_class)).pop()
                    model_input = prepared_for_class.pop(input_name)

                    loss = model(model_input, **prepared_for_class)[0]
                    self.assertEqual(loss.shape, [loss_size])

                    
                    _, prepared_for_class = self.model_tester.prepare_config_and_inputs_for_keras_fit()
                    loss = model(**prepared_for_class)[0]
                    self.assertEqual(loss.shape, [loss_size])

                    
                    label_keys = prepared_for_class.keys() - inputs_dict.keys()
                    signature = inspect.signature(model.call).parameters
                    signature_names = list(signature.keys())

                    
                    tuple_index_mapping = {0: input_name}
                    for label_key in label_keys:
                        label_key_index = signature_names.index(label_key)
                        tuple_index_mapping[label_key_index] = label_key
                    sorted_tuple_index_mapping = sorted(tuple_index_mapping.items())
                    
                    list_input = []

                    for name in signature_names:
                        if name != "kwargs":
                            list_input.append(signature[name].default)

                    for index, value in sorted_tuple_index_mapping:
                        list_input[index] = prepared_for_class[value]

                    tuple_input = tuple(list_input)

                    
                    loss = model(tuple_input[:-1])[0]

                    self.assertEqual(loss.shape, [loss_size])

    def test_for_image_classification(self):
        config_and_inputs = self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_for_image_classification(*config_and_inputs)

    @slow
    def test_model_from_pretrained(self):
        for model_name in TF_DATA2VEC_VISION_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
            model = TFData2VecVisionModel.from_pretrained(model_name)
            self.assertIsNotNone(model)



def prepare_img():
    image = Image.open("./tests/fixtures/tests_samples/COCO/000000039769.png")
    return image


@require_tf
@require_vision
class TFData2VecVisionModelIntegrationTest(unittest.TestCase):
    @cached_property
    def default_image_processor(self):
        return (
            BeitImageProcessor.from_pretrained("facebook/data2vec-vision-base-ft1k") if is_vision_available() else None
        )

    @slow
    def test_inference_image_classification_head_imagenet_1k(self):
        model = TFData2VecVisionForImageClassification.from_pretrained("facebook/data2vec-vision-base-ft1k")

        image_processor = self.default_image_processor
        image = prepare_img()
        inputs = image_processor(images=image, return_tensors="tf")

        
        outputs = model(**inputs)
        logits = outputs.logits

        
        expected_shape = tf.convert_to_tensor([1, 1000])
        self.assertEqual(logits.shape, expected_shape)

        expected_slice = tf.convert_to_tensor([0.3277, -0.1395, 0.0911])

        tf.debugging.assert_near(logits[0, :3], expected_slice, atol=1e-4)

        expected_top2 = [model.config.label2id[i] for i in ["remote control, remote", "tabby, tabby cat"]]
        self.assertEqual(tf.nn.top_k(outputs.logits[0], 2).indices.numpy().tolist(), expected_top2)