

"""simple docstring"""


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


UpperCAmelCase													=logging.get_logger(__name__)





class lowerCamelCase__   (				SCREAMING_SNAKE_CASE     ,   SCREAMING_SNAKE_CASE    ):



    '''simple docstring'''




    _lowerCamelCase					=   '''maskformer-swin'''

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




    def __init__( self ,lowerCamelCase_=2_2_4 ,lowerCamelCase_=4 ,lowerCamelCase_=3 ,lowerCamelCase_=9_6 ,lowerCamelCase_=[2, 2, 6, 2] ,lowerCamelCase_=[3, 6, 1_2, 2_4] ,lowerCamelCase_=7 ,lowerCamelCase_=4.0 ,lowerCamelCase_=True ,lowerCamelCase_=0.0 ,lowerCamelCase_=0.0 ,lowerCamelCase_=0.1 ,lowerCamelCase_="gelu" ,lowerCamelCase_=False ,lowerCamelCase_=0.02 ,lowerCamelCase_=1E-5 ,lowerCamelCase_=None ,lowerCamelCase_=None ,**lowerCamelCase_ ,)    ->      Any:
          super().__init__(**lowerCamelCase_    )

          A				  =		image_size
          A				  =		patch_size
          A				  =		num_channels
          A				  =		embed_dim
          A				  =		depths
          A				  =		len(lowerCamelCase_    )
          A				  =		num_heads
          A				  =		window_size
          A				  =		mlp_ratio
          A				  =		qkv_bias
          A				  =		hidden_dropout_prob
          A				  =		attention_probs_dropout_prob
          A				  =		drop_path_rate
          A				  =		hidden_act
          A				  =		use_absolute_embeddings
          A				  =		layer_norm_eps
          A				  =		initializer_range
          # we set the hidden_size attribute in order to make Swin work with VisionEncoderDecoderModel
          # this indicates the channel dimension after the last stage of the model
          A				  =		int(embed_dim * 2 ** (len(lowerCamelCase_    ) - 1)    )
          A				  =		["""stem"""] + [f'stage{idx}' for idx in range(1 ,len(lowerCamelCase_    ) + 1    )]
          A  ,			A				  =		get_aligned_output_features_output_indices(
              out_features=lowerCamelCase_ ,out_indices=lowerCamelCase_ ,stage_names=self.stage_names    )







"""simple docstring"""


import functools
import gc
import inspect

import torch

from .imports import is_npu_available, is_xpu_available



def   _A					(	*_a	:						int				):



      """simple docstring"""






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



      """simple docstring"""






      A				  =		[
          """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   _A					(	_a	:						callable = None     ,   _a	:						int = 1_2_8				):



      """simple docstring"""






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

      A				  =		starting_batch_size

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




