




import importlib
import inspect
from functools import wraps
from typing import TYPE_CHECKING, Optional, Union

from .download.streaming_download_manager import (
    xbasename,
    xdirname,
    xet_parse,
    xexists,
    xgetsize,
    xglob,
    xgzip_open,
    xisdir,
    xisfile,
    xjoin,
    xlistdir,
    xnumpy_load,
    xopen,
    xpandas_read_csv,
    xpandas_read_excel,
    xPath,
    xrelpath,
    xsio_loadmat,
    xsplit,
    xsplitext,
    xwalk,
    xxml_dom_minidom_parse,
)
from .utils.logging import get_logger
from .utils.patching import patch_submodule
from .utils.py_utils import get_imports


__SCREAMING_SNAKE_CASE			:      Optional[int]      			=get_logger(__name__)


if TYPE_CHECKING:
      from .builder import DatasetBuilder

def 	lowerCAmelCase_	(     lowerCamelCase:				Union[str, Any]   , lowerCamelCase:				Optional[Union[str, bool]] = None  )    ->	Optional[int]:


      '''simple docstring'''




      lowerCamelCase__					     = importlib.import_module(UpperCamelCase_  )

      # TODO(QL): always update the module to add subsequent new authentication
      if hasattr(UpperCamelCase_   , '_patched_for_streaming'  ) and module._patched_for_streaming:
            return

      def wrap_auth(lowerCamelCase:				str  ):
            @wraps(UpperCamelCase_  )
            def wrapper(*lowerCamelCase:				Dict   , **lowerCamelCase:				str  ):
                  return function(*UpperCamelCase_   , use_auth_token=UpperCamelCase_   , **UpperCamelCase_  )

            lowerCamelCase__					     = 'wrap_auth'
            return wrapper

      # open files in a streaming fashion
      patch_submodule(UpperCamelCase_   , 'open'   , wrap_auth(UpperCamelCase_  )  ).start()
      patch_submodule(UpperCamelCase_   , 'os.listdir'   , wrap_auth(UpperCamelCase_  )  ).start()
      patch_submodule(UpperCamelCase_   , 'os.walk'   , wrap_auth(UpperCamelCase_  )  ).start()
      patch_submodule(UpperCamelCase_   , 'glob.glob'   , wrap_auth(UpperCamelCase_  )  ).start()
      # allow to navigate in remote zip files
      patch_submodule(UpperCamelCase_   , 'os.path.join'   , UpperCamelCase_  ).start()
      patch_submodule(UpperCamelCase_   , 'os.path.dirname'   , UpperCamelCase_  ).start()
      patch_submodule(UpperCamelCase_   , 'os.path.basename'   , UpperCamelCase_  ).start()
      patch_submodule(UpperCamelCase_   , 'os.path.relpath'   , UpperCamelCase_  ).start()
      patch_submodule(UpperCamelCase_   , 'os.path.split'   , UpperCamelCase_  ).start()
      patch_submodule(UpperCamelCase_   , 'os.path.splitext'   , UpperCamelCase_  ).start()
      # allow checks on paths
      patch_submodule(UpperCamelCase_   , 'os.path.exists'   , wrap_auth(UpperCamelCase_  )  ).start()
      patch_submodule(UpperCamelCase_   , 'os.path.isdir'   , wrap_auth(UpperCamelCase_  )  ).start()
      patch_submodule(UpperCamelCase_   , 'os.path.isfile'   , wrap_auth(UpperCamelCase_  )  ).start()
      patch_submodule(UpperCamelCase_   , 'os.path.getsize'   , wrap_auth(UpperCamelCase_  )  ).start()
      patch_submodule(UpperCamelCase_   , 'pathlib.Path'   , UpperCamelCase_  ).start()
      # file readers
      patch_submodule(UpperCamelCase_   , 'gzip.open'   , wrap_auth(UpperCamelCase_  )  ).start()
      patch_submodule(UpperCamelCase_   , 'numpy.load'   , wrap_auth(UpperCamelCase_  )  ).start()
      patch_submodule(UpperCamelCase_   , 'pandas.read_csv'   , wrap_auth(UpperCamelCase_  )   , attrs=['__version__']  ).start()
      patch_submodule(UpperCamelCase_   , 'pandas.read_excel'   , wrap_auth(UpperCamelCase_  )   , attrs=['__version__']  ).start()
      patch_submodule(UpperCamelCase_   , 'scipy.io.loadmat'   , wrap_auth(UpperCamelCase_  )   , attrs=['__version__']  ).start()
      patch_submodule(UpperCamelCase_   , 'xml.etree.ElementTree.parse'   , wrap_auth(UpperCamelCase_  )  ).start()
      patch_submodule(UpperCamelCase_   , 'xml.dom.minidom.parse'   , wrap_auth(UpperCamelCase_  )  ).start()
      lowerCamelCase__					     = True


