





"""simple docstring"""
import enum
import inspect
from typing import Iterable, List, Optional, Tuple, Union
class lowerCamelCase__  (    enum.Enum				):
   __UpperCAmelCase        =					"""timm"""
   __UpperCAmelCase        =					"""transformers"""




def 			__snake_case						(      __A ,__A ,__A    )		->				List[str]:
   if stage_names is None:
      raise ValueError("""Stage_names must be set for transformers backbones"""    )

   if out_features is not None:
      if not isinstance(__A ,(list,)    ):
         raise ValueError(F'''out_features must be a list {type(__A    )}'''    )
      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(__A ,(list, tuple)    ):
         raise ValueError(F'''out_indices must be a list or tuple, got {type(__A    )}'''    )
      if any(idx >= len(__A    ) 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(__A    ) != len(__A    ):
         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 			__snake_case						(      __A ,__A ,__A ,)		->				str:
   if out_indices is None and out_features is None:
      lowercase    :			str  =       [len(__A    ) - 1]
      lowercase    :			Tuple  =       [stage_names[-1]]
   elif out_indices is None and out_features is not None:
      lowercase    :			List[Any]  =       [stage_names.index(__A    ) for layer in out_features]
   elif out_features is None and out_indices is not None:
      lowercase    :			Dict  =       [stage_names[idx] for idx in out_indices]
   return out_features, out_indices




def 			__snake_case						(      __A ,__A ,__A ,)		->				Tuple[List[str], List[int]]:
   verify_out_features_out_indices(out_features=__A ,out_indices=__A ,stage_names=__A    )
   lowercase  ,					lowercase    :			List[Any]  =       _align_output_features_output_indices(
       out_features=__A ,out_indices=__A ,stage_names=__A    )
   # Verify that the aligned out_features and out_indices are valid
   verify_out_features_out_indices(out_features=__A ,out_indices=__A ,stage_names=__A    )
   return output_features, output_indices
class lowerCamelCase__  :
   __UpperCAmelCase        =					None



   def    _UpperCAmelCase	(       self      ,   snake_case	) ->    None:







      """simple docstring"""
      if getattr(self      ,   """_backbone"""      ,   snake_case	) 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']
      lowercase    :			Union[str, Any]  =       [stage["""module"""] for stage in self._backbone.feature_info.info]
      lowercase    :			Dict  =       [stage["""num_chs"""] for stage in self._backbone.feature_info.info]
      lowercase    :			Union[str, Any]  =       self._backbone.feature_info.out_indices
      lowercase    :			Tuple  =       self._backbone.feature_info.module_name()

      # We verify the out indices and out features are valid
      verify_out_features_out_indices(
          out_features=snake_case      ,   out_indices=snake_case      ,   stage_names=self.stage_names	)
      lowercase  ,					lowercase    :			Optional[int]  =       out_features, out_indices



   def    _UpperCAmelCase	(       self      ,   snake_case	) ->    None:







      """simple docstring"""
      lowercase    :			str  =       getattr(snake_case      ,   """stage_names"""	)
      lowercase    :			List[str]  =       getattr(snake_case      ,   """out_features"""      ,   snake_case	)
      lowercase    :			Optional[int]  =       getattr(snake_case      ,   """out_indices"""      ,   snake_case	)

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



   def    _UpperCAmelCase	(       self      ,   snake_case	) ->    None:







      """simple docstring"""
      lowercase    :			Optional[Any]  =       config

      lowercase    :			Optional[Any]  =       getattr(snake_case      ,   """use_timm_backbone"""      ,   snake_case	)
      lowercase    :			Optional[int]  =       BackboneType.TIMM if self.use_timm_backbone else BackboneType.TRANSFORMERS

      if self.backbone_type == BackboneType.TIMM:
         self._init_timm_backbone(snake_case	)
      elif self.backbone_type == BackboneType.TRANSFORMERS:
         self._init_transformers_backbone(snake_case	)
      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      ,   snake_case	) ->    Dict:







      """simple docstring"""
      lowercase  ,					lowercase    :			Tuple  =       get_aligned_output_features_output_indices(
          out_features=snake_case      ,   out_indices=snake_case      ,   stage_names=self.stage_names	)



   @property
   def    _UpperCAmelCase	(       self	) ->    str:







      """simple docstring"""
      return self._out_indices



   @out_indices.setter
   def    _UpperCAmelCase	(       self      ,   snake_case	) ->    List[Any]:







      """simple docstring"""
      lowercase  ,					lowercase    :			int  =       get_aligned_output_features_output_indices(
          out_features=snake_case      ,   out_indices=snake_case      ,   stage_names=self.stage_names	)



   @property
   def    _UpperCAmelCase	(       self	) ->    Any:







      """simple docstring"""
      # the current backbones will output the number of channels for each stage
      # even if that stage is not in the out_features list.
      return {stage: self.num_features[i] for i, stage in enumerate(self.stage_names	)}



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







      """simple docstring"""
      return [self.out_feature_channels[name] for name in self.out_features]



   def    _UpperCAmelCase	(       self      ,   *snake_case      ,   **snake_case	) ->    Optional[Any]:







      """simple docstring"""
      lowercase    :			Tuple  =       dict(inspect.signature(self.forward	).parameters	)
      lowercase    :			Optional[int]  =       {k: v for k, v in kwargs.items() if k in signature}
      return self(*snake_case      ,   **snake_case	)



   def    _UpperCAmelCase	(       self      ,   snake_case      ,   snake_case = None      ,   snake_case = None      ,   snake_case = None      ,   ) ->    Optional[int]:







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





   def    _UpperCAmelCase	(       self	) ->    str:







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




