




import os
import posixpath
import uuid
from dataclasses import dataclass
from typing import TYPE_CHECKING, Iterable, List, Optional, Tuple, Union

import numpy as np
import pyarrow as pa

import datasets
from datasets.arrow_writer import ArrowWriter, ParquetWriter
from datasets.config import MAX_SHARD_SIZE
from datasets.filesystems import (
    is_remote_filesystem,
    rename,
)
from datasets.iterable_dataset import _BaseExamplesIterable
from datasets.utils.py_utils import convert_file_size_to_int


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

if TYPE_CHECKING:
  import pyspark
@dataclass
class 			_UpperCamelCase      (				datasets.BuilderConfig		):
      UpperCAmelCase_        =		None



def 						lowerCAmelCase		(		_lowerCAmelCase							:   "pyspark.sql.DataFrame"						,							_lowerCAmelCase							:   List[int]						,							):




 """simple docstring"""







 import pyspark

 def generate_fn():
  UpperCAmelCase__							 =						df.select("*"						,							pyspark.sql.functions.spark_partition_id().alias("part_id"	)	)
  for partition_id in partition_order:
   UpperCAmelCase__							 =						df_with_partition_id.select("*"	).where(F'''part_id = {partition_id}'''	).drop("part_id"	)
   UpperCAmelCase__							 =						partition_df.collect()
   UpperCAmelCase__							 =						0
   for row in rows:
    yield F'''{partition_id}_{row_id}''', row.asDict()
    row_id += 1

 return generate_fn
class 			_UpperCamelCase      (				_BaseExamplesIterable		):
      def __init__(			self   :List[str] ,      lowerCamelCase   :"pyspark.sql.DataFrame" ,      lowerCamelCase   :str=None ,      )     ->			Optional[int]:
       UpperCAmelCase__							 =						df
       UpperCAmelCase__							 =						partition_order or range(self.df.rdd.getNumPartitions()      )
       UpperCAmelCase__							 =						_generate_iterable_examples(self.df ,      self.partition_order      )
      def __iter__(			self   :List[Any]      )     ->			Tuple:
       yield from self.generate_examples_fn()
      def 	UpperCAmelCase_						(			self   :List[Any] ,      lowerCamelCase   :np.random.Generator      )     ->			"SparkExamplesIterable":
       UpperCAmelCase__							 =						list(range(self.df.rdd.getNumPartitions()      )      )
       generator.shuffle(lowerCamelCase      )
       return SparkExamplesIterable(self.df ,      partition_order=lowerCamelCase      )
      def 	UpperCAmelCase_						(			self   :Any ,      lowerCamelCase   :int ,      lowerCamelCase   :int      )     ->			"SparkExamplesIterable":
       UpperCAmelCase__							 =						self.split_shard_indices_by_worker(lowerCamelCase ,      lowerCamelCase      )
       return SparkExamplesIterable(self.df ,      partition_order=lowerCamelCase      )







      @property
      def 	UpperCAmelCase_						(			self   :Any      )     ->			int:
       return len(self.partition_order      )



