

import copy
import tempfile
import unittest

from huggingface_hub import HfFolder, delete_repo
from parameterized import parameterized
from requests.exceptions import HTTPError

from transformers import AutoConfig, GenerationConfig
from transformers.testing_utils import TOKEN, USER, is_staging_test






class 						__magic_name__    (       unittest.TestCase    ):


	@parameterized.expand([(None,), ("foo.json",)]    )
	def _UpperCamelCase	(						self		: str			,						lowerCAmelCase__		: Union[str, Any]    )    ->      Optional[int]:
			UpperCAmelCase					      =  GenerationConfig(
			    do_sample=lowerCAmelCase__			,						temperature=0.7			,						length_penalty=1.0			,						bad_words_ids=[[1, 2, 3], [4, 5]]			,						)
			with tempfile.TemporaryDirectory() as tmp_dir:
					config.save_pretrained(lowerCAmelCase__			,						config_name=lowerCAmelCase__    )
					UpperCAmelCase					      =  GenerationConfig.from_pretrained(lowerCAmelCase__			,						config_name=lowerCAmelCase__    )

			# Checks parameters that were specified
			self.assertEqual(loaded_config.do_sample			,						lowerCAmelCase__    )
			self.assertEqual(loaded_config.temperature			,						0.7    )
			self.assertEqual(loaded_config.length_penalty			,						1.0    )
			self.assertEqual(loaded_config.bad_words_ids			,						[[1, 2, 3], [4, 5]]    )

			# Checks parameters that were not specified (defaults)
			self.assertEqual(loaded_config.top_k			,						5_0    )
			self.assertEqual(loaded_config.max_length			,						2_0    )
			self.assertEqual(loaded_config.max_time			,						lowerCAmelCase__    )


	def _UpperCamelCase	(						self		: Dict    )    ->      Optional[Any]:
			UpperCAmelCase					      =  AutoConfig.from_pretrained("gpt2"    )
			UpperCAmelCase					      =  GenerationConfig.from_model_config(lowerCAmelCase__    )
			UpperCAmelCase					      =  GenerationConfig()

			# The generation config has loaded a few non-default parameters from the model config
			self.assertNotEqual(lowerCAmelCase__			,						lowerCAmelCase__    )

			# One of those parameters is eos_token_id -- check if it matches
			self.assertNotEqual(generation_config_from_model.eos_token_id			,						default_generation_config.eos_token_id    )
			self.assertEqual(generation_config_from_model.eos_token_id			,						model_config.eos_token_id    )


	def _UpperCamelCase	(						self		: int    )    ->      int:
			UpperCAmelCase					      =  GenerationConfig()
			UpperCAmelCase					      =  {
			    "max_new_tokens": 1_0_2_4,
			    "foo": "bar",
			}
			UpperCAmelCase					      =  copy.deepcopy(lowerCAmelCase__    )
			UpperCAmelCase					      =  generation_config.update(**lowerCAmelCase__    )

			# update_kwargs was not modified (no side effects)
			self.assertEqual(lowerCAmelCase__			,						lowerCAmelCase__    )

			# update_kwargs was used to update the config on valid attributes
			self.assertEqual(generation_config.max_new_tokens			,						1_0_2_4    )

			# `.update()` returns a dictionary of unused kwargs
			self.assertEqual(lowerCAmelCase__			,						{"foo": "bar"}    )


	def _UpperCamelCase	(						self		: List[Any]    )    ->      Dict:
			UpperCAmelCase					      =  GenerationConfig()
			UpperCAmelCase					      =  "bar"

			with tempfile.TemporaryDirectory("test-generation-config"    ) as tmp_dir:
					generation_config.save_pretrained(lowerCAmelCase__    )

					UpperCAmelCase					      =  GenerationConfig.from_pretrained(lowerCAmelCase__    )
			# update_kwargs was used to update the config on valid attributes
			self.assertEqual(new_config.foo			,						"bar"    )

			UpperCAmelCase					      =  GenerationConfig.from_model_config(lowerCAmelCase__    )
			assert not hasattr(lowerCAmelCase__			,						"foo"    )  # no new kwargs should be initialized if from config


	def _UpperCamelCase	(						self		: Tuple    )    ->      Dict:
			UpperCAmelCase					      =  GenerationConfig()
			self.assertEqual(default_config.temperature			,						1.0    )
			self.assertEqual(default_config.do_sample			,						lowerCAmelCase__    )
			self.assertEqual(default_config.num_beams			,						1    )

			UpperCAmelCase					      =  GenerationConfig(
			    do_sample=lowerCAmelCase__			,						temperature=0.7			,						length_penalty=1.0			,						bad_words_ids=[[1, 2, 3], [4, 5]]			,						)
			self.assertEqual(config.temperature			,						0.7    )
			self.assertEqual(config.do_sample			,						lowerCAmelCase__    )
			self.assertEqual(config.num_beams			,						1    )

			with tempfile.TemporaryDirectory() as tmp_dir:
					config.save_pretrained(lowerCAmelCase__    )
					UpperCAmelCase					      =  GenerationConfig.from_pretrained(lowerCAmelCase__			,						temperature=1.0    )

			self.assertEqual(loaded_config.temperature			,						1.0    )
			self.assertEqual(loaded_config.do_sample			,						lowerCAmelCase__    )
			self.assertEqual(loaded_config.num_beams			,						1    )  # default value






