




"""simple docstring"""
import unittest

from transformers import DebertaConfig, is_torch_available
from transformers.testing_utils import require_sentencepiece, require_tokenizers, require_torch, slow, torch_device

from ...test_configuration_common import ConfigTester
from ...test_modeling_common import ModelTesterMixin, ids_tensor
from ...test_pipeline_mixin import PipelineTesterMixin


if is_torch_available():
					import torch

					from transformers import (
					    DebertaForMaskedLM,
					    DebertaForQuestionAnswering,
					    DebertaForSequenceClassification,
					    DebertaForTokenClassification,
					    DebertaModel,
					)
					from transformers.models.deberta.modeling_deberta import DEBERTA_PRETRAINED_MODEL_ARCHIVE_LIST

class __lowercase(      lowercase__				):




					'''simple docstring'''


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

								__lowerCamelCase					:	Optional[Any]	       =				None
								if self.use_input_mask:
											__lowerCamelCase					:	Any	       =				ids_tensor([self.batch_size, self.seq_length]						,	vocab_size=2     )

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

								__lowerCamelCase					:	Any	       =				None
								__lowerCamelCase					:	Dict	       =				None
								__lowerCamelCase					:	Union[str, Any]	       =				None
								if self.use_labels:
											__lowerCamelCase					:	Any	       =				ids_tensor([self.batch_size]						,	self.type_sequence_label_size     )
											__lowerCamelCase					:	Any	       =				ids_tensor([self.batch_size, self.seq_length]						,	self.num_labels     )
											__lowerCamelCase					:	int	       =				ids_tensor([self.batch_size]						,	self.num_choices     )

								__lowerCamelCase					:	Dict	       =				self.get_config()

								return config, input_ids, token_type_ids, input_mask, sequence_labels, token_labels, choice_labels
					def     snake_case_				(    self     ):
								return DebertaConfig(
								    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						,	initializer_range=self.initializer_range						,	relative_attention=self.relative_attention						,	position_biased_input=self.position_biased_input						,	pos_att_type=self.pos_att_type						,	)
					def     snake_case_				(    self     ):
								__lowerCamelCase					:	Any	       =				self.get_config()
								__lowerCamelCase					:	Dict	       =				300
								return config
					def     snake_case_				(    self						,	__a     ):
								self.parent.assertListEqual(list(result.loss.size()     )						,	[]     )
					def     snake_case_				(    self						,	__a						,	__a						,	__a						,	__a						,	__a						,	__a						,	__a     ):
								__lowerCamelCase					:	Tuple	       =				DebertaModel(config=__a     )
								model.to(__a     )
								model.eval()
								__lowerCamelCase					:	Tuple	       =				model(__a						,	attention_mask=__a						,	token_type_ids=__a     )[0]
								__lowerCamelCase					:	Optional[Any]	       =				model(__a						,	token_type_ids=__a     )[0]
								__lowerCamelCase					:	Dict	       =				model(__a     )[0]

								self.parent.assertListEqual(list(sequence_output.size()     )						,	[self.batch_size, self.seq_length, self.hidden_size]     )
					def     snake_case_				(    self						,	__a						,	__a						,	__a						,	__a						,	__a						,	__a						,	__a     ):
								__lowerCamelCase					:	List[Any]	       =				DebertaForMaskedLM(config=__a     )
								model.to(__a     )
								model.eval()
								__lowerCamelCase					:	Optional[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.vocab_size)     )
					def     snake_case_				(    self						,	__a						,	__a						,	__a						,	__a						,	__a						,	__a						,	__a     ):
								__lowerCamelCase					:	int	       =				self.num_labels
								__lowerCamelCase					:	List[str]	       =				DebertaForSequenceClassification(__a     )
								model.to(__a     )
								model.eval()
								__lowerCamelCase					:	Dict	       =				model(__a						,	attention_mask=__a						,	token_type_ids=__a						,	labels=__a     )
								self.parent.assertListEqual(list(result.logits.size()     )						,	[self.batch_size, self.num_labels]     )
								self.check_loss_output(__a     )
					def     snake_case_				(    self						,	__a						,	__a						,	__a						,	__a						,	__a						,	__a						,	__a     ):
								__lowerCamelCase					:	Optional[int]	       =				self.num_labels
								__lowerCamelCase					:	str	       =				DebertaForTokenClassification(config=__a     )
								model.to(__a     )
								model.eval()
								__lowerCamelCase					:	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     snake_case_				(    self						,	__a						,	__a						,	__a						,	__a						,	__a						,	__a						,	__a     ):
								__lowerCamelCase					:	List[str]	       =				DebertaForQuestionAnswering(config=__a     )
								model.to(__a     )
								model.eval()
								__lowerCamelCase					:	Tuple	       =				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     snake_case_				(    self     ):
								__lowerCamelCase					:	Dict	       =				self.prepare_config_and_inputs()
								(
								    (
								    __lowerCamelCase
								)       ,      (
								    __lowerCamelCase
								)       ,      (
								    __lowerCamelCase
								)       ,      (
								    __lowerCamelCase
								)       ,      (
								    __lowerCamelCase
								)       ,      (
								    __lowerCamelCase
								)       ,      (
								    __lowerCamelCase
								)       ,      
								)					:	List[str]	       =				config_and_inputs
								__lowerCamelCase					:	List[str]	       =				{'input_ids': input_ids, 'token_type_ids': token_type_ids, 'attention_mask': input_mask}
								return config, inputs_dict



