


import json
import os
from pathlib import Path

import pytest

from datasets.download.download_config import DownloadConfig
from datasets.download.download_manager import DownloadManager
from datasets.utils.file_utils import hash_url_to_filename


lowercase_	   =     'http://www.mocksite.com/file1.txt'
lowercase_	   =     '"text": ["foo", "foo"]'
lowercase_	   =     '6d8ce9aa78a471c7477201efbeabd3bb01ac2e7d100a6dc024ba1608361f90a8'


class 	A_ :




      '''simple docstring'''


      __snake_case 	=       200
      __snake_case 	=       {"""Content-Length""": """100"""}
      __snake_case 	=       {}




      def 					_snake_case     (    self:	Optional[int]						,				**a:	Union[str, Any]    ):
       return [bytes(a						,				'utf-8'    )]



def 				UpperCamelCase__					(  *SCREAMING_SNAKE_CASE__      ,	**SCREAMING_SNAKE_CASE__						):
 return MockResponse()
@pytest.mark.parametrize('urls_type'      ,	[str, list, dict]						)
def 				UpperCamelCase__					(  SCREAMING_SNAKE_CASE__      ,	SCREAMING_SNAKE_CASE__      ,	SCREAMING_SNAKE_CASE__						):
 import requests

 monkeypatch.setattr(SCREAMING_SNAKE_CASE__      ,	'request'      ,	SCREAMING_SNAKE_CASE__						)

 __lowerCamelCase		:							Any		 =						URL
 if issubclass(SCREAMING_SNAKE_CASE__      ,	SCREAMING_SNAKE_CASE__						):
  __lowerCamelCase		:							Tuple		 =						url
 elif issubclass(SCREAMING_SNAKE_CASE__      ,	SCREAMING_SNAKE_CASE__						):
  __lowerCamelCase		:							str		 =						[url]
 elif issubclass(SCREAMING_SNAKE_CASE__      ,	SCREAMING_SNAKE_CASE__						):
  __lowerCamelCase		:							str		 =						{'train': url}
 __lowerCamelCase		:							List[str]		 =						'dummy'
 __lowerCamelCase		:							str		 =						'downloads'
 __lowerCamelCase		:							str		 =						tmp_path
 __lowerCamelCase		:							List[str]		 =						DownloadConfig(
     cache_dir=os.path.join(SCREAMING_SNAKE_CASE__      ,	SCREAMING_SNAKE_CASE__						)      ,	use_etag=SCREAMING_SNAKE_CASE__      ,	)
 __lowerCamelCase		:							List[Any]		 =						DownloadManager(dataset_name=SCREAMING_SNAKE_CASE__      ,	download_config=SCREAMING_SNAKE_CASE__						)
 __lowerCamelCase		:							int		 =						dl_manager.download(SCREAMING_SNAKE_CASE__						)
 __lowerCamelCase		:							List[str]		 =						urls
 for downloaded_paths in [downloaded_paths]:
  if isinstance(SCREAMING_SNAKE_CASE__      ,	SCREAMING_SNAKE_CASE__						):
   __lowerCamelCase		:							Dict		 =						[downloaded_paths]
   __lowerCamelCase		:							List[Any]		 =						[urls]
  elif isinstance(SCREAMING_SNAKE_CASE__      ,	SCREAMING_SNAKE_CASE__						):
   assert "train" in downloaded_paths.keys()
   __lowerCamelCase		:							int		 =						downloaded_paths.values()
   __lowerCamelCase		:							Optional[int]		 =						urls.values()
  assert downloaded_paths
  for downloaded_path, input_url in zip(SCREAMING_SNAKE_CASE__      ,	SCREAMING_SNAKE_CASE__						):
   assert downloaded_path == dl_manager.downloaded_paths[input_url]
   __lowerCamelCase		:							int		 =						Path(SCREAMING_SNAKE_CASE__						)
   __lowerCamelCase		:							Optional[int]		 =						downloaded_path.parts
   assert parts[-1] == HASH
   assert parts[-2] == cache_subdir
   assert downloaded_path.exists()
   __lowerCamelCase		:							List[Any]		 =						downloaded_path.read_text()
   assert content == CONTENT
   __lowerCamelCase		:							Tuple		 =						downloaded_path.with_suffix('.json'						)
   assert metadata_downloaded_path.exists()
   __lowerCamelCase		:							Optional[Any]		 =						json.loads(metadata_downloaded_path.read_text()						)
   assert metadata_content == {"url": URL, "etag": None}
