





'''simple docstring'''


from typing import Optional, Union

import torch
from torch import nn
from torch.nn import BCEWithLogitsLoss, CrossEntropyLoss, MSELoss

from ...activations import ACTaFN
from ...modeling_outputs import BaseModelOutputWithPoolingAndNoAttention, ImageClassifierOutputWithNoAttention
from ...modeling_utils import PreTrainedModel
from ...utils import add_code_sample_docstrings, add_start_docstrings, add_start_docstrings_to_model_forward, logging
from .configuration_mobilenet_va import MobileNetVaConfig


_SCREAMING_SNAKE_CASE	 =							logging.get_logger(__name__)


# General docstring
_SCREAMING_SNAKE_CASE	 =							"MobileNetV1Config"

# Base docstring
_SCREAMING_SNAKE_CASE	 =							"google/mobilenet_v1_1.0_224"
_SCREAMING_SNAKE_CASE	 =							[1, 10_24, 7, 7]

# Image classification docstring
_SCREAMING_SNAKE_CASE	 =							"google/mobilenet_v1_1.0_224"
_SCREAMING_SNAKE_CASE	 =							"tabby, tabby cat"


_SCREAMING_SNAKE_CASE	 =							[
    "google/mobilenet_v1_1.0_224",
    "google/mobilenet_v1_0.75_192",
    # See all MobileNetV1 models at https://huggingface.co/models?filter=mobilenet_v1
]


def 	__a(SCREAMING_SNAKE_CASE_  :					Dict		,       SCREAMING_SNAKE_CASE_  :					int		,       SCREAMING_SNAKE_CASE_  :					Optional[Any]=None    ):



    '''simple docstring'''





    _lowerCAmelCase				=      {}

    if isinstance(SCREAMING_SNAKE_CASE_		,       SCREAMING_SNAKE_CASE_    ):
        _lowerCAmelCase				=      model.mobilenet_va
    else:
        _lowerCAmelCase				=      model

    _lowerCAmelCase				=      "MobilenetV1/Conv2d_0/"
    _lowerCAmelCase				=      backbone.conv_stem.convolution.weight
    _lowerCAmelCase				=      backbone.conv_stem.normalization.bias
    _lowerCAmelCase				=      backbone.conv_stem.normalization.weight
    _lowerCAmelCase				=      backbone.conv_stem.normalization.running_mean
    _lowerCAmelCase				=      backbone.conv_stem.normalization.running_var

    for i in range(13    ):
        _lowerCAmelCase				=      i + 1
        _lowerCAmelCase				=      i * 2

        _lowerCAmelCase				=      backbone.layer[pt_index]
        _lowerCAmelCase				=      F'''MobilenetV1/Conv2d_{tf_index}_depthwise/'''
        _lowerCAmelCase				=      pointer.convolution.weight
        _lowerCAmelCase				=      pointer.normalization.bias
        _lowerCAmelCase				=      pointer.normalization.weight
        _lowerCAmelCase				=      pointer.normalization.running_mean
        _lowerCAmelCase				=      pointer.normalization.running_var

        _lowerCAmelCase				=      backbone.layer[pt_index + 1]
        _lowerCAmelCase				=      F'''MobilenetV1/Conv2d_{tf_index}_pointwise/'''
        _lowerCAmelCase				=      pointer.convolution.weight
        _lowerCAmelCase				=      pointer.normalization.bias
        _lowerCAmelCase				=      pointer.normalization.weight
        _lowerCAmelCase				=      pointer.normalization.running_mean
        _lowerCAmelCase				=      pointer.normalization.running_var

    if isinstance(SCREAMING_SNAKE_CASE_		,       SCREAMING_SNAKE_CASE_    ):
        _lowerCAmelCase				=      "MobilenetV1/Logits/Conv2d_1c_1x1/"
        _lowerCAmelCase				=      model.classifier.weight
        _lowerCAmelCase				=      model.classifier.bias

    return tf_to_pt_map


