




'''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    (       UpperCAmelCase_=2   ,	UpperCAmelCase_=True   ,	UpperCAmelCase_=False						):
    _a				:							List[str]							       =      Accelerator(step_scheduler_with_optimizer=a__   ,	split_batches=a__						)
    _a				:							Optional[int]							       =      torch.nn.Linear(2   ,	4						)
    _a				:							Tuple							       =      torch.optim.AdamW(model.parameters()   ,	lr=1.0						)
    _a				:							Tuple							       =      torch.optim.lr_scheduler.OneCycleLR(a__   ,	max_lr=0.01   ,	steps_per_epoch=2   ,	epochs=1						)
    _a       , _a       , _a				:							int							       =      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    (       UpperCAmelCase_=2   ,	UpperCAmelCase_=True   ,	UpperCAmelCase_=False						):
    _a				:							List[str]							       =      Accelerator(step_scheduler_with_optimizer=a__   ,	split_batches=a__						)
    _a				:							Tuple							       =      torch.nn.Linear(2   ,	4						)
    _a				:							List[Any]							       =      torch.optim.AdamW(model.parameters()   ,	lr=1.0						)
    _a				:							Tuple							       =      torch.optim.lr_scheduler.LambdaLR(a__   ,	lr_lambda=lambda UpperCAmelCase_						: 1 - n / 10						)
    _a       , _a       , _a				:							Dict							       =      accelerator.prepare(a__   ,	a__   ,	a__						)

    # Optimizer has stepped
    _a				:							str							       =      False
    scheduler.step()
    _a				:							Optional[int]							       =      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
    _a				:							List[str]							       =      True
    scheduler.step()
    if not step_scheduler_with_optimizer:
        _a				:							List[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    (       UpperCAmelCase_ = 2						):
    from transformers import get_linear_schedule_with_warmup

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

        _a       , _a       , _a				:							List[str]							       =      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 ):







      '''simple docstring'''


      debug_launcher(partial(lowerCamelCase__		,     num_processes=1 )		,     num_processes=1 )
      debug_launcher(partial(lowerCamelCase__		,     num_processes=1		,     split_batches=lowerCamelCase__ )		,     num_processes=1 )



  def     a_							(			self ):







      '''simple docstring'''


      debug_launcher(partial(lowerCamelCase__		,     num_processes=1 )		,     num_processes=1 )
      debug_launcher(partial(lowerCamelCase__		,     num_processes=1		,     split_batches=lowerCamelCase__ )		,     num_processes=1 )



  def     a_							(			self ):







      '''simple docstring'''


      debug_launcher(partial(lowerCamelCase__		,     num_processes=1		,     step_scheduler_with_optimizer=lowerCamelCase__ )		,     num_processes=1 )



  def     a_							(			self ):







      '''simple docstring'''


      debug_launcher(partial(lowerCamelCase__		,     num_processes=1		,     step_scheduler_with_optimizer=lowerCamelCase__ )		,     num_processes=1 )



  def     a_							(			self ):







      '''simple docstring'''


      AcceleratorState._reset_state(lowerCamelCase__ )
      debug_launcher(lowerCamelCase__ )
      debug_launcher(partial(lowerCamelCase__		,     num_processes=1		,     split_batches=lowerCamelCase__ )		,     num_processes=1 )



  def     a_							(			self ):







      '''simple docstring'''


      AcceleratorState._reset_state(lowerCamelCase__ )
      debug_launcher(lowerCamelCase__ )
      debug_launcher(partial(lowerCamelCase__		,     num_processes=1		,     split_batches=lowerCamelCase__ )		,     num_processes=1 )



  def     a_							(			self ):







      '''simple docstring'''


      AcceleratorState._reset_state(lowerCamelCase__ )
      debug_launcher(partial(lowerCamelCase__		,     step_scheduler_with_optimizer=lowerCamelCase__ ) )



  def     a_							(			self ):







      '''simple docstring'''


      AcceleratorState._reset_state(lowerCamelCase__ )
      debug_launcher(partial(lowerCamelCase__		,     step_scheduler_with_optimizer=lowerCamelCase__ ) )



  @require_huggingface_suite
  def     a_							(			self ):







      '''simple docstring'''


      AcceleratorState._reset_state(lowerCamelCase__ )
      debug_launcher(partial(lowerCamelCase__		,     num_processes=1 ) )
      debug_launcher(lowerCamelCase__ )








'''simple docstring'''
import os
def 							snake_case_    (  ):



    """simple docstring"""




    __lowercase     							=		os.path.dirname(os.path.realpath(a__     )     )
    __lowercase     							=		os.path.join(a__       ,"""triangle.txt"""     )

    with open(a__     ) as f:
        __lowercase     							=		f.readlines()

    __lowercase     							=		[[int(a__     ) for y in x.rstrip("""\r\n"""     ).split(""" """     )] for x in triangle]

    for i in range(1       ,len(a__     )     ):
        for j in range(len(a[i]     )     ):
            __lowercase     							=		a[i - 1][j] if j != len(a[i - 1]     ) else 0
            __lowercase     							=		a[i - 1][j - 1] if j > 0 else 0
            a[i][j] += max(a__       ,a__     )
    return max(a[-1]     )


if __name__ == "__main__":
  print(solution())




