'''simple docstring'''




import os
import tempfile
import unittest

from transformers import NezhaConfig, is_torch_available
from transformers.models.auto import get_values
from transformers.testing_utils import require_torch, require_torch_gpu, slow, torch_device

from ...generation.test_utils import GenerationTesterMixin
from ...test_configuration_common import ConfigTester
from ...test_modeling_common import ModelTesterMixin, floats_tensor, ids_tensor, random_attention_mask
from ...test_pipeline_mixin import PipelineTesterMixin


if is_torch_available():
						import torch

						from transformers import (
						    MODEL_FOR_PRETRAINING_MAPPING,
						    NezhaForMaskedLM,
						    NezhaForMultipleChoice,
						    NezhaForNextSentencePrediction,
						    NezhaForPreTraining,
						    NezhaForQuestionAnswering,
						    NezhaForSequenceClassification,
						    NezhaForTokenClassification,
						    NezhaModel,
						)
						from transformers.models.nezha.modeling_nezha import NEZHA_PRETRAINED_MODEL_ARCHIVE_LIST





class 					__magic_name__		:







			def __init__(			self       :						Tuple       ,    lowercase_       :						Any       ,    lowercase_       :						Optional[Any]=13       ,    lowercase_       :						List[str]=7       ,    lowercase_       :						str=True       ,    lowercase_       :						Union[str, Any]=True       ,    lowercase_       :						Optional[Any]=True       ,    lowercase_       :						int=True       ,    lowercase_       :						str=99       ,    lowercase_       :						List[Any]=32       ,    lowercase_       :						Optional[Any]=5       ,    lowercase_       :						int=4       ,    lowercase_       :						Optional[int]=37       ,    lowercase_       :						Dict="gelu"       ,    lowercase_       :						List[Any]=0.1       ,    lowercase_       :						Dict=0.1       ,    lowercase_       :						List[str]=128       ,    lowercase_       :						str=32       ,    lowercase_       :						Optional[int]=16       ,    lowercase_       :						Optional[Any]=2       ,    lowercase_       :						int=0.02       ,    lowercase_       :						Tuple=3       ,    lowercase_       :						Any=4       ,    lowercase_       :						Dict=None       ,    ):
										lowercase_      :    str       		= parent
										lowercase_      :    Dict       		= batch_size
										lowercase_      :    List[Any]       		= seq_length
										lowercase_      :    List[Any]       		= is_training
										lowercase_      :    Union[str, Any]       		= use_input_mask
										lowercase_      :    Optional[int]       		= use_token_type_ids
										lowercase_      :    int       		= use_labels
										lowercase_      :    Any       		= vocab_size
										lowercase_      :    Dict       		= hidden_size
										lowercase_      :    Union[str, Any]       		= num_hidden_layers
										lowercase_      :    List[str]       		= num_attention_heads
										lowercase_      :    List[Any]       		= intermediate_size
										lowercase_      :    Union[str, Any]       		= hidden_act
										lowercase_      :    List[str]       		= hidden_dropout_prob
										lowercase_      :    Union[str, Any]       		= attention_probs_dropout_prob
										lowercase_      :    Tuple       		= max_position_embeddings
										lowercase_      :    Union[str, Any]       		= type_vocab_size
										lowercase_      :    Optional[int]       		= type_sequence_label_size
										lowercase_      :    Optional[Any]       		= initializer_range
										lowercase_      :    List[str]       		= num_labels
										lowercase_      :    List[Any]       		= num_choices
										lowercase_      :    Optional[int]       		= scope







			def 			SCREAMING_SNAKE_CASE_  (			self       :						Any					):
										lowercase_      :    str       		= ids_tensor([self.batch_size, self.seq_length]       ,    self.vocab_size					)

										lowercase_      :    List[str]       		= None
										if self.use_input_mask:
																	lowercase_      :    Optional[Any]       		= random_attention_mask([self.batch_size, self.seq_length]					)

										lowercase_      :    List[str]       		= None
										if self.use_token_type_ids:
																	lowercase_      :    int       		= ids_tensor([self.batch_size, self.seq_length]       ,    self.type_vocab_size					)

										lowercase_      :    Optional[int]       		= None
										lowercase_      :    int       		= None
										lowercase_      :    Dict       		= None
										if self.use_labels:
																	lowercase_      :    List[str]       		= ids_tensor([self.batch_size]       ,    self.type_sequence_label_size					)
																	lowercase_      :    List[str]       		= ids_tensor([self.batch_size, self.seq_length]       ,    self.num_labels					)
																	lowercase_      :    Tuple       		= ids_tensor([self.batch_size]       ,    self.num_choices					)

										lowercase_      :    List[str]       		= self.get_config()

										return config, input_ids, token_type_ids, input_mask, sequence_labels, token_labels, choice_labels







			def 			SCREAMING_SNAKE_CASE_  (			self       :						Dict					):
										return NezhaConfig(
										    vocab_size=self.vocab_size       ,    hidden_size=self.hidden_size       ,    num_hidden_layers=self.num_hidden_layers       ,    num_attention_heads=self.num_attention_heads       ,    intermediate_size=self.intermediate_size       ,    hidden_act=self.hidden_act       ,    hidden_dropout_prob=self.hidden_dropout_prob       ,    attention_probs_dropout_prob=self.attention_probs_dropout_prob       ,    max_position_embeddings=self.max_position_embeddings       ,    type_vocab_size=self.type_vocab_size       ,    is_decoder=a_       ,    initializer_range=self.initializer_range       ,    )







			def 			SCREAMING_SNAKE_CASE_  (			self       :						int					):
										(
										    lowercase_      
										)      :    Union[str, Any]       		= self.prepare_config_and_inputs()

										lowercase_      :    int       		= True
										lowercase_      :    Optional[Any]       		= floats_tensor([self.batch_size, self.seq_length, self.hidden_size]					)
										lowercase_      :    int       		= ids_tensor([self.batch_size, self.seq_length]       ,    vocab_size=2					)

										return (
										    config,
										    input_ids,
										    token_type_ids,
										    input_mask,
										    sequence_labels,
										    token_labels,
										    choice_labels,
										    encoder_hidden_states,
										    encoder_attention_mask,
										)







			def 			SCREAMING_SNAKE_CASE_  (			self       :						Tuple       ,    lowercase_       :						str       ,    lowercase_       :						Optional[int]       ,    lowercase_       :						int       ,    lowercase_       :						Dict       ,    lowercase_       :						int       ,    lowercase_       :						Any       ,    lowercase_       :						str					):
										lowercase_      :    Dict       		= NezhaModel(config=a_					)
										model.to(a_					)
										model.eval()
										lowercase_      :    Any       		= model(a_       ,    attention_mask=a_       ,    token_type_ids=a_					)
										lowercase_      :    Union[str, Any]       		= model(a_       ,    token_type_ids=a_					)
										lowercase_      :    List[str]       		= model(a_					)
										self.parent.assertEqual(result.last_hidden_state.shape       ,    (self.batch_size, self.seq_length, self.hidden_size)					)
										self.parent.assertEqual(result.pooler_output.shape       ,    (self.batch_size, self.hidden_size)					)







			def 			SCREAMING_SNAKE_CASE_  (			self       :						List[str]       ,    lowercase_       :						Optional[Any]       ,    lowercase_       :						List[str]       ,    lowercase_       :						str       ,    lowercase_       :						Any       ,    lowercase_       :						Any       ,    lowercase_       :						Tuple       ,    lowercase_       :						Any       ,    lowercase_       :						Tuple       ,    lowercase_       :						Any       ,    ):
										lowercase_      :    str       		= True
										lowercase_      :    Dict       		= NezhaModel(a_					)
										model.to(a_					)
										model.eval()
										lowercase_      :    Optional[Any]       		= model(
										    a_       ,    attention_mask=a_       ,    token_type_ids=a_       ,    encoder_hidden_states=a_       ,    encoder_attention_mask=a_       ,    )
										lowercase_      :    Dict       		= model(
										    a_       ,    attention_mask=a_       ,    token_type_ids=a_       ,    encoder_hidden_states=a_       ,    )
										lowercase_      :    Tuple       		= model(a_       ,    attention_mask=a_       ,    token_type_ids=a_					)
										self.parent.assertEqual(result.last_hidden_state.shape       ,    (self.batch_size, self.seq_length, self.hidden_size)					)
										self.parent.assertEqual(result.pooler_output.shape       ,    (self.batch_size, self.hidden_size)					)







			def 			SCREAMING_SNAKE_CASE_  (			self       :						Optional[Any]       ,    lowercase_       :						Union[str, Any]       ,    lowercase_       :						int       ,    lowercase_       :						Optional[Any]       ,    lowercase_       :						str       ,    lowercase_       :						Dict       ,    lowercase_       :						Optional[int]       ,    lowercase_       :						Optional[Any]					):
										lowercase_      :    Optional[int]       		= NezhaForMaskedLM(config=a_					)
										model.to(a_					)
										model.eval()
										lowercase_      :    Dict       		= model(a_       ,    attention_mask=a_       ,    token_type_ids=a_       ,    labels=a_					)
										self.parent.assertEqual(result.logits.shape       ,    (self.batch_size, self.seq_length, self.vocab_size)					)







			def 			SCREAMING_SNAKE_CASE_  (			self       :						Tuple       ,    lowercase_       :						int       ,    lowercase_       :						Optional[Any]       ,    lowercase_       :						Any       ,    lowercase_       :						int       ,    lowercase_       :						Union[str, Any]       ,    lowercase_       :						Optional[int]       ,    lowercase_       :						List[Any]					):
										lowercase_      :    Union[str, Any]       		= NezhaForNextSentencePrediction(config=a_					)
										model.to(a_					)
										model.eval()
										lowercase_      :    Optional[int]       		= model(
										    a_       ,    attention_mask=a_       ,    token_type_ids=a_       ,    labels=a_       ,    )
										self.parent.assertEqual(result.logits.shape       ,    (self.batch_size, 2)					)







			def 			SCREAMING_SNAKE_CASE_  (			self       :						Any       ,    lowercase_       :						List[str]       ,    lowercase_       :						Union[str, Any]       ,    lowercase_       :						List[str]       ,    lowercase_       :						Optional[Any]       ,    lowercase_       :						int       ,    lowercase_       :						int       ,    lowercase_       :						Dict					):
										lowercase_      :    Tuple       		= NezhaForPreTraining(config=a_					)
										model.to(a_					)
										model.eval()
										lowercase_      :    int       		= model(
										    a_       ,    attention_mask=a_       ,    token_type_ids=a_       ,    labels=a_       ,    next_sentence_label=a_       ,    )
										self.parent.assertEqual(result.prediction_logits.shape       ,    (self.batch_size, self.seq_length, self.vocab_size)					)
										self.parent.assertEqual(result.seq_relationship_logits.shape       ,    (self.batch_size, 2)					)







			def 			SCREAMING_SNAKE_CASE_  (			self       :						Union[str, Any]       ,    lowercase_       :						Optional[Any]       ,    lowercase_       :						int       ,    lowercase_       :						List[Any]       ,    lowercase_       :						List[str]       ,    lowercase_       :						str       ,    lowercase_       :						Any       ,    lowercase_       :						Dict					):
										lowercase_      :    List[Any]       		= NezhaForQuestionAnswering(config=a_					)
										model.to(a_					)
										model.eval()
										lowercase_      :    int       		= model(
										    a_       ,    attention_mask=a_       ,    token_type_ids=a_       ,    start_positions=a_       ,    end_positions=a_       ,    )
										self.parent.assertEqual(result.start_logits.shape       ,    (self.batch_size, self.seq_length)					)
										self.parent.assertEqual(result.end_logits.shape       ,    (self.batch_size, self.seq_length)					)







			def 			SCREAMING_SNAKE_CASE_  (			self       :						int       ,    lowercase_       :						Union[str, Any]       ,    lowercase_       :						str       ,    lowercase_       :						Optional[Any]       ,    lowercase_       :						str       ,    lowercase_       :						List[str]       ,    lowercase_       :						List[str]       ,    lowercase_       :						Dict					):
										lowercase_      :    Any       		= self.num_labels
										lowercase_      :    Union[str, Any]       		= NezhaForSequenceClassification(a_					)
										model.to(a_					)
										model.eval()
										lowercase_      :    int       		= model(a_       ,    attention_mask=a_       ,    token_type_ids=a_       ,    labels=a_					)
										self.parent.assertEqual(result.logits.shape       ,    (self.batch_size, self.num_labels)					)







			def 			SCREAMING_SNAKE_CASE_  (			self       :						Dict       ,    lowercase_       :						Any       ,    lowercase_       :						Dict       ,    lowercase_       :						Tuple       ,    lowercase_       :						List[Any]       ,    lowercase_       :						int       ,    lowercase_       :						str       ,    lowercase_       :						Dict					):
										lowercase_      :    int       		= self.num_labels
										lowercase_      :    List[Any]       		= NezhaForTokenClassification(config=a_					)
										model.to(a_					)
										model.eval()
										lowercase_      :    List[Any]       		= model(a_       ,    attention_mask=a_       ,    token_type_ids=a_       ,    labels=a_					)
										self.parent.assertEqual(result.logits.shape       ,    (self.batch_size, self.seq_length, self.num_labels)					)







			def 			SCREAMING_SNAKE_CASE_  (			self       :						Any       ,    lowercase_       :						Dict       ,    lowercase_       :						Union[str, Any]       ,    lowercase_       :						str       ,    lowercase_       :						int       ,    lowercase_       :						int       ,    lowercase_       :						Tuple       ,    lowercase_       :						Dict					):
										lowercase_      :    Union[str, Any]       		= self.num_choices
										lowercase_      :    Optional[Any]       		= NezhaForMultipleChoice(config=a_					)
										model.to(a_					)
										model.eval()
										lowercase_      :    int       		= input_ids.unsqueeze(1					).expand(-1       ,    self.num_choices       ,    -1					).contiguous()
										lowercase_      :    List[Any]       		= token_type_ids.unsqueeze(1					).expand(-1       ,    self.num_choices       ,    -1					).contiguous()
										lowercase_      :    List[str]       		= input_mask.unsqueeze(1					).expand(-1       ,    self.num_choices       ,    -1					).contiguous()
										lowercase_      :    int       		= model(
										    a_       ,    attention_mask=a_       ,    token_type_ids=a_       ,    labels=a_       ,    )
										self.parent.assertEqual(result.logits.shape       ,    (self.batch_size, self.num_choices)					)







			def 			SCREAMING_SNAKE_CASE_  (			self       :						Dict					):
										lowercase_      :    Any       		= self.prepare_config_and_inputs()
										(
										    lowercase_      
										)      :    Tuple       		= config_and_inputs
										lowercase_      :    List[Any]       		= {"""input_ids""": input_ids, """token_type_ids""": token_type_ids, """attention_mask""": input_mask}
										return config, inputs_dict





