

from __future__ import annotations

import os
import tempfile
import unittest

import numpy as np
from huggingface_hub import hf_hub_download

from transformers import is_tensorflow_text_available, is_tf_available
from transformers.testing_utils import require_tensorflow_text, require_tf, slow

from ..test_modeling_tf_common import floats_tensor
from .test_framework_agnostic import GenerationIntegrationTestsMixin


if is_tf_available():
					import tensorflow as tf

					from transformers import (
					    AutoTokenizer,
					    TFAutoModelForCausalLM,
					    TFAutoModelForSeqaSeqLM,
					    TFAutoModelForSpeechSeqaSeq,
					    TFAutoModelForVisionaSeq,
					    TFBartForConditionalGeneration,
					    TFLogitsProcessorList,
					    TFMinLengthLogitsProcessor,
					    tf_top_k_top_p_filtering,
					)

if is_tensorflow_text_available():
					import tensorflow_text as text





@require_tf
class       snake_case_  (						unittest.TestCase   ):







			'''simple docstring'''

			def 							snake_case__(	self					:							Optional[Any]				) ->Any:
									snake_case_  =      tf.convert_to_tensor(
									    [
									        [
									            8.2220991,  # 3rd highest value; idx. 0
									            -0.5620044,
									            5.23229752,
									            4.0386393,
									            -6.8798378,
									            -0.54785802,
									            -3.2012153,
									            2.92777176,
									            1.88171953,
									            7.35341276,  # 5th highest value; idx. 9
									            8.43207833,  # 2nd highest value; idx. 10
									            -9.85711836,
									            -5.96209236,
									            -1.13039161,
									            -7.1115294,
									            -0.8369633,
									            -5.3186408,
									            7.06427407,
									            0.81369344,
									            -0.82023817,
									            -5.9179796,
									            0.58813443,
									            -6.99778438,
									            4.71551189,
									            -0.18771637,
									            7.44020759,  # 4th highest value; idx. 25
									            9.38450987,  # 1st highest value; idx. 26
									            2.12662941,
									            -9.32562038,
									            2.35652522,
									        ],  # cummulative prob of 5 highest values <= 0.6
									        [
									            0.58425518,
									            4.53139238,
									            -5.57510464,
									            -6.28030699,
									            -7.19529503,
									            -4.02122551,
									            1.39337037,
									            -6.06707057,
									            1.59480517,
									            -9.643119,
									            0.03907799,
									            0.67231762,
									            -8.88206726,
									            6.27115922,  # 4th highest value; idx. 13
									            2.28520723,
									            4.82767506,
									            4.30421368,
									            8.8275313,  # 2nd highest value; idx. 17
									            5.44029958,  # 5th highest value; idx. 18
									            -4.4735794,
									            7.38579536,  # 3rd highest value; idx. 20
									            -2.91051663,
									            2.61946077,
									            -2.5674762,
									            -9.48959302,
									            -4.02922645,
									            -1.35416918,
									            9.67702323,  # 1st highest value; idx. 27
									            -5.89478553,
									            1.85370467,
									        ],  # cummulative prob of 5 highest values <= 0.6
									    ]				,  dtype=tf.floataa				,  )

									snake_case_  =      tf.convert_to_tensor(
									    [[0, 0], [0, 9], [0, 1_0], [0, 2_5], [0, 2_6], [1, 1_3], [1, 1_7], [1, 1_8], [1, 2_0], [1, 2_7]]				,  dtype=tf.intaa				,  )  # expected non filtered idx as noted above

									snake_case_  =      tf.convert_to_tensor(
									    [8.222099, 7.3534126, 8.432078, 7.4402075, 9.38451, 6.271159, 8.827531, 5.4402995, 7.3857956, 9.677023]				,  dtype=tf.floataa				,  )  # expected non filtered values as noted above

									snake_case_  =      tf_top_k_top_p_filtering(_UpperCamelCase				,  top_k=1_0				,  top_p=0.6				,  min_tokens_to_keep=4				)

									snake_case_  =      output[output != -float('''inf'''				)]
									snake_case_  =      tf.cast(
									    tf.where(tf.not_equal(_UpperCamelCase				,  tf.constant(-float('''inf'''				)				,  dtype=tf.floataa				)				)				)				,  dtype=tf.intaa				,  )

									tf.debugging.assert_near(_UpperCamelCase				,  _UpperCamelCase				,  rtol=1e-12				)
									tf.debugging.assert_equal(_UpperCamelCase				,  _UpperCamelCase				)






