



import copy

from ...configuration_utils import PretrainedConfig
from ...utils import logging
from ..auto.configuration_auto import CONFIG_MAPPING


__SCREAMING_SNAKE_CASE						:   Any         =			logging.get_logger(__name__)



class        lowercase_   (					UpperCamelCase__		):
							_lowerCamelCase						=		'''upernet'''



							def __init__(						self				,    lowercase_=None				,    lowercase_=512				,    lowercase_=0.02				,    lowercase_=[1, 2, 3, 6]				,    lowercase_=True				,    lowercase_=0.4				,    lowercase_=384				,    lowercase_=256				,    lowercase_=1				,    lowercase_=False				,    lowercase_=255				,    **lowercase_				,    ):
													super().__init__(**UpperCamelCase_    )

													if backbone_config is None:
																			logger.info("`backbone_config` is `None`. Initializing the config with the default `ResNet` backbone."    )
																			_snake_case						:					Tuple    =   CONFIG_MAPPING["resnet"](out_features=["stage1", "stage2", "stage3", "stage4"]    )
													elif isinstance(UpperCamelCase_				,    UpperCamelCase_    ):
																			_snake_case						:					str    =   backbone_config.get("model_type"    )
																			_snake_case						:					Any    =   CONFIG_MAPPING[backbone_model_type]
																			_snake_case						:					List[Any]    =   config_class.from_dict(UpperCamelCase_    )

													_snake_case						:					Dict    =   backbone_config
													_snake_case						:					Union[str, Any]    =   hidden_size
													_snake_case						:					Optional[Any]    =   initializer_range
													_snake_case						:					List[str]    =   pool_scales
													_snake_case						:					Tuple    =   use_auxiliary_head
													_snake_case						:					Tuple    =   auxiliary_loss_weight
													_snake_case						:					Optional[Any]    =   auxiliary_in_channels
													_snake_case						:					List[Any]    =   auxiliary_channels
													_snake_case						:					int    =   auxiliary_num_convs
													_snake_case						:					Optional[Any]    =   auxiliary_concat_input
													_snake_case						:					Union[str, Any]    =   loss_ignore_index



							def 	UpperCamelCase	(						self    ):
													_snake_case						:					str    =   copy.deepcopy(self.__dict__    )
													_snake_case						:					List[Any]    =   self.backbone_config.to_dict()
													_snake_case						:					Tuple    =   self.__class__.model_type
													return output
import torch
from transformers import PreTrainedModel, XLMRobertaConfig, XLMRobertaModel



class        lowercase_   (					__snake_case		):
							_lowerCamelCase						=		'M-CLIP'



							def __init__(						self				,    lowercase_=1_024				,    lowercase_=768				,    **lowercase_    ):
													_snake_case						:					str    =   transformerDimSize
													_snake_case						:					Union[str, Any]    =   imageDimSize
													super().__init__(**lowercase_    )




class        lowercase_   (					__snake_case		):
							_lowerCamelCase						=		MCLIPConfig



							def __init__(						self				,    lowercase_				,    *lowercase_				,    **lowercase_    ):
													super().__init__(lowercase_				,    *lowercase_				,    **lowercase_    )
													_snake_case						:					List[Any]    =   XLMRobertaModel(lowercase_    )
													_snake_case						:					int    =   torch.nn.Linear(
													    in_features=config.transformerDimensions				,    out_features=config.numDims    )



							def 	UpperCamelCase	(						self				,    lowercase_				,    lowercase_    ):
													_snake_case						:					Tuple    =   self.transformer(input_ids=lowercase_				,    attention_mask=lowercase_    )[0]
													_snake_case						:					Tuple    =   (embs * attention_mask.unsqueeze(2    )).sum(dim=1    ) / attention_mask.sum(dim=1    )[:, None]
													return self.LinearTransformation(lowercase_    ), embs