@require_torch
class 					__magic_name__		(					__a,  __a,  __a,  unittest.TestCase):
			UpperCamelCase__       = (
			    (
			        NezhaModel,
			        NezhaForMaskedLM,
			        NezhaForMultipleChoice,
			        NezhaForNextSentencePrediction,
			        NezhaForPreTraining,
			        NezhaForQuestionAnswering,
			        NezhaForSequenceClassification,
			        NezhaForTokenClassification,
			    )
			    if is_torch_available()
			    else ()
			)
			UpperCamelCase__       = (
			    {
			        '''feature-extraction''': NezhaModel,
			        '''fill-mask''': NezhaForMaskedLM,
			        '''question-answering''': NezhaForQuestionAnswering,
			        '''text-classification''': NezhaForSequenceClassification,
			        '''token-classification''': NezhaForTokenClassification,
			        '''zero-shot''': NezhaForSequenceClassification,
			    }
			    if is_torch_available()
			    else {}
			)
			UpperCamelCase__       = True







			def 			SCREAMING_SNAKE_CASE_  (			self       :						Union[str, Any]       ,    lowercase_       :						int       ,    lowercase_       :						List[Any]       ,    lowercase_       :						List[str]=False					):
										lowercase_      :    Any       		= super()._prepare_for_class(a_       ,    a_       ,    return_labels=a_					)

										if return_labels:
																	if model_class in get_values(a_					):
																								lowercase_      :    Union[str, Any]       		= torch.zeros(
																								    (self.model_tester.batch_size, self.model_tester.seq_length)       ,    dtype=torch.long       ,    device=a_					)
																								lowercase_      :    str       		= torch.zeros(
																								    self.model_tester.batch_size       ,    dtype=torch.long       ,    device=a_					)
										return inputs_dict







			def 			SCREAMING_SNAKE_CASE_  (			self       :						Tuple					):
										lowercase_      :    int       		= NezhaModelTester(self					)
										lowercase_      :    List[Any]       		= ConfigTester(self       ,    config_class=a_       ,    hidden_size=37					)







			def 			SCREAMING_SNAKE_CASE_  (			self       :						Union[str, Any]					):
										self.config_tester.run_common_tests()







			def 			SCREAMING_SNAKE_CASE_  (			self       :						Any					):
										lowercase_      :    Dict       		= self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_model(*a_					)







			def 			SCREAMING_SNAKE_CASE_  (			self       :						Union[str, Any]					):
										lowercase_      :    Optional[int]       		= self.model_tester.prepare_config_and_inputs_for_decoder()
										self.model_tester.create_and_check_model_as_decoder(*a_					)







			def 			SCREAMING_SNAKE_CASE_  (			self       :						List[str]					):
										(
										    lowercase_      
										)      :    List[str]       		= self.model_tester.prepare_config_and_inputs_for_decoder()

										lowercase_      :    List[Any]       		= None

										self.model_tester.create_and_check_model_as_decoder(
										    a_       ,    a_       ,    a_       ,    a_       ,    a_       ,    a_       ,    a_       ,    a_       ,    a_       ,    )







			def 			SCREAMING_SNAKE_CASE_  (			self       :						int					):
										lowercase_      :    Optional[int]       		= self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_for_masked_lm(*a_					)







			def 			SCREAMING_SNAKE_CASE_  (			self       :						List[Any]					):
										lowercase_      :    Any       		= self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_for_multiple_choice(*a_					)







			def 			SCREAMING_SNAKE_CASE_  (			self       :						Tuple					):
										lowercase_      :    Tuple       		= self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_for_next_sequence_prediction(*a_					)







			def 			SCREAMING_SNAKE_CASE_  (			self       :						Any					):
										lowercase_      :    Optional[int]       		= self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_for_pretraining(*a_					)







			def 			SCREAMING_SNAKE_CASE_  (			self       :						int					):
										lowercase_      :    List[Any]       		= self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_for_question_answering(*a_					)







			def 			SCREAMING_SNAKE_CASE_  (			self       :						Any					):
										lowercase_      :    Dict       		= self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_for_sequence_classification(*a_					)







			def 			SCREAMING_SNAKE_CASE_  (			self       :						Optional[int]					):
										lowercase_      :    Union[str, Any]       		= self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_for_token_classification(*a_					)







			@slow
			def 			SCREAMING_SNAKE_CASE_  (			self       :						Any					):



										for model_name in NEZHA_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
																	lowercase_      :    List[Any]       		= NezhaModel.from_pretrained(a_					)
																	self.assertIsNotNone(a_					)







			@slow
			@require_torch_gpu
			def 			SCREAMING_SNAKE_CASE_  (			self       :						Optional[int]					):
										lowercase_      :    List[Any]       		= self.model_tester.prepare_config_and_inputs_for_common()
										for model_class in self.all_model_classes:
																	# NezhaForMultipleChoice behaves incorrectly in JIT environments.
																	if model_class == NezhaForMultipleChoice:
																								return

																	lowercase_      :    str       		= True
																	lowercase_      :    Tuple       		= model_class(config=a_					)

																	lowercase_      :    Optional[int]       		= self._prepare_for_class(a_       ,    a_					)
																	lowercase_      :    Optional[Any]       		= torch.jit.trace(
																	    a_       ,    (inputs_dict["""input_ids"""].to("""cpu"""					), inputs_dict["""attention_mask"""].to("""cpu"""					))					)

																	with tempfile.TemporaryDirectory() as tmp:
																								torch.jit.save(a_       ,    os.path.join(a_       ,    """bert.pt"""					)					)
																								lowercase_      :    List[str]       		= torch.jit.load(os.path.join(a_       ,    """bert.pt"""					)       ,    map_location=a_					)
																								loaded(inputs_dict["""input_ids"""].to(a_					)       ,    inputs_dict["""attention_mask"""].to(a_					)					)