@require_tf
class       snake_case_  (						unittest.TestCase      ,			__A   ):







			'''simple docstring'''

			if is_tf_available():
						SCREAMING_SNAKE_CASE	:  Optional[int]		     =							{
						    "AutoModelForCausalLM": TFAutoModelForCausalLM,
						    "AutoModelForSpeechSeq2Seq": TFAutoModelForSpeechSeqaSeq,
						    "AutoModelForSeq2SeqLM": TFAutoModelForSeqaSeqLM,
						    "AutoModelForVision2Seq": TFAutoModelForVisionaSeq,
						    "LogitsProcessorList": TFLogitsProcessorList,
						    "MinLengthLogitsProcessor": TFMinLengthLogitsProcessor,
						    "create_tensor_fn": tf.convert_to_tensor,
						    "floats_tensor": floats_tensor,
						    "return_tensors": "tf",
						}







			@slow
			def 							snake_case__(	self					:							List[Any]				) ->Optional[int]:
									# TF-only test: tf.saved_model export
									snake_case_  =      TFAutoModelForCausalLM.from_pretrained('''hf-internal-testing/tiny-random-gpt2'''				)
									snake_case_  =      2
									snake_case_  =      2





									class       snake_case_  (						tf.Module   ):







												'''simple docstring'''

												def __init__(	self					:							Optional[Any]				,  _UpperCamelCase					:							Optional[int]				) ->List[Any]:
																		super(_UpperCamelCase				,  self				).__init__()
																		snake_case_  =      model







												@tf.function(
												    input_signature=(
												        tf.TensorSpec((None, input_length)				,  tf.intaa				,  name='''input_ids'''				),
												        tf.TensorSpec((None, input_length)				,  tf.intaa				,  name='''attention_mask'''				),
												    )				,  jit_compile=_UpperCamelCase				,  )
												def 							snake_case__(	self					:							List[Any]				,  _UpperCamelCase					:							int				,  _UpperCamelCase					:							Union[str, Any]				) ->List[Any]:
																		snake_case_  =      self.model.generate(
																		    input_ids=_UpperCamelCase				,  attention_mask=_UpperCamelCase				,  max_new_tokens=_UpperCamelCase				,  return_dict_in_generate=_UpperCamelCase				,  )
																		return {"sequences": outputs["sequences"]}






									snake_case_  =      [[2, 0], [1_0_2, 1_0_3]]
									snake_case_  =      [[1, 0], [1, 1]]
									snake_case_  =      DummyModel(model=_UpperCamelCase				)
									with tempfile.TemporaryDirectory() as tmp_dir:
															tf.saved_model.save(_UpperCamelCase				,  _UpperCamelCase				,  signatures={'''serving_default''': dummy_model.serving}				)
															snake_case_  =      tf.saved_model.load(_UpperCamelCase				).signatures['''serving_default''']
															for batch_size in range(1				,  len(_UpperCamelCase				) + 1				):
																					snake_case_  =      {
																					    '''input_ids''': tf.constant(dummy_input_ids[:batch_size]				),
																					    '''attention_mask''': tf.constant(dummy_attention_masks[:batch_size]				),
																					}
																					snake_case_  =      serving_func(**_UpperCamelCase				)['''sequences''']
																					snake_case_  =      test_model.generate(**_UpperCamelCase				,  max_new_tokens=_UpperCamelCase				)
																					tf.debugging.assert_equal(_UpperCamelCase				,  _UpperCamelCase				)







			@slow
			def 							snake_case__(	self					:							List[str]				) ->int:
									# TF-only test: tf.saved_model export
									snake_case_  =      TFAutoModelForCausalLM.from_pretrained('''hf-internal-testing/tiny-random-gpt2'''				)
									snake_case_  =      1
									snake_case_  =      2





									class       snake_case_  (						tf.Module   ):







												'''simple docstring'''

												def __init__(	self					:							str				,  _UpperCamelCase					:							Any				) ->List[str]:
																		super(_UpperCamelCase				,  self				).__init__()
																		snake_case_  =      model







												@tf.function(
												    input_signature=(
												        tf.TensorSpec((batch_size, None)				,  tf.intaa				,  name='''input_ids'''				),
												        tf.TensorSpec((batch_size, None)				,  tf.intaa				,  name='''attention_mask'''				),
												    )				,  jit_compile=_UpperCamelCase				,  )
												def 							snake_case__(	self					:							int				,  _UpperCamelCase					:							Tuple				,  _UpperCamelCase					:							List[Any]				) ->Optional[int]:
																		snake_case_  =      self.model.generate(
																		    input_ids=_UpperCamelCase				,  attention_mask=_UpperCamelCase				,  max_new_tokens=_UpperCamelCase				,  return_dict_in_generate=_UpperCamelCase				,  )
																		return {"sequences": outputs["sequences"]}






									snake_case_  =      [[2], [1_0_2, 1_0_3]]
									snake_case_  =      [[1], [1, 1]]
									snake_case_  =      DummyModel(model=_UpperCamelCase				)
									with tempfile.TemporaryDirectory() as tmp_dir:
															tf.saved_model.save(_UpperCamelCase				,  _UpperCamelCase				,  signatures={'''serving_default''': dummy_model.serving}				)
															snake_case_  =      tf.saved_model.load(_UpperCamelCase				).signatures['''serving_default''']
															for input_row in range(len(_UpperCamelCase				)				):
																					snake_case_  =      {
																					    '''input_ids''': tf.constant([dummy_input_ids[input_row]]				),
																					    '''attention_mask''': tf.constant([dummy_attention_masks[input_row]]				),
																					}
																					snake_case_  =      serving_func(**_UpperCamelCase				)['''sequences''']
																					snake_case_  =      test_model.generate(**_UpperCamelCase				,  max_new_tokens=_UpperCamelCase				)
																					tf.debugging.assert_equal(_UpperCamelCase				,  _UpperCamelCase				)







			@slow
			@require_tensorflow_text
			def 							snake_case__(	self					:							Optional[Any]				) ->List[Any]:
									# TF-only test: tf.saved_model export
									with tempfile.TemporaryDirectory() as tmp_dir:
															# file needed to load the TF tokenizer
															hf_hub_download(repo_id='''google/flan-t5-small'''				,  filename='''spiece.model'''				,  local_dir=_UpperCamelCase				)





															class       snake_case_  (						tf.keras.layers.Layer   ):







																		'''simple docstring'''

																		def __init__(	self					:							Tuple				) ->List[Any]:
																								super().__init__()
																								snake_case_  =      text.SentencepieceTokenizer(
																								    model=tf.io.gfile.GFile(os.path.join(_UpperCamelCase				,  '''spiece.model'''				)				,  '''rb'''				).read()				)
																								snake_case_  =      TFAutoModelForSeqaSeqLM.from_pretrained('''hf-internal-testing/tiny-random-t5'''				)







																		def 							snake_case__(	self					:							Optional[Any]				,  _UpperCamelCase					:							List[Any]				,  *_UpperCamelCase					:							Optional[int]				,  **_UpperCamelCase					:							str				) ->List[Any]:
																								snake_case_  =      self.tokenizer.tokenize(_UpperCamelCase				)
																								snake_case_,       snake_case_  =      text.pad_model_inputs(
																								    _UpperCamelCase				,  max_seq_length=6_4				,  pad_value=self.model.config.pad_token_id				)
																								snake_case_  =      self.model.generate(input_ids=_UpperCamelCase				,  attention_mask=_UpperCamelCase				)
																								return self.tokenizer.detokenize(_UpperCamelCase				)






															snake_case_  =      CompleteSentenceTransformer()
															snake_case_  =      tf.keras.layers.Input(shape=(1,)				,  dtype=tf.string				,  name='''inputs'''				)
															snake_case_  =      complete_model(_UpperCamelCase				)
															snake_case_  =      tf.keras.Model(_UpperCamelCase				,  _UpperCamelCase				)
															keras_model.save(_UpperCamelCase				)







			def 							snake_case__(	self					:							Any				) ->List[Any]:
									# Has PT equivalent: this test relies on random sampling
									snake_case_  =      {
									    '''do_sample''': True,
									    '''num_beams''': 1,
									    '''top_p''': 0.7,
									    '''top_k''': 1_0,
									    '''temperature''': 0.7,
									}
									snake_case_  =      1_4

									snake_case_  =      AutoTokenizer.from_pretrained('''hf-internal-testing/tiny-random-gpt2'''				)
									snake_case_  =      '''Hello, my dog is cute and'''
									snake_case_  =      tokenizer(_UpperCamelCase				,  return_tensors='''tf'''				)
									snake_case_  =      TFAutoModelForCausalLM.from_pretrained('''hf-internal-testing/tiny-random-gpt2'''				)

									snake_case_  =      6_3_8
									# forces the generation to happen on CPU, to avoid GPU-related quirks
									with tf.device(''':/CPU:0'''				):
															tf.random.set_seed(0				)
															snake_case_  =      model.generate(**_UpperCamelCase				,  eos_token_id=_UpperCamelCase				,  **_UpperCamelCase				)
									self.assertTrue(expectation == len(generated_tokens[0]				)				)

									snake_case_  =      [6_3_8, 1_9_8]
									with tf.device(''':/CPU:0'''				):
															tf.random.set_seed(0				)
															snake_case_  =      model.generate(**_UpperCamelCase				,  eos_token_id=_UpperCamelCase				,  **_UpperCamelCase				)
									self.assertTrue(expectation == len(generated_tokens[0]				)				)







			def 							snake_case__(	self					:							str				) ->Dict:
									# Has PT equivalent: ample use of framework-specific code
									snake_case_  =      AutoTokenizer.from_pretrained('''hf-internal-testing/tiny-random-bart'''				)
									snake_case_  =      '''Hugging Face is a technology company based in New York and Paris.'''
									snake_case_  =      bart_tokenizer(_UpperCamelCase				,  return_tensors='''tf'''				).input_ids
									snake_case_  =      TFBartForConditionalGeneration.from_pretrained('''hf-internal-testing/tiny-random-bart'''				)
									snake_case_  =      bart_model.generate(_UpperCamelCase				).numpy()





									class       snake_case_  (						__A   ):







												'''simple docstring'''

												def 							snake_case__(	self					:							str				,  _UpperCamelCase					:							Any				,  _UpperCamelCase					:							Tuple=None				,  **_UpperCamelCase					:							Optional[int]				) ->List[str]:
																		return super().call(_UpperCamelCase				,  **_UpperCamelCase				)






									snake_case_  =      FakeBart.from_pretrained('''hf-internal-testing/tiny-random-bart'''				)
									snake_case_  =      bart_model.generate(_UpperCamelCase				,  foo='''bar'''				).numpy()
									self.assertTrue(np.array_equal(_UpperCamelCase				,  _UpperCamelCase				)				)





									class       snake_case_  (						bart_model.model.encoder.__class__   ):







												'''simple docstring'''

												def 							snake_case__(	self					:							Union[str, Any]				,  _UpperCamelCase					:							str				,  **_UpperCamelCase					:							Tuple				) ->Optional[Any]:
																		return super().call(_UpperCamelCase				,  **_UpperCamelCase				)






									snake_case_  =      FakeEncoder(bart_model.config				,  bart_model.model.shared				)
									snake_case_  =      fake_encoder

									# Normal generation still works (the output will be different because the encoder weights are different)
									snake_case_  =      bart_model.generate(_UpperCamelCase				).numpy()
									with self.assertRaises(_UpperCamelCase				):
															# FakeEncoder.call() accepts **kwargs -> no filtering -> value error due to unexpected input "foo"
															bart_model.generate(_UpperCamelCase				,  foo='''bar'''				)




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

