






from typing import Dict, Optional, Tuple

import torch





def 						A_					(						lowercase_			)				->torch.Tensor:







 """simple docstring"""

 SCREAMING_SNAKE_CASE				    =		boundaries[1] - boundaries[0]
 SCREAMING_SNAKE_CASE				    =		boundaries + step / 2
 SCREAMING_SNAKE_CASE				    =		torch.cat([bin_centers, (bin_centers[-1] + step).unsqueeze(-1			)]	,  dim=0			)
 return bin_centers





def 						A_					(						lowercase_	,  lowercase_	,  )				->Tuple[torch.Tensor, torch.Tensor]:







 """simple docstring"""

 SCREAMING_SNAKE_CASE				    =		_calculate_bin_centers(lowercase_			)
 return (
     torch.sum(aligned_distance_error_probs * bin_centers	,  dim=-1			),
     bin_centers[-1],
 )





def 						A_					(						lowercase_	,  lowercase_ = 3_1	,  lowercase_ = 6_4	,  **lowercase_	,  )				->Dict[str, torch.Tensor]:







 """simple docstring"""

 SCREAMING_SNAKE_CASE				    =		torch.linspace(0	,  lowercase_	,  steps=(no_bins - 1)	,  device=logits.device			)

 SCREAMING_SNAKE_CASE				    =		torch.nn.functional.softmax(lowercase_	,  dim=-1			)
 SCREAMING_SNAKE_CASE    ,   SCREAMING_SNAKE_CASE				    =		_calculate_expected_aligned_error(
     alignment_confidence_breaks=lowercase_	,  aligned_distance_error_probs=lowercase_	,  )

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





def 						A_					(						lowercase_	,  lowercase_ = None	,  lowercase_ = 3_1	,  lowercase_ = 6_4	,  lowercase_ = 1e-8	,  **lowercase_	,  )				->torch.Tensor:







 """simple docstring"""

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

 SCREAMING_SNAKE_CASE				    =		torch.linspace(0	,  lowercase_	,  steps=(no_bins - 1)	,  device=logits.device			)

 SCREAMING_SNAKE_CASE				    =		_calculate_bin_centers(lowercase_			)
 torch.sum(lowercase_			)
 SCREAMING_SNAKE_CASE				    =		logits.shape[-2]
 SCREAMING_SNAKE_CASE				    =		max(lowercase_	,  1_9			)

 SCREAMING_SNAKE_CASE				    =		1.2_4 * (clipped_n - 1_5) ** (1.0 / 3) - 1.8

 SCREAMING_SNAKE_CASE				    =		torch.nn.functional.softmax(lowercase_	,  dim=-1			)

 SCREAMING_SNAKE_CASE				    =		1.0 / (1 + (bin_centers**2) / (da**2))
 SCREAMING_SNAKE_CASE				    =		torch.sum(probs * tm_per_bin	,  dim=-1			)

 SCREAMING_SNAKE_CASE				    =		residue_weights / (eps + residue_weights.sum())
 SCREAMING_SNAKE_CASE				    =		torch.sum(predicted_tm_term * normed_residue_mask	,  dim=-1			)

 SCREAMING_SNAKE_CASE				    =		per_alignment * residue_weights

 SCREAMING_SNAKE_CASE				    =		(weighted == torch.max(lowercase_			)).nonzero()[0]
 return per_alignment[tuple(lowercase_			)]





import torch

from accelerate import Accelerator, DistributedType



class 			UpperCamelCase					:

     '''simple docstring'''

     def __enter__(       self    )			->					int:






           """simple docstring"""
           if self.enabled:
                 _lowerCamelCase           =					self.model.no_sync()
                 self.model_sync_obj.__enter__()

           return self



     def __exit__(       self					,					A_					,					A_					,					A_    )			->					Union[str, Any]:






           """simple docstring"""
           if self.enabled:
                 # Average all models on exit
                 self._sync_and_avg_model_params()
                 self.model_sync_obj.__exit__(A_					,					A_					,					A_    )



     def __init__(       self					,					A_					,					A_					,					A_					,					A_ = True    )			->					List[str]:






           """simple docstring"""
           if accelerator.distributed_type not in [
               DistributedType.NO,
               DistributedType.MULTI_CPU,
               DistributedType.MULTI_GPU,
           ]:
                 raise NotImplementedError('''LocalSGD is supported only for CPUs and GPUs (no DeepSpeed or MegatronLM)'''    )
           _lowerCamelCase           =					enabled and accelerator.distributed_type != DistributedType.NO
           _lowerCamelCase           =					0
           if self.enabled:
                 _lowerCamelCase           =					accelerator
                 _lowerCamelCase           =					model
                 _lowerCamelCase           =					local_sgd_steps



     def 		UpperCamelCase_    (       self    )			->					Optional[Any]:






           """simple docstring"""
           self.num_steps += 1
           if not self.enabled:
                 return

           if self.num_steps % self.local_sgd_steps == 0:
                 self._sync_and_avg_model_params()





     def 		UpperCamelCase_    (       self    )			->					Union[str, Any]:






           """simple docstring"""
           self.accelerator.wait_for_everyone()
           with self.accelerator.autocast():
                 for param in self.model.parameters():
                       _lowerCamelCase           =					self.accelerator.reduce(param.data					,					reduction='''mean'''    )