


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


lowerCamelCase__    	=     logging.get_logger(__name__)

# General docstring
lowerCamelCase__    	=     'PoolFormerConfig'

# Base docstring
lowerCamelCase__    	=     'sail/poolformer_s12'
lowerCamelCase__    	=     [1, 512, 7, 7]

# Image classification docstring
lowerCamelCase__    	=     'sail/poolformer_s12'
lowerCamelCase__    	=     'tabby, tabby cat'

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






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






class 	lowerCAmelCase__			(			nn.Module	):




			def __init__(		self  :     Optional[int]	,  lowerCamelCase__  :     Optional[float] = None )   ->None:



					'''simple docstring'''







					super().__init__()
					_UpperCAmelCase		:       Union[str, Any]								=      drop_prob




			def 							lowerCAmelCase__    (		self  :     Any	,  lowerCamelCase__  :     torch.Tensor )   ->torch.Tensor:



					'''simple docstring'''







					return drop_path(lowerCamelCase__	,  self.drop_prob	,  self.training )






			def 							lowerCAmelCase__    (		self  :     List[str] )   ->str:



					'''simple docstring'''







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






class 	lowerCAmelCase__			(			nn.Module	):




			def __init__(		self  :     Any	,  lowerCamelCase__  :     List[str]	,  lowerCamelCase__  :     int	,  lowerCamelCase__  :     Tuple	,  lowerCamelCase__  :     Tuple	,  lowerCamelCase__  :     Optional[Any]	,  lowerCamelCase__  :     Dict=None )   ->Optional[int]:



					'''simple docstring'''







					super().__init__()
					_UpperCAmelCase		:       Dict								=      patch_size if isinstance(lowerCamelCase__	,  collections.abc.Iterable ) else (patch_size, patch_size)
					_UpperCAmelCase		:       Optional[int]								=      stride if isinstance(lowerCamelCase__	,  collections.abc.Iterable ) else (stride, stride)
					_UpperCAmelCase		:       Any								=      padding if isinstance(lowerCamelCase__	,  collections.abc.Iterable ) else (padding, padding)

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






			def 							lowerCAmelCase__    (		self  :     Optional[int]	,  lowerCamelCase__  :     List[str] )   ->List[Any]:



					'''simple docstring'''







					_UpperCAmelCase		:       Dict								=      self.projection(lowerCamelCase__ )
					_UpperCAmelCase		:       int								=      self.norm(lowerCamelCase__ )
					return embeddings






class 	lowerCAmelCase__			(			nn.GroupNorm	):




			def __init__(		self  :     Any	,  lowerCamelCase__  :     List[str]	,  **lowerCamelCase__  :     Any )   ->Dict:



					'''simple docstring'''







					super().__init__(1	,  lowerCamelCase__	,  **lowerCamelCase__ )






