






"""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 						_SCREAMING_SNAKE_CASE			(   __snake_case      :  str=2		, __snake_case      :  Union[str, Any]=True		, __snake_case      :  Optional[Any]=False   ):





  '''simple docstring'''





  lowercase							   =	Accelerator(step_scheduler_with_optimizer=_A		, split_batches=_A   )
  lowercase							   =	torch.nn.Linear(2		, 4   )
  lowercase							   =	torch.optim.AdamW(model.parameters()		, lr=1.0   )
  lowercase							   =	torch.optim.lr_scheduler.OneCycleLR(_A		, max_lr=0.01		, steps_per_epoch=2		, epochs=1   )
  lowercase   ,					lowercase   ,					lowercase							   =	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 						_SCREAMING_SNAKE_CASE			(   __snake_case      :  List[Any]=2		, __snake_case      :  List[Any]=True		, __snake_case      :  Dict=False   ):





  '''simple docstring'''





  lowercase							   =	Accelerator(step_scheduler_with_optimizer=_A		, split_batches=_A   )
  lowercase							   =	torch.nn.Linear(2		, 4   )
  lowercase							   =	torch.optim.AdamW(model.parameters()		, lr=1.0   )
  lowercase							   =	torch.optim.lr_scheduler.LambdaLR(_A		, lr_lambda=lambda __snake_case   : 1 - n / 10   )
  lowercase   ,					lowercase   ,					lowercase							   =	accelerator.prepare(_A		, _A		, _A   )

  # Optimizer has stepped
  lowercase							   =	False
  scheduler.step()
  lowercase							   =	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
  lowercase							   =	True
  scheduler.step()
  if not step_scheduler_with_optimizer:
    lowercase							   =	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 						_SCREAMING_SNAKE_CASE			(   __snake_case      :  List[str] = 2   ):





  '''simple docstring'''





  from transformers import get_linear_schedule_with_warmup

  lowercase							   =	[1, 2, 4]
  for num_steps in steps:
    lowercase							   =	GradientAccumulationPlugin(num_steps=_A		, adjust_scheduler=num_steps > 1   )
    lowercase							   =	Accelerator(gradient_accumulation_plugin=_A   )
    lowercase							   =	torch.nn.Linear(2		, 4   )
    lowercase							   =	torch.optim.AdamW(model.parameters()		, lr=10.0   )
    lowercase							   =	get_linear_schedule_with_warmup(optimizer=_A		, num_warmup_steps=0		, num_training_steps=20   )

    lowercase   ,					lowercase   ,					lowercase							   =	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 	a					(       unittest.TestCase     ):







     def       UpperCamelCase_				(      self	):
       debug_launcher(partial(__lowercase							,		num_processes=1	)							,		num_processes=1	)
       debug_launcher(partial(__lowercase							,		num_processes=1							,		split_batches=__lowercase	)							,		num_processes=1	)







     def       UpperCamelCase_				(      self	):
       debug_launcher(partial(__lowercase							,		num_processes=1	)							,		num_processes=1	)
       debug_launcher(partial(__lowercase							,		num_processes=1							,		split_batches=__lowercase	)							,		num_processes=1	)







     def       UpperCamelCase_				(      self	):
       debug_launcher(partial(__lowercase							,		num_processes=1							,		step_scheduler_with_optimizer=__lowercase	)							,		num_processes=1	)







     def       UpperCamelCase_				(      self	):
       debug_launcher(partial(__lowercase							,		num_processes=1							,		step_scheduler_with_optimizer=__lowercase	)							,		num_processes=1	)







     def       UpperCamelCase_				(      self	):
       AcceleratorState._reset_state(__lowercase	)
       debug_launcher(__lowercase	)
       debug_launcher(partial(__lowercase							,		num_processes=1							,		split_batches=__lowercase	)							,		num_processes=1	)







     def       UpperCamelCase_				(      self	):
       AcceleratorState._reset_state(__lowercase	)
       debug_launcher(__lowercase	)
       debug_launcher(partial(__lowercase							,		num_processes=1							,		split_batches=__lowercase	)							,		num_processes=1	)







     def       UpperCamelCase_				(      self	):
       AcceleratorState._reset_state(__lowercase	)
       debug_launcher(partial(__lowercase							,		step_scheduler_with_optimizer=__lowercase	)	)







     def       UpperCamelCase_				(      self	):
       AcceleratorState._reset_state(__lowercase	)
       debug_launcher(partial(__lowercase							,		step_scheduler_with_optimizer=__lowercase	)	)







     @require_huggingface_suite
     def       UpperCamelCase_				(      self	):
       AcceleratorState._reset_state(__lowercase	)
       debug_launcher(partial(__lowercase							,		num_processes=1	)	)
       debug_launcher(__lowercase	)










"""simple docstring"""



def 						_SCREAMING_SNAKE_CASE			(   ):





  '''simple docstring'''





  for a in range(3_00   ):
    for b in range(a + 1		, 4_00   ):
      for c in range(b + 1		, 5_00   ):
        if (a + b + c) == 10_00 and (a**2) + (b**2) == (c**2):
          return a * b * c

  return -1
def 						_SCREAMING_SNAKE_CASE			(   ):





  '''simple docstring'''





  for a in range(3_00   ):
    for b in range(4_00   ):
      lowercase							   =	10_00 - a - b
      if a < b < c and (a**2) + (b**2) == (c**2):
        return a * b * c

  return -1
def 						_SCREAMING_SNAKE_CASE			(   ):





  '''simple docstring'''





  import timeit

  print(
      timeit.timeit('solution()'		, setup='from __main__ import solution'		, number=10_00   )   )
  print(
      timeit.timeit(
          'solution_fast()'		, setup='from __main__ import solution_fast'		, number=10_00   )   )


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


