


'''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 lowerCamelCase      (      lowerCAmelCase    :     Dict		):





	"""simple docstring"""







	return (torch.arange(state.num_processes		) + 1.0 + (state.num_processes * state.process_index)).to(state.device		)





def lowerCamelCase      (      lowerCAmelCase    :     int		):





	"""simple docstring"""







	__magic_name__	:   Any					     =					create_tensor(lowerCAmelCase		)
	__magic_name__	:   Any					     =					gather(lowerCAmelCase		)
	assert gathered_tensor.tolist() == list(range(1		,		state.num_processes**2 + 1		)		)





def lowerCamelCase      (      lowerCAmelCase    :     Dict		):





	"""simple docstring"""







	__magic_name__	:   List[Any]					     =					[state.process_index]
	__magic_name__	:   Any					     =					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 lowerCamelCase      (      lowerCAmelCase    :     int		):





	"""simple docstring"""







	__magic_name__	:   Optional[int]					     =					create_tensor(lowerCAmelCase		)
	__magic_name__	:   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 lowerCamelCase      (      lowerCAmelCase    :     Tuple		):





	"""simple docstring"""







	if state.is_main_process:
		__magic_name__	:   Optional[Any]					     =					torch.arange(state.num_processes + 1		).to(state.device		)
	else:
		__magic_name__	:   Union[str, Any]					     =					torch.arange(state.num_processes		).to(state.device		)
	__magic_name__	:   List[Any]					     =					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 lowerCamelCase      (      lowerCAmelCase    :     List[Any]		):





	"""simple docstring"""







	if state.num_processes != 2:
		return
	__magic_name__	:   Optional[Any]					     =					create_tensor(lowerCAmelCase		)
	__magic_name__	:   int					     =					reduce(lowerCAmelCase		,		'sum'		)
	__magic_name__	:   Optional[Any]					     =					torch.tensor([4.0, 6]		).to(state.device		)
	assert torch.allclose(lowerCAmelCase		,		lowerCAmelCase		), f'{reduced_tensor} != {truth_tensor}'





def lowerCamelCase      (      lowerCAmelCase    :     Tuple		):





	"""simple docstring"""







	if state.num_processes != 2:
		return
	__magic_name__	:   List[str]					     =					create_tensor(lowerCAmelCase		)
	__magic_name__	:   int					     =					reduce(lowerCAmelCase		,		'mean'		)
	__magic_name__	:   Optional[Any]					     =					torch.tensor([2.0, 3]		).to(state.device		)
	assert torch.allclose(lowerCAmelCase		,		lowerCAmelCase		), f'{reduced_tensor} != {truth_tensor}'





def lowerCamelCase      (      lowerCAmelCase    :     Any		):





	"""simple docstring"""







	main()





def lowerCamelCase      (      ):





	"""simple docstring"""







	__magic_name__	:   Tuple					     =					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'''

def lowerCamelCase      (      lowerCAmelCase    :     int		):





	"""simple docstring"""







	if not isinstance(lowerCAmelCase		,		lowerCAmelCase		):
		__magic_name__	:   int					     =					f'Input value of [number={number}] must be an integer'
		raise TypeError(lowerCAmelCase		)
	if number < 0:
		return False
	__magic_name__	:   Tuple					     =					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()