
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_  :	Tuple 				=					logging.get_logger(__name__)

# General docstring
lowerCamelCase_  :	Union[str, Any] 				=					"""PoolFormerConfig"""

# Base docstring
lowerCamelCase_  :	List[Any] 				=					"""sail/poolformer_s12"""
lowerCamelCase_  :	Any 				=					[1, 5_12, 7, 7]

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

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







def     A__   (  lowerCamelCase       ,      lowerCamelCase = 0.0       ,      lowerCamelCase = False  )					->		List[Any]:
				if drop_prob == 0.0 or not training:
								return input
				UpperCamelCase_:							Dict		=  1 - drop_prob
				UpperCamelCase_:							Tuple		=  (input.shape[0],) + (1,) * (input.ndim - 1)  # work with diff dim tensors, not just 2D ConvNets
				UpperCamelCase_:							Optional[int]		=  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     _UpperCamelCase			(   nn.Module					):






			'''simple docstring'''

			def __init__(    self	:    Any  ,		snake_case_	:    Optional[float] = None ):
							super().__init__()
							UpperCamelCase_:							List[str]		=  drop_prob

			def  lowerCAmelCase__		(    self	:    List[str]  ,		snake_case_	:    torch.Tensor ):
							return drop_path(snake_case_  ,		self.drop_prob  ,		self.training )




			def  lowerCAmelCase__		(    self	:    List[str] ):
							return "p={}".format(self.drop_prob )




class     _UpperCamelCase			(   nn.Module					):






			'''simple docstring'''

			def __init__(    self	:    str  ,		snake_case_	:    Tuple  ,		snake_case_	:    Optional[int]  ,		snake_case_	:    Any  ,		snake_case_	:    Optional[Any]  ,		snake_case_	:    Tuple  ,		snake_case_	:    Union[str, Any]=None ):
							super().__init__()
							UpperCamelCase_:							List[Any]		=  patch_size if isinstance(snake_case_  ,		collections.abc.Iterable ) else (patch_size, patch_size)
							UpperCamelCase_:							Optional[Any]		=  stride if isinstance(snake_case_  ,		collections.abc.Iterable ) else (stride, stride)
							UpperCamelCase_:							List[str]		=  padding if isinstance(snake_case_  ,		collections.abc.Iterable ) else (padding, padding)

							UpperCamelCase_:							int		=  nn.Convad(snake_case_  ,		snake_case_  ,		kernel_size=snake_case_  ,		stride=snake_case_  ,		padding=snake_case_ )
							UpperCamelCase_:							Optional[Any]		=  norm_layer(snake_case_ ) if norm_layer else nn.Identity()




			def  lowerCAmelCase__		(    self	:    Tuple  ,		snake_case_	:    Optional[Any] ):
							UpperCamelCase_:							Union[str, Any]		=  self.projection(snake_case_ )
							UpperCamelCase_:							Any		=  self.norm(snake_case_ )
							return embeddings




class     _UpperCamelCase			(   nn.GroupNorm					):






			'''simple docstring'''

			def __init__(    self	:    Dict  ,		snake_case_	:    Optional[int]  ,		**snake_case_	:    Optional[Any] ):
							super().__init__(1  ,		snake_case_  ,		**snake_case_ )




