





# Copyright 2023 The HuggingFace Team. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import torch

from accelerate import PartialState
from accelerate.utils.operations import broadcast, gather, gather_object, pad_across_processes, reduce
def      A__       (    SCREAMING_SNAKE_CASE_   :      List[str]						)  ->  Dict:






   """simple docstring"""







   return (torch.arange(state.num_processes						) + 1.0 + (state.num_processes * state.process_index)).to(state.device						)
def      A__       (    SCREAMING_SNAKE_CASE_   :      Tuple						)  ->  List[str]:






   """simple docstring"""







   _UpperCAmelCase           =   create_tensor(SCREAMING_SNAKE_CASE_						)
   _UpperCAmelCase           =   gather(SCREAMING_SNAKE_CASE_						)
   assert gathered_tensor.tolist() == list(range(1				,	state.num_processes**2 + 1						)						)
def      A__       (    SCREAMING_SNAKE_CASE_   :      Union[str, Any]						)  ->  str:






   """simple docstring"""







   _UpperCAmelCase           =   [state.process_index]
   _UpperCAmelCase           =   gather_object(SCREAMING_SNAKE_CASE_						)
   assert len(SCREAMING_SNAKE_CASE_						) == state.num_processes, F'''{gathered_obj}, {len(SCREAMING_SNAKE_CASE_						)} != {state.num_processes}'''
   assert gathered_obj == list(range(state.num_processes						)						), F'''{gathered_obj} != {list(range(state.num_processes						)						)}'''
def      A__       (    SCREAMING_SNAKE_CASE_   :      int						)  ->  Union[str, Any]:






   """simple docstring"""







   _UpperCAmelCase           =   create_tensor(SCREAMING_SNAKE_CASE_						)
   _UpperCAmelCase           =   broadcast(SCREAMING_SNAKE_CASE_						)
   assert broadcasted_tensor.shape == torch.Size([state.num_processes]						)
   assert broadcasted_tensor.tolist() == list(range(1				,	state.num_processes + 1						)						)
def      A__       (    SCREAMING_SNAKE_CASE_   :      str						)  ->  Dict:






   """simple docstring"""







   if state.is_main_process:
      _UpperCAmelCase           =   torch.arange(state.num_processes + 1						).to(state.device						)
   else:
      _UpperCAmelCase           =   torch.arange(state.num_processes						).to(state.device						)
   _UpperCAmelCase           =   pad_across_processes(SCREAMING_SNAKE_CASE_						)
   assert padded_tensor.shape == torch.Size([state.num_processes + 1]						)
   if not state.is_main_process:
      assert padded_tensor.tolist() == list(range(0				,	state.num_processes						)						) + [0]
def      A__       (    SCREAMING_SNAKE_CASE_   :      Any						)  ->  Dict:






   """simple docstring"""







   if state.num_processes != 2:
      return
   _UpperCAmelCase           =   create_tensor(SCREAMING_SNAKE_CASE_						)
   _UpperCAmelCase           =   reduce(SCREAMING_SNAKE_CASE_				,	'''sum'''						)
   _UpperCAmelCase           =   torch.tensor([4.0, 6]						).to(state.device						)
   assert torch.allclose(SCREAMING_SNAKE_CASE_				,	SCREAMING_SNAKE_CASE_						), F'''{reduced_tensor} != {truth_tensor}'''
def      A__       (    SCREAMING_SNAKE_CASE_   :      Tuple						)  ->  Optional[int]:






   """simple docstring"""







   if state.num_processes != 2:
      return
   _UpperCAmelCase           =   create_tensor(SCREAMING_SNAKE_CASE_						)
   _UpperCAmelCase           =   reduce(SCREAMING_SNAKE_CASE_				,	'''mean'''						)
   _UpperCAmelCase           =   torch.tensor([2.0, 3]						).to(state.device						)
   assert torch.allclose(SCREAMING_SNAKE_CASE_				,	SCREAMING_SNAKE_CASE_						), F'''{reduced_tensor} != {truth_tensor}'''
def      A__       (    SCREAMING_SNAKE_CASE_   :      Any						)  ->  List[str]:






   """simple docstring"""







   main()
def      A__       (    )  ->  Dict:






   """simple docstring"""







   _UpperCAmelCase           =   PartialState()
   state.print(F'''State: {state}'''						)
   state.print('''testing gather'''						)
   test_gather(SCREAMING_SNAKE_CASE_						)
   state.print('''testing gather_object'''						)
   test_gather_object(SCREAMING_SNAKE_CASE_						)
   state.print('''testing broadcast'''						)
   test_broadcast(SCREAMING_SNAKE_CASE_						)
   state.print('''testing pad_across_processes'''						)
   test_pad_across_processes(SCREAMING_SNAKE_CASE_						)
   state.print('''testing reduce_sum'''						)
   test_reduce_sum(SCREAMING_SNAKE_CASE_						)
   state.print('''testing reduce_mean'''						)
   test_reduce_mean(SCREAMING_SNAKE_CASE_						)


if __name__ == "__main__":
   main()






def      A__       (    SCREAMING_SNAKE_CASE_   :      int						)  ->  bool:






   """simple docstring"""







   if not isinstance(SCREAMING_SNAKE_CASE_				,	SCREAMING_SNAKE_CASE_						):
      _UpperCAmelCase           =   F'''Input value of [number={number}] must be an integer'''
      raise TypeError(SCREAMING_SNAKE_CASE_						)
   if number < 0:
      return False
   _UpperCAmelCase           =   number * number
   while number > 0:
      if number % 10 != number_square % 10:
         return False
      number //= 10
      number_square //= 10
   return True


if __name__ == "__main__":
   import doctest

   doctest.testmod()