@require_torch
class 					__magic_name__		(					unittest.TestCase):







			@slow
			def 			SCREAMING_SNAKE_CASE_  (			self       :						str					):
										lowercase_      :    int       		= NezhaModel.from_pretrained("""sijunhe/nezha-cn-base"""					)
										lowercase_      :    int       		= torch.tensor([[0, 1, 2, 3, 4, 5]]					)
										lowercase_      :    Tuple       		= torch.tensor([[0, 1, 1, 1, 1, 1]]					)
										with torch.no_grad():
																	lowercase_      :    List[Any]       		= model(a_       ,    attention_mask=a_					)[0]
										lowercase_      :    str       		= torch.Size((1, 6, 768)					)
										self.assertEqual(output.shape       ,    a_					)
										lowercase_      :    List[Any]       		= torch.tensor([[[0.06_85, 0.24_41, 0.11_02], [0.06_00, 0.19_06, 0.13_49], [0.02_21, 0.08_19, 0.05_86]]]					)

										self.assertTrue(torch.allclose(output[:, 1:4, 1:4]       ,    a_       ,    atol=1E-4					)					)







			@slow
			def 			SCREAMING_SNAKE_CASE_  (			self       :						Tuple					):
										lowercase_      :    Tuple       		= NezhaForMaskedLM.from_pretrained("""sijunhe/nezha-cn-base"""					)
										lowercase_      :    Any       		= torch.tensor([[0, 1, 2, 3, 4, 5]]					)
										lowercase_      :    int       		= torch.tensor([[1, 1, 1, 1, 1, 1]]					)
										with torch.no_grad():
																	lowercase_      :    Any       		= model(a_       ,    attention_mask=a_					)[0]
										lowercase_      :    Optional[int]       		= torch.Size((1, 6, 21128)					)
										self.assertEqual(output.shape       ,    a_					)
										lowercase_      :    int       		= torch.tensor(
										    [[-2.79_39, -1.79_02, -2.21_89], [-2.85_85, -1.89_08, -2.37_23], [-2.64_99, -1.77_50, -2.25_58]]					)

										self.assertTrue(torch.allclose(output[:, 1:4, 1:4]       ,    a_       ,    atol=1E-4					)					)






