




import copy
import inspect
import unittest

import numpy as np
from huggingface_hub import hf_hub_download

from transformers import VideoMAEConfig
from transformers.models.auto import get_values
from transformers.testing_utils import require_torch, require_vision, slow, torch_device
from transformers.utils import cached_property, is_torch_available, is_vision_available

from ...test_configuration_common import ConfigTester
from ...test_modeling_common import ModelTesterMixin, floats_tensor, ids_tensor
from ...test_pipeline_mixin import PipelineTesterMixin


if is_torch_available():
  import torch
  from torch import nn

  from transformers import (
      MODEL_FOR_VIDEO_CLASSIFICATION_MAPPING,
      VideoMAEForPreTraining,
      VideoMAEForVideoClassification,
      VideoMAEModel,
  )
  from transformers.models.videomae.modeling_videomae import VIDEOMAE_PRETRAINED_MODEL_ARCHIVE_LIST


if is_vision_available():
  from transformers import VideoMAEImageProcessor





class 				__lowerCAmelCase  :




  def __init__(       self							,     lowerCAmelCase							,     lowerCAmelCase=13							,     lowerCAmelCase=10							,     lowerCAmelCase=3							,     lowerCAmelCase=2							,     lowerCAmelCase=2							,     lowerCAmelCase=2							,     lowerCAmelCase=True							,     lowerCAmelCase=True							,     lowerCAmelCase=32							,     lowerCAmelCase=5							,     lowerCAmelCase=4							,     lowerCAmelCase=37							,     lowerCAmelCase="gelu"							,     lowerCAmelCase=0.1							,     lowerCAmelCase=0.1							,     lowerCAmelCase=10							,     lowerCAmelCase=0.02							,     lowerCAmelCase=0.9							,     lowerCAmelCase=None							,     )						->				Optional[int]:
        '''simple docstring'''






        _lowercase			 =parent
        _lowercase			 =batch_size
        _lowercase			 =image_size
        _lowercase			 =num_channels
        _lowercase			 =patch_size
        _lowercase			 =tubelet_size
        _lowercase			 =num_frames
        _lowercase			 =is_training
        _lowercase			 =use_labels
        _lowercase			 =hidden_size
        _lowercase			 =num_hidden_layers
        _lowercase			 =num_attention_heads
        _lowercase			 =intermediate_size
        _lowercase			 =hidden_act
        _lowercase			 =hidden_dropout_prob
        _lowercase			 =attention_probs_dropout_prob
        _lowercase			 =type_sequence_label_size
        _lowercase			 =initializer_range
        _lowercase			 =mask_ratio
        _lowercase			 =scope

        # in VideoMAE, the number of tokens equals num_frames/tubelet_size * num_patches per frame
        _lowercase			 =(image_size // patch_size) ** 2
        _lowercase			 =(num_frames // tubelet_size) * self.num_patches_per_frame

        # use this variable to define bool_masked_pos
        _lowercase			 =int(mask_ratio * self.seq_length      )




  def 						A__  (       self      )						->				Tuple:
        '''simple docstring'''






        _lowercase			 =floats_tensor(
            [self.batch_size, self.num_frames, self.num_channels, self.image_size, self.image_size]      )

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

        _lowercase			 =self.get_config()

        return config, pixel_values, labels




  def 						A__  (       self      )						->				List[str]:
        '''simple docstring'''






        return VideoMAEConfig(
            image_size=self.image_size							,     patch_size=self.patch_size							,     num_channels=self.num_channels							,     num_frames=self.num_frames							,     tubelet_size=self.tubelet_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							,     is_decoder=lowerCAmelCase							,     initializer_range=self.initializer_range							,     )




  def 						A__  (       self							,     lowerCAmelCase							,     lowerCAmelCase							,     lowerCAmelCase      )						->				Union[str, Any]:
        '''simple docstring'''






        _lowercase			 =VideoMAEModel(config=lowerCAmelCase      )
        model.to(lowerCAmelCase      )
        model.eval()
        _lowercase			 =model(lowerCAmelCase      )
        self.parent.assertEqual(result.last_hidden_state.shape							,     (self.batch_size, self.seq_length, self.hidden_size)      )




  def 						A__  (       self							,     lowerCAmelCase							,     lowerCAmelCase							,     lowerCAmelCase      )						->				Union[str, Any]:
        '''simple docstring'''






        _lowercase			 =VideoMAEForPreTraining(lowerCAmelCase      )
        model.to(lowerCAmelCase      )
        model.eval()
        # important: each video needs to have the same number of masked patches
        # hence we define a single mask, which we then repeat for each example in the batch
        _lowercase			 =torch.ones((self.num_masks,)      )
        _lowercase			 =torch.cat([mask, torch.zeros(self.seq_length - mask.size(0      )      )]      )
        _lowercase			 =mask.expand(self.batch_size							,     -1      ).bool()

        _lowercase			 =model(lowerCAmelCase							,     lowerCAmelCase      )
        # model only returns predictions for masked patches
        _lowercase			 =mask.sum().item()
        _lowercase			 =3 * self.tubelet_size * self.patch_size**2
        self.parent.assertEqual(result.logits.shape							,     (self.batch_size, num_masked_patches, decoder_num_labels)      )




  def 						A__  (       self      )						->				Optional[int]:
        '''simple docstring'''






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





@require_torch
class 				__lowerCAmelCase  (      UpperCamelCase_    ,    UpperCamelCase_    ,    unittest.TestCase							):
  _a		    =						(
      (VideoMAEModel, VideoMAEForPreTraining, VideoMAEForVideoClassification) if is_torch_available() else ()
  )
  _a		    =						(
      {"""feature-extraction""": VideoMAEModel, """video-classification""": VideoMAEForVideoClassification}
      if is_torch_available()
      else {}
  )

  _a		    =						False
  _a		    =						False
  _a		    =						False
  _a		    =						False




  def 						A__  (       self      )						->				Optional[int]:
        '''simple docstring'''






        _lowercase			 =VideoMAEModelTester(self      )
        _lowercase			 =ConfigTester(self							,     config_class=lowerCAmelCase							,     has_text_modality=lowerCAmelCase							,     hidden_size=37      )




  def 						A__  (       self							,     lowerCAmelCase							,     lowerCAmelCase							,     lowerCAmelCase=False      )						->				List[str]:
        '''simple docstring'''






        _lowercase			 =copy.deepcopy(lowerCAmelCase      )

        if model_class == VideoMAEForPreTraining:
              # important: each video needs to have the same number of masked patches
              # hence we define a single mask, which we then repeat for each example in the batch
              _lowercase			 =torch.ones((self.model_tester.num_masks,)      )
              _lowercase			 =torch.cat([mask, torch.zeros(self.model_tester.seq_length - mask.size(0      )      )]      )
              _lowercase			 =mask.expand(self.model_tester.batch_size							,     -1      ).bool()
              _lowercase			 =bool_masked_pos.to(lowerCAmelCase      )

        if return_labels:
              if model_class in [
                  *get_values(lowerCAmelCase      ),
              ]:
                    _lowercase			 =torch.zeros(
                        self.model_tester.batch_size							,     dtype=torch.long							,     device=lowerCAmelCase      )

        return inputs_dict




  def 						A__  (       self      )						->				Optional[Any]:
        '''simple docstring'''






        self.config_tester.run_common_tests()




  @unittest.skip(reason='VideoMAE does not use inputs_embeds'      )
  def 						A__  (       self      )						->				List[str]:
        '''simple docstring'''






        pass




  def 						A__  (       self      )						->				List[Any]:
        '''simple docstring'''






        _lowercase			 =self.model_tester.prepare_config_and_inputs_for_common()

        for model_class in self.all_model_classes:
              _lowercase			 =model_class(lowerCAmelCase      )
              self.assertIsInstance(model.get_input_embeddings()							,     (nn.Module)      )
              _lowercase			 =model.get_output_embeddings()
              self.assertTrue(x is None or isinstance(lowerCAmelCase							,     nn.Linear      )      )




  def 						A__  (       self      )						->				Union[str, Any]:
        '''simple docstring'''






        _lowercase			 =self.model_tester.prepare_config_and_inputs_for_common()

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

              _lowercase			 =['''pixel_values''']
              self.assertListEqual(arg_names[:1]							,     lowerCAmelCase      )




  def 						A__  (       self      )						->				Union[str, Any]:
        '''simple docstring'''






        _lowercase			 =self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_model(*lowerCAmelCase      )




  def 						A__  (       self      )						->				str:
        '''simple docstring'''






        _lowercase			 =self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_for_pretraining(*lowerCAmelCase      )




  @slow
  def 						A__  (       self      )						->				Optional[Any]:
        '''simple docstring'''






        for model_name in VIDEOMAE_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
              _lowercase			 =VideoMAEModel.from_pretrained(lowerCAmelCase      )
              self.assertIsNotNone(lowerCAmelCase      )




  def 						A__  (       self      )						->				int:
        '''simple docstring'''






        if not self.has_attentions:
              pass

        else:
              _lowercase			 =self.model_tester.prepare_config_and_inputs_for_common()
              _lowercase			 =True

              for model_class in self.all_model_classes:
                    _lowercase			 =self.model_tester.seq_length - self.model_tester.num_masks
                    _lowercase			 =(
                        num_visible_patches if model_class == VideoMAEForPreTraining else self.model_tester.seq_length
                    )

                    _lowercase			 =True
                    _lowercase			 =False
                    _lowercase			 =True
                    _lowercase			 =model_class(lowerCAmelCase      )
                    model.to(lowerCAmelCase      )
                    model.eval()
                    with torch.no_grad():
                          _lowercase			 =model(**self._prepare_for_class(lowerCAmelCase							,     lowerCAmelCase      )      )
                    _lowercase			 =outputs.attentions
                    self.assertEqual(len(lowerCAmelCase      )							,     self.model_tester.num_hidden_layers      )

                    # check that output_attentions also work using config
                    del inputs_dict["output_attentions"]
                    _lowercase			 =True
                    _lowercase			 =model_class(lowerCAmelCase      )
                    model.to(lowerCAmelCase      )
                    model.eval()
                    with torch.no_grad():
                          _lowercase			 =model(**self._prepare_for_class(lowerCAmelCase							,     lowerCAmelCase      )      )
                    _lowercase			 =outputs.attentions
                    self.assertEqual(len(lowerCAmelCase      )							,     self.model_tester.num_hidden_layers      )

                    self.assertListEqual(
                        list(attentions[0].shape[-3:]      )							,     [self.model_tester.num_attention_heads, seq_len, seq_len]							,     )
                    _lowercase			 =len(lowerCAmelCase      )

                    # Check attention is always last and order is fine
                    _lowercase			 =True
                    _lowercase			 =True
                    _lowercase			 =model_class(lowerCAmelCase      )
                    model.to(lowerCAmelCase      )
                    model.eval()
                    with torch.no_grad():
                          _lowercase			 =model(**self._prepare_for_class(lowerCAmelCase							,     lowerCAmelCase      )      )

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

                    _lowercase			 =outputs.attentions

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




  def 						A__  (       self      )						->				Tuple:
        '''simple docstring'''






        def check_hidden_states_output(lowerCAmelCase							,     lowerCAmelCase							,     lowerCAmelCase      ):
              _lowercase			 =model_class(lowerCAmelCase      )
              model.to(lowerCAmelCase      )
              model.eval()

              with torch.no_grad():
                    _lowercase			 =model(**self._prepare_for_class(lowerCAmelCase							,     lowerCAmelCase      )      )

              _lowercase			 =outputs.hidden_states
              _lowercase			 =self.model_tester.num_hidden_layers + 1
              self.assertEqual(len(lowerCAmelCase      )							,     lowerCAmelCase      )

              _lowercase			 =self.model_tester.seq_length - self.model_tester.num_masks
              _lowercase			 =num_visible_patches if model_class == VideoMAEForPreTraining else self.model_tester.seq_length

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

        _lowercase			 =self.model_tester.prepare_config_and_inputs_for_common()

        for model_class in self.all_model_classes:
              _lowercase			 =True
              check_hidden_states_output(lowerCAmelCase							,     lowerCAmelCase							,     lowerCAmelCase      )

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

              check_hidden_states_output(lowerCAmelCase							,     lowerCAmelCase							,     lowerCAmelCase      )




  @unittest.skip('Will be fixed soon by reducing the size of the model used for common tests.'      )
  def 						A__  (       self      )						->				str:
        '''simple docstring'''






        pass
def   a   (    )					->   Tuple:





      """simple docstring"""



      _lowercase			 =hf_hub_download(
          repo_id='hf-internal-testing/spaghetti-video'   ,     filename='eating_spaghetti.npy'   ,     repo_type='dataset'  )
      _lowercase			 =np.load(snake_case_  )
      return list(snake_case_  )







@require_torch
@require_vision
class 				__lowerCAmelCase  (      unittest.TestCase							):




  @cached_property
  def 						A__  (       self      )						->				List[str]:
        '''simple docstring'''






        return (
            VideoMAEImageProcessor(image_mean=[0.5, 0.5, 0.5]							,     image_std=[0.5, 0.5, 0.5]      )
            if is_vision_available()
            else None
        )




  @slow
  def 						A__  (       self      )						->				Optional[Any]:
        '''simple docstring'''






        _lowercase			 =VideoMAEForVideoClassification.from_pretrained('MCG-NJU/videomae-base-finetuned-kinetics'      ).to(
            lowerCAmelCase      )

        _lowercase			 =self.default_image_processor
        _lowercase			 =prepare_video()
        _lowercase			 =image_processor(lowerCAmelCase							,     return_tensors='pt'      ).to(lowerCAmelCase      )

        # forward pass
        with torch.no_grad():
              _lowercase			 =model(**lowerCAmelCase      )

        # verify the logits
        _lowercase			 =torch.Size((1, 400)      )
        self.assertEqual(outputs.logits.shape							,     lowerCAmelCase      )

        _lowercase			 =torch.tensor([0.3669, -0.0688, -0.2421]      ).to(lowerCAmelCase      )

        self.assertTrue(torch.allclose(outputs.logits[0, :3]							,     lowerCAmelCase							,     atol=1e-4      )      )




  @slow
  def 						A__  (       self      )						->				List[str]:
        '''simple docstring'''






        _lowercase			 =VideoMAEForPreTraining.from_pretrained('MCG-NJU/videomae-base-short'      ).to(lowerCAmelCase      )

        _lowercase			 =self.default_image_processor
        _lowercase			 =prepare_video()
        _lowercase			 =image_processor(lowerCAmelCase							,     return_tensors='pt'      ).to(lowerCAmelCase      )

        # add boolean mask, indicating which patches to mask
        _lowercase			 =hf_hub_download(repo_id='hf-internal-testing/bool-masked-pos'							,     filename='bool_masked_pos.pt'      )
        _lowercase			 =torch.load(lowerCAmelCase      )

        # forward pass
        with torch.no_grad():
              _lowercase			 =model(**lowerCAmelCase      )

        # verify the logits
        _lowercase			 =torch.Size([1, 1_408, 1_536]      )
        _lowercase			 =torch.tensor(
            [[0.7994, 0.9612, 0.8508], [0.7401, 0.8958, 0.8302], [0.5862, 0.7468, 0.7325]]							,     device=lowerCAmelCase      )
        self.assertEqual(outputs.logits.shape							,     lowerCAmelCase      )
        self.assertTrue(torch.allclose(outputs.logits[0, :3, :3]							,     lowerCAmelCase							,     atol=1e-4      )      )

        # verify the loss (`config.norm_pix_loss` = `True`)
        _lowercase			 =torch.tensor([0.5142]							,     device=lowerCAmelCase      )
        self.assertTrue(torch.allclose(outputs.loss							,     lowerCAmelCase							,     atol=1e-4      )      )

        # verify the loss (`config.norm_pix_loss` = `False`)
        _lowercase			 =VideoMAEForPreTraining.from_pretrained('MCG-NJU/videomae-base-short'							,     norm_pix_loss=lowerCAmelCase      ).to(
            lowerCAmelCase      )

        with torch.no_grad():
              _lowercase			 =model(**lowerCAmelCase      )

        _lowercase			 =torch.tensor(torch.tensor([0.6469]      )							,     device=lowerCAmelCase      )
        self.assertTrue(torch.allclose(outputs.loss							,     lowerCAmelCase							,     atol=1e-4      )      )





from __future__ import annotations

import collections
import tempfile
import unittest

import numpy as np

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

from ...test_modeling_tf_common import floats_tensor, ids_tensor, random_attention_mask
from ..bert.test_modeling_tf_bert import TFBertModelTester
from ..clip.test_modeling_tf_clip import TFCLIPVisionModelTester
from ..deit.test_modeling_tf_deit import TFDeiTModelTester
from ..roberta.test_modeling_tf_roberta import TFRobertaModelTester
from ..vit.test_modeling_tf_vit import TFViTModelTester


if is_tf_available():
  from transformers import (
      TFBertModel,
      TFCLIPVisionModel,
      TFDeiTModel,
      TFRobertaModel,
      TFVisionTextDualEncoderModel,
      TFViTModel,
      VisionTextDualEncoderConfig,
  )

if is_vision_available():
  from PIL import Image

  from transformers import VisionTextDualEncoderProcessor
def   a   (    A__			:     List[Any]  )					->   Union[str, Any]:





      """simple docstring"""



      if isinstance(A__   ,     collections.abc.Iterable  ):
            return x
      return (x, x)





@require_tf
class 				__lowerCAmelCase  :




  def 						A__  (       self							,     lowerCAmelCase							,     lowerCAmelCase      )						->				List[Any]:
        '''simple docstring'''






        pass




  def 						A__  (       self      )						->				str:
        '''simple docstring'''






        pass




  def 						A__  (       self      )						->				Optional[Any]:
        '''simple docstring'''






        pass




  def 						A__  (       self							,     lowerCAmelCase							,     lowerCAmelCase							,     lowerCAmelCase							,     lowerCAmelCase							,     lowerCAmelCase=None							,     **lowerCAmelCase      )						->				Optional[Any]:
        '''simple docstring'''






        _lowercase			 =VisionTextDualEncoderConfig.from_vision_text_configs(lowerCAmelCase							,     lowerCAmelCase      )

        _lowercase			 =TFVisionTextDualEncoderModel(lowerCAmelCase      )

        _lowercase			 =model(input_ids=lowerCAmelCase							,     pixel_values=lowerCAmelCase							,     attention_mask=lowerCAmelCase      )

        self.assertEqual(output['text_embeds'].shape							,     (input_ids.shape[0], config.projection_dim)      )
        self.assertEqual(output['image_embeds'].shape							,     (pixel_values.shape[0], config.projection_dim)      )




  def 						A__  (       self							,     lowerCAmelCase							,     lowerCAmelCase							,     lowerCAmelCase							,     lowerCAmelCase							,     lowerCAmelCase=None							,     **lowerCAmelCase      )						->				int:
        '''simple docstring'''






        _lowercase , _lowercase			 =self.get_vision_text_model(lowerCAmelCase							,     lowerCAmelCase      )
        _lowercase			 =TFVisionTextDualEncoderModel(vision_model=lowerCAmelCase							,     text_model=lowerCAmelCase      )

        _lowercase			 =model(input_ids=lowerCAmelCase							,     pixel_values=lowerCAmelCase							,     attention_mask=lowerCAmelCase      )

        self.assertEqual(output['text_embeds'].shape							,     (input_ids.shape[0], model.config.projection_dim)      )
        self.assertEqual(output['image_embeds'].shape							,     (pixel_values.shape[0], model.config.projection_dim)      )




  def 						A__  (       self							,     lowerCAmelCase							,     lowerCAmelCase							,     lowerCAmelCase							,     lowerCAmelCase							,     lowerCAmelCase=None							,     **lowerCAmelCase      )						->				Union[str, Any]:
        '''simple docstring'''






        _lowercase , _lowercase			 =self.get_vision_text_model(lowerCAmelCase							,     lowerCAmelCase      )
        _lowercase			 ={'vision_model': vision_model, 'text_model': text_model}
        _lowercase			 =TFVisionTextDualEncoderModel.from_vision_text_pretrained(**lowerCAmelCase      )

        _lowercase			 =model(input_ids=lowerCAmelCase							,     pixel_values=lowerCAmelCase							,     attention_mask=lowerCAmelCase      )

        self.assertEqual(output['text_embeds'].shape							,     (input_ids.shape[0], model.config.projection_dim)      )
        self.assertEqual(output['image_embeds'].shape							,     (pixel_values.shape[0], model.config.projection_dim)      )




  def 						A__  (       self							,     lowerCAmelCase							,     lowerCAmelCase							,     lowerCAmelCase							,     lowerCAmelCase							,     lowerCAmelCase=None							,     **lowerCAmelCase      )						->				int:
        '''simple docstring'''






        _lowercase , _lowercase			 =self.get_vision_text_model(lowerCAmelCase							,     lowerCAmelCase      )
        _lowercase			 =TFVisionTextDualEncoderModel(vision_model=lowerCAmelCase							,     text_model=lowerCAmelCase      )

        _lowercase			 =model(input_ids=lowerCAmelCase							,     pixel_values=lowerCAmelCase							,     attention_mask=lowerCAmelCase      )
        _lowercase			 =output[0].numpy()

        with tempfile.TemporaryDirectory() as tmpdirname:
              model.save_pretrained(lowerCAmelCase      )
              _lowercase			 =TFVisionTextDualEncoderModel.from_pretrained(lowerCAmelCase      )

              _lowercase			 =model(input_ids=lowerCAmelCase							,     pixel_values=lowerCAmelCase							,     attention_mask=lowerCAmelCase      )
              _lowercase			 =after_output[0].numpy()
              _lowercase			 =np.amax(np.abs(out_a - out_a      )      )
              self.assertLessEqual(lowerCAmelCase							,     1e-5      )




  def 						A__  (       self							,     lowerCAmelCase							,     lowerCAmelCase							,     lowerCAmelCase							,     lowerCAmelCase							,     lowerCAmelCase=None							,     **lowerCAmelCase      )						->				Optional[int]:
        '''simple docstring'''






        _lowercase , _lowercase			 =self.get_vision_text_model(lowerCAmelCase							,     lowerCAmelCase      )
        _lowercase			 =TFVisionTextDualEncoderModel(vision_model=lowerCAmelCase							,     text_model=lowerCAmelCase      )

        _lowercase			 =model(
            input_ids=lowerCAmelCase							,     pixel_values=lowerCAmelCase							,     attention_mask=lowerCAmelCase							,     output_attentions=lowerCAmelCase      )

        _lowercase			 =output.vision_model_output.attentions
        self.assertEqual(len(lowerCAmelCase      )							,     vision_config.num_hidden_layers      )

        # in ViT, the seq_len equals the number of patches + 1 (we add 1 for the [CLS] token)
        _lowercase			 =to_atuple(vision_model.config.image_size      )
        _lowercase			 =to_atuple(vision_model.config.patch_size      )
        _lowercase			 =(image_size[1] // patch_size[1]) * (image_size[0] // patch_size[0])
        _lowercase			 =num_patches + 1
        self.assertEqual(vision_attentions[0].shape[-3:]							,     (vision_config.num_attention_heads, seq_len, seq_len)      )

        _lowercase			 =output.text_model_output.attentions
        self.assertEqual(len(lowerCAmelCase      )							,     text_config.num_hidden_layers      )

        self.assertEqual(
            text_attentions[0].shape[-3:]							,     (text_config.num_attention_heads, input_ids.shape[-1], input_ids.shape[-1])							,     )




  def 						A__  (       self							,     lowerCAmelCase							,     lowerCAmelCase							,     lowerCAmelCase      )						->				Optional[Any]:
        '''simple docstring'''






        _lowercase			 =np.abs((a - b)      ).max()
        self.assertLessEqual(lowerCAmelCase							,     lowerCAmelCase							,     F'''Difference between torch and flax is {diff} (>= {tol}).'''      )




  def 						A__  (       self      )						->				str:
        '''simple docstring'''






        _lowercase			 =self.prepare_config_and_inputs()
        self.check_vision_text_dual_encoder_model(**lowerCAmelCase      )




  def 						A__  (       self      )						->				int:
        '''simple docstring'''






        _lowercase			 =self.prepare_config_and_inputs()
        self.check_model_from_pretrained_configs(**lowerCAmelCase      )




  def 						A__  (       self      )						->				Union[str, Any]:
        '''simple docstring'''






        _lowercase			 =self.prepare_config_and_inputs()
        self.check_vision_text_dual_encoder_from_pretrained(**lowerCAmelCase      )




  def 						A__  (       self      )						->				List[Any]:
        '''simple docstring'''






        _lowercase			 =self.prepare_config_and_inputs()
        self.check_save_load(**lowerCAmelCase      )




  def 						A__  (       self      )						->				Tuple:
        '''simple docstring'''






        _lowercase			 =self.prepare_config_and_inputs()
        self.check_vision_text_output_attention(**lowerCAmelCase      )




  @slow
  def 						A__  (       self      )						->				Union[str, Any]:
        '''simple docstring'''






        _lowercase , _lowercase			 =self.get_pretrained_model_and_inputs()

        _lowercase			 =model_a(**lowerCAmelCase      )
        _lowercase			 =outputs[0].numpy()

        with tempfile.TemporaryDirectory() as tmp_dirname:
              model_a.save_pretrained(lowerCAmelCase      )
              _lowercase			 =TFVisionTextDualEncoderModel.from_pretrained(lowerCAmelCase      )

              _lowercase			 =model_a(**lowerCAmelCase      )
              _lowercase			 =after_outputs[0].numpy()
              _lowercase			 =np.amax(np.abs(out_a - out_a      )      )
              self.assertLessEqual(lowerCAmelCase							,     1e-5      )





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




  def 						A__  (       self      )						->				List[Any]:
        '''simple docstring'''






        _lowercase			 =TFVisionTextDualEncoderModel.from_vision_text_pretrained(
            'hf-internal-testing/tiny-random-vit'							,     'hf-internal-testing/tiny-random-bert'      )
        _lowercase			 =13
        _lowercase			 =floats_tensor(
            [
                batch_size,
                model.vision_model.config.num_channels,
                model.vision_model.config.image_size,
                model.vision_model.config.image_size,
            ]      )
        _lowercase			 =ids_tensor([batch_size, 4]							,     model.text_model.config.vocab_size      )
        _lowercase			 =random_attention_mask([batch_size, 4]      )
        _lowercase			 ={'pixel_values': pixel_values, 'input_ids': input_ids, 'attention_mask': attention_mask}

        return model, inputs




  def 						A__  (       self							,     lowerCAmelCase							,     lowerCAmelCase      )						->				List[Any]:
        '''simple docstring'''






        _lowercase			 =TFViTModel(lowerCAmelCase							,     name='vision_model'      )
        _lowercase			 =TFBertModel(lowerCAmelCase							,     name='text_model'      )
        return vision_model, text_model




  def 						A__  (       self      )						->				Any:
        '''simple docstring'''






        _lowercase			 =TFViTModelTester(self      )
        _lowercase			 =TFBertModelTester(self      )
        _lowercase			 =vit_model_tester.prepare_config_and_inputs()
        _lowercase			 =bert_model_tester.prepare_config_and_inputs()

        _lowercase , _lowercase , _lowercase			 =vision_config_and_inputs

        (
            (
            _lowercase
        ) , (
            _lowercase
        ) , (
            _lowercase
        ) , (
            _lowercase
        ) , (
            _lowercase
        ) , (
            _lowercase
        ) , (
            _lowercase
        ) , 
        )			 =text_config_and_inputs

        return {
            "text_config": text_config,
            "vision_config": vision_config,
            "pixel_values": pixel_values,
            "attention_mask": input_mask,
            "input_ids": input_ids,
            "text_token_type_ids": token_type_ids,
            "text_sequence_labels": sequence_labels,
            "text_token_labels": token_labels,
            "text_choice_labels": choice_labels,
        }





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




  def 						A__  (       self      )						->				str:
        '''simple docstring'''






        _lowercase			 =TFVisionTextDualEncoderModel.from_vision_text_pretrained(
            'Rocketknight1/tiny-random-deit-tf'							,     'hf-internal-testing/tiny-random-roberta'      )
        _lowercase			 =13
        _lowercase			 =floats_tensor(
            [
                batch_size,
                model.vision_model.config.num_channels,
                model.vision_model.config.image_size,
                model.vision_model.config.image_size,
            ]      )
        _lowercase			 =ids_tensor([batch_size, 4]							,     model.text_model.config.vocab_size      )
        _lowercase			 =random_attention_mask([batch_size, 4]      )
        _lowercase			 ={'pixel_values': pixel_values, 'input_ids': input_ids, 'attention_mask': attention_mask}

        return model, inputs




  def 						A__  (       self							,     lowerCAmelCase							,     lowerCAmelCase							,     lowerCAmelCase							,     lowerCAmelCase							,     lowerCAmelCase=None							,     **lowerCAmelCase      )						->				List[str]:
        '''simple docstring'''






        _lowercase , _lowercase			 =self.get_vision_text_model(lowerCAmelCase							,     lowerCAmelCase      )
        _lowercase			 =TFVisionTextDualEncoderModel(vision_model=lowerCAmelCase							,     text_model=lowerCAmelCase      )

        _lowercase			 =model(
            input_ids=lowerCAmelCase							,     pixel_values=lowerCAmelCase							,     attention_mask=lowerCAmelCase							,     output_attentions=lowerCAmelCase      )

        _lowercase			 =output.vision_model_output.attentions
        self.assertEqual(len(lowerCAmelCase      )							,     vision_config.num_hidden_layers      )

        # in DEiT, the seq_len equals the number of patches + 2 (we add 2 for the [CLS] and distillation tokens)
        _lowercase			 =to_atuple(vision_model.config.image_size      )
        _lowercase			 =to_atuple(vision_model.config.patch_size      )
        _lowercase			 =(image_size[1] // patch_size[1]) * (image_size[0] // patch_size[0])
        _lowercase			 =num_patches + 2
        self.assertEqual(vision_attentions[0].shape[-3:]							,     (vision_config.num_attention_heads, seq_len, seq_len)      )

        _lowercase			 =output.text_model_output.attentions
        self.assertEqual(len(lowerCAmelCase      )							,     text_config.num_hidden_layers      )

        self.assertEqual(
            text_attentions[0].shape[-3:]							,     (text_config.num_attention_heads, input_ids.shape[-1], input_ids.shape[-1])							,     )




  def 						A__  (       self							,     lowerCAmelCase							,     lowerCAmelCase      )						->				Tuple:
        '''simple docstring'''






        _lowercase			 =TFDeiTModel(lowerCAmelCase							,     name='vision_model'      )
        _lowercase			 =TFRobertaModel(lowerCAmelCase							,     name='text_model'      )
        return vision_model, text_model




  def 						A__  (       self      )						->				Any:
        '''simple docstring'''






        _lowercase			 =TFDeiTModelTester(self      )
        _lowercase			 =TFRobertaModelTester(self      )
        _lowercase			 =vit_model_tester.prepare_config_and_inputs()
        _lowercase			 =bert_model_tester.prepare_config_and_inputs()

        _lowercase , _lowercase , _lowercase			 =vision_config_and_inputs

        (
            (
            _lowercase
        ) , (
            _lowercase
        ) , (
            _lowercase
        ) , (
            _lowercase
        ) , (
            _lowercase
        ) , (
            _lowercase
        ) , (
            _lowercase
        ) , 
        )			 =text_config_and_inputs

        return {
            "text_config": text_config,
            "vision_config": vision_config,
            "pixel_values": pixel_values,
            "attention_mask": input_mask,
            "input_ids": input_ids,
            "text_token_type_ids": token_type_ids,
            "text_sequence_labels": sequence_labels,
            "text_token_labels": token_labels,
            "text_choice_labels": choice_labels,
        }





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




  def 						A__  (       self      )						->				str:
        '''simple docstring'''






        _lowercase			 =TFVisionTextDualEncoderModel.from_vision_text_pretrained(
            'Rocketknight1/tiny-random-clip-tf'							,     'hf-internal-testing/tiny-random-bert'      )
        _lowercase			 =13
        _lowercase			 =floats_tensor(
            [
                batch_size,
                model.vision_model.config.num_channels,
                model.vision_model.config.image_size,
                model.vision_model.config.image_size,
            ]      )
        _lowercase			 =ids_tensor([batch_size, 4]							,     model.text_model.config.vocab_size      )
        _lowercase			 =random_attention_mask([batch_size, 4]      )
        _lowercase			 ={'pixel_values': pixel_values, 'input_ids': input_ids, 'attention_mask': attention_mask}

        return model, inputs




  def 						A__  (       self							,     lowerCAmelCase							,     lowerCAmelCase      )						->				Tuple:
        '''simple docstring'''






        _lowercase			 =TFCLIPVisionModel(lowerCAmelCase							,     name='vision_model'      )
        _lowercase			 =TFBertModel(lowerCAmelCase							,     name='text_model'      )
        return vision_model, text_model




  def 						A__  (       self      )						->				str:
        '''simple docstring'''






        _lowercase			 =TFCLIPVisionModelTester(self      )
        _lowercase			 =TFBertModelTester(self      )
        _lowercase			 =clip_model_tester.prepare_config_and_inputs()
        _lowercase			 =bert_model_tester.prepare_config_and_inputs()

        _lowercase , _lowercase			 =vision_config_and_inputs

        (
            (
            _lowercase
        ) , (
            _lowercase
        ) , (
            _lowercase
        ) , (
            _lowercase
        ) , (
            _lowercase
        ) , (
            _lowercase
        ) , (
            _lowercase
        ) , 
        )			 =text_config_and_inputs

        return {
            "text_config": text_config,
            "vision_config": vision_config,
            "pixel_values": pixel_values,
            "attention_mask": input_mask,
            "input_ids": input_ids,
            "text_token_type_ids": token_type_ids,
            "text_sequence_labels": sequence_labels,
            "text_token_labels": token_labels,
            "text_choice_labels": choice_labels,
        }





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




  @slow
  def 						A__  (       self      )						->				Optional[int]:
        '''simple docstring'''






        _lowercase			 =TFVisionTextDualEncoderModel.from_pretrained(
            'clip-italian/clip-italian'							,     logit_scale_init_value=1.0							,     from_pt=lowerCAmelCase      )
        _lowercase			 =VisionTextDualEncoderProcessor.from_pretrained('clip-italian/clip-italian'      )

        _lowercase			 =Image.open('./tests/fixtures/tests_samples/COCO/000000039769.png'      )
        _lowercase			 =processor(
            text=['una foto di un gatto', 'una foto di un cane']							,     images=lowerCAmelCase							,     padding=lowerCAmelCase							,     return_tensors='np'      )

        _lowercase			 =model(**lowerCAmelCase      )

        # verify the logits
        self.assertEqual(outputs.logits_per_image.shape							,     (inputs.pixel_values.shape[0], inputs.input_ids.shape[0])      )
        self.assertEqual(
            outputs.logits_per_text.shape							,     (inputs.input_ids.shape[0], inputs.pixel_values.shape[0])							,     )

        _lowercase			 =np.array([[1.2284727, 0.3104122]]      )

        self.assertTrue(np.allclose(outputs.logits_per_image.numpy()							,     lowerCAmelCase							,     atol=1e-3      )      )


