

import inspect
import warnings

import torch

from .state import AcceleratorState, GradientState
from .utils import DistributedType, honor_type, is_tpu_available


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




def      UpperCAmelCase__							(     _A :					List[Any]      , _A :					Any				):





  '''simple docstring'''







  if isinstance(_A      , (list, tuple)				):
    return honor_type(_A      , (move_to_device(_A      , _A				) for t in state)				)
  elif isinstance(_A      , _A				):
    return type(_A				)({k: move_to_device(_A      , _A				) for k, v in state.items()}				)
  elif isinstance(_A      , torch.Tensor				):
    return state.to(_A				)
  return state







class 		__magic_name__     (				torch.optim.Optimizer				):

   '''simple docstring'''




   def __init__(							self,       lowercase_,       lowercase_=True,       lowercase_=None			)				->       str:






     """simple docstring"""






     a__    =optimizer
     a__    =scaler
     a__    =AcceleratorState()
     a__    =GradientState()
     a__    =device_placement
     a__    =False
     a__    =None

     # Handle device placement
     if device_placement:
       a__    =self.optimizer.state_dict()
       if self.accelerator_state.distributed_type == DistributedType.TPU:
         xm.send_cpu_data_to_device(lowercase_,       self.accelerator_state.device			)
       else:
         a__    =move_to_device(lowercase_,       self.accelerator_state.device			)
       self.optimizer.load_state_dict(lowercase_			)



   @property
   def 					_UpperCAmelCase					(							self			)				->       str:






     """simple docstring"""






     return self.optimizer.state



   @state.setter
   def 					_UpperCAmelCase					(							self,       lowercase_			)				->       Any:






     """simple docstring"""






     a__    =state



   @property
   def 					_UpperCAmelCase					(							self			)				->       int:






     """simple docstring"""






     return self.optimizer.param_groups



   @param_groups.setter
   def 					_UpperCAmelCase					(							self,       lowercase_			)				->       str:






     """simple docstring"""






     a__    =param_groups



   @property
   def 					_UpperCAmelCase					(							self			)				->       Dict:






     """simple docstring"""






     return self.optimizer.defaults



   @defaults.setter
   def 					_UpperCAmelCase					(							self,       lowercase_			)				->       List[Any]:






     """simple docstring"""






     a__    =defaults



   def 					_UpperCAmelCase					(							self,       lowercase_			)				->       List[str]:






     """simple docstring"""






     self.optimizer.add_param_group(lowercase_			)



   def 					_UpperCAmelCase					(							self,       lowercase_			)				->       int:






     """simple docstring"""






     if self.accelerator_state.distributed_type == DistributedType.TPU and self.device_placement:
       xm.send_cpu_data_to_device(lowercase_,       self.accelerator_state.device			)
     self.optimizer.load_state_dict(lowercase_			)



   def 					_UpperCAmelCase					(							self			)				->       List[str]:






     """simple docstring"""






     return self.optimizer.state_dict()



   def 					_UpperCAmelCase					(							self,       lowercase_=None			)				->       Dict:






     """simple docstring"""






     if self.gradient_state.sync_gradients:
       a__    ='''set_to_none''' in inspect.signature(self.optimizer.zero_grad			).parameters
       if accept_arg:
         if set_to_none is None:
           a__    =False
         self.optimizer.zero_grad(set_to_none=lowercase_			)
       else:
         if set_to_none is not None:
           raise ValueError('''`set_to_none` for Optimizer.zero_grad` is not supported by this optimizer.'''			)
         self.optimizer.zero_grad()



   def 					_UpperCAmelCase					(							self,       lowercase_=None			)				->       Tuple:






     """simple docstring"""






     if self.gradient_state.sync_gradients:
       if self.accelerator_state.distributed_type == DistributedType.TPU:
         a__    ={'''closure''': closure} if closure is not None else {}
         xm.optimizer_step(self.optimizer,       optimizer_args=lowercase_			)
       elif self.scaler is not None:
         a__    =False
         if self._last_scale is None:
           # `get_scale` is an async operation requiring full synchronization
           # on CPU and GPUs before finishing. As a result, we store away
           # the prior one to reduce the call overhead
           a__    =self.scaler.get_scale()
           a__    =True
         self.scaler.step(self.optimizer,       lowercase_			)
         self.scaler.update()
         a__    =self.scaler.get_scale()
         if not new_scale:
           # If we reduced the loss scale, it means the optimizer step was skipped because of gradient overflow.
           a__    =scale_after < self._last_scale
         a__    =scale_after

       else:
         self.optimizer.step(lowercase_			)



   def 					_UpperCAmelCase					(							self,       lowercase_			)				->       Dict:






     """simple docstring"""






     for param_group in self.optimizer.param_groups:
       a__    =[parameters_map.get(lowercase_,       lowercase_			) for p in param_group['''params''']]



   @property
   def 					_UpperCAmelCase					(							self			)				->       Tuple:






     """simple docstring"""






     warnings.warn(
         '''The `is_overflow` property is deprecated and will be removed in version 1.0 of Accelerate use '''
         '''`optimizer.step_was_skipped` instead.''',       lowercase_,       )
     return self._is_overflow



   @property
   def 					_UpperCAmelCase					(							self			)				->       int:






     """simple docstring"""






     return self._is_overflow



   def __getstate__(							self			)				->       str:






     """simple docstring"""






     return self.__dict__.copy()



   def __setstate__(							self,       lowercase_			)				->       int:






     """simple docstring"""






     self.__dict__.update(lowercase_			)



