





def 							__snake_case							(    lowerCAmelCase_	)				->						int:

    if not grid or not grid[0]:
        raise TypeError('''The grid does not contain the appropriate information'''	)

    for cell_n in range(1 ,							len(grid[0]	)	):
        grid[0][cell_n] += grid[0][cell_n - 1]
    SCREAMING_SNAKE_CASE__											=			grid[0]

    for row_n in range(1 ,							len(lowerCAmelCase_	)	):
        SCREAMING_SNAKE_CASE__											=			grid[row_n]
        SCREAMING_SNAKE_CASE__											=			fill_row(lowerCAmelCase_ ,							lowerCAmelCase_	)
        SCREAMING_SNAKE_CASE__											=			grid[row_n]

    return grid[-1][-1]


def 							__snake_case							(    lowerCAmelCase_ ,							lowerCAmelCase_	)				->						list:
    current_row[0] += row_above[0]
    for cell_n in range(1 ,							len(lowerCAmelCase_	)	):
        current_row[cell_n] += min(current_row[cell_n - 1] ,							row_above[cell_n]	)

    return current_row


if __name__ == "__main__":
  import doctest

  doctest.testmod()







'''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__					:			Dict          =     logging.get_logger(__name__)

# General docstring
a__					:			Any          =     '''PoolFormerConfig'''

# Base docstring
a__					:			List[str]          =     '''sail/poolformer_s12'''
a__					:			Tuple          =     [1, 512, 7, 7]

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

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




def 						__lowerCamelCase			(	UpperCAmelCase_					,				UpperCAmelCase_ = 0.0					,				UpperCAmelCase_ = False							)   ->Optional[int]:
	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(UpperCAmelCase_					,				dtype=input.dtype					,				device=input.device							)
	random_tensor.floor_()  # binarize
	snake_case__	       =   input.div(UpperCAmelCase_							) * random_tensor
	return output






class 				__snake_case     (					nn.Module							):





	def __init__(		self      ,       UpperCamelCase_ = None		)      ->		None:
		super().__init__()
		snake_case__	       =   drop_prob





	def   _snake_case   (		self      ,       UpperCamelCase_		)      ->		torch.Tensor:
		return drop_path(UpperCamelCase_      ,       self.drop_prob      ,       self.training		)





	def   _snake_case   (		self		)      ->		str:
		return "p={}".format(self.drop_prob		)






class 				__snake_case     (					nn.Module							):





	def __init__(		self      ,       UpperCamelCase_      ,       UpperCamelCase_      ,       UpperCamelCase_      ,       UpperCamelCase_      ,       UpperCamelCase_      ,       UpperCamelCase_=None		)      ->		Optional[Any]:
		super().__init__()
		snake_case__	       =   patch_size if isinstance(UpperCamelCase_      ,       collections.abc.Iterable		) else (patch_size, patch_size)
		snake_case__	       =   stride if isinstance(UpperCamelCase_      ,       collections.abc.Iterable		) else (stride, stride)
		snake_case__	       =   padding if isinstance(UpperCamelCase_      ,       collections.abc.Iterable		) else (padding, padding)

		snake_case__	       =   nn.Convad(UpperCamelCase_      ,       UpperCamelCase_      ,       kernel_size=UpperCamelCase_      ,       stride=UpperCamelCase_      ,       padding=UpperCamelCase_		)
		snake_case__	       =   norm_layer(UpperCamelCase_		) if norm_layer else nn.Identity()





	def   _snake_case   (		self      ,       UpperCamelCase_		)      ->		int:
		snake_case__	       =   self.projection(UpperCamelCase_		)
		snake_case__	       =   self.norm(UpperCamelCase_		)
		return embeddings






class 				__snake_case     (					nn.GroupNorm							):





	def __init__(		self      ,       UpperCamelCase_      ,       **UpperCamelCase_		)      ->		Dict:
		super().__init__(1      ,       UpperCamelCase_      ,       **UpperCamelCase_		)






class 				__snake_case     (					nn.Module							):





	def __init__(		self      ,       UpperCamelCase_		)      ->		int:
		super().__init__()
		snake_case__	       =   nn.AvgPoolad(UpperCamelCase_      ,       stride=1      ,       padding=pool_size // 2      ,       count_include_pad=UpperCamelCase_		)





	def   _snake_case   (		self      ,       UpperCamelCase_		)      ->		str:
		return self.pool(UpperCamelCase_		) - hidden_states






class 				__snake_case     (					nn.Module							):





	def __init__(		self      ,       UpperCamelCase_      ,       UpperCamelCase_      ,       UpperCamelCase_      ,       UpperCamelCase_		)      ->		int:
		super().__init__()
		snake_case__	       =   nn.Convad(UpperCamelCase_      ,       UpperCamelCase_      ,       1		)
		snake_case__	       =   nn.Convad(UpperCamelCase_      ,       UpperCamelCase_      ,       1		)
		snake_case__	       =   PoolFormerDropPath(UpperCamelCase_		)
		if isinstance(config.hidden_act      ,       UpperCamelCase_		):
			snake_case__	       =   ACTaFN[config.hidden_act]
		else:
			snake_case__	       =   config.hidden_act





	def   _snake_case   (		self      ,       UpperCamelCase_		)      ->		Any:
		snake_case__	       =   self.conva(UpperCamelCase_		)
		snake_case__	       =   self.act_fn(UpperCamelCase_		)
		snake_case__	       =   self.drop(UpperCamelCase_		)
		snake_case__	       =   self.conva(UpperCamelCase_		)
		snake_case__	       =   self.drop(UpperCamelCase_		)

		return hidden_states






class 				__snake_case     (					nn.Module							):





	def __init__(		self      ,       UpperCamelCase_      ,       UpperCamelCase_      ,       UpperCamelCase_      ,       UpperCamelCase_      ,       UpperCamelCase_      ,       UpperCamelCase_		)      ->		Tuple:
		super().__init__()
		snake_case__	       =   PoolFormerPooling(UpperCamelCase_		)
		snake_case__	       =   PoolFormerOutput(UpperCamelCase_      ,       UpperCamelCase_      ,       UpperCamelCase_      ,       UpperCamelCase_		)
		snake_case__	       =   PoolFormerGroupNorm(UpperCamelCase_		)
		snake_case__	       =   PoolFormerGroupNorm(UpperCamelCase_		)

		# Useful for training neural nets
		snake_case__	       =   PoolFormerDropPath(UpperCamelCase_		) 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((UpperCamelCase_)		)      ,       requires_grad=UpperCamelCase_		)
			snake_case__	       =   nn.Parameter(
			    config.layer_scale_init_value * torch.ones((UpperCamelCase_)		)      ,       requires_grad=UpperCamelCase_		)





	def   _snake_case   (		self      ,       UpperCamelCase_		)      ->		List[str]:
		if self.use_layer_scale:
			snake_case__	       =   self.pooling(self.before_norm(UpperCamelCase_		)		)
			snake_case__	       =   self.layer_scale_a.unsqueeze(-1		).unsqueeze(-1		) * pooling_output
			# First residual connection
			snake_case__	       =   hidden_states + self.drop_path(UpperCamelCase_		)
			snake_case__	       =   ()

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

			snake_case__	       =   (output,) + outputs
			return outputs

		else:
			snake_case__	       =   self.drop_path(self.pooling(self.before_norm(UpperCamelCase_		)		)		)
			# 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(UpperCamelCase_		)		)		)
			snake_case__	       =   hidden_states + layer_output

			snake_case__	       =   (output,) + outputs
			return outputs






class 				__snake_case     (					nn.Module							):





	def __init__(		self      ,       UpperCamelCase_		)      ->		Dict:
		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(UpperCamelCase_		)

		# 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(
				        UpperCamelCase_      ,       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(UpperCamelCase_		)		)

		snake_case__	       =   nn.ModuleList(UpperCamelCase_		)





	def   _snake_case   (		self      ,       UpperCamelCase_      ,       UpperCamelCase_=False      ,       UpperCamelCase_=True		)      ->		Dict:
		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(UpperCamelCase_		)
			# Send the embeddings through the blocks
			for _, blk in enumerate(UpperCamelCase_		):
				snake_case__	       =   blk(UpperCamelCase_		)
				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=UpperCamelCase_      ,       hidden_states=UpperCamelCase_		)






class 				__snake_case     (					__magic_name__							):
	__lowerCAmelCase       						=  PoolFormerConfig
	__lowerCAmelCase       						=  '''poolformer'''
	__lowerCAmelCase       						=  '''pixel_values'''
	__lowerCAmelCase       						=  True





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





	def   _snake_case   (		self      ,       UpperCamelCase_      ,       UpperCamelCase_=False		)      ->		int:
		if isinstance(UpperCamelCase_      ,       UpperCamelCase_		):
			snake_case__	       =   value





a__					:			int          =     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__					:			Union[str, Any]          =     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.'''       ,       __magic_name__       ,       )
class 				__snake_case     (					__magic_name__							):





	def __init__(		self      ,       UpperCamelCase_		)      ->		Any:
		super().__init__(UpperCamelCase_		)
		snake_case__	       =   config

		snake_case__	       =   PoolFormerEncoder(UpperCamelCase_		)

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





	def   _snake_case   (		self		)      ->		str:
		return self.embeddings.patch_embeddings





	@add_start_docstrings_to_model_forward(UpperCamelCase_		)
	@add_code_sample_docstrings(
	    checkpoint=_CHECKPOINT_FOR_DOC      ,       output_type=UpperCamelCase_      ,       config_class=_CONFIG_FOR_DOC      ,       modality='vision'      ,       expected_output=_EXPECTED_OUTPUT_SHAPE      ,       )
	def   _snake_case   (		self      ,       UpperCamelCase_ = None      ,       UpperCamelCase_ = None      ,       UpperCamelCase_ = None      ,       )      ->		Union[Tuple, BaseModelOutputWithNoAttention]:
		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(
		    UpperCamelCase_      ,       output_hidden_states=UpperCamelCase_      ,       return_dict=UpperCamelCase_      ,       )
		snake_case__	       =   encoder_outputs[0]

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

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