class 	lowerCAmelCase__			(			nn.Module	):




			def __init__(		self  :     Any	,  lowerCamelCase__  :     Optional[int] )   ->List[Any]:



					'''simple docstring'''







					super().__init__()
					_UpperCAmelCase		:       int								=      nn.AvgPoolad(lowerCamelCase__	,  stride=1	,  padding=pool_size // 2	,  count_include_pad=lowerCamelCase__ )






			def 							lowerCAmelCase__    (		self  :     int	,  lowerCamelCase__  :     Union[str, Any] )   ->int:



					'''simple docstring'''







					return self.pool(lowerCamelCase__ ) - hidden_states






class 	lowerCAmelCase__			(			nn.Module	):




			def __init__(		self  :     Optional[Any]	,  lowerCamelCase__  :     List[Any]	,  lowerCamelCase__  :     int	,  lowerCamelCase__  :     int	,  lowerCamelCase__  :     int )   ->Optional[Any]:



					'''simple docstring'''







					super().__init__()
					_UpperCAmelCase		:       Optional[Any]								=      nn.Convad(lowerCamelCase__	,  lowerCamelCase__	,  1 )
					_UpperCAmelCase		:       Optional[Any]								=      nn.Convad(lowerCamelCase__	,  lowerCamelCase__	,  1 )
					_UpperCAmelCase		:       Union[str, Any]								=      PoolFormerDropPath(lowerCamelCase__ )
					if isinstance(config.hidden_act	,  lowerCamelCase__ ):
							_UpperCAmelCase		:       str								=      ACTaFN[config.hidden_act]
					else:
							_UpperCAmelCase		:       int								=      config.hidden_act






			def 							lowerCAmelCase__    (		self  :     Any	,  lowerCamelCase__  :     Optional[int] )   ->List[str]:



					'''simple docstring'''







					_UpperCAmelCase		:       Dict								=      self.conva(lowerCamelCase__ )
					_UpperCAmelCase		:       str								=      self.act_fn(lowerCamelCase__ )
					_UpperCAmelCase		:       Optional[int]								=      self.drop(lowerCamelCase__ )
					_UpperCAmelCase		:       int								=      self.conva(lowerCamelCase__ )
					_UpperCAmelCase		:       Tuple								=      self.drop(lowerCamelCase__ )

					return hidden_states






class 	lowerCAmelCase__			(			nn.Module	):




			def __init__(		self  :     Union[str, Any]	,  lowerCamelCase__  :     Union[str, Any]	,  lowerCamelCase__  :     List[str]	,  lowerCamelCase__  :     List[Any]	,  lowerCamelCase__  :     Tuple	,  lowerCamelCase__  :     Dict	,  lowerCamelCase__  :     Optional[Any] )   ->Optional[Any]:



					'''simple docstring'''







					super().__init__()
					_UpperCAmelCase		:       int								=      PoolFormerPooling(lowerCamelCase__ )
					_UpperCAmelCase		:       Optional[int]								=      PoolFormerOutput(lowerCamelCase__	,  lowerCamelCase__	,  lowerCamelCase__	,  lowerCamelCase__ )
					_UpperCAmelCase		:       Optional[int]								=      PoolFormerGroupNorm(lowerCamelCase__ )
					_UpperCAmelCase		:       Optional[Any]								=      PoolFormerGroupNorm(lowerCamelCase__ )

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






			def 							lowerCAmelCase__    (		self  :     Union[str, Any]	,  lowerCamelCase__  :     Any )   ->str:



					'''simple docstring'''







					if self.use_layer_scale:
							_UpperCAmelCase		:       Tuple								=      self.pooling(self.before_norm(lowerCamelCase__ ) )
							_UpperCAmelCase		:       Optional[Any]								=      self.layer_scale_a.unsqueeze(-1 ).unsqueeze(-1 ) * pooling_output
							# First residual connection
							_UpperCAmelCase		:       Optional[int]								=      hidden_states + self.drop_path(lowerCamelCase__ )
							_UpperCAmelCase		:       Optional[int]								=      ()

							_UpperCAmelCase		:       Optional[int]								=      self.output(self.after_norm(lowerCamelCase__ ) )
							_UpperCAmelCase		:       Tuple								=      self.layer_scale_a.unsqueeze(-1 ).unsqueeze(-1 ) * layer_output
							# Second residual connection
							_UpperCAmelCase		:       str								=      hidden_states + self.drop_path(lowerCamelCase__ )

							_UpperCAmelCase		:       List[Any]								=      (output,) + outputs
							return outputs

					else:
							_UpperCAmelCase		:       Dict								=      self.drop_path(self.pooling(self.before_norm(lowerCamelCase__ ) ) )
							# First residual connection
							_UpperCAmelCase		:       Tuple								=      pooling_output + hidden_states
							_UpperCAmelCase		:       str								=      ()

							# Second residual connection inside the PoolFormerOutput block
							_UpperCAmelCase		:       Optional[int]								=      self.drop_path(self.output(self.after_norm(lowerCamelCase__ ) ) )
							_UpperCAmelCase		:       int								=      hidden_states + layer_output

							_UpperCAmelCase		:       Optional[int]								=      (output,) + outputs
							return outputs






class 	lowerCAmelCase__			(			nn.Module	):




			def __init__(		self  :     List[str]	,  lowerCamelCase__  :     Optional[int] )   ->Union[str, Any]:



					'''simple docstring'''







					super().__init__()
					_UpperCAmelCase		:       Dict								=      config
					# stochastic depth decay rule
					_UpperCAmelCase		:       Optional[int]								=      [x.item() for x in torch.linspace(0	,  config.drop_path_rate	,  sum(config.depths ) )]

					# patch embeddings
					_UpperCAmelCase		:       Any								=      []
					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]	,  ) )
					_UpperCAmelCase		:       Tuple								=      nn.ModuleList(lowerCamelCase__ )

					# Transformer blocks
					_UpperCAmelCase		:       Any								=      []
					_UpperCAmelCase		:       Tuple								=      0
					for i in range(config.num_encoder_blocks ):
							# each block consists of layers
							_UpperCAmelCase		:       List[Any]								=      []
							if i != 0:
									cur += config.depths[i - 1]
							for j in range(config.depths[i] ):
									layers.append(
									    PoolFormerLayer(
									        lowerCamelCase__	,  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(lowerCamelCase__ ) )

					_UpperCAmelCase		:       List[str]								=      nn.ModuleList(lowerCamelCase__ )






			def 							lowerCAmelCase__    (		self  :     Optional[int]	,  lowerCamelCase__  :     str	,  lowerCamelCase__  :     List[str]=False	,  lowerCamelCase__  :     int=True )   ->List[Any]:



					'''simple docstring'''







					_UpperCAmelCase		:       List[str]								=      () if output_hidden_states else None

					_UpperCAmelCase		:       List[Any]								=      pixel_values
					for idx, layers in enumerate(zip(self.patch_embeddings	,  self.block ) ):
							_UpperCAmelCase		:       Dict								=      layers
							# Get patch embeddings from hidden_states
							_UpperCAmelCase		:       List[str]								=      embedding_layer(lowerCamelCase__ )
							# Send the embeddings through the blocks
							for _, blk in enumerate(lowerCamelCase__ ):
									_UpperCAmelCase		:       Union[str, Any]								=      blk(lowerCamelCase__ )
									_UpperCAmelCase		:       str								=      layer_outputs[0]

							if output_hidden_states:
									_UpperCAmelCase		:       str								=      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=lowerCamelCase__	,  hidden_states=lowerCamelCase__ )