def 	__a(SCREAMING_SNAKE_CASE_  :					str		,       SCREAMING_SNAKE_CASE_  :					Any		,       SCREAMING_SNAKE_CASE_  :					Any    ):



    '''simple docstring'''





    try:
        import numpy as np
        import tensorflow as tf
    except ImportError:
        logger.error(
            "Loading a TensorFlow models in PyTorch, requires TensorFlow to be installed. Please see "
            "https://www.tensorflow.org/install/ for installation instructions."    )
        raise

    # Load weights from TF model
    _lowerCAmelCase				=      tf.train.list_variables(SCREAMING_SNAKE_CASE_    )
    _lowerCAmelCase				=      {}
    for name, shape in init_vars:
        logger.info(F'''Loading TF weight {name} with shape {shape}'''    )
        _lowerCAmelCase				=      tf.train.load_variable(SCREAMING_SNAKE_CASE_		,       SCREAMING_SNAKE_CASE_    )
        _lowerCAmelCase				=      array

    # Build TF to PyTorch weights loading map
    _lowerCAmelCase				=      _build_tf_to_pytorch_map(SCREAMING_SNAKE_CASE_		,       SCREAMING_SNAKE_CASE_		,       SCREAMING_SNAKE_CASE_    )

    for name, pointer in tf_to_pt_map.items():
        logger.info(F'''Importing {name}'''    )
        if name not in tf_weights:
            logger.info(F'''{name} not in tf pre-trained weights, skipping'''    )
            continue

        _lowerCAmelCase				=      tf_weights[name]

        if "depthwise_weights" in name:
            logger.info("Transposing depthwise"    )
            _lowerCAmelCase				=      np.transpose(SCREAMING_SNAKE_CASE_		,       (2, 3, 0, 1)    )
        elif "weights" in name:
            logger.info("Transposing"    )
            if len(pointer.shape    ) == 2:  # copying into linear layer
                _lowerCAmelCase				=      array.squeeze().transpose()
            else:
                _lowerCAmelCase				=      np.transpose(SCREAMING_SNAKE_CASE_		,       (3, 2, 0, 1)    )

        if pointer.shape != array.shape:
            raise ValueError(F'''Pointer shape {pointer.shape} and array shape {array.shape} mismatched'''    )

        logger.info(F'''Initialize PyTorch weight {name} {array.shape}'''    )
        _lowerCAmelCase				=      torch.from_numpy(SCREAMING_SNAKE_CASE_    )

        tf_weights.pop(SCREAMING_SNAKE_CASE_		,       SCREAMING_SNAKE_CASE_    )
        tf_weights.pop(name + "/RMSProp"		,       SCREAMING_SNAKE_CASE_    )
        tf_weights.pop(name + "/RMSProp_1"		,       SCREAMING_SNAKE_CASE_    )
        tf_weights.pop(name + "/ExponentialMovingAverage"		,       SCREAMING_SNAKE_CASE_    )

    logger.info(F'''Weights not copied to PyTorch model: {', '.join(tf_weights.keys()    )}'''    )
    return model


def 	__a(SCREAMING_SNAKE_CASE_  :					torch.Tensor		,       SCREAMING_SNAKE_CASE_  :					nn.Convad    ):



    '''simple docstring'''





    _lowerCAmelCase			,			_lowerCAmelCase				=      features.shape[-2:]
    _lowerCAmelCase			,			_lowerCAmelCase				=      conv_layer.stride
    _lowerCAmelCase			,			_lowerCAmelCase				=      conv_layer.kernel_size

    if in_height % stride_height == 0:
        _lowerCAmelCase				=      max(kernel_height - stride_height		,       0    )
    else:
        _lowerCAmelCase				=      max(kernel_height - (in_height % stride_height)		,       0    )

    if in_width % stride_width == 0:
        _lowerCAmelCase				=      max(kernel_width - stride_width		,       0    )
    else:
        _lowerCAmelCase				=      max(kernel_width - (in_width % stride_width)		,       0    )

    _lowerCAmelCase				=      pad_along_width // 2
    _lowerCAmelCase				=      pad_along_width - pad_left
    _lowerCAmelCase				=      pad_along_height // 2
    _lowerCAmelCase				=      pad_along_height - pad_top

    _lowerCAmelCase				=      (pad_left, pad_right, pad_top, pad_bottom)
    return nn.functional.pad(SCREAMING_SNAKE_CASE_		,       SCREAMING_SNAKE_CASE_		,       "constant"		,       0.0    )