import copy

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


lowerCamelCase 	=   logging.get_logger(__name__)

lowerCamelCase 	=   {
    '''facebook/sam-vit-huge''': '''https://huggingface.co/facebook/sam-vit-huge/resolve/main/config.json''',
    '''facebook/sam-vit-large''': '''https://huggingface.co/facebook/sam-vit-large/resolve/main/config.json''',
    '''facebook/sam-vit-base''': '''https://huggingface.co/facebook/sam-vit-base/resolve/main/config.json''',
}







class 		__magic_name__     (				lowerCamelCase__				):

   '''simple docstring'''




   def __init__(							self,       lowercase_=256,       lowercase_=1024,       lowercase_=16,       lowercase_=16,       lowercase_=4,       lowercase_="gelu",       lowercase_=1E-6,       **lowercase_,       )				->       int:






     """simple docstring"""






     super().__init__(**lowercase_			)
     a__    =hidden_size
     a__    =image_size
     a__    =patch_size
     a__    =image_size // patch_size
     a__    =mask_input_channels
     a__    =num_point_embeddings
     a__    =hidden_act
     a__    =layer_norm_eps







class 		__magic_name__     (				lowerCamelCase__				):

   '''simple docstring'''




   def __init__(							self,       lowercase_=256,       lowercase_="relu",       lowercase_=2048,       lowercase_=2,       lowercase_=8,       lowercase_=2,       lowercase_=3,       lowercase_=3,       lowercase_=256,       lowercase_=1E-6,       **lowercase_,       )				->       int:






     """simple docstring"""






     super().__init__(**lowercase_			)
     a__    =hidden_size
     a__    =hidden_act
     a__    =mlp_dim
     a__    =num_hidden_layers
     a__    =num_attention_heads
     a__    =attention_downsample_rate
     a__    =num_multimask_outputs
     a__    =iou_head_depth
     a__    =iou_head_hidden_dim
     a__    =layer_norm_eps







class 		__magic_name__     (				lowerCamelCase__				):

   '''simple docstring'''




   def __init__(							self,       lowercase_=768,       lowercase_=256,       lowercase_=12,       lowercase_=12,       lowercase_=3,       lowercase_=1024,       lowercase_=16,       lowercase_="gelu",       lowercase_=1E-06,       lowercase_=0.0,       lowercase_=1E-10,       lowercase_=True,       lowercase_=4.0,       lowercase_=True,       lowercase_=True,       lowercase_=14,       lowercase_=[2, 5, 8, 11],       lowercase_=128,       lowercase_=None,       **lowercase_,       )				->       Union[str, Any]:






     """simple docstring"""






     super().__init__(**lowercase_			)

     a__    =hidden_size
     a__    =output_channels
     a__    =num_hidden_layers
     a__    =num_attention_heads
     a__    =num_channels
     a__    =image_size
     a__    =patch_size
     a__    =hidden_act
     a__    =layer_norm_eps
     a__    =attention_dropout
     a__    =initializer_range
     a__    =qkv_bias
     a__    =mlp_ratio
     a__    =use_abs_pos
     a__    =use_rel_pos
     a__    =window_size
     a__    =global_attn_indexes
     a__    =num_pos_feats
     a__    =int(hidden_size * mlp_ratio			) if mlp_dim is None else mlp_dim







class 		__magic_name__     (				lowerCamelCase__				):

   '''simple docstring'''




   lowerCamelCase__					:      Optional[int]        =  'sam'
   lowerCamelCase__					:      str        =  True



   def __init__(							self,       lowercase_=None,       lowercase_=None,       lowercase_=None,       lowercase_=0.02,       **lowercase_,       )				->       Tuple:






     """simple docstring"""






     super().__init__(**lowercase_			)
     a__    =vision_config if vision_config is not None else {}
     a__    =prompt_encoder_config if prompt_encoder_config is not None else {}
     a__    =mask_decoder_config if mask_decoder_config is not None else {}

     if isinstance(lowercase_,       lowercase_			):
       a__    =vision_config.to_dict()
     if isinstance(lowercase_,       lowercase_			):
       a__    =prompt_encoder_config.to_dict()
     if isinstance(lowercase_,       lowercase_			):
       a__    =mask_decoder_config.to_dict()

     a__    =SamVisionConfig(**lowercase_			)
     a__    =SamPromptEncoderConfig(**lowercase_			)
     a__    =SamMaskDecoderConfig(**lowercase_			)
     a__    =initializer_range



   def 					_UpperCAmelCase					(							self			)				->       List[str]:






     """simple docstring"""






     a__    =copy.deepcopy(self.__dict__			)
     a__    =self.vision_config.to_dict()
     a__    =self.prompt_encoder_config.to_dict()
     a__    =self.mask_decoder_config.to_dict()
     a__    =self.__class__.model_type
     return output