@require_torch
class __lowercase(      lowercase__	,	lowercase__	,	unittest.TestCase				):




					'''simple docstring'''


					__a						:						Tuple     		= (
					    (
					        DebertaModel,
					        DebertaForMaskedLM,
					        DebertaForSequenceClassification,
					        DebertaForTokenClassification,
					        DebertaForQuestionAnswering,
					    )
					    if is_torch_available()
					    else ()
					)
					__a						:						int     		= (
					    {
					        'feature-extraction': DebertaModel,
					        'fill-mask': DebertaForMaskedLM,
					        'question-answering': DebertaForQuestionAnswering,
					        'text-classification': DebertaForSequenceClassification,
					        'token-classification': DebertaForTokenClassification,
					        'zero-shot': DebertaForSequenceClassification,
					    }
					    if is_torch_available()
					    else {}
					)

					__a						:						int     		= True
					__a						:						Optional[Any]     		= False
					__a						:						Tuple     		= False
					__a						:						Optional[Any]     		= False
					__a						:						Tuple     		= False
					def     snake_case_				(    self     ):
								__lowerCamelCase					:	Union[str, Any]	       =				DebertaModelTester(self     )
								__lowerCamelCase					:	Tuple	       =				ConfigTester(self						,	config_class=__a						,	hidden_size=37     )
					def     snake_case_				(    self     ):
								self.config_tester.run_common_tests()
					def     snake_case_				(    self     ):
								__lowerCamelCase					:	Union[str, Any]	       =				self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_deberta_model(*__a     )
					def     snake_case_				(    self     ):
								__lowerCamelCase					:	str	       =				self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_deberta_for_sequence_classification(*__a     )
					def     snake_case_				(    self     ):
								__lowerCamelCase					:	int	       =				self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_deberta_for_masked_lm(*__a     )
					def     snake_case_				(    self     ):
								__lowerCamelCase					:	Dict	       =				self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_deberta_for_question_answering(*__a     )
					def     snake_case_				(    self     ):
								__lowerCamelCase					:	Optional[Any]	       =				self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_deberta_for_token_classification(*__a     )





					@slow
					def     snake_case_				(    self     ):
								for model_name in DEBERTA_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
											__lowerCamelCase					:	Any	       =				DebertaModel.from_pretrained(__a     )
											self.assertIsNotNone(__a     )



@require_torch
@require_sentencepiece
@require_tokenizers
class __lowercase(      unittest.TestCase				):




					'''simple docstring'''


					@unittest.skip(reason='Model not available yet'     )
					def     snake_case_				(    self     ):
								pass





					@slow
					def     snake_case_				(    self     ):
								__lowerCamelCase					:	Dict	       =				DebertaModel.from_pretrained('microsoft/deberta-base'     )

								__lowerCamelCase					:	str	       =				torch.tensor([[0, 31414, 232, 328, 740, 1140, 12695, 69, 46078, 1588, 2]]     )
								__lowerCamelCase					:	int	       =				torch.tensor([[0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]]     )
								with torch.no_grad():
											__lowerCamelCase					:	List[Any]	       =				model(__a						,	attention_mask=__a     )[0]
								# compare the actual values for a slice.
								__lowerCamelCase					:	Optional[int]	       =				torch.tensor(
								    [[[-0.5_986, -0.8_055, -0.8_462], [1.4_484, -0.9_348, -0.8_059], [0.3_123, 0.0_032, -1.4_131]]]     )
								self.assertTrue(torch.allclose(output[:, 1:4, 1:4]						,	__a						,	atol=1E-4     )						,	f'''{output[:, 1:4, 1:4]}'''     )









