



'''simple docstring'''




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

class      UpperCamelCase_					(				enum.Enum     ):
	lowercase		=					'timm'
	lowercase		=					'transformers'




def  __lowerCamelCase    (   _lowercase      ,							_lowercase      ,							_lowercase						) ->     Optional[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(_lowercase      ,							(list,)						):
																					raise ValueError(F'''out_features must be a list {type(_lowercase						)}'''						)
														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(_lowercase      ,							(list, tuple)						):
																					raise ValueError(F'''out_indices must be a list or tuple, got {type(_lowercase						)}'''						)
														if any(idx >= len(_lowercase						) 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(_lowercase						) != len(_lowercase						):
																					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  __lowerCamelCase    (   _lowercase      ,							_lowercase      ,							_lowercase      ,							) ->     str:
							if out_indices is None and out_features is None:
														UpperCAmelCase	:						Union[str, Any]					=						[len(_lowercase						) - 1]
														UpperCAmelCase	:						Dict					=						[stage_names[-1]]
							elif out_indices is None and out_features is not None:
														UpperCAmelCase	:						Union[str, Any]					=						[stage_names.index(_lowercase						) for layer in out_features]
							elif out_features is None and out_indices is not None:
														UpperCAmelCase	:						str					=						[stage_names[idx] for idx in out_indices]
							return out_features, out_indices



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

class      UpperCamelCase_					:
	lowercase		=					None





	def 					_lowercase(    self						,						A       )     ->     None:
								if getattr(self						,						"""_backbone"""						,						A       ) 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	:						Union[str, Any]					=						[stage["""module"""] for stage in self._backbone.feature_info.info]
								UpperCAmelCase	:						Dict					=						[stage["""num_chs"""] for stage in self._backbone.feature_info.info]
								UpperCAmelCase	:						List[Any]					=						self._backbone.feature_info.out_indices
								UpperCAmelCase	:						Any					=						self._backbone.feature_info.module_name()

								# We verify the out indices and out features are valid
								verify_out_features_out_indices(
								    out_features=A						,						out_indices=A						,						stage_names=self.stage_names       )
								UpperCAmelCase	,	UpperCAmelCase	:						str					=						out_features, out_indices





	def 					_lowercase(    self						,						A       )     ->     None:
								UpperCAmelCase	:						List[str]					=						getattr(A						,						"""stage_names"""       )
								UpperCAmelCase	:						Tuple					=						getattr(A						,						"""out_features"""						,						A       )
								UpperCAmelCase	:						str					=						getattr(A						,						"""out_indices"""						,						A       )

								UpperCAmelCase	:						Dict					=						stage_names
								UpperCAmelCase	,	UpperCAmelCase	:						str					=						get_aligned_output_features_output_indices(
								    out_features=A						,						out_indices=A						,						stage_names=A       )
								# Number of channels for each stage. This is set in the transformer backbone model init
								UpperCAmelCase	:						Union[str, Any]					=						None





	def 					_lowercase(    self						,						A       )     ->     None:
								UpperCAmelCase	:						Optional[Any]					=						config

								UpperCAmelCase	:						Tuple					=						getattr(A						,						"""use_timm_backbone"""						,						A       )
								UpperCAmelCase	:						Optional[int]					=						BackboneType.TIMM if self.use_timm_backbone else BackboneType.TRANSFORMERS

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





	@property
	def 					_lowercase(    self       )     ->     Dict:
								return self._out_features





	@out_features.setter
	def 					_lowercase(    self						,						A       )     ->     Tuple:
								UpperCAmelCase	,	UpperCAmelCase	:						str					=						get_aligned_output_features_output_indices(
								    out_features=A						,						out_indices=A						,						stage_names=self.stage_names       )





	@property
	def 					_lowercase(    self       )     ->     str:
								return self._out_indices





	@out_indices.setter
	def 					_lowercase(    self						,						A       )     ->     int:
								UpperCAmelCase	,	UpperCAmelCase	:						List[str]					=						get_aligned_output_features_output_indices(
								    out_features=A						,						out_indices=A						,						stage_names=self.stage_names       )





	@property
	def 					_lowercase(    self       )     ->     List[str]:
								# 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 					_lowercase(    self       )     ->     int:
								return [self.out_feature_channels[name] for name in self.out_features]





	def 					_lowercase(    self						,						*A						,						**A       )     ->     Optional[int]:
								UpperCAmelCase	:						Any					=						dict(inspect.signature(self.forward       ).parameters       )
								UpperCAmelCase	:						Dict					=						{k: v for k, v in kwargs.items() if k in signature}
								return self(*A						,						**A       )





	def 					_lowercase(    self						,						A						,						A = None						,						A = None						,						A = None						,						)     ->     int:
								raise NotImplementedError("""This method should be implemented by the derived class."""       )






	def 					_lowercase(    self       )     ->     Optional[Any]:
								UpperCAmelCase	:						int					=						super().to_dict()
								UpperCAmelCase	:						int					=						output.pop("""_out_features"""       )
								UpperCAmelCase	:						List[Any]					=						output.pop("""_out_indices"""       )
								return output




class      UpperCamelCase_					:





	@property
	def 					_lowercase(    self       )     ->     Any:
								return self._out_features





	@out_features.setter
	def 					_lowercase(    self						,						A       )     ->     Tuple:
								UpperCAmelCase	,	UpperCAmelCase	:						int					=						get_aligned_output_features_output_indices(
								    out_features=A						,						out_indices=A						,						stage_names=self.stage_names       )





	@property
	def 					_lowercase(    self       )     ->     Union[str, Any]:
								return self._out_indices





	@out_indices.setter
	def 					_lowercase(    self						,						A       )     ->     Tuple:
								UpperCAmelCase	,	UpperCAmelCase	:						List[Any]					=						get_aligned_output_features_output_indices(
								    out_features=A						,						out_indices=A						,						stage_names=self.stage_names       )






	def 					_lowercase(    self       )     ->     int:
								UpperCAmelCase	:						List[Any]					=						super().to_dict()
								UpperCAmelCase	:						Optional[Any]					=						output.pop("""_out_features"""       )
								UpperCAmelCase	:						Tuple					=						output.pop("""_out_indices"""       )
								return output






'''simple docstring'''




import functools
import operator

from ...configuration_utils import PretrainedConfig
from ...utils import logging


a  :     Union[str, Any]       =       logging.get_logger(__name__)

a  :     int       =       {
    """facebook/hubert-base-ls960""": """https://huggingface.co/facebook/hubert-base-ls960/resolve/main/config.json""",
    # See all Hubert models at https://huggingface.co/models?filter=hubert
}

class      UpperCamelCase_					(				__magic_name__     ):
	lowercase		=					'hubert'





	def __init__(    self						,						A=32						,						A=768						,						A=12						,						A=12						,						A=3072						,						A="gelu"						,						A=0.1						,						A=0.1						,						A=0.1						,						A=True						,						A=0.0						,						A=0.1						,						A=0.1						,						A=0.0_2						,						A=1e-5						,						A="group"						,						A="gelu"						,						A=(512, 512, 512, 512, 512, 512, 512)						,						A=(5, 2, 2, 2, 2, 2, 2)						,						A=(10, 3, 3, 3, 3, 2, 2)						,						A=False						,						A=128						,						A=16						,						A=False						,						A=True						,						A=0.0_5						,						A=10						,						A=2						,						A=0.0						,						A=10						,						A=0						,						A="sum"						,						A=False						,						A=False						,						A=256						,						A=0						,						A=1						,						A=2						,						**A						,						)     ->     List[str]:
								super().__init__(**A						,						pad_token_id=A						,						bos_token_id=A						,						eos_token_id=A       )
								UpperCAmelCase	:						List[Any]					=						hidden_size
								UpperCAmelCase	:						List[Any]					=						feat_extract_norm
								UpperCAmelCase	:						Optional[Any]					=						feat_extract_activation
								UpperCAmelCase	:						Optional[int]					=						list(A       )
								UpperCAmelCase	:						int					=						list(A       )
								UpperCAmelCase	:						Any					=						list(A       )
								UpperCAmelCase	:						Any					=						conv_bias
								UpperCAmelCase	:						Dict					=						num_conv_pos_embeddings
								UpperCAmelCase	:						Tuple					=						num_conv_pos_embedding_groups
								UpperCAmelCase	:						str					=						len(self.conv_dim       )
								UpperCAmelCase	:						Optional[Any]					=						num_hidden_layers
								UpperCAmelCase	:						int					=						intermediate_size
								UpperCAmelCase	:						Any					=						hidden_act
								UpperCAmelCase	:						Tuple					=						num_attention_heads
								UpperCAmelCase	:						int					=						hidden_dropout
								UpperCAmelCase	:						Any					=						attention_dropout
								UpperCAmelCase	:						Any					=						activation_dropout
								UpperCAmelCase	:						str					=						feat_proj_layer_norm
								UpperCAmelCase	:						int					=						feat_proj_dropout
								UpperCAmelCase	:						List[Any]					=						final_dropout
								UpperCAmelCase	:						int					=						layerdrop
								UpperCAmelCase	:						Dict					=						layer_norm_eps
								UpperCAmelCase	:						Union[str, Any]					=						initializer_range
								UpperCAmelCase	:						Any					=						vocab_size
								UpperCAmelCase	:						Optional[int]					=						do_stable_layer_norm
								UpperCAmelCase	:						Optional[int]					=						use_weighted_layer_sum
								UpperCAmelCase	:						Dict					=						classifier_proj_size

								if (
								    (len(self.conv_stride       ) != self.num_feat_extract_layers)
								    or (len(self.conv_kernel       ) != self.num_feat_extract_layers)
								    or (len(self.conv_dim       ) != self.num_feat_extract_layers)
								):
															raise ValueError(
															    """Configuration for convolutional layers is incorrect. It is required that `len(config.conv_dim)` =="""
															    """ `len(config.conv_stride)` == `len(config.conv_kernel)`, but is `len(config.conv_dim) ="""
															    f''' {len(self.conv_dim       )}`, `len(config.conv_stride) = {len(self.conv_stride       )}`,'''
															    f''' `len(config.conv_kernel) = {len(self.conv_kernel       )}`.'''       )

								# fine-tuning config parameters for SpecAugment: https://arxiv.org/abs/1904.08779
								UpperCAmelCase	:						Union[str, Any]					=						apply_spec_augment
								UpperCAmelCase	:						Tuple					=						mask_time_prob
								UpperCAmelCase	:						Optional[Any]					=						mask_time_length
								UpperCAmelCase	:						List[Any]					=						mask_time_min_masks
								UpperCAmelCase	:						Any					=						mask_feature_prob
								UpperCAmelCase	:						Any					=						mask_feature_length
								UpperCAmelCase	:						Tuple					=						mask_feature_min_masks

								# ctc loss
								UpperCAmelCase	:						int					=						ctc_loss_reduction
								UpperCAmelCase	:						Any					=						ctc_zero_infinity






	@property
	def 					_lowercase(    self       )     ->     Union[str, Any]:
								return functools.reduce(operator.mul						,						self.conv_stride						,						1       )

