
"""simple docstring"""

import os
import socket
from contextlib import contextmanager

import torch

from ..commands.config.default import write_basic_config  # noqa: F401
from ..state import PartialState
from .dataclasses import DistributedType
from .imports import is_deepspeed_available, is_tpu_available
from .transformer_engine import convert_model
from .versions import is_torch_version


if is_deepspeed_available():
      from deepspeed import DeepSpeedEngine

if is_tpu_available(check_device=False):
      import torch_xla.core.xla_model as xm






def 							A		(       snake_case   :Union[str, Any]	)   ->			Any:
 if is_torch_version('<'       ,      '2.0.0'	) or not hasattr(snake_case       ,      '_dynamo'	):
  return False
 return isinstance(snake_case       ,      torch._dynamo.eval_frame.OptimizedModule	)






def 							A		(       snake_case   :Any       ,      snake_case   :bool = True	)   ->			Tuple:
 __UpperCamelCase       						= (torch.nn.parallel.DistributedDataParallel, torch.nn.DataParallel)

 __UpperCamelCase       						= is_compiled_module(snake_case	)
 if is_compiled:
  __UpperCamelCase       						= model
  __UpperCamelCase       						= model._orig_mod

 if is_deepspeed_available():
  options += (DeepSpeedEngine,)

 while isinstance(snake_case       ,      snake_case	):
  __UpperCamelCase       						= model.module

 if not keep_fpaa_wrapper:
  __UpperCamelCase       						= getattr(snake_case       ,      'forward'	)
  __UpperCamelCase       						= model.__dict__.pop('_original_forward'       ,      snake_case	)
  if original_forward is not None:
   while hasattr(snake_case       ,      '__wrapped__'	):
    __UpperCamelCase       						= forward.__wrapped__
    if forward == original_forward:
     break
   __UpperCamelCase       						= forward
  if getattr(snake_case       ,      '_converted_to_transformer_engine'       ,      snake_case	):
   convert_model(snake_case       ,      to_transformer_engine=snake_case	)

 if is_compiled:
  __UpperCamelCase       						= model
  __UpperCamelCase       						= compiled_model

 return model






def 							A		(       )   ->			int:
 PartialState().wait_for_everyone()






def 							A		(       snake_case   :Dict       ,      snake_case   :List[str]	)   ->			Dict:
 if PartialState().distributed_type == DistributedType.TPU:
  xm.save(snake_case       ,      snake_case	)
 elif PartialState().local_process_index == 0:
  torch.save(snake_case       ,      snake_case	)






@contextmanager
def 							A		(       **snake_case   :Optional[Any]	)   ->			List[Any]:
 for key, value in kwargs.items():
  __UpperCamelCase       						= str(snake_case	)

 yield

 for key in kwargs:
  if key.upper() in os.environ:
   del os.environ[key.upper()]






def 							A		(       snake_case   :Optional[Any]	)   ->			str:
 if not hasattr(snake_case       ,      '__qualname__'	) and not hasattr(snake_case       ,      '__name__'	):
  __UpperCamelCase       						= getattr(snake_case       ,      '__class__'       ,      snake_case	)
 if hasattr(snake_case       ,      '__qualname__'	):
  return obj.__qualname__
 if hasattr(snake_case       ,      '__name__'	):
  return obj.__name__
 return str(snake_case	)






def 							A		(       snake_case   :List[Any]       ,      snake_case   :int	)   ->			Any:
 for key, value in source.items():
  if isinstance(snake_case       ,      snake_case	):
   __UpperCamelCase       						= destination.setdefault(snake_case       ,      {}	)
   merge_dicts(snake_case       ,      snake_case	)
  else:
   __UpperCamelCase       						= value

 return destination






def 							A		(       snake_case   :int = None	)   ->			bool:
 if port is None:
  __UpperCamelCase       						= 2_9_5_0_0
 with socket.socket(socket.AF_INET       ,      socket.SOCK_STREAM	) as s:
  return s.connect_ex(('localhost', port)	) == 0





"""simple docstring"""

from typing import List

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


UpperCamelCase			:       Tuple									=				logging.get_logger(__name__)

UpperCamelCase			:       List[str]									=				{
    "snap-research/efficientformer-l1-300": (
        "https://huggingface.co/snap-research/efficientformer-l1-300/resolve/main/config.json"
    ),
}

class __lowerCAmelCase	(   __SCREAMING_SNAKE_CASE    ):
     lowercase		     =		"efficientformer"





     def __init__(			self			,  __UpperCAmelCase = [3, 2, 6, 4]			,  __UpperCAmelCase = [48, 96, 224, 448]			,  __UpperCAmelCase = [True, True, True, True]			,  __UpperCAmelCase = 448			,  __UpperCAmelCase = 32			,  __UpperCAmelCase = 4			,  __UpperCAmelCase = 7			,  __UpperCAmelCase = 5			,  __UpperCAmelCase = 8			,  __UpperCAmelCase = 4			,  __UpperCAmelCase = 0.0			,  __UpperCAmelCase = 16			,  __UpperCAmelCase = 3			,  __UpperCAmelCase = 3			,  __UpperCAmelCase = 3			,  __UpperCAmelCase = 2			,  __UpperCAmelCase = 1			,  __UpperCAmelCase = 0.0			,  __UpperCAmelCase = 1			,  __UpperCAmelCase = True			,  __UpperCAmelCase = True			,  __UpperCAmelCase = 1E-5			,  __UpperCAmelCase = "gelu"			,  __UpperCAmelCase = 0.0_2			,  __UpperCAmelCase = 1E-12			,  __UpperCAmelCase = 224			,  __UpperCAmelCase = 1E-05			,  **__UpperCAmelCase			,  ):

      '''simple docstring'''




      super().__init__(**__UpperCAmelCase   )

      __UpperCamelCase       						= hidden_act
      __UpperCamelCase       						= hidden_dropout_prob
      __UpperCamelCase       						= hidden_sizes
      __UpperCamelCase       						= num_hidden_layers
      __UpperCamelCase       						= num_attention_heads
      __UpperCamelCase       						= initializer_range
      __UpperCamelCase       						= layer_norm_eps
      __UpperCamelCase       						= patch_size
      __UpperCamelCase       						= num_channels
      __UpperCamelCase       						= depths
      __UpperCamelCase       						= mlp_expansion_ratio
      __UpperCamelCase       						= downsamples
      __UpperCamelCase       						= dim
      __UpperCamelCase       						= key_dim
      __UpperCamelCase       						= attention_ratio
      __UpperCamelCase       						= resolution
      __UpperCamelCase       						= pool_size
      __UpperCamelCase       						= downsample_patch_size
      __UpperCamelCase       						= downsample_stride
      __UpperCamelCase       						= downsample_pad
      __UpperCamelCase       						= drop_path_rate
      __UpperCamelCase       						= num_metaad_blocks
      __UpperCamelCase       						= distillation
      __UpperCamelCase       						= use_layer_scale
      __UpperCamelCase       						= layer_scale_init_value
      __UpperCamelCase       						= image_size
      __UpperCamelCase       						= batch_norm_eps



