
    l
qi@                        U d dl Z d dlZd dlZd dlmZ d dlmZ d dl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 dd
lmZ  ej        e          Zg dZi aeeef         ed<   dae	e         ed<   dadZi Z eeeeef         f         ed<   dedeeef         ddfdZ! ed           G d de                      Z" ed           G d d                      Z# ed          ed.d                        Z$ ed          de%e         fd            Z& ed          edefd                        Z' ed          defd             Z( ed          d!             Z) ed          d"             Z* ed          de%e         fd#            Z+ ed          de,fd$            Z- ed          ed/d&                        Z. ed          deeef         fd'            Z/ ed          ed(                         Z0 ed          d)             Z1 ed          d*edeeef         fd+            Z2d,edefd-Z3dS )0    N)contextmanager)Enum)AnyOptionalUnion)signpost_event   )compatibility)Graph)GraphModule)Node)annotateannotate_fnpreserve_node_metahas_preserved_node_metaset_stack_traceset_grad_fn_seq_nrreset_grad_fn_seq_nrformat_stackset_current_metaget_current_meta
NodeSourceNodeSourceActionget_graph_provenance_jsonset_current_replay_nodeget_current_replay_nodecurrent_metacurrent_replay_nodeFz<Gradient addition node due to multiple use of tensor around:_FX_METADATA_REGISTRYmodule_namemetadatareturnc                     |t           | <   dS )ap  
    Register FX metadata in the global in-memory registry.

    This is called automatically during graph module compilation to store metadata
    for later use by memory profiler augmentation.

    Args:
        module_name: The module identifier (content-addressed filename)
        metadata: Metadata dict containing lineno_map, node_metadata, and source_code
    N)r   )r    r!   s     r/var/www/html/bestrading.cuttalo.com/services/ml-inference/venv/lib/python3.11/site-packages/torch/fx/traceback.py_register_fx_metadatar%   4   s     *2+&&&    )is_backward_compatiblec                       e Zd ZdZdZdS )r   createreplaceN)__name__
__module____qualname__CREATEREPLACE r&   r$   r   r   C   s        FGGGr&   r   c                      e Zd ZU dZ G d d          Zeed<   ed         ed<   ed          ed<   ed         ed<   ee	ee
f                  ed	<   ee         ed
<   	 	 ddee         dedeeded         f                  fdZedefd            Zedefd            Zedefd            Zd Zd ZddZde	fdZdefdZd Zedee	         ded          fd            ZdS )r   z
    NodeSource is a data structure that contains the provenance information of a node.
    If node `a` is created from node `b`, then `a.meta["from_node"]` may contain NodeSource(b).
    c                   "    e Zd ZdededefdZdS )NodeSource.NodeInfonametargetgraph_idc                 0    || _         || _        || _        d S Nr4   r5   r6   )selfr4   r5   r6   s       r$   __init__zNodeSource.NodeInfo.__init__Q   s    DI DK$DMMMr&   N)r+   r,   r-   strintr;   r0   r&   r$   NodeInfor3   P   s=        	% 	%c 	%S 	% 	% 	% 	% 	% 	%r&   r>   	pass_namer   action	from_node	node_info_dict_action_string Nnodec                    || _         |g }nt          |t                    s|g}|D ]}t          |t                    sJ || _        |rv|                     |j        t          |j                  t          |j
                            | _        d|j        v rt          j        |j        d                   ng | _        nd | _        g | _        d | _        d | _        d S )Nr9   rA   )r?   
isinstancelistr   r@   r>   r4   r<   r5   idgraphrB   metacopydeepcopyrA   rD   rC   )r:   rF   r?   r@   as        r$   r;   zNodeSource.__init__]   s     #>FFFD)) 	XF 	3 	3Aa!1222222 	 !]]Ys4;'7'7"TZ.. +  DN
 $)++ di4555 NN "DNDN .2/3


