





'''simple docstring'''







import inspect
import unittest

from transformers import is_torch_available, is_vision_available
from transformers.models.auto import get_values
from transformers.testing_utils import require_torch, slow, torch_device

from ...test_configuration_common import ConfigTester
from ...test_modeling_common import ModelTesterMixin, floats_tensor, ids_tensor
from ...test_pipeline_mixin import PipelineTesterMixin


if is_torch_available():
	import torch

	from transformers import MODEL_MAPPING, PoolFormerConfig, PoolFormerForImageClassification, PoolFormerModel
	from transformers.models.poolformer.modeling_poolformer import POOLFORMER_PRETRAINED_MODEL_ARCHIVE_LIST


if is_vision_available():
	from PIL import Image

	from transformers import PoolFormerImageProcessor





class 					_UpperCAmelCase							(		lowerCAmelCase_     ):
				def 						lowerCamelCase__    (						self    ):



							'''simple docstring'''

							__lowerCAmelCase      		=       self.config_class(**self.inputs_dict    )
							self.parent.assertTrue(hasattr(__SCREAMING_SNAKE_CASE,"""hidden_sizes"""    )    )
							self.parent.assertTrue(hasattr(__SCREAMING_SNAKE_CASE,"""num_encoder_blocks"""    )    )





class 					_UpperCAmelCase							:
				def __init__(						self,__SCREAMING_SNAKE_CASE,__SCREAMING_SNAKE_CASE=13,__SCREAMING_SNAKE_CASE=64,__SCREAMING_SNAKE_CASE=3,__SCREAMING_SNAKE_CASE=4,__SCREAMING_SNAKE_CASE=[2, 2, 2, 2],__SCREAMING_SNAKE_CASE=[8, 4, 2, 1],__SCREAMING_SNAKE_CASE=[16, 32, 64, 1_28],__SCREAMING_SNAKE_CASE=[1, 4, 8, 16],__SCREAMING_SNAKE_CASE=False,__SCREAMING_SNAKE_CASE=True,__SCREAMING_SNAKE_CASE="gelu",__SCREAMING_SNAKE_CASE=0.1,__SCREAMING_SNAKE_CASE=0.02,__SCREAMING_SNAKE_CASE=3,__SCREAMING_SNAKE_CASE=None,):



							'''simple docstring'''

							__lowerCAmelCase      		=       parent
							__lowerCAmelCase      		=       batch_size
							__lowerCAmelCase      		=       image_size
							__lowerCAmelCase      		=       num_channels
							__lowerCAmelCase      		=       num_encoder_blocks
							__lowerCAmelCase      		=       sr_ratios
							__lowerCAmelCase      		=       depths
							__lowerCAmelCase      		=       hidden_sizes
							__lowerCAmelCase      		=       downsampling_rates
							__lowerCAmelCase      		=       is_training
							__lowerCAmelCase      		=       use_labels
							__lowerCAmelCase      		=       hidden_act
							__lowerCAmelCase      		=       hidden_dropout_prob
							__lowerCAmelCase      		=       initializer_range
							__lowerCAmelCase      		=       num_labels
							__lowerCAmelCase      		=       scope
				def 						lowerCamelCase__    (						self    ):



							'''simple docstring'''

							__lowerCAmelCase      		=       floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]    )

							__lowerCAmelCase      		=       None
							if self.use_labels:
										__lowerCAmelCase      		=       ids_tensor([self.batch_size, self.image_size, self.image_size],self.num_labels    )

							__lowerCAmelCase      		=       PoolFormerConfig(
							    image_size=self.image_size,num_channels=self.num_channels,num_encoder_blocks=self.num_encoder_blocks,depths=self.depths,hidden_sizes=self.hidden_sizes,hidden_act=self.hidden_act,hidden_dropout_prob=self.hidden_dropout_prob,initializer_range=self.initializer_range,)

							return config, pixel_values, labels
				def 						lowerCamelCase__    (						self,__SCREAMING_SNAKE_CASE,__SCREAMING_SNAKE_CASE,__SCREAMING_SNAKE_CASE    ):



							'''simple docstring'''

							__lowerCAmelCase      		=       PoolFormerModel(config=__SCREAMING_SNAKE_CASE    )
							model.to(__SCREAMING_SNAKE_CASE    )
							model.eval()
							__lowerCAmelCase      		=       model(__SCREAMING_SNAKE_CASE    )
							__lowerCAmelCase      		=       __lowerCAmelCase      		=       self.image_size // 32.0
							self.parent.assertEqual(
							    result.last_hidden_state.shape,(self.batch_size, self.hidden_sizes[-1], expected_height, expected_width)    )







				def 						lowerCamelCase__    (						self    ):



							'''simple docstring'''

							__lowerCAmelCase      		=       self.prepare_config_and_inputs()
							__lowerCAmelCase		, __lowerCAmelCase		, __lowerCAmelCase      		=       config_and_inputs
							__lowerCAmelCase      		=       {"""pixel_values""": pixel_values}
							return config, inputs_dict





