o
    Ih3                    @   sJ  U d dl Z d dlZd dlZd dlZd dlZd dlZd dlZd dlZd dlZd dl	m
Z
 d dlmZmZ d dlmZmZmZmZ d dlZd dlZd dlZd dlmZ d dlm  mZ d dlmZ d dlmZ d dl m!Z!m"Z" d dl#m$Z$m%Z% d d	l&m'Z'm(Z(m)Z)m*Z* d d
l+m,Z, d dl-m.Z. d dl/m0Z0 ddl1m2Z2 ddl3m4Z4 ddl5m6Z6m7Z7m8Z8 ddl9m:Z: ddl;m<Z< ddl=m>Z>m?Z? ddl@mAZAmBZB erd dlCZCe2jDZEeFeGd< eHeIZJejKeGd< ejLjMZMejLjNZNeG dd dZOeG dd dZPeG dd dZQdejRdeFfdd ZSd!ejTdeFfd"d#ZUd!ejTdeFfd$d%ZVdejRdeWfd&d'ZXG d(d) d)ZYeY ZZ	dd*ej[d+e\ejR d,e\ejR d-ee] dej[f
d.d/Z^dejRdeFfd0d1Z_dejRdeFfd2d3Z`dejRdeFfd4d5ZadejRdeFfd6d7ZbdejRdeFfd8d9ZcdejRdeFfd:d;ZddejRdeFfd<d=ZedejRdeFfd>d?Zfd@ejTdege\ejR e\ejR f fdAdBZhdCe\ejR dDe]fdEdFZid@ejTdCe\ejR dGe\ejR dHeWdegejTejTf f
dIdJZjd@ejTdegejTejTf fdKdLZkeWdMZldNeWdeWfdOdPZmdejRdeWfdQdRZndSej[fdTdUZoepddVdW ZqdXerejReWf de\egejReWf  fdYdZZsd[ejTdejTfd\d]Ztd^ejjTd_ejjTd`ejjRdaejjRdbejudceWddejjRdeejjRfdfdgZvd@ejTd^ejTd_ejTdheWdegejTejTf f
didjZwd@ejTdejTfdkdlZx	dd*ej[dmePdneQdoee.ejR  fdpdqZydrds ZzdeOfdtduZ{dSej[fdvdwZ|d*ej[dxe\e} dye\e} dze}dmePd{e\ejR dege}e\eW e\eW f fd|d}Z~d d~lmZ dejdeWdejfddZdd Z	dd*ej[dmePde\ejR fddZ	dd@ejTdegejTejTf fddZ					ddejjTde]de]deFdeee]e\e] f  deFdee] ddfddZdS )    Ndefaultdict)	dataclassreplace)CallableOptionalTYPE_CHECKINGUnion)(create_structured_trace_for_min_cut_info)BackwardState)is_sym_nodepy_sym_types)magic_methodsmethod_to_operator)find_symbol_binding_fx_nodesfree_symbolshint_intis_symbol_binding_fx_node)graph_drawer)
OrderedSet)CheckpointPolicy   )config)GraphInfoProvider)dp_knapsackgreedy_knapsackilp_knapsack)KnapsackEvaluator)get_aot_graph_name)get_cuda_generator_meta_valis_with_effects)fx_graph_cseget_aten_targetAOT_PARTITIONER_DEBUGlogc                   @   s   e Zd ZU dZee ed< ee ed< ee ed< ee ed< ee ed< dejfdd	Z	dejfd
dZ
dejfddZdejfddZdejfddZdS )OpTypesz8Class for keeping track of different operator categoriesfusible_opscompute_intensive_ops
random_opsview_opsrecomputable_opsnodec                 C      t || jv S N)r"   r&   selfr+    r0   Q/var/www/vscode/kcb/lib/python3.10/site-packages/torch/_functorch/partitioners.py
is_fusibleF      zOpTypes.is_fusiblec                 C   r,   r-   )r"   r'   r.   r0   r0   r1   is_compute_intensiveI   r3   zOpTypes.is_compute_intensivec                 C   r,   r-   )r"   r(   r.   r0   r0   r1   	is_randomL   r3   zOpTypes.is_randomc                 C   r,   r-   )r"   r)   r.   r0   r0   r1   is_viewO   r3   zOpTypes.is_viewc                 C   r,   r-   )r"   r*   r.   r0   r0   r1   is_recomputableR   r3   zOpTypes.is_recomputableN)__name__
__module____qualname____doc__r   r   __annotations__fxNoder2   r4   r5   r6   r7   r0   r0   r0   r1   r%   <   s   
 r%   c                   @   s   e Zd ZU eej ed< eej ed< eej ed< eej ed< eeje	f ed< e
