


import copy
import inspect
import unittest

from transformers import PretrainedConfig, SwiftFormerConfig
from transformers.testing_utils import (
    require_torch,
    require_vision,
    slow,
    torch_device,
)
from transformers.utils import cached_property, is_torch_available, is_vision_available

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 torch import nn

							from transformers import SwiftFormerForImageClassification, SwiftFormerModel
							from transformers.models.swiftformer.modeling_swiftformer import SWIFTFORMER_PRETRAINED_MODEL_ARCHIVE_LIST


if is_vision_available():
							from PIL import Image

							from transformers import ViTImageProcessor

class 						A__							:




						def __init__(       self					:      int				,    _a					:      Optional[Any]				,    _a					:      Dict=13				,    _a					:      List[str]=3				,    _a					:      Union[str, Any]=True				,    _a					:      Tuple=True				,    _a					:      Tuple=0.1				,    _a					:      str=0.1				,    _a					:      int=224				,    _a					:      List[str]=1000				,    _a					:      str=[3, 3, 6, 4]				,    _a					:      List[str]=[48, 56, 112, 220]				,    )							->     List[Any]:


										"""simple docstring"""





										_SCREAMING_SNAKE_CASE			  =parent
										_SCREAMING_SNAKE_CASE			  =batch_size
										_SCREAMING_SNAKE_CASE			  =num_channels
										_SCREAMING_SNAKE_CASE			  =is_training
										_SCREAMING_SNAKE_CASE			  =use_labels
										_SCREAMING_SNAKE_CASE			  =hidden_dropout_prob
										_SCREAMING_SNAKE_CASE			  =attention_probs_dropout_prob
										_SCREAMING_SNAKE_CASE			  =num_labels
										_SCREAMING_SNAKE_CASE			  =image_size
										_SCREAMING_SNAKE_CASE			  =layer_depths
										_SCREAMING_SNAKE_CASE			  =embed_dims




						def 							__UpperCamelCase				(       self					:      Any  )							->     List[str]:


										"""simple docstring"""





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

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

										_SCREAMING_SNAKE_CASE			  =self.get_config()

										return config, pixel_values, labels




						def 							__UpperCamelCase				(       self					:      Dict  )							->     Tuple:


										"""simple docstring"""





										return SwiftFormerConfig(
										    depths=self.layer_depths				,    embed_dims=self.embed_dims				,    mlp_ratio=4				,    downsamples=[True, True, True, True]				,    hidden_act='''gelu'''				,    num_labels=self.num_labels				,    down_patch_size=3				,    down_stride=2				,    down_pad=1				,    drop_rate=0.0				,    drop_path_rate=0.0				,    use_layer_scale=_a				,    layer_scale_init_value=1E-5				,    )




						def 							__UpperCamelCase				(       self					:      Dict				,    _a					:      List[str]				,    _a					:      str				,    _a					:      Any  )							->     str:


										"""simple docstring"""





										_SCREAMING_SNAKE_CASE			  =SwiftFormerModel(config=_a  )
										model.to(_a  )
										model.eval()
										_SCREAMING_SNAKE_CASE			  =model(_a  )
										self.parent.assertEqual(result.last_hidden_state.shape				,    (self.batch_size, self.embed_dims[-1], 7, 7)  )




						def 							__UpperCamelCase				(       self					:      Optional[int]				,    _a					:      Optional[int]				,    _a					:      int				,    _a					:      Dict  )							->     Tuple:


										"""simple docstring"""





										_SCREAMING_SNAKE_CASE			  =self.num_labels
										_SCREAMING_SNAKE_CASE			  =SwiftFormerForImageClassification(_a  )
										model.to(_a  )
										model.eval()
										_SCREAMING_SNAKE_CASE			  =model(_a				,    labels=_a  )
										self.parent.assertEqual(result.logits.shape				,    (self.batch_size, self.num_labels)  )

										_SCREAMING_SNAKE_CASE			  =SwiftFormerForImageClassification(_a  )
										model.to(_a  )
										model.eval()

										_SCREAMING_SNAKE_CASE			  =floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]  )
										_SCREAMING_SNAKE_CASE			  =model(_a  )
										self.parent.assertEqual(result.logits.shape				,    (self.batch_size, self.num_labels)  )




						def 							__UpperCamelCase				(       self					:      int  )							->     Dict:


										"""simple docstring"""





										((_SCREAMING_SNAKE_CASE)  ,				(_SCREAMING_SNAKE_CASE)  ,				(_SCREAMING_SNAKE_CASE))			  =self.prepare_config_and_inputs()
										_SCREAMING_SNAKE_CASE			  ={'''pixel_values''': pixel_values}
										return config, inputs_dict