"""simple docstring"""
import collections.abc
from typing import Optional, Tuple, Union

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

from ...activations import ACTaFN
from ...modeling_outputs import BaseModelOutputWithNoAttention, ImageClassifierOutputWithNoAttention
from ...modeling_utils import PreTrainedModel
from ...utils import add_code_sample_docstrings, add_start_docstrings, add_start_docstrings_to_model_forward, logging
from .configuration_poolformer import PoolFormerConfig


a_				:    Tuple										=     logging.get_logger(__name__)

# General docstring
a_				:    List[str]										=     '''PoolFormerConfig'''

# Base docstring
a_				:    Optional[Any]										=     '''sail/poolformer_s12'''
a_				:    List[Any]										=     [1, 5_12, 7, 7]

# Image classification docstring
a_				:    Any										=     '''sail/poolformer_s12'''
a_				:    Optional[int]										=     '''tabby, tabby cat'''

a_				:    Optional[Any]										=     [
    '''sail/poolformer_s12''',
    # See all PoolFormer models at https://huggingface.co/models?filter=poolformer
]





def 						UpperCAmelCase				(			A__:    Optional[Any]							,				A__:    float = 0.0							,				A__:    bool = False     )  ->     Tuple:
			if drop_prob == 0.0 or not training:
						return input
			__lowerCamelCase					:	Dict	       =				1 - drop_prob
			__lowerCamelCase					:	List[Any]	       =				(input.shape[0],) + (1,) * (input.ndim - 1)  # work with diff dim tensors, not just 2D ConvNets
			__lowerCamelCase					:	List[Any]	       =				keep_prob + torch.rand(A__							,				dtype=input.dtype							,				device=input.device     )
			random_tensor.floor_()  # binarize
			__lowerCamelCase					:	Any	       =				input.div(A__     ) * random_tensor
			return output

class __lowercase(      nn.Module				):




					'''simple docstring'''


					def __init__(    self						,	__a = None     ):
								super().__init__()
								__lowerCamelCase					:	int	       =				drop_prob
					def     snake_case_				(    self						,	__a     ):
								return drop_path(__a						,	self.drop_prob						,	self.training     )





					def     snake_case_				(    self     ):
								return "p={}".format(self.drop_prob     )



class __lowercase(      nn.Module				):




					'''simple docstring'''


					def __init__(    self						,	__a						,	__a						,	__a						,	__a						,	__a						,	__a=None     ):
								super().__init__()
								__lowerCamelCase					:	int	       =				patch_size if isinstance(__a						,	collections.abc.Iterable     ) else (patch_size, patch_size)
								__lowerCamelCase					:	int	       =				stride if isinstance(__a						,	collections.abc.Iterable     ) else (stride, stride)
								__lowerCamelCase					:	Optional[int]	       =				padding if isinstance(__a						,	collections.abc.Iterable     ) else (padding, padding)

								__lowerCamelCase					:	Optional[Any]	       =				nn.Convad(__a						,	__a						,	kernel_size=__a						,	stride=__a						,	padding=__a     )
								__lowerCamelCase					:	List[str]	       =				norm_layer(__a     ) if norm_layer else nn.Identity()





					def     snake_case_				(    self						,	__a     ):
								__lowerCamelCase					:	List[Any]	       =				self.projection(__a     )
								__lowerCamelCase					:	Dict	       =				self.norm(__a     )
								return embeddings



class __lowercase(      nn.GroupNorm				):




					'''simple docstring'''


					def __init__(    self						,	__a						,	**__a     ):
								super().__init__(1						,	__a						,	**__a     )



