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    A_ (       a_			,       a_ ):
  _SCREAMING_SNAKE_CASE    				=			"""pixel_values"""
  _SCREAMING_SNAKE_CASE    				=			False
  _SCREAMING_SNAKE_CASE    				=			TimmBackboneConfig

  def __init__(		self :      Optional[int]     ,							__SCREAMING_SNAKE_CASE :      Optional[int]     ,							**__SCREAMING_SNAKE_CASE :      List[Any]       ):
   requires_backends(self     ,							"timm"       )
   super().__init__(__SCREAMING_SNAKE_CASE       )
   __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(__SCREAMING_SNAKE_CASE     ,							"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(__SCREAMING_SNAKE_CASE     ,							"use_pretrained_backbone"     ,							__SCREAMING_SNAKE_CASE       )
   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(__SCREAMING_SNAKE_CASE     ,							"out_indices"     ,							__SCREAMING_SNAKE_CASE       ) is not None else (-1,)

   __a       =  timm.create_model(
       config.backbone     ,							pretrained=__SCREAMING_SNAKE_CASE     ,							features_only=config.features_only     ,							in_chans=config.num_channels     ,							out_indices=__SCREAMING_SNAKE_CASE     ,							**__SCREAMING_SNAKE_CASE     ,							)
   # 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(__SCREAMING_SNAKE_CASE       ) for i, layer in enumerate(self._backbone.feature_info.info       )}
   super()._init_backbone(__SCREAMING_SNAKE_CASE       )

  @classmethod
  def 		_UpperCAmelCase   (		cls :      Dict     ,							__SCREAMING_SNAKE_CASE :      Dict     ,							*__SCREAMING_SNAKE_CASE :      Optional[int]     ,							**__SCREAMING_SNAKE_CASE :      Tuple       ):
   requires_backends(cls     ,							["vision", "timm"]       )
   from ...models.timm_backbone import TimmBackboneConfig

   __a       =  kwargs.pop("config"     ,							TimmBackboneConfig()       )

   __a       =  kwargs.pop("use_timm_backbone"     ,							__SCREAMING_SNAKE_CASE       )
   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=__SCREAMING_SNAKE_CASE     ,							num_channels=__SCREAMING_SNAKE_CASE     ,							features_only=__SCREAMING_SNAKE_CASE     ,							use_pretrained_backbone=__SCREAMING_SNAKE_CASE     ,							out_indices=__SCREAMING_SNAKE_CASE     ,							)
   return super()._from_config(__SCREAMING_SNAKE_CASE     ,							**__SCREAMING_SNAKE_CASE       )

  def 		_UpperCAmelCase   (		self :      Optional[Any]     ,							__SCREAMING_SNAKE_CASE :      List[Any]       ):
   pass

  def 		_UpperCAmelCase   (		self :      Dict     ,							__SCREAMING_SNAKE_CASE :      Union[str, Any]     ,							__SCREAMING_SNAKE_CASE :      Optional[int]=None     ,							__SCREAMING_SNAKE_CASE :      Optional[Any]=None     ,							__SCREAMING_SNAKE_CASE :      Tuple=None     ,							**__SCREAMING_SNAKE_CASE :      List[Any]       ):
   __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(__SCREAMING_SNAKE_CASE     ,							**__SCREAMING_SNAKE_CASE       )
    __a       =  self._return_layers
    __a       =  tuple(hidden_states[i] for i in self.out_indices       )
   else:
    __a       =  self._backbone(__SCREAMING_SNAKE_CASE     ,							**__SCREAMING_SNAKE_CASE       )
    __a       =  None

   __a       =  tuple(__SCREAMING_SNAKE_CASE       )
   __a       =  tuple(__SCREAMING_SNAKE_CASE       ) 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=__SCREAMING_SNAKE_CASE     ,							hidden_states=__SCREAMING_SNAKE_CASE     ,							attentions=__SCREAMING_SNAKE_CASE       )



import os
import sys
from contextlib import contextmanager


# Windows only
if os.name == "nt":
  import ctypes
  import msvcrt  # noqa
  class    A_ (       ctypes.Structure ):
    # _fields is a specific attr expected by ctypes
    _SCREAMING_SNAKE_CASE    				=			[("""size""", ctypes.c_int), ("""visible""", ctypes.c_byte)]



def   __A		(			):



 """simple docstring"""


 if os.name == "nt":
  __a       =  CursorInfo()
  __a       =  ctypes.windll.kernelaa.GetStdHandle(-11					)
  ctypes.windll.kernelaa.GetConsoleCursorInfo(_A							,				ctypes.byref(_A					)					)
  __a       =  False
  ctypes.windll.kernelaa.SetConsoleCursorInfo(_A							,				ctypes.byref(_A					)					)
 elif os.name == "posix":
  sys.stdout.write("\033[?25l"					)
  sys.stdout.flush()



def   __A		(			):



 """simple docstring"""


 if os.name == "nt":
  __a       =  CursorInfo()
  __a       =  ctypes.windll.kernelaa.GetStdHandle(-11					)
  ctypes.windll.kernelaa.GetConsoleCursorInfo(_A							,				ctypes.byref(_A					)					)
  __a       =  True
  ctypes.windll.kernelaa.SetConsoleCursorInfo(_A							,				ctypes.byref(_A					)					)
 elif os.name == "posix":
  sys.stdout.write("\033[?25h"					)
  sys.stdout.flush()






@contextmanager
def   __A		(			):



 """simple docstring"""


 try:
  hide_cursor()
  yield
 finally:
  show_cursor()


