





import inspect
import unittest

from transformers import ConvNextConfig
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_backbone_common import BackboneTesterMixin
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 ConvNextBackbone, ConvNextForImageClassification, ConvNextModel
  from transformers.models.convnext.modeling_convnext import CONVNEXT_PRETRAINED_MODEL_ARCHIVE_LIST


if is_vision_available():
  from PIL import Image

  from transformers import AutoImageProcessor






class     lowercase__       :

   '''simple docstring'''


   def __init__(    self,		__magic_name__,		__magic_name__=13,		__magic_name__=32,		__magic_name__=3,		__magic_name__=4,		__magic_name__=[10, 20, 30, 40],		__magic_name__=[2, 2, 3, 2],		__magic_name__=True,		__magic_name__=True,		__magic_name__=37,		__magic_name__="gelu",		__magic_name__=10,		__magic_name__=0.02,		__magic_name__=["stage2", "stage3", "stage4"],		__magic_name__=[2, 3, 4],		__magic_name__=None,		) ->      str:






       """simple docstring"""
       UpperCamelCase__							:				int          =      parent
       UpperCamelCase__							:				str          =      batch_size
       UpperCamelCase__							:				Dict          =      image_size
       UpperCamelCase__							:				List[Any]          =      num_channels
       UpperCamelCase__							:				Any          =      num_stages
       UpperCamelCase__							:				Tuple          =      hidden_sizes
       UpperCamelCase__							:				Dict          =      depths
       UpperCamelCase__							:				Any          =      is_training
       UpperCamelCase__							:				List[str]          =      use_labels
       UpperCamelCase__							:				List[Any]          =      intermediate_size
       UpperCamelCase__							:				Tuple          =      hidden_act
       UpperCamelCase__							:				Optional[int]          =      num_labels
       UpperCamelCase__							:				Optional[Any]          =      initializer_range
       UpperCamelCase__							:				List[Any]          =      out_features
       UpperCamelCase__							:				int          =      out_indices
       UpperCamelCase__							:				List[Any]          =      scope




   def       UpperCamelCase__  (    self							) ->      Any:






       """simple docstring"""
       UpperCamelCase__							:				Union[str, Any]          =      floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]							)

       UpperCamelCase__							:				Union[str, Any]          =      None
       if self.use_labels:
           UpperCamelCase__							:				int          =      ids_tensor([self.batch_size],		self.num_labels							)

       UpperCamelCase__							:				Dict          =      self.get_config()
       return config, pixel_values, labels




   def       UpperCamelCase__  (    self							) ->      Dict:






       """simple docstring"""
       return ConvNextConfig(
           num_channels=self.num_channels,		hidden_sizes=self.hidden_sizes,		depths=self.depths,		num_stages=self.num_stages,		hidden_act=self.hidden_act,		is_decoder=a__,		initializer_range=self.initializer_range,		out_features=self.out_features,		out_indices=self.out_indices,		num_labels=self.num_labels,		)




   def       UpperCamelCase__  (    self,		__magic_name__,		__magic_name__,		__magic_name__							) ->      Dict:






       """simple docstring"""
       UpperCamelCase__							:				Dict          =      ConvNextModel(config=a__							)
       model.to(a__							)
       model.eval()
       UpperCamelCase__							:				Tuple          =      model(a__							)
       # expected last hidden states: B, C, H // 32, W // 32
       self.parent.assertEqual(
           result.last_hidden_state.shape,		(self.batch_size, self.hidden_sizes[-1], self.image_size // 32, self.image_size // 32),		)




   def       UpperCamelCase__  (    self,		__magic_name__,		__magic_name__,		__magic_name__							) ->      Optional[Any]:






       """simple docstring"""
       UpperCamelCase__							:				Optional[Any]          =      ConvNextForImageClassification(a__							)
       model.to(a__							)
       model.eval()
       UpperCamelCase__							:				List[Any]          =      model(a__,		labels=a__							)
       self.parent.assertEqual(result.logits.shape,		(self.batch_size, self.num_labels)							)




   def       UpperCamelCase__  (    self,		__magic_name__,		__magic_name__,		__magic_name__							) ->      Tuple:






       """simple docstring"""
       UpperCamelCase__							:				List[str]          =      ConvNextBackbone(config=a__							)
       model.to(a__							)
       model.eval()
       UpperCamelCase__							:				str          =      model(a__							)

       # verify hidden states
       self.parent.assertEqual(len(result.feature_maps							),		len(config.out_features							)							)
       self.parent.assertListEqual(list(result.feature_maps[0].shape							),		[self.batch_size, self.hidden_sizes[1], 4, 4]							)

       # verify channels
       self.parent.assertEqual(len(model.channels							),		len(config.out_features							)							)
       self.parent.assertListEqual(model.channels,		config.hidden_sizes[1:]							)

       # verify backbone works with out_features=None
       UpperCamelCase__							:				Dict          =      None
       UpperCamelCase__							:				Optional[int]          =      ConvNextBackbone(config=a__							)
       model.to(a__							)
       model.eval()
       UpperCamelCase__							:				int          =      model(a__							)

       # verify feature maps
       self.parent.assertEqual(len(result.feature_maps							),		1							)
       self.parent.assertListEqual(list(result.feature_maps[0].shape							),		[self.batch_size, self.hidden_sizes[-1], 1, 1]							)

       # verify channels
       self.parent.assertEqual(len(model.channels							),		1							)
       self.parent.assertListEqual(model.channels,		[config.hidden_sizes[-1]]							)




   def       UpperCamelCase__  (    self							) ->      List[str]:






       """simple docstring"""
       UpperCamelCase__							:				Union[str, Any]          =      self.prepare_config_and_inputs()
       UpperCamelCase__							:				Optional[Any]          =      config_and_inputs
       UpperCamelCase__							:				Optional[int]          =      {"""pixel_values""": pixel_values}
       return config, inputs_dict






@require_torch
class     lowercase__       (	SCREAMING_SNAKE_CASE_			,				SCREAMING_SNAKE_CASE_			,				unittest.TestCase				):

   '''simple docstring'''


   a						:							Tuple   		=		(
       (
           ConvNextModel,
           ConvNextForImageClassification,
           ConvNextBackbone,
       )
       if is_torch_available()
       else ()
   )
   a						:							List[Any]   		=		(
       {"feature-extraction": ConvNextModel, "image-classification": ConvNextForImageClassification}
       if is_torch_available()
       else {}
   )

   a						:							Optional[int]   		=		True
   a						:							List[str]   		=		False
   a						:							Dict   		=		False
   a						:							str   		=		False
   a						:							Dict   		=		False




   def       UpperCamelCase__  (    self							) ->      List[str]:






       """simple docstring"""
       UpperCamelCase__							:				Optional[Any]          =      ConvNextModelTester(self							)
       UpperCamelCase__							:				Optional[Any]          =      ConfigTester(self,		config_class=a__,		has_text_modality=a__,		hidden_size=37							)




   def       UpperCamelCase__  (    self							) ->      str:






       """simple docstring"""
       self.create_and_test_config_common_properties()
       self.config_tester.create_and_test_config_to_json_string()
       self.config_tester.create_and_test_config_to_json_file()
       self.config_tester.create_and_test_config_from_and_save_pretrained()
       self.config_tester.create_and_test_config_with_num_labels()
       self.config_tester.check_config_can_be_init_without_params()
       self.config_tester.check_config_arguments_init()




   def       UpperCamelCase__  (    self							) ->      Optional[Any]:






       """simple docstring"""
       return




   @unittest.skip(reason='''ConvNext does not use inputs_embeds'''							)
   def       UpperCamelCase__  (    self							) ->      Optional[int]:






       """simple docstring"""
       pass




   @unittest.skip(reason='''ConvNext does not support input and output embeddings'''							)
   def       UpperCamelCase__  (    self							) ->      Dict:






       """simple docstring"""
       pass




   @unittest.skip(reason='''ConvNext does not use feedforward chunking'''							)
   def       UpperCamelCase__  (    self							) ->      Union[str, Any]:






       """simple docstring"""
       pass




   def       UpperCamelCase__  (    self							) ->      Any:






       """simple docstring"""
       UpperCamelCase__							:				Optional[Any]          =      self.model_tester.prepare_config_and_inputs_for_common()

       for model_class in self.all_model_classes:
           UpperCamelCase__							:				Union[str, Any]          =      model_class(a__							)
           UpperCamelCase__							:				Union[str, Any]          =      inspect.signature(model.forward							)
           # signature.parameters is an OrderedDict => so arg_names order is deterministic
           UpperCamelCase__							:				int          =      [*signature.parameters.keys()]

           UpperCamelCase__							:				Optional[int]          =      ["""pixel_values"""]
           self.assertListEqual(arg_names[:1],		a__							)




   def       UpperCamelCase__  (    self							) ->      List[str]:






       """simple docstring"""
       UpperCamelCase__							:				List[Any]          =      self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_model(*a__							)




   def       UpperCamelCase__  (    self							) ->      Optional[Any]:






       """simple docstring"""
       UpperCamelCase__							:				int          =      self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_backbone(*a__							)




   def       UpperCamelCase__  (    self							) ->      List[Any]:






       """simple docstring"""
       def check_hidden_states_output(__magic_name__,		__magic_name__,		__magic_name__							):
           UpperCamelCase__							:				Dict          =      model_class(a__							)
           model.to(a__							)
           model.eval()

           with torch.no_grad():
               UpperCamelCase__							:				List[str]          =      model(**self._prepare_for_class(a__,		a__							)							)

           UpperCamelCase__							:				Optional[int]          =      outputs.encoder_hidden_states if config.is_encoder_decoder else outputs.hidden_states

           UpperCamelCase__							:				str          =      self.model_tester.num_stages
           self.assertEqual(len(a__							),		expected_num_stages + 1							)

           # ConvNext's feature maps are of shape (batch_size, num_channels, height, width)
           self.assertListEqual(
               list(hidden_states[0].shape[-2:]							),		[self.model_tester.image_size // 4, self.model_tester.image_size // 4],		)

       UpperCamelCase__							:				Optional[Any]          =      self.model_tester.prepare_config_and_inputs_for_common()

       for model_class in self.all_model_classes:
           UpperCamelCase__							:				Optional[int]          =      True
           check_hidden_states_output(a__,		a__,		a__							)

           # check that output_hidden_states also work using config
           del inputs_dict["output_hidden_states"]
           UpperCamelCase__							:				Optional[Any]          =      True

           check_hidden_states_output(a__,		a__,		a__							)




   def       UpperCamelCase__  (    self							) ->      Optional[Any]:






       """simple docstring"""
       UpperCamelCase__							:				Optional[int]          =      self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_for_image_classification(*a__							)




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






       """simple docstring"""
       for model_name in CONVNEXT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
           UpperCamelCase__							:				int          =      ConvNextModel.from_pretrained(a__							)
           self.assertIsNotNone(a__							)




def 		lowerCAmelCase_    (  )      ->	List[str]:
    UpperCamelCase__							:				int          =      Image.open('''./tests/fixtures/tests_samples/COCO/000000039769.png'''      )
    return image






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

   '''simple docstring'''


   @cached_property
   def       UpperCamelCase__  (    self							) ->      int:






       """simple docstring"""
       return AutoImageProcessor.from_pretrained('''facebook/convnext-tiny-224'''							) if is_vision_available() else None




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






       """simple docstring"""
       UpperCamelCase__							:				Optional[Any]          =      ConvNextForImageClassification.from_pretrained('''facebook/convnext-tiny-224'''							).to(a__							)

       UpperCamelCase__							:				Dict          =      self.default_image_processor
       UpperCamelCase__							:				int          =      prepare_img()
       UpperCamelCase__							:				Union[str, Any]          =      image_processor(images=a__,		return_tensors='''pt'''							).to(a__							)

       # forward pass
       with torch.no_grad():
           UpperCamelCase__							:				int          =      model(**a__							)

       # verify the logits
       UpperCamelCase__							:				Union[str, Any]          =      torch.Size((1, 1000)							)
       self.assertEqual(outputs.logits.shape,		a__							)

       UpperCamelCase__							:				Tuple          =      torch.tensor([-0.0260, -0.4739, 0.1911]							).to(a__							)

       self.assertTrue(torch.allclose(outputs.logits[0, :3],		a__,		atol=1E-4							)							)






@require_torch
class     lowercase__       (	unittest.TestCase			,				SCREAMING_SNAKE_CASE_				):

   '''simple docstring'''


   a						:							List[str]   		=		(ConvNextBackbone,) if is_torch_available() else ()
   a						:							int   		=		ConvNextConfig

   a						:							Dict   		=		False




   def       UpperCamelCase__  (    self							) ->      Any:






       """simple docstring"""
       UpperCamelCase__							:				str          =      ConvNextModelTester(self							)


"""simple docstring"""




import itertools
from dataclasses import dataclass
from typing import List, Optional

import pyarrow as pa
import pyarrow.parquet as pq

import datasets
from datasets.table import table_cast


_a      :		Dict           =   datasets.utils.logging.get_logger(__name__)
@dataclass
class 						__A							( datasets.BuilderConfig		):
     _UpperCamelCase							:			int							=		10_000
     _UpperCamelCase							:			Optional[List[str]]							=		None
     _UpperCamelCase							:			Optional[datasets.Features]							=		None







class 						__A							( datasets.ArrowBasedBuilder		):
     _UpperCamelCase							:			List[str]							=		ParquetConfig






     def     __A  (		self  ):
       return datasets.DatasetInfo(features=self.config.features  )






     def     __A  (		self				,     a__  ):
       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						:       Optional[Any]  			=     dl_manager.download_and_extract(self.config.data_files  )
       if isinstance(a__				,     (str, list, tuple)  ):
         _lowerCAmelCase						:       Any  			=     data_files
         if isinstance(a__				,     a__  ):
           _lowerCAmelCase						:       Tuple  			=     [files]
         # Use `dl_manager.iter_files` to skip hidden files in an extracted archive
         _lowerCAmelCase						:       Any  			=     [dl_manager.iter_files(a__  ) for file in files]
         return [datasets.SplitGenerator(name=datasets.Split.TRAIN				,     gen_kwargs={"""files""": files}  )]
       _lowerCAmelCase						:       Optional[Any]  			=     []
       for split_name, files in data_files.items():
         if isinstance(a__				,     a__  ):
           _lowerCAmelCase						:       Dict  			=     [files]
         # Use `dl_manager.iter_files` to skip hidden files in an extracted archive
         _lowerCAmelCase						:       Tuple  			=     [dl_manager.iter_files(a__  ) for file in files]
         # Infer features is they are stoed in the arrow schema
         if self.info.features is None:
           for file in itertools.chain.from_iterable(a__  ):
             with open(a__				,     """rb"""  ) as f:
               _lowerCAmelCase						:       Optional[Any]  			=     datasets.Features.from_arrow_schema(pq.read_schema(a__  )  )
             break
         splits.append(datasets.SplitGenerator(name=a__				,     gen_kwargs={"""files""": files}  )  )
       return splits






     def     __A  (		self				,     a__  ):
       if self.info.features is not None:
         # more expensive cast to support nested features with keys in a different order
         # allows str <-> int/float or str to Audio for example
         _lowerCAmelCase						:       Optional[int]  			=     table_cast(a__				,     self.info.features.arrow_schema  )
       return pa_table






     def     __A  (		self				,     a__  ):
       _lowerCAmelCase						:       Optional[int]  			=     self.info.features.arrow_schema if self.info.features is not None else None
       if self.info.features is not None and self.config.columns is not None:
         if sorted(field.name for field in schema  ) != sorted(self.config.columns  ):
           raise ValueError(
               F"Tried to load parquet data with columns '{self.config.columns}' with mismatching features '{self.info.features}'"  )
       for file_idx, file in enumerate(itertools.chain.from_iterable(a__  )  ):
         with open(a__				,     """rb"""  ) as f:
           _lowerCAmelCase						:       Tuple  			=     pq.ParquetFile(a__  )
           try:
             for batch_idx, record_batch in enumerate(
                 parquet_file.iter_batches(batch_size=self.config.batch_size				,     columns=self.config.columns  )  ):
               _lowerCAmelCase						:       Any  			=     pa.Table.from_batches([record_batch]  )
               # 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 F"{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