class 			_UpperCamelCase      (				datasets.DatasetBuilder		):
      UpperCAmelCase_        =		SparkConfig
      def __init__(			self   :str ,      lowerCamelCase   :"pyspark.sql.DataFrame" ,      lowerCamelCase   :str = None ,      lowerCamelCase   :str = None ,      **lowerCamelCase   :List[str] ,      )     ->			Any:
       import pyspark

       UpperCAmelCase__							 =						pyspark.sql.SparkSession.builder.getOrCreate()
       UpperCAmelCase__							 =						df
       UpperCAmelCase__							 =						working_dir

       super().__init__(
           cache_dir=lowerCamelCase ,      config_name=str(self.df.semanticHash()      ) ,      **lowerCamelCase ,      )
      def 	UpperCAmelCase_						(			self   :List[str]      )     ->			Any:
       # Returns the path of the created file.
       def create_cache_and_write_probe(lowerCamelCase   :List[Any]      ):
        # makedirs with exist_ok will recursively create the directory. It will not throw an error if directories
        # already exist.
        os.makedirs(self._cache_dir ,      exist_ok=lowerCamelCase      )
        UpperCAmelCase__							 =						os.path.join(self._cache_dir ,      "fs_test" + uuid.uuida().hex      )
        # Opening the file in append mode will create a new file unless it already exists, in which case it will not
        # change the file contents.
        open(lowerCamelCase ,      "a"      )
        return [probe_file]

       if self._spark.conf.get("spark.master" ,      ""      ).startswith("local"      ):
        return

       # If the cluster is multi-node, make sure that the user provided a cache_dir and that it is on an NFS
       # accessible to the driver.
       # TODO: Stream batches to the driver using ArrowCollectSerializer instead of throwing an error.
       if self._cache_dir:
        UpperCAmelCase__							 =						(
            self._spark.sparkContext.parallelize(range(1      ) ,      1      ).mapPartitions(lowerCamelCase      ).collect()
        )
        if os.path.isfile(probe[0]      ):
         return

       raise ValueError(
           "When using Dataset.from_spark on a multi-node cluster, the driver and all workers should be able to access cache_dir"      )
      def 	UpperCAmelCase_						(			self   :Union[str, Any]      )     ->			List[str]:
       return datasets.DatasetInfo(features=self.config.features      )
      def 	UpperCAmelCase_						(			self   :Tuple ,      lowerCamelCase   :datasets.download.download_manager.DownloadManager      )     ->			Union[str, Any]:
       return [datasets.SplitGenerator(name=datasets.Split.TRAIN      )]
      def 	UpperCAmelCase_						(			self   :Optional[Any] ,      lowerCamelCase   :List[Any]      )     ->			Tuple:
       import pyspark

       def get_arrow_batch_size(lowerCamelCase   :int      ):
        for batch in it:
         yield pa.RecordBatch.from_pydict({"batch_bytes": [batch.nbytes]}      )

       UpperCAmelCase__							 =						self.df.count()
       UpperCAmelCase__							 =						df_num_rows if df_num_rows <= 100 else 100
       # Approximate the size of each row (in Arrow format) by averaging over a max-100-row sample.
       UpperCAmelCase__							 =						(
           self.df.limit(lowerCamelCase      )
           .repartition(1      )
           .mapInArrow(lowerCamelCase ,      "batch_bytes: long"      )
           .agg(pyspark.sql.functions.sum("batch_bytes"      ).alias("sample_bytes"      )      )
           .collect()[0]
           .sample_bytes
           / sample_num_rows
       )
       UpperCAmelCase__							 =						approx_bytes_per_row * df_num_rows
       if approx_total_size > max_shard_size:
        # Make sure there is at least one row per partition.
        UpperCAmelCase__							 =						min(lowerCamelCase ,      int(approx_total_size / max_shard_size      )      )
        UpperCAmelCase__							 =						self.df.repartition(lowerCamelCase      )
      def 	UpperCAmelCase_						(			self   :Union[str, Any] ,      lowerCamelCase   :str ,      lowerCamelCase   :str ,      lowerCamelCase   :int ,      )     ->			Iterable[Tuple[int, bool, Union[int, tuple]]]:
       import pyspark

       UpperCAmelCase__							 =						ParquetWriter if file_format == "parquet" else ArrowWriter
       UpperCAmelCase__							 =						os.path.join(self._working_dir ,      os.path.basename(lowerCamelCase      )      ) if self._working_dir else fpath
       UpperCAmelCase__							 =						file_format == "parquet"

       # Define these so that we don't reference self in write_arrow, which will result in a pickling error due to
       # pickling the SparkContext.
       UpperCAmelCase__							 =						self.config.features
       UpperCAmelCase__							 =						self._writer_batch_size
       UpperCAmelCase__							 =						self._fs.storage_options

       def write_arrow(lowerCamelCase   :Dict      ):
        # Within the same SparkContext, no two task attempts will share the same attempt ID.
        UpperCAmelCase__							 =						pyspark.TaskContext().taskAttemptId()
        UpperCAmelCase__							 =						next(lowerCamelCase ,      lowerCamelCase      )
        if first_batch is None:
         # Some partitions might not receive any data.
         return pa.RecordBatch.from_arrays(
             [[task_id], [0], [0]] ,      names=["task_id", "num_examples", "num_bytes"] ,      )
        UpperCAmelCase__							 =						0
        UpperCAmelCase__							 =						writer_class(
            features=lowerCamelCase ,      path=working_fpath.replace("SSSSS" ,      f'''{shard_id:05d}'''      ).replace("TTTTT" ,      f'''{task_id:05d}'''      ) ,      writer_batch_size=lowerCamelCase ,      storage_options=lowerCamelCase ,      embed_local_files=lowerCamelCase ,      )
        UpperCAmelCase__							 =						pa.Table.from_batches([first_batch]      )
        writer.write_table(lowerCamelCase      )
        for batch in it:
         if max_shard_size is not None and writer._num_bytes >= max_shard_size:
          UpperCAmelCase__ , UpperCAmelCase__							 =						writer.finalize()
          writer.close()
          yield pa.RecordBatch.from_arrays(
              [[task_id], [num_examples], [num_bytes]] ,      names=["task_id", "num_examples", "num_bytes"] ,      )
          shard_id += 1
          UpperCAmelCase__							 =						writer_class(
              features=writer._features ,      path=working_fpath.replace("SSSSS" ,      f'''{shard_id:05d}'''      ).replace("TTTTT" ,      f'''{task_id:05d}'''      ) ,      writer_batch_size=lowerCamelCase ,      storage_options=lowerCamelCase ,      embed_local_files=lowerCamelCase ,      )
         UpperCAmelCase__							 =						pa.Table.from_batches([batch]      )
         writer.write_table(lowerCamelCase      )

        if writer._num_bytes > 0:
         UpperCAmelCase__ , UpperCAmelCase__							 =						writer.finalize()
         writer.close()
         yield pa.RecordBatch.from_arrays(
             [[task_id], [num_examples], [num_bytes]] ,      names=["task_id", "num_examples", "num_bytes"] ,      )

        if working_fpath != fpath:
         for file in os.listdir(os.path.dirname(lowerCamelCase      )      ):
          UpperCAmelCase__							 =						os.path.join(os.path.dirname(lowerCamelCase      ) ,      os.path.basename(lowerCamelCase      )      )
          shutil.move(lowerCamelCase ,      lowerCamelCase      )

       UpperCAmelCase__							 =						(
           self.df.mapInArrow(lowerCamelCase ,      "task_id: long, num_examples: long, num_bytes: long"      )
           .groupBy("task_id"      )
           .agg(
               pyspark.sql.functions.sum("num_examples"      ).alias("total_num_examples"      ) ,      pyspark.sql.functions.sum("num_bytes"      ).alias("total_num_bytes"      ) ,      pyspark.sql.functions.count("num_bytes"      ).alias("num_shards"      ) ,      pyspark.sql.functions.collect_list("num_examples"      ).alias("shard_lengths"      ) ,      )
           .collect()
       )
       for row in stats:
        yield row.task_id, (row.total_num_examples, row.total_num_bytes, row.num_shards, row.shard_lengths)
      def 	UpperCAmelCase_						(			self   :Tuple ,      lowerCamelCase   :"datasets.SplitGenerator" ,      lowerCamelCase   :str = "arrow" ,      lowerCamelCase   :Optional[Union[str, int]] = None ,      lowerCamelCase   :Optional[int] = None ,      **lowerCamelCase   :List[Any] ,      )     ->			List[str]:
       self._validate_cache_dir()

       UpperCAmelCase__							 =						convert_file_size_to_int(max_shard_size or MAX_SHARD_SIZE      )
       self._repartition_df_if_needed(lowerCamelCase      )
       UpperCAmelCase__							 =						not is_remote_filesystem(self._fs      )
       UpperCAmelCase__							 =						os.path.join if is_local else posixpath.join

       UpperCAmelCase__							 =						"-TTTTT-SSSSS-of-NNNNN"
       UpperCAmelCase__							 =						f'''{self.name}-{split_generator.name}{SUFFIX}.{file_format}'''
       UpperCAmelCase__							 =						path_join(self._output_dir ,      lowerCamelCase      )

       UpperCAmelCase__							 =						0
       UpperCAmelCase__							 =						0
       UpperCAmelCase__							 =						0
       UpperCAmelCase__							 =						[]
       UpperCAmelCase__							 =						[]

       for task_id, content in self._prepare_split_single(lowerCamelCase ,      lowerCamelCase ,      lowerCamelCase      ):
        (
            (
            UpperCAmelCase__
        ) , (
            UpperCAmelCase__
        ) , (
            UpperCAmelCase__
        ) , (
            UpperCAmelCase__
        ) , 
        )							 =						content
        if num_bytes > 0:
         total_num_examples += num_examples
         total_num_bytes += num_bytes
         total_shards += num_shards
         task_id_and_num_shards.append((task_id, num_shards)      )
         all_shard_lengths.extend(lowerCamelCase      )

       UpperCAmelCase__							 =						total_num_examples
       UpperCAmelCase__							 =						total_num_bytes

       # should rename everything at the end
       logger.debug(f'''Renaming {total_shards} shards.'''      )
       if total_shards > 1:
        UpperCAmelCase__							 =						all_shard_lengths

        # Define fs outside of _rename_shard so that we don't reference self in the function, which will result in a
        # pickling error due to pickling the SparkContext.
        UpperCAmelCase__							 =						self._fs

        # use the -SSSSS-of-NNNNN pattern
        def _rename_shard(
            lowerCamelCase   :int ,      lowerCamelCase   :int ,      lowerCamelCase   :int ,      ):
         rename(
             lowerCamelCase ,      fpath.replace("SSSSS" ,      f'''{shard_id:05d}'''      ).replace("TTTTT" ,      f'''{task_id:05d}'''      ) ,      fpath.replace("TTTTT-SSSSS" ,      f'''{global_shard_id:05d}'''      ).replace("NNNNN" ,      f'''{total_shards:05d}'''      ) ,      )

        UpperCAmelCase__							 =						[]
        UpperCAmelCase__							 =						0
        for i in range(len(lowerCamelCase      )      ):
         UpperCAmelCase__ , UpperCAmelCase__							 =						task_id_and_num_shards[i]
         for shard_id in range(lowerCamelCase      ):
          args.append([task_id, shard_id, global_shard_id]      )
          global_shard_id += 1
        self._spark.sparkContext.parallelize(lowerCamelCase ,      len(lowerCamelCase      )      ).map(lambda lowerCamelCase      : _rename_shard(*lowerCamelCase      )      ).collect()
       else:
        # don't use any pattern
        UpperCAmelCase__							 =						0
        UpperCAmelCase__							 =						task_id_and_num_shards[0][0]
        self._rename(
            fpath.replace("SSSSS" ,      f'''{shard_id:05d}'''      ).replace("TTTTT" ,      f'''{task_id:05d}'''      ) ,      fpath.replace(lowerCamelCase ,      ""      ) ,      )







      def 	UpperCAmelCase_						(			self   :str ,      lowerCamelCase   :"datasets.SplitGenerator" ,      )     ->			SparkExamplesIterable:
       return SparkExamplesIterable(self.df      )











