




import functools
import gc
import inspect

import torch

from .imports import is_npu_available, is_xpu_available







def 		lowercase__   (		*__A:	List[str]				):





     '''simple docstring'''






     if not isinstance(__a							,__a				):
          __magic_name__							:	List[Any]    							=							list(__a				)
     for i in range(len(__a				)				):
          __magic_name__							:	Optional[Any]    							=							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__   (		__A:	Exception				):





     '''simple docstring'''






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







def 		lowercase__   (		__A:	callable = None							,__A:	int = 1_2_8				):





     '''simple docstring'''






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

     __magic_name__							:	Union[str, Any]    							=							starting_batch_size

     def decorator(*__A:	int							,**__A:	List[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()
          __magic_name__							:	int    							=							list(inspect.signature(__a				).parameters.keys()				)
          # Guard against user error
          if len(__a				) < (len(__a				) + 1):
               __magic_name__							:	int    							=							''', '''.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(__a							,*__a							,**__a				)
               except Exception as e:
                    if should_reduce_batch_size(__a				):
                         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









from ...configuration_utils import PretrainedConfig
from ...utils import logging
from ...utils.backbone_utils import BackboneConfigMixin, get_aligned_output_features_output_indices


__lowerCamelCase   :						Optional[int]				=			logging.get_logger(__name__)

__lowerCamelCase   :						Tuple				=			{
    '''shi-labs/nat-mini-in1k-224''': '''https://huggingface.co/shi-labs/nat-mini-in1k-224/resolve/main/config.json''',
    # See all Nat models at https://huggingface.co/models?filter=nat
}


class    lowerCamelCase    (		_lowerCamelCase				,_lowerCamelCase						):






			'''simple docstring'''





			UpperCamelCase__								='''nat'''

			UpperCamelCase__								={
			    '''num_attention_heads''': '''num_heads''',
			    '''num_hidden_layers''': '''num_layers''',
			}

			def __init__( self			:   Union[str, Any]       ,						lowerCamelCase_			:   Dict=4       ,						lowerCamelCase_			:   Union[str, Any]=3       ,						lowerCamelCase_			:   List[str]=64       ,						lowerCamelCase_			:   Union[str, Any]=[3, 4, 6, 5]       ,						lowerCamelCase_			:   List[Any]=[2, 4, 8, 16]       ,						lowerCamelCase_			:   List[Any]=7       ,						lowerCamelCase_			:   Union[str, Any]=3.0       ,						lowerCamelCase_			:   int=True       ,						lowerCamelCase_			:   str=0.0       ,						lowerCamelCase_			:   int=0.0       ,						lowerCamelCase_			:   Dict=0.1       ,						lowerCamelCase_			:   Optional[int]="gelu"       ,						lowerCamelCase_			:   Tuple=0.0_2       ,						lowerCamelCase_			:   Tuple=1E-5       ,						lowerCamelCase_			:   List[Any]=0.0       ,						lowerCamelCase_			:   str=None       ,						lowerCamelCase_			:   Tuple=None       ,						**lowerCamelCase_			:   Any       ,						)						->   Any:
								super().__init__(**lowerCamelCase_  )

								__magic_name__							:	List[Any]    							=							patch_size
								__magic_name__							:	str    							=							num_channels
								__magic_name__							:	Union[str, Any]    							=							embed_dim
								__magic_name__							:	Dict    							=							depths
								__magic_name__							:	str    							=							len(lowerCamelCase_  )
								__magic_name__							:	List[str]    							=							num_heads
								__magic_name__							:	Optional[int]    							=							kernel_size
								__magic_name__							:	Any    							=							mlp_ratio
								__magic_name__							:	Any    							=							qkv_bias
								__magic_name__							:	Union[str, Any]    							=							hidden_dropout_prob
								__magic_name__							:	Any    							=							attention_probs_dropout_prob
								__magic_name__							:	List[str]    							=							drop_path_rate
								__magic_name__							:	str    							=							hidden_act
								__magic_name__							:	Tuple    							=							layer_norm_eps
								__magic_name__							:	Any    							=							initializer_range
								# we set the hidden_size attribute in order to make Nat work with VisionEncoderDecoderModel
								# this indicates the channel dimension after the last stage of the model
								__magic_name__							:	str    							=							int(embed_dim * 2 ** (len(lowerCamelCase_  ) - 1)  )
								__magic_name__							:	str    							=							layer_scale_init_value
								__magic_name__							:	Dict    							=							['''stem'''] + [F'''stage{idx}''' for idx in range(1       ,						len(lowerCamelCase_  ) + 1  )]
								__magic_name__							,    __magic_name__							:	int    							=							get_aligned_output_features_output_indices(
								    out_features=lowerCamelCase_       ,						out_indices=lowerCamelCase_       ,						stage_names=self.stage_names  )



