





from typing import TYPE_CHECKING

from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available


_UpperCAmelCase				:       int	   =				{
    """configuration_megatron_bert""": ["""MEGATRON_BERT_PRETRAINED_CONFIG_ARCHIVE_MAP""", """MegatronBertConfig"""],
}

try:
    if not is_torch_available():
        raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
    pass
else:
    _UpperCAmelCase				:       List[str]	   =				[
        """MEGATRON_BERT_PRETRAINED_MODEL_ARCHIVE_LIST""",
        """MegatronBertForCausalLM""",
        """MegatronBertForMaskedLM""",
        """MegatronBertForMultipleChoice""",
        """MegatronBertForNextSentencePrediction""",
        """MegatronBertForPreTraining""",
        """MegatronBertForQuestionAnswering""",
        """MegatronBertForSequenceClassification""",
        """MegatronBertForTokenClassification""",
        """MegatronBertModel""",
        """MegatronBertPreTrainedModel""",
    ]

if TYPE_CHECKING:
    from .configuration_megatron_bert import MEGATRON_BERT_PRETRAINED_CONFIG_ARCHIVE_MAP, MegatronBertConfig

    try:
        if not is_torch_available():
            raise OptionalDependencyNotAvailable()
    except OptionalDependencyNotAvailable:
        pass
    else:
        from .modeling_megatron_bert import (
            MEGATRON_BERT_PRETRAINED_MODEL_ARCHIVE_LIST,
            MegatronBertForCausalLM,
            MegatronBertForMaskedLM,
            MegatronBertForMultipleChoice,
            MegatronBertForNextSentencePrediction,
            MegatronBertForPreTraining,
            MegatronBertForQuestionAnswering,
            MegatronBertForSequenceClassification,
            MegatronBertForTokenClassification,
            MegatronBertModel,
            MegatronBertPreTrainedModel,
        )

else:
    import sys

    _UpperCAmelCase				:       Optional[Any]	   =				_LazyModule(__name__, globals()["""__file__"""], _import_structure, module_spec=__spec__)












import torch
from transformers import PreTrainedModel, XLMRobertaConfig, XLMRobertaModel

class    lowerCAmelCase							(					__UpperCamelCase			):
 UpperCAmelCase__													=		"""M-CLIP"""
 def __init__(      self  :      Optional[Any]			,     UpperCAmelCase  :      Union[str, Any]=1024			,     UpperCAmelCase  :      Tuple=768			,     **UpperCAmelCase  :      Optional[int]	)			->	Dict:
     lowerCamelCase__    :      Optional[int]					 =		transformerDimSize
     lowerCamelCase__    :      Optional[Any]					 =		imageDimSize
     super().__init__(**UpperCAmelCase	)


class    lowerCAmelCase							(					__UpperCamelCase			):
 UpperCAmelCase__													=		MCLIPConfig
 def __init__(      self  :      List[Any]			,     UpperCAmelCase  :      Dict			,     *UpperCAmelCase  :      Any			,     **UpperCAmelCase  :      Dict	)			->	Dict:
     super().__init__(UpperCAmelCase			,     *UpperCAmelCase			,     **UpperCAmelCase	)
     lowerCamelCase__    :      Tuple					 =		XLMRobertaModel(UpperCAmelCase	)
     lowerCamelCase__    :      Union[str, Any]					 =		torch.nn.Linear(
         in_features=config.transformerDimensions			,     out_features=config.numDims	)







 def      A_	(      self  :      Optional[int]			,     UpperCAmelCase  :      Optional[Any]			,     UpperCAmelCase  :      Dict	)			->	Tuple:
     lowerCamelCase__    :      Any					 =		self.transformer(input_ids=UpperCAmelCase			,     attention_mask=UpperCAmelCase	)[0]
     lowerCamelCase__    :      int					 =		(embs * attention_mask.unsqueeze(2	)).sum(dim=1	) / attention_mask.sum(dim=1	)[:, None]
     return self.LinearTransformation(UpperCAmelCase	), embs





