





"""simple docstring"""


import os
import tempfile
import unittest

from transformers import is_torch_available
from transformers.testing_utils import require_torch


if is_torch_available():
		import torch
		from torch import nn

		from transformers import (
		    Adafactor,
		    AdamW,
		    get_constant_schedule,
		    get_constant_schedule_with_warmup,
		    get_cosine_schedule_with_warmup,
		    get_cosine_with_hard_restarts_schedule_with_warmup,
		    get_inverse_sqrt_schedule,
		    get_linear_schedule_with_warmup,
		    get_polynomial_decay_schedule_with_warmup,
		)

def      UpperCamelCase							(SCREAMING_SNAKE_CASE							,     SCREAMING_SNAKE_CASE=10     ):
						UpperCamelCase				:		Dict				   =			[]
						for _ in range(SCREAMING_SNAKE_CASE     ):
												lrs.append(scheduler.get_lr()[0]     )
												scheduler.step()
						return lrs

def      UpperCamelCase							(SCREAMING_SNAKE_CASE							,     SCREAMING_SNAKE_CASE=10     ):
						UpperCamelCase				:		Any				   =			[]
						for step in range(SCREAMING_SNAKE_CASE     ):
												lrs.append(scheduler.get_lr()[0]     )
												scheduler.step()
												if step == num_steps // 2:
																		with tempfile.TemporaryDirectory() as tmpdirname:
																								UpperCamelCase				:		int				   =			os.path.join(SCREAMING_SNAKE_CASE							,     """schedule.bin"""     )
																								torch.save(scheduler.state_dict()							,     SCREAMING_SNAKE_CASE     )

																								UpperCamelCase				:		Union[str, Any]				   =			torch.load(SCREAMING_SNAKE_CASE     )
																								scheduler.load_state_dict(SCREAMING_SNAKE_CASE     )
						return lrs






@require_torch
class 							lowercase__			(   unittest.TestCase	):

			"""simple docstring"""







			def   _a      (      self						,   _A						,   _A						,   _A    ):






									'''simple docstring'''







									self.assertEqual(len(_A    )						,   len(_A    )    )
									for a, b in zip(_A						,   _A    ):
															self.assertAlmostEqual(_A						,   _A						,   delta=_A    )







			def   _a      (      self    ):






									'''simple docstring'''







									UpperCamelCase				:		Union[str, Any]				   =			torch.tensor([0.1, -0.2, -0.1]						,   requires_grad=_A    )
									UpperCamelCase				:		List[str]				   =			torch.tensor([0.4, 0.2, -0.5]    )
									UpperCamelCase				:		Optional[int]				   =			nn.MSELoss()
									# No warmup, constant schedule, no gradient clipping
									UpperCamelCase				:		Tuple				   =			AdamW(params=[w]						,   lr=2e-1						,   weight_decay=0.0    )
									for _ in range(1_0_0    ):
															UpperCamelCase				:		Optional[int]				   =			criterion(_A						,   _A    )
															loss.backward()
															optimizer.step()
															w.grad.detach_()  # No zero_grad() function on simple tensors. we do it ourselves.
															w.grad.zero_()
									self.assertListAlmostEqual(w.tolist()						,   [0.4, 0.2, -0.5]						,   tol=1e-2    )







			def   _a      (      self    ):






									'''simple docstring'''







									UpperCamelCase				:		str				   =			torch.tensor([0.1, -0.2, -0.1]						,   requires_grad=_A    )
									UpperCamelCase				:		List[Any]				   =			torch.tensor([0.4, 0.2, -0.5]    )
									UpperCamelCase				:		Union[str, Any]				   =			nn.MSELoss()
									# No warmup, constant schedule, no gradient clipping
									UpperCamelCase				:		Optional[Any]				   =			Adafactor(
									    params=[w]						,   lr=1e-2						,   eps=(1e-3_0, 1e-3)						,   clip_threshold=1.0						,   decay_rate=-0.8						,   betaa=_A						,   weight_decay=0.0						,   relative_step=_A						,   scale_parameter=_A						,   warmup_init=_A						,   )
									for _ in range(1_0_0_0    ):
															UpperCamelCase				:		Dict				   =			criterion(_A						,   _A    )
															loss.backward()
															optimizer.step()
															w.grad.detach_()  # No zero_grad() function on simple tensors. we do it ourselves.
															w.grad.zero_()
									self.assertListAlmostEqual(w.tolist()						,   [0.4, 0.2, -0.5]						,   tol=1e-2    )






