
    Ph              
          U d dl Z d dlZd dlZd dlmZ d dlmZmZmZm	Z	m
Z
mZ d dlmZ d dlmZ d dlmZ d dlmZmZ  e j,                  d      Ze j0                  ed<    G d	 d
ee      Z G d dee      Zd dej:                  dedefdZdej@                  dej:                  fdZ!	 d!dej@                  dedede
ej:                     fdZ"dej@                  de
ej:                     defdZ#dej@                  de
ej:                     dej:                  de
ej:                     fdZ$d dejJ                  deddfdZ&d"deeejJ                  f   dedefdZ'y)#    N)Enum)CallablecastDictIterableListSet)TensorMetadata)_pytree)tree_flattentree_unflattengraph_utilsloggerc                   $    e Zd ZdZdZdZdZdZdZy)OPcall_functioncall_modulecall_methodget_attroutputplaceholderN)	__name__
__module____qualname__CALL_FUNCTIONCALL_MODULECALL_METHODGET_ATTROUTPUTPLACEHOLDER     nC:\Users\daisl\Desktop\realtime-object-detection\venv\Lib\site-packages\torch/distributed/_spmd/graph_utils.pyr   r      s     #MKKHFKr"   r   c                        e Zd ZdZdZdZdZdZy)CommType
allreduce_
allgather_
broadcast_reduce_scatter_scatter_N)r   r   r   	ALLREDUCE	ALLGATHER	BROADCASTREDUCESCATTERSCATTERr!   r"   r#   r%   r%      s    III%MGr"   r%   nodeis_requiredreturnc           
          | j                   j                  dd       }|rA|?t        d| j                   d| j                   d| j                   d| j
                         |S )Ntensor_metaz2Callsite expects that ``tensor_meta`` exists in ``z ``, but got None instead. Node:  )metagetRuntimeErrornameoptarget)r0   r1   metadatas      r#   get_node_tensor_metadatar=   !   sf    yy}}]D1Hx'@ L++/77)1TYYKqO
 	
 Or"   graphc                     t        | j                        D ]#  }|j                  t        j                  k(  s!|c S  t        d|        )zTake a graphmodule and return the graph output node.

    We traverse in reverse to expedite it, with the idea that last node should be output
    zCannot find the output node in )reversednodesr:   r   r   r8   )r>   r0   s     r#   
get_outputrB   +   sA    
 %77biiK & 8@
AAr"   	predicatereverse_orderc                     t        t        t        j                     | j                        }|r3t        t        t        j                     t        t        |                  }|D cg c]  } ||      s| c}S c c}w )zSTake a predicate and return all the nodes in the `graph` where the predicate holds.)r   r   fxNoderA   iterr@   )r>   rC   rD   rA   r0   s        r#   	find_noderI   6   s\     "''"EKK0EXbgg&Xe_(=>"6UTioDU666s   %A:3A:subgraphc                     t        |      }t        |       }|D ];  }|j                  D ]*  }t        |t        j
                        s||vs#||k7  s)  y = y)zEnsure nodes in ``subgraph`` satisfy one of the following rules.

    1. The user of the node is in ``subgraph``.
    2. The user of the node is output.
    3. There are no users -- the node is a side-effect node.
    FT)setrB   users
isinstancerF   rG   )r>   rJ   	all_nodesr   r0   users         r#   is_leaf_subgraphrQ   @   sV     "(mIFJJDdBGG,9$	   r"   r;   c                    t        |      }t               }g }| j                  |      5  |D ]  }| j                  |j                  |j
                  |j                  |j                        }t        j                  |j
                  i |j                  }t        |j
                  |j                  f      \  }	}
g }t        ||	      D ]O  \  }}t        |t        j                        r||v r||v sJ |j                  ||          ?|j                  |       Q t!        ||
      \  |_        |_        |||<   |j                  |        	 ddd       |S # 1 sw Y   |S xY w)zClone the given subgraph and insert it before ``target``.

    This API currently does not support inserting after ``target``.
    N)rL   dictinserting_beforer   r;   argskwargstypepytreearg_tree_leavesr   ziprN   rF   rG   appendr   )r>   rJ   r;   rO   mappingcloned_subgraphr0   cloned_nodeoriginal_inputcloned_inputspecmapped_cloned_inputoriginal_input_nodecloned_input_nodes                 r#   clone_subgraphre   R   sU    HI&*fGO				'D--TYYTYYK $33TYYN$++NN!-{/?/?ASAS.T!UL$"$:=;6#%6 2BGG<+y8.'999'..w7J/KL'../@A; 4B#T40Kk0 (GDM"";/3  
(8 9 
(8 s   D"EE gmremove_dead_codec                     | j                   j                          |r| j                   j                          | j                          y)zRun the required steps to ensure production-ready graph.

    Note - per the fx docs, elimination of dead code is not very precise.
    Hence, the flag to make this step optional.
    N)r>   linteliminate_dead_code	recompile)rf   rg   s     r#   rebuild_graphrl   {   s-     HHMMO
$$&LLNr"   graphsfolderc                 F   |st        j                         }| j                         D ]U  \  }}t        t        j
                  j                  || d      d      5 }|j                  t        |             d d d        W t        j                  d|       |S # 1 sw Y   yxY w)Nz.graphwzDump graphs to %s)tempfilemkdtempitemsopenospathjoinwritestrr   warning)rm   rn   prefixrf   fps        r#   dump_graphs_to_filesr}      s    !!#lln
"'',,v&'893?2HHSW @? % NN&/M @?s   BB 	)T)F) )(loggingru   rq   enumr   typingr   r   r   r   r   r	   torch.fxrF   torch.fx.passes.shape_propr
   torch.utilsr   rX   torch.utils._pytreer   r   	getLoggerr   Logger__annotations__ry   r   r%   rG   boolr=   GraphrB   rI   rQ   re   GraphModulerl   r}   r!   r"   r#   <module>r      s{    	   < <  5 ) < +**=9 9 d  sD 277   Bbhh B277 B AF7887 (79=7	"'']7BHH RWW $ $&88&#BGG}&68gg&	"'']&R	bnn 	 	 	
c2>>&9!: 
C 
QT 
r"   