


"""simple docstring"""






import enum
import inspect
from typing import Iterable, List, Optional, Tuple, Union


class 			SCREAMING_SNAKE_CASE_  (   enum.Enum):




							'''simple docstring'''





							__magic_name__					:     Union[str, Any]				  =  '''timm'''
							__magic_name__					:     List[Any]				  =  '''transformers'''



def 			A__		(					_UpperCAmelCase    :      Optional[Iterable[str]]				,    _UpperCAmelCase    :      Optional[Iterable[int]]				,    _UpperCAmelCase    :      Optional[Iterable[str]]	)							->	Optional[int]:

	'''simple docstring'''


	if stage_names is None:
		raise ValueError("Stage_names must be set for transformers backbones"	)

	if out_features is not None:
		if not isinstance(_UpperCAmelCase				,    (list,)	):
			raise ValueError(F"""out_features must be a list {type(_UpperCAmelCase	)}"""	)
		if any(feat not in stage_names for feat in out_features	):
			raise ValueError(F"""out_features must be a subset of stage_names: {stage_names} got {out_features}"""	)

	if out_indices is not None:
		if not isinstance(_UpperCAmelCase				,    (list, tuple)	):
			raise ValueError(F"""out_indices must be a list or tuple, got {type(_UpperCAmelCase	)}"""	)
		if any(idx >= len(_UpperCAmelCase	) for idx in out_indices	):
			raise ValueError("out_indices must be valid indices for stage_names {stage_names}, got {out_indices}"	)

	if out_features is not None and out_indices is not None:
		if len(_UpperCAmelCase	) != len(_UpperCAmelCase	):
			raise ValueError("out_features and out_indices should have the same length if both are set"	)
		if out_features != [stage_names[idx] for idx in out_indices]:
			raise ValueError("out_features and out_indices should correspond to the same stages if both are set"	)



def 			A__		(					_UpperCAmelCase    :      Optional[List[str]]				,    _UpperCAmelCase    :      Optional[Union[List[int], Tuple[int]]]				,    _UpperCAmelCase    :      List[str]				,    )							->	Union[str, Any]:

	'''simple docstring'''


	if out_indices is None and out_features is None:
		snake_case__  :						List[str]		=      [len(_UpperCAmelCase	) - 1]
		snake_case__  :						Any		=      [stage_names[-1]]
	elif out_indices is None and out_features is not None:
		snake_case__  :						Optional[Any]		=      [stage_names.index(_UpperCAmelCase	) for layer in out_features]
	elif out_features is None and out_indices is not None:
		snake_case__  :						Union[str, Any]		=      [stage_names[idx] for idx in out_indices]
	return out_features, out_indices



def 			A__		(					_UpperCAmelCase    :      Optional[List[str]]				,    _UpperCAmelCase    :      Optional[Union[List[int], Tuple[int]]]				,    _UpperCAmelCase    :      List[str]				,    )							->	Tuple[List[str], List[int]]:

	'''simple docstring'''


	verify_out_features_out_indices(out_features=_UpperCAmelCase				,    out_indices=_UpperCAmelCase				,    stage_names=_UpperCAmelCase	)
	snake_case__,					snake_case__  :						int		=      _align_output_features_output_indices(
	    out_features=_UpperCAmelCase				,    out_indices=_UpperCAmelCase				,    stage_names=_UpperCAmelCase	)
	# Verify that the aligned out_features and out_indices are valid
	verify_out_features_out_indices(out_features=_UpperCAmelCase				,    out_indices=_UpperCAmelCase				,    stage_names=_UpperCAmelCase	)
	return output_features, output_indices


