


'''simple docstring'''
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 				lowerCAmelCase__	(       UpperCAmelCase    ,       UpperCAmelCase     ):






       """simple docstring"""





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







class 					_A						(						torch.optim.Optimizer							):



   '''simple docstring'''






   def __init__(	self	:					Optional[int]					, lowerCamelCase	:					List[Any]					, lowerCamelCase	:					Dict=True					, lowerCamelCase	:					List[Any]=None						)->	Tuple:
          snake_case__					:	Any          =					optimizer
          snake_case__					:	Union[str, Any]          =					scaler
          snake_case__					:	Optional[Any]          =					AcceleratorState()
          snake_case__					:	int          =					GradientState()
          snake_case__					:	Optional[int]          =					device_placement
          snake_case__					:	int          =					False
          snake_case__					:	int          =					None

          # Handle device placement
          if device_placement:
                 snake_case__					:	Tuple          =					self.optimizer.state_dict()
                 if self.accelerator_state.distributed_type == DistributedType.TPU:
                        xm.send_cpu_data_to_device(lowerCamelCase					, self.accelerator_state.device						)
                 else:
                        snake_case__					:	Union[str, Any]          =					move_to_device(lowerCamelCase					, self.accelerator_state.device						)
                 self.optimizer.load_state_dict(lowerCamelCase						)




   @property
   def   __lowerCAmelCase						(	self	:					Optional[int]						)->	Dict:
          return self.optimizer.state




   @state.setter
   def   __lowerCAmelCase						(	self	:					Optional[Any]					, lowerCamelCase	:					Any						)->	Tuple:
          snake_case__					:	List[Any]          =					state




   @property
   def   __lowerCAmelCase						(	self	:					List[str]						)->	Union[str, Any]:
          return self.optimizer.param_groups




   @param_groups.setter
   def   __lowerCAmelCase						(	self	:					Union[str, Any]					, lowerCamelCase	:					Optional[Any]						)->	Union[str, Any]:
          snake_case__					:	Optional[Any]          =					param_groups




   @property
   def   __lowerCAmelCase						(	self	:					int						)->	int:
          return self.optimizer.defaults




   @defaults.setter
   def   __lowerCAmelCase						(	self	:					str					, lowerCamelCase	:					str						)->	List[str]:
          snake_case__					:	Tuple          =					defaults




   def   __lowerCAmelCase						(	self	:					str					, lowerCamelCase	:					Optional[int]						)->	Optional[int]:
          self.optimizer.add_param_group(lowerCamelCase						)




   def   __lowerCAmelCase						(	self	:					Dict					, lowerCamelCase	:					Any						)->	Optional[int]:
          if self.accelerator_state.distributed_type == DistributedType.TPU and self.device_placement:
                 xm.send_cpu_data_to_device(lowerCamelCase					, self.accelerator_state.device						)
          self.optimizer.load_state_dict(lowerCamelCase						)




   def   __lowerCAmelCase						(	self	:					List[Any]						)->	int:
          return self.optimizer.state_dict()




   def   __lowerCAmelCase						(	self	:					Any					, lowerCamelCase	:					Optional[Any]=None						)->	int:
          if self.gradient_state.sync_gradients:
                 snake_case__					:	Dict          =					"""set_to_none""" in inspect.signature(self.optimizer.zero_grad						).parameters
                 if accept_arg:
                        if set_to_none is None:
                               snake_case__					:	int          =					False
                        self.optimizer.zero_grad(set_to_none=lowerCamelCase						)
                 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   __lowerCAmelCase						(	self	:					str					, lowerCamelCase	:					List[str]=None						)->	Any:
          if self.gradient_state.sync_gradients:
                 if self.accelerator_state.distributed_type == DistributedType.TPU:
                        snake_case__					:	int          =					{"""closure""": closure} if closure is not None else {}
                        xm.optimizer_step(self.optimizer					, optimizer_args=lowerCamelCase						)
                 elif self.scaler is not None:
                        snake_case__					:	Any          =					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
                               snake_case__					:	Optional[int]          =					self.scaler.get_scale()
                               snake_case__					:	List[Any]          =					True
                        self.scaler.step(self.optimizer					, lowerCamelCase						)
                        self.scaler.update()
                        snake_case__					:	Optional[int]          =					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.
                               snake_case__					:	Tuple          =					scale_after < self._last_scale
                        snake_case__					:	Tuple          =					scale_after

                 else:
                        self.optimizer.step(lowerCamelCase						)




   def   __lowerCAmelCase						(	self	:					Dict					, lowerCamelCase	:					Any						)->	Tuple:
          for param_group in self.optimizer.param_groups:
                 snake_case__					:	List[str]          =					[parameters_map.get(lowerCamelCase					, lowerCamelCase						) for p in param_group["""params"""]]




   @property
   def   __lowerCAmelCase						(	self	:					List[Any]						)->	Union[str, Any]:
          warnings.warn(
              """The `is_overflow` property is deprecated and will be removed in version 1.0 of Accelerate use """
              """`optimizer.step_was_skipped` instead."""					, lowerCamelCase					, )
          return self._is_overflow




   @property
   def   __lowerCAmelCase						(	self	:					Union[str, Any]						)->	List[Any]:
          return self._is_overflow




   def __getstate__(	self	:					Optional[Any]						)->	str:
          return self.__dict__.copy()




   def __setstate__(	self	:					List[Any]					, lowerCamelCase	:					str						)->	int:
          self.__dict__.update(lowerCamelCase						)