import inspect
import unittest

from transformers import RegNetConfig
from transformers.file_utils import cached_property, is_torch_available, is_vision_available
from transformers.testing_utils import require_torch, require_vision, 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 torch import nn

  from transformers import RegNetForImageClassification, RegNetModel
  from transformers.models.regnet.modeling_regnet import REGNET_PRETRAINED_MODEL_ARCHIVE_LIST


if is_vision_available():
  from PIL import Image

  from transformers import AutoImageProcessor
class 			_UpperCamelCase      :
      def __init__(			self   :List[Any] ,      lowerCamelCase   :Optional[Any] ,      lowerCamelCase   :Any=3 ,      lowerCamelCase   :List[str]=32 ,      lowerCamelCase   :List[str]=3 ,      lowerCamelCase   :List[str]=10 ,      lowerCamelCase   :List[Any]=[10, 20, 30, 40] ,      lowerCamelCase   :Optional[Any]=[1, 1, 2, 1] ,      lowerCamelCase   :List[str]=True ,      lowerCamelCase   :List[Any]=True ,      lowerCamelCase   :str="relu" ,      lowerCamelCase   :Optional[Any]=3 ,      lowerCamelCase   :List[str]=None ,      )     ->			Union[str, Any]:
       UpperCAmelCase__							 =						parent
       UpperCAmelCase__							 =						batch_size
       UpperCAmelCase__							 =						image_size
       UpperCAmelCase__							 =						num_channels
       UpperCAmelCase__							 =						embeddings_size
       UpperCAmelCase__							 =						hidden_sizes
       UpperCAmelCase__							 =						depths
       UpperCAmelCase__							 =						is_training
       UpperCAmelCase__							 =						use_labels
       UpperCAmelCase__							 =						hidden_act
       UpperCAmelCase__							 =						num_labels
       UpperCAmelCase__							 =						scope
       UpperCAmelCase__							 =						len(lowerCamelCase      )
      def 	UpperCAmelCase_						(			self   :Union[str, Any]      )     ->			List[str]:
       UpperCAmelCase__							 =						floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]      )

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

       UpperCAmelCase__							 =						self.get_config()

       return config, pixel_values, labels
      def 	UpperCAmelCase_						(			self   :Tuple      )     ->			Optional[int]:
       return RegNetConfig(
           num_channels=self.num_channels ,      embeddings_size=self.embeddings_size ,      hidden_sizes=self.hidden_sizes ,      depths=self.depths ,      hidden_act=self.hidden_act ,      num_labels=self.num_labels ,      )
      def 	UpperCAmelCase_						(			self   :str ,      lowerCamelCase   :Dict ,      lowerCamelCase   :Optional[int] ,      lowerCamelCase   :Union[str, Any]      )     ->			Dict:
       UpperCAmelCase__							 =						RegNetModel(config=lowerCamelCase      )
       model.to(lowerCamelCase      )
       model.eval()
       UpperCAmelCase__							 =						model(lowerCamelCase      )
       # 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   :Union[str, Any] ,      lowerCamelCase   :Union[str, Any] ,      lowerCamelCase   :Tuple ,      lowerCamelCase   :List[str]      )     ->			Union[str, Any]:
       UpperCAmelCase__							 =						self.num_labels
       UpperCAmelCase__							 =						RegNetForImageClassification(lowerCamelCase      )
       model.to(lowerCamelCase      )
       model.eval()
       UpperCAmelCase__							 =						model(lowerCamelCase ,      labels=lowerCamelCase      )
       self.parent.assertEqual(result.logits.shape ,      (self.batch_size, self.num_labels)      )







      def 	UpperCAmelCase_						(			self   :Any      )     ->			Optional[Any]:
       UpperCAmelCase__							 =						self.prepare_config_and_inputs()
       UpperCAmelCase__ , UpperCAmelCase__ , UpperCAmelCase__							 =						config_and_inputs
       UpperCAmelCase__							 =						{"pixel_values": pixel_values}
       return config, inputs_dict