@pytest.mark.parametrize('paths_type'      ,	[str, list, dict]						)
def 				UpperCamelCase__					(  SCREAMING_SNAKE_CASE__      ,	SCREAMING_SNAKE_CASE__      ,	SCREAMING_SNAKE_CASE__						):
 __lowerCamelCase		:							Optional[Any]		 =						str(SCREAMING_SNAKE_CASE__						)
 if issubclass(SCREAMING_SNAKE_CASE__      ,	SCREAMING_SNAKE_CASE__						):
  __lowerCamelCase		:							Dict		 =						filename
 elif issubclass(SCREAMING_SNAKE_CASE__      ,	SCREAMING_SNAKE_CASE__						):
  __lowerCamelCase		:							str		 =						[filename]
 elif issubclass(SCREAMING_SNAKE_CASE__      ,	SCREAMING_SNAKE_CASE__						):
  __lowerCamelCase		:							List[str]		 =						{'train': filename}
 __lowerCamelCase		:							Any		 =						'dummy'
 __lowerCamelCase		:							int		 =						xz_file.parent
 __lowerCamelCase		:							List[str]		 =						'extracted'
 __lowerCamelCase		:							Any		 =						DownloadConfig(
     cache_dir=SCREAMING_SNAKE_CASE__      ,	use_etag=SCREAMING_SNAKE_CASE__      ,	)
 __lowerCamelCase		:							Tuple		 =						DownloadManager(dataset_name=SCREAMING_SNAKE_CASE__      ,	download_config=SCREAMING_SNAKE_CASE__						)
 __lowerCamelCase		:							Optional[Any]		 =						dl_manager.extract(SCREAMING_SNAKE_CASE__						)
 __lowerCamelCase		:							int		 =						paths
 for extracted_paths in [extracted_paths]:
  if isinstance(SCREAMING_SNAKE_CASE__      ,	SCREAMING_SNAKE_CASE__						):
   __lowerCamelCase		:							Tuple		 =						[extracted_paths]
   __lowerCamelCase		:							Tuple		 =						[paths]
  elif isinstance(SCREAMING_SNAKE_CASE__      ,	SCREAMING_SNAKE_CASE__						):
   assert "train" in extracted_paths.keys()
   __lowerCamelCase		:							Union[str, Any]		 =						extracted_paths.values()
   __lowerCamelCase		:							Optional[Any]		 =						paths.values()
  assert extracted_paths
  for extracted_path, input_path in zip(SCREAMING_SNAKE_CASE__      ,	SCREAMING_SNAKE_CASE__						):
   assert extracted_path == dl_manager.extracted_paths[input_path]
   __lowerCamelCase		:							Union[str, Any]		 =						Path(SCREAMING_SNAKE_CASE__						)
   __lowerCamelCase		:							List[str]		 =						extracted_path.parts
   assert parts[-1] == hash_url_to_filename(SCREAMING_SNAKE_CASE__      ,	etag=SCREAMING_SNAKE_CASE__						)
   assert parts[-2] == extracted_subdir
   assert extracted_path.exists()
   __lowerCamelCase		:							List[Any]		 =						extracted_path.read_text()
   __lowerCamelCase		:							Union[str, Any]		 =						text_file.read_text()
   assert extracted_file_content == expected_file_content