@require_torch
class 					_UpperCAmelCase							(		lowerCAmelCase_			,      lowerCAmelCase_			,      unittest.TestCase     ):
				a     :     Optional[Any]     						=(PoolFormerModel, PoolFormerForImageClassification) if is_torch_available() else ()
				a     :     Union[str, Any]     						=(
				    {"""feature-extraction""": PoolFormerModel, """image-classification""": PoolFormerForImageClassification}
				    if is_torch_available()
				    else {}
				)

				a     :     Dict     						=False
				a     :     Optional[int]     						=False
				a     :     Optional[Any]     						=False
				a     :     str     						=False
				a     :     Optional[int]     						=False
				def 						lowerCamelCase__    (						self    ):



							'''simple docstring'''

							__lowerCAmelCase      		=       PoolFormerModelTester(self    )
							__lowerCAmelCase      		=       PoolFormerConfigTester(self,config_class=__SCREAMING_SNAKE_CASE    )
				def 						lowerCamelCase__    (						self    ):



							'''simple docstring'''

							self.config_tester.run_common_tests()
				def 						lowerCamelCase__    (						self    ):



							'''simple docstring'''

							__lowerCAmelCase      		=       self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_model(*__SCREAMING_SNAKE_CASE    )
				@unittest.skip("""PoolFormer does not use inputs_embeds"""    )
				def 						lowerCamelCase__    (						self    ):



							'''simple docstring'''

							pass
				@unittest.skip("""PoolFormer does not have get_input_embeddings method and get_output_embeddings methods"""    )
				def 						lowerCamelCase__    (						self    ):



							'''simple docstring'''

							pass
				def 						lowerCamelCase__    (						self    ):



							'''simple docstring'''

							def check_hidden_states_output(__SCREAMING_SNAKE_CASE,__SCREAMING_SNAKE_CASE,__SCREAMING_SNAKE_CASE    ):
										__lowerCAmelCase      		=       model_class(__SCREAMING_SNAKE_CASE    )
										model.to(__SCREAMING_SNAKE_CASE    )
										model.eval()

										with torch.no_grad():
													__lowerCAmelCase      		=       model(**self._prepare_for_class(__SCREAMING_SNAKE_CASE,__SCREAMING_SNAKE_CASE    )    )

										__lowerCAmelCase      		=       outputs.hidden_states

										__lowerCAmelCase      		=       self.model_tester.num_encoder_blocks
										self.assertEqual(len(__SCREAMING_SNAKE_CASE    ),__SCREAMING_SNAKE_CASE    )

										# verify the first hidden states (first block)
										self.assertListEqual(
										    list(hidden_states[0].shape[-3:]    ),[
										        self.model_tester.hidden_sizes[0],
										        self.model_tester.image_size // 4,
										        self.model_tester.image_size // 4,
										    ],)

							__lowerCAmelCase		, __lowerCAmelCase      		=       self.model_tester.prepare_config_and_inputs_for_common()

							for model_class in self.all_model_classes:
										__lowerCAmelCase      		=       True
										check_hidden_states_output(__SCREAMING_SNAKE_CASE,__SCREAMING_SNAKE_CASE,__SCREAMING_SNAKE_CASE    )

										# check that output_hidden_states also work using config
										del inputs_dict["output_hidden_states"]
										__lowerCAmelCase      		=       True

										check_hidden_states_output(__SCREAMING_SNAKE_CASE,__SCREAMING_SNAKE_CASE,__SCREAMING_SNAKE_CASE    )
				def 						lowerCamelCase__    (						self    ):



							'''simple docstring'''

							if not self.model_tester.is_training:
										return

							__lowerCAmelCase		, __lowerCAmelCase      		=       self.model_tester.prepare_config_and_inputs_for_common()
							__lowerCAmelCase      		=       True

							for model_class in self.all_model_classes:
										if model_class in get_values(__SCREAMING_SNAKE_CASE    ):
													continue
										__lowerCAmelCase      		=       model_class(__SCREAMING_SNAKE_CASE    )
										model.to(__SCREAMING_SNAKE_CASE    )
										model.train()
										__lowerCAmelCase      		=       self._prepare_for_class(__SCREAMING_SNAKE_CASE,__SCREAMING_SNAKE_CASE,return_labels=__SCREAMING_SNAKE_CASE    )
										__lowerCAmelCase      		=       model(**__SCREAMING_SNAKE_CASE    ).loss
										loss.backward()
				def 						lowerCamelCase__    (						self    ):



							'''simple docstring'''

							__lowerCAmelCase		, __lowerCAmelCase      		=       self.model_tester.prepare_config_and_inputs_for_common()

							for model_class in self.all_model_classes:
										__lowerCAmelCase      		=       model_class(__SCREAMING_SNAKE_CASE    )
										__lowerCAmelCase      		=       inspect.signature(model.forward    )
										# signature.parameters is an OrderedDict => so arg_names order is deterministic
										__lowerCAmelCase      		=       [*signature.parameters.keys()]

										__lowerCAmelCase      		=       ["""pixel_values"""]
										self.assertListEqual(arg_names[:1],__SCREAMING_SNAKE_CASE    )







				@slow
				def 						lowerCamelCase__    (						self    ):



							'''simple docstring'''

							for model_name in POOLFORMER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
										__lowerCAmelCase      		=       PoolFormerModel.from_pretrained(__SCREAMING_SNAKE_CASE    )
										self.assertIsNotNone(__SCREAMING_SNAKE_CASE    )





