




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 		UpperCamelCase__	(lowerCAmelCase__    ,				lowerCAmelCase__     ):

    '''simple docstring'''



    lowerCamelCase_      :	Optional[Any]	     =      """pixel_values"""
    lowerCamelCase_      :	Optional[Any]	     =      False
    lowerCamelCase_      :	str	     =      TimmBackboneConfig







    def __init__(    self     ,  UpperCamelCase__     ,  **UpperCamelCase__     )     ->      List[Any]:
           requires_backends(self     ,  "timm"     )
           super().__init__(UpperCamelCase__     )
           lowerCamelCase   :      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(UpperCamelCase__     ,  "out_features"     ) and config.out_features is not None:
                  raise ValueError("out_features is not supported by TimmBackbone. Please use out_indices instead."     )

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

           lowerCamelCase   :      Optional[Any]			 =					timm.create_model(
               config.backbone     ,  pretrained=UpperCamelCase__     ,  features_only=config.features_only     ,  in_chans=config.num_channels     ,  out_indices=UpperCamelCase__     ,  **UpperCamelCase__     ,  )
           # 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   :      List[Any]			 =					self._backbone.return_layers
           lowerCamelCase   :      Optional[int]			 =					{layer["module"]: str(UpperCamelCase__     ) for i, layer in enumerate(self._backbone.feature_info.info     )}
           super()._init_backbone(UpperCamelCase__     )







    @classmethod
    def     _lowercase     (    cls     ,  UpperCamelCase__     ,  *UpperCamelCase__     ,  **UpperCamelCase__     )     ->      List[Any]:
           requires_backends(cls     ,  ["vision", "timm"]     )
           from ...models.timm_backbone import TimmBackboneConfig

           lowerCamelCase   :      Dict			 =					kwargs.pop("config"     ,  TimmBackboneConfig()     )

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

           lowerCamelCase   :      Any			 =					kwargs.pop("num_channels"     ,  config.num_channels     )
           lowerCamelCase   :      Tuple			 =					kwargs.pop("features_only"     ,  config.features_only     )
           lowerCamelCase   :      Optional[Any]			 =					kwargs.pop("use_pretrained_backbone"     ,  config.use_pretrained_backbone     )
           lowerCamelCase   :      Optional[int]			 =					kwargs.pop("out_indices"     ,  config.out_indices     )
           lowerCamelCase   :      Optional[Any]			 =					TimmBackboneConfig(
               backbone=UpperCamelCase__     ,  num_channels=UpperCamelCase__     ,  features_only=UpperCamelCase__     ,  use_pretrained_backbone=UpperCamelCase__     ,  out_indices=UpperCamelCase__     ,  )
           return super()._from_config(UpperCamelCase__     ,  **UpperCamelCase__     )







    def     _lowercase     (    self     ,  UpperCamelCase__     )     ->      Union[str, Any]:
           pass







    def     _lowercase     (    self     ,  UpperCamelCase__     ,  UpperCamelCase__=None     ,  UpperCamelCase__=None     ,  UpperCamelCase__=None     ,  **UpperCamelCase__     )     ->      Union[BackboneOutput, Tuple[Tensor, ...]]:
           lowerCamelCase   :      Any			 =					return_dict if return_dict is not None else self.config.use_return_dict
           lowerCamelCase   :      Tuple			 =					(
               output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states
           )
           lowerCamelCase   :      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
                  lowerCamelCase   :      Any			 =					self._all_layers
                  lowerCamelCase   :      Union[str, Any]			 =					self._backbone(UpperCamelCase__     ,  **UpperCamelCase__     )
                  lowerCamelCase   :      int			 =					self._return_layers
                  lowerCamelCase   :      List[str]			 =					tuple(hidden_states[i] for i in self.out_indices     )
           else:
                  lowerCamelCase   :      Optional[int]			 =					self._backbone(UpperCamelCase__     ,  **UpperCamelCase__     )
                  lowerCamelCase   :      Optional[Any]			 =					None

           lowerCamelCase   :      Optional[int]			 =					tuple(UpperCamelCase__     )
           lowerCamelCase   :      List[str]			 =					tuple(UpperCamelCase__     ) if hidden_states is not None else None

           if not return_dict:
                  lowerCamelCase   :      Dict			 =					(feature_maps,)
                  if output_hidden_states:
                         lowerCamelCase   :      List[Any]			 =					output + (hidden_states,)
                  return output

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












import os
import tempfile
import unittest

from transformers.models.marian.convert_marian_tatoeba_to_pytorch import DEFAULT_REPO, TatoebaConverter
from transformers.testing_utils import slow
from transformers.utils import cached_property




@unittest.skipUnless(os.path.exists(lowerCAmelCase__     )    ,				"""Tatoeba directory does not exist."""     )
class 		UpperCamelCase__	(unittest.TestCase     ):

    '''simple docstring'''



    @cached_property
    def     _lowercase     (    self     )     ->      int:
           lowerCamelCase   :      str			 =					tempfile.mkdtemp()
           return TatoebaConverter(save_dir=UpperCamelCase__     )







    @slow
    def     _lowercase     (    self     )     ->      List[Any]:
           self.resolver.convert_models(["heb-eng"]     )







    @slow
    def     _lowercase     (    self     )     ->      Tuple:
           lowerCamelCase ,      lowerCamelCase   :      Dict			 =					self.resolver.write_model_card("opus-mt-he-en"     ,  dry_run=UpperCamelCase__     )
           assert mmeta["long_pair"] == "heb-eng"






