






import enum
import inspect
from typing import Iterable, List, Optional, Tuple, Union




class        _lowerCAmelCase( enum.Enum      ):





							"""simple docstring"""







							a       :	int								='''timm'''
							a       :	str								='''transformers'''







def 						snake_case      (UpperCAmelCase__			,					UpperCAmelCase__			,					UpperCAmelCase__						) ->       int:
		if stage_names is None:
				raise ValueError('Stage_names must be set for transformers backbones'						)

		if out_features is not None:
				if not isinstance(UpperCAmelCase__			,					(list,)						):
						raise ValueError(F'''out_features must be a list {type(UpperCAmelCase__						)}'''						)
				if any(feat not in stage_names for feat in out_features						):
						raise ValueError(F'''out_features must be a subset of stage_names: {stage_names} got {out_features}'''						)

		if out_indices is not None:
				if not isinstance(UpperCAmelCase__			,					(list, tuple)						):
						raise ValueError(F'''out_indices must be a list or tuple, got {type(UpperCAmelCase__						)}'''						)
				if any(idx >= len(UpperCAmelCase__						) for idx in out_indices						):
						raise ValueError('out_indices must be valid indices for stage_names {stage_names}, got {out_indices}'						)

		if out_features is not None and out_indices is not None:
				if len(UpperCAmelCase__						) != len(UpperCAmelCase__						):
						raise ValueError('out_features and out_indices should have the same length if both are set'						)
				if out_features != [stage_names[idx] for idx in out_indices]:
						raise ValueError('out_features and out_indices should correspond to the same stages if both are set'						)

def 						snake_case      (UpperCAmelCase__			,					UpperCAmelCase__			,					UpperCAmelCase__			,					) ->       Dict:
		if out_indices is None and out_features is None:
				UpperCamelCase_:      str					   =       [len(UpperCAmelCase__						) - 1]
				UpperCamelCase_:      int					   =       [stage_names[-1]]
		elif out_indices is None and out_features is not None:
				UpperCamelCase_:      int					   =       [stage_names.index(UpperCAmelCase__						) for layer in out_features]
		elif out_features is None and out_indices is not None:
				UpperCamelCase_:      Dict					   =       [stage_names[idx] for idx in out_indices]
		return out_features, out_indices

def 						snake_case      (UpperCAmelCase__			,					UpperCAmelCase__			,					UpperCAmelCase__			,					) ->       Tuple[List[str], List[int]]:
		verify_out_features_out_indices(out_features=UpperCAmelCase__			,					out_indices=UpperCAmelCase__			,					stage_names=UpperCAmelCase__						)
		UpperCamelCase_:      int					   =       _align_output_features_output_indices(
		    out_features=UpperCAmelCase__			,					out_indices=UpperCAmelCase__			,					stage_names=UpperCAmelCase__						)
		# Verify that the aligned out_features and out_indices are valid
		verify_out_features_out_indices(out_features=UpperCAmelCase__			,					out_indices=UpperCAmelCase__			,					stage_names=UpperCAmelCase__						)
		return output_features, output_indices