@require_torch
class 							lowercase__			(   unittest.TestCase	):

			"""simple docstring"""
			__lowerCAmelCase     :				List[Any]										=  nn.Linear(50					,     50	) if is_torch_available() else None
			__lowerCAmelCase     :				str										=  AdamW(m.parameters()					,     lr=10.0	) if is_torch_available() else None
			__lowerCAmelCase     :				int										=  10







			def   _a      (      self						,   _A						,   _A						,   _A						,   _A=None    ):






									'''simple docstring'''







									self.assertEqual(len(_A    )						,   len(_A    )    )
									for a, b in zip(_A						,   _A    ):
															self.assertAlmostEqual(_A						,   _A						,   delta=_A						,   msg=_A    )







			def   _a      (      self    ):






									'''simple docstring'''







									UpperCamelCase				:		List[str]				   =			{"""num_warmup_steps""": 2, """num_training_steps""": 1_0}
									# schedulers doct format
									# function: (sched_args_dict, expected_learning_rates)
									UpperCamelCase				:		str				   =			{
									    get_constant_schedule: ({}, [10.0] * self.num_steps),
									    get_constant_schedule_with_warmup: (
									        {"""num_warmup_steps""": 4},
									        [0.0, 2.5, 5.0, 7.5, 10.0, 10.0, 10.0, 10.0, 10.0, 10.0],
									    ),
									    get_linear_schedule_with_warmup: (
									        {**common_kwargs},
									        [0.0, 5.0, 10.0, 8.75, 7.5, 6.25, 5.0, 3.75, 2.5, 1.25],
									    ),
									    get_cosine_schedule_with_warmup: (
									        {**common_kwargs},
									        [0.0, 5.0, 10.0, 9.61, 8.53, 6.91, 5.0, 3.08, 1.46, 0.38],
									    ),
									    get_cosine_with_hard_restarts_schedule_with_warmup: (
									        {**common_kwargs, """num_cycles""": 2},
									        [0.0, 5.0, 10.0, 8.53, 5.0, 1.46, 10.0, 8.53, 5.0, 1.46],
									    ),
									    get_polynomial_decay_schedule_with_warmup: (
									        {**common_kwargs, """power""": 2.0, """lr_end""": 1e-7},
									        [0.0, 5.0, 10.0, 7.6_56, 5.6_25, 3.9_06, 2.5, 1.4_06, 0.6_25, 0.1_56],
									    ),
									    get_inverse_sqrt_schedule: (
									        {"""num_warmup_steps""": 2},
									        [0.0, 5.0, 10.0, 8.1_65, 7.0_71, 6.3_25, 5.7_74, 5.3_45, 5.0, 4.7_14],
									    ),
									}

									for scheduler_func, data in scheds.items():
															UpperCamelCase   ,						UpperCamelCase				:		List[str]				   =			data

															UpperCamelCase				:		Any				   =			scheduler_func(self.optimizer						,   **_A    )
															self.assertEqual(len([scheduler.get_lr()[0]]    )						,   1    )
															UpperCamelCase				:		Union[str, Any]				   =			unwrap_schedule(_A						,   self.num_steps    )
															self.assertListAlmostEqual(
															    _A						,   _A						,   tol=1e-2						,   msg=f"""failed for {scheduler_func} in normal scheduler"""						,   )

															UpperCamelCase				:		List[Any]				   =			scheduler_func(self.optimizer						,   **_A    )
															if scheduler_func.__name__ != "get_constant_schedule":
																					LambdaScheduleWrapper.wrap_scheduler(_A    )  # wrap to test picklability of the schedule
															UpperCamelCase				:		List[Any]				   =			unwrap_and_save_reload_schedule(_A						,   self.num_steps    )
															self.assertListEqual(_A						,   _A						,   msg=f"""failed for {scheduler_func} in save and reload"""    )






class 							lowercase__			:

			"""simple docstring"""







			def __init__(      self						,   _A    ):






									'''simple docstring'''







									UpperCamelCase				:		List[Any]				   =			fn







			def __call__(      self						,   *_A						,   **_A    ):






									'''simple docstring'''







									return self.fn(*_A						,   **_A    )







			@classmethod
			def   _a      (      self						,   _A    ):






									'''simple docstring'''







									UpperCamelCase				:		List[Any]				   =			list(map(self						,   scheduler.lr_lambdas    )    )










"""simple docstring"""


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


__magic_name__       :					Any						=			logging.get_logger(__name__)

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






class 							lowercase__			(   __SCREAMING_SNAKE_CASE					,     __SCREAMING_SNAKE_CASE	):

			"""simple docstring"""
			__lowerCAmelCase     :				str										=  """convnextv2"""







			def __init__(      self						,   _A=3						,   _A=4						,   _A=4						,   _A=None						,   _A=None						,   _A="gelu"						,   _A=0.02						,   _A=1e-1_2						,   _A=0.0						,   _A=2_2_4						,   _A=None						,   _A=None						,   **_A						,   ):






									'''simple docstring'''







									super().__init__(**_A    )

									UpperCamelCase				:		Optional[Any]				   =			num_channels
									UpperCamelCase				:		int				   =			patch_size
									UpperCamelCase				:		Dict				   =			num_stages
									UpperCamelCase				:		Tuple				   =			[9_6, 1_9_2, 3_8_4, 7_6_8] if hidden_sizes is None else hidden_sizes
									UpperCamelCase				:		str				   =			[3, 3, 9, 3] if depths is None else depths
									UpperCamelCase				:		List[str]				   =			hidden_act
									UpperCamelCase				:		List[str]				   =			initializer_range
									UpperCamelCase				:		List[Any]				   =			layer_norm_eps
									UpperCamelCase				:		Any				   =			drop_path_rate
									UpperCamelCase				:		Any				   =			image_size
									UpperCamelCase				:		Union[str, Any]				   =			["""stem"""] + [f"""stage{idx}""" for idx in range(1						,   len(self.depths    ) + 1    )]
									UpperCamelCase   ,						UpperCamelCase				:		Optional[int]				   =			get_aligned_output_features_output_indices(
									    out_features=_A						,   out_indices=_A						,   stage_names=self.stage_names    )