class __lowercase(      nn.Module				):




					'''simple docstring'''


					def __init__(    self						,	__a     ):
								super().__init__()
								__lowerCamelCase					:	str	       =				nn.AvgPoolad(__a						,	stride=1						,	padding=pool_size // 2						,	count_include_pad=__a     )





					def     snake_case_				(    self						,	__a     ):
								return self.pool(__a     ) - hidden_states



class __lowercase(      nn.Module				):




					'''simple docstring'''


					def __init__(    self						,	__a						,	__a						,	__a						,	__a     ):
								super().__init__()
								__lowerCamelCase					:	Any	       =				nn.Convad(__a						,	__a						,	1     )
								__lowerCamelCase					:	Dict	       =				nn.Convad(__a						,	__a						,	1     )
								__lowerCamelCase					:	List[Any]	       =				PoolFormerDropPath(__a     )
								if isinstance(config.hidden_act						,	__a     ):
											__lowerCamelCase					:	List[str]	       =				ACTaFN[config.hidden_act]
								else:
											__lowerCamelCase					:	str	       =				config.hidden_act





					def     snake_case_				(    self						,	__a     ):
								__lowerCamelCase					:	int	       =				self.conva(__a     )
								__lowerCamelCase					:	Dict	       =				self.act_fn(__a     )
								__lowerCamelCase					:	List[str]	       =				self.drop(__a     )
								__lowerCamelCase					:	int	       =				self.conva(__a     )
								__lowerCamelCase					:	str	       =				self.drop(__a     )

								return hidden_states



class __lowercase(      nn.Module				):




					'''simple docstring'''


					def __init__(    self						,	__a						,	__a						,	__a						,	__a						,	__a						,	__a     ):
								super().__init__()
								__lowerCamelCase					:	Tuple	       =				PoolFormerPooling(__a     )
								__lowerCamelCase					:	Union[str, Any]	       =				PoolFormerOutput(__a						,	__a						,	__a						,	__a     )
								__lowerCamelCase					:	List[Any]	       =				PoolFormerGroupNorm(__a     )
								__lowerCamelCase					:	List[Any]	       =				PoolFormerGroupNorm(__a     )

								# Useful for training neural nets
								__lowerCamelCase					:	Any	       =				PoolFormerDropPath(__a     ) if drop_path > 0.0 else nn.Identity()
								__lowerCamelCase					:	Tuple	       =				config.use_layer_scale
								if config.use_layer_scale:
											__lowerCamelCase					:	List[str]	       =				nn.Parameter(
											    config.layer_scale_init_value * torch.ones((__a)     )						,	requires_grad=__a     )
											__lowerCamelCase					:	Optional[int]	       =				nn.Parameter(
											    config.layer_scale_init_value * torch.ones((__a)     )						,	requires_grad=__a     )





					def     snake_case_				(    self						,	__a     ):
								if self.use_layer_scale:
											__lowerCamelCase					:	Union[str, Any]	       =				self.pooling(self.before_norm(__a     )     )
											__lowerCamelCase					:	Any	       =				self.layer_scale_a.unsqueeze(-1     ).unsqueeze(-1     ) * pooling_output
											# First residual connection
											__lowerCamelCase					:	Optional[Any]	       =				hidden_states + self.drop_path(__a     )
											__lowerCamelCase					:	Tuple	       =				()

											__lowerCamelCase					:	Optional[Any]	       =				self.output(self.after_norm(__a     )     )
											__lowerCamelCase					:	List[Any]	       =				self.layer_scale_a.unsqueeze(-1     ).unsqueeze(-1     ) * layer_output
											# Second residual connection
											__lowerCamelCase					:	List[Any]	       =				hidden_states + self.drop_path(__a     )

											__lowerCamelCase					:	Optional[Any]	       =				(output,) + outputs
											return outputs

								else:
											__lowerCamelCase					:	Tuple	       =				self.drop_path(self.pooling(self.before_norm(__a     )     )     )
											# First residual connection
											__lowerCamelCase					:	List[str]	       =				pooling_output + hidden_states
											__lowerCamelCase					:	int	       =				()

											# Second residual connection inside the PoolFormerOutput block
											__lowerCamelCase					:	List[str]	       =				self.drop_path(self.output(self.after_norm(__a     )     )     )
											__lowerCamelCase					:	str	       =				hidden_states + layer_output

											__lowerCamelCase					:	int	       =				(output,) + outputs
											return outputs



class __lowercase(      nn.Module				):




					'''simple docstring'''


					def __init__(    self						,	__a     ):
								super().__init__()
								__lowerCamelCase					:	int	       =				config
								# stochastic depth decay rule
								__lowerCamelCase					:	int	       =				[x.item() for x in torch.linspace(0						,	config.drop_path_rate						,	sum(config.depths     )     )]

								# patch embeddings
								__lowerCamelCase					:	List[str]	       =				[]
								for i in range(config.num_encoder_blocks     ):
											embeddings.append(
											    PoolFormerEmbeddings(
											        patch_size=config.patch_sizes[i]						,	stride=config.strides[i]						,	padding=config.padding[i]						,	num_channels=config.num_channels if i == 0 else config.hidden_sizes[i - 1]						,	hidden_size=config.hidden_sizes[i]						,	)     )
								__lowerCamelCase					:	Optional[int]	       =				nn.ModuleList(__a     )

								# Transformer blocks
								__lowerCamelCase					:	Any	       =				[]
								__lowerCamelCase					:	int	       =				0
								for i in range(config.num_encoder_blocks     ):
											# each block consists of layers
											__lowerCamelCase					:	Optional[int]	       =				[]
											if i != 0:
														cur += config.depths[i - 1]
											for j in range(config.depths[i]     ):
														layers.append(
														    PoolFormerLayer(
														        __a						,	num_channels=config.hidden_sizes[i]						,	pool_size=config.pool_size						,	hidden_size=config.hidden_sizes[i]						,	intermediate_size=int(config.hidden_sizes[i] * config.mlp_ratio     )						,	drop_path=dpr[cur + j]						,	)     )
											blocks.append(nn.ModuleList(__a     )     )

								__lowerCamelCase					:	str	       =				nn.ModuleList(__a     )





					def     snake_case_				(    self						,	__a						,	__a=False						,	__a=True     ):
								__lowerCamelCase					:	Union[str, Any]	       =				() if output_hidden_states else None

								__lowerCamelCase					:	int	       =				pixel_values
								for idx, layers in enumerate(zip(self.patch_embeddings						,	self.block     )     ):
											__lowerCamelCase       ,      __lowerCamelCase					:	Any	       =				layers
											# Get patch embeddings from hidden_states
											__lowerCamelCase					:	Any	       =				embedding_layer(__a     )
											# Send the embeddings through the blocks
											for _, blk in enumerate(__a     ):
														__lowerCamelCase					:	Optional[int]	       =				blk(__a     )
														__lowerCamelCase					:	Tuple	       =				layer_outputs[0]

											if output_hidden_states:
														__lowerCamelCase					:	Union[str, Any]	       =				all_hidden_states + (hidden_states,)

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

								return BaseModelOutputWithNoAttention(last_hidden_state=__a						,	hidden_states=__a     )



class __lowercase(      lowercase__				):




					'''simple docstring'''


					__a						:						Tuple     		= PoolFormerConfig
					__a						:						Tuple     		= 'poolformer'
					__a						:						Optional[int]     		= 'pixel_values'
					__a						:						Optional[Any]     		= True
					def     snake_case_				(    self						,	__a     ):
								if isinstance(__a						,	(nn.Linear, nn.Convad)     ):
											module.weight.data.normal_(mean=0.0						,	std=self.config.initializer_range     )
											if module.bias is not None:
														module.bias.data.zero_()
								elif isinstance(__a						,	nn.LayerNorm     ):
											module.bias.data.zero_()
											module.weight.data.fill_(1.0     )





					def     snake_case_				(    self						,	__a						,	__a=False     ):
								if isinstance(__a						,	__a     ):
											__lowerCamelCase					:	Union[str, Any]	       =				value



a_				:    Union[str, Any]										=     R'''
    This model is a PyTorch [torch.nn.Module](https://pytorch.org/docs/stable/nn.html#torch.nn.Module) sub-class. Use
    it as a regular PyTorch Module and refer to the PyTorch documentation for all matter related to general usage and
    behavior.

    Parameters:
        config ([`PoolFormerConfig`]): Model configuration class with all the parameters of the model.
            Initializing with a config file does not load the weights associated with the model, only the
            configuration. Check out the [`~PreTrainedModel.from_pretrained`] method to load the model weights.
'''

a_				:    List[str]										=     R'''
    Args:
        pixel_values (`torch.FloatTensor` of shape `(batch_size, num_channels, height, width)`):
            Pixel values. Pixel values can be obtained using [`AutoImageProcessor`]. See
            [`PoolFormerImageProcessor.__call__`] for details.
'''

@add_start_docstrings(
    'The bare PoolFormer Model transformer outputting raw hidden-states without any specific head on top.'	,	lowercase__	,	)
class __lowercase(      lowercase__				):




					'''simple docstring'''


					def __init__(    self						,	__a     ):
								super().__init__(__a     )
								__lowerCamelCase					:	Optional[Any]	       =				config

								__lowerCamelCase					:	Any	       =				PoolFormerEncoder(__a     )

								# Initialize weights and apply final processing
								self.post_init()
					def     snake_case_				(    self     ):
								return self.embeddings.patch_embeddings





					@add_start_docstrings_to_model_forward(__a     )
					@add_code_sample_docstrings(
					    checkpoint=_CHECKPOINT_FOR_DOC						,	output_type=__a						,	config_class=_CONFIG_FOR_DOC						,	modality='vision'						,	expected_output=_EXPECTED_OUTPUT_SHAPE						,	)
					def     snake_case_				(    self						,	__a = None						,	__a = None						,	__a = None						,	):
								__lowerCamelCase					:	Union[str, Any]	       =				(
								    output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states
								)
								__lowerCamelCase					:	Dict	       =				return_dict if return_dict is not None else self.config.use_return_dict

								if pixel_values is None:
											raise ValueError('You have to specify pixel_values'     )

								__lowerCamelCase					:	Any	       =				self.encoder(
								    __a						,	output_hidden_states=__a						,	return_dict=__a						,	)
								__lowerCamelCase					:	int	       =				encoder_outputs[0]

								if not return_dict:
											return (sequence_output, None) + encoder_outputs[1:]

								return BaseModelOutputWithNoAttention(
								    last_hidden_state=__a						,	hidden_states=encoder_outputs.hidden_states						,	)



class __lowercase(      nn.Module				):




					'''simple docstring'''


					def __init__(    self						,	__a     ):
								super().__init__()
								__lowerCamelCase					:	Optional[Any]	       =				nn.Linear(config.hidden_size						,	config.hidden_size     )





					def     snake_case_				(    self						,	__a     ):
								__lowerCamelCase					:	List[Any]	       =				self.dense(__a     )
								return output



@add_start_docstrings(
    '\n    PoolFormer Model transformer with an image classification head on top\n    '	,	lowercase__	,	)
class __lowercase(      lowercase__				):




					'''simple docstring'''


					def __init__(    self						,	__a     ):
								super().__init__(__a     )
								__lowerCamelCase					:	str	       =				config.num_labels
								__lowerCamelCase					:	Optional[Any]	       =				PoolFormerModel(__a     )

								# Final norm
								__lowerCamelCase					:	str	       =				PoolFormerGroupNorm(config.hidden_sizes[-1]     )
								# Classifier head
								__lowerCamelCase					:	Optional[Any]	       =				(
								    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(__a     )
					@add_code_sample_docstrings(
					    checkpoint=_IMAGE_CLASS_CHECKPOINT						,	output_type=__a						,	config_class=_CONFIG_FOR_DOC						,	expected_output=_IMAGE_CLASS_EXPECTED_OUTPUT						,	)
					def     snake_case_				(    self						,	__a = None						,	__a = None						,	__a = None						,	__a = None						,	):
								__lowerCamelCase					:	Any	       =				return_dict if return_dict is not None else self.config.use_return_dict

								__lowerCamelCase					:	Tuple	       =				self.poolformer(
								    __a						,	output_hidden_states=__a						,	return_dict=__a						,	)

								__lowerCamelCase					:	int	       =				outputs[0]

								__lowerCamelCase					:	Optional[int]	       =				self.classifier(self.norm(__a     ).mean([-2, -1]     )     )

								__lowerCamelCase					:	Union[str, Any]	       =				None
								if labels is not None:
											if self.config.problem_type is None:
														if self.num_labels == 1:
																	__lowerCamelCase					:	Any	       =				'regression'
														elif self.num_labels > 1 and (labels.dtype == torch.long or labels.dtype == torch.int):
																	__lowerCamelCase					:	Any	       =				'single_label_classification'
														else:
																	__lowerCamelCase					:	Optional[Any]	       =				'multi_label_classification'

											if self.config.problem_type == "regression":
														__lowerCamelCase					:	int	       =				MSELoss()
														if self.num_labels == 1:
																	__lowerCamelCase					:	Optional[Any]	       =				loss_fct(logits.squeeze()						,	labels.squeeze()     )
														else:
																	__lowerCamelCase					:	Optional[Any]	       =				loss_fct(__a						,	__a     )
											elif self.config.problem_type == "single_label_classification":
														__lowerCamelCase					:	Tuple	       =				CrossEntropyLoss()
														__lowerCamelCase					:	int	       =				loss_fct(logits.view(-1						,	self.num_labels     )						,	labels.view(-1     )     )
											elif self.config.problem_type == "multi_label_classification":
														__lowerCamelCase					:	List[Any]	       =				BCEWithLogitsLoss()
														__lowerCamelCase					:	Optional[Any]	       =				loss_fct(__a						,	__a     )

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

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