class 			SCREAMING_SNAKE_CASE_  :




							'''simple docstring'''





							__magic_name__					:     Optional[BackboneType]				  =  None




							def        UpperCAmelCase (						self	,					lowerCamelCase__)   ->	None:







								'''simple docstring'''




								if getattr(self	,					"_backbone"	,					lowerCamelCase__) is None:
									raise ValueError("self._backbone must be set before calling _init_timm_backbone")

								# These will diagree with the defaults for the transformers models e.g. for resnet50
								# the transformer model has out_features = ['stem', 'stage1', 'stage2', 'stage3', 'stage4']
								# the timm model has out_features = ['act', 'layer1', 'layer2', 'layer3', 'layer4']
								snake_case__  :						Union[str, Any]		=      [stage["module"] for stage in self._backbone.feature_info.info]
								snake_case__  :						str		=      [stage["num_chs"] for stage in self._backbone.feature_info.info]
								snake_case__  :						str		=      self._backbone.feature_info.out_indices
								snake_case__  :						Optional[int]		=      self._backbone.feature_info.module_name()

								# We verify the out indices and out features are valid
								verify_out_features_out_indices(
								    out_features=lowerCamelCase__	,					out_indices=lowerCamelCase__	,					stage_names=self.stage_names)
								snake_case__,					snake_case__  :						int		=      out_features, out_indices




							def        UpperCAmelCase (						self	,					lowerCamelCase__)   ->	None:







								'''simple docstring'''




								snake_case__  :						Tuple		=      getattr(lowerCamelCase__	,					"stage_names")
								snake_case__  :						int		=      getattr(lowerCamelCase__	,					"out_features"	,					lowerCamelCase__)
								snake_case__  :						List[str]		=      getattr(lowerCamelCase__	,					"out_indices"	,					lowerCamelCase__)

								snake_case__  :						int		=      stage_names
								snake_case__,					snake_case__  :						Optional[Any]		=      get_aligned_output_features_output_indices(
								    out_features=lowerCamelCase__	,					out_indices=lowerCamelCase__	,					stage_names=lowerCamelCase__)
								# Number of channels for each stage. This is set in the transformer backbone model init
								snake_case__  :						Dict		=      None




							def        UpperCAmelCase (						self	,					lowerCamelCase__)   ->	None:







								'''simple docstring'''




								snake_case__  :						Optional[int]		=      config

								snake_case__  :						Optional[int]		=      getattr(lowerCamelCase__	,					"use_timm_backbone"	,					lowerCamelCase__)
								snake_case__  :						Optional[int]		=      BackboneType.TIMM if self.use_timm_backbone else BackboneType.TRANSFORMERS

								if self.backbone_type == BackboneType.TIMM:
									self._init_timm_backbone(lowerCamelCase__)
								elif self.backbone_type == BackboneType.TRANSFORMERS:
									self._init_transformers_backbone(lowerCamelCase__)
								else:
									raise ValueError(f"""backbone_type {self.backbone_type} not supported.""")




							@property
							def        UpperCAmelCase (						self)   ->	Dict:







								'''simple docstring'''




								return self._out_features




							@out_features.setter
							def        UpperCAmelCase (						self	,					lowerCamelCase__)   ->	Optional[Any]:







								'''simple docstring'''




								snake_case__,					snake_case__  :						int		=      get_aligned_output_features_output_indices(
								    out_features=lowerCamelCase__	,					out_indices=lowerCamelCase__	,					stage_names=self.stage_names)




							@property
							def        UpperCAmelCase (						self)   ->	List[str]:







								'''simple docstring'''




								return self._out_indices




							@out_indices.setter
							def        UpperCAmelCase (						self	,					lowerCamelCase__)   ->	Dict:







								'''simple docstring'''




								snake_case__,					snake_case__  :						Tuple		=      get_aligned_output_features_output_indices(
								    out_features=lowerCamelCase__	,					out_indices=lowerCamelCase__	,					stage_names=self.stage_names)




							@property
							def        UpperCAmelCase (						self)   ->	List[str]:







								'''simple docstring'''




								return {stage: self.num_features[i] for i, stage in enumerate(self.stage_names)}




							@property
							def        UpperCAmelCase (						self)   ->	Dict:







								'''simple docstring'''




								return [self.out_feature_channels[name] for name in self.out_features]




							def        UpperCAmelCase (						self	,					*lowerCamelCase__	,					**lowerCamelCase__)   ->	str:







								'''simple docstring'''




								snake_case__  :						Tuple		=      dict(inspect.signature(self.forward).parameters)
								snake_case__  :						Dict		=      {k: v for k, v in kwargs.items() if k in signature}
								return self(*lowerCamelCase__	,					**lowerCamelCase__)




							def        UpperCAmelCase (						self	,					lowerCamelCase__	,					lowerCamelCase__ = None	,					lowerCamelCase__ = None	,					lowerCamelCase__ = None	,					)   ->	str:







								'''simple docstring'''




								raise NotImplementedError("This method should be implemented by the derived class.")







							def        UpperCAmelCase (						self)   ->	int:







								'''simple docstring'''




								snake_case__  :						Union[str, Any]		=      super().to_dict()
								snake_case__  :						Optional[Any]		=      output.pop("_out_features")
								snake_case__  :						List[str]		=      output.pop("_out_indices")
								return output


