


'''simple docstring'''






import unittest
from functools import partial

import torch

from accelerate import Accelerator, debug_launcher
from accelerate.state import AcceleratorState, GradientState
from accelerate.test_utils import require_cpu, require_huggingface_suite
from accelerate.utils import GradientAccumulationPlugin





def    UpperCAmelCase					(						lowerCamelCase_     :Tuple=2 ,					lowerCamelCase_     :Tuple=True ,					lowerCamelCase_     :Optional[Any]=False       ):







		'''simple docstring'''
		snake_case_						:							Optional[int]					=  Accelerator(step_scheduler_with_optimizer=__A ,					split_batches=__A       )
		snake_case_						:							List[str]					=  torch.nn.Linear(2 ,					4       )
		snake_case_						:							Dict					=  torch.optim.AdamW(model.parameters() ,					lr=1.0       )
		snake_case_						:							str					=  torch.optim.lr_scheduler.OneCycleLR(__A ,					max_lr=0.01 ,					steps_per_epoch=2 ,					epochs=1       )
		snake_case_						:							Optional[Any]					=  accelerator.prepare(__A ,					__A ,					__A       )

		# Optimizer has stepped
		scheduler.step()
		if step_scheduler_with_optimizer or (num_processes == 1):
				assert (
				    scheduler.scheduler.last_epoch == num_processes
				), F'''Last Epoch ({scheduler.scheduler.last_epoch}) != Num Processes ({num_processes})'''
		else:
				assert (
				    scheduler.scheduler.last_epoch != num_processes
				), F'''Last Epoch ({scheduler.scheduler.last_epoch}) == Num Processes ({num_processes})'''





def    UpperCAmelCase					(						lowerCamelCase_     :List[Any]=2 ,					lowerCamelCase_     :str=True ,					lowerCamelCase_     :List[Any]=False       ):







		'''simple docstring'''
		snake_case_						:							int					=  Accelerator(step_scheduler_with_optimizer=__A ,					split_batches=__A       )
		snake_case_						:							str					=  torch.nn.Linear(2 ,					4       )
		snake_case_						:							Optional[int]					=  torch.optim.AdamW(model.parameters() ,					lr=1.0       )
		snake_case_						:							Dict					=  torch.optim.lr_scheduler.LambdaLR(__A ,					lr_lambda=lambda lowerCamelCase_       : 1 - n / 10       )
		snake_case_						:							Union[str, Any]					=  accelerator.prepare(__A ,					__A ,					__A       )

		# Optimizer has stepped
		snake_case_						:							List[str]					=  False
		scheduler.step()
		snake_case_						:							List[str]					=  1 - (num_processes if (step_scheduler_with_optimizer and not split_batches) else 1) / 10
		assert (
		    scheduler.get_last_lr()[0] == expected_lr
		), F'''Wrong lr found at first step, expected {expected_lr}, got {scheduler.get_last_lr()[0]}'''

		# Optimizer has not stepped
		snake_case_						:							int					=  True
		scheduler.step()
		if not step_scheduler_with_optimizer:
				snake_case_						:							Union[str, Any]					=  1 - 2 / 10
		assert (
		    scheduler.get_last_lr()[0] == expected_lr
		), F'''Wrong lr found at second step, expected {expected_lr}, got {scheduler.get_last_lr()[0]}'''





def    UpperCAmelCase					(						lowerCamelCase_     :int = 2       ):







		'''simple docstring'''
		from transformers import get_linear_schedule_with_warmup

		snake_case_						:							Any					=  [1, 2, 4]
		for num_steps in steps:
				snake_case_						:							Dict					=  GradientAccumulationPlugin(num_steps=__A ,					adjust_scheduler=num_steps > 1       )
				snake_case_						:							str					=  Accelerator(gradient_accumulation_plugin=__A       )
				snake_case_						:							List[str]					=  torch.nn.Linear(2 ,					4       )
				snake_case_						:							List[str]					=  torch.optim.AdamW(model.parameters() ,					lr=10.0       )
				snake_case_						:							Tuple					=  get_linear_schedule_with_warmup(optimizer=__A ,					num_warmup_steps=0 ,					num_training_steps=20       )

				snake_case_						:							Optional[int]					=  accelerator.prepare(__A ,					__A ,					__A       )

				for i in range(10 * num_steps       ):
						with accelerator.accumulate(__A       ):
								optimizer.step()
								scheduler.step()

						if i == (10 * num_steps - 2):
								assert (
								    scheduler.get_last_lr()[0] != 0
								), F'''Wrong lr found at second-to-last step, expected non-zero, got {scheduler.get_last_lr()[0]}. num_steps: {num_steps}'''
				assert (
				    scheduler.get_last_lr()[0] == 0
				), F'''Wrong lr found at last step, expected 0, got {scheduler.get_last_lr()[0]}'''
				GradientState._reset_state()






