





import functools
import operator

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


UpperCamelCase__           =			logging.get_logger(__name__)

UpperCamelCase__           =			{
    "asapp/sew-tiny-100k": "https://huggingface.co/asapp/sew-tiny-100k/resolve/main/config.json",
    # See all SEW models at https://huggingface.co/models?filter=sew
}

class 							__SCREAMING_SNAKE_CASE       (  _a							):
							snake_case  :   Optional[Any]      =							"""sew"""






							def __init__(    self	,			__lowerCAmelCase=32	,			__lowerCAmelCase=768	,			__lowerCAmelCase=12	,			__lowerCAmelCase=12	,			__lowerCAmelCase=3072	,			__lowerCAmelCase=2	,			__lowerCAmelCase="gelu"	,			__lowerCAmelCase=0.1	,			__lowerCAmelCase=0.1	,			__lowerCAmelCase=0.1	,			__lowerCAmelCase=0.0	,			__lowerCAmelCase=0.1	,			__lowerCAmelCase=0.1	,			__lowerCAmelCase=0.02	,			__lowerCAmelCase=1E-5	,			__lowerCAmelCase="group"	,			__lowerCAmelCase="gelu"	,			__lowerCAmelCase=(64, 128, 128, 128, 128, 256, 256, 256, 256, 512, 512, 512, 512)	,			__lowerCAmelCase=(5, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1)	,			__lowerCAmelCase=(10, 3, 1, 3, 1, 3, 1, 3, 1, 2, 1, 2, 1)	,			__lowerCAmelCase=False	,			__lowerCAmelCase=128	,			__lowerCAmelCase=16	,			__lowerCAmelCase=True	,			__lowerCAmelCase=0.05	,			__lowerCAmelCase=10	,			__lowerCAmelCase=2	,			__lowerCAmelCase=0.0	,			__lowerCAmelCase=10	,			__lowerCAmelCase=0	,			__lowerCAmelCase="mean"	,			__lowerCAmelCase=False	,			__lowerCAmelCase=False	,			__lowerCAmelCase=256	,			__lowerCAmelCase=0	,			__lowerCAmelCase=1	,			__lowerCAmelCase=2	,			**__lowerCAmelCase	,			):
														super().__init__(**__lowerCAmelCase	,			pad_token_id=__lowerCAmelCase	,			bos_token_id=__lowerCAmelCase	,			eos_token_id=__lowerCAmelCase	)
														UpperCamelCase__					  =      hidden_size
														UpperCamelCase__					  =      feat_extract_norm
														UpperCamelCase__					  =      feat_extract_activation
														UpperCamelCase__					  =      list(__lowerCAmelCase	)
														UpperCamelCase__					  =      list(__lowerCAmelCase	)
														UpperCamelCase__					  =      list(__lowerCAmelCase	)
														UpperCamelCase__					  =      conv_bias
														UpperCamelCase__					  =      num_conv_pos_embeddings
														UpperCamelCase__					  =      num_conv_pos_embedding_groups
														UpperCamelCase__					  =      len(self.conv_dim	)
														UpperCamelCase__					  =      num_hidden_layers
														UpperCamelCase__					  =      intermediate_size
														UpperCamelCase__					  =      squeeze_factor
														UpperCamelCase__					  =      hidden_act
														UpperCamelCase__					  =      num_attention_heads
														UpperCamelCase__					  =      hidden_dropout
														UpperCamelCase__					  =      attention_dropout
														UpperCamelCase__					  =      activation_dropout
														UpperCamelCase__					  =      feat_proj_dropout
														UpperCamelCase__					  =      final_dropout
														UpperCamelCase__					  =      layerdrop
														UpperCamelCase__					  =      layer_norm_eps
														UpperCamelCase__					  =      initializer_range
														UpperCamelCase__					  =      vocab_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)`,"""
																					    f"""but is `len(config.conv_dim) = {len(self.conv_dim	)}`, `len(config.conv_stride)"""
																					    f"""= {len(self.conv_stride	)}`, `len(config.conv_kernel) = {len(self.conv_kernel	)}`."""	)

														# fine-tuning config parameters for SpecAugment: https://arxiv.org/abs/1904.08779
														UpperCamelCase__					  =      apply_spec_augment
														UpperCamelCase__					  =      mask_time_prob
														UpperCamelCase__					  =      mask_time_length
														UpperCamelCase__					  =      mask_time_min_masks
														UpperCamelCase__					  =      mask_feature_prob
														UpperCamelCase__					  =      mask_feature_length
														UpperCamelCase__					  =      mask_feature_min_masks

														# ctc loss
														UpperCamelCase__					  =      ctc_loss_reduction
														UpperCamelCase__					  =      ctc_zero_infinity

														# sequence classification
														UpperCamelCase__					  =      use_weighted_layer_sum
														UpperCamelCase__					  =      classifier_proj_size







							@property
							def 					_lowerCamelCase		(    self	):
														return functools.reduce(operator.mul	,			self.conv_stride	,			1	)











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 							__SCREAMING_SNAKE_CASE       (  _a ,		_a							):
							snake_case  :   int      =							"""pixel_values"""
							snake_case  :   List[Any]      =							False
							snake_case  :   str      =							TimmBackboneConfig






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

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

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






							@classmethod
							def 					_lowerCamelCase		(    cls	,			__lowerCAmelCase	,			*__lowerCAmelCase	,			**__lowerCAmelCase	):
														requires_backends(cls	,			["""vision""", """timm"""]	)
														from ...models.timm_backbone import TimmBackboneConfig

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

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

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






							def 					_lowerCamelCase		(    self	,			__lowerCAmelCase	):
														pass







							def 					_lowerCamelCase		(    self	,			__lowerCAmelCase	,			__lowerCAmelCase=None	,			__lowerCAmelCase=None	,			__lowerCAmelCase=None	,			**__lowerCAmelCase	):
														UpperCamelCase__					  =      return_dict if return_dict is not None else self.config.use_return_dict
														UpperCamelCase__					  =      (
														    output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states
														)
														UpperCamelCase__					  =      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
																					UpperCamelCase__					  =      self._all_layers
																					UpperCamelCase__					  =      self._backbone(__lowerCAmelCase	,			**__lowerCAmelCase	)
																					UpperCamelCase__					  =      self._return_layers
																					UpperCamelCase__					  =      tuple(hidden_states[i] for i in self.out_indices	)
														else:
																					UpperCamelCase__					  =      self._backbone(__lowerCAmelCase	,			**__lowerCAmelCase	)
																					UpperCamelCase__					  =      None

														UpperCamelCase__					  =      tuple(__lowerCAmelCase	)
														UpperCamelCase__					  =      tuple(__lowerCAmelCase	) if hidden_states is not None else None

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

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




