


from ....configuration_utils import PretrainedConfig
from ....utils import logging


__snake_case            =   logging.get_logger(__name__)

__snake_case            =   {
    "Visual-Attention-Network/van-base": (
        "https://huggingface.co/Visual-Attention-Network/van-base/blob/main/config.json"
    ),
}




class    UpperCAmelCase					(       _UpperCamelCase    ):
   lowercase						= """van"""




   def __init__(						self							:		Optional[int]       ,       __magic_name__							:		Optional[int]=2_2_4       ,       __magic_name__							:		Tuple=3       ,       __magic_name__							:		Optional[Any]=[7, 3, 3, 3]       ,       __magic_name__							:		Dict=[4, 2, 2, 2]       ,       __magic_name__							:		List[Any]=[6_4, 1_2_8, 3_2_0, 5_1_2]       ,       __magic_name__							:		Any=[3, 3, 1_2, 3]       ,       __magic_name__							:		Any=[8, 8, 4, 4]       ,       __magic_name__							:		Dict="gelu"       ,       __magic_name__							:		Optional[Any]=0.02       ,       __magic_name__							:		Dict=1e-6       ,       __magic_name__							:		Dict=1e-2       ,       __magic_name__							:		int=0.0       ,       __magic_name__							:		int=0.0       ,       **__magic_name__							:		Optional[Any]       ,       ):







      """simple docstring"""







      super().__init__(**__magic_name__						)
      UpperCamelCase         =					image_size
      UpperCamelCase         =					num_channels
      UpperCamelCase         =					patch_sizes
      UpperCamelCase         =					strides
      UpperCamelCase         =					hidden_sizes
      UpperCamelCase         =					depths
      UpperCamelCase         =					mlp_ratios
      UpperCamelCase         =					hidden_act
      UpperCamelCase         =					initializer_range
      UpperCamelCase         =					layer_norm_eps
      UpperCamelCase         =					layer_scale_init_value
      UpperCamelCase         =					drop_path_rate
      UpperCamelCase         =					dropout_rate








import functools
import gc
import inspect

import torch

from .imports import is_npu_available, is_xpu_available






def 							_lowercase    (		*SCREAMING_SNAKE_CASE_   :		List[Any]	):





			"""simple docstring"""




			if not isinstance(SCREAMING_SNAKE_CASE_       ,  SCREAMING_SNAKE_CASE_	):
						UpperCamelCase         =					list(SCREAMING_SNAKE_CASE_	)
			for i in range(len(SCREAMING_SNAKE_CASE_	)	):
						UpperCamelCase         =					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 							_lowercase    (		SCREAMING_SNAKE_CASE_   :		Exception	):





			"""simple docstring"""




			UpperCamelCase         =					[
			    """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 							_lowercase    (		SCREAMING_SNAKE_CASE_   :		callable = None       ,  SCREAMING_SNAKE_CASE_   :		int = 128	):





			"""simple docstring"""




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

			UpperCamelCase         =					starting_batch_size

			def decorator(*SCREAMING_SNAKE_CASE_   :		Union[str, Any]       ,  **SCREAMING_SNAKE_CASE_   :		Tuple	):
						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()
						UpperCamelCase         =					list(inspect.signature(SCREAMING_SNAKE_CASE_	).parameters.keys()	)
						# Guard against user error
						if len(SCREAMING_SNAKE_CASE_	) < (len(SCREAMING_SNAKE_CASE_	) + 1):
									UpperCamelCase         =					""", """.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