class    lowerCAmelCase_		(		nn.Module		):




 def __init__(	self			,     _lowerCAmelCase			,     _lowerCAmelCase			,     _lowerCAmelCase			,     _lowerCAmelCase			,     _lowerCAmelCase = 1			,     _lowerCAmelCase = 1			,     _lowerCAmelCase = False			,     _lowerCAmelCase = True			,     _lowerCAmelCase = True			,     )    ->							None:
     super().__init__()
     _lowerCAmelCase				=      config

     if in_channels % groups != 0:
         raise ValueError(f'''Input channels ({in_channels}) are not divisible by {groups} groups.'''		)
     if out_channels % groups != 0:
         raise ValueError(f'''Output channels ({out_channels}) are not divisible by {groups} groups.'''		)

     _lowerCAmelCase				=      0 if config.tf_padding else int((kernel_size - 1) / 2		)

     _lowerCAmelCase				=      nn.Convad(
         in_channels=_lowerCAmelCase			,     out_channels=_lowerCAmelCase			,     kernel_size=_lowerCAmelCase			,     stride=_lowerCAmelCase			,     padding=_lowerCAmelCase			,     groups=_lowerCAmelCase			,     bias=_lowerCAmelCase			,     padding_mode="zeros"			,     )

     if use_normalization:
         _lowerCAmelCase				=      nn.BatchNormad(
             num_features=_lowerCAmelCase			,     eps=config.layer_norm_eps			,     momentum=0.9997			,     affine=_lowerCAmelCase			,     track_running_stats=_lowerCAmelCase			,     )
     else:
         _lowerCAmelCase				=      None

     if use_activation:
         if isinstance(_lowerCAmelCase			,     _lowerCAmelCase		):
             _lowerCAmelCase				=      ACTaFN[use_activation]
         elif isinstance(config.hidden_act			,     _lowerCAmelCase		):
             _lowerCAmelCase				=      ACTaFN[config.hidden_act]
         else:
             _lowerCAmelCase				=      config.hidden_act
     else:
         _lowerCAmelCase				=      None





 def 							_snake_case   (	self			,     _lowerCAmelCase		)    ->							torch.Tensor:
     if self.config.tf_padding:
         _lowerCAmelCase				=      apply_tf_padding(_lowerCAmelCase			,     self.convolution		)
     _lowerCAmelCase				=      self.convolution(_lowerCAmelCase		)
     if self.normalization is not None:
         _lowerCAmelCase				=      self.normalization(_lowerCAmelCase		)
     if self.activation is not None:
         _lowerCAmelCase				=      self.activation(_lowerCAmelCase		)
     return features







class    lowerCAmelCase_		(		__magic_name__		):
 __lowerCamelCase						:							Tuple         =					MobileNetVaConfig
 __lowerCamelCase						:							Any         =					load_tf_weights_in_mobilenet_va
 __lowerCamelCase						:							Optional[int]         =					"mobilenet_v1"
 __lowerCamelCase						:							Optional[Any]         =					"pixel_values"
 __lowerCamelCase						:							List[Any]         =					False




 def 							_snake_case   (	self			,     _lowerCAmelCase		)    ->							None:
     if isinstance(_lowerCAmelCase			,     (nn.Linear, nn.Convad)		):
         module.weight.data.normal_(mean=0.0			,     std=self.config.initializer_range		)
         if module.bias is not None:
             module.bias.data.zero_()
     elif isinstance(_lowerCAmelCase			,     nn.BatchNormad		):
         module.bias.data.zero_()
         module.weight.data.fill_(1.0		)







_SCREAMING_SNAKE_CASE	 =							r"\n    This model is a PyTorch [torch.nn.Module](https://pytorch.org/docs/stable/nn.html#torch.nn.Module) subclass. Use it\n    as a regular PyTorch Module and refer to the PyTorch documentation for all matter related to general usage and\n    behavior.\n\n    Parameters:\n        config ([`MobileNetV1Config`]): Model configuration class with all the parameters of the model.\n            Initializing with a config file does not load the weights associated with the model, only the\n            configuration. Check out the [`~PreTrainedModel.from_pretrained`] method to load the model weights.\n"

_SCREAMING_SNAKE_CASE	 =							r"\n    Args:\n        pixel_values (`torch.FloatTensor` of shape `(batch_size, num_channels, height, width)`):\n            Pixel values. Pixel values can be obtained using [`AutoImageProcessor`]. See\n            [`MobileNetV1ImageProcessor.__call__`] for details.\n        output_hidden_states (`bool`, *optional*):\n            Whether or not to return the hidden states of all layers. See `hidden_states` under returned tensors for\n            more detail.\n        return_dict (`bool`, *optional*):\n            Whether or not to return a [`~utils.ModelOutput`] instead of a plain tuple.\n"

