




'''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_							(_a	:							Any     ):
    return (torch.arange(state.num_processes     ) + 1.0 + (state.num_processes * state.process_index)).to(state.device     )





def 						snake_case_							(_a	:							List[Any]     ):
    UpperCAmelCase			       =		create_tensor(_a     )
    UpperCAmelCase			       =		gather(_a     )
    assert gathered_tensor.tolist() == list(range(1    ,   state.num_processes**2 + 1     )     )





def 						snake_case_							(_a	:							str     ):
    UpperCAmelCase			       =		[state.process_index]
    UpperCAmelCase			       =		gather_object(_a     )
    assert len(_a     ) == state.num_processes, F"{gathered_obj}, {len(_a     )} != {state.num_processes}"
    assert gathered_obj == list(range(state.num_processes     )     ), F"{gathered_obj} != {list(range(state.num_processes     )     )}"





def 						snake_case_							(_a	:							Optional[int]     ):
    UpperCAmelCase			       =		create_tensor(_a     )
    UpperCAmelCase			       =		broadcast(_a     )
    assert broadcasted_tensor.shape == torch.Size([state.num_processes]     )
    assert broadcasted_tensor.tolist() == list(range(1    ,   state.num_processes + 1     )     )





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





def 						snake_case_							(_a	:							Optional[Any]     ):
    # For now runs on only two processes
    if state.num_processes != 2:
        return
    UpperCAmelCase			       =		create_tensor(_a     )
    UpperCAmelCase			       =		reduce(_a    ,   '''mean'''     )
    UpperCAmelCase			       =		torch.tensor([2.0, 3]     ).to(state.device     )
    assert torch.allclose(_a    ,   _a     ), F"{reduced_tensor} != {truth_tensor}"





def 						snake_case_							(_a	:							Any     ):
    # For xla_spawn (TPUs)
    main()





def 						snake_case_							():
    UpperCAmelCase			       =		PartialState()
    state.print(F"State: {state}"     )
    state.print('''testing gather'''     )
    test_gather(_a     )
    state.print('''testing gather_object'''     )
    test_gather_object(_a     )
    state.print('''testing broadcast'''     )
    test_broadcast(_a     )
    state.print('''testing pad_across_processes'''     )
    test_pad_across_processes(_a     )
    state.print('''testing reduce_sum'''     )
    test_reduce_sum(_a     )
    state.print('''testing reduce_mean'''     )
    test_reduce_mean(_a     )


if __name__ == "__main__":
   main()






from __future__ import annotations





class       _snake_case					:







			def __init__(				self						,   _a		):
				__magic_name__		:		Optional[Any]	   =     data
				__magic_name__		:		Node | None	   =     None
				__magic_name__		:		Node | None	   =     None







def   lowerCAmelCase_						(					_snake_case      :  Node | None   )				->		None:  # In Order traversal of the tree







	'''simple docstring'''


	if tree:
		display(tree.left   )
		print(tree.data   )
		display(tree.right   )






def   lowerCAmelCase_						(					_snake_case      :  Node | None   )				->		int:







	'''simple docstring'''


	return 1 + max(depth_of_tree(tree.left   )      ,   depth_of_tree(tree.right   )   ) if tree else 0






def   lowerCAmelCase_						(					_snake_case      :  Node   )				->		bool:







	'''simple docstring'''


	if not tree:
		return True
	if tree.left and tree.right:
		return is_full_binary_tree(tree.left   ) and is_full_binary_tree(tree.right   )
	else:
		return not tree.left and not tree.right






def   lowerCAmelCase_						(					)				->		None:  # Main function for testing.







	'''simple docstring'''


	__magic_name__		:		int	   =     Node(1   )
	__magic_name__		:		Union[str, Any]	   =     Node(2   )
	__magic_name__		:		Tuple	   =     Node(3   )
	__magic_name__		:		Optional[Any]	   =     Node(4   )
	__magic_name__		:		Union[str, Any]	   =     Node(5   )
	__magic_name__		:		Any	   =     Node(6   )
	__magic_name__		:		int	   =     Node(7   )
	__magic_name__		:		List[str]	   =     Node(8   )
	__magic_name__		:		Union[str, Any]	   =     Node(9   )

	print(is_full_binary_tree(_snake_case   )   )
	print(depth_of_tree(_snake_case   )   )
	print("Tree is: "   )
	display(_snake_case   )


if __name__ == "__main__":
				main()