# General docstring
lowerCamelCase		       =      """PoolFormerConfig"""

# Base docstring
lowerCamelCase		       =      """sail/poolformer_s12"""
lowerCamelCase		       =      [1, 5_12, 7, 7]

# Image classification docstring
lowerCamelCase		       =      """sail/poolformer_s12"""
lowerCamelCase		       =      """tabby, tabby cat"""

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






def     SCREAMING_SNAKE_CASE(      __UpperCamelCase	,				__UpperCamelCase = 0.0	,				__UpperCamelCase = False )	->						Dict:
				if drop_prob == 0.0 or not training:
								return input
				a__	:							Tuple      						= 1 - drop_prob
				a__	:							Any      						= (input.shape[0],) + (1,) * (input.ndim - 1)  # work with diff dim tensors, not just 2D ConvNets
				a__	:							Dict      						= keep_prob + torch.rand(__UpperCamelCase	,				dtype=input.dtype	,				device=input.device )
				random_tensor.floor_()  # binarize
				a__	:							Optional[int]      						= input.div(__UpperCamelCase ) * random_tensor
				return output


class 				_a						(       nn.Module			):





							'''simple docstring'''

							def __init__(							self ,					__UpperCAmelCase = None				):







											"""simple docstring"""



											super().__init__()
											a__	:							Optional[Any]      						= drop_prob



							def 		_A	(							self ,					__UpperCAmelCase				):







											"""simple docstring"""



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




							def 		_A	(							self				):







											"""simple docstring"""



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