@add_start_docstrings(
    "The bare MobileNetV1 model outputting raw hidden-states without any specific head on top."						,__magic_name__						,)
class    lowerCAmelCase_		(		__magic_name__		):




 def __init__(	self			,     _lowerCAmelCase			,     _lowerCAmelCase = True		)    ->							Tuple:
     super().__init__(_lowerCAmelCase		)
     _lowerCAmelCase				=      config

     _lowerCAmelCase				=      32
     _lowerCAmelCase				=      max(int(depth * config.depth_multiplier		)			,     config.min_depth		)

     _lowerCAmelCase				=      MobileNetVaConvLayer(
         _lowerCAmelCase			,     in_channels=config.num_channels			,     out_channels=_lowerCAmelCase			,     kernel_size=3			,     stride=2			,     )

     _lowerCAmelCase				=      [1, 2, 1, 2, 1, 2, 1, 1, 1, 1, 1, 2, 1]

     _lowerCAmelCase				=      nn.ModuleList()
     for i in range(13		):
         _lowerCAmelCase				=      out_channels

         if strides[i] == 2 or i == 0:
             depth *= 2
             _lowerCAmelCase				=      max(int(depth * config.depth_multiplier		)			,     config.min_depth		)

         self.layer.append(
             MobileNetVaConvLayer(
                 _lowerCAmelCase			,     in_channels=_lowerCAmelCase			,     out_channels=_lowerCAmelCase			,     kernel_size=3			,     stride=strides[i]			,     groups=_lowerCAmelCase			,     )		)

         self.layer.append(
             MobileNetVaConvLayer(
                 _lowerCAmelCase			,     in_channels=_lowerCAmelCase			,     out_channels=_lowerCAmelCase			,     kernel_size=1			,     )		)

     _lowerCAmelCase				=      nn.AdaptiveAvgPoolad((1, 1)		) if add_pooling_layer else None

     # Initialize weights and apply final processing
     self.post_init()




 def 							_snake_case   (	self			,     _lowerCAmelCase		)    ->							Optional[Any]:
     raise NotImplementedError





 @add_start_docstrings_to_model_forward(_lowerCAmelCase		)
 @add_code_sample_docstrings(
     checkpoint=_CHECKPOINT_FOR_DOC			,     output_type=_lowerCAmelCase			,     config_class=_CONFIG_FOR_DOC			,     modality="vision"			,     expected_output=_EXPECTED_OUTPUT_SHAPE			,     )
 def 							_snake_case   (	self			,     _lowerCAmelCase = None			,     _lowerCAmelCase = None			,     _lowerCAmelCase = None			,     )    ->							Union[tuple, BaseModelOutputWithPoolingAndNoAttention]:
     _lowerCAmelCase				=      (
         output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states
     )
     _lowerCAmelCase				=      return_dict if return_dict is not None else self.config.use_return_dict

     if pixel_values is None:
         raise ValueError("You have to specify pixel_values"		)

     _lowerCAmelCase				=      self.conv_stem(_lowerCAmelCase		)

     _lowerCAmelCase				=      () if output_hidden_states else None

     for i, layer_module in enumerate(self.layer		):
         _lowerCAmelCase				=      layer_module(_lowerCAmelCase		)

         if output_hidden_states:
             _lowerCAmelCase				=      all_hidden_states + (hidden_states,)

     _lowerCAmelCase				=      hidden_states

     if self.pooler is not None:
         _lowerCAmelCase				=      torch.flatten(self.pooler(_lowerCAmelCase		)			,     start_dim=1		)
     else:
         _lowerCAmelCase				=      None

     if not return_dict:
         return tuple(v for v in [last_hidden_state, pooled_output, all_hidden_states] if v is not None		)

     return BaseModelOutputWithPoolingAndNoAttention(
         last_hidden_state=_lowerCAmelCase			,     pooler_output=_lowerCAmelCase			,     hidden_states=_lowerCAmelCase			,     )







@add_start_docstrings(
    "\n    MobileNetV1 model with an image classification head on top (a linear layer on top of the pooled features), e.g. for\n    ImageNet.\n    "						,__magic_name__						,)
