o
    "j(,                     @   s   d dl 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
mZ g ZG dd	 d	ZG d
d dZG dd dejZi Z	dddZdd Zdd Zdd ZdS )    N)Sequence)core)Program)gast   )
ORIGI_INFOunwrapc                   @   s2   e Zd ZdZdZd
ddZdd Zedd	 ZdS )Locationz.
    Location information of source code.
    filepathlineno
col_offsetNc                 C      || _ || _|| _d S Nr
   )selfr   r   r    r   a/var/www/html/Deteccion_Ine/venv/lib/python3.10/site-packages/paddle/jit/dy2static/origin_info.py__init__&      
zLocation.__init__c                 C   s   d| j  d| j d| j S )Nz
location: :r
   r   r   r   r   __str__+   s   zLocation.__str__c                 C   s   | j | jfS r   )r   r   r   r   r   r   line_location.   s   zLocation.line_locationr   )	__name__
__module____qualname____doc__	__slots__r   r   propertyr   r   r   r   r   r	      s    
r	   c                   @   s4   e Zd ZdZdZdd Zdd Zdd Zd	d
 ZdS )
OriginInfoz.
    Original information of source code.
    locationfunction_namesource_codec                 C   r   r   r    )r   r!   r"   r#   r   r   r   r   >   r   zOriginInfo.__init__c                 C   s   d | j| j| jS )Nz&{} 
source_code: {}  in function {}
  )formatr!   r#   r"   r   r   r   r   r   C   s   zOriginInfo.__str__c                 C   s&   d}d | jj| jj| j|| j S )Nz(* user code *)z$    File "{}", line {}, in {} {}
	{})r$   r!   r   r   r"   r#   lstrip)r   Zflag_for_origin_infor   r   r   formated_messageH   s   zOriginInfo.formated_messagec                 C   s   | j j| j j| j| j fS r   )r!   r   r   r"   r#   r%   r   r   r   r   as_frameR   s
   zOriginInfo.as_frameN)	r   r   r   r   r   r   r   r&   r'   r   r   r   r   r   3   s    
r   c                   @   s@   e Zd ZdZdd Zdd Zdd Zdd	 Zd
d Zdd Z	dS )OriginInfoAttacherzZ
    Attach original source information to AST node according corresponding function.
    c                 C   s6   || _ t|| _t| j| _t| j| _g | _d S r   )	rootr   funcinspectgetsourcefiler   	getsourcer#   current_func)r   r)   r*   r   r   r   r   `   s
   

zOriginInfoAttacher.__init__c                 C   sX   t | j\}}|d }t|t|  | _dd |D | _|d | _| | j	 d S )Nr   c                 S   s   g | ]}| d qS )
)strip).0liner   r   r   
<listcomp>k   s    z0OriginInfoAttacher.transform.<locals>.<listcomp>r   )
r+   getsourcelinesr*   lenr%   r   source_lineslineno_offsetvisitr)   )r   r6   Zbegin_linenoZ
begin_liner   r   r   	transformg   s   
zOriginInfoAttacher.transformc                 C   sP   t |tjr| j| t|dr| | | | t |tjr&| j  |S )Nr   )	
isinstancer   FunctionDefr.   appendhasattr_attach_origin_infogeneric_visitpopr   noder   r   r   r8   o   s   



zOriginInfoAttacher.visitc                 C   sx   t |tjsJ t|dsJ | |}| |}t| j||}| jd j	}| j
|jd  }t|||}t|t| d S )Nr   r   )r:   r   ASTr=   _abs_lineno_abs_col_offsetr	   r   r.   namer6   r   r   setattrr   )r   rB   r   r   loc	func_nameZ	code_lineZorigin_infor   r   r   r>   z   s   

