'''simple docstring'''

from __future__ import annotations



def snake_case_      (					__snake_case  :							int)			->					list[int]:
   lowerCAmelCase_			=      [True] * limit
   lowerCAmelCase_			=      False
   lowerCAmelCase_			=      False
   lowerCAmelCase_			=      True

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

   lowerCAmelCase_			=      [2]

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

   return primes



def snake_case_      (					__snake_case  :							int = 1000000)			->					int:
   lowerCAmelCase_			=      prime_sieve(__snake_case)
   lowerCAmelCase_			=      0
   lowerCAmelCase_			=      0

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

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

   return largest


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






'''simple docstring'''

import json
import os
import tempfile
from unittest.mock import patch

import torch
from torch.utils.data import DataLoader, TensorDataset

from accelerate import DistributedType, infer_auto_device_map, init_empty_weights
from accelerate.accelerator import Accelerator
from accelerate.state import GradientState, PartialState
from accelerate.test_utils import require_bnb, require_multi_gpu, slow
from accelerate.test_utils.testing import AccelerateTestCase, require_cuda
from accelerate.utils import patch_environment



def snake_case_      (					)			->					str:
   lowerCAmelCase_			=      torch.nn.Linear(2	,   4)
   lowerCAmelCase_			=      torch.optim.AdamW(model.parameters()	,   lr=1.0)
   lowerCAmelCase_			=      torch.optim.lr_scheduler.OneCycleLR(__snake_case	,   max_lr=0.0_1	,   steps_per_epoch=2	,   epochs=1)
   lowerCAmelCase_			=      DataLoader(TensorDataset(torch.tensor([1, 2, 3])))
   lowerCAmelCase_			=      DataLoader(TensorDataset(torch.tensor([4, 5, 6])))

   return model, optimizer, scheduler, train_dl, valid_dl



def snake_case_      (					__snake_case  :							Any)			->					List[Any]:
   return (model.weight.abs().sum() + model.bias.abs().sum()).item()



def snake_case_      (					__snake_case  :							Optional[int])			->					Any:
   lowerCAmelCase_			=      torch.nn.Linear(*tuple(model.weight.T.shape)).state_dict()
   model.load_state_dict(__snake_case)