def 	lowerCAmelCase_	(     lowerCamelCase:				"DatasetBuilder"  )    ->	Tuple:


      '''simple docstring'''




      extend_module_for_streaming(builder.__module__   , use_auth_token=builder.use_auth_token  )
      # if needed, we also have to extend additional internal imports (like wmt14 -> wmt_utils)
      if not builder.__module__.startswith('datasets.'  ):  # check that it's not a packaged builder like csv
            for imports in get_imports(inspect.getfile(builder.__class__  )  ):
                  if imports[0] == "internal":
                        lowerCamelCase__					     = imports[1]
                        lowerCamelCase__					     = '.'.join(builder.__module__.split('.'  )[:-1] + [internal_import_name]  )
                        extend_module_for_streaming(UpperCamelCase_   , use_auth_token=builder.use_auth_token  )

    # builders can inherit from other builders that might use streaming functionality
    # (for example, ImageFolder and AudioFolder inherit from FolderBuilder which implements examples generation)
    # but these parents builders are not patched automatically as they are not instantiated, so we patch them here
      from .builder import DatasetBuilder

      lowerCamelCase__					     = [
          cls.__module__
          for cls in type(UpperCamelCase_  ).__mro__[1:]  # make sure it's not the same module we've already patched
          if issubclass(cls   , UpperCamelCase_  ) and cls.__module__ != DatasetBuilder.__module__
      ]  # check it's not a standard builder from datasets.builder
      for module in parent_builder_modules:
            extend_module_for_streaming(UpperCamelCase_   , use_auth_token=builder.use_auth_token  )







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 			__SCREAMING_SNAKE_CASE    (      __lowercase):






						def 					UpperCamelCase__					(		self      ):






												"""simple docstring"""
												lowerCAmelCase__			  =  self.config_class(**self.inputs_dict      )
												self.parent.assertTrue(hasattr(_UpperCamelCase   ,  'hidden_sizes'      )      )
												self.parent.assertTrue(hasattr(_UpperCamelCase   ,  'num_encoder_blocks'      )      )




