





"""simple docstring"""
from __future__ import annotations

from queue import Queue
class lowerCamelCase__  :



   def __init__(       self	) ->    None:







      """simple docstring"""
      lowercase    :			dict[int, list[int]]  =       {}



   def    _UpperCAmelCase	(       self	) ->    None:







      """simple docstring"""
      for i in self.vertices:
         print(snake_case      ,   """ : """      ,   """ -> """.join([str(snake_case	) for j in self.vertices[i]]	)	)



   def    _UpperCAmelCase	(       self      ,   snake_case      ,   snake_case	) ->    None:







      """simple docstring"""
      if from_vertex in self.vertices:
         self.vertices[from_vertex].append(snake_case	)
      else:
         lowercase    :			Union[str, Any]  =       [to_vertex]





   def    _UpperCAmelCase	(       self      ,   snake_case	) ->    set[int]:







      """simple docstring"""
      lowercase    :			Optional[int]  =       set()

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

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

      while not queue.empty():
         lowercase    :			str  =       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(snake_case	)
               visited.add(snake_case	)
      return visited


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

   testmod(verbose=True)

   lowerCAmelCase:	Optional[int]							      =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]









"""simple docstring"""
import torch.nn as nn

from .imports import is_fpa_available


if is_fpa_available():
   import transformer_engine.pytorch as te




def 			__snake_case						(      __A ,__A=True ,__A=True ,__A=True    )		->				Union[str, Any]:
   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(__A ,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
         lowercase    :			Optional[int]  =       module.bias is not None
         lowercase    :			Union[str, Any]  =       te.Linear(
             module.in_features ,module.out_features ,bias=__A ,params_dtype=module.weight.dtype    )
         lowercase    :			Union[str, Any]  =       module.weight.data.clone()
         if has_bias:
            lowercase    :			Dict  =       module.bias.data.clone()

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

         setattr(__A ,__A ,__A    )
      elif isinstance(__A ,te.Linear    ) and not to_transformer_engine and _convert_linear:
         lowercase    :			Any  =       module.bias is not None
         lowercase    :			List[Any]  =       nn.Linear(
             module.in_features ,module.out_features ,bias=__A ,params_dtype=module.weight.dtype    )
         lowercase    :			Union[str, Any]  =       module.weight.data.clone()
         if has_bias:
            lowercase    :			Optional[Any]  =       module.bias.data.clone()

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

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







def 			__snake_case						(      __A    )		->				Union[str, Any]:
   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(__A ,(te.LayerNorm, te.Linear)    ):
         return True
   return False