jdeej fddZd	ejdefd
dZd	ejdefddZd	ejdefddZd	ejde	fddZdS )NodeInfoinputs_required_fw_nodesrequired_bw_nodesunclaimed_nodesfw_orderreturnc                    s    t dd  jD  fdddS )Nc                 s   s    | ]}|V  qd S r-   r0   .0nr0   r0   r1   	<genexpr>c   s    z-NodeInfo.required_fw_nodes.<locals>.<genexpr>c                    s
    j |  S r-   )rD   rH   r/   r0   r1   <lambda>c      
 z,NodeInfo.required_fw_nodes.<locals>.<lambda>key)sortedrA   rK   r0   rK   r1   required_fw_nodes`   s   zNodeInfo.required_fw_nodesrH   c                 C   
   || j v S r-   )rA   r/   rH   r0   r0   r1   is_required_fwf      
zNodeInfo.is_required_fwc                 C   rR   r-   )rB   rS   r0   r0   r1   is_required_bwi   rU   zNodeInfo.is_required_bwc                 C   rR   r-   )rC   rS   r0   r0   r1   is_unclaimedl   rU   zNodeInfo.is_unclaimedc                 C   s$   || j v sJ d| d| j| S )NNode z not in fw nodes!)rA   rD   rS   r0   r0   r1   get_fw_ordero   s   
zNodeInfo.get_fw_orderN)r8   r9   r:   listr=   r>   r<   r   dictint	functoolscached_propertyrQ   boolrT   rV   rW   rY   r0   r0   r0   r1   r?   V   s   
 r?   c                   @   s6   e Zd ZU eed< eed< eed< eed< eed< dS )MinCutOptionsban_if_used_far_apartban_if_long_fusible_chainsban_if_materialized_backwardban_if_not_in_allowlistban_if_reductionN)r8   r9   r:   r_   r<   r0   r0   r0   r1   r`   t   s   
 r`   r+   rE   c                 C   s   | j dd tjtjfv S )N	recompute)metagetr   MUST_RECOMPUTEPREFER_RECOMPUTEr+   r0   r0   r1   must_recompute}   s   rl   fx_gc                 C   s    | j jD ]	}t|r dS qdS )NTF)graphnodesrl   rm   r+   r0   r0   r1   has_recomputable_ops   s
   rq   c                 C   s<   | j jD ]}t|rt|jdrtjj|jjv r dS qdS )NtagsTF)	rn   ro   rl   hasattrtargettorchTagnondeterministic_seededrr   rp   r0   r0   r1   has_recomputable_rng_ops   s   
rx   c                 C   s6   t | jd tjtjfrdS t | jd tjsJ dS )Nvalr      )
isinstancerg   ru   SymIntSymBoolSymFloatrk   r0   r0   r1   sym_node_size   s   r   c                   @   s   e Zd Zdd ZdS )InvalidNodeBasec                 C   s   dS )NzInvalid Noder0   rK   r0   r0   r1   __repr__   s   zInvalidNodeBase.__repr__N)r8   r9   r:   r   r0   r0   r0   r1   r      s    r   joint_graphr@   outputssubgraphc           
         s  t  }i  |D ]}||j}|j|_| |< q| jD ]d}t|r+|dkr+t |< q| v r0q|jdkr:t |< q|jdkrht	j
|ji |j} fdd|D }t|r[t |< q|| fdd |< q|jdkrz|| fd	d |< q|jd
kr	 qg }|D ]0}	t|	t jr|	 vrtd|	 dt |	 trJ d|	 d| |	  q||	 q|t| |  |  |S )a  
    Given a graph, extracts out a subgraph that takes the specified nodes as
    inputs and returns the specified outputs.

    This includes specifying non-placeholder nodes as inputs.

    The general strategy is to initialize all inputs with proxies as we
    encounter them, and trace through the graph, only keeping values which take
    in valid proxies. Then, all dead code is eliminated.
    backwardplaceholdercall_functionc                    s&   g | ]}t |tjrt  | tqS r0   )r{   r=   r>   r   )rG   xenvr0   r1   
<listcomp>   s    
z6_extract_graph_with_inputs_outputs.<locals>.<listcomp>c                        |  S r-   r0   r   r   r0   r1   rL          z4_extract_graph_with_inputs_outputs.<locals>.<lambda>get_attrc                    r   r-   r0   r   r   r0   r1   rL      r   outputrX   z couldn't be found in envz was invalid, but is output)r=   Graphr   namerg   ro   _must_be_in_backwardInvalidNodeoppytreearg_tree_leavesargskwargsany	node_copyr{   r>   RuntimeErrorr   appendr   tupleeliminate_dead_codelint)
r   r@   r   r   	new_graphr+   new_nodeall_argsoutput_valuesr   r0   r   r1   "_extract_graph_with_inputs_outputs   sX   








r   c                 C   s,   | j dkodt| jvot|  ot|  S Nr   tangents)r   strrt   _is_bwd_seed_offset_is_fwd_seed_offsetrk   r0   r0   r1   
_is_primal   s   
r   c                 C   s   | j dkodt| jv S r   r   r   rt   rk   r0   r0   r1   _is_tangent   s   r   c                 C   &   | j dkodt| jv pdt| jv S )Nr   bwd_seedbwd_base_offsetr   rk   r0   r0   r1   r         
r   c                 C   r   )Nr   fwd_seedfwd_base_offsetr   rk   r0   r0   r1   r      r   r   c                 C   s   | j dkot| jdtS )Nr   ry   )r   r{   rg   rh   r   rk   r0   r0   r1   _is_backward_state  s   r   c                 C      | j dd dkS )Npartitioner_tagis_backwardrg   rh   rk   r0   r0   r1   _has_tag_is_backward	     r   c                 C   r   )Nr   must_be_in_backwardr   rk   r0   r0   r1   _has_tag_must_be_in_backward  r   r   c                 C   s   t | pt| ot| S r-   )r   r   r    rk   r0   r0   r1   r     s   r   joint_modulec                C   s>   t jdd | jjddD  }|d | }||d  }||fS )Nc                 s   s    | ]}|j V  qd S r-   )r   rG   r+   r0   r0   r1   rI     s    z+_extract_fwd_bwd_outputs.<locals>.<genexpr>r   r   )r   r   rn   
find_nodes)r   num_fwd_outputsr   fwd_outputsbwd_outputsr0   r0   r1   _extract_fwd_bwd_outputs  s   r   saved_valuesr   c                 C   s(   | D ]}|j |kr| |  d S qd S r-   )r   remove)r   r   saved_valuer0   r0   r1   _remove_by_name"  s   

r   saved_sym_nodesr   c                C   s  t | |d\}}| jjdd}g tt|}g tt|}g tt|}	g tt|}
g tt|}t	| j|| | |
 |d}|jddD ] }|j
sXt||j t||j qFt|rft||j |sfJ qFt }g }g }|D ]}t|}|r|| || qp|| qpt| j}t|||D ],}d|jvrqt|jd | }t|dd dD ]}||vrq|||  q||O }q|  |||  t	| j||	 || | d	}t	| j|| | |
 | |d}tj| |}tj| |}||fS )
Nr   r   r   r   ry   c                 S      | j S r-   r   )sr0   r0   r1   rL   e      z*_extract_fwd_bwd_modules.<locals>.<lambda>rN   forward)r   rn   r   filterr   r   r   r   r   r   usersr   r   r   r   addr   r   	itertoolschainrg   r   rP   clearextendr=   _lazy_graph_module_make_graph_module)r   r   r   r   r   r   placeholdersprimal_inputstangent_inputsfwd_seed_offset_inputsbwd_seed_offset_inputsbackward_state_inputs	bwd_graphr+   saved_symbolssaved_sym_nodes_bindingsaved_sym_nodes_derivedsymbolsymbol_bindingsnew_symbolsr   	fwd_graph
fwd_module
bwd_moduler0   r0   r1   _extract_fwd_bwd_modules)  s   





r   c                   sb  t | rt| ||dS ttt| jj}ttt| jj}|| }t| |d\}}t	| j||d}t
dd |jD  g }	g }
| jjD ]S}|j vrKqCt|rU|
| qCd|jvrs|jdkrs|j}tdd |D smJ |	| qC fdd	|jD }d|jv rtd
d |D r|
| qC|	| qCtt|	 }	tt|
 }
t| |	|
|dS )a  
    Partitions the :attr:`joint_module` in a manner that closely resembles the
    behavior observed in the original ``.forward()`` and ``.backward()`` of the
    callable, i.e., the resulting forward graph contains those operators that
    are executed in the original ``.forward()`` callable passed to
    :func:`aot_function`.

    The default partitioner collects the operators that are between the forward
    inputs and the forward outputs. This helps in finding the tensors which have
    to be stashed for the backward pass. These stashed tensors become the output
    of the generated forward graph. The remaining operators are then placed in
    the backward graph.

    .. warning::
        This API is experimental and likely to change.

    Args:
        joint_module(fx.GraphModule): The joint forward and backward graph. This
            is the result of AOT Autograd tracing.

    Returns:
        Returns the generated forward and backward Fx graph modules.
    r   r   c                 s   s     | ]}|j d kr|jV  qdS r   Nr   r   r   r0   r0   r1   rI         z$default_partition.<locals>.<genexpr>tensor_metar   c                 s   s    | ]	}|j tjkV  qd S r-   )rt   operatorgetitemrG   userr0   r0   r1   rI         c                    s   g | ]	}|j  vr|qS r0   r   rF   forward_node_namesr0   r1   r     s    z%default_partition.<locals>.<listcomp>c                 s       | ]}t |V  qd S r-   r   rF   r0   r0   r1   rI         
r   r   )rq   #min_cut_rematerialization_partitionrZ   r   r   rn   ro   r   r   r   r   r   r   r   rg   r   r   allr   r[   fromkeyskeysr   )r   _joint_inputsr   r   r   r@   r   r   forward_only_graphr   r   r+   r   backward_usagesr0   r   r1   default_partition  sV   



r   g    .Anumelc                 C   s
   | |j  S r-   )itemsize)r  dtyper0   r0   r1   _tensor_nbytes  rU   r  c                    s   dt fdd d| jv rR| jd }t|trdS t|ttfr*t fdd|D S t|tr<t fdd| D S t|t	j
rF |S td	t| d
|  | jdks`| jt	jjjju rbdS td|  d)NrE   c                 S   s(   t | tjsdS tt|  dd| jS )Nr      fallback)r{   ru   Tensorr  r   r  r  r   r0   r0   r1   object_nbytes  s   z_size_of.<locals>.object_nbytesry   r   c                 3       | ]} |V  qd S r-   r0   rF   r	  r0   r1   rI         z_size_of.<locals>.<genexpr>c                 3   s    | ]	\}} |V  qd S r-   r0   )rG   _rH   r  r0   r1   rI     r   zUnknown metadata type z	 on node r   r   rX   zO didn't have `val` metadata; we should always have `val` metadata on the nodes.)r\   rg   r{   r   rZ   r   sumr[   itemsru   r  r   typer   rt   opsaten_assert_scalardefault)r+   ry   r0   r  r1   _size_of  s"   