class 	lowerCAmelCase__			(			UpperCAmelCase__	):
			lowerCAmelCase :	List[Any]          =     PoolFormerConfig
			lowerCAmelCase :	Optional[int]          =     "poolformer"
			lowerCAmelCase :	Dict          =     "pixel_values"
			lowerCAmelCase :	Dict          =     True




			def 							lowerCAmelCase__    (		self  :     Optional[int]	,  lowerCamelCase__  :     Union[str, Any] )   ->List[str]:



					'''simple docstring'''







					if isinstance(lowerCamelCase__	,  (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(lowerCamelCase__	,  nn.LayerNorm ):
							module.bias.data.zero_()
							module.weight.data.fill_(1.0 )






			def 							lowerCAmelCase__    (		self  :     List[Any]	,  lowerCamelCase__  :     Optional[int]	,  lowerCamelCase__  :     List[Any]=False )   ->Any:



					'''simple docstring'''







					if isinstance(lowerCamelCase__	,  lowerCamelCase__ ):
							_UpperCAmelCase		:       Optional[Any]								=      value




lowerCamelCase__    	=     r'\n    This model is a PyTorch [torch.nn.Module](https://pytorch.org/docs/stable/nn.html#torch.nn.Module) sub-class. Use\n    it 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 ([`PoolFormerConfig`]): 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'

lowerCamelCase__    	=     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            [`PoolFormerImageProcessor.__call__`] for details.\n'






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




			def __init__(		self  :     Dict	,  lowerCamelCase__  :     str )   ->str:



					'''simple docstring'''







					super().__init__(lowerCamelCase__ )
					_UpperCAmelCase		:       Dict								=      config

					_UpperCAmelCase		:       int								=      PoolFormerEncoder(lowerCamelCase__ )

					# Initialize weights and apply final processing
					self.post_init()




			def 							lowerCAmelCase__    (		self  :     Dict )   ->Dict:



					'''simple docstring'''







					return self.embeddings.patch_embeddings






			@add_start_docstrings_to_model_forward(lowerCamelCase__ )
			@add_code_sample_docstrings(
			    checkpoint=_CHECKPOINT_FOR_DOC	,  output_type=lowerCamelCase__	,  config_class=_CONFIG_FOR_DOC	,  modality="vision"	,  expected_output=_EXPECTED_OUTPUT_SHAPE	,  )
			def 							lowerCAmelCase__    (		self  :     Optional[Any]	,  lowerCamelCase__  :     Optional[torch.FloatTensor] = None	,  lowerCamelCase__  :     Optional[bool] = None	,  lowerCamelCase__  :     Optional[bool] = None	,  )   ->Union[Tuple, BaseModelOutputWithNoAttention]:



					'''simple docstring'''







					_UpperCAmelCase		:       int								=      (
					    output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states
					)
					_UpperCAmelCase		:       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" )

					_UpperCAmelCase		:       List[str]								=      self.encoder(
					    lowerCamelCase__	,  output_hidden_states=lowerCamelCase__	,  return_dict=lowerCamelCase__	,  )
					_UpperCAmelCase		:       int								=      encoder_outputs[0]

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

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






class 	lowerCAmelCase__			(			nn.Module	):




			def __init__(		self  :     Dict	,  lowerCamelCase__  :     str )   ->Tuple:



					'''simple docstring'''







					super().__init__()
					_UpperCAmelCase		:       List[Any]								=      nn.Linear(config.hidden_size	,  config.hidden_size )






			def 							lowerCAmelCase__    (		self  :     Optional[int]	,  lowerCamelCase__  :     Any )   ->List[str]:



					'''simple docstring'''







					_UpperCAmelCase		:       Optional[Any]								=      self.dense(lowerCamelCase__ )
					return output






@add_start_docstrings(
    "\n    PoolFormer Model transformer with an image classification head on top\n    "    ,       UpperCAmelCase__    ,       )
class 	lowerCAmelCase__			(			UpperCAmelCase__	):




			def __init__(		self  :     str	,  lowerCamelCase__  :     Optional[Any] )   ->Tuple:



					'''simple docstring'''







					super().__init__(lowerCamelCase__ )
					_UpperCAmelCase		:       List[Any]								=      config.num_labels
					_UpperCAmelCase		:       str								=      PoolFormerModel(lowerCamelCase__ )

					# Final norm
					_UpperCAmelCase		:       List[str]								=      PoolFormerGroupNorm(config.hidden_sizes[-1] )
					# Classifier head
					_UpperCAmelCase		:       Union[str, 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(lowerCamelCase__ )
			@add_code_sample_docstrings(
			    checkpoint=_IMAGE_CLASS_CHECKPOINT	,  output_type=lowerCamelCase__	,  config_class=_CONFIG_FOR_DOC	,  expected_output=_IMAGE_CLASS_EXPECTED_OUTPUT	,  )
			def 							lowerCAmelCase__    (		self  :     Optional[Any]	,  lowerCamelCase__  :     Optional[torch.FloatTensor] = None	,  lowerCamelCase__  :     Optional[torch.LongTensor] = None	,  lowerCamelCase__  :     Optional[bool] = None	,  lowerCamelCase__  :     Optional[bool] = None	,  )   ->Union[Tuple, ImageClassifierOutputWithNoAttention]:



					'''simple docstring'''







					_UpperCAmelCase		:       Optional[int]								=      return_dict if return_dict is not None else self.config.use_return_dict

					_UpperCAmelCase		:       Any								=      self.poolformer(
					    lowerCamelCase__	,  output_hidden_states=lowerCamelCase__	,  return_dict=lowerCamelCase__	,  )

					_UpperCAmelCase		:       List[Any]								=      outputs[0]

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

					_UpperCAmelCase		:       int								=      None
					if labels is not None:
							if self.config.problem_type is None:
									if self.num_labels == 1:
											_UpperCAmelCase		:       Optional[int]								=      "regression"
									elif self.num_labels > 1 and (labels.dtype == torch.long or labels.dtype == torch.int):
											_UpperCAmelCase		:       List[Any]								=      "single_label_classification"
									else:
											_UpperCAmelCase		:       Any								=      "multi_label_classification"

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

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

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










'''simple docstring'''


from typing import TYPE_CHECKING

from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available


lowerCamelCase__    	=     {
    'configuration_instructblip': [
        'INSTRUCTBLIP_PRETRAINED_CONFIG_ARCHIVE_MAP',
        'InstructBlipConfig',
        'InstructBlipQFormerConfig',
        'InstructBlipVisionConfig',
    ],
    'processing_instructblip': ['InstructBlipProcessor'],
}

try:
						if not is_torch_available():
												raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
						pass
else:
						lowerCamelCase__    	=     [
						    'INSTRUCTBLIP_PRETRAINED_MODEL_ARCHIVE_LIST',
						    'InstructBlipQFormerModel',
						    'InstructBlipPreTrainedModel',
						    'InstructBlipForConditionalGeneration',
						    'InstructBlipVisionModel',
						]

if TYPE_CHECKING:
						from .configuration_instructblip import (
						    INSTRUCTBLIP_PRETRAINED_CONFIG_ARCHIVE_MAP,
						    InstructBlipConfig,
						    InstructBlipQFormerConfig,
						    InstructBlipVisionConfig,
						)
						from .processing_instructblip import InstructBlipProcessor

						try:
												if not is_torch_available():
																		raise OptionalDependencyNotAvailable()
						except OptionalDependencyNotAvailable:
												pass
						else:
												from .modeling_instructblip import (
												    INSTRUCTBLIP_PRETRAINED_MODEL_ARCHIVE_LIST,
												    InstructBlipForConditionalGeneration,
												    InstructBlipPreTrainedModel,
												    InstructBlipQFormerModel,
												    InstructBlipVisionModel,
												)

else:
						import sys

						lowerCamelCase__    	=     _LazyModule(__name__, globals()['__file__'], _import_structure, module_spec=__spec__)