'''simple docstring'''
from typing import Dict, Optional, Tuple

import torch



def 				lowerCAmelCase__	(       UpperCAmelCase     ):






       """simple docstring"""





       snake_case__					:	Optional[int]          =					boundaries[1] - boundaries[0]
       snake_case__					:	Optional[Any]          =					boundaries + step / 2
       snake_case__					:	str          =					torch.cat([bin_centers, (bin_centers[-1] + step).unsqueeze(-1     )]    ,       dim=0     )
       return bin_centers



def 				lowerCAmelCase__	(       UpperCAmelCase    ,       UpperCAmelCase    ,       ):






       """simple docstring"""





       snake_case__					:	str          =					_calculate_bin_centers(UpperCAmelCase     )
       return (
           torch.sum(aligned_distance_error_probs * bin_centers    ,       dim=-1     ),
           bin_centers[-1],
       )



def 				lowerCAmelCase__	(       UpperCAmelCase    ,       UpperCAmelCase = 31    ,       UpperCAmelCase = 64    ,       **UpperCAmelCase    ,       ):






       """simple docstring"""





       snake_case__					:	Dict          =					torch.linspace(0    ,       UpperCAmelCase    ,       steps=(no_bins - 1)    ,       device=logits.device     )

       snake_case__					:	str          =					torch.nn.functional.softmax(UpperCAmelCase    ,       dim=-1     )
       snake_case__       ,   snake_case__					:	List[str]          =					_calculate_expected_aligned_error(
           alignment_confidence_breaks=UpperCAmelCase    ,       aligned_distance_error_probs=UpperCAmelCase    ,       )

       return {
           "aligned_confidence_probs": aligned_confidence_probs,
           "predicted_aligned_error": predicted_aligned_error,
           "max_predicted_aligned_error": max_predicted_aligned_error,
       }






def 				lowerCAmelCase__	(       UpperCAmelCase    ,       UpperCAmelCase = None    ,       UpperCAmelCase = 31    ,       UpperCAmelCase = 64    ,       UpperCAmelCase = 1E-8    ,       **UpperCAmelCase    ,       ):






       """simple docstring"""





       if residue_weights is None:
              snake_case__					:	List[Any]          =					logits.new_ones(logits.shape[-2]     )

       snake_case__					:	Optional[Any]          =					torch.linspace(0    ,       UpperCAmelCase    ,       steps=(no_bins - 1)    ,       device=logits.device     )

       snake_case__					:	int          =					_calculate_bin_centers(UpperCAmelCase     )
       torch.sum(UpperCAmelCase     )
       snake_case__					:	Dict          =					logits.shape[-2]
       snake_case__					:	Optional[Any]          =					max(UpperCAmelCase    ,       19     )

       snake_case__					:	Optional[int]          =					1.2_4 * (clipped_n - 15) ** (1.0 / 3) - 1.8

       snake_case__					:	Tuple          =					torch.nn.functional.softmax(UpperCAmelCase    ,       dim=-1     )

       snake_case__					:	int          =					1.0 / (1 + (bin_centers**2) / (da**2))
       snake_case__					:	Optional[int]          =					torch.sum(probs * tm_per_bin    ,       dim=-1     )

       snake_case__					:	Optional[Any]          =					residue_weights / (eps + residue_weights.sum())
       snake_case__					:	Any          =					torch.sum(predicted_tm_term * normed_residue_mask    ,       dim=-1     )

       snake_case__					:	Dict          =					per_alignment * residue_weights

       snake_case__					:	List[str]          =					(weighted == torch.max(UpperCAmelCase     )).nonzero()[0]
       return per_alignment[tuple(UpperCAmelCase     )]