r&   r"   c                 ,    | j         r| j         j        ndS NrE   )rB   r4   r:   s    r$   r4   zNodeSource.name}   s    &*n<t~"""<r&   c                 ,    | j         r| j         j        ndS rQ   )rB   r5   rR   s    r$   r5   zNodeSource.target   s    (,>t~$$B>r&   c                 ,    | j         r| j         j        ndS )N)rB   r6   rR   s    r$   r6   zNodeSource.graph_id   s    *..@t~&&b@r&   c                 *    |                                  S r8   )print_readablerR   s    r$   __repr__zNodeSource.__repr__   s    ""$$$r&   c                 p    | j         )d                    d | j        D                       | _         | j         S )N+c                 @    g | ]}|j                                         S r0   )r4   lower).0rO   s     r$   
<listcomp>z1NodeSource._get_action_string.<locals>.<listcomp>   s"    +P+P+PqAFLLNN+P+P+Pr&   )rD   joinr@   rR   s    r$   _get_action_stringzNodeSource._get_action_string   s9    &"%((+P+PDK+P+P+P"Q"QD""r&   r   c                     |dk    rdS d}|                                  }|d|z  dz  d| j         d| j         d| d| j         d		z   z  }| j        D ]}||                    |d
z             z  }|S )N	   rE       z(name=z, pass_name=z	, action=z, graph_id=z)
r	   )r`   r4   r?   r6   rA   rW   )r:   indentresultaction_stringitems        r$   rW   zNodeSource.print_readable   s    A::2//11&L1styssdnss}ssaeanssst	
 N 	6 	6Dd))&1*555FFr&   c                     | j         E|                                 }| j        | j        | j        | j        |d | j        D             d| _         | j         J | j         S )Nc                 6    g | ]}|                                 S r0   to_dict)r]   rF   s     r$   r^   z&NodeSource.to_dict.<locals>.<listcomp>   s     HHHdllnnHHHr&   )r4   r5   r6   r?   r@   rA   )rC   r`   r4   r5   r6   r?   rA   )r:   rg   s     r$   rl   zNodeSource.to_dict   sk    : 3355M	+ M!^'HHHHH DJ z%%%zr&   otherc                     t          |t                    sdS |                                 |                                k    S )NF)rH   r   rl   )r:   rm   s     r$   __eq__zNodeSource.__eq__   s4    %,, 	5||~~00r&   c                 b    fdt           |                                                     S )Nc                    t          | t                    r:t          t          fd|                                 D                                 S t          | t
                    rt          fd| D                       S | S )Nc              3   8   K   | ]\  }}| |          fV  d S r8   r0   )r]   kv_make_hashables      r$   	<genexpr>z>NodeSource.__hash__.<locals>._make_hashable.<locals>.<genexpr>   s6      #S#Stq!Qq(9(9$:#S#S#S#S#S#Sr&   c              3   .   K   | ]} |          V  d S r8   r0   )r]   rh   ru   s     r$   rv   z>NodeSource.__hash__.<locals>._make_hashable.<locals>.<genexpr>   s-      BBd^^D11BBBBBBr&   )rH   dicttuplesorteditemsrI   )objru   s    r$   ru   z+NodeSource.__hash__.<locals>._make_hashable   s    #t$$ V#S#S#S#Ssyy{{#S#S#SSSTTTC&& BBBBcBBBBBB
r&   )hashrl   )r:   ru   s    @r$   __hash__zNodeSource.__hash__   s>    	 	 	 	 	 NN4<<>>22333r&   dc                     |dS t          |t                    sJ dt          |                       t                              t                    }d|_        d|_        |                    dd          |_        |                    dd          }g }|r|	                    d          D ]q}|
                                dk    r |                    t          j                   :|
                                dk    r|                    t          j                   r||_        d	|v rhd
