


"""simple docstring"""


from typing import Tuple, Union

from ...modeling_outputs import BackboneOutput
from ...modeling_utils import PreTrainedModel
from ...utils import is_timm_available, is_torch_available, requires_backends
from ...utils.backbone_utils import BackboneMixin
from .configuration_timm_backbone import TimmBackboneConfig


if is_timm_available():
						import timm


if is_torch_available():
						from torch import Tensor



class 			lowerCamelCase__						(   _lowercase							,    _lowercase			):
						lowerCAmelCase       =     '''pixel_values'''
						lowerCAmelCase       =     False
						lowerCAmelCase       =     TimmBackboneConfig







						def __init__(							self   :    str			,			_lowercase   :    Tuple			,			**_lowercase   :    Union[str, Any]		):
								requires_backends(self			,			'timm'		)
								super().__init__(A_		)
								A					       = config

								if config.backbone is None:
										raise ValueError('backbone is not set in the config. Please set it to a timm model name.'		)

								if config.backbone not in timm.list_models():
										raise ValueError(f'backbone {config.backbone} is not supported by timm.'		)

								if hasattr(A_			,			'out_features'		) and config.out_features is not None:
										raise ValueError('out_features is not supported by TimmBackbone. Please use out_indices instead.'		)

								A					       = getattr(A_			,			'use_pretrained_backbone'			,			A_		)
								if pretrained is None:
										raise ValueError('use_pretrained_backbone is not set in the config. Please set it to True or False.'		)

								# We just take the final layer by default. This matches the default for the transformers models.
								A					       = config.out_indices if getattr(A_			,			'out_indices'			,			A_		) is not None else (-1,)

								A					       = timm.create_model(
								    config.backbone			,			pretrained=A_			,			features_only=config.features_only			,			in_chans=config.num_channels			,			out_indices=A_			,			**A_			,			)
								# These are used to control the output of the model when called. If output_hidden_states is True, then
								# return_layers is modified to include all layers.
								A					       = self._backbone.return_layers
								A					       = {layer['module']: str(A_		) for i, layer in enumerate(self._backbone.feature_info.info		)}
								super()._init_backbone(A_		)







						@classmethod
						def __a						(							cls   :    Optional[int]			,			_lowercase   :    Union[str, Any]			,			*_lowercase   :    List[Any]			,			**_lowercase   :    Optional[Any]		):
								requires_backends(cls			,			['vision', 'timm']		)
								from ...models.timm_backbone import TimmBackboneConfig

								A					       = kwargs.pop('config'			,			TimmBackboneConfig()		)

								A					       = kwargs.pop('use_timm_backbone'			,			A_		)
								if not use_timm:
										raise ValueError('use_timm_backbone must be True for timm backbones'		)

								A					       = kwargs.pop('num_channels'			,			config.num_channels		)
								A					       = kwargs.pop('features_only'			,			config.features_only		)
								A					       = kwargs.pop('use_pretrained_backbone'			,			config.use_pretrained_backbone		)
								A					       = kwargs.pop('out_indices'			,			config.out_indices		)
								A					       = TimmBackboneConfig(
								    backbone=A_			,			num_channels=A_			,			features_only=A_			,			use_pretrained_backbone=A_			,			out_indices=A_			,			)
								return super()._from_config(A_			,			**A_		)







						def __a						(							self   :    str			,			_lowercase   :    Any		):
								pass







						def __a						(							self   :    int			,			_lowercase   :    int			,			_lowercase   :    Optional[int]=None			,			_lowercase   :    Any=None			,			_lowercase   :    List[Any]=None			,			**_lowercase   :    Optional[int]		):
								A					       = return_dict if return_dict is not None else self.config.use_return_dict
								A					       = (
								    output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states
								)
								A					       = output_attentions if output_attentions is not None else self.config.output_attentions

								if output_attentions:
										raise ValueError('Cannot output attentions for timm backbones at the moment'		)

								if output_hidden_states:
										# We modify the return layers to include all the stages of the backbone
										A					       = self._all_layers
										A					       = self._backbone(A_			,			**A_		)
										A					       = self._return_layers
										A					       = tuple(hidden_states[i] for i in self.out_indices		)
								else:
										A					       = self._backbone(A_			,			**A_		)
										A					       = None

								A					       = tuple(A_		)
								A					       = tuple(A_		) if hidden_states is not None else None

								if not return_dict:
										A					       = (feature_maps,)
										if output_hidden_states:
												A					       = output + (hidden_states,)
										return output

								return BackboneOutput(feature_maps=A_			,			hidden_states=A_			,			attentions=A_		)









"""simple docstring"""


from typing import TYPE_CHECKING

from ...utils import (
    OptionalDependencyNotAvailable,
    _LazyModule,
    is_tf_available,
    is_tokenizers_available,
    is_torch_available,
)


UpperCamelCase						:				Any             =      {
    "configuration_lxmert": ["LXMERT_PRETRAINED_CONFIG_ARCHIVE_MAP", "LxmertConfig"],
    "tokenization_lxmert": ["LxmertTokenizer"],
}

try:
	if not is_tokenizers_available():
		raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
	pass
else:
	UpperCamelCase						:				int             =      ["LxmertTokenizerFast"]

try:
	if not is_torch_available():
		raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
	pass
else:
	UpperCamelCase						:				Union[str, Any]             =      [
	    "LxmertEncoder",
	    "LxmertForPreTraining",
	    "LxmertForQuestionAnswering",
	    "LxmertModel",
	    "LxmertPreTrainedModel",
	    "LxmertVisualFeatureEncoder",
	    "LxmertXLayer",
	]

try:
	if not is_tf_available():
		raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
	pass
else:
	UpperCamelCase						:				Union[str, Any]             =      [
	    "TF_LXMERT_PRETRAINED_MODEL_ARCHIVE_LIST",
	    "TFLxmertForPreTraining",
	    "TFLxmertMainLayer",
	    "TFLxmertModel",
	    "TFLxmertPreTrainedModel",
	    "TFLxmertVisualFeatureEncoder",
	]


if TYPE_CHECKING:
	from .configuration_lxmert import LXMERT_PRETRAINED_CONFIG_ARCHIVE_MAP, LxmertConfig
	from .tokenization_lxmert import LxmertTokenizer

	try:
		if not is_tokenizers_available():
			raise OptionalDependencyNotAvailable()
	except OptionalDependencyNotAvailable:
		pass
	else:
		from .tokenization_lxmert_fast import LxmertTokenizerFast

	try:
		if not is_torch_available():
			raise OptionalDependencyNotAvailable()
	except OptionalDependencyNotAvailable:
		pass
	else:
		from .modeling_lxmert import (
		    LxmertEncoder,
		    LxmertForPreTraining,
		    LxmertForQuestionAnswering,
		    LxmertModel,
		    LxmertPreTrainedModel,
		    LxmertVisualFeatureEncoder,
		    LxmertXLayer,
		)

	try:
		if not is_tf_available():
			raise OptionalDependencyNotAvailable()
	except OptionalDependencyNotAvailable:
		pass
	else:
		from .modeling_tf_lxmert import (
		    TF_LXMERT_PRETRAINED_MODEL_ARCHIVE_LIST,
		    TFLxmertForPreTraining,
		    TFLxmertMainLayer,
		    TFLxmertModel,
		    TFLxmertPreTrainedModel,
		    TFLxmertVisualFeatureEncoder,
		)

else:
	import sys

	UpperCamelCase						:				Dict             =      _LazyModule(__name__, globals()["__file__"], _import_structure, module_spec=__spec__)