def 				UpperCamelCase__					(  SCREAMING_SNAKE_CASE__      ,	SCREAMING_SNAKE_CASE__						):
 assert path.endswith('.jsonl'						)
 for num_items, line in enumerate(SCREAMING_SNAKE_CASE__      ,	start=1						):
  __lowerCamelCase		:							Optional[int]		 =						json.loads(line.decode('utf-8'						)						)
  assert item.keys() == {"col_1", "col_2", "col_3"}
 assert num_items == 4
@pytest.mark.parametrize('archive_jsonl'      ,	['tar_jsonl_path', 'zip_jsonl_path']						)
def 				UpperCamelCase__					(  SCREAMING_SNAKE_CASE__      ,	SCREAMING_SNAKE_CASE__						):
 __lowerCamelCase		:							List[str]		 =						request.getfixturevalue(SCREAMING_SNAKE_CASE__						)
 __lowerCamelCase		:							Optional[Any]		 =						DownloadManager()
 for num_jsonl, (path, file) in enumerate(dl_manager.iter_archive(SCREAMING_SNAKE_CASE__						)      ,	start=1						):
  _test_jsonl(SCREAMING_SNAKE_CASE__      ,	SCREAMING_SNAKE_CASE__						)
 assert num_jsonl == 2
@pytest.mark.parametrize('archive_nested_jsonl'      ,	['tar_nested_jsonl_path', 'zip_nested_jsonl_path']						)
def 				UpperCamelCase__					(  SCREAMING_SNAKE_CASE__      ,	SCREAMING_SNAKE_CASE__						):
 __lowerCamelCase		:							Any		 =						request.getfixturevalue(SCREAMING_SNAKE_CASE__						)
 __lowerCamelCase		:							Optional[Any]		 =						DownloadManager()
 for num_tar, (path, file) in enumerate(dl_manager.iter_archive(SCREAMING_SNAKE_CASE__						)      ,	start=1						):
  for num_jsonl, (subpath, subfile) in enumerate(dl_manager.iter_archive(SCREAMING_SNAKE_CASE__						)      ,	start=1						):
   _test_jsonl(SCREAMING_SNAKE_CASE__      ,	SCREAMING_SNAKE_CASE__						)
 assert num_tar == 1
 assert num_jsonl == 2



def 				UpperCamelCase__					(  SCREAMING_SNAKE_CASE__						):
 __lowerCamelCase		:							Optional[Any]		 =						DownloadManager()
 for num_file, file in enumerate(dl_manager.iter_files(SCREAMING_SNAKE_CASE__						)      ,	start=1						):
  assert os.path.basename(SCREAMING_SNAKE_CASE__						) == ("test.txt" if num_file == 1 else "train.txt")
 assert num_file == 2





import inspect
import unittest

from transformers import RegNetConfig, is_flax_available
from transformers.testing_utils import require_flax, slow
from transformers.utils import cached_property, is_vision_available

from ...test_configuration_common import ConfigTester
from ...test_modeling_flax_common import FlaxModelTesterMixin, floats_tensor


if is_flax_available():
       import jax
       import jax.numpy as jnp

       from transformers.models.regnet.modeling_flax_regnet import FlaxRegNetForImageClassification, FlaxRegNetModel

if is_vision_available():
       from PIL import Image

       from transformers import AutoImageProcessor