def      _lowerCAmelCase		(		)      ->      Any:
			__lowerCAmelCase      		=       Image.open("""./tests/fixtures/tests_samples/COCO/000000039769.png"""			)
			return image







@require_torch
class 					_UpperCAmelCase							(		unittest.TestCase     ):
				@slow
				def 						lowerCamelCase__    (						self    ):



							'''simple docstring'''

							__lowerCAmelCase      		=       PoolFormerImageProcessor()
							__lowerCAmelCase      		=       PoolFormerForImageClassification.from_pretrained("""sail/poolformer_s12"""    ).to(__SCREAMING_SNAKE_CASE    )

							__lowerCAmelCase      		=       image_processor(images=prepare_img(),return_tensors="""pt"""    ).to(__SCREAMING_SNAKE_CASE    )

							# forward pass
							with torch.no_grad():
										__lowerCAmelCase      		=       model(**__SCREAMING_SNAKE_CASE    )

							# verify the logits
							__lowerCAmelCase      		=       torch.Size((1, 10_00)    )
							self.assertEqual(outputs.logits.shape,__SCREAMING_SNAKE_CASE    )

							__lowerCAmelCase      		=       torch.tensor([-0.6113, 0.1685, -0.0492]    ).to(__SCREAMING_SNAKE_CASE    )
							self.assertTrue(torch.allclose(outputs.logits[0, :3],__SCREAMING_SNAKE_CASE,atol=1e-4    )    )








'''simple docstring'''







import itertools
from dataclasses import dataclass
from io import StringIO
from typing import Optional

import pyarrow as pa

import datasets
from datasets.features.features import require_storage_cast
from datasets.table import table_cast


_a      :				Tuple											=					datasets.utils.logging.get_logger(__name__)





@dataclass
class 					_UpperCAmelCase							(		datasets.BuilderConfig     ):
		a     :     Optional[datasets.Features]     						=None
		a     :     str     						="utf-8"
		a     :     Optional[str]     						=None
		a     :     int     						=10 << 20  # 10MB
		a     :     bool     						=False
		a     :     str     						="line"