class        _lowerCAmelCase:





							"""simple docstring"""







							a       :	Optional[BackboneType]								=None





							def     _a					(      self						,    _lowerCamelCase							):
									if getattr(self						,    '_backbone'						,    _lowerCamelCase							) is None:
											raise ValueError('self._backbone must be set before calling _init_timm_backbone'							)

									# These will diagree with the defaults for the transformers models e.g. for resnet50
									# the transformer model has out_features = ['stem', 'stage1', 'stage2', 'stage3', 'stage4']
									# the timm model has out_features = ['act', 'layer1', 'layer2', 'layer3', 'layer4']
									UpperCamelCase_:      Optional[Any]					   =       [stage['module'] for stage in self._backbone.feature_info.info]
									UpperCamelCase_:      Tuple					   =       [stage['num_chs'] for stage in self._backbone.feature_info.info]
									UpperCamelCase_:      List[str]					   =       self._backbone.feature_info.out_indices
									UpperCamelCase_:      str					   =       self._backbone.feature_info.module_name()

									# We verify the out indices and out features are valid
									verify_out_features_out_indices(
									    out_features=_lowerCamelCase						,    out_indices=_lowerCamelCase						,    stage_names=self.stage_names							)
									UpperCamelCase_:      List[str]					   =       out_features, out_indices





							def     _a					(      self						,    _lowerCamelCase							):
									UpperCamelCase_:      Optional[Any]					   =       getattr(_lowerCamelCase						,    'stage_names'							)
									UpperCamelCase_:      Dict					   =       getattr(_lowerCamelCase						,    'out_features'						,    _lowerCamelCase							)
									UpperCamelCase_:      Union[str, Any]					   =       getattr(_lowerCamelCase						,    'out_indices'						,    _lowerCamelCase							)

									UpperCamelCase_:      List[str]					   =       stage_names
									UpperCamelCase_:      Optional[Any]					   =       get_aligned_output_features_output_indices(
									    out_features=_lowerCamelCase						,    out_indices=_lowerCamelCase						,    stage_names=_lowerCamelCase							)
									# Number of channels for each stage. This is set in the transformer backbone model init
									UpperCamelCase_:      str					   =       None





							def     _a					(      self						,    _lowerCamelCase							):
									UpperCamelCase_:      Optional[int]					   =       config

									UpperCamelCase_:      str					   =       getattr(_lowerCamelCase						,    'use_timm_backbone'						,    _lowerCamelCase							)
									UpperCamelCase_:      int					   =       BackboneType.TIMM if self.use_timm_backbone else BackboneType.TRANSFORMERS

									if self.backbone_type == BackboneType.TIMM:
											self._init_timm_backbone(_lowerCamelCase							)
									elif self.backbone_type == BackboneType.TRANSFORMERS:
											self._init_transformers_backbone(_lowerCamelCase							)
									else:
											raise ValueError(f'''backbone_type {self.backbone_type} not supported.'''							)





							@property
							def     _a					(      self							):
									return self._out_features





							@out_features.setter
							def     _a					(      self						,    _lowerCamelCase							):
									UpperCamelCase_:      Optional[Any]					   =       get_aligned_output_features_output_indices(
									    out_features=_lowerCamelCase						,    out_indices=_lowerCamelCase						,    stage_names=self.stage_names							)





							@property
							def     _a					(      self							):
									return self._out_indices





							@out_indices.setter
							def     _a					(      self						,    _lowerCamelCase							):
									UpperCamelCase_:      Optional[Any]					   =       get_aligned_output_features_output_indices(
									    out_features=_lowerCamelCase						,    out_indices=_lowerCamelCase						,    stage_names=self.stage_names							)





							@property
							def     _a					(      self							):
									# the current backbones will output the number of channels for each stage
									# even if that stage is not in the out_features list.
									return {stage: self.num_features[i] for i, stage in enumerate(self.stage_names							)}





							@property
							def     _a					(      self							):
									return [self.out_feature_channels[name] for name in self.out_features]





							def     _a					(      self						,    *_lowerCamelCase						,    **_lowerCamelCase							):
									UpperCamelCase_:      str					   =       dict(inspect.signature(self.forward							).parameters							)
									UpperCamelCase_:      str					   =       {k: v for k, v in kwargs.items() if k in signature}
									return self(*_lowerCamelCase						,    **_lowerCamelCase							)





							def     _a					(      self						,    _lowerCamelCase						,    _lowerCamelCase = None						,    _lowerCamelCase = None						,    _lowerCamelCase = None						,    ):
									raise NotImplementedError('This method should be implemented by the derived class.'							)





							def     _a					(      self							):
									UpperCamelCase_:      List[Any]					   =       super().to_dict()
									UpperCamelCase_:      str					   =       output.pop('_out_features'							)
									UpperCamelCase_:      Dict					   =       output.pop('_out_indices'							)
									return output







