




import logging
import os
from logging import (
    CRITICAL,  # NOQA
    DEBUG,  # NOQA
    ERROR,  # NOQA
    FATAL,  # NOQA
    INFO,  # NOQA
    NOTSET,  # NOQA
    WARN,  # NOQA
    WARNING,  # NOQA
)
from typing import Optional

from tqdm import auto as tqdm_lib


lowercase__							:	Union[str, Any]											=		{
    "debug": logging.DEBUG,
    "info": logging.INFO,
    "warning": logging.WARNING,
    "error": logging.ERROR,
    "critical": logging.CRITICAL,
}

lowercase__							:	Dict											=		logging.WARNING


def 		lowerCamelCase__       (						):







	'''simple docstring'''





	snake_case_						       =	os.getenv("DATASETS_VERBOSITY"	,      _A				)
	if env_level_str:
		if env_level_str in log_levels:
			return log_levels[env_level_str]
		else:
			logging.getLogger().warning(
			    f"Unknown option DATASETS_VERBOSITY={env_level_str}, "
			    f"has to be one of: { ', '.join(log_levels.keys()				) }"				)
	return _default_log_level


def 		lowerCamelCase__       (						):







	'''simple docstring'''





	return __name__.split("."				)[0]


def 		lowerCamelCase__       (						):







	'''simple docstring'''





	return logging.getLogger(_get_library_name()				)


def 		lowerCamelCase__       (						):







	'''simple docstring'''





	snake_case_						       =	_get_library_root_logger()
	library_root_logger.setLevel(_get_default_logging_level()				)


def 		lowerCamelCase__       (						):







	'''simple docstring'''





	snake_case_						       =	_get_library_root_logger()
	library_root_logger.setLevel(logging.NOTSET				)


def 		lowerCamelCase__       (						_A = None				):







	'''simple docstring'''





	if name is None:
		snake_case_						       =	_get_library_name()
	return logging.getLogger(_A				)


def 		lowerCamelCase__       (						):







	'''simple docstring'''





	return _get_library_root_logger().getEffectiveLevel()


def 		lowerCamelCase__       (						_A				):







	'''simple docstring'''





	_get_library_root_logger().setLevel(_A				)


def 		lowerCamelCase__       (						):







	'''simple docstring'''





	return set_verbosity(_A				)


def 		lowerCamelCase__       (						):







	'''simple docstring'''





	return set_verbosity(_A				)


def 		lowerCamelCase__       (						):







	'''simple docstring'''





	return set_verbosity(_A				)


def 		lowerCamelCase__       (						):







	'''simple docstring'''





	return set_verbosity(_A				)


def 		lowerCamelCase__       (						):







	'''simple docstring'''





	snake_case_						       =	False


def 		lowerCamelCase__       (						):







	'''simple docstring'''





	snake_case_						       =	True


# Configure the library root logger at the module level (singleton-like)
_configure_library_root_logger()
class 						UpperCAmelCase  :







						'''simple docstring'''





						def __init__(		self				:	List[str]			,      *__lowercase				:	Optional[Any]			,      **__lowercase				:	List[Any]		):  # pylint: disable=unused-argument






							"""simple docstring"""






							snake_case_						       =	args[0] if args else None






						def __iter__(		self				:	int		):






							"""simple docstring"""






							return iter(self._iterator		)






						def __getattr__(		self				:	Tuple			,      __lowercase				:	str		):






							"""simple docstring"""






							def empty_fn(*__lowercase				:	List[str]			,      **__lowercase				:	Optional[Any]		):  # pylint: disable=unused-argument
								return

							return empty_fn






						def __enter__(		self				:	Dict		):






							"""simple docstring"""






							return self






						def __exit__(		self				:	Tuple			,      __lowercase				:	Dict			,      __lowercase				:	Any			,      __lowercase				:	int		):






							"""simple docstring"""






							return







lowercase__							:	Optional[int]											=		True
class 						UpperCAmelCase  :







						'''simple docstring'''





						def __call__(		self				:	Dict			,      *__lowercase				:	Optional[Any]			,      __lowercase				:	Dict=False			,      **__lowercase				:	List[Any]		):






							"""simple docstring"""






							if _tqdm_active and not disable:
								return tqdm_lib.tqdm(*__lowercase			,      **__lowercase		)
							else:
								return EmptyTqdm(*__lowercase			,      **__lowercase		)






						def 			snake_case__		(		self				:	Union[str, Any]			,      *__lowercase				:	Dict			,      **__lowercase				:	int		):






							"""simple docstring"""






							snake_case_						       =	None
							if _tqdm_active:
								return tqdm_lib.tqdm.set_lock(*__lowercase			,      **__lowercase		)






						def 			snake_case__		(		self				:	Any		):






							"""simple docstring"""






							if _tqdm_active:
								return tqdm_lib.tqdm.get_lock()