r  rn   c                 C   s`   ddl m} |t}| jD ]}|jdkr||jj  d7  < qtdt	|
 dd dd	 d S )
Nr   r   r   r   z%sc                 S      | d S Nr   r0   r   r0   r0   r1   rL     r   z_count_ops.<locals>.<lambda>TrO   reverse)collectionsr   r\   ro   r   rt   r8   r$   inforP   r  )rn   r   cntr+   r0   r0   r1   
_count_ops  s   

"r  c                  C   sl   g } t tjjD ]+}ttjj|}t|tjjsq| D ]}t||}tj	j
|jv r2| |  nqq| S r-   )dirru   r  r  getattrr{   _opsOpOverloadPacket	overloadsrv   	pointwiserr   r   )r  	attr_nameopoverloadpacketoverloadop_overloadr0   r0   r1   pointwise_ops  s   

r(  	depth_mapc                    s(    fdd| D }t | dd ddS )Nc                    s&   i | ]}t |tjjjr| | qS r0   )r{   ru   r=   r+   r>   )rG   argr)  r0   r1   
<dictcomp>#  s
    zsort_depths.<locals>.<dictcomp>c                 S   r  r  r0   r   r0   r0   r1   rL   &  r   zsort_depths.<locals>.<lambda>Tr  )rP   r  )r   r)  
arg_depthsr0   r+  r1   sort_depths"  s   
r.  gmc           
         s   t  i  | jjddD ]}| fdd |< qi t| jjD ]\}}||< q$ fdd}ttt	| jj}d}t
j}|D ]}|jD ]}| |k rX| }|}qJqE|du r`| S t| jj| d D ]}|| qltj | }	|	S )a  
    This pass finds the first bwd node in the graph (by looking at users of
    tangents) and then reorders the graph by walking from this node to all the
    way to the end of the graph. At each op in this traveral, we insert this op
    in a new graph and try to bring only the relevant subgraph from the other
    non-bwd edges relevant for this op. This closely mimics the behavior of
    autograd engine.

    Why is this pass required in the first place?

    This is an artifact of how partitioners work today. The starting point of
    partitioner is a joint graph, which is fwd and then bwd graph. In the case
    of checkpointing, we keep portions of fwd graph in their original place in
    the joint graph, while obtaining a bwd graph. As a result, the resulting bwd
    graph has copies of recomputed fwd subgraphs followed by the original bwd
    graph. If we run this naively, this leads to bad memory footprint, because
    the fwd subgraphs are live for way longer duration than necessary. This pass
    reorders the operations such that we prioritize the ops for the original bwd
    graph while only realizing those ops from the fwd graph that are necessary
    at any given point in the graph.
    r   r   c                    r   r-   r0   r   r   r0   r1   rL   E  r   z5reordering_to_mimic_autograd_engine.<locals>.<lambda>c                    s   | g}t  }t|dkr)| } | |v s|  v rq||  || j7 }t|dkst|fddd}|D ]} |  fdd | < q5d S )Nr   c                    r   r-   r0   rJ   )orderr0   r1   rL   X  r   zSreordering_to_mimic_autograd_engine.<locals>.insert_node_in_graph.<locals>.<lambda>rN   c                    r   r-   r0   r   r   r0   r1   rL   Z  r   )r   lenpopr   all_input_nodesrP   r   )r+   	cur_nodesinsertable_nodesr   r   r0  r0   r1   insert_node_in_graphK  s   


zAreordering_to_mimic_autograd_engine.<locals>.insert_node_in_graphN)r=   r   rn   r   r   	enumeratero   rZ   r   r   mathinfr   ru   GraphModule)
r/  r+   idxr7  r   first_node_in_bwdminimum_ordertangentr   new_gmr0   r6  r1   #reordering_to_mimic_autograd_engine)  s0   


