


'''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_  (       A		,	A=1_0      ):
   '''simple docstring'''





   _a					:  Optional[Any]						=  []
   for _ in range(A__      ):
      lrs.append(scheduler.get_lr()[0]      )
      scheduler.step()
   return lrs


def    UpperCAmelCase_  (       A		,	A=1_0      ):
   '''simple docstring'''





   _a					:  List[str]						=  []
   for step in range(A__      ):
      lrs.append(scheduler.get_lr()[0]      )
      scheduler.step()
      if step == num_steps // 2:
         with tempfile.TemporaryDirectory() as tmpdirname:
            _a					:  str						=  os.path.join(A__		,	'schedule.bin'      )
            torch.save(scheduler.state_dict()		,	A__      )

            _a					:  Tuple						=  torch.load(A__      )
            scheduler.load_state_dict(A__      )
   return lrs







@require_torch
class 					a			(				unittest.TestCase  ):
 '''simple docstring'''




 def   __UpperCamelCase   (    self		,      lowerCamelCase_		,      lowerCamelCase_		,      lowerCamelCase_      )      ->    Tuple:
    self.assertEqual(len(lowerCamelCase_      )		,      len(lowerCamelCase_      )      )
    for a, b in zip(lowerCamelCase_		,      lowerCamelCase_      ):
       self.assertAlmostEqual(lowerCamelCase_		,      lowerCamelCase_		,      delta=lowerCamelCase_      )




 def   __UpperCamelCase   (    self      )      ->    List[str]:
    _a					:  Any						=  torch.tensor([0.1, -0.2, -0.1]		,      requires_grad=lowerCamelCase_      )
    _a					:  Tuple						=  torch.tensor([0.4, 0.2, -0.5]      )
    _a					:  List[str]						=  nn.MSELoss()
    # No warmup, constant schedule, no gradient clipping
    _a					:  Any						=  AdamW(params=[w]		,      lr=2e-1		,      weight_decay=0.0      )
    for _ in range(1_0_0      ):
       _a					:  Dict						=  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   __UpperCamelCase   (    self      )      ->    Any:
    _a					:  Any						=  torch.tensor([0.1, -0.2, -0.1]		,      requires_grad=lowerCamelCase_      )
    _a					:  List[str]						=  torch.tensor([0.4, 0.2, -0.5]      )
    _a					:  List[Any]						=  nn.MSELoss()
    # No warmup, constant schedule, no gradient clipping
    _a					:  Optional[Any]						=  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(1_0_0_0      ):
       _a					:  Any						=  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 					a			(				unittest.TestCase  ):
 '''simple docstring'''




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




 def   __UpperCamelCase   (    self		,      lowerCamelCase_		,      lowerCamelCase_		,      lowerCamelCase_		,      lowerCamelCase_=None      )      ->    Optional[int]:
    self.assertEqual(len(lowerCamelCase_      )		,      len(lowerCamelCase_      )      )
    for a, b in zip(lowerCamelCase_		,      lowerCamelCase_      ):
       self.assertAlmostEqual(lowerCamelCase_		,      lowerCamelCase_		,      delta=lowerCamelCase_		,      msg=lowerCamelCase_      )




 def   __UpperCamelCase   (    self      )      ->    List[str]:
    _a					:  Union[str, Any]						=  {'num_warmup_steps': 2, 'num_training_steps': 1_0}
    # schedulers doct format
    # function: (sched_args_dict, expected_learning_rates)
    _a					:  Union[str, Any]						=  {
        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.656, 5.625, 3.906, 2.5, 1.406, 0.625, 0.156],
        ),
        get_inverse_sqrt_schedule: (
            {'num_warmup_steps': 2},
            [0.0, 5.0, 10.0, 8.165, 7.071, 6.325, 5.774, 5.345, 5.0, 4.714],
        ),
    }

    for scheduler_func, data in scheds.items():
       _a		,						_a					:  List[Any]						=  data

       _a					:  List[Any]						=  scheduler_func(self.optimizer		,      **lowerCamelCase_      )
       self.assertEqual(len([scheduler.get_lr()[0]]      )		,      1      )
       _a					:  Union[str, Any]						=  unwrap_schedule(lowerCamelCase_		,      self.num_steps      )
       self.assertListAlmostEqual(
           lowerCamelCase_		,      lowerCamelCase_		,      tol=1e-2		,      msg=F'''failed for {scheduler_func} in normal scheduler'''		,      )

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







class 					a			:
 '''simple docstring'''




 def __init__(    self		,      lowerCamelCase_      )      ->    Tuple:
    _a					:  List[Any]						=  fn




 def __call__(    self		,      *lowerCamelCase_		,      **lowerCamelCase_      )      ->    Tuple:
    return self.fn(*lowerCamelCase_		,      **lowerCamelCase_      )




 @classmethod
 def   __UpperCamelCase   (    self		,      lowerCamelCase_      )      ->    str:
    _a					:  Optional[int]						=  list(map(self		,      scheduler.lr_lambdas      )      )




'''simple docstring'''
from __future__ import annotations






def      UpperCamelCase_				(      A__						):
	a_      				=						[True] * limit
	a_      				=						False
	a_      				=						False
	a_      				=						True

	for i in range(3  ,      int(limit**0.5 + 1						)  ,      2						):
		a_      				=						i * 2
		while index < limit:
			a_      				=						False
			a_      				=						index + i

	a_      				=						[2]

	for i in range(3  ,      A__  ,      2						):
		if is_prime[i]:
			primes.append(A__						)

	return primes






def      UpperCamelCase_				(      A__ = 1_00_00_00						):
	a_      				=						prime_sieve(A__						)
	a_      				=						0
	a_      				=						0

	for i in range(len(A__						)						):
		for j in range(i + length  ,      len(A__						)						):
			a_      				=						sum(primes[i:j]						)
			if sol >= ceiling:
				break

			if sol in primes:
				a_      				=						j - i
				a_      				=						sol

	return largest


if __name__ == "__main__":
		print(F"""{solution() = }""")
