






'''simple docstring'''







import torch.nn as nn

from .imports import is_fpa_available


if is_fpa_available():
       import transformer_engine.pytorch as te


def 						_UpperCamelCase							(     snake_case__							,			snake_case__=True							,			snake_case__=True							,			snake_case__=True							):
      if not is_fpa_available():
            raise ImportError("Using `convert_model` requires transformer_engine to be installed."							)
      for name, module in model.named_children():
            if isinstance(snake_case__							,			nn.Linear							) and to_transformer_engine and _convert_linear:
                  # Return early if the linear layer weights are not multiples of 16
                  if any(p % 16 != 0 for p in module.weight.shape							):
                        return
                  __A		   =      module.bias is not None
                  __A		   =      te.Linear(
                      module.in_features							,			module.out_features							,			bias=snake_case__							,			params_dtype=module.weight.dtype							)
                  __A		   =      module.weight.data.clone()
                  if has_bias:
                        __A		   =      module.bias.data.clone()

                  setattr(snake_case__							,			snake_case__							,			snake_case__							)
            elif isinstance(snake_case__							,			nn.LayerNorm							) and to_transformer_engine and _convert_ln:
                  __A		   =      te.LayerNorm(module.normalized_shape[0]							,			eps=module.eps							,			params_dtype=module.weight.dtype							)
                  __A		   =      module.weight.data.clone()
                  __A		   =      module.bias.data.clone()

                  setattr(snake_case__							,			snake_case__							,			snake_case__							)
            elif isinstance(snake_case__							,			te.Linear							) and not to_transformer_engine and _convert_linear:
                  __A		   =      module.bias is not None
                  __A		   =      nn.Linear(
                      module.in_features							,			module.out_features							,			bias=snake_case__							,			params_dtype=module.weight.dtype							)
                  __A		   =      module.weight.data.clone()
                  if has_bias:
                        __A		   =      module.bias.data.clone()

                  setattr(snake_case__							,			snake_case__							,			snake_case__							)
            elif isinstance(snake_case__							,			te.LayerNorm							) and not to_transformer_engine and _convert_ln:
                  __A		   =      nn.LayerNorm(module.normalized_shape[0]							,			eps=module.eps							,			params_dtype=module.weight.dtype							)
                  __A		   =      module.weight.data.clone()
                  __A		   =      module.bias.data.clone()

                  setattr(snake_case__							,			snake_case__							,			snake_case__							)
            else:
                  convert_model(
                      snake_case__							,			to_transformer_engine=snake_case__							,			_convert_linear=_convert_linear							,			_convert_ln=_convert_ln							,			)





def 						_UpperCamelCase							(     snake_case__							):
      if not is_fpa_available():
            raise ImportError("Using `has_transformer_engine_layers` requires transformer_engine to be installed."							)
      for m in model.modules():
            if isinstance(snake_case__							,			(te.LayerNorm, te.Linear)							):
                  return True
      return False










'''simple docstring'''







from __future__ import annotations

from queue import Queue






class 					a__    :
     '''simple docstring'''







     def __init__(self    :	Any					)  ->       None:
           __A		   =      {}






     def     _UpperCamelCase   (self    :	Optional[int]					)  ->       None:
           for i in self.vertices:
                 print(lowercase__				,				" : "				,				" -> ".join([str(lowercase__					) for j in self.vertices[i]]					)					)






     def     _UpperCamelCase   (self    :	Union[str, Any]				,				lowercase__    :	int				,				lowercase__    :	int					)  ->       None:
           if from_vertex in self.vertices:
                 self.vertices[from_vertex].append(lowercase__					)
           else:
                 __A		   =      [to_vertex]






     def     _UpperCamelCase   (self    :	Optional[int]				,				lowercase__    :	int					)  ->       set[int]:
           __A		   =      set()

           # create a first in first out queue to store all the vertices for BFS
           __A		   =      Queue()

           # mark the source node as visited and enqueue it
           visited.add(lowercase__					)
           queue.put(lowercase__					)

           while not queue.empty():
                 __A		   =      queue.get()

                 # loop through all adjacent vertex and enqueue it if not yet visited
                 for adjacent_vertex in self.vertices[vertex]:
                       if adjacent_vertex not in visited:
                             queue.put(lowercase__					)
                             visited.add(lowercase__					)
           return visited


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

       testmod(verbose=True)

       __A      :       Dict      =	Graph()
       g.add_edge(0, 1)
       g.add_edge(0, 2)
       g.add_edge(1, 2)
       g.add_edge(2, 0)
       g.add_edge(2, 3)
       g.add_edge(3, 3)

       g.print_graph()
       # 0  :  1 -> 2
       # 1  :  2
       # 2  :  0 -> 3
       # 3  :  3

       assert sorted(g.bfs(2)) == [0, 1, 2, 3]