class 					_UpperCAmelCase							(		datasets.ArrowBasedBuilder     ):
		a     :     Optional[int]     						=TextConfig
		def 						lowerCamelCase__    (						self    ):



					'''simple docstring'''

					return datasets.DatasetInfo(features=self.config.features    )
		def 						lowerCamelCase__    (						self,__SCREAMING_SNAKE_CASE    ):



					'''simple docstring'''

					if not self.config.data_files:
								raise ValueError(f'At least one data file must be specified, but got data_files={self.config.data_files}'    )
					__lowerCAmelCase      		=       dl_manager.download_and_extract(self.config.data_files    )
					if isinstance(__SCREAMING_SNAKE_CASE,(str, list, tuple)    ):
								__lowerCAmelCase      		=       data_files
								if isinstance(__SCREAMING_SNAKE_CASE,__SCREAMING_SNAKE_CASE    ):
											__lowerCAmelCase      		=       [files]
								__lowerCAmelCase      		=       [dl_manager.iter_files(__SCREAMING_SNAKE_CASE    ) for file in files]
								return [datasets.SplitGenerator(name=datasets.Split.TRAIN,gen_kwargs={"""files""": files}    )]
					__lowerCAmelCase      		=       []
					for split_name, files in data_files.items():
								if isinstance(__SCREAMING_SNAKE_CASE,__SCREAMING_SNAKE_CASE    ):
											__lowerCAmelCase      		=       [files]
								__lowerCAmelCase      		=       [dl_manager.iter_files(__SCREAMING_SNAKE_CASE    ) for file in files]
								splits.append(datasets.SplitGenerator(name=__SCREAMING_SNAKE_CASE,gen_kwargs={"""files""": files}    )    )
					return splits
		def 						lowerCamelCase__    (						self,__SCREAMING_SNAKE_CASE    ):



					'''simple docstring'''

					if self.config.features is not None:
								__lowerCAmelCase      		=       self.config.features.arrow_schema
								if all(not require_storage_cast(__SCREAMING_SNAKE_CASE    ) for feature in self.config.features.values()    ):
											# cheaper cast
											__lowerCAmelCase      		=       pa_table.cast(__SCREAMING_SNAKE_CASE    )
								else:
											# more expensive cast; allows str <-> int/float or str to Audio for example
											__lowerCAmelCase      		=       table_cast(__SCREAMING_SNAKE_CASE,__SCREAMING_SNAKE_CASE    )
								return pa_table
					else:
								return pa_table.cast(pa.schema({"""text""": pa.string()}    )    )







		def 						lowerCamelCase__    (						self,__SCREAMING_SNAKE_CASE    ):



					'''simple docstring'''

					__lowerCAmelCase      		=       list(self.config.features    ) if self.config.features is not None else ["""text"""]
					for file_idx, file in enumerate(itertools.chain.from_iterable(__SCREAMING_SNAKE_CASE    )    ):
								# open in text mode, by default translates universal newlines ("\n", "\r\n" and "\r") into "\n"
								with open(__SCREAMING_SNAKE_CASE,encoding=self.config.encoding,errors=self.config.errors    ) as f:
											if self.config.sample_by == "line":
														__lowerCAmelCase      		=       0
														while True:
																	__lowerCAmelCase      		=       f.read(self.config.chunksize    )
																	if not batch:
																				break
																	batch += f.readline()  # finish current line
																	# StringIO.readlines, by default splits only on "\n" (and keeps line breaks)
																	__lowerCAmelCase      		=       StringIO(__SCREAMING_SNAKE_CASE    ).readlines()
																	if not self.config.keep_linebreaks:
																				__lowerCAmelCase      		=       [line.rstrip("""\n"""    ) for line in batch]
																	__lowerCAmelCase      		=       pa.Table.from_arrays([pa.array(__SCREAMING_SNAKE_CASE    )],names=__SCREAMING_SNAKE_CASE    )
																	# Uncomment for debugging (will print the Arrow table size and elements)
																	# logger.warning(f"pa_table: {pa_table} num rows: {pa_table.num_rows}")
																	# logger.warning('\n'.join(str(pa_table.slice(i, 1).to_pydict()) for i in range(pa_table.num_rows)))
																	yield (file_idx, batch_idx), self._cast_table(__SCREAMING_SNAKE_CASE    )
																	batch_idx += 1
											elif self.config.sample_by == "paragraph":
														__lowerCAmelCase      		=       0
														__lowerCAmelCase      		=       """"""
														while True:
																	__lowerCAmelCase      		=       f.read(self.config.chunksize    )
																	if not new_batch:
																				break
																	batch += new_batch
																	batch += f.readline()  # finish current line
																	__lowerCAmelCase      		=       batch.split("""\n\n"""    )
																	__lowerCAmelCase      		=       pa.Table.from_arrays(
																	    [pa.array([example for example in batch[:-1] if example]    )],names=__SCREAMING_SNAKE_CASE    )
																	# Uncomment for debugging (will print the Arrow table size and elements)
																	# logger.warning(f"pa_table: {pa_table} num rows: {pa_table.num_rows}")
																	# logger.warning('\n'.join(str(pa_table.slice(i, 1).to_pydict()) for i in range(pa_table.num_rows)))
																	yield (file_idx, batch_idx), self._cast_table(__SCREAMING_SNAKE_CASE    )
																	batch_idx += 1
																	__lowerCAmelCase      		=       batch[-1]
														if batch:
																	__lowerCAmelCase      		=       pa.Table.from_arrays([pa.array([batch]    )],names=__SCREAMING_SNAKE_CASE    )
																	yield (file_idx, batch_idx), self._cast_table(__SCREAMING_SNAKE_CASE    )
											elif self.config.sample_by == "document":
														__lowerCAmelCase      		=       f.read()
														__lowerCAmelCase      		=       pa.Table.from_arrays([pa.array([text]    )],names=__SCREAMING_SNAKE_CASE    )
														yield file_idx, self._cast_table(__SCREAMING_SNAKE_CASE    )

