


"""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 				UpperCamelCase_			(				UpperCamelCase    ,						UpperCamelCase):

	"""simple docstring"""





	snake_case__       :						Any					      =    "pixel_values"
	snake_case__       :						Any					      =    False
	snake_case__       :						Tuple					      =    TimmBackboneConfig




	def __init__(				self    :       Optional[int]    ,    UpperCAmelCase__    :       Tuple    ,    **UpperCAmelCase__    :       Optional[int]     ) ->			Dict:
						requires_backends(self    ,    "timm"     )
						super().__init__(UpperCAmelCase__     )
						__SCREAMING_SNAKE_CASE		     =       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."     )

						__SCREAMING_SNAKE_CASE		     =       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.
						__SCREAMING_SNAKE_CASE		     =       config.out_indices if getattr(UpperCAmelCase__    ,    "out_indices"    ,    UpperCAmelCase__     ) is not None else (-1,)

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




	@classmethod
	def 						UpperCAmelCase_   (				cls    :       List[Any]    ,    UpperCAmelCase__    :       List[str]    ,    *UpperCAmelCase__    :       Optional[int]    ,    **UpperCAmelCase__    :       Tuple     ) ->			Tuple:
						requires_backends(cls    ,    ["vision", "timm"]     )
						from ...models.timm_backbone import TimmBackboneConfig

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

						__SCREAMING_SNAKE_CASE		     =       kwargs.pop("use_timm_backbone"    ,    UpperCAmelCase__     )
						if not use_timm:
											raise ValueError("use_timm_backbone must be True for timm backbones"     )

						__SCREAMING_SNAKE_CASE		     =       kwargs.pop("num_channels"    ,    config.num_channels     )
						__SCREAMING_SNAKE_CASE		     =       kwargs.pop("features_only"    ,    config.features_only     )
						__SCREAMING_SNAKE_CASE		     =       kwargs.pop("use_pretrained_backbone"    ,    config.use_pretrained_backbone     )
						__SCREAMING_SNAKE_CASE		     =       kwargs.pop("out_indices"    ,    config.out_indices     )
						__SCREAMING_SNAKE_CASE		     =       TimmBackboneConfig(
						    backbone=UpperCAmelCase__    ,    num_channels=UpperCAmelCase__    ,    features_only=UpperCAmelCase__    ,    use_pretrained_backbone=UpperCAmelCase__    ,    out_indices=UpperCAmelCase__    ,    )
						return super()._from_config(UpperCAmelCase__    ,    **UpperCAmelCase__     )




	def 						UpperCAmelCase_   (				self    :       Tuple    ,    UpperCAmelCase__    :       Tuple     ) ->			Union[str, Any]:
						pass




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

						__SCREAMING_SNAKE_CASE		     =       tuple(UpperCAmelCase__     )
						__SCREAMING_SNAKE_CASE		     =       tuple(UpperCAmelCase__     ) if hidden_states is not None else None

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

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




"""simple docstring"""







import platform
from argparse import ArgumentParser

import huggingface_hub

from .. import __version__ as version
from ..utils import is_accelerate_available, is_torch_available, is_transformers_available, is_xformers_available
from . import BaseDiffusersCLICommand




def      UpperCAmelCase__ (lowerCAmelCase_     ):




					'''simple docstring'''


					return EnvironmentCommand()




class 				UpperCamelCase_			(				UpperCamelCase):

	"""simple docstring"""





	@staticmethod
	def 						UpperCAmelCase_   (				UpperCAmelCase__    :       ArgumentParser     ) ->			Optional[Any]:
						__SCREAMING_SNAKE_CASE		     =       parser.add_parser("env"     )
						download_parser.set_defaults(func=UpperCAmelCase__     )




	def 						UpperCAmelCase_   (				self    :       List[Any]     ) ->			List[str]:
						__SCREAMING_SNAKE_CASE		     =       huggingface_hub.__version__

						__SCREAMING_SNAKE_CASE		     =       "not installed"
						__SCREAMING_SNAKE_CASE		     =       "NA"
						if is_torch_available():
											import torch

											__SCREAMING_SNAKE_CASE		     =       torch.__version__
											__SCREAMING_SNAKE_CASE		     =       torch.cuda.is_available()

						__SCREAMING_SNAKE_CASE		     =       "not installed"
						if is_transformers_available():
											import transformers

											__SCREAMING_SNAKE_CASE		     =       transformers.__version__

						__SCREAMING_SNAKE_CASE		     =       "not installed"
						if is_accelerate_available():
											import accelerate

											__SCREAMING_SNAKE_CASE		     =       accelerate.__version__

						__SCREAMING_SNAKE_CASE		     =       "not installed"
						if is_xformers_available():
											import xformers

											__SCREAMING_SNAKE_CASE		     =       xformers.__version__

						__SCREAMING_SNAKE_CASE		     =       {
						    "`diffusers` version": version,
						    "Platform": platform.platform(),
						    "Python version": platform.python_version(),
						    "PyTorch version (GPU?)": F"""{pt_version} ({pt_cuda_available})""",
						    "Huggingface_hub version": hub_version,
						    "Transformers version": transformers_version,
						    "Accelerate version": accelerate_version,
						    "xFormers version": xformers_version,
						    "Using GPU in script?": "<fill in>",
						    "Using distributed or parallel set-up in script?": "<fill in>",
						}

						print("\nCopy-and-paste the text below in your GitHub issue and FILL OUT the two last points.\n"     )
						print(self.format_dict(UpperCAmelCase__     )     )

						return info




	@staticmethod
	def 						UpperCAmelCase_   (				UpperCAmelCase__    :       Optional[Any]     ) ->			str:
						return "\n".join([F"""- {prop}: {val}""" for prop, val in d.items()]     ) + "\n"
