





"""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       a							(   a__   , a__  ):
 snake_case__             =       '''pixel_values'''
 snake_case__             =       False
 snake_case__             =       TimmBackboneConfig



 def __init__(     self				,       _snake_case				,       **_snake_case     ):







  """simple docstring"""






  requires_backends(self				,       'timm'     )
  super().__init__(_snake_case     )
  lowerCAmelCase    				= 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(_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.'     )

  lowerCAmelCase    				= getattr(_snake_case				,       'use_pretrained_backbone'				,       _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.
  lowerCAmelCase    				= config.out_indices if getattr(_snake_case				,       'out_indices'				,       _snake_case     ) is not None else (-1,)

  lowerCAmelCase    				= timm.create_model(
      config.backbone				,       pretrained=_snake_case				,       features_only=config.features_only				,       in_chans=config.num_channels				,       out_indices=_snake_case				,       **_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.
  lowerCAmelCase    				= self._backbone.return_layers
  lowerCAmelCase    				= {layer['module']: str(_snake_case     ) for i, layer in enumerate(self._backbone.feature_info.info     )}
  super()._init_backbone(_snake_case     )



 @classmethod
 def    UpperCamelCase__  (     cls				,       _snake_case				,       *_snake_case				,       **_snake_case     ):







  """simple docstring"""






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

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

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

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



 def    UpperCamelCase__  (     self				,       _snake_case     ):







  """simple docstring"""






  pass



 def    UpperCamelCase__  (     self				,       _snake_case				,       _snake_case=None				,       _snake_case=None				,       _snake_case=None				,       **_snake_case     ):







  """simple docstring"""






  lowerCAmelCase    				= return_dict if return_dict is not None else self.config.use_return_dict
  lowerCAmelCase    				= (
      output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states
  )
  lowerCAmelCase    				= 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
   lowerCAmelCase    				= self._all_layers
   lowerCAmelCase    				= self._backbone(_snake_case				,       **_snake_case     )
   lowerCAmelCase    				= self._return_layers
   lowerCAmelCase    				= tuple(hidden_states[i] for i in self.out_indices     )
  else:
   lowerCAmelCase    				= self._backbone(_snake_case				,       **_snake_case     )
   lowerCAmelCase    				= None

  lowerCAmelCase    				= tuple(_snake_case     )
  lowerCAmelCase    				= tuple(_snake_case     ) if hidden_states is not None else None

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

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






"""simple docstring"""



import unittest

from transformers import load_tool

from .test_tools_common import ToolTesterMixin
class 						__UpperCAmelCase    (unittest.TestCase							,   __A     ):







			'''simple docstring'''





			def 		lowerCamelCase					(			self							):







										'''simple docstring'''


										A__       :     List[Any]								=			load_tool("""text-classification"""							)
										self.tool.setup()
										A__       :     Any								=			load_tool("""text-classification"""   ,      remote=snake_case_							)





			def 		lowerCamelCase					(			self							):







										'''simple docstring'''


										A__       :     int								=			self.tool("""That's quite cool"""   ,      ["""positive""", """negative"""]							)
										self.assertEqual(snake_case_   ,      """positive"""							)





			def 		lowerCamelCase					(			self							):







										'''simple docstring'''


										A__       :     Optional[Any]								=			self.remote_tool("""That's quite cool"""   ,      ["""positive""", """negative"""]							)
										self.assertEqual(snake_case_   ,      """positive"""							)





			def 		lowerCamelCase					(			self							):







										'''simple docstring'''


										A__       :     str								=			self.tool(text="""That's quite cool"""   ,      labels=["""positive""", """negative"""]							)
										self.assertEqual(snake_case_   ,      """positive"""							)





			def 		lowerCamelCase					(			self							):







										'''simple docstring'''


										A__       :     int								=			self.remote_tool(text="""That's quite cool"""   ,      labels=["""positive""", """negative"""]							)
										self.assertEqual(snake_case_   ,      """positive"""							)