@require_torch
class 						A__							(		UpperCamelCase__   ,     UpperCamelCase__   ,     unittest.TestCase  ):
						UpperCAmelCase             =						(SwiftFormerModel, SwiftFormerForImageClassification) if is_torch_available() else ()
						UpperCAmelCase             =						(
						    {"feature-extraction": SwiftFormerModel, "image-classification": SwiftFormerForImageClassification}
						    if is_torch_available()
						    else {}
						)

						UpperCAmelCase             =						False
						UpperCAmelCase             =						False
						UpperCAmelCase             =						False
						UpperCAmelCase             =						False
						UpperCAmelCase             =						False




						def 							__UpperCamelCase				(       self					:      Dict  )							->     Union[str, Any]:


										"""simple docstring"""





										_SCREAMING_SNAKE_CASE			  =SwiftFormerModelTester(self  )
										_SCREAMING_SNAKE_CASE			  =ConfigTester(
										    self				,    config_class=_a				,    has_text_modality=_a				,    hidden_size=37				,    num_attention_heads=12				,    num_hidden_layers=12				,    )




						def 							__UpperCamelCase				(       self					:      int  )							->     List[Any]:


										"""simple docstring"""





										self.config_tester.run_common_tests()




						@unittest.skip(reason='''SwiftFormer does not use inputs_embeds'''  )
						def 							__UpperCamelCase				(       self					:      int  )							->     Dict:


										"""simple docstring"""





										pass




						def 							__UpperCamelCase				(       self					:      Union[str, Any]  )							->     Union[str, Any]:


										"""simple docstring"""





										_SCREAMING_SNAKE_CASE  ,				_SCREAMING_SNAKE_CASE			  =self.model_tester.prepare_config_and_inputs_for_common()

										for model_class in self.all_model_classes:
														_SCREAMING_SNAKE_CASE			  =model_class(_a  )
														_SCREAMING_SNAKE_CASE			  =model.get_output_embeddings()
														self.assertTrue(x is None or isinstance(_a				,    nn.Linear  )  )




						def 							__UpperCamelCase				(       self					:      Union[str, Any]  )							->     List[str]:


										"""simple docstring"""





										_SCREAMING_SNAKE_CASE  ,				_SCREAMING_SNAKE_CASE			  =self.model_tester.prepare_config_and_inputs_for_common()

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

														_SCREAMING_SNAKE_CASE			  =['''pixel_values''']
														self.assertListEqual(arg_names[:1]				,    _a  )




						def 							__UpperCamelCase				(       self					:      Union[str, Any]  )							->     str:


										"""simple docstring"""





										_SCREAMING_SNAKE_CASE			  =self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_model(*_a  )




						def 							__UpperCamelCase				(       self					:      str  )							->     Optional[int]:


										"""simple docstring"""





										_SCREAMING_SNAKE_CASE			  =self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_for_image_classification(*_a  )




						@slow
						def 							__UpperCamelCase				(       self					:      List[Any]  )							->     Optional[Any]:


										"""simple docstring"""





										for model_name in SWIFTFORMER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
														_SCREAMING_SNAKE_CASE			  =SwiftFormerModel.from_pretrained(_a  )
														self.assertIsNotNone(_a  )




						@unittest.skip(reason='''SwiftFormer does not output attentions'''  )
						def 							__UpperCamelCase				(       self					:      Optional[Any]  )							->     Tuple:


										"""simple docstring"""





										pass




						def 							__UpperCamelCase				(       self					:      Dict  )							->     List[Any]:


										"""simple docstring"""





										def check_hidden_states_output(_a					:      int				,    _a					:      List[Any]				,    _a					:      int  ):
														_SCREAMING_SNAKE_CASE			  =model_class(_a  )
														model.to(_a  )
														model.eval()

														with torch.no_grad():
																		_SCREAMING_SNAKE_CASE			  =model(**self._prepare_for_class(_a				,    _a  )  )

														_SCREAMING_SNAKE_CASE			  =outputs.hidden_states

														_SCREAMING_SNAKE_CASE			  =8
														self.assertEqual(len(_a  )				,    _a  )  # TODO

														# SwiftFormer's feature maps are of shape (batch_size, embed_dims, height, width)
														# with the width and height being successively divided by 2, after every 2 blocks
														for i in range(len(_a  )  ):
																		self.assertEqual(
																		    hidden_states[i].shape				,    torch.Size(
																		        [
																		            self.model_tester.batch_size,
																		            self.model_tester.embed_dims[i // 2],
																		            (self.model_tester.image_size // 4) // 2 ** (i // 2),
																		            (self.model_tester.image_size // 4) // 2 ** (i // 2),
																		        ]  )				,    )

										_SCREAMING_SNAKE_CASE  ,				_SCREAMING_SNAKE_CASE			  =self.model_tester.prepare_config_and_inputs_for_common()

										for model_class in self.all_model_classes:
														_SCREAMING_SNAKE_CASE			  =True
														check_hidden_states_output(_a				,    _a				,    _a  )

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

														check_hidden_states_output(_a				,    _a				,    _a  )




						def 							__UpperCamelCase				(       self					:      Tuple  )							->     Dict:


										"""simple docstring"""





										def _config_zero_init(_a					:      Optional[int]  ):
														_SCREAMING_SNAKE_CASE			  =copy.deepcopy(_a  )
														for key in configs_no_init.__dict__.keys():
																		if "_range" in key or "_std" in key or "initializer_factor" in key or "layer_scale" in key:
																						setattr(_a				,    _a				,    1E-10  )
																		if isinstance(getattr(_a				,    _a				,    _a  )				,    _a  ):
																						_SCREAMING_SNAKE_CASE			  =_config_zero_init(getattr(_a				,    _a  )  )
																						setattr(_a				,    _a				,    _a  )
														return configs_no_init

										_SCREAMING_SNAKE_CASE  ,				_SCREAMING_SNAKE_CASE			  =self.model_tester.prepare_config_and_inputs_for_common()

										_SCREAMING_SNAKE_CASE			  =_config_zero_init(_a  )
										for model_class in self.all_model_classes:
														_SCREAMING_SNAKE_CASE			  =model_class(config=_a  )
														for name, param in model.named_parameters():
																		if param.requires_grad:
																						self.assertIn(
																						    ((param.data.mean() * 1E9) / 1E9).round().item()				,    [0.0, 1.0]				,    msg=f"Parameter {name} of model {model_class} seems not properly initialized"				,    )




						@unittest.skip('''Will be fixed soon by reducing the size of the model used for common tests.'''  )
						def 							__UpperCamelCase				(       self					:      Optional[Any]  )							->     Tuple:


										"""simple docstring"""





										pass







def 						lowerCamelCase(   ):
				_SCREAMING_SNAKE_CASE			  =Image.open('''./tests/fixtures/tests_samples/COCO/000000039769.png''')
				return image


@require_torch
@require_vision
class 						A__							(		unittest.TestCase  ):




						@cached_property
						def 							__UpperCamelCase				(       self					:      Optional[Any]  )							->     Union[str, Any]:


										"""simple docstring"""





										return ViTImageProcessor.from_pretrained('''MBZUAI/swiftformer-xs'''  ) if is_vision_available() else None




						@slow
						def 							__UpperCamelCase				(       self					:      Dict  )							->     List[Any]:


										"""simple docstring"""





										_SCREAMING_SNAKE_CASE			  =SwiftFormerForImageClassification.from_pretrained('''MBZUAI/swiftformer-xs'''  ).to(_a  )

										_SCREAMING_SNAKE_CASE			  =self.default_image_processor
										_SCREAMING_SNAKE_CASE			  =prepare_img()
										_SCREAMING_SNAKE_CASE			  =image_processor(images=_a				,    return_tensors='''pt'''  ).to(_a  )

										# forward pass
										with torch.no_grad():
														_SCREAMING_SNAKE_CASE			  =model(**_a  )

										# verify the logits
										_SCREAMING_SNAKE_CASE			  =torch.Size((1, 1000)  )
										self.assertEqual(outputs.logits.shape				,    _a  )

										_SCREAMING_SNAKE_CASE			  =torch.tensor([[-2.17_03E00, 2.11_07E00, -2.08_11E00]]  ).to(_a  )
										self.assertTrue(torch.allclose(outputs.logits[0, :3]				,    _a				,    atol=1E-4  )  )



import itertools
from dataclasses import dataclass
from typing import Any, Callable, Dict, List, Optional, Union

import pandas as pd
import pyarrow as pa

import datasets
import datasets.config
from datasets.features.features import require_storage_cast
from datasets.table import table_cast
from datasets.utils.py_utils import Literal


snake_case_						:			Any													=							datasets.utils.logging.get_logger(__name__)

snake_case_						:			List[Any]													=							['''names''', '''prefix''']
snake_case_						:			Dict													=							['''warn_bad_lines''', '''error_bad_lines''', '''mangle_dupe_cols''']
snake_case_						:			Dict													=							['''encoding_errors''', '''on_bad_lines''']
snake_case_						:			Optional[Any]													=							['''date_format''']

@dataclass
class 						A__							(		datasets.BuilderConfig  ):
						UpperCAmelCase             =						","
						UpperCAmelCase             =						None
						UpperCAmelCase             =						"infer"
						UpperCAmelCase             =						None
						UpperCAmelCase             =						None
						UpperCAmelCase             =						None
						UpperCAmelCase             =						None
						UpperCAmelCase             =						None
						UpperCAmelCase             =						True
						UpperCAmelCase             =						None
						UpperCAmelCase             =						None
						UpperCAmelCase             =						None
						UpperCAmelCase             =						None
						UpperCAmelCase             =						False
						UpperCAmelCase             =						None
						UpperCAmelCase             =						None
						UpperCAmelCase             =						None
						UpperCAmelCase             =						True
						UpperCAmelCase             =						True
						UpperCAmelCase             =						False
						UpperCAmelCase             =						True
						UpperCAmelCase             =						None
						UpperCAmelCase             =						"."
						UpperCAmelCase             =						None
						UpperCAmelCase             =						'"'
						UpperCAmelCase             =						0
						UpperCAmelCase             =						None
						UpperCAmelCase             =						None
						UpperCAmelCase             =						None
						UpperCAmelCase             =						None
						UpperCAmelCase             =						True
						UpperCAmelCase             =						True
						UpperCAmelCase             =						0
						UpperCAmelCase             =						True
						UpperCAmelCase             =						False
						UpperCAmelCase             =						None
						UpperCAmelCase             =						10000
						UpperCAmelCase             =						None
						UpperCAmelCase             =						"strict"
						UpperCAmelCase             =						"error"
						UpperCAmelCase             =						None




						def 							__UpperCamelCase				(       self					:      int  )							->     Union[str, Any]:


										"""simple docstring"""





										if self.delimiter is not None:
														_SCREAMING_SNAKE_CASE			  =self.delimiter
										if self.column_names is not None:
														_SCREAMING_SNAKE_CASE			  =self.column_names




						@property
						def 							__UpperCamelCase				(       self					:      Dict  )							->     List[Any]:


										"""simple docstring"""





										_SCREAMING_SNAKE_CASE			  ={
										    '''sep''': self.sep,
										    '''header''': self.header,
										    '''names''': self.names,
										    '''index_col''': self.index_col,
										    '''usecols''': self.usecols,
										    '''prefix''': self.prefix,
										    '''mangle_dupe_cols''': self.mangle_dupe_cols,
										    '''engine''': self.engine,
										    '''converters''': self.converters,
										    '''true_values''': self.true_values,
										    '''false_values''': self.false_values,
										    '''skipinitialspace''': self.skipinitialspace,
										    '''skiprows''': self.skiprows,
										    '''nrows''': self.nrows,
										    '''na_values''': self.na_values,
										    '''keep_default_na''': self.keep_default_na,
										    '''na_filter''': self.na_filter,
										    '''verbose''': self.verbose,
										    '''skip_blank_lines''': self.skip_blank_lines,
										    '''thousands''': self.thousands,
										    '''decimal''': self.decimal,
										    '''lineterminator''': self.lineterminator,
										    '''quotechar''': self.quotechar,
										    '''quoting''': self.quoting,
										    '''escapechar''': self.escapechar,
										    '''comment''': self.comment,
										    '''encoding''': self.encoding,
										    '''dialect''': self.dialect,
										    '''error_bad_lines''': self.error_bad_lines,
										    '''warn_bad_lines''': self.warn_bad_lines,
										    '''skipfooter''': self.skipfooter,
										    '''doublequote''': self.doublequote,
										    '''memory_map''': self.memory_map,
										    '''float_precision''': self.float_precision,
										    '''chunksize''': self.chunksize,
										    '''encoding_errors''': self.encoding_errors,
										    '''on_bad_lines''': self.on_bad_lines,
										    '''date_format''': self.date_format,
										}

										# some kwargs must not be passed if they don't have a default value
										# some others are deprecated and we can also not pass them if they are the default value
										for pd_read_csv_parameter in _PANDAS_READ_CSV_NO_DEFAULT_PARAMETERS + _PANDAS_READ_CSV_DEPRECATED_PARAMETERS:
														if pd_read_csv_kwargs[pd_read_csv_parameter] == getattr(CsvConfig()				,    _a  ):
																		del pd_read_csv_kwargs[pd_read_csv_parameter]

        # Remove 2.0 new arguments
										if not (datasets.config.PANDAS_VERSION.major >= 2):
														for pd_read_csv_parameter in _PANDAS_READ_CSV_NEW_2_0_0_PARAMETERS:
																		del pd_read_csv_kwargs[pd_read_csv_parameter]

        # Remove 1.3 new arguments
										if not (datasets.config.PANDAS_VERSION.major >= 1 and datasets.config.PANDAS_VERSION.minor >= 3):
														for pd_read_csv_parameter in _PANDAS_READ_CSV_NEW_1_3_0_PARAMETERS:
																		del pd_read_csv_kwargs[pd_read_csv_parameter]

										return pd_read_csv_kwargs

class 						A__							(		datasets.ArrowBasedBuilder  ):
						UpperCAmelCase             =						CsvConfig




						def 							__UpperCamelCase				(       self					:      Optional[int]  )							->     int:


										"""simple docstring"""





										return datasets.DatasetInfo(features=self.config.features  )




						def 							__UpperCamelCase				(       self					:      Dict				,    _a					:      str  )							->     List[str]:


										"""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}"  )
										_SCREAMING_SNAKE_CASE			  =dl_manager.download_and_extract(self.config.data_files  )
										if isinstance(_a				,    (str, list, tuple)  ):
														_SCREAMING_SNAKE_CASE			  =data_files
														if isinstance(_a				,    _a  ):
																		_SCREAMING_SNAKE_CASE			  =[files]
														_SCREAMING_SNAKE_CASE			  =[dl_manager.iter_files(_a  ) for file in files]
														return [datasets.SplitGenerator(name=datasets.Split.TRAIN				,    gen_kwargs={'''files''': files}  )]
										_SCREAMING_SNAKE_CASE			  =[]
										for split_name, files in data_files.items():
														if isinstance(_a				,    _a  ):
																		_SCREAMING_SNAKE_CASE			  =[files]
														_SCREAMING_SNAKE_CASE			  =[dl_manager.iter_files(_a  ) for file in files]
														splits.append(datasets.SplitGenerator(name=_a				,    gen_kwargs={'''files''': files}  )  )
										return splits




						def 							__UpperCamelCase				(       self					:      Tuple				,    _a					:      pa.Table  )							->     pa.Table:


										"""simple docstring"""





										if self.config.features is not None:
														_SCREAMING_SNAKE_CASE			  =self.config.features.arrow_schema
														if all(not require_storage_cast(_a  ) for feature in self.config.features.values()  ):
																		# cheaper cast
																		_SCREAMING_SNAKE_CASE			  =pa.Table.from_arrays([pa_table[field.name] for field in schema]				,    schema=_a  )
														else:
																		# more expensive cast; allows str <-> int/float or str to Audio for example
																		_SCREAMING_SNAKE_CASE			  =table_cast(_a				,    _a  )
										return pa_table




						def 							__UpperCamelCase				(       self					:      str				,    _a					:      Union[str, Any]  )							->     int:


										"""simple docstring"""





										_SCREAMING_SNAKE_CASE			  =self.config.features.arrow_schema if self.config.features else None
										# dtype allows reading an int column as str
										_SCREAMING_SNAKE_CASE			  =(
										    {
										        name: dtype.to_pandas_dtype() if not require_storage_cast(_a  ) else object
										        for name, dtype, feature in zip(schema.names				,    schema.types				,    self.config.features.values()  )
										    }
										    if schema is not None
										    else None
										)
										for file_idx, file in enumerate(itertools.chain.from_iterable(_a  )  ):
														_SCREAMING_SNAKE_CASE			  =pd.read_csv(_a				,    iterator=_a				,    dtype=_a				,    **self.config.pd_read_csv_kwargs  )
														try:
																		for batch_idx, df in enumerate(_a  ):
																						_SCREAMING_SNAKE_CASE			  =pa.Table.from_pandas(_a  )
																						# 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(_a  )
														except ValueError as e:
																		logger.error(f"Failed to read file '{file}' with error {type(_a  )}: {e}"  )
																		raise