rA  	fw_module	bw_modulefw_nodebw_nodedevice	rng_countlast_fwd_inputlast_bwd_inputc                 C   s  |j }|dus	J | j}	|j}
tjjj}| j| | jd| }t||j	d< |}W d   n1 s7w   Y  |j| |jd| }t||j	d< |}W d   n1 s_w   Y  t
|j}||d< | j| |	jd||jg|jR |d}W d   n1 sw   Y  || |	| t
|j}||d< |
|$ |
jd||jg|jR |d}|| |
| W d   ||fS 1 sw   Y  ||fS )a%  
    Note [CUDA Graph Safe RNG Functionalization]

    CUDA Graph capture doesn't work with get_rng_state and set_rng_state because these functions operate on CPU values,
    while CUDA Graph RNG capture uses on-device CUDA tensors. To solve this, we use graphsafe_set_state with a
    CUDA Generator registered to the CUDA Graph before capture begins. graphsafe_set_state updates the generator's pointer
    to reference a different GeneratorImpl, ensuring subsequent calls are correctly forwarded to the desired generator
    (and its cuda-tensor RNG state during graph capture).

    For each RNG operation's forward/backward pair:

    - We create two generators initialized with identical values
    - Each forward and backward call advances its respective generator equally
    - This keeps generators synchronized so forward and backward operations use matching RNG values

    When forward is called multiple times before backward (causing desynchronization):

    - We save the forward RNG state
    - We update the backward Generator's state before executing backward

    Before each CUDA Graph replay, replay_prologue updates captured RNG pointers with current states, ensuring backward Generator
    changes are reflected during replay.

    This function modifies both forward and backward computation graphs by:

    Creating RNG state placeholders for both passes
    Updating the forward node to use graph-safe RNG state
    Updating the backward node to use graph-safe RNG state

    For more details: https://github.com/pytorch/pytorch/issues/113541
    Nfwd_rng_state_ry   bwd_rng_state_	rng_stater   r   r   )indexrn   ru   _prims	rng_primsgraphsafe_run_with_rng_stateinserting_afterr   r   rg   r[   r   create_nodert   r   replace_all_uses_with
erase_nodeinserting_before)rB  rC  rD  rE  rF  rG  rH  rI  
device_idxfw_graphbw_graphrQ  fwd_rng_statebwd_rng_state	fw_kwargsfunctional_fw_node
bwd_kwargs
rng_outputr0   r0   r1   %apply_graphsafe_rng_functionalizations  sT   )








r`  num_sym_nodesc           '   
      s  t  }dd }dttj fdd dttj fdd}|| }||}||}	i }
| jjD ]*}t|rXt|j	d	rXtj
j|j	jv rX||j }||j }|	|j }||d
|
|< q.tjjj}tjjj}d }|jjddD ]}d|jv rw|} nql|d u rtdg }tt|jjdd}tt|jjdd}t fdd|
 D }|td t|dk}tjj}tjo| o|j p|jj}t |
! D ]\}\}}|d }|d } |}|j}|j}|r|d ur|j"dkrt#||||||||\}}q|$|? |j%d||j	g|j&R |j'd}|j%dt(j)|dfi d}|j%dt(j)|dfi d} |*|  |+| |,| W d    n	1 sDw   Y  |$| dt| }!|-|!}"|||"j.d< W d    n	1 smw   Y  |$|# |j%d||"|j	g|j&R |j'd} |*|  |+| W d    n	1 sw   Y  q|rtt/|jjdd}#|#j&d }$t|$| }%|$d |% t0| |$|%d   }&|j1|& |j+|# |2  |2  ||fS )Nc                 S   sF   i }| j jD ]}|jdkr t|jdr tjj|jjv r |||j	< q|S )Nr   rr   )
rn   ro   r   rs   rt   ru   rv   rw   rr   r   )gmodrandom_nodesr+   r0   r0   r1   get_rng_ops  s   


z*functionalize_rng_ops.<locals>.get_rng_opsrE   c                 S   s^   d| j vrdS | j d }t|ts|f}|D ]}t|tjr)|jjdkr)|j  S qtdS )zV
        Check the example value of the node outputs to find the device type.
        ry   Ncudacpu)rg   r{   r   ru   r  rF  r  )r+   
candidates	candidater0   r0   r1   
get_device  s   




z)functionalize_rng_ops.<locals>.get_devicerF  c                 S   s$   | d ur| j dkrtj S t S )Nre  )r  ru   re  get_rng_state)rF  r0   r0   r1   get_sample_rng_state  s   
z3functionalize_rng_ops.<locals>.get_sample_rng_staterr   )fwdbwdr   r   r?  zaCouldn't find tangent node in graph inputs. This is unexpected, please file a bug if you see thisc                 3   s    | ]	} |d  V  qdS )rl  Nr0   )rG   	node_pairri  r0   r1   rI   /  s    
z(functionalize_rng_ops.<locals>.<genexpr>rf  r   rl  rm  re  r   rM  r   rng_state_output_ry   r   )3r   countr   ru   rF  rn   ro   rl   rs   rt   rv   rw   rr   r   rO  rP  run_and_save_rng_staterun_with_rng_stater   r   nextreversedr   valuesdiscardr1  	_inductorr   graphsafe_rng_functionalizationfallback_randomtest_configs*graphsafe_rng_func_ignores_fallback_randomr8  r  r  r`  rV  rS  r   r   r   r   rT  rU  r   r   rg   iterr   r   	recompile)'r   rB  rC  ra  uidrd  rk  joint_graph_rng_opsfw_graph_rng_opsbw_graph_rng_opsrecomputable_rng_ops_mapr+   	base_noderD  rE  run_and_save_rngrs  bw_tangent_start_nodefw_rng_state_outputsrH  rI  devicesmulti_cuda_devices
ind_config'use_rng_graphsafe_rng_functionalizationrG  rn  rF  rX  rY  r]  stater_  
state_namebw_rng_state_nodefw_output_node
fw_outputssym_node_start_idxr   r0   ro  r1   functionalize_rng_ops  s   






	


	





r  c                 C   s|   | j jD ]7}t|r;|jD ]}t|r#|jd |jd kr#tj|jd< q|jddr;tdd |jD s;tj|jd< q| S )a  
    If there are two consecutive checkpointed blocks with no operator in
    between, we would still want to stash the tensor at the boundary of
    checkpointed blocks. The following pass makes the last output node
    non-recomputable to allow for that.
    ac_graph_idrf   has_backward_hookFc                 s   r   r-   )rl   r   r0   r0   r1   rI     r   z)cleanup_recompute_tags.<locals>.<genexpr>)	rn   ro   rl   r   rg   r   	MUST_SAVErh   r   )r   r+   r   r0   r0   r1   cleanup_recompute_tags  s   