@require_cpu
class   __UpperCamelCase		(  unittest.TestCase							):





							def 						a__      (     self						:str	):
									debug_launcher(partial(_UpperCamelCase     ,num_processes=1	)     ,num_processes=1	)
									debug_launcher(partial(_UpperCamelCase     ,num_processes=1     ,split_batches=_UpperCamelCase	)     ,num_processes=1	)





							def 						a__      (     self						:Dict	):
									debug_launcher(partial(_UpperCamelCase     ,num_processes=1	)     ,num_processes=1	)
									debug_launcher(partial(_UpperCamelCase     ,num_processes=1     ,split_batches=_UpperCamelCase	)     ,num_processes=1	)





							def 						a__      (     self						:Dict	):
									debug_launcher(partial(_UpperCamelCase     ,num_processes=1     ,step_scheduler_with_optimizer=_UpperCamelCase	)     ,num_processes=1	)





							def 						a__      (     self						:Dict	):
									debug_launcher(partial(_UpperCamelCase     ,num_processes=1     ,step_scheduler_with_optimizer=_UpperCamelCase	)     ,num_processes=1	)





							def 						a__      (     self						:List[Any]	):
									AcceleratorState._reset_state(_UpperCamelCase	)
									debug_launcher(_UpperCamelCase	)
									debug_launcher(partial(_UpperCamelCase     ,num_processes=1     ,split_batches=_UpperCamelCase	)     ,num_processes=1	)





							def 						a__      (     self						:Optional[int]	):
									AcceleratorState._reset_state(_UpperCamelCase	)
									debug_launcher(_UpperCamelCase	)
									debug_launcher(partial(_UpperCamelCase     ,num_processes=1     ,split_batches=_UpperCamelCase	)     ,num_processes=1	)





							def 						a__      (     self						:Dict	):
									AcceleratorState._reset_state(_UpperCamelCase	)
									debug_launcher(partial(_UpperCamelCase     ,step_scheduler_with_optimizer=_UpperCamelCase	)	)





							def 						a__      (     self						:Union[str, Any]	):
									AcceleratorState._reset_state(_UpperCamelCase	)
									debug_launcher(partial(_UpperCamelCase     ,step_scheduler_with_optimizer=_UpperCamelCase	)	)





							@require_huggingface_suite
							def 						a__      (     self						:Union[str, Any]	):
									AcceleratorState._reset_state(_UpperCamelCase	)
									debug_launcher(partial(_UpperCamelCase     ,num_processes=1	)	)
									debug_launcher(_UpperCamelCase	)


from math import sqrt





def 	snake_case_      (__A     :						int     )			->	list[int]:
   __lowerCAmelCase  :			List[str]					    =						[1]
   for i in range(2					,   int(sqrt(__A     )     ) + 1					,   1     ):
      if number % i == 0:
         values.append(__A     )
         if int(number // i     ) != i:
            values.append(int(number // i     )     )
   return sorted(__A     )





def 	snake_case_      (__A     :						int     )			->	bool:
   return sum(factors(__A     )     ) > n





def 	snake_case_      (__A     :						int     )			->	bool:
   __lowerCAmelCase  :			Any					    =						factors(__A     )
   __lowerCAmelCase  :			Union[str, Any]					    =						len(__A     )
   __lowerCAmelCase  :			str					    =						[[0 for i in range(number + 1     )] for j in range(r + 1     )]
   for i in range(r + 1     ):
      __lowerCAmelCase  :			Tuple					    =						True

   for i in range(1					,   number + 1     ):
      __lowerCAmelCase  :			List[str]					    =						False

   for i in range(1					,   r + 1     ):
      for j in range(1					,   number + 1     ):
         if j < values[i - 1]:
            __lowerCAmelCase  :			Dict					    =						subset[i - 1][j]
         else:
            __lowerCAmelCase  :			Dict					    =						subset[i - 1][j] or subset[i - 1][j - values[i - 1]]

   return subset[r][number] != 0





def 	snake_case_      (__A     :						int     )			->	bool:
   return abundant(__A     ) and not semi_perfect(__A     )


if __name__ == "__main__":
   import doctest

   doctest.testmod(verbose=True)
   for number in (69, 70, 71):
      print(F'{number} is {"" if weird(number) else "not "}weird.')