class 	A_ (	unittest.TestCase   ):




      '''simple docstring'''


      def __init__(    self:	int						,				a:	Optional[Any]						,				a:	Optional[Any]=3						,				a:	List[str]=32						,				a:	Optional[int]=3						,				a:	Any=10						,				a:	List[str]=[10, 20, 30, 40]						,				a:	Any=[1, 1, 2, 1]						,				a:	Optional[int]=True						,				a:	List[str]=True						,				a:	Tuple="relu"						,				a:	List[Any]=3						,				a:	List[Any]=None						,				):
       __lowerCamelCase		:							Union[str, Any]		 =						parent
       __lowerCamelCase		:							Any		 =						batch_size
       __lowerCamelCase		:							List[str]		 =						image_size
       __lowerCamelCase		:							Tuple		 =						num_channels
       __lowerCamelCase		:							int		 =						embeddings_size
       __lowerCamelCase		:							Optional[int]		 =						hidden_sizes
       __lowerCamelCase		:							Optional[Any]		 =						depths
       __lowerCamelCase		:							Optional[int]		 =						is_training
       __lowerCamelCase		:							List[str]		 =						use_labels
       __lowerCamelCase		:							Dict		 =						hidden_act
       __lowerCamelCase		:							Union[str, Any]		 =						num_labels
       __lowerCamelCase		:							Tuple		 =						scope
       __lowerCamelCase		:							Union[str, Any]		 =						len(a    )




      def 					_snake_case     (    self:	int    ):
       __lowerCamelCase		:							Optional[Any]		 =						floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]    )

       __lowerCamelCase		:							List[str]		 =						self.get_config()

       return config, pixel_values




      def 					_snake_case     (    self:	List[str]    ):
       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						,				image_size=self.image_size						,				)




      def 					_snake_case     (    self:	Tuple						,				a:	Optional[int]						,				a:	int    ):
       __lowerCamelCase		:							Optional[Any]		 =						FlaxRegNetModel(config=a    )
       __lowerCamelCase		:							List[str]		 =						model(a    )

       # Output shape (b, c, h, w)
       self.parent.assertEqual(
           result.last_hidden_state.shape						,				(self.batch_size, self.hidden_sizes[-1], self.image_size // 32, self.image_size // 32)						,				)




      def 					_snake_case     (    self:	Optional[int]						,				a:	List[Any]						,				a:	List[Any]    ):
       __lowerCamelCase		:							Tuple		 =						self.num_labels
       __lowerCamelCase		:							Union[str, Any]		 =						FlaxRegNetForImageClassification(config=a    )
       __lowerCamelCase		:							List[Any]		 =						model(a    )
       self.parent.assertEqual(result.logits.shape						,				(self.batch_size, self.num_labels)    )




      def 					_snake_case     (    self:	Optional[Any]    ):
       __lowerCamelCase		:							Union[str, Any]		 =						self.prepare_config_and_inputs()
       __lowerCamelCase ,					__lowerCamelCase		:							str		 =						config_and_inputs
       __lowerCamelCase		:							Tuple		 =						{'pixel_values': pixel_values}
       return config, inputs_dict



@require_flax
class 	A_ (	__UpperCamelCase ,				unittest.TestCase   ):




      '''simple docstring'''


      __snake_case 	=       (FlaxRegNetModel, FlaxRegNetForImageClassification) if is_flax_available() else ()

      __snake_case 	=       False
      __snake_case 	=       False
      __snake_case 	=       False




      def 					_snake_case     (    self:	Tuple    ):
       __lowerCamelCase		:							Dict		 =						FlaxRegNetModelTester(self    )
       __lowerCamelCase		:							List[str]		 =						ConfigTester(self						,				config_class=a						,				has_text_modality=a    )




      def 					_snake_case     (    self:	Union[str, Any]    ):
       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 					_snake_case     (    self:	List[Any]    ):
       return




      def 					_snake_case     (    self:	Dict    ):
       __lowerCamelCase		:							Union[str, Any]		 =						self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_model(*a    )




      def 					_snake_case     (    self:	Optional[int]    ):
       __lowerCamelCase		:							Union[str, Any]		 =						self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_for_image_classification(*a    )




      @unittest.skip(reason='RegNet does not use inputs_embeds'    )
      def 					_snake_case     (    self:	Tuple    ):
       pass




      @unittest.skip(reason='RegNet does not support input and output embeddings'    )
      def 					_snake_case     (    self:	str    ):
       pass




      def 					_snake_case     (    self:	List[Any]    ):
       __lowerCamelCase ,					__lowerCamelCase		:							Optional[Any]		 =						self.model_tester.prepare_config_and_inputs_for_common()

       for model_class in self.all_model_classes:
        __lowerCamelCase		:							Optional[Any]		 =						model_class(a    )
        __lowerCamelCase		:							Dict		 =						inspect.signature(model.__call__    )
        # signature.parameters is an OrderedDict => so arg_names order is deterministic
        __lowerCamelCase		:							Union[str, Any]		 =						[*signature.parameters.keys()]

        __lowerCamelCase		:							Union[str, Any]		 =						['pixel_values']
        self.assertListEqual(arg_names[:1]						,				a    )




      def 					_snake_case     (    self:	List[str]    ):
       def check_hidden_states_output(a:	List[Any]						,				a:	List[Any]						,				a:	Union[str, Any]    ):
        __lowerCamelCase		:							str		 =						model_class(a    )

        __lowerCamelCase		:							Optional[Any]		 =						model(**self._prepare_for_class(a						,				a    )    )

        __lowerCamelCase		:							List[str]		 =						outputs.encoder_hidden_states if config.is_encoder_decoder else outputs.hidden_states

        __lowerCamelCase		:							List[str]		 =						self.model_tester.num_stages
        self.assertEqual(len(a    )						,				expected_num_stages + 1    )

       __lowerCamelCase ,					__lowerCamelCase		:							Dict		 =						self.model_tester.prepare_config_and_inputs_for_common()

       for model_class in self.all_model_classes:
        __lowerCamelCase		:							int		 =						True
        check_hidden_states_output(a						,				a						,				a    )

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

        check_hidden_states_output(a						,				a						,				a    )




      def 					_snake_case     (    self:	Dict    ):
       __lowerCamelCase ,					__lowerCamelCase		:							Optional[int]		 =						self.model_tester.prepare_config_and_inputs_for_common()

       for model_class in self.all_model_classes:
        with self.subTest(model_class.__name__    ):
         __lowerCamelCase		:							str		 =						self._prepare_for_class(a						,				a    )
         __lowerCamelCase		:							List[str]		 =						model_class(a    )

         @jax.jit
         def model_jitted(a:	Optional[int]						,				**a:	str    ):
          return model(pixel_values=a						,				**a    )

         with self.subTest('JIT Enabled'    ):
          __lowerCamelCase		:							List[str]		 =						model_jitted(**a    ).to_tuple()

         with self.subTest('JIT Disabled'    ):
          with jax.disable_jit():
           __lowerCamelCase		:							Optional[int]		 =						model_jitted(**a    ).to_tuple()

         self.assertEqual(len(a    )						,				len(a    )    )
         for jitted_output, output in zip(a						,				a    ):
          self.assertEqual(jitted_output.shape						,				output.shape    )



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



@require_flax
class 	A_ (	unittest.TestCase   ):




      '''simple docstring'''


      @cached_property
      def 					_snake_case     (    self:	Tuple    ):
       return AutoImageProcessor.from_pretrained('facebook/regnet-y-040'    ) if is_vision_available() else None




      @slow
      def 					_snake_case     (    self:	Optional[Any]    ):
       __lowerCamelCase		:							Optional[Any]		 =						FlaxRegNetForImageClassification.from_pretrained('facebook/regnet-y-040'    )

       __lowerCamelCase		:							Tuple		 =						self.default_image_processor
       __lowerCamelCase		:							int		 =						prepare_img()
       __lowerCamelCase		:							Tuple		 =						image_processor(images=a						,				return_tensors='np'    )

       __lowerCamelCase		:							List[Any]		 =						model(**a    )

       # verify the logits
       __lowerCamelCase		:							str		 =						(1, 1000)
       self.assertEqual(outputs.logits.shape						,				a    )

       __lowerCamelCase		:							int		 =						jnp.array([-0.4_1_8_0, -1.5_0_5_1, -3.4_8_3_6]    )

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