r  	node_infomin_cut_optionsdont_banc           $         s  d u rt  t tr(t dd | jD }|t dd jD  }td| dd dd fd	d
zdd l}W n tyO } zt	d|d }~ww 
fddfdd}fdddt
ffdd}	| t    fdd}
| jD ]}|jdkrq|
jv r|
jvrj|jd dtjd qj|jd dtjd t|rj|jd dtjd qt|st|r|
| 
|r||r|
| d|jvod|jvpd|jv ot|jd tj }t|rt
t|}n|rt|jdtrdntj}n|	|}j|jd |jd |d |jD ]}j|jd |jd tjd q$qd t t!j" d!t#dt#f
fd"d#}j$r
j%D ]]}
fd$d%|jD }
fd&d%|jD }t&|dkr||t'|}t(|jD ]2}
|r
)||kr||r| v rqytd'|
)|||
)| |
| qyqPj*r:t  }| jD ]}
|sq
)||fg}
)|}t&|dkr8t+,|\}}||v rq|-| 
)||d( krt&|dkrtd)||
)|
)| |
| n*|jD ]}
|r/||r/| vr/t+.|
)||f qt&|dks֐qz|/d*d\}}W n t0yd   td+ td,1|j2j34 t5  w |\}t  }fd-d|D D ]\}|6fd.d|D  qut  }|D ]\} }!| d d/ |!d d0 ksJ | d d/ }"|-|" qt7| d1d2 t8| jD 	t9fd3d|D 	fd4d5d6}#|# fS )7Nc                 s   s2    | ]}|j d krt|jdrt|jjV  qdS )r   _overloadpacketN)r   rs   rt   r   r  r   r0   r0   r1   rI     s    
z solve_min_cut.<locals>.<genexpr>c                 s   r   r-   )r   rG   ir0   r0   r1   rI     r   z&Ops banned from re-materialization: %sc                 S   sn   |j tjjjkr
dS |jd }tjj|\}}|D ]}|j	| }| |u r( dS t
|tr4| |v r4 dS qdS NFr   T)rt   ru   r  higher_orderauto_functionalizedr   _higher_order_opsauto_functionalizeget_mutable_argsr   r{   rZ   )ab
mutable_opmutable_arg_namesr  r   r*  r0   r0   r1   !can_fuse_into_auto_functionalized  s    


z8solve_min_cut.<locals>.can_fuse_into_auto_functionalizedc                 S   sH   |j tjjjkr
dS |jd }|D ]}|jd | }| |u r! dS qdS )NFtensors_to_cloner   T)rt   ru   r  r   triton_kernel_wrapper_functionalr   )r  r  r  r   r*  r0   r0   r1   .can_fuse_into_triton_kernel_wrapper_functional  s   
zEsolve_min_cut.<locals>.can_fuse_into_triton_kernel_wrapper_functionalc                    sh   t |tjkr	dS  | |rdS | |rdS | jtju r*| jd jtjj	j
u r*dS | o3|S )NTr   F)r"   r  catrt   r   r   r   ru   r  r  r  r2   )r  r  )r  r  op_typesr0   r1   r2     s   


z!solve_min_cut.<locals>.is_fusibler   zANeed networkx installed to perform smart recomputation heuristicsc                    sv    | rdS t| g}t|dkr9| }|jD ]}|s( ||s( dS  |r2|| qt|dksdS r  )r6   r   r1  r2  r   rT   r   )r+   r4  curr   )r2   r  r  r0   r1   is_materialized_backwards  s   




z0solve_min_cut.<locals>.is_materialized_backwardsc                    s  | j dkrdS | jtjkrdS | jdd tjkrdS tj	r%
| r%dS | jtjjtjjfv r2dS jr=| s<dS n| sG| rIdS jr\ | r\td| t| j dS | jdk ri| jtjkridS jrtdd | jD }t| }|d	 |k S dS )
Nr   Frf   Tzmaterialized backwards: %s %si  c                 s   s$    | ]}t |tjrt|V  qd S r-   )r{   r=   r>   r  r  r0   r0   r1   rI   D  s    
zBsolve_min_cut.<locals>.should_ban_recomputation.<locals>.<genexpr>rz   )r   rt   r   r   rg   rh   r   r  r   recompute_viewsr6   r  lift_fresh_copyr  
lift_freshrd   r7   r5   r4   rc   r$   debugr   r   dist_from_bwmax_dist_from_bwre   r  r   r  )r+   input_tensors_sizeoutput_size)r  r  r  r0   r1   should_ban_recomputation  s<   

z/solve_min_cut.<locals>.should_ban_recomputationc                    s*    j dkrdS t fdd jD  S )Nr   Tc                 3   s    | ]} |V  qd S r-   r0   r   )r2   r+   r0   r1   rI   O  s    z9solve_min_cut.<locals>.is_materialized.<locals>.<genexpr>)r   r   r   rk   )r2   rk   r1   is_materializedK  s   
z&solve_min_cut.<locals>.is_materializedrE   c                    sv   t | }tjr| rtjS t| jd tr"t| jd t	j
s"tS t|dtt| jdd  } | r7|S |d S )Nry   g?d   r      )r  r   r  r6   r9  r:  r{   rg   r   ru   r|   INT_INFr\   maxminr  )r+   mem_sz)r  r  r0   r1   get_node_weightQ  s   z&solve_min_cut.<locals>.get_node_weightc                    sl    | rdS | v rdS t| rdS d| jv r#t| jd tjr#dS  |  jd| jd t	j
d dS )NFry   source_incapacityT)r6   rl   rg   r{   ru   r~   r   add_edger   r9  r:  rk   )banned_nodesr  nx_graphr  r0   r1   ban_recomputation_if_allowedm  s   

z3solve_min_cut.<locals>.ban_recomputation_if_allowedr   r  sinkr  _outry   r           start_nodes	max_rangec           	         s   g }| D ]}t |||df qt|dkrVt |\}}}|s(|S |jD ]$}|rO||kr:q+|| ||f}||vrOt || q+t|dks|S )z
        Finds the first unfusible node in the chain of nodes starting from
        `start_nodes` and returns its position.
        Tr   )heapqheappushrY   r1  heappopr   rT   )	r  r  sorted_nodesrH   r  r+   node_is_fusibler   ry   )r2   r  r0   r1   find_first_unfusible  s(   


z+solve_min_cut.<locals>.find_first_unfusiblec                    s    g | ]}  |r |qS r0   )rT   rY   r   r  r0   r1   r     s    z!solve_min_cut.<locals>.<listcomp>c                    s   g | ]	}  |r|qS r0   )rT   r   r  r0   r1   r     s
    