class 				_a						(       nn.Module			):





							'''simple docstring'''

							def __init__(							self ,					__UpperCAmelCase ,					__UpperCAmelCase ,					__UpperCAmelCase ,					__UpperCAmelCase ,					__UpperCAmelCase ,					__UpperCAmelCase=None				):







											"""simple docstring"""



											super().__init__()
											a__	:							Optional[Any]      						= patch_size if isinstance(__UpperCAmelCase ,					collections.abc.Iterable				) else (patch_size, patch_size)
											a__	:							List[str]      						= stride if isinstance(__UpperCAmelCase ,					collections.abc.Iterable				) else (stride, stride)
											a__	:							Union[str, Any]      						= padding if isinstance(__UpperCAmelCase ,					collections.abc.Iterable				) else (padding, padding)

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




							def 		_A	(							self ,					__UpperCAmelCase				):







											"""simple docstring"""



											a__	:							Dict      						= self.projection(__UpperCAmelCase				)
											a__	:							Union[str, Any]      						= self.norm(__UpperCAmelCase				)
											return embeddings





class 				_a						(       nn.GroupNorm			):





							'''simple docstring'''

							def __init__(							self ,					__UpperCAmelCase ,					**__UpperCAmelCase				):







											"""simple docstring"""



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





class 				_a						(       nn.Module			):





							'''simple docstring'''

							def __init__(							self ,					__UpperCAmelCase				):







											"""simple docstring"""



											super().__init__()
											a__	:							List[str]      						= nn.AvgPoolad(__UpperCAmelCase ,					stride=1 ,					padding=pool_size // 2 ,					count_include_pad=__UpperCAmelCase				)




							def 		_A	(							self ,					__UpperCAmelCase				):







											"""simple docstring"""



											return self.pool(__UpperCAmelCase				) - hidden_states





class 				_a						(       nn.Module			):





							'''simple docstring'''

							def __init__(							self ,					__UpperCAmelCase ,					__UpperCAmelCase ,					__UpperCAmelCase ,					__UpperCAmelCase				):







											"""simple docstring"""



											super().__init__()
											a__	:							str      						= nn.Convad(__UpperCAmelCase ,					__UpperCAmelCase ,					1				)
											a__	:							int      						= nn.Convad(__UpperCAmelCase ,					__UpperCAmelCase ,					1				)
											a__	:							Any      						= PoolFormerDropPath(__UpperCAmelCase				)
											if isinstance(config.hidden_act ,					__UpperCAmelCase				):
															a__	:							List[Any]      						= ACTaFN[config.hidden_act]
											else:
															a__	:							Union[str, Any]      						= config.hidden_act




							def 		_A	(							self ,					__UpperCAmelCase				):







											"""simple docstring"""



											a__	:							Union[str, Any]      						= self.conva(__UpperCAmelCase				)
											a__	:							Union[str, Any]      						= self.act_fn(__UpperCAmelCase				)
											a__	:							Dict      						= self.drop(__UpperCAmelCase				)
											a__	:							Union[str, Any]      						= self.conva(__UpperCAmelCase				)
											a__	:							int      						= self.drop(__UpperCAmelCase				)

											return hidden_states





class 				_a						(       nn.Module			):





							'''simple docstring'''

							def __init__(							self ,					__UpperCAmelCase ,					__UpperCAmelCase ,					__UpperCAmelCase ,					__UpperCAmelCase ,					__UpperCAmelCase ,					__UpperCAmelCase				):







											"""simple docstring"""



											super().__init__()
											a__	:							Any      						= PoolFormerPooling(__UpperCAmelCase				)
											a__	:							Any      						= PoolFormerOutput(__UpperCAmelCase ,					__UpperCAmelCase ,					__UpperCAmelCase ,					__UpperCAmelCase				)
											a__	:							Any      						= PoolFormerGroupNorm(__UpperCAmelCase				)
											a__	:							Dict      						= PoolFormerGroupNorm(__UpperCAmelCase				)

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




							def 		_A	(							self ,					__UpperCAmelCase				):







											"""simple docstring"""



											if self.use_layer_scale:
															a__	:							Any      						= self.pooling(self.before_norm(__UpperCAmelCase				)				)
															a__	:							int      						= self.layer_scale_a.unsqueeze(-1				).unsqueeze(-1				) * pooling_output
															# First residual connection
															a__	:							Optional[int]      						= hidden_states + self.drop_path(__UpperCAmelCase				)
															a__	:							Dict      						= ()

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

															a__	:							Optional[int]      						= (output,) + outputs
															return outputs

											else:
															a__	:							Optional[int]      						= self.drop_path(self.pooling(self.before_norm(__UpperCAmelCase				)				)				)
															# First residual connection
															a__	:							Tuple      						= pooling_output + hidden_states
															a__	:							Tuple      						= ()

															# Second residual connection inside the PoolFormerOutput block
															a__	:							Optional[int]      						= self.drop_path(self.output(self.after_norm(__UpperCAmelCase				)				)				)
															a__	:							str      						= hidden_states + layer_output

															a__	:							Optional[Any]      						= (output,) + outputs
															return outputs





class 				_a						(       nn.Module			):





							'''simple docstring'''

							def __init__(							self ,					__UpperCAmelCase				):







											"""simple docstring"""



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

											# patch embeddings
											a__	:							List[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] ,					)				)
											a__	:							Any      						= nn.ModuleList(__UpperCAmelCase				)

											# Transformer blocks
											a__	:							Optional[int]      						= []
											a__	:							List[Any]      						= 0
											for i in range(config.num_encoder_blocks				):
															# each block consists of layers
															a__	:							str      						= []
															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				)				)

											a__	:							Any      						= nn.ModuleList(__UpperCAmelCase				)




							def 		_A	(							self ,					__UpperCAmelCase ,					__UpperCAmelCase=False ,					__UpperCAmelCase=True				):







											"""simple docstring"""



											a__	:							int      						= () if output_hidden_states else None

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

															if output_hidden_states:
																			a__	:							Optional[Any]      						= 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 				_a						(       SCREAMING_SNAKE_CASE			):





							'''simple docstring'''

							A				:Optional[int] =       PoolFormerConfig
							A				:List[str] =       "poolformer"
							A				:Tuple =       "pixel_values"
							A				:List[str] =       True



							def 		_A	(							self ,					__UpperCAmelCase				):







											"""simple docstring"""



											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 		_A	(							self ,					__UpperCAmelCase ,					__UpperCAmelCase=False				):







											"""simple docstring"""



											if isinstance(__UpperCAmelCase ,					__UpperCAmelCase				):
															a__	:							Dict      						= value





