



"""simple docstring"""






# 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 			snake_case		(		lowerCAmelCase_	)						->				Optional[Any]:
  return (torch.arange(state.num_processes	) + 1.0 + (state.num_processes * state.process_index)).to(state.device	)




def 			snake_case		(		lowerCAmelCase_	)						->				Any:
  _snake_case   	=      create_tensor(lowerCAmelCase_	)
  _snake_case   	=      gather(lowerCAmelCase_	)
  assert gathered_tensor.tolist() == list(range(1  ,						state.num_processes**2 + 1	)	)




def 			snake_case		(		lowerCAmelCase_	)						->				str:
  _snake_case   	=      [state.process_index]
  _snake_case   	=      gather_object(lowerCAmelCase_	)
  assert len(lowerCAmelCase_	) == state.num_processes, f"""{gathered_obj}, {len(lowerCAmelCase_	)} != {state.num_processes}"""
  assert gathered_obj == list(range(state.num_processes	)	), f"""{gathered_obj} != {list(range(state.num_processes	)	)}"""




def 			snake_case		(		lowerCAmelCase_	)						->				Dict:
  _snake_case   	=      create_tensor(lowerCAmelCase_	)
  _snake_case   	=      broadcast(lowerCAmelCase_	)
  assert broadcasted_tensor.shape == torch.Size([state.num_processes]	)
  assert broadcasted_tensor.tolist() == list(range(1  ,						state.num_processes + 1	)	)




def 			snake_case		(		lowerCAmelCase_	)						->				str:
  # We need to pad the tensor with one more element if we are the main process
  # to ensure that we can pad
  if state.is_main_process:
    _snake_case   	=      torch.arange(state.num_processes + 1	).to(state.device	)
  else:
    _snake_case   	=      torch.arange(state.num_processes	).to(state.device	)
  _snake_case   	=      pad_across_processes(lowerCAmelCase_	)
  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 			snake_case		(		lowerCAmelCase_	)						->				str:
  # For now runs on only two processes
  if state.num_processes != 2:
    return
  _snake_case   	=      create_tensor(lowerCAmelCase_	)
  _snake_case   	=      reduce(lowerCAmelCase_  ,						'''sum'''	)
  _snake_case   	=      torch.tensor([4.0, 6]	).to(state.device	)
  assert torch.allclose(lowerCAmelCase_  ,						lowerCAmelCase_	), f"""{reduced_tensor} != {truth_tensor}"""




def 			snake_case		(		lowerCAmelCase_	)						->				Any:
  # For now runs on only two processes
  if state.num_processes != 2:
    return
  _snake_case   	=      create_tensor(lowerCAmelCase_	)
  _snake_case   	=      reduce(lowerCAmelCase_  ,						'''mean'''	)
  _snake_case   	=      torch.tensor([2.0, 3]	).to(state.device	)
  assert torch.allclose(lowerCAmelCase_  ,						lowerCAmelCase_	), f"""{reduced_tensor} != {truth_tensor}"""




def 			snake_case		(		lowerCAmelCase_	)						->				Dict:
  # For xla_spawn (TPUs)
  main()




def 			snake_case		(		)						->				int:
  _snake_case   	=      PartialState()
  state.print(f"""State: {state}"""	)
  state.print('''testing gather'''	)
  test_gather(lowerCAmelCase_	)
  state.print('''testing gather_object'''	)
  test_gather_object(lowerCAmelCase_	)
  state.print('''testing broadcast'''	)
  test_broadcast(lowerCAmelCase_	)
  state.print('''testing pad_across_processes'''	)
  test_pad_across_processes(lowerCAmelCase_	)
  state.print('''testing reduce_sum'''	)
  test_reduce_sum(lowerCAmelCase_	)
  state.print('''testing reduce_mean'''	)
  test_reduce_mean(lowerCAmelCase_	)


if __name__ == "__main__":
     main()










"""simple docstring"""






from abc import ABC, abstractmethod
from argparse import ArgumentParser



class     UpperCAmelCase    (	__SCREAMING_SNAKE_CASE  ):



   @staticmethod
   @abstractmethod
   def    __UpperCAmelCase    (       __lowerCamelCase    :	ArgumentParser				):



     """simple docstring"""
     raise NotImplementedError()





   @abstractmethod
   def    __UpperCAmelCase    (       self    :	Optional[Any]				):



     """simple docstring"""
     raise NotImplementedError()