lowercase__							:	str											=		_tqdm_cls()


def 		lowerCamelCase__       (						):







	'''simple docstring'''





	global _tqdm_active
	return bool(_tqdm_active				)


def 		lowerCamelCase__       (						):







	'''simple docstring'''





	global _tqdm_active
	snake_case_						       =	True






def 		lowerCamelCase__       (						):







	'''simple docstring'''





	global _tqdm_active
	snake_case_						       =	False






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


lowercase__							:	Optional[Any]											=		logging.get_logger(__name__)

# General docstring
lowercase__							:	Optional[int]											=		"PoolFormerConfig"

# Base docstring
lowercase__							:	Optional[Any]											=		"sail/poolformer_s12"
lowercase__							:	Union[str, Any]											=		[1, 512, 7, 7]

# Image classification docstring
lowercase__							:	List[str]											=		"sail/poolformer_s12"
lowercase__							:	Dict											=		"tabby, tabby cat"

lowercase__							:	Union[str, Any]											=		[
    "sail/poolformer_s12",
    # See all PoolFormer models at https://huggingface.co/models?filter=poolformer
]


def 		lowerCamelCase__       (						_A	,      _A = 0.0	,      _A = False				):







	'''simple docstring'''





	if drop_prob == 0.0 or not training:
		return input
	snake_case_						       =	1 - drop_prob
	snake_case_						       =	(input.shape[0],) + (1,) * (input.ndim - 1)  # work with diff dim tensors, not just 2D ConvNets
	snake_case_						       =	keep_prob + torch.rand(_A	,      dtype=input.dtype	,      device=input.device				)
	random_tensor.floor_()  # binarize
	snake_case_						       =	input.div(_A				) * random_tensor
	return output
class 						UpperCAmelCase  (							nn.Module     ):







						'''simple docstring'''





						def __init__(		self				:	Optional[Any]			,      __lowercase				:	Optional[float] = None		):






							"""simple docstring"""






							super().__init__()
							snake_case_						       =	drop_prob






						def 			snake_case__		(		self				:	List[str]			,      __lowercase				:	torch.Tensor		):






							"""simple docstring"""






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






						def 			snake_case__		(		self				:	Any		):






							"""simple docstring"""






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







class 						UpperCAmelCase  (							nn.Module     ):







						'''simple docstring'''





						def __init__(		self				:	Optional[Any]			,      __lowercase				:	Dict			,      __lowercase				:	Tuple			,      __lowercase				:	Optional[Any]			,      __lowercase				:	int			,      __lowercase				:	Union[str, Any]			,      __lowercase				:	Dict=None		):






							"""simple docstring"""






							super().__init__()
							snake_case_						       =	patch_size if isinstance(__lowercase			,      collections.abc.Iterable		) else (patch_size, patch_size)
							snake_case_						       =	stride if isinstance(__lowercase			,      collections.abc.Iterable		) else (stride, stride)
							snake_case_						       =	padding if isinstance(__lowercase			,      collections.abc.Iterable		) else (padding, padding)

							snake_case_						       =	nn.Convad(__lowercase			,      __lowercase			,      kernel_size=__lowercase			,      stride=__lowercase			,      padding=__lowercase		)
							snake_case_						       =	norm_layer(__lowercase		) if norm_layer else nn.Identity()






						def 			snake_case__		(		self				:	Any			,      __lowercase				:	Optional[Any]		):






							"""simple docstring"""






							snake_case_						       =	self.projection(__lowercase		)
							snake_case_						       =	self.norm(__lowercase		)
							return embeddings







class 						UpperCAmelCase  (							nn.GroupNorm     ):







						'''simple docstring'''





						def __init__(		self				:	int			,      __lowercase				:	List[str]			,      **__lowercase				:	Union[str, Any]		):






							"""simple docstring"""






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







