



'''simple docstring'''





import copy
import inspect
import unittest

from transformers import AutoBackbone
from transformers.configuration_utils import PretrainedConfig
from transformers.testing_utils import require_timm, require_torch, torch_device
from transformers.utils.import_utils import is_torch_available

from ...test_backbone_common import BackboneTesterMixin
from ...test_configuration_common import ConfigTester
from ...test_modeling_common import ModelTesterMixin, floats_tensor


if is_torch_available():
   import torch

   from transformers import TimmBackbone, TimmBackboneConfig

from ...test_pipeline_mixin import PipelineTesterMixin






class 					lowerCamelCase_    :
  """simple docstring"""




  def __init__( self      :	Union[str, Any]			,	_a      :	Optional[int]			,	_a      :	str=None			,	_a      :	int=None			,	_a      :	Dict=None			,	_a      :	Optional[Any]="resnet50"			,	_a      :	int=3			,	_a      :	Optional[Any]=32			,	_a      :	int=3			,	_a      :	Dict=True			,	_a      :	Optional[int]=True			,	)					->					Dict:
    __lowerCamelCase       :       Tuple           = parent
    __lowerCamelCase       :       int           = out_indices if out_indices is not None else [4]
    __lowerCamelCase       :       Any           = stage_names
    __lowerCamelCase       :       Optional[Any]           = out_features
    __lowerCamelCase       :       Tuple           = backbone
    __lowerCamelCase       :       Dict           = batch_size
    __lowerCamelCase       :       Optional[int]           = image_size
    __lowerCamelCase       :       str           = num_channels
    __lowerCamelCase       :       List[str]           = use_pretrained_backbone
    __lowerCamelCase       :       Tuple           = is_training




  def 						_lowercase     ( self      :	str   )					->					Tuple:
    __lowerCamelCase       :       Optional[Any]           = floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]   )
    __lowerCamelCase       :       Dict           = self.get_config()

    return config, pixel_values




  def 						_lowercase     ( self      :	Union[str, Any]   )					->					Union[str, Any]:
    return TimmBackboneConfig(
        image_size=self.image_size			,	num_channels=self.num_channels			,	out_features=self.out_features			,	out_indices=self.out_indices			,	stage_names=self.stage_names			,	use_pretrained_backbone=self.use_pretrained_backbone			,	backbone=self.backbone			,	)




  def 						_lowercase     ( self      :	Dict			,	_a      :	Tuple			,	_a      :	Tuple   )					->					List[str]:
    __lowerCamelCase       :       Dict           = TimmBackbone(config=_a   )
    model.to(_a   )
    model.eval()
    with torch.no_grad():
      __lowerCamelCase       :       str           = model(_a   )
    self.parent.assertEqual(
        result.feature_map[-1].shape			,	(self.batch_size, model.channels[-1], 14, 14)			,	)




  def 						_lowercase     ( self      :	Any   )					->					List[Any]:
    __lowerCamelCase       :       List[Any]           = self.prepare_config_and_inputs()
    __lowerCamelCase       ,__lowerCamelCase       :       Any           = config_and_inputs
    __lowerCamelCase       :       Any           = {'pixel_values': pixel_values}
    return config, inputs_dict






