




"""simple docstring"""




import functools
import gc
import inspect

import torch

from .imports import is_npu_available, is_xpu_available



def     a__ (				*snake_case__				)						->					List[str]:
			if not isinstance(snake_case__       , snake_case__				):
						lowerCamelCase			      =					list(snake_case__				)
			for i in range(len(snake_case__				)				):
						lowerCamelCase			      =					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__ (				snake_case__				)						->					bool:
			lowerCamelCase			      =					[
			    """CUDA out of memory.""",  # CUDA OOM
			    """cuDNN error: CUDNN_STATUS_NOT_SUPPORTED.""",  # CUDNN SNAFU
			    """DefaultCPUAllocator: can't allocate memory""",  # CPU OOM
			]
			if isinstance(snake_case__       , snake_case__				) and len(exception.args				) == 1:
						return any(err in exception.args[0] for err in _statements				)
			return False



def     a__ (				snake_case__ = None       , snake_case__ = 1_28				)						->					Tuple:
			if function is None:
						return functools.partial(snake_case__       , starting_batch_size=snake_case__				)

			lowerCamelCase			      =					starting_batch_size

			def decorator(*snake_case__       , **snake_case__				):
						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()
						lowerCamelCase			      =					list(inspect.signature(snake_case__				).parameters.keys()				)
						# Guard against user error
						if len(snake_case__				) < (len(snake_case__				) + 1):
									lowerCamelCase			      =					""", """.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(snake_case__       , *snake_case__       , **snake_case__				)
									except Exception as e:
												if should_reduce_batch_size(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









"""simple docstring"""




def     a__ (				snake_case__       , snake_case__ = False				)						->					str:

			if not isinstance(snake_case__       , snake_case__				):
						lowerCamelCase			      =					F'Expected string as input, found {type(snake_case__				)}'
						raise ValueError(snake_case__				)
			if not isinstance(snake_case__       , snake_case__				):
						lowerCamelCase			      =					F'Expected boolean as use_pascal parameter, found {type(snake_case__				)}'
						raise ValueError(snake_case__				)

			lowerCamelCase			      =					input_str.split("""_"""				)

			lowerCamelCase			      =					0 if use_pascal else 1

			lowerCamelCase			      =					words[start_index:]

			lowerCamelCase			      =					[word[0].upper() + word[1:] for word in words_to_capitalize]

			lowerCamelCase			      =					"""""" if use_pascal else words[0]

			return "".join([initial_word, *capitalized_words]				)


if __name__ == "__main__":
					from doctest import testmod

					testmod()