lowerCamelCase		       =      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		       =      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."       ,  SCREAMING_SNAKE_CASE       ,  )
class 				_a						(       SCREAMING_SNAKE_CASE			):





							'''simple docstring'''

							def __init__(							self ,					__UpperCAmelCase				):







											"""simple docstring"""



											super().__init__(__UpperCAmelCase				)
											a__	:							Optional[Any]      						= config

											a__	:							int      						= PoolFormerEncoder(__UpperCAmelCase				)

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



							def 		_A	(							self				):







											"""simple docstring"""



											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 		_A	(							self ,					__UpperCAmelCase = None ,					__UpperCAmelCase = None ,					__UpperCAmelCase = None ,					):







											"""simple docstring"""



											a__	:							int      						= (
											    output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states
											)
											a__	:							str      						= 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__	:							List[Any]      						= self.encoder(
											    __UpperCAmelCase ,					output_hidden_states=__UpperCAmelCase ,					return_dict=__UpperCAmelCase ,					)
											a__	:							Any      						= 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 				_a						(       nn.Module			):





							'''simple docstring'''

							def __init__(							self ,					__UpperCAmelCase				):







											"""simple docstring"""



											super().__init__()
											a__	:							Any      						= nn.Linear(config.hidden_size ,					config.hidden_size				)




							def 		_A	(							self ,					__UpperCAmelCase				):







											"""simple docstring"""



											a__	:							Any      						= self.dense(__UpperCAmelCase				)
											return output





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





							'''simple docstring'''

							def __init__(							self ,					__UpperCAmelCase				):







											"""simple docstring"""



											super().__init__(__UpperCAmelCase				)
											a__	:							Optional[Any]      						= config.num_labels
											a__	:							int      						= PoolFormerModel(__UpperCAmelCase				)

											# Final norm
											a__	:							Any      						= PoolFormerGroupNorm(config.hidden_sizes[-1]				)
											# Classifier head
											a__	:							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(__UpperCAmelCase				)
							@add_code_sample_docstrings(
							    checkpoint=_IMAGE_CLASS_CHECKPOINT ,					output_type=__UpperCAmelCase ,					config_class=_CONFIG_FOR_DOC ,					expected_output=_IMAGE_CLASS_EXPECTED_OUTPUT ,					)
							def 		_A	(							self ,					__UpperCAmelCase = None ,					__UpperCAmelCase = None ,					__UpperCAmelCase = None ,					__UpperCAmelCase = None ,					):







											"""simple docstring"""



											a__	:							str      						= return_dict if return_dict is not None else self.config.use_return_dict

											a__	:							str      						= self.poolformer(
											    __UpperCAmelCase ,					output_hidden_states=__UpperCAmelCase ,					return_dict=__UpperCAmelCase ,					)

											a__	:							Optional[Any]      						= outputs[0]

											a__	:							Union[str, Any]      						= self.classifier(self.norm(__UpperCAmelCase				).mean([-2, -1]				)				)

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

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

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

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