class     __UpperCAmelCase       ( __a		):





  @require_cuda
  def 						UpperCAmelCase_	(	self ):
     lowerCAmelCase_			=      Accelerator()
     assert PartialState._shared_state["_cpu"] is False
     assert PartialState._shared_state["device"].type == "cuda"
     with self.assertRaises(_lowerCamelCase ):
        lowerCAmelCase_			=      Accelerator(cpu=_lowerCamelCase )





  def 						UpperCAmelCase_	(	self ):
     lowerCAmelCase_			=      Accelerator()
     lowerCAmelCase_			=      GradientState()
     assert state.num_steps == 1
     lowerCAmelCase_			=      4
     assert state.num_steps == 4

     assert state.sync_gradients is True
     lowerCAmelCase_			=      False
     assert state.sync_gradients is False
     GradientState._reset_state()





  def 						UpperCAmelCase_	(	self ):
     lowerCAmelCase_			=      Accelerator()
     lowerCAmelCase_	,lowerCAmelCase_	,lowerCAmelCase_	,lowerCAmelCase_	,lowerCAmelCase_			=      create_components()

     (
         (
         lowerCAmelCase_
     )	,(
         lowerCAmelCase_
     )	,(
         lowerCAmelCase_
     )	,(
         lowerCAmelCase_
     )	,(
         lowerCAmelCase_
     )	,
     )			=      accelerator.prepare(_lowerCamelCase    ,     _lowerCamelCase    ,     _lowerCamelCase    ,     _lowerCamelCase    ,     _lowerCamelCase )

     self.assertTrue(prepared_model in accelerator._models )
     self.assertTrue(prepared_optimizer in accelerator._optimizers )
     self.assertTrue(prepared_scheduler in accelerator._schedulers )
     self.assertTrue(prepared_train_dl in accelerator._dataloaders )
     self.assertTrue(prepared_valid_dl in accelerator._dataloaders )





  def 						UpperCAmelCase_	(	self ):
     lowerCAmelCase_			=      Accelerator()
     lowerCAmelCase_	,lowerCAmelCase_	,lowerCAmelCase_	,lowerCAmelCase_	,lowerCAmelCase_			=      create_components()
     accelerator.prepare(_lowerCamelCase    ,     _lowerCamelCase    ,     _lowerCamelCase    ,     _lowerCamelCase    ,     _lowerCamelCase )
     accelerator.free_memory()

     self.assertTrue(len(accelerator._models ) == 0 )
     self.assertTrue(len(accelerator._optimizers ) == 0 )
     self.assertTrue(len(accelerator._schedulers ) == 0 )
     self.assertTrue(len(accelerator._dataloaders ) == 0 )





  def 						UpperCAmelCase_	(	self ):
     PartialState._reset_state()

     # Mock torch.cuda.set_device to avoid an exception as the device doesn't exist
     def noop(*_lowerCamelCase    ,     **_lowerCamelCase ):
        pass

     with patch('''torch.cuda.set_device'''    ,     _lowerCamelCase ), patch_environment(ACCELERATE_TORCH_DEVICE='''cuda:64''' ):
        lowerCAmelCase_			=      Accelerator()
        self.assertEqual(str(accelerator.state.device )    ,     '''cuda:64''' )





  def 						UpperCAmelCase_	(	self ):
     lowerCAmelCase_			=      Accelerator()
     lowerCAmelCase_	,lowerCAmelCase_	,lowerCAmelCase_	,lowerCAmelCase_	,lowerCAmelCase_			=      create_components()
     accelerator.prepare(_lowerCamelCase    ,     _lowerCamelCase    ,     _lowerCamelCase    ,     _lowerCamelCase    ,     _lowerCamelCase )

     lowerCAmelCase_			=      get_signature(_lowerCamelCase )

     with tempfile.TemporaryDirectory() as tmpdirname:
        accelerator.save_state(_lowerCamelCase )

        # make sure random weights don't match
        load_random_weights(_lowerCamelCase )
        self.assertTrue(abs(model_signature - get_signature(_lowerCamelCase ) ) > 1E-3 )

        # make sure loaded weights match
        accelerator.load_state(_lowerCamelCase )
        self.assertTrue(abs(model_signature - get_signature(_lowerCamelCase ) ) < 1E-3 )





  def 						UpperCAmelCase_	(	self ):
     lowerCAmelCase_			=      Accelerator()
     lowerCAmelCase_	,lowerCAmelCase_	,lowerCAmelCase_	,lowerCAmelCase_	,lowerCAmelCase_			=      create_components()
     accelerator.prepare(_lowerCamelCase    ,     _lowerCamelCase    ,     _lowerCamelCase    ,     _lowerCamelCase    ,     _lowerCamelCase )

     lowerCAmelCase_			=      get_signature(_lowerCamelCase )

     # saving hook
     def save_config(_lowerCamelCase    ,     _lowerCamelCase    ,     _lowerCamelCase ):
        lowerCAmelCase_			=      {'''class_name''': models[0].__class__.__name__}

        with open(os.path.join(_lowerCamelCase    ,     '''data.json''' )    ,     '''w''' ) as f:
           json.dump(_lowerCamelCase    ,     _lowerCamelCase )

        # loading hook
     def load_config(_lowerCamelCase    ,     _lowerCamelCase ):
        with open(os.path.join(_lowerCamelCase    ,     '''data.json''' )    ,     '''r''' ) as f:
           lowerCAmelCase_			=      json.load(_lowerCamelCase )

        lowerCAmelCase_			=      config['''class_name''']

     lowerCAmelCase_			=      accelerator.register_save_state_pre_hook(_lowerCamelCase )
     lowerCAmelCase_			=      accelerator.register_load_state_pre_hook(_lowerCamelCase )

     with tempfile.TemporaryDirectory() as tmpdirname:
        accelerator.save_state(_lowerCamelCase )

        # make sure random weights don't match with hooks
        load_random_weights(_lowerCamelCase )
        self.assertTrue(abs(model_signature - get_signature(_lowerCamelCase ) ) > 1E-3 )

        # random class name to verify correct one is loaded
        lowerCAmelCase_			=      '''random'''

        # make sure loaded weights match with hooks
        accelerator.load_state(_lowerCamelCase )
        self.assertTrue(abs(model_signature - get_signature(_lowerCamelCase ) ) < 1E-3 )

        # mode.class_name is loaded from config
        self.assertTrue(model.class_name == model.__class__.__name__ )

     # remove hooks
     save_hook.remove()
     load_hook.remove()

     with tempfile.TemporaryDirectory() as tmpdirname:
        accelerator.save_state(_lowerCamelCase )

        # make sure random weights don't match with hooks removed
        load_random_weights(_lowerCamelCase )
        self.assertTrue(abs(model_signature - get_signature(_lowerCamelCase ) ) > 1E-3 )

        # random class name to verify correct one is loaded
        lowerCAmelCase_			=      '''random'''

        # make sure loaded weights match with hooks removed
        accelerator.load_state(_lowerCamelCase )
        self.assertTrue(abs(model_signature - get_signature(_lowerCamelCase ) ) < 1E-3 )

        # mode.class_name is NOT loaded from config
        self.assertTrue(model.class_name != model.__class__.__name__ )





  def 						UpperCAmelCase_	(	self ):
     lowerCAmelCase_			=      Accelerator()
     lowerCAmelCase_	,lowerCAmelCase_	,lowerCAmelCase_	,lowerCAmelCase_	,lowerCAmelCase_			=      create_components()
     lowerCAmelCase_			=      None

     # This should work
     lowerCAmelCase_	,lowerCAmelCase_	,lowerCAmelCase_	,lowerCAmelCase_	,lowerCAmelCase_	,lowerCAmelCase_			=      accelerator.prepare(
         _lowerCamelCase    ,     _lowerCamelCase    ,     _lowerCamelCase    ,     _lowerCamelCase    ,     _lowerCamelCase    ,     _lowerCamelCase )
     self.assertTrue(dummy_obj is None )





  def 						UpperCAmelCase_	(	self ):
     lowerCAmelCase_			=      Accelerator()
     lowerCAmelCase_	,lowerCAmelCase_	,lowerCAmelCase_	,lowerCAmelCase_	,lowerCAmelCase_			=      create_components()
     lowerCAmelCase_			=      [1, 2, 3]

     # This should work
     lowerCAmelCase_	,lowerCAmelCase_	,lowerCAmelCase_	,lowerCAmelCase_	,lowerCAmelCase_	,lowerCAmelCase_			=      accelerator.prepare(
         _lowerCamelCase    ,     _lowerCamelCase    ,     _lowerCamelCase    ,     _lowerCamelCase    ,     _lowerCamelCase    ,     _lowerCamelCase )
     self.assertEqual(
         getattr(_lowerCamelCase    ,     '''_is_accelerate_prepared'''    ,     _lowerCamelCase )    ,     _lowerCamelCase    ,     '''Dummy object should have `_is_accelerate_prepared` set to `True`'''    ,     )
     self.assertEqual(
         getattr(_lowerCamelCase    ,     '''_is_accelerate_prepared'''    ,     _lowerCamelCase )    ,     _lowerCamelCase    ,     '''Model is missing `_is_accelerator_prepared` or is set to `False`'''    ,     )
     self.assertEqual(
         getattr(_lowerCamelCase    ,     '''_is_accelerate_prepared'''    ,     _lowerCamelCase )    ,     _lowerCamelCase    ,     '''Optimizer is missing `_is_accelerator_prepared` or is set to `False`'''    ,     )
     self.assertEqual(
         getattr(_lowerCamelCase    ,     '''_is_accelerate_prepared'''    ,     _lowerCamelCase )    ,     _lowerCamelCase    ,     '''Scheduler is missing `_is_accelerator_prepared` or is set to `False`'''    ,     )
     self.assertEqual(
         getattr(_lowerCamelCase    ,     '''_is_accelerate_prepared'''    ,     _lowerCamelCase )    ,     _lowerCamelCase    ,     '''Train Dataloader is missing `_is_accelerator_prepared` or is set to `False`'''    ,     )
     self.assertEqual(
         getattr(_lowerCamelCase    ,     '''_is_accelerate_prepared'''    ,     _lowerCamelCase )    ,     _lowerCamelCase    ,     '''Valid Dataloader is missing `_is_accelerator_prepared` or is set to `False`'''    ,     )





  @slow
  @require_bnb
  def 						UpperCAmelCase_	(	self ):
     from transformers import AutoModelForCausalLM

     lowerCAmelCase_			=      AutoModelForCausalLM.from_pretrained(
         '''EleutherAI/gpt-neo-125m'''    ,     load_in_abit=_lowerCamelCase    ,     device_map={'''''': 0}    ,     )
     lowerCAmelCase_			=      Accelerator()

     # This should work
     lowerCAmelCase_			=      accelerator.prepare(_lowerCamelCase )





  @slow
  @require_bnb
  def 						UpperCAmelCase_	(	self ):
     from transformers import AutoModelForCausalLM

     lowerCAmelCase_			=      Accelerator()

     with init_empty_weights():
        lowerCAmelCase_			=      AutoModelForCausalLM.from_pretrained(
            '''EleutherAI/gpt-neo-125m'''    ,     )
        model.tie_weights()
        lowerCAmelCase_			=      infer_auto_device_map(_lowerCamelCase )
        lowerCAmelCase_			=      '''cpu'''

     lowerCAmelCase_			=      AutoModelForCausalLM.from_pretrained(
         '''EleutherAI/gpt-neo-125m'''    ,     device_map=_lowerCamelCase    ,     load_in_abit=_lowerCamelCase    ,     llm_inta_enable_fpaa_cpu_offload=_lowerCamelCase )

     # This should not work and get value error
     with self.assertRaises(_lowerCamelCase ):
        lowerCAmelCase_			=      accelerator.prepare(_lowerCamelCase )





  @slow
  @require_bnb
  @require_multi_gpu
  def 						UpperCAmelCase_	(	self ):
     from transformers import AutoModelForCausalLM

     lowerCAmelCase_			=      {'''distributed_type''': DistributedType.MULTI_GPU}

     with init_empty_weights():
        lowerCAmelCase_			=      AutoModelForCausalLM.from_pretrained(
            '''EleutherAI/gpt-neo-125m'''    ,     )
        model.tie_weights()
        lowerCAmelCase_			=      infer_auto_device_map(_lowerCamelCase )
        lowerCAmelCase_			=      1

     lowerCAmelCase_			=      AutoModelForCausalLM.from_pretrained(
         '''EleutherAI/gpt-neo-125m'''    ,     load_in_abit=_lowerCamelCase    ,     device_map=_lowerCamelCase    ,     )
     lowerCAmelCase_			=      Accelerator()

     # This should not work and get value error
     with self.assertRaises(_lowerCamelCase ):
        lowerCAmelCase_			=      accelerator.prepare(_lowerCamelCase )

     PartialState._reset_state()





  @slow
  @require_bnb
  @require_multi_gpu
  def 						UpperCAmelCase_	(	self ):
     from transformers import AutoModelForCausalLM

     with init_empty_weights():
        lowerCAmelCase_			=      AutoModelForCausalLM.from_pretrained(
            '''EleutherAI/gpt-neo-125m'''    ,     )
        lowerCAmelCase_			=      infer_auto_device_map(_lowerCamelCase )
        lowerCAmelCase_			=      1

     lowerCAmelCase_			=      AutoModelForCausalLM.from_pretrained(
         '''EleutherAI/gpt-neo-125m'''    ,     load_in_abit=_lowerCamelCase    ,     device_map=_lowerCamelCase    ,     )
     lowerCAmelCase_			=      Accelerator()

     # This should work
     lowerCAmelCase_			=      accelerator.prepare(_lowerCamelCase )







  @require_cuda
  def 						UpperCAmelCase_	(	self ):
     lowerCAmelCase_			=      torch.nn.Linear(10    ,     10 )
     lowerCAmelCase_			=      torch.optim.SGD(model.parameters()    ,     lr=0.01 )
     lowerCAmelCase_			=      Accelerator(cpu=_lowerCamelCase )
     lowerCAmelCase_			=      accelerator.prepare(_lowerCamelCase )