z&OriginInfoAttacher._attach_origin_infoc                 C   s   | j |j S r   )r7   r   rA   r   r   r   rE      s   	zOriginInfoAttacher._abs_linenoc                 C   s   | j |j  S r   )r   rA   r   r   r   rF      s   z"OriginInfoAttacher._abs_col_offsetN)
r   r   r   r   r   r9   r8   r>   rE   rF   r   r   r   r   r(   [   s    r(   Tc                 C   s   i }t |}t|}t||}t| |D ]P\}}t|t|ks.J dt|t|t|t	d}t|t	d}	|du sB|	du rCq|	j
j}
||
}|durb|j
j|j
jkrYq|j
j|j
jkrbq|||
< qt| |rptS |S )a  
    Creates a original information map between transformed static function and original dygraph function.

    Args:
        transformed_node(gast.AST): The AST node of transformed dygraph function with attached source information of original dygraph function.
        static_func(Callable): The static function transformed by dygraph function corresponding to transformed_node.

    Returns:
        The original information map.
    [The node types should be the same, but received type(t_node) is {}, and type(s_node) is {}.N)r+   r-   r   parseattach_origin_infoast_walktyper$   getattrr   r!   r   getr   r   global_origin_info_mapupdate)transformed_nodeZstatic_func	is_globalZorigin_info_mapZstatic_sourcestatic_nodet_nodes_nodeZdygraph_infoZstatic_infoZ
static_locZexist_origin_infor   r   r   !create_and_update_origin_info_map   s@   





rY   c                 C   s   t | |}|  | S )ap  
    Attach original source information to AST node according corresponding function.

    Args:
        ast_node(gast.AST): The AST node to attach original source information.
        func(Callable): The corresponding function of ast_node. Parse the original information from this function.

    Returns:
        An AST node attached original source information.
    )r(   r9   )Zast_noder*   resolverr   r   r   rM      s   
rM   c                 c   sN   dd }|| }||}|rt |t |ksJ | }| }t|t|kr<t|tjtjfs;t|tjtjfr<qt|t|ksPJ dt|t|||fV  |jD ]H}t	||}t	||}	t|tj
ru|| ||	 qXt|ttfrt |t |	ksJ t||	D ]\}
}t|
tj
r||
 || qqX|sdS dS )a   
    Recursively yield all descendant nodes in the trees starting at transformed_node and static_node (including itself) in parallel.

    NOTE(liym27):
        Function ast.walk is not used because it yield all descendant nodes in no specified order.
    c                 S   s$   | d u rg S t | trt| S | gS r   )r:   r   list)xr   r   r   _as_list   s   zast_walk.<locals>._as_listrK   N)r5   r@   rO   r:   r   LoadParamr$   _fieldsrP   rD   r<   r[   tuplezip)rT   rV   r]   Ztransformed_node_listZstatic_node_listrW   rX   fieldZt_node_childZs_node_childZd_itemZs_itemr   r   r   rN      sF   






rN   c                 C   s   t | tsJ dd }tj}| }| jD ](}t|jD ] \}}||r<|	|}||}z|
|| W q   Y qqq| S )ze
    Replaces op callstack information about transformed static code with original dygraph code.
    c           	      S   s   t | d dks
J tdt | dD ]Z}| | dd}|d dd }t|d dd }|d d	d }| |d  d}t||}t|j}|rV|	 \}}}}d
| d| d| | |< d| | |d < q| S )a  
        An example of callstack:

            File "path1/to/file.py", line 10, in func_1
                y = paddle.tensor.fill_constant(x, shape=[1], dtype="int32")
            File "path2/to/file.py", line 740, in fill_constant
                stop_gradient=True)
            File "path3/to/file.py", line 43, in append_op
              return self.main_program.current_block().append_op(*args, **kwargs)
            File "path4/to/file.py", line 2811, in append_op
              attrs=kwargs.get("attrs", None))
            File "path5/to/file.py", line 1919, in __init__
              for frame in traceback.extract_stack():
           r    ,   rC   r   N   z  File "z", line z, in z    )
r5   ranger%   splitintr	   rR   rQ   r   r'   )		callstackiZ	file_liner   r   funcnamecoderI   Zdygraph_func_infor   r   r   get_new_op_callstack  s   
zBupdate_op_callstack_with_origin_info.<locals>.get_new_op_callstack)r:   r   r   Zop_proto_and_checker_makerZkOpCreationCallstackAttrNameblocks	enumerateopsZhas_attrattrZ	_set_attr)programrp   Zop_makerZcallstack_var_nameblockrm   oprl   r   r   r   $update_op_callstack_with_origin_info  s    #


rx   )T)r+   collections.abcr   Zpaddle.baser   Zpaddle.base.frameworkr   Zpaddle.utilsr   utilsr   r   __all__r	   r   NodeTransformerr(   rR   rY   rM   rN   rx   r   r   r   r   <module>   s    (;
64