|v rdd|v r`t                              |                    d	d          |                    d
d          |                    dd                    }||_        nd|_        |                    dd          ) fd|                    dg           D             |_        ng |_        |S )z
        Recursively deserialize from_node metadata from dictionary data.
        It is used to deserialize the from_node field from serialized metadata.
        Please use constructor NodeSource(node, ...) to create a NodeSource object.
        NzExpected a dict, got r?   rE   r@   rZ   r.   r/   r4   r5   r6   rU   rA   c                 B    g | ]}                     |          xS r8   )
_from_dict)r]   fnclsrf   s     r$   r^   z)NodeSource._from_dict.<locals>.<listcomp>   s:     % % %!nnR000F= ===r&   )rH   rx   typer   __new__rD   rC   getr?   splitupperappendr   r.   r/   r@   r>   rB   rA   )r   r   node_source
action_stractionsaction_namerB   rf   s   `      @r$   r   zNodeSource._from_dict   s    94!T""EE$EDGG$E$EEEE !((44 &*"  !"k2 6 6 UU8R((
 	=)//44 = =$$&&(22NN#3#:;;;; &&((I55NN#3#;<<<$ Q;;8q==Z1__"++fb!!1552#6#6j"8M8M I %.K!!$(K! 55d##/% % % % %%%R00% % %K!! %'K!r&   )rE   N)r   )r+   r,   r-   __doc__r>   r<   __annotations__rI   r   rx   r   r   r   r;   propertyr4   r5   r=   r6   rX   r`   rW   rl   objectro   r~   classmethodr   r0   r&   r$   r   r   I   s0         
% % % % % % % % NNN#$$$$L!!!!
####DcN####SM!!!
 PT	4 4tn4 4 148J3KKLM	4 4 4 4@ =c = = = X= ? ? ? ? X? A# A A A XA% % %# # #
        1F 1 1 1 1
4 4 4 38D> 3h|.D 3 3 3 [3 3 3r&   r   Tc              #   r   K   t           }t                                          }	 | a d V  |a |ad S # |a |aw xY wr8   )should_preserve_node_metar   rM   )enablesaved_should_preserve_node_metasaved_current_metas      r$   r   r      s_      
 '@#%**,,*$*!$C!) %D!)))))s   0 6stackc                 Z    t           r!| r!d                    |           t          d<   d S d S d S )NrE   stack_trace)r   r_   r   )r   s    r$   r   r     sA     ! 5U 5&(ggenn]###5 5 5 5r&   annotation_dictc              #   8  K   dt           v }t          j        t                               di                     }	 |s
i t           d<   t           d                             |            dV  |r|t           d<   dS t           d= dS # |r|t           d<   nt           d= w xY w)a  
    Temporarily adds custom annotations to the current tracing context.
    The fx_node produced from this tracing context will have the
    custom annotations in node.metadata["custom"] field.

    This context manager allows you to insert arbitrary metadata into the PT2
    tracing system by updating the global `current_meta["custom"]` dictionary.
    The annotations are automatically reverted after the context exits.

    Gradient accumulation nodes will not be annotated.

    This is intended for advanced users who need to attach additional metadata to the fx nodes
    (e.g., for debugging, analysis, or external tooling) during export tracing.

    Note:
        This API is **not backward compatible** and may evolve in future releases.

    Note:
        This API is not compatible with fx.symbolic_trace or jit.trace. It's intended
        to be used with PT2 family of tracers, e.g. torch.export and dynamo.

    Args:
        annotation_dict (dict): A dictionary of custom key-value pairs to inject
            into the FX trace metadata.

    Example:
        After exiting the context, custom annotations are removed.

        >>> with annotate({"source": "custom_pass", "tag": 42}):
        ...     pass  # Your computation here
    customN)r   rM   r   update)r   
has_custom
old_customs      r$   r   r     s      J \)J<++Hb99::J' 	(%'L" 	X%%o666 	'%/L"""X&&&	  	'%/L""X&&&&&s   0B Bc                 "     ddl m  fd}|S )a&  
    A decorator that wraps a function with the annotate context manager.
    Use this when you want to annotate an entire function instead of a specific code block.

    Note:
        This API is **not backward compatible** and may evolve in future releases.

    Note:
        This API is not compatible with fx.symbolic_trace or jit.trace. It's intended
        to be used with PT2 family of tracers, e.g. torch.export and dynamo.

    Args:
        annotation_dict (dict): A dictionary of custom key-value pairs to inject
            into the FX trace metadata for all operations in the function.

    Example:
        All operations in my_function will have {"pp_stage": 1} in their metadata.

        >>> @annotate_fn({"pp_stage": 1})
        ... def my_function(x):
        ...     return x + 1
    r   )wrapsc                 8                  fd            }|S )Nc                  f    t                    5   | i |cd d d            S # 1 swxY w Y   d S r8   )r   )argskwargsr   funcs     r$   wrapperz/annotate_fn.<locals>.decorator.<locals>.wrappera  s    /** - -tT,V,,- - - - - - - - - - - - - - - - - -s   &**r0   )r   r   r   r   s   ` r$   	decoratorzannotate_fn.<locals>.decorator`  s;    	t	- 	- 	- 	- 	- 
	- r&   )	functoolsr   )r   r   r   s   ` @r$   r   r   F  s>    0        r&   c                     t           rOt                              dg           | gz   t          d<   t                              dd          dz   t          d<   d S d S )Ngrad_fn_seq_nr
in_grad_fnr   r	   r   r   r   )seq_nrs    r$   r   r   k  sj     ! K)5)9)9:JB)O)OS
 *
%& &2%5%5lA%F%F%J\"""K Kr&   c                      t           rgt                              dd          } | dk    sJ | dk    rt          d= t          d= d S | dz
  t          d<   t          d         d d         t          d<   d S d S )Nr   r   r	   r   rU   r   )current_levels    r$   r   r   w  s    
 ! Q$((q99q    A\*-...)6):L&-9:J-KCRC-PL)***Q Qr&   c                      t           rt                              dd          gS t          j        t          j                    d d                   S )Nr   rE   rU   )r   r   r   	tracebackformat_listextract_stackr0   r&   r$   r   r     sI      E  3344 $Y%<%>%>ss%CDDDr&   c                      t           S r8   )r   r0   r&   r$   r   r     s    $$r&   rE   c              #      K   t           r[| j        rTt          }	 | j                                        at	          | |t
          j                  gt          d<   d V  |ad S # |aw xY wd V  d S )NrA   )r   rL   r   rM   r   r   r.   )rF   r?   
saved_metas      r$   r   r     s       ! TY !
	&9>>++L 4,<,CDD)L% EEE%LLL:L%%%%s   AA A"c                      t           S r8   )r   r0   r&   r$   r   r     s    r&   c              #   8   K   t           }	 | a dV  |a dS # |a w xY w)z
    Set the currently replay node. If `current_replay_node` is not None,
    then we're re-generating the `current_replay_node` in FunctionalTensorMode.
    Nr   )rF   saved_current_replay_nodes     r$   r   r     sA       !48"777777s    c                      t           S )z'
    Get the currently replay node
    r   r0   r&   r$   r   r     s
    
 r&   rK   c           	      "   	 i }| j         D ]7}|j        dk    r*d|j        v rd |j        d         D             ng ||j        <   8|S # t          $ r?}t          dddt          |          t          j                    d           i cY d}~S d}~ww xY w)	za
    Given an fx.Graph, return a json that contains the provenance information of each node.
    call_functionrA   c                 6    g | ]}|                                 S r0   rk   )r]   sources     r$   r^   z-get_graph_provenance_json.<locals>.<listcomp>  s"    KKK&V^^%%KKKr&   inductorprovenance_tracking_errorr   )function	error_msgr   N)	nodesoprL   r4   	Exceptionr   r<   r   
format_exc)rK   provenance_tracking_jsonrF   es       r$   r   r     s    
#% K 	 	Dw/)) #di// LKDIk4JKKKK )3
 ('    	'7 VV(355 	
 	
 	
 						s   AA 
B4B	B	Bgmc                     t          | t                    sJ dt          ffdd                    d  |           D                       S )Nr   c           	         g }| j         j        D ]}t          |d          rH|j                            dd           r-|                    |j        |j        |j        d         f           |j        dk    rYt          t          | |j
                  t                    r1|                     t          | |j
                                       |S )NrL   r   get_attr)rK   r   hasattrrL   r   r   r   r4   rH   getattrr5   r   )r   custom_metadatarF   helpers      r$   r   z$_get_custom_metadata.<locals>.helper  s    HN 	I 	IDtV$$ Rx)F)F R&&DIh<O'PQQQw*$$DK((+* *$  &&vvgb$+.F.F'G'GHHHr&   
c              3   4   K   | ]}t          |          V  d S r8   )r<   )r]   xs     r$   rv   z'_get_custom_metadata.<locals>.<genexpr>  s(      00SVV000000r&   )rH   r   r_   )r   r   s    @r$   _get_custom_metadatar     si    b+&&&&&	; 	 	 	 	 	 	 9900VVBZZ000000r&   )T)rE   )4rM   loggingr   
contextlibr   enumr   typingr   r   r   torch._utils_internalr   _compatibilityr
   rK   r   graph_moduler   rF   r   	getLoggerr+   log__all__r   rx   r<   r   r   r   GRADIENT_ACC_SPECIAL_STACKr   r%   r   r   r   rI   r   r   r   r   r   r   boolr   r   r   r   r   r   r   r0   r&   r$   <module>r      s         % % % % % %       ' ' ' ' ' ' ' ' ' ' 0 0 0 0 0 0 ) ) ) ) ) )       % % % % % %       g!!  $  "d38n ! ! !&* Xd^ * * *!  C  46 tCc3h/0 5 5 52s 2d38n 2 2 2 2 2 e,,,    t   -,
 e,,,j j j j j j j -,jZ e,,,* * *  -,* e,,,549 5 5 5 -,5 e,,,2'd 2' 2' 2'  -,2'j e,,,! ! ! ! -,!H e,,,K K -,K e,,,Q Q -,Q e,,,Ed3i E E E -,E e,,,% % % % -,% e,,,    -,* e,,,$sCx.    -, e,,,8 8  -,8 e,,,  -, e,,,U tCH~    -,:1[ 1S 1 1 1 1 1 1r&   