





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 	__lowerCAmelCase      (       _UpperCamelCase		,_UpperCamelCase	):
   _UpperCamelCase			:    int  		=					"""pixel_values"""
   _UpperCamelCase			:    int  		=					False
   _UpperCamelCase			:    int  		=					TimmBackboneConfig





   def __init__(			self       ,      snake_case       ,      **snake_case   )					->	int:
      """simple docstring"""





      requires_backends(self       ,      "timm"   )
      super().__init__(snake_case   )
      a__						:    Optional[int]   		=	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."   )

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

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





   @classmethod
   def 						_snake_case			(			cls       ,      snake_case       ,      *snake_case       ,      **snake_case   )					->	Dict:
      """simple docstring"""





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

      a__						:    Union[str, Any]   		=	kwargs.pop("config"       ,      TimmBackboneConfig()   )

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

      a__						:    int   		=	kwargs.pop("num_channels"       ,      config.num_channels   )
      a__						:    str   		=	kwargs.pop("features_only"       ,      config.features_only   )
      a__						:    List[Any]   		=	kwargs.pop("use_pretrained_backbone"       ,      config.use_pretrained_backbone   )
      a__						:    Tuple   		=	kwargs.pop("out_indices"       ,      config.out_indices   )
      a__						:    int   		=	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 						_snake_case			(			self       ,      snake_case   )					->	Tuple:
      """simple docstring"""





      pass





   def 						_snake_case			(			self       ,      snake_case       ,      snake_case=None       ,      snake_case=None       ,      snake_case=None       ,      **snake_case   )					->	Union[BackboneOutput, Tuple[Tensor, ...]]:
      """simple docstring"""





      a__						:    int   		=	return_dict if return_dict is not None else self.config.use_return_dict
      a__						:    List[str]   		=	(
          output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states
      )
      a__						:    Dict   		=	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__						:    List[str]   		=	self._all_layers
         a__						:    List[Any]   		=	self._backbone(snake_case       ,      **snake_case   )
         a__						:    Union[str, Any]   		=	self._return_layers
         a__						:    List[Any]   		=	tuple(hidden_states[i] for i in self.out_indices   )
      else:
         a__						:    Dict   		=	self._backbone(snake_case       ,      **snake_case   )
         a__						:    Optional[int]   		=	None

      a__						:    List[Any]   		=	tuple(snake_case   )
      a__						:    Union[str, Any]   		=	tuple(snake_case   ) if hidden_states is not None else None

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

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








from ...utils import (
    OptionalDependencyNotAvailable,
    is_torch_available,
    is_transformers_available,
    is_transformers_version,
)


try:
  if not (is_transformers_available() and is_torch_available() and is_transformers_version(""">=""", """4.25.0""")):
    raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
  from ...utils.dummy_torch_and_transformers_objects import (
      VersatileDiffusionDualGuidedPipeline,
      VersatileDiffusionImageVariationPipeline,
      VersatileDiffusionPipeline,
      VersatileDiffusionTextToImagePipeline,
  )
else:
  from .modeling_text_unet import UNetFlatConditionModel
  from .pipeline_versatile_diffusion import VersatileDiffusionPipeline
  from .pipeline_versatile_diffusion_dual_guided import VersatileDiffusionDualGuidedPipeline
  from .pipeline_versatile_diffusion_image_variation import VersatileDiffusionImageVariationPipeline
  from .pipeline_versatile_diffusion_text_to_image import VersatileDiffusionTextToImagePipeline

