





'''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 : Tuple			):
   return (torch.arange(state.num_processes			) + 1.0 + (state.num_processes * state.process_index)).to(state.device			)
def 							UpperCAmelCase_  (						__lowerCamelCase : Optional[int]			):
   lowercase_      :Any         =			create_tensor(lowercase_			)
   lowercase_      :Optional[int]         =			gather(lowercase_			)
   assert gathered_tensor.tolist() == list(range(1     ,state.num_processes**2 + 1			)			)
def 							UpperCAmelCase_  (						__lowerCamelCase : int			):
   lowercase_      :str         =			[state.process_index]
   lowercase_      :Any         =			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 							UpperCAmelCase_  (						__lowerCamelCase : Tuple			):
   lowercase_      :List[str]         =			create_tensor(lowercase_			)
   lowercase_      :Tuple         =			broadcast(lowercase_			)
   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]			):





   if state.is_main_process:
      lowercase_      :Optional[int]         =			torch.arange(state.num_processes + 1			).to(state.device			)
   else:
      lowercase_      :List[str]         =			torch.arange(state.num_processes			).to(state.device			)
   lowercase_      :List[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 							UpperCAmelCase_  (						__lowerCamelCase : List[str]			):





   if state.num_processes != 2:
      return
   lowercase_      :int         =			create_tensor(lowercase_			)
   lowercase_      :Optional[int]         =			reduce(lowercase_     ,"sum"			)
   lowercase_      :List[Any]         =			torch.tensor([4.0, 6]			).to(state.device			)
   assert torch.allclose(lowercase_     ,lowercase_			), F'{reduced_tensor} != {truth_tensor}'
def 							UpperCAmelCase_  (						__lowerCamelCase : Tuple			):





   if state.num_processes != 2:
      return
   lowercase_      :Tuple         =			create_tensor(lowercase_			)
   lowercase_      :Tuple         =			reduce(lowercase_     ,"mean"			)
   lowercase_      :List[str]         =			torch.tensor([2.0, 3]			).to(state.device			)
   assert torch.allclose(lowercase_     ,lowercase_			), F'{reduced_tensor} != {truth_tensor}'
def 							UpperCAmelCase_  (						__lowerCamelCase : Optional[int]			):
   main()
def 							UpperCAmelCase_  (						):
   lowercase_      :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'''


from __future__ import annotations

lowerCAmelCase :					Any 							={
    '''A''': ['''B''', '''C''', '''E'''],
    '''B''': ['''A''', '''D''', '''E'''],
    '''C''': ['''A''', '''F''', '''G'''],
    '''D''': ['''B'''],
    '''E''': ['''A''', '''B''', '''D'''],
    '''F''': ['''C'''],
    '''G''': ['''C'''],
}
class 				a_       :





     def __init__(			self		: List[Any]     ,				lowercase		: dict[str, list[str]]     ,				lowercase		: str						):





        """simple docstring"""




        lowercase_      :List[str]         =			graph
        # mapping node to its parent in resulting breadth first tree
        lowercase_      :dict[str, str | None]         =			{}
        lowercase_      :Dict         =			source_vertex





     def 							lowercase__				(			self		: Tuple						):





        """simple docstring"""




        lowercase_      :Union[str, Any]         =			{self.source_vertex}
        lowercase_      :Tuple         =			None
        lowercase_      :Dict         =			[self.source_vertex]  # first in first out queue

        while queue:
           lowercase_      :int         =			queue.pop(0						)
           for adjacent_vertex in self.graph[vertex]:
              if adjacent_vertex not in visited:
                 visited.add(lowercase						)
                 lowercase_      :Optional[Any]         =			vertex
                 queue.append(lowercase						)







     def 							lowercase__				(			self		: Tuple     ,				lowercase		: str						):





        """simple docstring"""




        if target_vertex == self.source_vertex:
           return self.source_vertex

        lowercase_      :List[str]         =			self.parent.get(lowercase						)
        if target_vertex_parent is None:
           lowercase_      :Union[str, Any]         =			(
               F'No path from vertex: {self.source_vertex} to vertex: {target_vertex}'
           )
           raise ValueError(lowercase						)

        return self.shortest_path(lowercase						) + F'->{target_vertex}'


if __name__ == "__main__":
 lowerCAmelCase :					Dict 							=Graph(graph, '''G''')
 g.breath_first_search()
 print(g.shortest_path('''D'''))
 print(g.shortest_path('''G'''))
 print(g.shortest_path('''Foo'''))