@require_torch
class 			_UpperCamelCase      (				lowerCAmelCase	,       lowerCAmelCase	,       unittest.TestCase		):
      UpperCAmelCase_        =		(RegNetModel, RegNetForImageClassification) if is_torch_available() else ()
      UpperCAmelCase_        =		(
          {"""feature-extraction""": RegNetModel, """image-classification""": RegNetForImageClassification}
          if is_torch_available()
          else {}
      )

      UpperCAmelCase_        =		False
      UpperCAmelCase_        =		False
      UpperCAmelCase_        =		False
      UpperCAmelCase_        =		False
      def 	UpperCAmelCase_						(			self   :int      )     ->			Dict:
       UpperCAmelCase__							 =						RegNetModelTester(self      )
       UpperCAmelCase__							 =						ConfigTester(self ,      config_class=lowerCamelCase ,      has_text_modality=lowerCamelCase      )
      def 	UpperCAmelCase_						(			self   :str      )     ->			Tuple:
       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   :Any      )     ->			List[str]:
       return
      @unittest.skip(reason="RegNet does not use inputs_embeds"      )
      def 	UpperCAmelCase_						(			self   :Optional[Any]      )     ->			Any:
       pass
      @unittest.skip(reason="RegNet does not support input and output embeddings"      )
      def 	UpperCAmelCase_						(			self   :Tuple      )     ->			Union[str, Any]:
       pass
      def 	UpperCAmelCase_						(			self   :List[str]      )     ->			Tuple:
       UpperCAmelCase__ , UpperCAmelCase__							 =						self.model_tester.prepare_config_and_inputs_for_common()

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

        UpperCAmelCase__							 =						["pixel_values"]
        self.assertListEqual(arg_names[:1] ,      lowerCamelCase      )
      def 	UpperCAmelCase_						(			self   :Dict      )     ->			List[Any]:
       UpperCAmelCase__							 =						self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_model(*lowerCamelCase      )
      def 	UpperCAmelCase_						(			self   :Optional[Any]      )     ->			int:
       UpperCAmelCase__ , UpperCAmelCase__							 =						self.model_tester.prepare_config_and_inputs_for_common()

       for model_class in self.all_model_classes:
        UpperCAmelCase__							 =						model_class(config=lowerCamelCase      )
        for name, module in model.named_modules():
         if isinstance(lowerCamelCase ,      (nn.BatchNormad, nn.GroupNorm)      ):
          self.assertTrue(
              torch.all(module.weight == 1      ) ,      msg=f'''Parameter {name} of model {model_class} seems not properly initialized''' ,      )
          self.assertTrue(
              torch.all(module.bias == 0      ) ,      msg=f'''Parameter {name} of model {model_class} seems not properly initialized''' ,      )
      def 	UpperCAmelCase_						(			self   :Optional[int]      )     ->			List[Any]:
       def check_hidden_states_output(lowerCamelCase   :Optional[int] ,      lowerCamelCase   :int ,      lowerCamelCase   :Optional[int]      ):
        UpperCAmelCase__							 =						model_class(lowerCamelCase      )
        model.to(lowerCamelCase      )
        model.eval()

        with torch.no_grad():
         UpperCAmelCase__							 =						model(**self._prepare_for_class(lowerCamelCase ,      lowerCamelCase      )      )

        UpperCAmelCase__							 =						outputs.encoder_hidden_states if config.is_encoder_decoder else outputs.hidden_states

        UpperCAmelCase__							 =						self.model_tester.num_stages
        self.assertEqual(len(lowerCamelCase      ) ,      expected_num_stages + 1      )

        # RegNet'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 // 2, self.model_tester.image_size // 2] ,      )

       UpperCAmelCase__ , UpperCAmelCase__							 =						self.model_tester.prepare_config_and_inputs_for_common()
       UpperCAmelCase__							 =						["basic", "bottleneck"]
       for model_class in self.all_model_classes:
        for layer_type in layers_type:
         UpperCAmelCase__							 =						layer_type
         UpperCAmelCase__							 =						True
         check_hidden_states_output(lowerCamelCase ,      lowerCamelCase ,      lowerCamelCase      )

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

         check_hidden_states_output(lowerCamelCase ,      lowerCamelCase ,      lowerCamelCase      )
      def 	UpperCAmelCase_						(			self   :Dict      )     ->			Union[str, Any]:
       UpperCAmelCase__							 =						self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_for_image_classification(*lowerCamelCase      )







      @slow
      def 	UpperCAmelCase_						(			self   :Tuple      )     ->			Tuple:
       for model_name in REGNET_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
        UpperCAmelCase__							 =						RegNetModel.from_pretrained(lowerCamelCase      )
        self.assertIsNotNone(lowerCamelCase      )