class 						UpperCAmelCase  (							nn.Module     ):







						'''simple docstring'''





						def __init__(		self				:	Dict			,      __lowercase				:	List[str]		):






							"""simple docstring"""






							super().__init__()
							snake_case_						       =	nn.AvgPoolad(__lowercase			,      stride=1			,      padding=pool_size // 2			,      count_include_pad=__lowercase		)






						def 			snake_case__		(		self				:	int			,      __lowercase				:	Tuple		):






							"""simple docstring"""






							return self.pool(__lowercase		) - hidden_states







class 						UpperCAmelCase  (							nn.Module     ):







						'''simple docstring'''





						def __init__(		self				:	str			,      __lowercase				:	List[Any]			,      __lowercase				:	str			,      __lowercase				:	Optional[Any]			,      __lowercase				:	Optional[Any]		):






							"""simple docstring"""






							super().__init__()
							snake_case_						       =	nn.Convad(__lowercase			,      __lowercase			,      1		)
							snake_case_						       =	nn.Convad(__lowercase			,      __lowercase			,      1		)
							snake_case_						       =	PoolFormerDropPath(__lowercase		)
							if isinstance(config.hidden_act			,      __lowercase		):
								snake_case_						       =	ACTaFN[config.hidden_act]
							else:
								snake_case_						       =	config.hidden_act






						def 			snake_case__		(		self				:	Union[str, Any]			,      __lowercase				:	int		):






							"""simple docstring"""






							snake_case_						       =	self.conva(__lowercase		)
							snake_case_						       =	self.act_fn(__lowercase		)
							snake_case_						       =	self.drop(__lowercase		)
							snake_case_						       =	self.conva(__lowercase		)
							snake_case_						       =	self.drop(__lowercase		)

							return hidden_states







class 						UpperCAmelCase  (							nn.Module     ):







						'''simple docstring'''





						def __init__(		self				:	int			,      __lowercase				:	List[str]			,      __lowercase				:	Optional[Any]			,      __lowercase				:	str			,      __lowercase				:	Optional[Any]			,      __lowercase				:	List[str]			,      __lowercase				:	int		):






							"""simple docstring"""






							super().__init__()
							snake_case_						       =	PoolFormerPooling(__lowercase		)
							snake_case_						       =	PoolFormerOutput(__lowercase			,      __lowercase			,      __lowercase			,      __lowercase		)
							snake_case_						       =	PoolFormerGroupNorm(__lowercase		)
							snake_case_						       =	PoolFormerGroupNorm(__lowercase		)

							# Useful for training neural nets
							snake_case_						       =	PoolFormerDropPath(__lowercase		) if drop_path > 0.0 else nn.Identity()
							snake_case_						       =	config.use_layer_scale
							if config.use_layer_scale:
								snake_case_						       =	nn.Parameter(
								    config.layer_scale_init_value * torch.ones((__lowercase)		)			,      requires_grad=__lowercase		)
								snake_case_						       =	nn.Parameter(
								    config.layer_scale_init_value * torch.ones((__lowercase)		)			,      requires_grad=__lowercase		)






						def 			snake_case__		(		self				:	Union[str, Any]			,      __lowercase				:	Union[str, Any]		):






							"""simple docstring"""






							if self.use_layer_scale:
								snake_case_						       =	self.pooling(self.before_norm(__lowercase		)		)
								snake_case_						       =	self.layer_scale_a.unsqueeze(-1		).unsqueeze(-1		) * pooling_output
								# First residual connection
								snake_case_						       =	hidden_states + self.drop_path(__lowercase		)
								snake_case_						       =	()

								snake_case_						       =	self.output(self.after_norm(__lowercase		)		)
								snake_case_						       =	self.layer_scale_a.unsqueeze(-1		).unsqueeze(-1		) * layer_output
								# Second residual connection
								snake_case_						       =	hidden_states + self.drop_path(__lowercase		)

								snake_case_						       =	(output,) + outputs
								return outputs

							else:
								snake_case_						       =	self.drop_path(self.pooling(self.before_norm(__lowercase		)		)		)
								# First residual connection
								snake_case_						       =	pooling_output + hidden_states
								snake_case_						       =	()

								# Second residual connection inside the PoolFormerOutput block
								snake_case_						       =	self.drop_path(self.output(self.after_norm(__lowercase		)		)		)
								snake_case_						       =	hidden_states + layer_output

								snake_case_						       =	(output,) + outputs
								return outputs







class 						UpperCAmelCase  (							nn.Module     ):







						'''simple docstring'''





						def __init__(		self				:	List[Any]			,      __lowercase				:	Optional[Any]		):






							"""simple docstring"""






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

							# patch embeddings
							snake_case_						       =	[]
							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]			,      )		)
							snake_case_						       =	nn.ModuleList(__lowercase		)

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

							snake_case_						       =	nn.ModuleList(__lowercase		)






						def 			snake_case__		(		self				:	List[str]			,      __lowercase				:	List[Any]			,      __lowercase				:	int=False			,      __lowercase				:	Tuple=True		):






							"""simple docstring"""






							snake_case_						       =	() if output_hidden_states else None

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

								if output_hidden_states:
									snake_case_						       =	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=__lowercase			,      hidden_states=__lowercase		)







