




'''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						)-> str:




     '''simple docstring'''





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







def       _UpperCamelCase			(_lowerCamelCase				:  Optional[int]						)-> int:




     '''simple docstring'''





     __snake_case            =	create_tensor(_lowerCAmelCase						)
     __snake_case            =	gather(_lowerCAmelCase						)
     assert gathered_tensor.tolist() == list(range(1 ,						state.num_processes**2 + 1						)						)







def       _UpperCamelCase			(_lowerCamelCase				:  Dict						)-> Dict:




     '''simple docstring'''





     __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       _UpperCamelCase			(_lowerCamelCase				:  int						)-> Tuple:




     '''simple docstring'''





     __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       _UpperCamelCase			(_lowerCamelCase				:  List[str]						)-> Tuple:




     '''simple docstring'''





     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       _UpperCamelCase			(_lowerCamelCase				:  List[str]						)-> int:




     '''simple docstring'''





     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       _UpperCamelCase			(_lowerCamelCase				:  str						)-> Dict:




     '''simple docstring'''





     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       _UpperCamelCase			(_lowerCamelCase				:  List[Any]						)-> Optional[Any]:




     '''simple docstring'''





     main()







def       _UpperCamelCase			()-> str:




     '''simple docstring'''





     __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()










def 						_lowerCAmelCase							(     _lowerCAmelCase			)    ->						int:


						'''simple docstring'''






						assert column_title.isupper()
						__snake_case				=		0
						__snake_case				=		len(_lowerCAmelCase			) - 1
						__snake_case				=		0

						while index >= 0:
												__snake_case				=		(ord(column_title[index]			) - 64) * pow(26   ,						_lowerCAmelCase			)
												answer += value
												power += 1
												index -= 1

						return answer


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

	testmod()





