



'''simple docstring'''






from typing import Optional

import torch
import torch.utils.checkpoint
from torch import Tensor, nn
from torch.nn import BCEWithLogitsLoss, CrossEntropyLoss, MSELoss

from ...activations import ACTaFN
from ...file_utils import add_code_sample_docstrings, add_start_docstrings, add_start_docstrings_to_model_forward
from ...modeling_outputs import (
    BaseModelOutputWithNoAttention,
    BaseModelOutputWithPoolingAndNoAttention,
    ImageClassifierOutputWithNoAttention,
)
from ...modeling_utils import PreTrainedModel
from ...utils import logging
from .configuration_regnet import RegNetConfig


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

# General docstring
UpperCamelCase__:   int								=			"RegNetConfig"

# Base docstring
UpperCamelCase__:   Optional[int]								=			"facebook/regnet-y-040"
UpperCamelCase__:   List[str]								=			[1, 1088, 7, 7]

# Image classification docstring
UpperCamelCase__:   Dict								=			"facebook/regnet-y-040"
UpperCamelCase__:   Union[str, Any]								=			"tabby, tabby cat"

UpperCamelCase__:   List[str]								=			[
    "facebook/regnet-y-040",
    # See all regnet models at https://huggingface.co/models?filter=regnet
]

class 				SCREAMING_SNAKE_CASE(							nn.Module			):







	"""simple docstring"""






	def __init__(						self			:     Tuple		,			__snake_case			:     int		,			__snake_case			:     int		,			__snake_case			:     int = 3		,			__snake_case			:     int = 1		,			__snake_case			:     int = 1		,			__snake_case			:     Optional[str] = "relu"		,			)			->		List[Any]:
								super().__init__()
								UpperCAmelCase :       Tuple 							=		nn.Convad(
								    __snake_case		,			__snake_case		,			kernel_size=__snake_case		,			stride=__snake_case		,			padding=kernel_size // 2		,			groups=__snake_case		,			bias=__snake_case		,			)
								UpperCAmelCase :       int 							=		nn.BatchNormad(__snake_case		)
								UpperCAmelCase :       List[str] 							=		ACTaFN[activation] if activation is not None else nn.Identity()







	def      A	(						self			:     int		,			__snake_case			:     Any		)			->		Tuple:
								UpperCAmelCase :       str 							=		self.convolution(__snake_case		)
								UpperCAmelCase :       Optional[int] 							=		self.normalization(__snake_case		)
								UpperCAmelCase :       List[Any] 							=		self.activation(__snake_case		)
								return hidden_state

class 				SCREAMING_SNAKE_CASE(							nn.Module			):







	"""simple docstring"""






	def __init__(						self			:     Dict		,			__snake_case			:     RegNetConfig		)			->		str:
								super().__init__()
								UpperCAmelCase :       Optional[Any] 							=		RegNetConvLayer(
								    config.num_channels		,			config.embedding_size		,			kernel_size=3		,			stride=2		,			activation=config.hidden_act		)
								UpperCAmelCase :       Optional[Any] 							=		config.num_channels







	def      A	(						self			:     Union[str, Any]		,			__snake_case			:     Tuple		)			->		Tuple:
								UpperCAmelCase :       List[Any] 							=		pixel_values.shape[1]
								if num_channels != self.num_channels:
															raise ValueError(
															    '''Make sure that the channel dimension of the pixel values match with the one set in the configuration.'''		)
								UpperCAmelCase :       List[str] 							=		self.embedder(__snake_case		)
								return hidden_state

class 				SCREAMING_SNAKE_CASE(							nn.Module			):







	"""simple docstring"""






	def __init__(						self			:     Any		,			__snake_case			:     int		,			__snake_case			:     int		,			__snake_case			:     int = 2		)			->		Optional[Any]:
								super().__init__()
								UpperCAmelCase :       str 							=		nn.Convad(__snake_case		,			__snake_case		,			kernel_size=1		,			stride=__snake_case		,			bias=__snake_case		)
								UpperCAmelCase :       List[Any] 							=		nn.BatchNormad(__snake_case		)







	def      A	(						self			:     Any		,			__snake_case			:     Tensor		)			->		Tensor:
								UpperCAmelCase :       int 							=		self.convolution(__snake_case		)
								UpperCAmelCase :       Union[str, Any] 							=		self.normalization(__snake_case		)
								return hidden_state

class 				SCREAMING_SNAKE_CASE(							nn.Module			):







	"""simple docstring"""






	def __init__(						self			:     List[Any]		,			__snake_case			:     int		,			__snake_case			:     int		)			->		Tuple:
								super().__init__()

								UpperCAmelCase :       Dict 							=		nn.AdaptiveAvgPoolad((1, 1)		)
								UpperCAmelCase :       Any 							=		nn.Sequential(
								    nn.Convad(__snake_case		,			__snake_case		,			kernel_size=1		)		,			nn.ReLU()		,			nn.Convad(__snake_case		,			__snake_case		,			kernel_size=1		)		,			nn.Sigmoid()		,			)







	def      A	(						self			:     str		,			__snake_case			:     Optional[int]		)			->		Optional[int]:
								# b c h w -> b c 1 1
								UpperCAmelCase :       str 							=		self.pooler(__snake_case		)
								UpperCAmelCase :       Optional[int] 							=		self.attention(__snake_case		)
								UpperCAmelCase :       int 							=		hidden_state * attention
								return hidden_state

class 				SCREAMING_SNAKE_CASE(							nn.Module			):







	"""simple docstring"""






	def __init__(						self			:     str		,			__snake_case			:     RegNetConfig		,			__snake_case			:     int		,			__snake_case			:     int		,			__snake_case			:     int = 1		)			->		Any:
								super().__init__()
								UpperCAmelCase :       int 							=		in_channels != out_channels or stride != 1
								UpperCAmelCase :       Union[str, Any] 							=		max(1		,			out_channels // config.groups_width		)
								UpperCAmelCase :       str 							=		(
								    RegNetShortCut(__snake_case		,			__snake_case		,			stride=__snake_case		) if should_apply_shortcut else nn.Identity()
								)
								UpperCAmelCase :       Union[str, Any] 							=		nn.Sequential(
								    RegNetConvLayer(__snake_case		,			__snake_case		,			kernel_size=1		,			activation=config.hidden_act		)		,			RegNetConvLayer(__snake_case		,			__snake_case		,			stride=__snake_case		,			groups=__snake_case		,			activation=config.hidden_act		)		,			RegNetConvLayer(__snake_case		,			__snake_case		,			kernel_size=1		,			activation=__snake_case		)		,			)
								UpperCAmelCase :       Optional[int] 							=		ACTaFN[config.hidden_act]







	def      A	(						self			:     List[Any]		,			__snake_case			:     Any		)			->		Union[str, Any]:
								UpperCAmelCase :       Dict 							=		hidden_state
								UpperCAmelCase :       int 							=		self.layer(__snake_case		)
								UpperCAmelCase :       Union[str, Any] 							=		self.shortcut(__snake_case		)
								hidden_state += residual
								UpperCAmelCase :       str 							=		self.activation(__snake_case		)
								return hidden_state

class 				SCREAMING_SNAKE_CASE(							nn.Module			):







	"""simple docstring"""






	def __init__(						self			:     Optional[Any]		,			__snake_case			:     RegNetConfig		,			__snake_case			:     int		,			__snake_case			:     int		,			__snake_case			:     int = 1		)			->		List[str]:
								super().__init__()
								UpperCAmelCase :       Any 							=		in_channels != out_channels or stride != 1
								UpperCAmelCase :       List[str] 							=		max(1		,			out_channels // config.groups_width		)
								UpperCAmelCase :       List[str] 							=		(
								    RegNetShortCut(__snake_case		,			__snake_case		,			stride=__snake_case		) if should_apply_shortcut else nn.Identity()
								)
								UpperCAmelCase :       int 							=		nn.Sequential(
								    RegNetConvLayer(__snake_case		,			__snake_case		,			kernel_size=1		,			activation=config.hidden_act		)		,			RegNetConvLayer(__snake_case		,			__snake_case		,			stride=__snake_case		,			groups=__snake_case		,			activation=config.hidden_act		)		,			RegNetSELayer(__snake_case		,			reduced_channels=int(round(in_channels / 4		)		)		)		,			RegNetConvLayer(__snake_case		,			__snake_case		,			kernel_size=1		,			activation=__snake_case		)		,			)
								UpperCAmelCase :       List[Any] 							=		ACTaFN[config.hidden_act]







	def      A	(						self			:     Dict		,			__snake_case			:     Any		)			->		int:
								UpperCAmelCase :       List[str] 							=		hidden_state
								UpperCAmelCase :       Optional[int] 							=		self.layer(__snake_case		)
								UpperCAmelCase :       Tuple 							=		self.shortcut(__snake_case		)
								hidden_state += residual
								UpperCAmelCase :       Any 							=		self.activation(__snake_case		)
								return hidden_state

class 				SCREAMING_SNAKE_CASE(							nn.Module			):







	"""simple docstring"""






	def __init__(						self			:     Union[str, Any]		,			__snake_case			:     RegNetConfig		,			__snake_case			:     int		,			__snake_case			:     int		,			__snake_case			:     int = 2		,			__snake_case			:     int = 2		,			)			->		int:
								super().__init__()

								UpperCAmelCase :       Any 							=		RegNetXLayer if config.layer_type == '''x''' else RegNetYLayer

								UpperCAmelCase :       str 							=		nn.Sequential(
								    # downsampling is done in the first layer with stride of 2
								    layer(
								        __snake_case		,			__snake_case		,			__snake_case		,			stride=__snake_case		,			)		,			*[layer(__snake_case		,			__snake_case		,			__snake_case		) for _ in range(depth - 1		)]		,			)







	def      A	(						self			:     List[str]		,			__snake_case			:     str		)			->		Any:
								UpperCAmelCase :       int 							=		self.layers(__snake_case		)
								return hidden_state

class 				SCREAMING_SNAKE_CASE(							nn.Module			):







	"""simple docstring"""






	def __init__(						self			:     List[Any]		,			__snake_case			:     RegNetConfig		)			->		Dict:
								super().__init__()
								UpperCAmelCase :       Optional[Any] 							=		nn.ModuleList([]		)
								# based on `downsample_in_first_stage`, the first layer of the first stage may or may not downsample the input
								self.stages.append(
								    RegNetStage(
								        __snake_case		,			config.embedding_size		,			config.hidden_sizes[0]		,			stride=2 if config.downsample_in_first_stage else 1		,			depth=config.depths[0]		,			)		)
								UpperCAmelCase :       str 							=		zip(config.hidden_sizes		,			config.hidden_sizes[1:]		)
								for (in_channels, out_channels), depth in zip(__snake_case		,			config.depths[1:]		):
															self.stages.append(RegNetStage(__snake_case		,			__snake_case		,			__snake_case		,			depth=__snake_case		)		)







	def      A	(						self			:     Dict		,			__snake_case			:     Tensor		,			__snake_case			:     bool = False		,			__snake_case			:     bool = True		)			->		BaseModelOutputWithNoAttention:
								UpperCAmelCase :       Any 							=		() if output_hidden_states else None

								for stage_module in self.stages:
															if output_hidden_states:
																						UpperCAmelCase :       str 							=		hidden_states + (hidden_state,)

															UpperCAmelCase :       Any 							=		stage_module(__snake_case		)

								if output_hidden_states:
															UpperCAmelCase :       str 							=		hidden_states + (hidden_state,)

								if not return_dict:
															return tuple(v for v in [hidden_state, hidden_states] if v is not None		)

								return BaseModelOutputWithNoAttention(last_hidden_state=__snake_case		,			hidden_states=__snake_case		)

class 				SCREAMING_SNAKE_CASE(							A__			):







	"""simple docstring"""






	lowerCamelCase__			 =       RegNetConfig
	lowerCamelCase__			 =       """regnet"""
	lowerCamelCase__			 =       """pixel_values"""
	lowerCamelCase__			 =       True







	def      A	(						self			:     List[Any]		,			__snake_case			:     Union[str, Any]		)			->		Dict:
								if isinstance(__snake_case		,			nn.Convad		):
															nn.init.kaiming_normal_(module.weight		,			mode='''fan_out'''		,			nonlinearity='''relu'''		)
								elif isinstance(__snake_case		,			(nn.BatchNormad, nn.GroupNorm)		):
															nn.init.constant_(module.weight		,			1		)
															nn.init.constant_(module.bias		,			0		)







	def      A	(						self			:     int		,			__snake_case			:     Dict		,			__snake_case			:     List[str]=False		)			->		str:
								if isinstance(__snake_case		,			__snake_case		):
															UpperCAmelCase :       Dict 							=		value

UpperCamelCase__:   List[str]								=			r"\n    This model is a PyTorch [torch.nn.Module](https://pytorch.org/docs/stable/nn.html#torch.nn.Module) subclass. Use it\n    as a regular PyTorch Module and refer to the PyTorch documentation for all matter related to general usage and\n    behavior.\n\n    Parameters:\n        config ([`RegNetConfig`]): Model configuration class with all the parameters of the model.\n            Initializing with a config file does not load the weights associated with the model, only the\n            configuration. Check out the [`~PreTrainedModel.from_pretrained`] method to load the model weights.\n"

UpperCamelCase__:   Any								=			r"\n    Args:\n        pixel_values (`torch.FloatTensor` of shape `(batch_size, num_channels, height, width)`):\n            Pixel values. Pixel values can be obtained using [`AutoImageProcessor`]. See\n            [`ConvNextImageProcessor.__call__`] for details.\n\n        output_hidden_states (`bool`, *optional*):\n            Whether or not to return the hidden states of all layers. See `hidden_states` under returned tensors for\n            more detail.\n        return_dict (`bool`, *optional*):\n            Whether or not to return a [`~file_utils.ModelOutput`] instead of a plain tuple.\n"

@add_start_docstrings(
    """The bare RegNet model outputting raw features without any specific head on top."""							,		A__							,		)
# Copied from transformers.models.resnet.modeling_resnet.ResNetModel with RESNET->REGNET,ResNet->RegNet
class 				SCREAMING_SNAKE_CASE(							A__			):







	"""simple docstring"""






	def __init__(						self			:     Optional[int]		,			__snake_case			:     Dict		)			->		int:
								super().__init__(__snake_case		)
								UpperCAmelCase :       Dict 							=		config
								UpperCAmelCase :       List[Any] 							=		RegNetEmbeddings(__snake_case		)
								UpperCAmelCase :       int 							=		RegNetEncoder(__snake_case		)
								UpperCAmelCase :       Optional[Any] 							=		nn.AdaptiveAvgPoolad((1, 1)		)
								# Initialize weights and apply final processing
								self.post_init()







	@add_start_docstrings_to_model_forward(__snake_case		)
	@add_code_sample_docstrings(
	    checkpoint=_CHECKPOINT_FOR_DOC		,			output_type=__snake_case		,			config_class=_CONFIG_FOR_DOC		,			modality='''vision'''		,			expected_output=_EXPECTED_OUTPUT_SHAPE		,			)
	def      A	(						self			:     Optional[Any]		,			__snake_case			:     Tensor		,			__snake_case			:     Optional[bool] = None		,			__snake_case			:     Optional[bool] = None		)			->		BaseModelOutputWithPoolingAndNoAttention:
								UpperCAmelCase :       str 							=		(
								    output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states
								)
								UpperCAmelCase :       Optional[int] 							=		return_dict if return_dict is not None else self.config.use_return_dict

								UpperCAmelCase :       Optional[Any] 							=		self.embedder(__snake_case		)

								UpperCAmelCase :       Dict 							=		self.encoder(
								    __snake_case		,			output_hidden_states=__snake_case		,			return_dict=__snake_case		)

								UpperCAmelCase :       List[Any] 							=		encoder_outputs[0]

								UpperCAmelCase :       Optional[int] 							=		self.pooler(__snake_case		)

								if not return_dict:
															return (last_hidden_state, pooled_output) + encoder_outputs[1:]

								return BaseModelOutputWithPoolingAndNoAttention(
								    last_hidden_state=__snake_case		,			pooler_output=__snake_case		,			hidden_states=encoder_outputs.hidden_states		,			)

@add_start_docstrings(
    """
    RegNet Model with an image classification head on top (a linear layer on top of the pooled features), e.g. for
    ImageNet.
    """							,		A__							,		)
# Copied from transformers.models.resnet.modeling_resnet.ResNetForImageClassification with RESNET->REGNET,ResNet->RegNet,resnet->regnet
class 				SCREAMING_SNAKE_CASE(							A__			):







	"""simple docstring"""






	def __init__(						self			:     List[str]		,			__snake_case			:     List[str]		)			->		Optional[int]:
								super().__init__(__snake_case		)
								UpperCAmelCase :       Any 							=		config.num_labels
								UpperCAmelCase :       List[str] 							=		RegNetModel(__snake_case		)
								# classification head
								UpperCAmelCase :       str 							=		nn.Sequential(
								    nn.Flatten()		,			nn.Linear(config.hidden_sizes[-1]		,			config.num_labels		) if config.num_labels > 0 else nn.Identity()		,			)
								# initialize weights and apply final processing
								self.post_init()







	@add_start_docstrings_to_model_forward(__snake_case		)
	@add_code_sample_docstrings(
	    checkpoint=_IMAGE_CLASS_CHECKPOINT		,			output_type=__snake_case		,			config_class=_CONFIG_FOR_DOC		,			expected_output=_IMAGE_CLASS_EXPECTED_OUTPUT		,			)
	def      A	(						self			:     Union[str, Any]		,			__snake_case			:     Optional[torch.FloatTensor] = None		,			__snake_case			:     Optional[torch.LongTensor] = None		,			__snake_case			:     Optional[bool] = None		,			__snake_case			:     Optional[bool] = None		,			)			->		ImageClassifierOutputWithNoAttention:
								UpperCAmelCase :       Dict 							=		return_dict if return_dict is not None else self.config.use_return_dict

								UpperCAmelCase :       str 							=		self.regnet(__snake_case		,			output_hidden_states=__snake_case		,			return_dict=__snake_case		)

								UpperCAmelCase :       Dict 							=		outputs.pooler_output if return_dict else outputs[1]

								UpperCAmelCase :       Union[str, Any] 							=		self.classifier(__snake_case		)

								UpperCAmelCase :       Optional[int] 							=		None

								if labels is not None:
															if self.config.problem_type is None:
																						if self.num_labels == 1:
																													UpperCAmelCase :       Optional[Any] 							=		'''regression'''
																						elif self.num_labels > 1 and (labels.dtype == torch.long or labels.dtype == torch.int):
																													UpperCAmelCase :       Any 							=		'''single_label_classification'''
																						else:
																													UpperCAmelCase :       Any 							=		'''multi_label_classification'''
															if self.config.problem_type == "regression":
																						UpperCAmelCase :       Optional[int] 							=		MSELoss()
																						if self.num_labels == 1:
																													UpperCAmelCase :       List[str] 							=		loss_fct(logits.squeeze()		,			labels.squeeze()		)
																						else:
																													UpperCAmelCase :       str 							=		loss_fct(__snake_case		,			__snake_case		)
															elif self.config.problem_type == "single_label_classification":
																						UpperCAmelCase :       Union[str, Any] 							=		CrossEntropyLoss()
																						UpperCAmelCase :       List[Any] 							=		loss_fct(logits.view(-1		,			self.num_labels		)		,			labels.view(-1		)		)
															elif self.config.problem_type == "multi_label_classification":
																						UpperCAmelCase :       Optional[int] 							=		BCEWithLogitsLoss()
																						UpperCAmelCase :       Optional[Any] 							=		loss_fct(__snake_case		,			__snake_case		)

								if not return_dict:
															UpperCAmelCase :       Optional[int] 							=		(logits,) + outputs[2:]
															return (loss,) + output if loss is not None else output

								return ImageClassifierOutputWithNoAttention(loss=__snake_case		,			logits=__snake_case		,			hidden_states=outputs.hidden_states		)










'''simple docstring'''






import warnings
from contextlib import contextmanager

from ....processing_utils import ProcessorMixin

class 				SCREAMING_SNAKE_CASE(							A__			):







	"""simple docstring"""






	lowerCamelCase__			 =       """MCTCTFeatureExtractor"""
	lowerCamelCase__			 =       """AutoTokenizer"""







	def __init__(						self			:     Dict		,			__snake_case			:     Optional[int]		,			__snake_case			:     List[str]		)			->		str:
								super().__init__(__snake_case		,			__snake_case		)
								UpperCAmelCase :       List[Any] 							=		self.feature_extractor
								UpperCAmelCase :       Union[str, Any] 							=		False







	def __call__(						self			:     Any		,			*__snake_case			:     List[str]		,			**__snake_case			:     Any		)			->		List[Any]:
								# For backward compatibility
								if self._in_target_context_manager:
															return self.current_processor(*__snake_case		,			**__snake_case		)

								if "raw_speech" in kwargs:
															warnings.warn('''Using `raw_speech` as a keyword argument is deprecated. Use `audio` instead.'''		)
															UpperCAmelCase :       int 							=		kwargs.pop('''raw_speech'''		)
								else:
															UpperCAmelCase :       Union[str, Any] 							=		kwargs.pop('''audio'''		,			__snake_case		)
								UpperCAmelCase :       Optional[Any] 							=		kwargs.pop('''sampling_rate'''		,			__snake_case		)
								UpperCAmelCase :       Dict 							=		kwargs.pop('''text'''		,			__snake_case		)
								if len(__snake_case		) > 0:
															UpperCAmelCase :       Any 							=		args[0]
															UpperCAmelCase :       Optional[int] 							=		args[1:]

								if audio is None and text is None:
															raise ValueError('''You need to specify either an `audio` or `text` input to process.'''		)

								if audio is not None:
															UpperCAmelCase :       List[str] 							=		self.feature_extractor(__snake_case		,			*__snake_case		,			sampling_rate=__snake_case		,			**__snake_case		)
								if text is not None:
															UpperCAmelCase :       int 							=		self.tokenizer(__snake_case		,			**__snake_case		)

								if text is None:
															return inputs
								elif audio is None:
															return encodings
								else:
															UpperCAmelCase :       str 							=		encodings['''input_ids''']
															return inputs







	def      A	(						self			:     List[Any]		,			*__snake_case			:     List[Any]		,			**__snake_case			:     List[Any]		)			->		str:
								return self.tokenizer.batch_decode(*__snake_case		,			**__snake_case		)







	def      A	(						self			:     List[Any]		,			*__snake_case			:     int		,			**__snake_case			:     Optional[int]		)			->		Any:
								# For backward compatibility
								if self._in_target_context_manager:
															return self.current_processor.pad(*__snake_case		,			**__snake_case		)

								UpperCAmelCase :       List[Any] 							=		kwargs.pop('''input_features'''		,			__snake_case		)
								UpperCAmelCase :       Optional[Any] 							=		kwargs.pop('''labels'''		,			__snake_case		)
								if len(__snake_case		) > 0:
															UpperCAmelCase :       List[str] 							=		args[0]
															UpperCAmelCase :       List[Any] 							=		args[1:]

								if input_features is not None:
															UpperCAmelCase :       Tuple 							=		self.feature_extractor.pad(__snake_case		,			*__snake_case		,			**__snake_case		)
								if labels is not None:
															UpperCAmelCase :       Optional[int] 							=		self.tokenizer.pad(__snake_case		,			**__snake_case		)

								if labels is None:
															return input_features
								elif input_features is None:
															return labels
								else:
															UpperCAmelCase :       List[str] 							=		labels['''input_ids''']
															return input_features







	def      A	(						self			:     Union[str, Any]		,			*__snake_case			:     Optional[Any]		,			**__snake_case			:     Optional[int]		)			->		Optional[Any]:
								return self.tokenizer.decode(*__snake_case		,			**__snake_case		)







	@contextmanager
	def      A	(						self			:     Any		)			->		Optional[int]:
								warnings.warn(
								    '''`as_target_processor` is deprecated and will be removed in v5 of Transformers. You can process your '''
								    '''labels by using the argument `text` of the regular `__call__` method (either in the same call as '''
								    '''your audio inputs, or in a separate call.'''		)
								UpperCAmelCase :       Dict 							=		True
								UpperCAmelCase :       List[Any] 							=		self.tokenizer
								yield
								UpperCAmelCase :       Tuple 							=		self.feature_extractor
								UpperCAmelCase :       List[Any] 							=		False