class    lowerCAmelCase_		(		__magic_name__		):




 def __init__(	self			,     _lowerCAmelCase		)    ->							None:
     super().__init__(_lowerCAmelCase		)

     _lowerCAmelCase				=      config.num_labels
     _lowerCAmelCase				=      MobileNetVaModel(_lowerCAmelCase		)

     _lowerCAmelCase				=      self.mobilenet_va.layer[-1].convolution.out_channels

     # Classifier head
     _lowerCAmelCase				=      nn.Dropout(config.classifier_dropout_prob			,     inplace=_lowerCAmelCase		)
     _lowerCAmelCase				=      nn.Linear(_lowerCAmelCase			,     config.num_labels		) if config.num_labels > 0 else nn.Identity()

     # Initialize weights and apply final processing
     self.post_init()





 @add_start_docstrings_to_model_forward(_lowerCAmelCase		)
 @add_code_sample_docstrings(
     checkpoint=_IMAGE_CLASS_CHECKPOINT			,     output_type=_lowerCAmelCase			,     config_class=_CONFIG_FOR_DOC			,     expected_output=_IMAGE_CLASS_EXPECTED_OUTPUT			,     )
 def 							_snake_case   (	self			,     _lowerCAmelCase = None			,     _lowerCAmelCase = None			,     _lowerCAmelCase = None			,     _lowerCAmelCase = None			,     )    ->							Union[tuple, ImageClassifierOutputWithNoAttention]:
     _lowerCAmelCase				=      return_dict if return_dict is not None else self.config.use_return_dict

     _lowerCAmelCase				=      self.mobilenet_va(_lowerCAmelCase			,     output_hidden_states=_lowerCAmelCase			,     return_dict=_lowerCAmelCase		)

     _lowerCAmelCase				=      outputs.pooler_output if return_dict else outputs[1]

     _lowerCAmelCase				=      self.classifier(self.dropout(_lowerCAmelCase		)		)

     _lowerCAmelCase				=      None
     if labels is not None:
         if self.config.problem_type is None:
             if self.num_labels == 1:
                 _lowerCAmelCase				=      "regression"
             elif self.num_labels > 1 and (labels.dtype == torch.long or labels.dtype == torch.int):
                 _lowerCAmelCase				=      "single_label_classification"
             else:
                 _lowerCAmelCase				=      "multi_label_classification"

         if self.config.problem_type == "regression":
             _lowerCAmelCase				=      MSELoss()
             if self.num_labels == 1:
                 _lowerCAmelCase				=      loss_fct(logits.squeeze()			,     labels.squeeze()		)
             else:
                 _lowerCAmelCase				=      loss_fct(_lowerCAmelCase			,     _lowerCAmelCase		)
         elif self.config.problem_type == "single_label_classification":
             _lowerCAmelCase				=      CrossEntropyLoss()
             _lowerCAmelCase				=      loss_fct(logits.view(-1			,     self.num_labels		)			,     labels.view(-1		)		)
         elif self.config.problem_type == "multi_label_classification":
             _lowerCAmelCase				=      BCEWithLogitsLoss()
             _lowerCAmelCase				=      loss_fct(_lowerCAmelCase			,     _lowerCAmelCase		)

     if not return_dict:
         _lowerCAmelCase				=      (logits,) + outputs[2:]
         return ((loss,) + output) if loss is not None else output

     return ImageClassifierOutputWithNoAttention(
         loss=_lowerCAmelCase			,     logits=_lowerCAmelCase			,     hidden_states=outputs.hidden_states			,     )









'''simple docstring'''


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(SCREAMING_SNAKE_CASE_  :					str    ):



    '''simple docstring'''





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