class 				__snake_case     (					nn.Module							):





	def __init__(		self      ,       UpperCamelCase_		)      ->		str:
		super().__init__()
		snake_case__	       =   nn.Linear(config.hidden_size      ,       config.hidden_size		)





	def   _snake_case   (		self      ,       UpperCamelCase_		)      ->		Any:
		snake_case__	       =   self.dense(UpperCamelCase_		)
		return output






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





	def __init__(		self      ,       UpperCamelCase_		)      ->		Optional[Any]:
		super().__init__(UpperCamelCase_		)
		snake_case__	       =   config.num_labels
		snake_case__	       =   PoolFormerModel(UpperCamelCase_		)

		# 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(UpperCamelCase_		)
	@add_code_sample_docstrings(
	    checkpoint=_IMAGE_CLASS_CHECKPOINT      ,       output_type=UpperCamelCase_      ,       config_class=_CONFIG_FOR_DOC      ,       expected_output=_IMAGE_CLASS_EXPECTED_OUTPUT      ,       )
	def   _snake_case   (		self      ,       UpperCamelCase_ = None      ,       UpperCamelCase_ = None      ,       UpperCamelCase_ = None      ,       UpperCamelCase_ = None      ,       )      ->		Union[Tuple, ImageClassifierOutputWithNoAttention]:
		snake_case__	       =   return_dict if return_dict is not None else self.config.use_return_dict

		snake_case__	       =   self.poolformer(
		    UpperCamelCase_      ,       output_hidden_states=UpperCamelCase_      ,       return_dict=UpperCamelCase_      ,       )

		snake_case__	       =   outputs[0]

		snake_case__	       =   self.classifier(self.norm(UpperCamelCase_		).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(UpperCamelCase_      ,       UpperCamelCase_		)
			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(UpperCamelCase_      ,       UpperCamelCase_		)

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

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