class lowerCamelCase__  :



   @property
   def    _UpperCAmelCase	(       self	) ->    Tuple:







      """simple docstring"""
      return self._out_features



   @out_features.setter
   def    _UpperCAmelCase	(       self      ,   snake_case	) ->    Tuple:







      """simple docstring"""
      lowercase  ,					lowercase    :			str  =       get_aligned_output_features_output_indices(
          out_features=snake_case      ,   out_indices=snake_case      ,   stage_names=self.stage_names	)



   @property
   def    _UpperCAmelCase	(       self	) ->    Optional[int]:







      """simple docstring"""
      return self._out_indices



   @out_indices.setter
   def    _UpperCAmelCase	(       self      ,   snake_case	) ->    int:







      """simple docstring"""
      lowercase  ,					lowercase    :			str  =       get_aligned_output_features_output_indices(
          out_features=snake_case      ,   out_indices=snake_case      ,   stage_names=self.stage_names	)





   def    _UpperCAmelCase	(       self	) ->    Any:







      """simple docstring"""
      lowercase    :			Dict  =       super().to_dict()
      lowercase    :			int  =       output.pop("""_out_features"""	)
      lowercase    :			List[str]  =       output.pop("""_out_indices"""	)
      return output









"""simple docstring"""
from typing import Optional, Union

from ...file_utils import TensorType
from ...processing_utils import ProcessorMixin
from ...tokenization_utils_base import BatchEncoding, PaddingStrategy, TruncationStrategy
class lowerCamelCase__  (    __UpperCamelCase				):
   __UpperCAmelCase        =					"""MarkupLMFeatureExtractor"""
   __UpperCAmelCase        =					("""MarkupLMTokenizer""", """MarkupLMTokenizerFast""")
   __UpperCAmelCase        =					True



   def __call__(       self      ,   snake_case=None      ,   snake_case=None      ,   snake_case=None      ,   snake_case=None      ,   snake_case=None      ,   snake_case = True      ,   snake_case = False      ,   snake_case = None      ,   snake_case = None      ,   snake_case = 0      ,   snake_case = None      ,   snake_case = None      ,   snake_case = None      ,   snake_case = False      ,   snake_case = False      ,   snake_case = False      ,   snake_case = False      ,   snake_case = True      ,   snake_case = None      ,   **snake_case      ,   ) ->    BatchEncoding:







      """simple docstring"""
      # first, create nodes and xpaths
      if self.parse_html:
         if html_strings is None:
            raise ValueError("""Make sure to pass HTML strings in case `parse_html` is set to `True`"""	)

         if nodes is not None or xpaths is not None or node_labels is not None:
            raise ValueError(
                """Please don't pass nodes, xpaths nor node labels in case `parse_html` is set to `True`"""	)

         lowercase    :			List[Any]  =       self.feature_extractor(snake_case	)
         lowercase    :			Union[str, Any]  =       features["""nodes"""]
         lowercase    :			List[Any]  =       features["""xpaths"""]
      else:
         if html_strings is not None:
            raise ValueError("""You have passed HTML strings but `parse_html` is set to `False`."""	)
         if nodes is None or xpaths is None:
            raise ValueError("""Make sure to pass nodes and xpaths in case `parse_html` is set to `False`"""	)

        # # second, apply the tokenizer
      if questions is not None and self.parse_html:
         if isinstance(snake_case      ,   snake_case	):
            lowercase    :			Dict  =       [questions]  # add batch dimension (as the feature extractor always adds a batch dimension)

      lowercase    :			Dict  =       self.tokenizer(
          text=questions if questions is not None else nodes      ,   text_pair=nodes if questions is not None else None      ,   xpaths=snake_case      ,   node_labels=snake_case      ,   add_special_tokens=snake_case      ,   padding=snake_case      ,   truncation=snake_case      ,   max_length=snake_case      ,   stride=snake_case      ,   pad_to_multiple_of=snake_case      ,   return_token_type_ids=snake_case      ,   return_attention_mask=snake_case      ,   return_overflowing_tokens=snake_case      ,   return_special_tokens_mask=snake_case      ,   return_offsets_mapping=snake_case      ,   return_length=snake_case      ,   verbose=snake_case      ,   return_tensors=snake_case      ,   **snake_case      ,   )

      return encoded_inputs



   def    _UpperCAmelCase	(       self      ,   *snake_case      ,   **snake_case	) ->    Optional[Any]:







      """simple docstring"""
      return self.tokenizer.batch_decode(*snake_case      ,   **snake_case	)



   def    _UpperCAmelCase	(       self      ,   *snake_case      ,   **snake_case	) ->    Tuple:







      """simple docstring"""
      return self.tokenizer.decode(*snake_case      ,   **snake_case	)





   @property
   def    _UpperCAmelCase	(       self	) ->    Dict:







      """simple docstring"""
      lowercase    :			Any  =       self.tokenizer.model_input_names
      return tokenizer_input_names