class     _UpperCamelCase			(   nn.Module					):






			'''simple docstring'''

			def __init__(    self	:    Dict  ,		snake_case_	:    Dict ):
							super().__init__()
							UpperCamelCase_:							List[Any]		=  nn.AvgPoolad(snake_case_  ,		stride=1  ,		padding=pool_size // 2  ,		count_include_pad=snake_case_ )




			def  lowerCAmelCase__		(    self	:    Optional[int]  ,		snake_case_	:    List[str] ):
							return self.pool(snake_case_ ) - hidden_states




class     _UpperCamelCase			(   nn.Module					):






			'''simple docstring'''

			def __init__(    self	:    str  ,		snake_case_	:    Optional[Any]  ,		snake_case_	:    List[str]  ,		snake_case_	:    Tuple  ,		snake_case_	:    Tuple ):
							super().__init__()
							UpperCamelCase_:							Union[str, Any]		=  nn.Convad(snake_case_  ,		snake_case_  ,		1 )
							UpperCamelCase_:							Optional[int]		=  nn.Convad(snake_case_  ,		snake_case_  ,		1 )
							UpperCamelCase_:							Tuple		=  PoolFormerDropPath(snake_case_ )
							if isinstance(config.hidden_act  ,		snake_case_ ):
											UpperCamelCase_:							str		=  ACTaFN[config.hidden_act]
							else:
											UpperCamelCase_:							Optional[Any]		=  config.hidden_act




			def  lowerCAmelCase__		(    self	:    int  ,		snake_case_	:    Optional[Any] ):
							UpperCamelCase_:							Dict		=  self.conva(snake_case_ )
							UpperCamelCase_:							Optional[int]		=  self.act_fn(snake_case_ )
							UpperCamelCase_:							Optional[int]		=  self.drop(snake_case_ )
							UpperCamelCase_:							Tuple		=  self.conva(snake_case_ )
							UpperCamelCase_:							Any		=  self.drop(snake_case_ )

							return hidden_states




class     _UpperCamelCase			(   nn.Module					):






			'''simple docstring'''

			def __init__(    self	:    int  ,		snake_case_	:    List[Any]  ,		snake_case_	:    Dict  ,		snake_case_	:    str  ,		snake_case_	:    List[str]  ,		snake_case_	:    Union[str, Any]  ,		snake_case_	:    Tuple ):
							super().__init__()
							UpperCamelCase_:							List[str]		=  PoolFormerPooling(snake_case_ )
							UpperCamelCase_:							int		=  PoolFormerOutput(snake_case_  ,		snake_case_  ,		snake_case_  ,		snake_case_ )
							UpperCamelCase_:							List[str]		=  PoolFormerGroupNorm(snake_case_ )
							UpperCamelCase_:							Dict		=  PoolFormerGroupNorm(snake_case_ )

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




			def  lowerCAmelCase__		(    self	:    List[Any]  ,		snake_case_	:    Any ):
							if self.use_layer_scale:
											UpperCamelCase_:							Optional[Any]		=  self.pooling(self.before_norm(snake_case_ ) )
											UpperCamelCase_:							str		=  self.layer_scale_a.unsqueeze(-1 ).unsqueeze(-1 ) * pooling_output
											# First residual connection
											UpperCamelCase_:							Dict		=  hidden_states + self.drop_path(snake_case_ )
											UpperCamelCase_:							List[str]		=  ()

											UpperCamelCase_:							List[str]		=  self.output(self.after_norm(snake_case_ ) )
											UpperCamelCase_:							Optional[Any]		=  self.layer_scale_a.unsqueeze(-1 ).unsqueeze(-1 ) * layer_output
											# Second residual connection
											UpperCamelCase_:							Union[str, Any]		=  hidden_states + self.drop_path(snake_case_ )

											UpperCamelCase_:							Tuple		=  (output,) + outputs
											return outputs

							else:
											UpperCamelCase_:							str		=  self.drop_path(self.pooling(self.before_norm(snake_case_ ) ) )
											# First residual connection
											UpperCamelCase_:							Dict		=  pooling_output + hidden_states
											UpperCamelCase_:							Union[str, Any]		=  ()

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

											UpperCamelCase_:							Union[str, Any]		=  (output,) + outputs
											return outputs




class     _UpperCamelCase			(   nn.Module					):






			'''simple docstring'''

			def __init__(    self	:    Union[str, Any]  ,		snake_case_	:    List[str] ):
							super().__init__()
							UpperCamelCase_:							int		=  config
							# stochastic depth decay rule
							UpperCamelCase_:							int		=  [x.item() for x in torch.linspace(0  ,		config.drop_path_rate  ,		sum(config.depths ) )]

							# patch embeddings
							UpperCamelCase_:							Union[str, 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_:							Union[str, Any]		=  nn.ModuleList(snake_case_ )

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

							UpperCamelCase_:							Any		=  nn.ModuleList(snake_case_ )




			def  lowerCAmelCase__		(    self	:    Dict  ,		snake_case_	:    Dict  ,		snake_case_	:    Optional[Any]=False  ,		snake_case_	:    Optional[Any]=True ):
							UpperCamelCase_:							Dict		=  () if output_hidden_states else None

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

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




class     _UpperCamelCase			(   _A					):






			'''simple docstring'''

			__UpperCamelCase :  int    =	PoolFormerConfig
			__UpperCamelCase :  Any    =	"""poolformer"""
			__UpperCamelCase :  Optional[Any]    =	"""pixel_values"""
			__UpperCamelCase :  List[str]    =	True

			def  lowerCAmelCase__		(    self	:    Optional[int]  ,		snake_case_	:    Tuple ):
							if isinstance(snake_case_  ,		(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(snake_case_  ,		nn.LayerNorm ):
											module.bias.data.zero_()
											module.weight.data.fill_(1.0 )




			def  lowerCAmelCase__		(    self	:    Dict  ,		snake_case_	:    Optional[int]  ,		snake_case_	:    Dict=False ):
							if isinstance(snake_case_  ,		snake_case_ ):
											UpperCamelCase_:							Any		=  value




lowerCamelCase_  :	List[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.
"""

lowerCamelCase_  :	Tuple 				=					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."""       ,    _A       ,    )
class     _UpperCamelCase			(   _A					):






			'''simple docstring'''

			def __init__(    self	:    int  ,		snake_case_	:    Tuple ):
							super().__init__(snake_case_ )
							UpperCamelCase_:							Dict		=  config

							UpperCamelCase_:							Optional[Any]		=  PoolFormerEncoder(snake_case_ )

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

			def  lowerCAmelCase__		(    self	:    str ):
							return self.embeddings.patch_embeddings




			@add_start_docstrings_to_model_forward(snake_case_ )
			@add_code_sample_docstrings(
			    checkpoint=_CHECKPOINT_FOR_DOC  ,		output_type=snake_case_  ,		config_class=_CONFIG_FOR_DOC  ,		modality="""vision"""  ,		expected_output=_EXPECTED_OUTPUT_SHAPE  ,		)
			def  lowerCAmelCase__		(    self	:    Union[str, Any]  ,		snake_case_	:    Optional[torch.FloatTensor] = None  ,		snake_case_	:    Optional[bool] = None  ,		snake_case_	:    Optional[bool] = None  ,		):
							UpperCamelCase_:							Union[str, Any]		=  (
							    output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states
							)
							UpperCamelCase_:							List[Any]		=  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_:							Any		=  self.encoder(
							    snake_case_  ,		output_hidden_states=snake_case_  ,		return_dict=snake_case_  ,		)
							UpperCamelCase_:							List[Any]		=  encoder_outputs[0]

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

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




class     _UpperCamelCase			(   nn.Module					):






			'''simple docstring'''

			def __init__(    self	:    int  ,		snake_case_	:    Any ):
							super().__init__()
							UpperCamelCase_:							str		=  nn.Linear(config.hidden_size  ,		config.hidden_size )




			def  lowerCAmelCase__		(    self	:    str  ,		snake_case_	:    List[Any] ):
							UpperCamelCase_:							List[str]		=  self.dense(snake_case_ )
							return output




@add_start_docstrings(
    """
    PoolFormer Model transformer with an image classification head on top
    """       ,    _A       ,    )
class     _UpperCamelCase			(   _A					):






			'''simple docstring'''

			def __init__(    self	:    Optional[int]  ,		snake_case_	:    Any ):
							super().__init__(snake_case_ )
							UpperCamelCase_:							Optional[Any]		=  config.num_labels
							UpperCamelCase_:							Optional[int]		=  PoolFormerModel(snake_case_ )

							# Final norm
							UpperCamelCase_:							Union[str, Any]		=  PoolFormerGroupNorm(config.hidden_sizes[-1] )
							# Classifier head
							UpperCamelCase_:							Dict		=  (
							    nn.Linear(config.hidden_sizes[-1]  ,		config.num_labels ) if config.num_labels > 0 else nn.Identity()
							)

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




			@add_start_docstrings_to_model_forward(snake_case_ )
			@add_code_sample_docstrings(
			    checkpoint=_IMAGE_CLASS_CHECKPOINT  ,		output_type=snake_case_  ,		config_class=_CONFIG_FOR_DOC  ,		expected_output=_IMAGE_CLASS_EXPECTED_OUTPUT  ,		)
			def  lowerCAmelCase__		(    self	:    List[Any]  ,		snake_case_	:    Optional[torch.FloatTensor] = None  ,		snake_case_	:    Optional[torch.LongTensor] = None  ,		snake_case_	:    Optional[bool] = None  ,		snake_case_	:    Optional[bool] = None  ,		):
							UpperCamelCase_:							Union[str, Any]		=  return_dict if return_dict is not None else self.config.use_return_dict

							UpperCamelCase_:							Tuple		=  self.poolformer(
							    snake_case_  ,		output_hidden_states=snake_case_  ,		return_dict=snake_case_  ,		)

							UpperCamelCase_:							Union[str, Any]		=  outputs[0]

							UpperCamelCase_:							Optional[Any]		=  self.classifier(self.norm(snake_case_ ).mean([-2, -1] ) )

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

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

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

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








import argparse

import torch

from transformers import GPTaConfig, GPTaModel, load_tf_weights_in_gpta
from transformers.utils import CONFIG_NAME, WEIGHTS_NAME, logging


logging.set_verbosity_info()







def     A__   (  lowerCamelCase       ,      lowerCamelCase       ,      lowerCamelCase  )					->		Union[str, Any]:
				# Construct model
				if gpta_config_file == "":
								UpperCamelCase_:							Union[str, Any]		=  GPTaConfig()
				else:
								UpperCamelCase_:							Optional[Any]		=  GPTaConfig.from_json_file(lowerCamelCase  )
				UpperCamelCase_:							List[Any]		=  GPTaModel(lowerCamelCase  )

				# Load weights from numpy
				load_tf_weights_in_gpta(lowerCamelCase       ,      lowerCamelCase       ,      lowerCamelCase  )

				# Save pytorch-model
				UpperCamelCase_:							int		=  pytorch_dump_folder_path + """/""" + WEIGHTS_NAME
				UpperCamelCase_:							Any		=  pytorch_dump_folder_path + """/""" + CONFIG_NAME
				print(F'''Save PyTorch model to {pytorch_weights_dump_path}'''  )
				torch.save(model.state_dict()       ,      lowerCamelCase  )
				print(F'''Save configuration file to {pytorch_config_dump_path}'''  )
				with open(lowerCamelCase       ,      """w"""       ,      encoding="""utf-8"""  ) as f:
								f.write(config.to_json_string()  )


if __name__ == "__main__":
							lowerCamelCase_  :	int 				=					argparse.ArgumentParser()
							# Required parameters
							parser.add_argument(
							    """--gpt2_checkpoint_path""", default=None, type=str, required=True, help="""Path to the TensorFlow checkpoint path."""
							)
							parser.add_argument(
							    """--pytorch_dump_folder_path""", default=None, type=str, required=True, help="""Path to the output PyTorch model."""
							)
							parser.add_argument(
							    """--gpt2_config_file""",
							    default="""""",
							    type=str,
							    help=(
							        """An optional config json file corresponding to the pre-trained OpenAI model. \n"""
							        """This specifies the model architecture."""
							    ),
							)
							lowerCamelCase_  :	List[str] 				=					parser.parse_args()
							convert_gpta_checkpoint_to_pytorch(args.gpta_checkpoint_path, args.gpta_config_file, args.pytorch_dump_folder_path)