class 			SCREAMING_SNAKE_CASE_  :




							'''simple docstring'''





							@property
							def        UpperCAmelCase (						self)   ->	List[str]:







								'''simple docstring'''




								return self._out_features




							@out_features.setter
							def        UpperCAmelCase (						self	,					lowerCamelCase__)   ->	Tuple:







								'''simple docstring'''




								snake_case__,					snake_case__  :						List[Any]		=      get_aligned_output_features_output_indices(
								    out_features=lowerCamelCase__	,					out_indices=lowerCamelCase__	,					stage_names=self.stage_names)




							@property
							def        UpperCAmelCase (						self)   ->	int:







								'''simple docstring'''




								return self._out_indices




							@out_indices.setter
							def        UpperCAmelCase (						self	,					lowerCamelCase__)   ->	Optional[Any]:







								'''simple docstring'''




								snake_case__,					snake_case__  :						Any		=      get_aligned_output_features_output_indices(
								    out_features=lowerCamelCase__	,					out_indices=lowerCamelCase__	,					stage_names=self.stage_names)







							def        UpperCAmelCase (						self)   ->	int:







								'''simple docstring'''




								snake_case__  :						Optional[int]		=      super().to_dict()
								snake_case__  :						int		=      output.pop("_out_features")
								snake_case__  :						Dict		=      output.pop("_out_indices")
								return output












from typing import TYPE_CHECKING

from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_tokenizers_available, is_torch_available


SCREAMING_SNAKE_CASE_		  =			{
    """configuration_markuplm""": ["""MARKUPLM_PRETRAINED_CONFIG_ARCHIVE_MAP""", """MarkupLMConfig"""],
    """feature_extraction_markuplm""": ["""MarkupLMFeatureExtractor"""],
    """processing_markuplm""": ["""MarkupLMProcessor"""],
    """tokenization_markuplm""": ["""MarkupLMTokenizer"""],
}

try:
      if not is_tokenizers_available():
            raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
      pass
else:
      SCREAMING_SNAKE_CASE_		  =			["""MarkupLMTokenizerFast"""]

try:
      if not is_torch_available():
            raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
      pass
else:
      SCREAMING_SNAKE_CASE_		  =			[
          """MARKUPLM_PRETRAINED_MODEL_ARCHIVE_LIST""",
          """MarkupLMForQuestionAnswering""",
          """MarkupLMForSequenceClassification""",
          """MarkupLMForTokenClassification""",
          """MarkupLMModel""",
          """MarkupLMPreTrainedModel""",
      ]


if TYPE_CHECKING:
      from .configuration_markuplm import MARKUPLM_PRETRAINED_CONFIG_ARCHIVE_MAP, MarkupLMConfig
      from .feature_extraction_markuplm import MarkupLMFeatureExtractor
      from .processing_markuplm import MarkupLMProcessor
      from .tokenization_markuplm import MarkupLMTokenizer

      try:
            if not is_tokenizers_available():
                  raise OptionalDependencyNotAvailable()
      except OptionalDependencyNotAvailable:
            pass
      else:
            from .tokenization_markuplm_fast import MarkupLMTokenizerFast

      try:
            if not is_torch_available():
                  raise OptionalDependencyNotAvailable()
      except OptionalDependencyNotAvailable:
            pass
      else:
            from .modeling_markuplm import (
                MARKUPLM_PRETRAINED_MODEL_ARCHIVE_LIST,
                MarkupLMForQuestionAnswering,
                MarkupLMForSequenceClassification,
                MarkupLMForTokenClassification,
                MarkupLMModel,
                MarkupLMPreTrainedModel,
            )


else:
      import sys

      SCREAMING_SNAKE_CASE_		  =			_LazyModule(__name__, globals()["""__file__"""], _import_structure)


