





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							:			Optional[int]         =			logging.get_logger(__name__)

# General docstring
__A							:			Optional[Any]         =			"PoolFormerConfig"

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

# Image classification docstring
__A							:			Optional[int]         =			"sail/poolformer_s12"
__A							:			Union[str, Any]         =			"tabby, tabby cat"

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





def 				__lowerCAmelCase(       _SCREAMING_SNAKE_CASE							,     _SCREAMING_SNAKE_CASE = 0.0							,     _SCREAMING_SNAKE_CASE = False				)							-> str:




    """simple docstring"""




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







class      lowerCamelCase(						nn.Module	):
     '''simple docstring'''


     def __init__(    self				,     snake_case_ = None							):
         super().__init__()
         _A     =							drop_prob




     def        lowerCAmelCase__		(    self				,     snake_case_							):
         return drop_path(snake_case_				,     self.drop_prob				,     self.training							)




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







class      lowerCamelCase(						nn.Module	):
     '''simple docstring'''


     def __init__(    self				,     snake_case_				,     snake_case_				,     snake_case_				,     snake_case_				,     snake_case_				,     snake_case_=None							):
         super().__init__()
         _A     =							patch_size if isinstance(snake_case_				,     collections.abc.Iterable							) else (patch_size, patch_size)
         _A     =							stride if isinstance(snake_case_				,     collections.abc.Iterable							) else (stride, stride)
         _A     =							padding if isinstance(snake_case_				,     collections.abc.Iterable							) else (padding, padding)

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




     def        lowerCAmelCase__		(    self				,     snake_case_							):
         _A     =							self.projection(snake_case_							)
         _A     =							self.norm(snake_case_							)
         return embeddings







class      lowerCamelCase(						nn.GroupNorm	):
     '''simple docstring'''


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







class      lowerCamelCase(						nn.Module	):
     '''simple docstring'''


     def __init__(    self				,     snake_case_							):
         super().__init__()
         _A     =							nn.AvgPoolad(snake_case_				,     stride=1				,     padding=pool_size // 2				,     count_include_pad=snake_case_							)




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







class      lowerCamelCase(						nn.Module	):
     '''simple docstring'''


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




     def        lowerCAmelCase__		(    self				,     snake_case_							):
         _A     =							self.conva(snake_case_							)
         _A     =							self.act_fn(snake_case_							)
         _A     =							self.drop(snake_case_							)
         _A     =							self.conva(snake_case_							)
         _A     =							self.drop(snake_case_							)

         return hidden_states







class      lowerCamelCase(						nn.Module	):
     '''simple docstring'''


     def __init__(    self				,     snake_case_				,     snake_case_				,     snake_case_				,     snake_case_				,     snake_case_				,     snake_case_							):
         super().__init__()
         _A     =							PoolFormerPooling(snake_case_							)
         _A     =							PoolFormerOutput(snake_case_				,     snake_case_				,     snake_case_				,     snake_case_							)
         _A     =							PoolFormerGroupNorm(snake_case_							)
         _A     =							PoolFormerGroupNorm(snake_case_							)

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




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

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

             _A     =							(output,) + outputs
             return outputs

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

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

             _A     =							(output,) + outputs
             return outputs







class      lowerCamelCase(						nn.Module	):
     '''simple docstring'''


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

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

         # Transformer blocks
         _A     =							[]
         _A     =							0
         for i in range(config.num_encoder_blocks							):
             # each block consists of layers
             _A     =							[]
             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_							)							)

         _A     =							nn.ModuleList(snake_case_							)




     def        lowerCAmelCase__		(    self				,     snake_case_				,     snake_case_=False				,     snake_case_=True							):
         _A     =							() if output_hidden_states else None

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

             if output_hidden_states:
                 _A     =							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      lowerCamelCase(						__snake_case	):
     '''simple docstring'''


     __magic_name__   =			PoolFormerConfig
     __magic_name__   =			'poolformer'
     __magic_name__   =			'pixel_values'
     __magic_name__   =			True




     def        lowerCAmelCase__		(    self				,     snake_case_							):
         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				,     snake_case_				,     snake_case_=False							):
         if isinstance(snake_case_				,     snake_case_							):
             _A     =							value




__A							:			Tuple         =			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"

__A							:			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.'			, __snake_case			, )
class      lowerCamelCase(						__snake_case	):
     '''simple docstring'''


     def __init__(    self				,     snake_case_							):
         super().__init__(snake_case_							)
         _A     =							config

         _A     =							PoolFormerEncoder(snake_case_							)

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




     def        lowerCAmelCase__		(    self							):
         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				,     snake_case_ = None				,     snake_case_ = None				,     snake_case_ = None				,     ):
         _A     =							(
             output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states
         )
         _A     =							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'							)

         _A     =							self.encoder(
             snake_case_				,     output_hidden_states=snake_case_				,     return_dict=snake_case_				,     )
         _A     =							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      lowerCamelCase(						nn.Module	):
     '''simple docstring'''


     def __init__(    self				,     snake_case_							):
         super().__init__()
         _A     =							nn.Linear(config.hidden_size				,     config.hidden_size							)




     def        lowerCAmelCase__		(    self				,     snake_case_							):
         _A     =							self.dense(snake_case_							)
         return output







@add_start_docstrings(
    '\n    PoolFormer Model transformer with an image classification head on top\n    '			, __snake_case			, )
class      lowerCamelCase(						__snake_case	):
     '''simple docstring'''


     def __init__(    self				,     snake_case_							):
         super().__init__(snake_case_							)
         _A     =							config.num_labels
         _A     =							PoolFormerModel(snake_case_							)

         # Final norm
         _A     =							PoolFormerGroupNorm(config.hidden_sizes[-1]							)
         # Classifier head
         _A     =							(
             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				,     snake_case_ = None				,     snake_case_ = None				,     snake_case_ = None				,     snake_case_ = None				,     ):
         _A     =							return_dict if return_dict is not None else self.config.use_return_dict

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

         _A     =							outputs[0]

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

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

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

         if not return_dict:
             _A     =							(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							)












def 				__lowerCAmelCase(       _SCREAMING_SNAKE_CASE							,     _SCREAMING_SNAKE_CASE				)							-> int:




    """simple docstring"""




    return int((input_a, input_a).count(0				) != 0				)





def 				__lowerCAmelCase(       )							-> None:




    """simple docstring"""




    assert nand_gate(0							,     0				) == 1
    assert nand_gate(0							,     1				) == 1
    assert nand_gate(1							,     0				) == 1
    assert nand_gate(1							,     1				) == 0


if __name__ == "__main__":
 print(nand_gate(0, 0))
 print(nand_gate(0, 1))
 print(nand_gate(1, 0))
 print(nand_gate(1, 1))





