





import functools
import gc
import inspect

import torch

from .imports import is_npu_available, is_xpu_available




def _a (  *SCREAMING_SNAKE_CASE__      :  str       )	->    Tuple:

							'''simple docstring'''






							if not isinstance(SCREAMING_SNAKE_CASE__			,				SCREAMING_SNAKE_CASE__       ):
														SCREAMING_SNAKE_CASE__       :      int     =						list(SCREAMING_SNAKE_CASE__       )
							for i in range(len(SCREAMING_SNAKE_CASE__       )       ):
														SCREAMING_SNAKE_CASE__       :      int     =						None
							gc.collect()
							if is_xpu_available():
														torch.xpu.empty_cache()
							elif is_npu_available():
														torch.npu.empty_cache()
							else:
														torch.cuda.empty_cache()
							return objects




def _a (  SCREAMING_SNAKE_CASE__      :  Exception       )	->    bool:

							'''simple docstring'''






							SCREAMING_SNAKE_CASE__       :      Optional[int]     =						[
							    "CUDA out of memory.",  # CUDA OOM
							    "cuDNN error: CUDNN_STATUS_NOT_SUPPORTED.",  # CUDNN SNAFU
							    "DefaultCPUAllocator: can't allocate memory",  # CPU OOM
							]
							if isinstance(SCREAMING_SNAKE_CASE__			,				SCREAMING_SNAKE_CASE__       ) and len(exception.args       ) == 1:
														return any(err in exception.args[0] for err in _statements       )
							return False







def _a (  SCREAMING_SNAKE_CASE__      :  callable = None			,				SCREAMING_SNAKE_CASE__      :  int = 1_28       )	->    Optional[Any]:

							'''simple docstring'''






							if function is None:
														return functools.partial(SCREAMING_SNAKE_CASE__			,				starting_batch_size=SCREAMING_SNAKE_CASE__       )

							SCREAMING_SNAKE_CASE__       :      int     =						starting_batch_size

							def decorator(*SCREAMING_SNAKE_CASE__      :  Optional[int]			,				**SCREAMING_SNAKE_CASE__      :  str       ):
														nonlocal batch_size
														gc.collect()
														if is_xpu_available():
																					torch.xpu.empty_cache()
														elif is_npu_available():
																					torch.npu.empty_cache()
														else:
																					torch.cuda.empty_cache()
														SCREAMING_SNAKE_CASE__       :      Tuple     =						list(inspect.signature(SCREAMING_SNAKE_CASE__       ).parameters.keys()       )
														# Guard against user error
														if len(SCREAMING_SNAKE_CASE__       ) < (len(SCREAMING_SNAKE_CASE__       ) + 1):
																					SCREAMING_SNAKE_CASE__       :      Tuple     =						", ".join([f'''{arg}={value}''' for arg, value in zip(params[1:]			,				args[1:]       )]       )
																					raise TypeError(
																					    f'''Batch size was passed into `{function.__name__}` as the first argument when called.'''
																					    f'''Remove this as the decorator already does so: `{function.__name__}({arg_str})`'''       )
														while True:
																					if batch_size == 0:
																												raise RuntimeError("No executable batch size found, reached zero."       )
																					try:
																												return function(SCREAMING_SNAKE_CASE__			,				*SCREAMING_SNAKE_CASE__			,				**SCREAMING_SNAKE_CASE__       )
																					except Exception as e:
																												if should_reduce_batch_size(SCREAMING_SNAKE_CASE__       ):
																																			gc.collect()
																																			if is_xpu_available():
																																										torch.xpu.empty_cache()
																																			elif is_npu_available():
																																										torch.npu.empty_cache()
																																			else:
																																										torch.cuda.empty_cache()
																																			batch_size //= 2
																												else:
																																			raise

							return decorator













_lowerCamelCase :    dict[tuple[int, int, int], int]  							= {}




def _a (  SCREAMING_SNAKE_CASE__      :  int			,				SCREAMING_SNAKE_CASE__      :  int			,				SCREAMING_SNAKE_CASE__      :  int       )	->    int:

							'''simple docstring'''






							if late == 3 or absent == 2:
														return 0

							# if we have no days left, and have not failed any other rules,
							# we have a prize string
							if days == 0:
														return 1

							# No easy solution, so now we need to do the recursive calculation

							# First, check if the combination is already in the cache, and
							# if yes, return the stored value from there since we already
							# know the number of possible prize strings from this point on
							SCREAMING_SNAKE_CASE__       :      Union[str, Any]     =						(days, absent, late)
							if key in cache:
														return cache[key]

							# now we calculate the three possible ways that can unfold from
							# this point on, depending on our attendance today

							# 1) if we are late (but not absent), the "absent" counter stays as
							# it is, but the "late" counter increases by one
							SCREAMING_SNAKE_CASE__       :      Tuple     =						_calculate(days - 1			,				SCREAMING_SNAKE_CASE__			,				late + 1       )

							# 2) if we are absent, the "absent" counter increases by 1, and the
							# "late" counter resets to 0
							SCREAMING_SNAKE_CASE__       :      Dict     =						_calculate(days - 1			,				absent + 1			,				0       )

							# 3) if we are on time, this resets the "late" counter and keeps the
							# absent counter
							SCREAMING_SNAKE_CASE__       :      Any     =						_calculate(days - 1			,				SCREAMING_SNAKE_CASE__			,				0       )

							SCREAMING_SNAKE_CASE__       :      str     =						state_late + state_absent + state_ontime

							SCREAMING_SNAKE_CASE__       :      Optional[int]     =						prizestrings
							return prizestrings




def _a (  SCREAMING_SNAKE_CASE__      :  int = 30       )	->    int:

							'''simple docstring'''






							return _calculate(SCREAMING_SNAKE_CASE__			,				absent=0			,				late=0       )


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






