



'''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    lowerCamelCase_     (    lowercase__				,     lowercase__=10):
 lowerCamelCase__  =     []
 for _ in range(lowercase__):
  lrs.append(scheduler.get_lr()[0])
  scheduler.step()
 return lrs






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

    lowerCamelCase__  =     torch.load(lowercase__)
    scheduler.load_state_dict(lowercase__)
 return lrs
@require_torch
class 				lowercase       (							unittest.TestCase      ):


    '''simple docstring'''

    def    a__				(					self     :     Tuple				,     __lowerCamelCase     :     List[str]				,     __lowerCamelCase     :     Any				,     __lowerCamelCase     :     int     )							->							Optional[int]:

     '''simple docstring'''




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

    def    a__				(					self     :     Optional[int]     )							->							Union[str, Any]:

     '''simple docstring'''




     lowerCamelCase__  =     torch.tensor([0.1, -0.2, -0.1]				,     requires_grad=__lowerCamelCase     )
     lowerCamelCase__  =     torch.tensor([0.4, 0.2, -0.5]     )
     lowerCamelCase__  =     nn.MSELoss()
     # No warmup, constant schedule, no gradient clipping
     lowerCamelCase__  =     AdamW(params=[w]				,     lr=2E-1				,     weight_decay=0.0     )
     for _ in range(100     ):
      lowerCamelCase__  =     criterion(__lowerCamelCase				,     __lowerCamelCase     )
      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     :     str     )							->							str:

     '''simple docstring'''




     lowerCamelCase__  =     torch.tensor([0.1, -0.2, -0.1]				,     requires_grad=__lowerCamelCase     )
     lowerCamelCase__  =     torch.tensor([0.4, 0.2, -0.5]     )
     lowerCamelCase__  =     nn.MSELoss()
     # No warmup, constant schedule, no gradient clipping
     lowerCamelCase__  =     Adafactor(
         params=[w]				,     lr=1E-2				,     eps=(1E-30, 1E-3)				,     clip_threshold=1.0				,     decay_rate=-0.8				,     betaa=__lowerCamelCase				,     weight_decay=0.0				,     relative_step=__lowerCamelCase				,     scale_parameter=__lowerCamelCase				,     warmup_init=__lowerCamelCase				,     )
     for _ in range(1000     ):
      lowerCamelCase__  =     criterion(__lowerCamelCase				,     __lowerCamelCase     )
      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__      				=							nn.Linear(50 ,       50      ) if is_torch_available() else None
    lowerCAmelCase__      				=							AdamW(m.parameters() ,       lr=10.0      ) if is_torch_available() else None
    lowerCAmelCase__      				=							10

    def    a__				(					self     :     Union[str, Any]				,     __lowerCamelCase     :     List[str]				,     __lowerCamelCase     :     Any				,     __lowerCamelCase     :     List[Any]				,     __lowerCamelCase     :     Dict=None     )							->							Dict:

     '''simple docstring'''




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




    def    a__				(					self     :     str     )							->							Union[str, Any]:

     '''simple docstring'''




     lowerCamelCase__  =     {"num_warmup_steps": 2, "num_training_steps": 10}
     # schedulers doct format
     # function: (sched_args_dict, expected_learning_rates)
     lowerCamelCase__  =     {
         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.7_5, 7.5, 6.2_5, 5.0, 3.7_5, 2.5, 1.2_5],
         ),
         get_cosine_schedule_with_warmup: (
             {**common_kwargs},
             [0.0, 5.0, 10.0, 9.6_1, 8.5_3, 6.9_1, 5.0, 3.0_8, 1.4_6, 0.3_8],
         ),
         get_cosine_with_hard_restarts_schedule_with_warmup: (
             {**common_kwargs, "num_cycles": 2},
             [0.0, 5.0, 10.0, 8.5_3, 5.0, 1.4_6, 10.0, 8.5_3, 5.0, 1.4_6],
         ),
         get_polynomial_decay_schedule_with_warmup: (
             {**common_kwargs, "power": 2.0, "lr_end": 1E-7},
             [0.0, 5.0, 10.0, 7.6_5_6, 5.6_2_5, 3.9_0_6, 2.5, 1.4_0_6, 0.6_2_5, 0.1_5_6],
         ),
         get_inverse_sqrt_schedule: (
             {"num_warmup_steps": 2},
             [0.0, 5.0, 10.0, 8.1_6_5, 7.0_7_1, 6.3_2_5, 5.7_7_4, 5.3_4_5, 5.0, 4.7_1_4],
         ),
     }

     for scheduler_func, data in scheds.items():
      lowerCamelCase__  ,	lowerCamelCase__  =     data

      lowerCamelCase__  =     scheduler_func(self.optimizer				,     **__lowerCamelCase     )
      self.assertEqual(len([scheduler.get_lr()[0]]     )				,     1     )
      lowerCamelCase__  =     unwrap_schedule(__lowerCamelCase				,     self.num_steps     )
      self.assertListAlmostEqual(
          __lowerCamelCase				,     __lowerCamelCase				,     tol=1E-2				,     msg=f'''failed for {scheduler_func} in normal scheduler'''				,     )

      lowerCamelCase__  =     scheduler_func(self.optimizer				,     **__lowerCamelCase     )
      if scheduler_func.__name__ != "get_constant_schedule":
       LambdaScheduleWrapper.wrap_scheduler(__lowerCamelCase     )  # wrap to test picklability of the schedule
      lowerCamelCase__  =     unwrap_and_save_reload_schedule(__lowerCamelCase				,     self.num_steps     )
      self.assertListEqual(__lowerCamelCase				,     __lowerCamelCase				,     msg=f'''failed for {scheduler_func} in save and reload'''     )
class 				lowercase       :


    '''simple docstring'''

    def __init__(					self     :     List[Any]				,     __lowerCamelCase     :     Optional[Any]     )							->							Any:

     '''simple docstring'''




     lowerCamelCase__  =     fn

    def __call__(					self     :     Dict				,     *__lowerCamelCase     :     Union[str, Any]				,     **__lowerCamelCase     :     Dict     )							->							int:

     '''simple docstring'''




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




    @classmethod
    def    a__				(					self     :     Optional[Any]				,     __lowerCamelCase     :     Union[str, Any]     )							->							Dict:

     '''simple docstring'''




     lowerCamelCase__  =     list(map(self				,     scheduler.lr_lambdas     )     )









'''simple docstring'''





from collections import OrderedDict
from typing import Mapping

from packaging import version

from ...configuration_utils import PretrainedConfig
from ...onnx import OnnxConfig
from ...utils import logging


__A							:							Optional[int]  							=		logging.get_logger(__name__)

__A							:							Tuple  							=		{
    """google/mobilenet_v1_1.0_224""": """https://huggingface.co/google/mobilenet_v1_1.0_224/resolve/main/config.json""",
    """google/mobilenet_v1_0.75_192""": """https://huggingface.co/google/mobilenet_v1_0.75_192/resolve/main/config.json""",
    # See all MobileNetV1 models at https://huggingface.co/models?filter=mobilenet_v1
}
class 				lowercase       (							_lowerCamelCase      ):


    '''simple docstring'''

    lowerCAmelCase__      				=							"mobilenet_v1"

    def __init__(					self     :     Union[str, Any]				,     __lowerCamelCase     :     Tuple=3				,     __lowerCamelCase     :     Any=224				,     __lowerCamelCase     :     Tuple=1.0				,     __lowerCamelCase     :     Optional[int]=8				,     __lowerCamelCase     :     str="relu6"				,     __lowerCamelCase     :     Any=True				,     __lowerCamelCase     :     Union[str, Any]=0.9_9_9				,     __lowerCamelCase     :     List[Any]=0.0_2				,     __lowerCamelCase     :     str=0.0_0_1				,     **__lowerCamelCase     :     str				,     )							->							Dict:

     '''simple docstring'''




     super().__init__(**__lowerCamelCase     )

     if depth_multiplier <= 0:
      raise ValueError("depth_multiplier must be greater than zero."     )

     lowerCamelCase__  =     num_channels
     lowerCamelCase__  =     image_size
     lowerCamelCase__  =     depth_multiplier
     lowerCamelCase__  =     min_depth
     lowerCamelCase__  =     hidden_act
     lowerCamelCase__  =     tf_padding
     lowerCamelCase__  =     classifier_dropout_prob
     lowerCamelCase__  =     initializer_range
     lowerCamelCase__  =     layer_norm_eps
class 				lowercase       (							_lowerCamelCase      ):


    '''simple docstring'''

    lowerCAmelCase__      				=							version.parse("1.11"      )

    @property
    def    a__				(					self     :     List[str]     )							->							Mapping[str, Mapping[int, str]]:

     '''simple docstring'''




     return OrderedDict([("pixel_values", {0: "batch"})]     )

    @property
    def    a__				(					self     :     str     )							->							Mapping[str, Mapping[int, str]]:

     '''simple docstring'''




     if self.task == "image-classification":
      return OrderedDict([("logits", {0: "batch"})]     )
     else:
      return OrderedDict([("last_hidden_state", {0: "batch"}), ("pooler_output", {0: "batch"})]     )




    @property
    def    a__				(					self     :     List[Any]     )							->							float:

     '''simple docstring'''




     return 1E-4

