def    __magic_name__(	__UpperCAmelCase		)    ->     tuple[int, int]:



      '''simple docstring'''

      if len(__UpperCAmelCase		) == 1:
            return a[0], a[0]
      else:
            # split A into half.
            _lowerCamelCase           =					a[: len(__UpperCAmelCase		) // 2]
            _lowerCamelCase           =					a[len(__UpperCAmelCase		) // 2 :]

            # 2 sub problems, 1/2 of original size.
            _lowerCamelCase     ,       _lowerCamelCase           =					max_difference(__UpperCAmelCase		)
            _lowerCamelCase     ,       _lowerCamelCase           =					max_difference(__UpperCAmelCase		)

            # get min of first and max of second
            # linear time
            _lowerCamelCase           =					min(__UpperCAmelCase		)
            _lowerCamelCase           =					max(__UpperCAmelCase		)

            # 3 cases, either (small1, big1),
            # (min_first, max_second), (small2, big2)
            # constant comparisons
            if biga - smalla > max_second - min_first and biga - smalla > biga - smalla:
                  return smalla, biga
            elif biga - smalla > max_second - min_first:
                  return smalla, biga
            else:
                  return min_first, max_second


if __name__ == "__main__":
     import doctest

     doctest.testmod()
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'''    )