

"""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   __lowercase      (     __lowerCamelCase						,      __lowerCamelCase       ):
  snake_case_												=							"""pixel_values"""
  snake_case_												=							False
  snake_case_												=							TimmBackboneConfig

  def __init__(	self		:      Union[str, Any]       ,A		:      Optional[int]       ,**A		:      Union[str, Any]     ):






   '''simple docstring'''
   requires_backends(self       ,"""timm"""     )
   super().__init__(A     )
   UpperCAmelCase__       :	List[Any]        =       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."""     )

   UpperCAmelCase__       :	List[Any]        =       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.
   UpperCAmelCase__       :	List[str]        =       config.out_indices if getattr(A       ,"""out_indices"""       ,A     ) is not None else (-1,)

   UpperCAmelCase__       :	Dict        =       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.
   UpperCAmelCase__       :	Union[str, Any]        =       self._backbone.return_layers
   UpperCAmelCase__       :	int        =       {layer["""module"""]: str(A     ) for i, layer in enumerate(self._backbone.feature_info.info     )}
   super()._init_backbone(A     )

  @classmethod
  def  __lowercase							(	cls		:      int       ,A		:      Optional[Any]       ,*A		:      int       ,**A		:      Optional[Any]     ):






   '''simple docstring'''
   requires_backends(cls       ,["""vision""", """timm"""]     )
   from ...models.timm_backbone import TimmBackboneConfig

   UpperCAmelCase__       :	str        =       kwargs.pop("""config"""       ,TimmBackboneConfig()     )

   UpperCAmelCase__       :	Optional[Any]        =       kwargs.pop("""use_timm_backbone"""       ,A     )
   if not use_timm:
    raise ValueError("""use_timm_backbone must be True for timm backbones"""     )

   UpperCAmelCase__       :	Union[str, Any]        =       kwargs.pop("""num_channels"""       ,config.num_channels     )
   UpperCAmelCase__       :	Tuple        =       kwargs.pop("""features_only"""       ,config.features_only     )
   UpperCAmelCase__       :	List[str]        =       kwargs.pop("""use_pretrained_backbone"""       ,config.use_pretrained_backbone     )
   UpperCAmelCase__       :	Union[str, Any]        =       kwargs.pop("""out_indices"""       ,config.out_indices     )
   UpperCAmelCase__       :	Any        =       TimmBackboneConfig(
       backbone=A       ,num_channels=A       ,features_only=A       ,use_pretrained_backbone=A       ,out_indices=A       ,)
   return super()._from_config(A       ,**A     )

  def  __lowercase							(	self		:      Optional[int]       ,A		:      Optional[Any]     ):






   '''simple docstring'''
   pass




  def  __lowercase							(	self		:      Optional[Any]       ,A		:      Optional[Any]       ,A		:      Optional[Any]=None       ,A		:      Any=None       ,A		:      int=None       ,**A		:      int     ):






   '''simple docstring'''
   UpperCAmelCase__       :	str        =       return_dict if return_dict is not None else self.config.use_return_dict
   UpperCAmelCase__       :	str        =       (
       output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states
   )
   UpperCAmelCase__       :	Tuple        =       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
    UpperCAmelCase__       :	Tuple        =       self._all_layers
    UpperCAmelCase__       :	Tuple        =       self._backbone(A       ,**A     )
    UpperCAmelCase__       :	Tuple        =       self._return_layers
    UpperCAmelCase__       :	Union[str, Any]        =       tuple(hidden_states[i] for i in self.out_indices     )
   else:
    UpperCAmelCase__       :	List[str]        =       self._backbone(A       ,**A     )
    UpperCAmelCase__       :	Any        =       None

   UpperCAmelCase__       :	List[Any]        =       tuple(A     )
   UpperCAmelCase__       :	Dict        =       tuple(A     ) if hidden_states is not None else None

   if not return_dict:
    UpperCAmelCase__       :	str        =       (feature_maps,)
    if output_hidden_states:
     UpperCAmelCase__       :	Optional[Any]        =       output + (hidden_states,)
    return output

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











import warnings

from ...utils import logging
from .image_processing_layoutlmva import LayoutLMvaImageProcessor


lowerCamelCase__					:  Union[str, Any]      							=				logging.get_logger(__name__)





class 			lowerCamelCase_				( _SCREAMING_SNAKE_CASE   ):


 '''simple docstring'''







 def __init__(				self    :						Dict							,							*_lowerCAmelCase    :						Optional[Any]							,							**_lowerCAmelCase    :						Any		):
       warnings.warn(
           'The class LayoutLMv2FeatureExtractor is deprecated and will be removed in version 5 of Transformers.'
           ' Please use LayoutLMv2ImageProcessor instead.'							,							_lowerCAmelCase							,							)
       super().__init__(*_lowerCAmelCase							,							**_lowerCAmelCase		)