U
    D‹Ñh  ã                   @   sb   d dl mZmZ d dlZd dlmZ ddd„Zdd„ Zejj	ee dœd	d
„Z
dedœdd„ZdS )é    )ÚDictÚOptionalN©Ú
LazyStringc                    s2   ‡‡fdd„‰ dˆkrdˆd< t ‡ ‡‡fdd„ƒS )z;
    Returns a LazyString that formats the graph code.
    c                      s   ˆ d k	rˆ› dˆ › �S ˆS d S )Nú © r   )Úmaybe_idÚnamer   ú=/tmp/user/2202/pip-unpacked-wheel-bf9o3bgk/torch/fx/_utils.pyÚformat_name   s    z+lazy_format_graph_code.<locals>.format_nameZprint_outputFc                      s$   t dˆ ƒ › d�ˆjjjˆjf ˆŽƒS )Nz===== z =====
)Ú_format_graph_codeZforwardÚ__code__Úco_filenameZprint_readabler   )r   ÚgmÚkwargsr   r
   Ú<lambda>   s   
ýz(lazy_format_graph_code.<locals>.<lambda>r   )r	   r   r   r   r   )r   r   r   r   r	   r
   Úlazy_format_graph_code	   s    ÿr   c                 C   s   d| › d|› d|› d�S )z7
    Returns a string that formats the graph code.
    zTRACED GRAPH
 r   Ú
r   )r	   ÚfilenameZ	graph_strr   r   r
   r       s    r   )ÚgraphÚreturnc                 C   s2   | j D ]&}|jdkrd|jkr|jd   S qdS )zF
    Returns the nn_module_stack of the first call_function node.
    Úcall_functionZnn_module_stackN)ZnodesÚopÚmeta)r   Únoder   r   r
   Ú#first_call_function_nn_module_stack'   s    
r   é   )r   c                 C   sL   g }| }t |ƒD ]&}| | ¡ ¡ |jdkr0 q8|j}qd |ddd… ¡S )zD
    Returns a string of the last num_nodes nodes in the graph.
    Úrootr   Néÿÿÿÿ)ÚrangeÚappendZformat_noder   ÚprevÚjoin)r   Z	num_nodesZnode_contextsÚcurÚir   r   r
   Úget_node_context1   s    
r%   )N)r   )Útypingr   r   ZtorchZtorch._loggingr   r   r   ZfxZGraphr   Ústrr%   r   r   r   r
   Ú<module>   s   