def 						lowerCAmelCase		(		):




 """simple docstring"""







 UpperCAmelCase__							 =						Image.open("./tests/fixtures/tests_samples/COCO/000000039769.png"	)
 return image


@require_torch
@require_vision
class 			_UpperCamelCase      (				unittest.TestCase		):
      @cached_property
      def 	UpperCAmelCase_						(			self   :Any      )     ->			Optional[Any]:
       return (
           AutoImageProcessor.from_pretrained(REGNET_PRETRAINED_MODEL_ARCHIVE_LIST[0]      )
           if is_vision_available()
           else None
       )







      @slow
      def 	UpperCAmelCase_						(			self   :Tuple      )     ->			Union[str, Any]:
       UpperCAmelCase__							 =						RegNetForImageClassification.from_pretrained(REGNET_PRETRAINED_MODEL_ARCHIVE_LIST[0]      ).to(lowerCamelCase      )

       UpperCAmelCase__							 =						self.default_image_processor
       UpperCAmelCase__							 =						prepare_img()
       UpperCAmelCase__							 =						image_processor(images=lowerCamelCase ,      return_tensors="pt"      ).to(lowerCamelCase      )

       # forward pass
       with torch.no_grad():
        UpperCAmelCase__							 =						model(**lowerCamelCase      )

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

       UpperCAmelCase__							 =						torch.tensor([-0.41_80, -1.50_51, -3.48_36]      ).to(lowerCamelCase      )

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