'''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


_lowercase		:				Tuple	       =		logging.get_logger(__name__)

# General docstring
_lowercase		:				List[str]	       =		"RegNetConfig"

# Base docstring
_lowercase		:				Dict	       =		"facebook/regnet-y-040"
_lowercase		:				Union[str, Any]	       =		[1, 1088, 7, 7]

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

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





class 					__magic_name__		(					nn.Module):







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







			def 			SCREAMING_SNAKE_CASE_  (			self       :						List[Any]       ,    lowercase_       :						List[str]					):
										lowercase_      :    Dict       		= self.convolution(lowercase_					)
										lowercase_      :    str       		= self.normalization(lowercase_					)
										lowercase_      :    Optional[Any]       		= self.activation(lowercase_					)
										return hidden_state





class 					__magic_name__		(					nn.Module):







			def __init__(			self       :						List[Any]       ,    lowercase_       :						RegNetConfig					):
										super().__init__()
										lowercase_      :    str       		= RegNetConvLayer(
										    config.num_channels       ,    config.embedding_size       ,    kernel_size=3       ,    stride=2       ,    activation=config.hidden_act					)
										lowercase_      :    Any       		= config.num_channels







			def 			SCREAMING_SNAKE_CASE_  (			self       :						Dict       ,    lowercase_       :						Optional[Any]					):
										lowercase_      :    List[str]       		= 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."""					)
										lowercase_      :    Any       		= self.embedder(lowercase_					)
										return hidden_state





class 					__magic_name__		(					nn.Module):







			def __init__(			self       :						Optional[int]       ,    lowercase_       :						int       ,    lowercase_       :						int       ,    lowercase_       :						int = 2					):
										super().__init__()
										lowercase_      :    Optional[Any]       		= nn.Convad(lowercase_       ,    lowercase_       ,    kernel_size=1       ,    stride=lowercase_       ,    bias=lowercase_					)
										lowercase_      :    Union[str, Any]       		= nn.BatchNormad(lowercase_					)







			def 			SCREAMING_SNAKE_CASE_  (			self       :						Tuple       ,    lowercase_       :						Tensor					):
										lowercase_      :    Tuple       		= self.convolution(lowercase_					)
										lowercase_      :    str       		= self.normalization(lowercase_					)
										return hidden_state





class 					__magic_name__		(					nn.Module):







			def __init__(			self       :						str       ,    lowercase_       :						int       ,    lowercase_       :						int					):
										super().__init__()

										lowercase_      :    int       		= nn.AdaptiveAvgPoolad((1, 1)					)
										lowercase_      :    int       		= nn.Sequential(
										    nn.Convad(lowercase_       ,    lowercase_       ,    kernel_size=1					)       ,    nn.ReLU()       ,    nn.Convad(lowercase_       ,    lowercase_       ,    kernel_size=1					)       ,    nn.Sigmoid()       ,    )







			def 			SCREAMING_SNAKE_CASE_  (			self       :						Optional[int]       ,    lowercase_       :						Any					):
										# b c h w -> b c 1 1
										lowercase_      :    List[str]       		= self.pooler(lowercase_					)
										lowercase_      :    Optional[int]       		= self.attention(lowercase_					)
										lowercase_      :    Any       		= hidden_state * attention
										return hidden_state





class 					__magic_name__		(					nn.Module):







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







			def 			SCREAMING_SNAKE_CASE_  (			self       :						Any       ,    lowercase_       :						Any					):
										lowercase_      :    Any       		= hidden_state
										lowercase_      :    Union[str, Any]       		= self.layer(lowercase_					)
										lowercase_      :    Union[str, Any]       		= self.shortcut(lowercase_					)
										hidden_state += residual
										lowercase_      :    str       		= self.activation(lowercase_					)
										return hidden_state





class 					__magic_name__		(					nn.Module):







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







			def 			SCREAMING_SNAKE_CASE_  (			self       :						List[str]       ,    lowercase_       :						Union[str, Any]					):
										lowercase_      :    Optional[int]       		= hidden_state
										lowercase_      :    str       		= self.layer(lowercase_					)
										lowercase_      :    int       		= self.shortcut(lowercase_					)
										hidden_state += residual
										lowercase_      :    Optional[Any]       		= self.activation(lowercase_					)
										return hidden_state





class 					__magic_name__		(					nn.Module):







			def __init__(			self       :						str       ,    lowercase_       :						RegNetConfig       ,    lowercase_       :						int       ,    lowercase_       :						int       ,    lowercase_       :						int = 2       ,    lowercase_       :						int = 2       ,    ):
										super().__init__()

										lowercase_      :    str       		= RegNetXLayer if config.layer_type == """x""" else RegNetYLayer

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







			def 			SCREAMING_SNAKE_CASE_  (			self       :						Union[str, Any]       ,    lowercase_       :						List[str]					):
										lowercase_      :    Tuple       		= self.layers(lowercase_					)
										return hidden_state





class 					__magic_name__		(					nn.Module):







			def __init__(			self       :						Dict       ,    lowercase_       :						RegNetConfig					):
										super().__init__()
										lowercase_      :    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(
										        lowercase_       ,    config.embedding_size       ,    config.hidden_sizes[0]       ,    stride=2 if config.downsample_in_first_stage else 1       ,    depth=config.depths[0]       ,    )					)
										lowercase_      :    Optional[Any]       		= zip(config.hidden_sizes       ,    config.hidden_sizes[1:]					)
										for (in_channels, out_channels), depth in zip(lowercase_       ,    config.depths[1:]					):
																	self.stages.append(RegNetStage(lowercase_       ,    lowercase_       ,    lowercase_       ,    depth=lowercase_					)					)







			def 			SCREAMING_SNAKE_CASE_  (			self       :						str       ,    lowercase_       :						Tensor       ,    lowercase_       :						bool = False       ,    lowercase_       :						bool = True					):
										lowercase_      :    Tuple       		= () if output_hidden_states else None

										for stage_module in self.stages:
																	if output_hidden_states:
																								lowercase_      :    Union[str, Any]       		= hidden_states + (hidden_state,)

																	lowercase_      :    Dict       		= stage_module(lowercase_					)

										if output_hidden_states:
																	lowercase_      :    Optional[Any]       		= 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=lowercase_       ,    hidden_states=lowercase_					)





class 					__magic_name__		(					_UpperCAmelCase):
			UpperCamelCase__       = RegNetConfig
			UpperCamelCase__       = '''regnet'''
			UpperCamelCase__       = '''pixel_values'''
			UpperCamelCase__       = True







			def 			SCREAMING_SNAKE_CASE_  (			self       :						int       ,    lowercase_       :						Optional[Any]					):
										if isinstance(lowercase_       ,    nn.Convad					):
																	nn.init.kaiming_normal_(module.weight       ,    mode="""fan_out"""       ,    nonlinearity="""relu"""					)
										elif isinstance(lowercase_       ,    (nn.BatchNormad, nn.GroupNorm)					):
																	nn.init.constant_(module.weight       ,    1					)
																	nn.init.constant_(module.bias       ,    0					)







			def 			SCREAMING_SNAKE_CASE_  (			self       :						List[Any]       ,    lowercase_       :						Optional[int]       ,    lowercase_       :						Any=False					):
										if isinstance(lowercase_       ,    lowercase_					):
																	lowercase_      :    List[str]       		= value




_lowercase		:				Dict	       =		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"

_lowercase		:				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.''',  _UpperCAmelCase,  )
# Copied from transformers.models.resnet.modeling_resnet.ResNetModel with RESNET->REGNET,ResNet->RegNet
class 					__magic_name__		(					_UpperCAmelCase):







			def __init__(			self       :						Any       ,    lowercase_       :						Any					):
										super().__init__(lowercase_					)
										lowercase_      :    List[str]       		= config
										lowercase_      :    Union[str, Any]       		= RegNetEmbeddings(lowercase_					)
										lowercase_      :    Union[str, Any]       		= RegNetEncoder(lowercase_					)
										lowercase_      :    str       		= nn.AdaptiveAvgPoolad((1, 1)					)
										# Initialize weights and apply final processing
										self.post_init()







			@add_start_docstrings_to_model_forward(lowercase_					)
			@add_code_sample_docstrings(
			    checkpoint=_CHECKPOINT_FOR_DOC       ,    output_type=lowercase_       ,    config_class=_CONFIG_FOR_DOC       ,    modality="""vision"""       ,    expected_output=_EXPECTED_OUTPUT_SHAPE       ,    )
			def 			SCREAMING_SNAKE_CASE_  (			self       :						str       ,    lowercase_       :						Tensor       ,    lowercase_       :						Optional[bool] = None       ,    lowercase_       :						Optional[bool] = None					):
										lowercase_      :    List[Any]       		= (
										    output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states
										)
										lowercase_      :    Tuple       		= return_dict if return_dict is not None else self.config.use_return_dict

										lowercase_      :    str       		= self.embedder(lowercase_					)

										lowercase_      :    Optional[Any]       		= self.encoder(
										    lowercase_       ,    output_hidden_states=lowercase_       ,    return_dict=lowercase_					)

										lowercase_      :    List[Any]       		= encoder_outputs[0]

										lowercase_      :    str       		= self.pooler(lowercase_					)

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

										return BaseModelOutputWithPoolingAndNoAttention(
										    last_hidden_state=lowercase_       ,    pooler_output=lowercase_       ,    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.
    ''',  _UpperCAmelCase,  )
# Copied from transformers.models.resnet.modeling_resnet.ResNetForImageClassification with RESNET->REGNET,ResNet->RegNet,resnet->regnet
class 					__magic_name__		(					_UpperCAmelCase):







			def __init__(			self       :						Dict       ,    lowercase_       :						str					):
										super().__init__(lowercase_					)
										lowercase_      :    Any       		= config.num_labels
										lowercase_      :    List[str]       		= RegNetModel(lowercase_					)
										# classification head
										lowercase_      :    Any       		= 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(lowercase_					)
			@add_code_sample_docstrings(
			    checkpoint=_IMAGE_CLASS_CHECKPOINT       ,    output_type=lowercase_       ,    config_class=_CONFIG_FOR_DOC       ,    expected_output=_IMAGE_CLASS_EXPECTED_OUTPUT       ,    )
			def 			SCREAMING_SNAKE_CASE_  (			self       :						List[str]       ,    lowercase_       :						Optional[torch.FloatTensor] = None       ,    lowercase_       :						Optional[torch.LongTensor] = None       ,    lowercase_       :						Optional[bool] = None       ,    lowercase_       :						Optional[bool] = None       ,    ):
										lowercase_      :    Union[str, Any]       		= return_dict if return_dict is not None else self.config.use_return_dict

										lowercase_      :    Optional[int]       		= self.regnet(lowercase_       ,    output_hidden_states=lowercase_       ,    return_dict=lowercase_					)

										lowercase_      :    Optional[int]       		= outputs.pooler_output if return_dict else outputs[1]

										lowercase_      :    List[Any]       		= self.classifier(lowercase_					)

										lowercase_      :    Optional[int]       		= None

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

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

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





