



"""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      __UpperCAmelCase      (    __lowerCamelCase	)							->      Dict:
		return (torch.arange(state.num_processes	) + 1.0 + (state.num_processes * state.process_index)).to(state.device	)



def      __UpperCAmelCase      (    __lowerCamelCase	)							->      List[Any]:
		lowercase__   :    Dict				=			create_tensor(__lowerCamelCase	)
		lowercase__   :    int				=			gather(__lowerCamelCase	)
		assert gathered_tensor.tolist() == list(range(1						,     state.num_processes**2 + 1	)	)



def      __UpperCAmelCase      (    __lowerCamelCase	)							->      List[str]:
		lowercase__   :    List[str]				=			[state.process_index]
		lowercase__   :    Dict				=			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      __UpperCAmelCase      (    __lowerCamelCase	)							->      List[Any]:
		lowercase__   :    str				=			create_tensor(__lowerCamelCase	)
		lowercase__   :    Optional[Any]				=			broadcast(__lowerCamelCase	)
		assert broadcasted_tensor.shape == torch.Size([state.num_processes]	)
		assert broadcasted_tensor.tolist() == list(range(1						,     state.num_processes + 1	)	)



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



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



def      __UpperCAmelCase      (    __lowerCamelCase	)							->      Optional[int]:
		# For xla_spawn (TPUs)
		main()



def      __UpperCAmelCase      (    )							->      List[str]:
		lowercase__   :    List[str]				=			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"""







import unittest

from accelerate import debug_launcher
from accelerate.test_utils import require_cpu, test_ops, test_script
@require_cpu
class        __A       (	unittest.TestCase    ):







		'''simple docstring'''





		def 					UpperCAmelCase    (	self						:  Optional[int]						)					->		str:






				"""simple docstring"""



				debug_launcher(test_script.main						)







		def 					UpperCAmelCase    (	self						:  Dict						)					->		Tuple:






				"""simple docstring"""



				debug_launcher(test_ops.main						)