z1used above/below fusible %s:(%s) -> %s -> %s:(%s)r  ztoo long %s %s %s %sr  z-Failed to compute min-cut on following graph:
c                 3   s    | ]	}| | fV  qd S r-   r0   rF   )r  r0   r1   rI   :  r   c                 3   s     | ]}| v r|fV  qd S r-   r0   )rG   v)non_reachableur0   r1   rI   ;  s    c                 S   s   i | ]\}}||qS r0   r0   )rG   r<  r+   r0   r0   r1   r,  E  s    z!solve_min_cut.<locals>.<dictcomp>c                 3   s    | ]} | V  qd S r-   r0   r   name_to_noder0   r1   rI   G  r  c                    r   r-   r0   r   )node_idxr0   r1   rL   G  r   zsolve_min_cut.<locals>.<lambda>rN   ):r   get_default_op_listr#   ro   r*   r$   r  networkxImportErrorr   floatDiGraphr   rB   r@   r  r   r9  r:  rl   r   r   rT   rg   r{   ru   r  r   r   rh   r   r   rZ   r=   r>   r\   ra   rQ   r1  r  r   rY   rb   r  r  r   r  minimum_cut	Exceptionjoin	readwriteedgelistgenerate_edgelistvisualize_min_cut_graphupdateget_name_to_noder8  rP   )$r   r  r  r  joint_module_opsops_ignorednxer  r  r  r+   is_non_tensor_nodeweightr   r  	used_nodeordersfw_usersfirst_unfusible_usevisited
start_nodefusiblestart_orderr  r  	cut_value	partition	reachablecutsetnbrs	cut_nodesnode_innode_out	node_namer   r0   )r  r  r  r  r2   r  r  r  r  r  r  r  r  r  r  r1   solve_min_cut  s$  


2



	
""










r  c                 C   s   dd l }dd l}|j|  }||d }| D ] }| |  |  d }|	t
| |tdkr;|d qtd |d d S )Nr   r  r:  redz2Visualizing the failed graph to min_cut_failed.svgzmin_cut_failed.svg)r  pydotnx_pydotto_pydot	to_stringgraph_from_dot_data	get_edges
get_sourceget_destination	set_labelr   r  	set_colorr$   r  	write_svg)r  r  r  
dot_format	dot_graphedger  r0   r0   r1   r  L  s   

r  c                  C   s  g t jt jt jt jt jt jt jt jt j	t j
t jt jt jt jt jt jt jt jt jt jt jt jt jt jt jt jt jt jt jt jt jt j t j!t j"t j#t j$t j%t j&t j't j(t j)t j*t j+t j,t j-t j.t j/t j0t j1t j2t j3t j4t j5t j6t j7t j8t j9t j:t j;t j<t j=t j>t j?t j@t jAt jBt jCt jDt jEt jFtGjHt jIt jJt jKt jL} t jIt jJt jMg}|t jNt jOt jPtQjRt jSt jTt jUt jVg7 }|}| g tQjtQjWt jXt jLt jYtQjZtQj@t jZt j[tQjRt jVt j\t jNt jSt jOt j]t j^t j_t j`t jat jbt jct jdt jet jft jgt jht jTt jit jjt jkt jlt jmtQjntQjo7 } | t jpt jqg7 } | |7 } | tr 7 } | t jsg7 } | dd ttD 7 } tu| }tut jvt jwt jxg}t jyt jzt j{t j|t j}t j~t jt jt jt jt jg}||B }t|tu||tu||S )Nc                 S      g | ]}t |qS r0   )r   )rG   mr0   r0   r1   r         z'get_default_op_list.<locals>.<listcomp>)r  r   subdivatan2mulr  r  pow	remainderfmod__and____or____xor__
__lshift__
__rshift__eqnegegtleltabsbitwise_notceilfloorfracnegreluroundsilutruncr$   log10log1plog2lgammaexpexpm1erferfccosacoscoshsinasinsinhtanatantanhatanhsqrtrsqrt
reciprocalsigmoidsoftplus	thresholdthreshold_backwardclampwherelerpaddcmulgelugelu_backwardr  mean_grad_sum_to_sizesum_to_sizeamaxtotype_asr   r   squeeze	unsqueezersub_to_copyaliasviewslicetprimsbroadcast_in_dimexpand
as_stridedpermuteselectconvert_element_typeclone	full_likevarstd_unsafe_viewreshapebroadcast_tensorsscalar_tensorones	new_zerosr  arangetriuvar_meanisinfr   fullzerosempty
empty_likeargmaxmaximumiota)_low_memory_max_pool2d_offsets_to_indicesrN  gatherr(  
zeros_liker   r   native_dropout	rand_like
randn_likemmconvolutionconvolution_backwardbmmaddmm#_scaled_dot_product_flash_attention'_scaled_dot_product_efficient_attention_flash_attention_forward_efficient_attention_forwardupsample_bilinear2d
_scaled_mmr%   )default_recomputable_opsrecomputable_view_opsr)   r*   r(   r'   r&   r0   r0   r1   r  ]  s  	
 !"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKM
	
 !"#&
r  c                 C   s   i }| j D ]}|||j< q|S r-   )ro   r   )rn   r  r+   r0   r0   r1   r    s   
r  memoryruntimes
max_memoryall_recomputable_banned_nodesc           
      C   s   t j}|dkrt|||S |dkrt|||S |dkr!t|||S |dkrAtd tj| |||d}t||t	|dj
t|dS t|rT||| |||\}}	d	||	fS td
| )Ngreedyilpdpdynamic_memory_budget_dpzdynamic_memory_budget_dp is an experimental solver. It does not guarantee performance improvements. Additionally, it is not guaranteed to be stable.)r   r   recorded_knapsack_input_memories recorded_knapsack_input_runtimes)graph_info_provider)knapsack_algomax_mem_budgetr  z,Not aware of memory budget knapsack solver: )r   activation_memory_budget_solverr   r   r   r$   warningr   inialize_from_graphr   get_knee_point_memory_budgetcallabler   )
r   r  r  r  r  r  SOLVERr  saved_node_idxrecomp_node_idxr0   r0   r1   #_optimize_runtime_with_given_memory	  sD   


r  no_dispatchr   r  c                    sL   t | j} fddfdd|D }fdd|  D }| j||dS )Nc                    s   t |  dS )Nr  )r   )dr  r0   r1   realize_symbol>  s   z8_remove_symbols_without_guarding.<locals>.realize_symbolc                       g | ]} |qS r0   r0   rG   r   r  r0   r1   r   A  r  z4_remove_symbols_without_guarding.<locals>.<listcomp>c                    r  r0   r0   r  r  r0   r1   r   B  r  )stride)rZ   shaper  new_empty_strided)r   r  r  r  r0   )r  r  r1    _remove_symbols_without_guarding;  s
   
