
import os
import unittest

from transformers import LayoutLMTokenizer, LayoutLMTokenizerFast
from transformers.models.layoutlm.tokenization_layoutlm import VOCAB_FILES_NAMES
from transformers.testing_utils import require_tokenizers

from ...test_tokenization_common import TokenizerTesterMixin




@require_tokenizers
class        SCREAMING_SNAKE_CASE__					(     UpperCAmelCase							,unittest.TestCase ):





	'''simple docstring'''



	_UpperCAmelCase	:		List[Any]          = LayoutLMTokenizer
	_UpperCAmelCase	:		int          = LayoutLMTokenizerFast
	_UpperCAmelCase	:		List[str]          = True
	_UpperCAmelCase	:		Tuple          = True



	def 			A							(  self     :   List[str]     ):


				'''simple docstring'''


				super().setUp()

				_snake_case          =      [
				    '[UNK]',
				    '[CLS]',
				    '[SEP]',
				    'want',
				    '##want',
				    '##ed',
				    'wa',
				    'un',
				    'runn',
				    '##ing',
				    ',',
				    'low',
				    'lowest',
				]
				_snake_case          =      os.path.join(self.tmpdirname      ,  VOCAB_FILES_NAMES['vocab_file']     )
				with open(self.vocab_file      ,  'w'      ,  encoding='utf-8'     ) as vocab_writer:
							vocab_writer.write(''.join([x + '\n' for x in vocab_tokens]     )     )



	def 			A							(  self     :   List[Any]      ,  **lowercase     :   Union[str, Any]     ):


				'''simple docstring'''


				return LayoutLMTokenizer.from_pretrained(self.tmpdirname      ,  **lowercase     )



	def 			A							(  self     :   Tuple      ,  lowercase     :   Optional[int]     ):


				'''simple docstring'''


				_snake_case          =      'UNwant\u00E9d,running'
				_snake_case          =      'unwanted, running'
				return input_text, output_text



	def 			A							(  self     :   Dict     ):


				'''simple docstring'''


				_snake_case          =      self.tokenizer_class(self.vocab_file     )

				_snake_case          =      tokenizer.tokenize('UNwant\u00E9d,running'     )
				self.assertListEqual(lowercase      ,  ['un', '##want', '##ed', ',', 'runn', '##ing']     )
				self.assertListEqual(tokenizer.convert_tokens_to_ids(lowercase     )      ,  [7, 4, 5, 10, 8, 9]     )





	def 			A							(  self     :   Tuple     ):


				'''simple docstring'''


				pass

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

# General docstring
_lowerCamelCase	:    Dict          =      '''PoolFormerConfig'''

# Base docstring
_lowerCamelCase	:    int          =      '''sail/poolformer_s12'''
_lowerCamelCase	:    Optional[Any]          =      [1, 512, 7, 7]

# Image classification docstring
_lowerCamelCase	:    Optional[int]          =      '''sail/poolformer_s12'''
_lowerCamelCase	:    List[Any]          =      '''tabby, tabby cat'''

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






def 	a_		(	__lowercase       :     List[Any]	, __lowercase       :     float = 0.0	, __lowercase       :     bool = 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(__lowercase	, dtype=input.dtype	, device=input.device			)
			random_tensor.floor_()  # binarize
			_snake_case          =      input.div(__lowercase			) * random_tensor
			return output




class        SCREAMING_SNAKE_CASE__					(     nn.Module ):





	'''simple docstring'''



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


				'''simple docstring'''


				super().__init__()
				_snake_case          =      drop_prob



	def 			A							(  self     :   Any      ,  lowercase     :   torch.Tensor     ):


				'''simple docstring'''


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





	def 			A							(  self     :   Tuple     ):


				'''simple docstring'''


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




class        SCREAMING_SNAKE_CASE__					(     nn.Module ):





	'''simple docstring'''



	def __init__(  self     :   Tuple      ,  lowercase     :   Dict      ,  lowercase     :   Dict      ,  lowercase     :   str      ,  lowercase     :   int      ,  lowercase     :   Optional[Any]      ,  lowercase     :   str=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 			A							(  self     :   int      ,  lowercase     :   Union[str, Any]     ):


				'''simple docstring'''


				_snake_case          =      self.projection(lowercase     )
				_snake_case          =      self.norm(lowercase     )
				return embeddings




class        SCREAMING_SNAKE_CASE__					(     nn.GroupNorm ):





	'''simple docstring'''



	def __init__(  self     :   Dict      ,  lowercase     :   List[Any]      ,  **lowercase     :   str     ):


				'''simple docstring'''


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




class        SCREAMING_SNAKE_CASE__					(     nn.Module ):





	'''simple docstring'''



	def __init__(  self     :   int      ,  lowercase     :   List[Any]     ):


				'''simple docstring'''


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





	def 			A							(  self     :   int      ,  lowercase     :   List[str]     ):


				'''simple docstring'''


				return self.pool(lowercase     ) - hidden_states




class        SCREAMING_SNAKE_CASE__					(     nn.Module ):





	'''simple docstring'''



	def __init__(  self     :   Dict      ,  lowercase     :   Tuple      ,  lowercase     :   str      ,  lowercase     :   Optional[Any]      ,  lowercase     :   Union[str, 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 			A							(  self     :   Optional[int]      ,  lowercase     :   str     ):


				'''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        SCREAMING_SNAKE_CASE__					(     nn.Module ):





	'''simple docstring'''



	def __init__(  self     :   str      ,  lowercase     :   Tuple      ,  lowercase     :   int      ,  lowercase     :   str      ,  lowercase     :   Union[str, Any]      ,  lowercase     :   str      ,  lowercase     :   Dict     ):


				'''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 			A							(  self     :   Optional[int]      ,  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        SCREAMING_SNAKE_CASE__					(     nn.Module ):





	'''simple docstring'''



	def __init__(  self     :   Optional[Any]      ,  lowercase     :   Optional[int]     ):


				'''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 			A							(  self     :   Any      ,  lowercase     :   List[str]      ,  lowercase     :   str=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        SCREAMING_SNAKE_CASE__					(     UpperCAmelCase ):





	'''simple docstring'''



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



	def 			A							(  self     :   Tuple      ,  lowercase     :   str     ):


				'''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 			A							(  self     :   Optional[Any]      ,  lowercase     :   str      ,  lowercase     :   Dict=False     ):


				'''simple docstring'''


				if isinstance(lowercase      ,  lowercase     ):
							_snake_case          =      value




_lowerCamelCase	:    Optional[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."							,UpperCAmelCase							,)
class        SCREAMING_SNAKE_CASE__					(     UpperCAmelCase ):





	'''simple docstring'''



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


				'''simple docstring'''


				super().__init__(lowercase     )
				_snake_case          =      config

				_snake_case          =      PoolFormerEncoder(lowercase     )

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



	def 			A							(  self     :   List[str]     ):


				'''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 			A							(  self     :   Tuple      ,  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        SCREAMING_SNAKE_CASE__					(     nn.Module ):





	'''simple docstring'''



	def __init__(  self     :   List[str]      ,  lowercase     :   Union[str, Any]     ):


				'''simple docstring'''


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





	def 			A							(  self     :   Optional[Any]      ,  lowercase     :   Optional[int]     ):


				'''simple docstring'''


				_snake_case          =      self.dense(lowercase     )
				return output




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





	'''simple docstring'''



	def __init__(  self     :   str      ,  lowercase     :   Any     ):


				'''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 			A							(  self     :   Union[str, Any]      ,  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     )