





from collections import OrderedDict
from typing import Mapping

from packaging import version

from ...configuration_utils import PretrainedConfig
from ...onnx import OnnxConfig
from ...utils import logging


UpperCAmelCase_					:   Dict 							=						logging.get_logger(__name__)

UpperCAmelCase_					:   Union[str, Any] 							=						{
    """facebook/data2vec-vision-base-ft""": (
        """https://huggingface.co/facebook/data2vec-vision-base-ft/resolve/main/config.json"""
    ),
}





class      lowercase__    (							__A  ):
  __UpperCamelCase	     =  """data2vec-vision"""







  def __init__(	self    ,						_lowercase=768    ,						_lowercase=12    ,						_lowercase=12    ,						_lowercase=3_072    ,						_lowercase="gelu"    ,						_lowercase=0.0    ,						_lowercase=0.0    ,						_lowercase=0.02    ,						_lowercase=1e-12    ,						_lowercase=224    ,						_lowercase=16    ,						_lowercase=3    ,						_lowercase=False    ,						_lowercase=False    ,						_lowercase=False    ,						_lowercase=False    ,						_lowercase=0.1    ,						_lowercase=0.1    ,						_lowercase=True    ,						_lowercase=[3, 5, 7, 11]    ,						_lowercase=[1, 2, 3, 6]    ,						_lowercase=True    ,						_lowercase=0.4    ,						_lowercase=256    ,						_lowercase=1    ,						_lowercase=False    ,						_lowercase=255    ,						**_lowercase    ,						):
       super().__init__(**_lowercase						)

       lowerCAmelCase_					:       Any    =		hidden_size
       lowerCAmelCase_					:       Any    =		num_hidden_layers
       lowerCAmelCase_					:       List[Any]    =		num_attention_heads
       lowerCAmelCase_					:       Optional[int]    =		intermediate_size
       lowerCAmelCase_					:       List[str]    =		hidden_act
       lowerCAmelCase_					:       str    =		hidden_dropout_prob
       lowerCAmelCase_					:       str    =		attention_probs_dropout_prob
       lowerCAmelCase_					:       str    =		initializer_range
       lowerCAmelCase_					:       List[str]    =		layer_norm_eps

       lowerCAmelCase_					:       str    =		image_size
       lowerCAmelCase_					:       Any    =		patch_size
       lowerCAmelCase_					:       Dict    =		num_channels
       lowerCAmelCase_					:       Tuple    =		use_mask_token
       lowerCAmelCase_					:       List[str]    =		use_absolute_position_embeddings
       lowerCAmelCase_					:       str    =		use_relative_position_bias
       lowerCAmelCase_					:       int    =		use_shared_relative_position_bias
       lowerCAmelCase_					:       Any    =		layer_scale_init_value
       lowerCAmelCase_					:       Union[str, Any]    =		drop_path_rate
       lowerCAmelCase_					:       List[Any]    =		use_mean_pooling
       # decode head attributes (semantic segmentation)
       lowerCAmelCase_					:       Any    =		out_indices
       lowerCAmelCase_					:       Optional[Any]    =		pool_scales
       # auxiliary head attributes (semantic segmentation)
       lowerCAmelCase_					:       Dict    =		use_auxiliary_head
       lowerCAmelCase_					:       Union[str, Any]    =		auxiliary_loss_weight
       lowerCAmelCase_					:       Dict    =		auxiliary_channels
       lowerCAmelCase_					:       Union[str, Any]    =		auxiliary_num_convs
       lowerCAmelCase_					:       List[str]    =		auxiliary_concat_input
       lowerCAmelCase_					:       int    =		semantic_loss_ignore_index





class      lowercase__    (							__A  ):
  __UpperCamelCase	     =  version.parse("""1.11"""  )







  @property
  def 	UpperCAmelCase__		(	self						):
       return OrderedDict(
           [
               ("""pixel_values""", {0: """batch""", 1: """num_channels""", 2: """height""", 3: """width"""}),
           ]						)







  @property
  def 	UpperCAmelCase__		(	self						):
       return 1e-4







import torch
from transformers import PreTrainedModel, XLMRobertaConfig, XLMRobertaModel





class      lowercase__    (							__A  ):
  __UpperCamelCase	     =  """M-CLIP"""







  def __init__(	self    ,						_lowercase=1_024    ,						_lowercase=768    ,						**_lowercase						):
       lowerCAmelCase_					:       Tuple    =		transformerDimSize
       lowerCAmelCase_					:       Optional[Any]    =		imageDimSize
       super().__init__(**_lowercase						)





class      lowercase__    (							__A  ):
  __UpperCamelCase	     =  MCLIPConfig







  def __init__(	self    ,						_lowercase    ,						*_lowercase    ,						**_lowercase						):
       super().__init__(_lowercase    ,						*_lowercase    ,						**_lowercase						)
       lowerCAmelCase_					:       str    =		XLMRobertaModel(_lowercase						)
       lowerCAmelCase_					:       str    =		torch.nn.Linear(
           in_features=config.transformerDimensions    ,						out_features=config.numDims						)







  def 	UpperCAmelCase__		(	self    ,						_lowercase    ,						_lowercase						):
       lowerCAmelCase_					:       Optional[int]    =		self.transformer(input_ids=_lowercase    ,						attention_mask=_lowercase						)[0]
       lowerCAmelCase_					:       Any    =		(embs * attention_mask.unsqueeze(2						)).sum(dim=1						) / attention_mask.sum(dim=1						)[:, None]
       return self.LinearTransformation(_lowercase						), embs