r  c                    s  t j}dd }|dkrdS |dkrEt ' ddlm} t|jjf\ |	 fdd	}|W  d    S 1 s>w   Y  d S |d
krddl
m} t|jjf\ |dd}j i  W d    n1 ssw   Y  | }t|dS td| )Nc                 S   s   t | tjrt | jd tjrt| jd ddS t | tjr0t | jd tjr0t| jd ddS t | tjrAt | jd tj	rAdS t | tjrRt | jd tj
rRdS | S )Nry   r  r        ?T)r{   r=   r>   rg   ru   r  r  r|   r   r~   r}   r   r0   r0   r1   materialize_argI  s   z)estimate_runtime.<locals>.materialize_argtestingr   profiler   )benchmarkerc                      s   j  i S r-   )rt   r0   r   r   r+   r0   r1   rL   ]  s    z"estimate_runtime.<locals>.<lambda>flops)FlopCounterModeF)displayz Not aware of runtime estimator: )r   *activation_memory_budget_runtime_estimatorr  $torch._inductor.runtime.benchmarkingr  r   tree_mapr   r   benchmark_gputorch.utils.flop_counterr  rt   get_total_flopsr  r   )r+   RUNTIME_MODEr  r  msr  modecounted_flopsr0   r  r1   estimate_runtimeF  s(   $
r  c                    sX  |dks|dk rt d| ttjtjtjtjtjd}tjr)t	|ddddd}|dkr0j
S t|\}}|dkr>|S dttj dtfd	d
j
|		krY|S 	fdddttj f	fddt	|dddd}t|\}}||k r|S t	|dd t \}}	||k r|S ddlm tfddj
D dttj dttj ffdd}
|
|	}t|dd d}t|tddtdkr܈j
S fddD 
dd D dd lm  
fd!d"tjr#fd#d$}|d%|d&g}|d dd  |d dd  kr|d |d fg}|r| \}}|d |d  d'k rL|| || q,||d |d  d( }|dd  |dd  krl|||f |dd  |dd  kr|||f |s/|  dd lm} d)d |D }d*d |D }|jd+d, |j||d-d. t |D ]\}}|j!|d/||| fd0d1d2d3 q|"d4 |#d5 |$d6 |%d |& }|'  t() }tj*d urtj*}t(j+|dd7 d8}t,j-. r	t,j-/ r	d9t,j-0  }t(j12|d:| d;t3  d<}|4| t56d=| |d>d S )?Nr   r   zJThe valid ranges for memory budget are 0 <= m <= 1. The provided value is )ra   rb   rc   rd   re   F)ra   rb   rc   rd   r   rE   c                 S   s   t tt| d S N    eA)r  mapr  )r   r0   r0   r1   estimate_activations_size  r   z:choose_saved_values_set.<locals>.estimate_activations_sizec                    s   | d    S r  r0   )sz)max_act_sizemin_act_sizer0   r1   get_normalized_size  s   z4choose_saved_values_set.<locals>.get_normalized_sizeactivationsc                    s    |    S r-   r0   )r  )r  r  r  r0   r1   get_mem_ratio  s   
z.choose_saved_values_set.<locals>.get_mem_ratio)ra   rb   rc   )rd   get_node_storagec                 3   r
  r-   r0   r   r  r0   r1   rI     r  z*choose_saved_values_set.<locals>.<genexpr>r  c                    s    fdd| D S )Nc                    s*   g | ]}|j td k r |vr|qS )r  )r  r\   r  r  input_storagesr0   r1   r     s    zRchoose_saved_values_set.<locals>.get_recomputable_banned_nodes.<locals>.<listcomp>r0   )r  r  r0   r1   get_recomputable_banned_nodes  s   z>choose_saved_values_set.<locals>.get_recomputable_banned_nodesc                 S   r   r-   r   r   r0   r0   r1   rL     r   z)choose_saved_values_set.<locals>.<lambda>rN   Tr  c                    s   g | ]} t |qS r0   r  r  )r  r0   r1   r     s    z+choose_saved_values_set.<locals>.<listcomp>c                 S   r  r0   )r  r   r0   r0   r1   r     s    r  c           
   
      s     t |t| d|\}}}W d    n1 sw   Y  t }|D ]}z	||  W q' ty;   Y q'w |sCJ t|| |\}}	trZt|||||d ||fS )Nr   )r   r  saved_node_idxsrecomputable_node_idxsexpected_runtimememories_banned_nodesruntimes_banned_nodesmin_cut_saved_values)	r  r  r   r   BaseExceptionissubsetr  r#   r
   )
memory_budgetr  r   r  r  r  r  r<  r   r  )aggressive_optionsr  r  r  r  r0   r1   get_saved_values_knapsack  sP   
z:choose_saved_values_set.<locals>.get_saved_values_knapsackc                    s(   | d\}}| t |  |fS )N)r  r   )r  )r  r   r  )r  r  r   r  r  r0   r1   estimate_for_budget  s   

z4choose_saved_values_set.<locals>.estimate_for_budgetr  r  gMbP?r  c                 S      g | ]}|d  qS )r  r0   rG   itemr0   r0   r1   r   *  r  c                 S   r  r   r0   r  r0   r0   r1   r   +  r  )
      )figsizeo)markerz.4fzoffset points)r   r  center)