@is_staging_test
class 						__magic_name__    (       unittest.TestCase    ):


	@classmethod
	def _UpperCamelCase	(						cls		: Optional[int]    )    ->      Union[str, Any]:
			UpperCAmelCase					      =  TOKEN
			HfFolder.save_token(lowerCAmelCase__    )


	@classmethod
	def _UpperCamelCase	(						cls		: List[str]    )    ->      int:
			try:
					delete_repo(token=cls._token			,						repo_id="test-generation-config"    )
			except HTTPError:
					pass

			try:
					delete_repo(token=cls._token			,						repo_id="valid_org/test-generation-config-org"    )
			except HTTPError:
					pass


	def _UpperCamelCase	(						self		: List[Any]    )    ->      Any:
			UpperCAmelCase					      =  GenerationConfig(
			    do_sample=lowerCAmelCase__			,						temperature=0.7			,						length_penalty=1.0			,						)
			config.push_to_hub("test-generation-config"			,						use_auth_token=self._token    )

			UpperCAmelCase					      =  GenerationConfig.from_pretrained(f"{USER}/test-generation-config"    )
			for k, v in config.to_dict().items():
					if k != "transformers_version":
							self.assertEqual(lowerCAmelCase__			,						getattr(lowerCAmelCase__			,						lowerCAmelCase__    )    )

							 # Reset repo
			delete_repo(token=self._token			,						repo_id="test-generation-config"    )

			# Push to hub via save_pretrained
			with tempfile.TemporaryDirectory() as tmp_dir:
					config.save_pretrained(
					    lowerCAmelCase__			,						repo_id="test-generation-config"			,						push_to_hub=lowerCAmelCase__			,						use_auth_token=self._token    )

			UpperCAmelCase					      =  GenerationConfig.from_pretrained(f"{USER}/test-generation-config"    )
			for k, v in config.to_dict().items():
					if k != "transformers_version":
							self.assertEqual(lowerCAmelCase__			,						getattr(lowerCAmelCase__			,						lowerCAmelCase__    )    )


	def _UpperCamelCase	(						self		: Any    )    ->      Dict:
			UpperCAmelCase					      =  GenerationConfig(
			    do_sample=lowerCAmelCase__			,						temperature=0.7			,						length_penalty=1.0			,						)
			config.push_to_hub("valid_org/test-generation-config-org"			,						use_auth_token=self._token    )

			UpperCAmelCase					      =  GenerationConfig.from_pretrained("valid_org/test-generation-config-org"    )
			for k, v in config.to_dict().items():
					if k != "transformers_version":
							self.assertEqual(lowerCAmelCase__			,						getattr(lowerCAmelCase__			,						lowerCAmelCase__    )    )

							 # Reset repo
			delete_repo(token=self._token			,						repo_id="valid_org/test-generation-config-org"    )

			# Push to hub via save_pretrained
			with tempfile.TemporaryDirectory() as tmp_dir:
					config.save_pretrained(
					    lowerCAmelCase__			,						repo_id="valid_org/test-generation-config-org"			,						push_to_hub=lowerCAmelCase__			,						use_auth_token=self._token    )

			UpperCAmelCase					      =  GenerationConfig.from_pretrained("valid_org/test-generation-config-org"    )
			for k, v in config.to_dict().items():
					if k != "transformers_version":
							self.assertEqual(lowerCAmelCase__			,						getattr(lowerCAmelCase__			,						lowerCAmelCase__    )    )






from ...configuration_utils import PretrainedConfig
from ...utils import logging
from ...utils.backbone_utils import BackboneConfigMixin, get_aligned_output_features_output_indices


lowerCAmelCase__       = logging.get_logger(__name__)

lowerCAmelCase__       = {
    "facebook/convnextv2-tiny-1k-224": "https://huggingface.co/facebook/convnextv2-tiny-1k-224/resolve/main/config.json",
}






class 						__magic_name__    (       _snake_case	,						_snake_case    ):
 UpperCAmelCase							    =						"""convnextv2"""


 def __init__(						self		: Optional[Any]			,						lowerCAmelCase__		: List[Any]=3			,						lowerCAmelCase__		: str=4			,						lowerCAmelCase__		: Dict=4			,						lowerCAmelCase__		: Optional[Any]=None			,						lowerCAmelCase__		: Optional[Any]=None			,						lowerCAmelCase__		: str="gelu"			,						lowerCAmelCase__		: Optional[int]=0.02			,						lowerCAmelCase__		: Dict=1e-1_2			,						lowerCAmelCase__		: str=0.0			,						lowerCAmelCase__		: str=2_2_4			,						lowerCAmelCase__		: int=None			,						lowerCAmelCase__		: List[Any]=None			,						**lowerCAmelCase__		: List[Any]			,						)    ->      List[Any]:
   super().__init__(**lowerCAmelCase__    )

   UpperCAmelCase					      =  num_channels
   UpperCAmelCase					      =  patch_size
   UpperCAmelCase					      =  num_stages
   UpperCAmelCase					      =  [9_6, 1_9_2, 3_8_4, 7_6_8] if hidden_sizes is None else hidden_sizes
   UpperCAmelCase					      =  [3, 3, 9, 3] if depths is None else depths
   UpperCAmelCase					      =  hidden_act
   UpperCAmelCase					      =  initializer_range
   UpperCAmelCase					      =  layer_norm_eps
   UpperCAmelCase					      =  drop_path_rate
   UpperCAmelCase					      =  image_size
   UpperCAmelCase					      =  ["stem"] + [f"stage{idx}" for idx in range(1			,						len(self.depths    ) + 1    )]
   UpperCAmelCase      ,	UpperCAmelCase					      =  get_aligned_output_features_output_indices(
       out_features=lowerCAmelCase__			,						out_indices=lowerCAmelCase__			,						stage_names=self.stage_names    )