@require_tf
class    lowerCAmelCase_		:




 def 							_snake_case   (	self			,     _lowerCAmelCase			,     _lowerCAmelCase		)    ->							str:
     pass




 def 							_snake_case   (	self		)    ->							Dict:
     pass




 def 							_snake_case   (	self		)    ->							int:
     pass




 def 							_snake_case   (	self			,     _lowerCAmelCase			,     _lowerCAmelCase			,     _lowerCAmelCase			,     _lowerCAmelCase			,     _lowerCAmelCase=None			,     **_lowerCAmelCase		)    ->							Tuple:
     _lowerCAmelCase				=      VisionTextDualEncoderConfig.from_vision_text_configs(_lowerCAmelCase			,     _lowerCAmelCase		)

     _lowerCAmelCase				=      TFVisionTextDualEncoderModel(_lowerCAmelCase		)

     _lowerCAmelCase				=      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 							_snake_case   (	self			,     _lowerCAmelCase			,     _lowerCAmelCase			,     _lowerCAmelCase			,     _lowerCAmelCase			,     _lowerCAmelCase=None			,     **_lowerCAmelCase		)    ->							Tuple:
     _lowerCAmelCase			,			_lowerCAmelCase				=      self.get_vision_text_model(_lowerCAmelCase			,     _lowerCAmelCase		)
     _lowerCAmelCase				=      TFVisionTextDualEncoderModel(vision_model=_lowerCAmelCase			,     text_model=_lowerCAmelCase		)

     _lowerCAmelCase				=      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 							_snake_case   (	self			,     _lowerCAmelCase			,     _lowerCAmelCase			,     _lowerCAmelCase			,     _lowerCAmelCase			,     _lowerCAmelCase=None			,     **_lowerCAmelCase		)    ->							Tuple:
     _lowerCAmelCase			,			_lowerCAmelCase				=      self.get_vision_text_model(_lowerCAmelCase			,     _lowerCAmelCase		)
     _lowerCAmelCase				=      {"vision_model": vision_model, "text_model": text_model}
     _lowerCAmelCase				=      TFVisionTextDualEncoderModel.from_vision_text_pretrained(**_lowerCAmelCase		)

     _lowerCAmelCase				=      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 							_snake_case   (	self			,     _lowerCAmelCase			,     _lowerCAmelCase			,     _lowerCAmelCase			,     _lowerCAmelCase			,     _lowerCAmelCase=None			,     **_lowerCAmelCase		)    ->							Optional[int]:
     _lowerCAmelCase			,			_lowerCAmelCase				=      self.get_vision_text_model(_lowerCAmelCase			,     _lowerCAmelCase		)
     _lowerCAmelCase				=      TFVisionTextDualEncoderModel(vision_model=_lowerCAmelCase			,     text_model=_lowerCAmelCase		)

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

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

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




 def 							_snake_case   (	self			,     _lowerCAmelCase			,     _lowerCAmelCase			,     _lowerCAmelCase			,     _lowerCAmelCase			,     _lowerCAmelCase=None			,     **_lowerCAmelCase		)    ->							Dict:
     _lowerCAmelCase			,			_lowerCAmelCase				=      self.get_vision_text_model(_lowerCAmelCase			,     _lowerCAmelCase		)
     _lowerCAmelCase				=      TFVisionTextDualEncoderModel(vision_model=_lowerCAmelCase			,     text_model=_lowerCAmelCase		)

     _lowerCAmelCase				=      model(
         input_ids=_lowerCAmelCase			,     pixel_values=_lowerCAmelCase			,     attention_mask=_lowerCAmelCase			,     output_attentions=_lowerCAmelCase		)

     _lowerCAmelCase				=      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)
     _lowerCAmelCase				=      to_atuple(vision_model.config.image_size		)
     _lowerCAmelCase				=      to_atuple(vision_model.config.patch_size		)
     _lowerCAmelCase				=      (image_size[1] // patch_size[1]) * (image_size[0] // patch_size[0])
     _lowerCAmelCase				=      num_patches + 1
     self.assertEqual(vision_attentions[0].shape[-3:]			,     (vision_config.num_attention_heads, seq_len, seq_len)		)

     _lowerCAmelCase				=      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 							_snake_case   (	self			,     _lowerCAmelCase			,     _lowerCAmelCase			,     _lowerCAmelCase		)    ->							Optional[int]:
     _lowerCAmelCase				=      np.abs((a - b)		).max()
     self.assertLessEqual(_lowerCAmelCase			,     _lowerCAmelCase			,     f'''Difference between torch and flax is {diff} (>= {tol}).'''		)




 def 							_snake_case   (	self		)    ->							Tuple:
     _lowerCAmelCase				=      self.prepare_config_and_inputs()
     self.check_vision_text_dual_encoder_model(**_lowerCAmelCase		)




 def 							_snake_case   (	self		)    ->							Optional[Any]:
     _lowerCAmelCase				=      self.prepare_config_and_inputs()
     self.check_model_from_pretrained_configs(**_lowerCAmelCase		)




 def 							_snake_case   (	self		)    ->							Union[str, Any]:
     _lowerCAmelCase				=      self.prepare_config_and_inputs()
     self.check_vision_text_dual_encoder_from_pretrained(**_lowerCAmelCase		)




 def 							_snake_case   (	self		)    ->							List[Any]:
     _lowerCAmelCase				=      self.prepare_config_and_inputs()
     self.check_save_load(**_lowerCAmelCase		)




 def 							_snake_case   (	self		)    ->							List[Any]:
     _lowerCAmelCase				=      self.prepare_config_and_inputs()
     self.check_vision_text_output_attention(**_lowerCAmelCase		)





 @slow
 def 							_snake_case   (	self		)    ->							List[Any]:
     _lowerCAmelCase			,			_lowerCAmelCase				=      self.get_pretrained_model_and_inputs()

     _lowerCAmelCase				=      model_a(**_lowerCAmelCase		)
     _lowerCAmelCase				=      outputs[0].numpy()

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

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







@require_tf
class    lowerCAmelCase_		(		__magic_name__						,unittest.TestCase		):




 def 							_snake_case   (	self		)    ->							Union[str, Any]:
     _lowerCAmelCase				=      TFVisionTextDualEncoderModel.from_vision_text_pretrained(
         "hf-internal-testing/tiny-random-vit"			,     "hf-internal-testing/tiny-random-bert"		)
     _lowerCAmelCase				=      13
     _lowerCAmelCase				=      floats_tensor(
         [
             batch_size,
             model.vision_model.config.num_channels,
             model.vision_model.config.image_size,
             model.vision_model.config.image_size,
         ]		)
     _lowerCAmelCase				=      ids_tensor([batch_size, 4]			,     model.text_model.config.vocab_size		)
     _lowerCAmelCase				=      random_attention_mask([batch_size, 4]		)
     _lowerCAmelCase				=      {"pixel_values": pixel_values, "input_ids": input_ids, "attention_mask": attention_mask}

     return model, inputs




 def 							_snake_case   (	self			,     _lowerCAmelCase			,     _lowerCAmelCase		)    ->							Optional[Any]:
     _lowerCAmelCase				=      TFViTModel(_lowerCAmelCase			,     name="vision_model"		)
     _lowerCAmelCase				=      TFBertModel(_lowerCAmelCase			,     name="text_model"		)
     return vision_model, text_model





 def 							_snake_case   (	self		)    ->							Optional[int]:
     _lowerCAmelCase				=      TFViTModelTester(self		)
     _lowerCAmelCase				=      TFBertModelTester(self		)
     _lowerCAmelCase				=      vit_model_tester.prepare_config_and_inputs()
     _lowerCAmelCase				=      bert_model_tester.prepare_config_and_inputs()

     _lowerCAmelCase			,			_lowerCAmelCase			,			_lowerCAmelCase				=      vision_config_and_inputs

     (
         (
         _lowerCAmelCase
     )			,			(
         _lowerCAmelCase
     )			,			(
         _lowerCAmelCase
     )			,			(
         _lowerCAmelCase
     )			,			(
         _lowerCAmelCase
     )			,			(
         _lowerCAmelCase
     )			,			(
         _lowerCAmelCase
     )			,			
     )				=      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_		(		__magic_name__						,unittest.TestCase		):




 def 							_snake_case   (	self		)    ->							List[Any]:
     # DeiT repo doesn't have TF weights, but we don't actually use the weights at all so let's
     # just reinitialize it.
     _lowerCAmelCase				=      TFVisionTextDualEncoderModel.from_vision_text_pretrained(
         "Rocketknight1/tiny-random-deit-tf"			,     "hf-internal-testing/tiny-random-roberta"		)
     _lowerCAmelCase				=      13
     _lowerCAmelCase				=      floats_tensor(
         [
             batch_size,
             model.vision_model.config.num_channels,
             model.vision_model.config.image_size,
             model.vision_model.config.image_size,
         ]		)
     _lowerCAmelCase				=      ids_tensor([batch_size, 4]			,     model.text_model.config.vocab_size		)
     _lowerCAmelCase				=      random_attention_mask([batch_size, 4]		)
     _lowerCAmelCase				=      {"pixel_values": pixel_values, "input_ids": input_ids, "attention_mask": attention_mask}

     return model, inputs




 def 							_snake_case   (	self			,     _lowerCAmelCase			,     _lowerCAmelCase			,     _lowerCAmelCase			,     _lowerCAmelCase			,     _lowerCAmelCase=None			,     **_lowerCAmelCase		)    ->							int:
     _lowerCAmelCase			,			_lowerCAmelCase				=      self.get_vision_text_model(_lowerCAmelCase			,     _lowerCAmelCase		)
     _lowerCAmelCase				=      TFVisionTextDualEncoderModel(vision_model=_lowerCAmelCase			,     text_model=_lowerCAmelCase		)

     _lowerCAmelCase				=      model(
         input_ids=_lowerCAmelCase			,     pixel_values=_lowerCAmelCase			,     attention_mask=_lowerCAmelCase			,     output_attentions=_lowerCAmelCase		)

     _lowerCAmelCase				=      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)
     _lowerCAmelCase				=      to_atuple(vision_model.config.image_size		)
     _lowerCAmelCase				=      to_atuple(vision_model.config.patch_size		)
     _lowerCAmelCase				=      (image_size[1] // patch_size[1]) * (image_size[0] // patch_size[0])
     _lowerCAmelCase				=      num_patches + 2
     self.assertEqual(vision_attentions[0].shape[-3:]			,     (vision_config.num_attention_heads, seq_len, seq_len)		)

     _lowerCAmelCase				=      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 							_snake_case   (	self			,     _lowerCAmelCase			,     _lowerCAmelCase		)    ->							Optional[Any]:
     _lowerCAmelCase				=      TFDeiTModel(_lowerCAmelCase			,     name="vision_model"		)
     _lowerCAmelCase				=      TFRobertaModel(_lowerCAmelCase			,     name="text_model"		)
     return vision_model, text_model





 def 							_snake_case   (	self		)    ->							List[str]:
     _lowerCAmelCase				=      TFDeiTModelTester(self		)
     _lowerCAmelCase				=      TFRobertaModelTester(self		)
     _lowerCAmelCase				=      vit_model_tester.prepare_config_and_inputs()
     _lowerCAmelCase				=      bert_model_tester.prepare_config_and_inputs()

     _lowerCAmelCase			,			_lowerCAmelCase			,			_lowerCAmelCase				=      vision_config_and_inputs

     (
         (
         _lowerCAmelCase
     )			,			(
         _lowerCAmelCase
     )			,			(
         _lowerCAmelCase
     )			,			(
         _lowerCAmelCase
     )			,			(
         _lowerCAmelCase
     )			,			(
         _lowerCAmelCase
     )			,			(
         _lowerCAmelCase
     )			,			
     )				=      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_		(		__magic_name__						,unittest.TestCase		):




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

     return model, inputs




 def 							_snake_case   (	self			,     _lowerCAmelCase			,     _lowerCAmelCase		)    ->							int:
     _lowerCAmelCase				=      TFCLIPVisionModel(_lowerCAmelCase			,     name="vision_model"		)
     _lowerCAmelCase				=      TFBertModel(_lowerCAmelCase			,     name="text_model"		)
     return vision_model, text_model





 def 							_snake_case   (	self		)    ->							Union[str, Any]:
     _lowerCAmelCase				=      TFCLIPVisionModelTester(self		)
     _lowerCAmelCase				=      TFBertModelTester(self		)
     _lowerCAmelCase				=      clip_model_tester.prepare_config_and_inputs()
     _lowerCAmelCase				=      bert_model_tester.prepare_config_and_inputs()

     _lowerCAmelCase			,			_lowerCAmelCase				=      vision_config_and_inputs

     (
         (
         _lowerCAmelCase
     )			,			(
         _lowerCAmelCase
     )			,			(
         _lowerCAmelCase
     )			,			(
         _lowerCAmelCase
     )			,			(
         _lowerCAmelCase
     )			,			(
         _lowerCAmelCase
     )			,			(
         _lowerCAmelCase
     )			,			
     )				=      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 							_snake_case   (	self		)    ->							Union[str, Any]:
     _lowerCAmelCase				=      TFVisionTextDualEncoderModel.from_pretrained(
         "clip-italian/clip-italian"			,     logit_scale_init_value=1.0			,     from_pt=_lowerCAmelCase		)
     _lowerCAmelCase				=      VisionTextDualEncoderProcessor.from_pretrained("clip-italian/clip-italian"		)

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

     _lowerCAmelCase				=      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])			,     )

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

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