textcoordsxytexthazMemory Budgetz Runtime of Recomputed Componentsz:Pareto Frontier of Memory Budget vs. Recomputation Runtime)exist_ok _rank_memory_budget_paretor  z.svgz%Generated Pareto frontier curve at %s)r  r  r   )7r   r`   r   ban_recompute_used_far_apart!ban_recompute_long_fusible_chains#ban_recompute_materialized_backwardban_recompute_not_in_allowlistban_recompute_reductionsaggressive_recomputationr   r@   r  rZ   r=   r>   r  torch._inductor.fx_utilsr  r   rP   r  r1  torch.utils._mode_utilsr  visualize_memory_budget_paretor2  r   sortmatplotlib.pyplotpyplotfigureplotr8  annotatexlabelylabeltitlegridgcfshowosgetcwdmemory_budget_pareto_dirmakedirsru   distributedis_availableis_initializedget_rankpathr  r   savefigr$   r  )r   r  r  r  runtime_optimized_saved_valuesr  more_aggressive_optionsmore_aggressive_saved_values%aggressive_recomputation_saved_valuesr  r  recomputable_banned_nodesr  optionsbisectslhsrhsmidpltx_valuesy_valuesr  txtfigfig_dirrank_suffixfig_namer0   )r  r  r  r  r  r  r  r  r   r  r  r  r  r  r  r1   choose_saved_values_setm  s  

+
"








r   inductorc                   s  | j   |   | j }tjrt|}|| _ | j }t| }t| }|r't| }  fdd}	|	| }
t	|
j
dkr?t| | dS t| j jD ]+}|jdkrRtd|_qE|
|s[d|_qEtd|_|jD ]}t|j|jd |_qcqEtj}|jD ]}t|jdd	tr|jd } nqwt||
|d
}ttt|}ttdd |}t| || d\}}|r|rt| ||t	|\}}t|}t r@t!dd |D }t"dd |D d }t#$d| t#$d| t%dd |j jD }t%dd |j jD }||@ }t&t}|j jD ]}|j'|v rt(|j)dr|t*|j)j+  d7  < qt#$dt	|t	|t	| t!|, dd dd}t#$d| ||fS )ax  
    Partitions the joint graph such that the backward recomputes the forward.
    Recomputing helps in trading off memory bandwidth with computation.

    To create the fwd and bwd graph, we copy the joint graph, manually set the
    outputs to just original forward or backward outputs. And then we run the
    resulting graphs through dead code elimination.

    .. warning::
        This API is experimental and likely to change.

    Args:
        joint_module(fx.GraphModule): The joint forward and backward graph. This
            is the result of AOT Autograd tracing.
        _joint_inputs: The inputs to the joint graph. This is unused.
        compiler: This option determines the default set of recomputable ops.
            Currently, there are two options: ``nvfuser`` and ``inductor``.
        recomputable_ops: This is an optional set of recomputable ops. If this
            is not None, then this set of ops will be used instead of the
            default set of ops.
        num_fwd_outputs: The number of outputs from the forward graph.

    Returns:
        Returns the generated forward and backward Fx graph modules.
    c                    s6  t | j t | jjD ]%}|jdkrd|jv r| n	t|r'| |v r1|j	 qt
tt| jj}t
tt| jj}|| }t| d\}}dd |D  t| j||d}t fdd|jD tfdd| jjD }d	}	i }
| jjD ]}|v r|	|
|< |	d
7 }	qt|||
S )Nr   r   r   c                 s   s&    | ]}|d ur|j dkr|V  qd S )Nr   r   )rG   r  r0   r0   r1   rI     s    zNmin_cut_rematerialization_partition.<locals>.classify_nodes.<locals>.<genexpr>r   c                 3   s$    | ]}|j d kr |j V  qdS r   r   r   r  r0   r1   rI     s    
c                 3   s$    | ]}|vr| vr|V  qd S r-   r0   r   )rB   rQ   r0   r1   rI     s    r   r   )r  rn   r   ro   r   rt   r   r   r  r   rZ   r   r   r   r   r   r?   )r   r+   r   r   r@   r   r   r   rC   fw_cntrD   r   )r  rB   rQ   r1   classify_nodes  sP   





z;min_cut_rematerialization_partition.<locals>.classify_nodesr   r   r   r  r   r  N)r  c                 S   s
   t |  S r-   r   rJ   r0   r0   r1   rL     rM   z5min_cut_rematerialization_partition.<locals>.<lambda>r   c                 S   s   g | ]
}t |t|fqS r0   )r  r   r  r0   r0   r1   r     s    z7min_cut_rematerialization_partition.<locals>.<listcomp>c                 s   r   r-   r  r  r0   r0   r1   rI     r  z6min_cut_rematerialization_partition.<locals>.<genexpr>z'Theoretical Activations Stored: %.2f GBz,Theoretical Per Activation Storage Sizes: %sc                 s        | ]}|j d kr|jV  qdS r   Nr   r   r0   r0   r1   rI     r   c                 s   r$  r%  r   r   r0   r0   r1   rI     r   r  z# remat/fw/bw: %d/%d/%dc                 S   r  r  r0   r   r0   r0   r1   rL     r   Tr  zCount of Ops Rematerialized: %s)-rn   r   r~  r   cser!   rq   rx   r  r1  rB   r   ru  ro   r   r\   r  rT   r   r  activation_memory_budgetr{   rg   rh   r  r   rZ   r   r   r   r  rA  r#   rP   r  r$   r  r   r   r   rs   rt   r   r  r  )r   r   compilerr   rm   	cse_graphr   graph_has_recomputable_opsgraph_has_recomputable_rng_opsr#  r  r+   r   r  r   r   rB  rC  sorted_sizestotal_activations_size_gbfw_module_nodesbw_module_nodesremat_nodescountsrematerialized_opsr0   r   r1   r   Y  s   
!.






r   fx_graphTFtracedfnamefigname
clear_metaprogparse_stack_tracedot_graph_shapec                 C   s   |rt | j}t| |} | jjD ]}i |_qtj	|\}	}
|
s'dt
j }
td|	|
 tj| |||d}| }t|d|
d }|	 |
 }|d u rU|| d S |||d d S )N.zWriting FX graph to file: %s%s)r9  r:  write_)r8  )copydeepcopyrn   r=   r;  ro   rg   r  r  splitextr   torch_compile_graph_formatr$   r  r   FxGraphDrawerget_main_dot_graphr  lstrip)r4  r5  r6  r7  r8  r9  r:  r   r+   baseextgr   write_methodr0   r0   r1   
draw_graph	  s*   	
rH  r-   r  )r!  )r3  TNFN)r=  r]   r  r   loggingr9  r   r  os.pathr  r   dataclassesr   r   typingr   r   r   r	   ru   torch._inductor.inductor_primstorch.distributedtorch.fxr=   torch.utils._pytreeutils_pytreer   ;torch._functorch._activation_checkpointing.ac_logging_utilsr
   %torch.fx.experimental._backward_stater   "torch.fx.experimental.proxy_tensorr   r   torch.fx.experimental.sym_noder   r   %torch.fx.experimental.symbolic_shapesr   r   r   r   torch.fx.passesr   torch.utils._ordered_setr   torch.utils.checkpointr   r  r   -_activation_checkpointing.graph_info_providerr   "_activation_checkpointing.knapsackr   r   r   ,_activation_checkpointing.knapsack_evaluatorr   _aot_autograd.logging_utilsr   _aot_autograd.utilsr   r    compile_utilsr!   r"   sympydebug_partitionerr#   r_   r<   	getLoggerr8   r$   Loggerr  r  r^  r%   r?   r`   r>   rl   r;  rq   rx   r\   r   r   r   r   rZ   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r  r  r  r  	lru_cacher(  r[   r.  rA  rF  r`  r  r  r  r  r  r  r  r  r  r  r  r  r  r   r   rH  r0   r0   r0   r1   <module>   s  

G	

c
V

*J
]
 L*
    &
.*
 p
 4