@require_torch
@require_timm
class 					lowerCamelCase_    (				SCREAMING_SNAKE_CASE_   ,   SCREAMING_SNAKE_CASE_   ,   SCREAMING_SNAKE_CASE_   ,   unittest.TestCase     ):
  """simple docstring"""




  a_												=(TimmBackbone,) if is_torch_available() else ()
  a_												={"""feature-extraction""": TimmBackbone} if is_torch_available() else {}
  a_												=False
  a_												=False
  a_												=False
  a_												=False




  def 						_lowercase     ( self      :	List[str]   )					->					str:
    __lowerCamelCase       :       str           = TimmBackboneModelTester(self   )
    __lowerCamelCase       :       str           = ConfigTester(self			,	config_class=_a			,	has_text_modality=_a   )




  def 						_lowercase     ( self      :	Tuple   )					->					Tuple:
    self.config_tester.create_and_test_config_to_json_string()
    self.config_tester.create_and_test_config_to_json_file()
    self.config_tester.create_and_test_config_from_and_save_pretrained()
    self.config_tester.create_and_test_config_with_num_labels()
    self.config_tester.check_config_can_be_init_without_params()
    self.config_tester.check_config_arguments_init()




  def 						_lowercase     ( self      :	int   )					->					List[Any]:
    __lowerCamelCase       :       int           = 'resnet18'
    __lowerCamelCase       :       Optional[int]           = 'microsoft/resnet-18'

    __lowerCamelCase       :       List[str]           = AutoBackbone.from_pretrained(_a			,	use_timm_backbone=_a   )
    __lowerCamelCase       :       Optional[Any]           = AutoBackbone.from_pretrained(_a   )

    self.assertEqual(len(timm_model.out_features   )			,	len(transformers_model.out_features   )   )
    self.assertEqual(len(timm_model.stage_names   )			,	len(transformers_model.stage_names   )   )
    self.assertEqual(timm_model.channels			,	transformers_model.channels   )
    # Out indices are set to the last layer by default. For timm models, we don't know
    # the number of layers in advance, so we set it to (-1,), whereas for transformers
    # models, we set it to [len(stage_names) - 1] (kept for backward compatibility).
    self.assertEqual(timm_model.out_indices			,	(-1,)   )
    self.assertEqual(transformers_model.out_indices			,	[len(timm_model.stage_names   ) - 1]   )

    __lowerCamelCase       :       List[Any]           = AutoBackbone.from_pretrained(_a			,	use_timm_backbone=_a			,	out_indices=[1, 2, 3]   )
    __lowerCamelCase       :       Dict           = AutoBackbone.from_pretrained(_a			,	out_indices=[1, 2, 3]   )

    self.assertEqual(timm_model.out_indices			,	transformers_model.out_indices   )
    self.assertEqual(len(timm_model.out_features   )			,	len(transformers_model.out_features   )   )
    self.assertEqual(timm_model.channels			,	transformers_model.channels   )




  @unittest.skip('TimmBackbone doesn\'t support feed forward chunking'   )
  def 						_lowercase     ( self      :	List[str]   )					->					Any:
    pass




  @unittest.skip('TimmBackbone doesn\'t have num_hidden_layers attribute'   )
  def 						_lowercase     ( self      :	Dict   )					->					Tuple:
    pass




  @unittest.skip('TimmBackbone initialization is managed on the timm side'   )
  def 						_lowercase     ( self      :	List[Any]   )					->					List[Any]:
    pass




  @unittest.skip('TimmBackbone models doesn\'t have inputs_embeds'   )
  def 						_lowercase     ( self      :	str   )					->					Optional[Any]:
    pass




  @unittest.skip('TimmBackbone models doesn\'t have inputs_embeds'   )
  def 						_lowercase     ( self      :	List[Any]   )					->					List[Any]:
    pass




  @unittest.skip('TimmBackbone model cannot be created without specifying a backbone checkpoint'   )
  def 						_lowercase     ( self      :	int   )					->					Any:
    pass




  @unittest.skip('Only checkpoints on timm can be loaded into TimmBackbone'   )
  def 						_lowercase     ( self      :	int   )					->					Optional[Any]:
    pass




  @unittest.skip('model weights aren\'t tied in TimmBackbone.'   )
  def 						_lowercase     ( self      :	Dict   )					->					Optional[int]:
    pass




  @unittest.skip('model weights aren\'t tied in TimmBackbone.'   )
  def 						_lowercase     ( self      :	Union[str, Any]   )					->					List[Any]:
    pass




  @unittest.skip('Only checkpoints on timm can be loaded into TimmBackbone'   )
  def 						_lowercase     ( self      :	Any   )					->					List[str]:
    pass




  @unittest.skip('Only checkpoints on timm can be loaded into TimmBackbone'   )
  def 						_lowercase     ( self      :	List[str]   )					->					Optional[int]:
    pass




  @unittest.skip('TimmBackbone doesn\'t have hidden size info in its configuration.'   )
  def 						_lowercase     ( self      :	str   )					->					List[Any]:
    pass




  @unittest.skip('TimmBackbone doesn\'t support output_attentions.'   )
  def 						_lowercase     ( self      :	Dict   )					->					str:
    pass




  @unittest.skip('Safetensors is not supported by timm.'   )
  def 						_lowercase     ( self      :	Tuple   )					->					Optional[int]:
    pass




  @unittest.skip('Will be fixed soon by reducing the size of the model used for common tests.'   )
  def 						_lowercase     ( self      :	Tuple   )					->					int:
    pass




  def 						_lowercase     ( self      :	str   )					->					str:
    __lowerCamelCase       ,__lowerCamelCase       :       List[str]           = self.model_tester.prepare_config_and_inputs_for_common()

    for model_class in self.all_model_classes:
      __lowerCamelCase       :       Dict           = model_class(_a   )
      __lowerCamelCase       :       int           = inspect.signature(model.forward   )
      # signature.parameters is an OrderedDict => so arg_names order is deterministic
      __lowerCamelCase       :       Dict           = [*signature.parameters.keys()]

      __lowerCamelCase       :       List[str]           = ['pixel_values']
      self.assertListEqual(arg_names[:1]			,	_a   )




  def 						_lowercase     ( self      :	Union[str, Any]   )					->					Optional[Any]:
    __lowerCamelCase       ,__lowerCamelCase       :       List[str]           = self.model_tester.prepare_config_and_inputs_for_common()
    __lowerCamelCase       :       Union[str, Any]           = True
    __lowerCamelCase       :       str           = self.has_attentions

    # no need to test all models as different heads yield the same functionality
    __lowerCamelCase       :       int           = self.all_model_classes[0]
    __lowerCamelCase       :       int           = model_class(_a   )
    model.to(_a   )

    __lowerCamelCase       :       List[str]           = self._prepare_for_class(_a			,	_a   )
    __lowerCamelCase       :       Any           = model(**_a   )
    __lowerCamelCase       :       Optional[Any]           = outputs[0][-1]

    # Encoder-/Decoder-only models
    __lowerCamelCase       :       str           = outputs.hidden_states[0]
    hidden_states.retain_grad()

    if self.has_attentions:
      __lowerCamelCase       :       str           = outputs.attentions[0]
      attentions.retain_grad()

    output.flatten()[0].backward(retain_graph=_a   )

    self.assertIsNotNone(hidden_states.grad   )

    if self.has_attentions:
      self.assertIsNotNone(attentions.grad   )




  def 						_lowercase     ( self      :	Union[str, Any]   )					->					Any:
    __lowerCamelCase       ,__lowerCamelCase       :       int           = self.model_tester.prepare_config_and_inputs_for_common()

    for model_class in self.all_model_classes:
      __lowerCamelCase       :       Dict           = model_class(_a   )
      model.to(_a   )
      model.eval()
      __lowerCamelCase       :       List[str]           = model(**_a   )

      self.assertEqual(len(result.feature_maps   )			,	len(config.out_indices   )   )
      self.assertEqual(len(model.channels   )			,	len(config.out_indices   )   )

      # Check output of last stage is taken if out_features=None, out_indices=None
      __lowerCamelCase       :       Optional[Any]           = copy.deepcopy(_a   )
      __lowerCamelCase       :       int           = None
      __lowerCamelCase       :       Dict           = model_class(_a   )
      model.to(_a   )
      model.eval()
      __lowerCamelCase       :       str           = model(**_a   )

      self.assertEqual(len(result.feature_maps   )			,	1   )
      self.assertEqual(len(model.channels   )			,	1   )

      # Check backbone can be initialized with fresh weights
      __lowerCamelCase       :       Tuple           = copy.deepcopy(_a   )
      __lowerCamelCase       :       Any           = False
      __lowerCamelCase       :       str           = model_class(_a   )
      model.to(_a   )
      model.eval()
      __lowerCamelCase       :       Optional[int]           = model(**_a   )











'''simple docstring'''





from typing import TYPE_CHECKING

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


_UpperCamelCase											=						{'configuration_speech_encoder_decoder': ['SpeechEncoderDecoderConfig']}

try:
   if not is_torch_available():
      raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
   pass
else:
   _UpperCamelCase											=						['SpeechEncoderDecoderModel']

try:
   if not is_flax_available():
      raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
   pass
else:
   _UpperCamelCase											=						['FlaxSpeechEncoderDecoderModel']

if TYPE_CHECKING:
   from .configuration_speech_encoder_decoder import SpeechEncoderDecoderConfig

   try:
      if not is_torch_available():
         raise OptionalDependencyNotAvailable()
   except OptionalDependencyNotAvailable:
      pass
   else:
      from .modeling_speech_encoder_decoder import SpeechEncoderDecoderModel

   try:
      if not is_flax_available():
         raise OptionalDependencyNotAvailable()
   except OptionalDependencyNotAvailable:
      pass
   else:
      from .modeling_flax_speech_encoder_decoder import FlaxSpeechEncoderDecoderModel

else:
   import sys

   _UpperCamelCase											=						_LazyModule(__name__, globals()['__file__'], _import_structure, module_spec=__spec__)






