




"""simple docstring"""






import torch

from accelerate import PartialState
from accelerate.utils.operations import broadcast, gather, gather_object, pad_across_processes, reduce







def   _snake_case    (       lowercase__	):
				return (torch.arange(state.num_processes	) + 1.0 + (state.num_processes * state.process_index)).to(state.device	)







def   _snake_case    (       lowercase__	):
				_lowerCamelCase  :    List[Any]					=					create_tensor(lowercase__	)
				_lowerCamelCase  :    int					=					gather(lowercase__	)
				assert gathered_tensor.tolist() == list(range(1							,					state.num_processes**2 + 1	)	)







def   _snake_case    (       lowercase__	):
				_lowerCamelCase  :    List[str]					=					[state.process_index]
				_lowerCamelCase  :    Tuple					=					gather_object(lowercase__	)
				assert len(lowercase__	) == state.num_processes, f'''{gathered_obj}, {len(lowercase__	)} != {state.num_processes}'''
				assert gathered_obj == list(range(state.num_processes	)	), f'''{gathered_obj} != {list(range(state.num_processes	)	)}'''







def   _snake_case    (       lowercase__	):
				_lowerCamelCase  :    Any					=					create_tensor(lowercase__	)
				_lowerCamelCase  :    List[Any]					=					broadcast(lowercase__	)
				assert broadcasted_tensor.shape == torch.Size([state.num_processes]	)
				assert broadcasted_tensor.tolist() == list(range(1							,					state.num_processes + 1	)	)







def   _snake_case    (       lowercase__	):
				# 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:
								_lowerCamelCase  :    Optional[Any]					=					torch.arange(state.num_processes + 1	).to(state.device	)
				else:
								_lowerCamelCase  :    int					=					torch.arange(state.num_processes	).to(state.device	)
				_lowerCamelCase  :    str					=					pad_across_processes(lowercase__	)
				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    (       lowercase__	):
				# For now runs on only two processes
				if state.num_processes != 2:
								return
				_lowerCamelCase  :    Any					=					create_tensor(lowercase__	)
				_lowerCamelCase  :    Optional[int]					=					reduce(lowercase__							,					'sum'	)
				_lowerCamelCase  :    Optional[Any]					=					torch.tensor([4.0, 6]	).to(state.device	)
				assert torch.allclose(lowercase__							,					lowercase__	), f'''{reduced_tensor} != {truth_tensor}'''







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







def   _snake_case    (       lowercase__	):
				# For xla_spawn (TPUs)
				main()







def   _snake_case    (       ):
				_lowerCamelCase  :    List[Any]					=					PartialState()
				state.print(f'''State: {state}'''	)
				state.print('testing gather'	)
				test_gather(lowercase__	)
				state.print('testing gather_object'	)
				test_gather_object(lowercase__	)
				state.print('testing broadcast'	)
				test_broadcast(lowercase__	)
				state.print('testing pad_across_processes'	)
				test_pad_across_processes(lowercase__	)
				state.print('testing reduce_sum'	)
				test_reduce_sum(lowercase__	)
				state.print('testing reduce_mean'	)
				test_reduce_mean(lowercase__	)


if __name__ == "__main__":
			main()





"""simple docstring"""






def   _snake_case    (       lowercase__	):
				return "".join(chr(ord(lowercase__	) - 32	) if 'a' <= char <= 'z' else char for char in word	)


if __name__ == "__main__":
			from doctest import testmod

			testmod()