class        _lowerCAmelCase:





							"""simple docstring"""







							@property
							def     _a					(      self							):
									return self._out_features





							@out_features.setter
							def     _a					(      self						,    _lowerCamelCase							):
									UpperCamelCase_:      Any					   =       get_aligned_output_features_output_indices(
									    out_features=_lowerCamelCase						,    out_indices=_lowerCamelCase						,    stage_names=self.stage_names							)





							@property
							def     _a					(      self							):
									return self._out_indices





							@out_indices.setter
							def     _a					(      self						,    _lowerCamelCase							):
									UpperCamelCase_:      Optional[int]					   =       get_aligned_output_features_output_indices(
									    out_features=_lowerCamelCase						,    out_indices=_lowerCamelCase						,    stage_names=self.stage_names							)





							def     _a					(      self							):
									UpperCamelCase_:      Optional[int]					   =       super().to_dict()
									UpperCamelCase_:      List[Any]					   =       output.pop('_out_features'							)
									UpperCamelCase_:      Optional[int]					   =       output.pop('_out_indices'							)
									return output







from typing import TYPE_CHECKING

from ...utils import (
    OptionalDependencyNotAvailable,
    _LazyModule,
    is_tf_available,
    is_tokenizers_available,
    is_torch_available,
)


A_	:	List[Any]				=				{
    'configuration_openai': ['OPENAI_GPT_PRETRAINED_CONFIG_ARCHIVE_MAP', 'OpenAIGPTConfig'],
    'tokenization_openai': ['OpenAIGPTTokenizer'],
}

try:
   if not is_tokenizers_available():
      raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
   pass
else:
   A_	:	List[str]				=				['OpenAIGPTTokenizerFast']

try:
   if not is_torch_available():
      raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
   pass
else:
   A_	:	Any				=				[
       'OPENAI_GPT_PRETRAINED_MODEL_ARCHIVE_LIST',
       'OpenAIGPTDoubleHeadsModel',
       'OpenAIGPTForSequenceClassification',
       'OpenAIGPTLMHeadModel',
       'OpenAIGPTModel',
       'OpenAIGPTPreTrainedModel',
       'load_tf_weights_in_openai_gpt',
   ]

try:
   if not is_tf_available():
      raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
   pass
else:
   A_	:	str				=				[
       'TF_OPENAI_GPT_PRETRAINED_MODEL_ARCHIVE_LIST',
       'TFOpenAIGPTDoubleHeadsModel',
       'TFOpenAIGPTForSequenceClassification',
       'TFOpenAIGPTLMHeadModel',
       'TFOpenAIGPTMainLayer',
       'TFOpenAIGPTModel',
       'TFOpenAIGPTPreTrainedModel',
   ]


if TYPE_CHECKING:
   from .configuration_openai import OPENAI_GPT_PRETRAINED_CONFIG_ARCHIVE_MAP, OpenAIGPTConfig
   from .tokenization_openai import OpenAIGPTTokenizer

   try:
      if not is_tokenizers_available():
         raise OptionalDependencyNotAvailable()
   except OptionalDependencyNotAvailable:
      pass
   else:
      from .tokenization_openai_fast import OpenAIGPTTokenizerFast

   try:
      if not is_torch_available():
         raise OptionalDependencyNotAvailable()
   except OptionalDependencyNotAvailable:
      pass
   else:
      from .modeling_openai import (
          OPENAI_GPT_PRETRAINED_MODEL_ARCHIVE_LIST,
          OpenAIGPTDoubleHeadsModel,
          OpenAIGPTForSequenceClassification,
          OpenAIGPTLMHeadModel,
          OpenAIGPTModel,
          OpenAIGPTPreTrainedModel,
          load_tf_weights_in_openai_gpt,
      )

   try:
      if not is_tf_available():
         raise OptionalDependencyNotAvailable()
   except OptionalDependencyNotAvailable:
      pass
   else:
      from .modeling_tf_openai import (
          TF_OPENAI_GPT_PRETRAINED_MODEL_ARCHIVE_LIST,
          TFOpenAIGPTDoubleHeadsModel,
          TFOpenAIGPTForSequenceClassification,
          TFOpenAIGPTLMHeadModel,
          TFOpenAIGPTMainLayer,
          TFOpenAIGPTModel,
          TFOpenAIGPTPreTrainedModel,
      )

else:
   import sys

   A_	:	str				=				_LazyModule(__name__, globals()['__file__'], _import_structure, module_spec=__spec__)