class 						UpperCAmelCase  (							UpperCAmelCase__     ):







						'''simple docstring'''





						lowerCAmelCase_							=  PoolFormerConfig
						lowerCAmelCase_							=  '''poolformer'''
						lowerCAmelCase_							=  '''pixel_values'''
						lowerCAmelCase_							=  True






						def 			snake_case__		(		self				:	Dict			,      __lowercase				:	Optional[Any]		):






							"""simple docstring"""






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






						def 			snake_case__		(		self				:	str			,      __lowercase				:	Any			,      __lowercase				:	Tuple=False		):






							"""simple docstring"""






							if isinstance(__lowercase			,      __lowercase		):
								snake_case_						       =	value







lowercase__							:	Optional[int]											=		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"

lowercase__							:	List[str]											=		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 						UpperCAmelCase  (							UpperCAmelCase__     ):







						'''simple docstring'''





						def __init__(		self				:	Any			,      __lowercase				:	Any		):






							"""simple docstring"""






							super().__init__(__lowercase		)
							snake_case_						       =	config

							snake_case_						       =	PoolFormerEncoder(__lowercase		)

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






						def 			snake_case__		(		self				:	List[Any]		):






							"""simple docstring"""






							return self.embeddings.patch_embeddings






						@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 			snake_case__		(		self				:	Optional[Any]			,      __lowercase				:	Optional[torch.FloatTensor] = None			,      __lowercase				:	Optional[bool] = None			,      __lowercase				:	Optional[bool] = None			,      ):






							"""simple docstring"""






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

							snake_case_						       =	self.encoder(
							    __lowercase			,      output_hidden_states=__lowercase			,      return_dict=__lowercase			,      )
							snake_case_						       =	encoder_outputs[0]

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

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







class 						UpperCAmelCase  (							nn.Module     ):







						'''simple docstring'''





						def __init__(		self				:	Dict			,      __lowercase				:	Tuple		):






							"""simple docstring"""






							super().__init__()
							snake_case_						       =	nn.Linear(config.hidden_size			,      config.hidden_size		)






						def 			snake_case__		(		self				:	Dict			,      __lowercase				:	List[str]		):






							"""simple docstring"""






							snake_case_						       =	self.dense(__lowercase		)
							return output







@add_start_docstrings(
    '''
    PoolFormer Model transformer with an image classification head on top
    '''			,  UpperCAmelCase__			,  )
class 						UpperCAmelCase  (							UpperCAmelCase__     ):







						'''simple docstring'''





						def __init__(		self				:	Optional[int]			,      __lowercase				:	int		):






							"""simple docstring"""






							super().__init__(__lowercase		)
							snake_case_						       =	config.num_labels
							snake_case_						       =	PoolFormerModel(__lowercase		)

							# Final norm
							snake_case_						       =	PoolFormerGroupNorm(config.hidden_sizes[-1]		)
							# Classifier head
							snake_case_						       =	(
							    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 			snake_case__		(		self				:	str			,      __lowercase				:	Optional[torch.FloatTensor] = None			,      __lowercase				:	Optional[torch.LongTensor] = None			,      __lowercase				:	Optional[bool] = None			,      __lowercase				:	Optional[bool] = None			,      ):






							"""simple docstring"""






							snake_case_						       =	return_dict if return_dict is not None else self.config.use_return_dict

							snake_case_						       =	self.poolformer(
							    __lowercase			,      output_hidden_states=__lowercase			,      return_dict=__lowercase			,      )

							snake_case_						       =	outputs[0]

							snake_case_						       =	self.classifier(self.norm(__lowercase		).mean([-2, -1]		)		)

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

								if self.config.problem_type == "regression":
									snake_case_						       =	MSELoss()
									if self.num_labels == 1:
										snake_case_						       =	loss_fct(logits.squeeze()			,      labels.squeeze()		)
									else:
										snake_case_						       =	loss_fct(__lowercase			,      __lowercase		)
								elif self.config.problem_type == "single_label_classification":
									snake_case_						       =	CrossEntropyLoss()
									snake_case_						       =	loss_fct(logits.view(-1			,      self.num_labels		)			,      labels.view(-1		)		)
								elif self.config.problem_type == "multi_label_classification":
									snake_case_						       =	BCEWithLogitsLoss()
									snake_case_						       =	loss_fct(__lowercase			,      __lowercase		)

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

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