class 			__SCREAMING_SNAKE_CASE    :






						def __init__(		self   ,  _UpperCamelCase   ,  _UpperCamelCase=13   ,  _UpperCamelCase=64   ,  _UpperCamelCase=3   ,  _UpperCamelCase=4   ,  _UpperCamelCase=[2, 2, 2, 2]   ,  _UpperCamelCase=[8, 4, 2, 1]   ,  _UpperCamelCase=[16, 32, 64, 1_28]   ,  _UpperCamelCase=[1, 4, 8, 16]   ,  _UpperCamelCase=False   ,  _UpperCamelCase=True   ,  _UpperCamelCase="gelu"   ,  _UpperCamelCase=0.1   ,  _UpperCamelCase=0.02   ,  _UpperCamelCase=3   ,  _UpperCamelCase=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 					UpperCamelCase__					(		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 					UpperCamelCase__					(		self   ,  _UpperCamelCase   ,  _UpperCamelCase   ,  _UpperCamelCase      ):






												"""simple docstring"""
												lowerCAmelCase__			  =  PoolFormerModel(config=_UpperCamelCase      )
												model.to(_UpperCamelCase      )
												model.eval()
												lowerCAmelCase__			  =  model(_UpperCamelCase      )
												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 					UpperCamelCase__					(		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 			__SCREAMING_SNAKE_CASE    (      __lowercase						,    __lowercase						,    unittest.TestCase):
						_SCREAMING_SNAKE_CASE		:				List[Any]            =    (PoolFormerModel, PoolFormerForImageClassification) if is_torch_available() else ()
						_SCREAMING_SNAKE_CASE		:				Optional[int]            =    (
						    {'''feature-extraction''': PoolFormerModel, '''image-classification''': PoolFormerForImageClassification}
						    if is_torch_available()
						    else {}
						)

						_SCREAMING_SNAKE_CASE		:				List[str]            =    False
						_SCREAMING_SNAKE_CASE		:				Optional[int]            =    False
						_SCREAMING_SNAKE_CASE		:				Optional[Any]            =    False
						_SCREAMING_SNAKE_CASE		:				str            =    False
						_SCREAMING_SNAKE_CASE		:				List[Any]            =    False






						def 					UpperCamelCase__					(		self      ):






												"""simple docstring"""
												lowerCAmelCase__			  =  PoolFormerModelTester(self      )
												lowerCAmelCase__			  =  PoolFormerConfigTester(self   ,  config_class=_UpperCamelCase      )






						def 					UpperCamelCase__					(		self      ):






												"""simple docstring"""
												self.config_tester.run_common_tests()






						def 					UpperCamelCase__					(		self      ):






												"""simple docstring"""
												lowerCAmelCase__			  =  self.model_tester.prepare_config_and_inputs()
												self.model_tester.create_and_check_model(*_UpperCamelCase      )






						@unittest.skip('PoolFormer does not use inputs_embeds'      )
						def 					UpperCamelCase__					(		self      ):






												"""simple docstring"""
												pass






						@unittest.skip('PoolFormer does not have get_input_embeddings method and get_output_embeddings methods'      )
						def 					UpperCamelCase__					(		self      ):






												"""simple docstring"""
												pass






						def 					UpperCamelCase__					(		self      ):






												"""simple docstring"""
												def check_hidden_states_output(_UpperCamelCase   ,  _UpperCamelCase   ,  _UpperCamelCase      ):
																		lowerCAmelCase__			  =  model_class(_UpperCamelCase      )
																		model.to(_UpperCamelCase      )
																		model.eval()

																		with torch.no_grad():
																								lowerCAmelCase__			  =  model(**self._prepare_for_class(_UpperCamelCase   ,  _UpperCamelCase      )      )

																		lowerCAmelCase__			  =  outputs.hidden_states

																		lowerCAmelCase__			  =  self.model_tester.num_encoder_blocks
																		self.assertEqual(len(_UpperCamelCase      )   ,  _UpperCamelCase      )

																		# 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(_UpperCamelCase   ,  _UpperCamelCase   ,  _UpperCamelCase      )

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

																		check_hidden_states_output(_UpperCamelCase   ,  _UpperCamelCase   ,  _UpperCamelCase      )






						def 					UpperCamelCase__					(		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(_UpperCamelCase      ):
																								continue
																		lowerCAmelCase__			  =  model_class(_UpperCamelCase      )
																		model.to(_UpperCamelCase      )
																		model.train()
																		lowerCAmelCase__			  =  self._prepare_for_class(_UpperCamelCase   ,  _UpperCamelCase   ,  return_labels=_UpperCamelCase      )
																		lowerCAmelCase__			  =  model(**_UpperCamelCase      ).loss
																		loss.backward()






						def 					UpperCamelCase__					(		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(_UpperCamelCase      )
																		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]   ,  _UpperCamelCase      )







						@slow
						def 					UpperCamelCase__					(		self      ):






												"""simple docstring"""
												for model_name in POOLFORMER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
																		lowerCAmelCase__			  =  PoolFormerModel.from_pretrained(_UpperCamelCase      )
																		self.assertIsNotNone(_UpperCamelCase      )







def 		_UpperCamelCase      (			)     ->      Optional[int]:

						"""simple docstring"""



						lowerCAmelCase__			  =  Image.open('./tests/fixtures/tests_samples/COCO/000000039769.png'						)
						return image


@require_torch
class 			__SCREAMING_SNAKE_CASE    (      unittest.TestCase):






						@slow
						def 					UpperCamelCase__					(		self      ):






												"""simple docstring"""
												lowerCAmelCase__			  =  PoolFormerImageProcessor()
												lowerCAmelCase__			  =  PoolFormerForImageClassification.from_pretrained('sail/poolformer_s12'      ).to(_UpperCamelCase      )

												lowerCAmelCase__			  =  image_processor(images=prepare_img()   ,  return_tensors='pt'      ).to(_UpperCamelCase      )

												# forward pass
												with torch.no_grad():
																		lowerCAmelCase__			  =  model(**_UpperCamelCase      )

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

												lowerCAmelCase__			  =  torch.tensor([-0.61_13, 0.16_85, -0.04_92]      ).to(_UpperCamelCase      )
												self.assertTrue(torch.allclose(outputs.logits[0, :3]   ,  _UpperCamelCase   ,  atol=1E-4      )      )





