
    x-j+                         d dl Z 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 g Z G d d	          Z G d
 d          Z G d de	j                  Zi Z	 ddZd Zd Zd ZdS )    N)Sequence)core)use_pir_api)gast   )ORIGIN_INFOc                   :    e Zd ZdZdZddZd Zed             ZdS )Locationz.
    Location information of source code.
    filepathlineno
col_offsetNc                 0    || _         || _        || _        d S Nr   )selfr   r   r   s       `/var/www/html/banglarbhumi/venv/lib/python3.11/site-packages/paddle/jit/dy2static/origin_info.py__init__zLocation.__init__'   s     $    c                 6    d| j          d| j         d| j         S )Nz
location: :r   r   s    r   __str__zLocation.__str__,   s&    KDMKKDKKK$/KKKr   c                     | j         | j        fS r   )r   r   r   s    r   line_locationzLocation.line_location/   s    t{++r   r   )	__name__
__module____qualname____doc__	__slots__r   r   propertyr    r   r   r
   r
      sf         I% % % %
L L L , , X, , ,r   r
   c                   .    e Zd ZdZdZd Zd Zd Zd ZdS )
OriginInfoz.
    Original information of source code.
    locationfunction_namesource_codec                 0    || _         || _        || _        d S r   r$   )r   r%   r&   r'   s       r   r   zOriginInfo.__init__?   s     *&r   c                 6    | j          d| j         d| j         dS )Nz 
source_code: z  in function z
  )r%   r'   r&   r   s    r   r   zOriginInfo.__str__D   s*    -ii1AiiQUQciiiir   c                     d}d| j         j         d| j         j         d| j         d| d| j                                         
S )Nz(* user code *)z
    File "", line , in  z
	r%   r   r   r&   r'   lstrip)r   flag_for_origin_infos     r   formatted_messagezOriginInfo.formatted_messageG   s    0 bDM2  b  bDM<P  b  bW[Wi  b  b  mA  b  b  GK  GW  G^  G^  G`  G`  b  b  	br   c                 n    | j         j        | j         j        | j        | j                                        fS r   r.   r   s    r   as_framezOriginInfo.as_frameK   s4    M"M ##%%	
 	
r   N)	r   r   r   r   r   r   r   r1   r3   r!   r   r   r#   r#   4   sg         I' ' '
j j jb b b
 
 
 
 
r   r#   c                   6    e Zd ZdZd Zd Zd Zd Zd Zd Z	dS )	OriginInfoAttacherzZ
    Attach original source information to AST node according corresponding function.
    c                     || _         t          j        |          | _        t          j        | j                  | _        t          j        | j                  | _        g | _        d S r   )	rootinspectunwrapfuncgetsourcefiler   	getsourcer'   current_func)r   r7   r:   s      r   r   zOriginInfoAttacher.__init__Y   sQ    	N4((	-di88",TY77r   c                 $   t          j        | j                  \  }}|d         }t          |          t          |                                          z
  | _        d |D             | _        |dz
  | _        |                     | j	                   d S )Nr   c                 8    g | ]}|                     d           S )
)strip).0lines     r   
<listcomp>z0OriginInfoAttacher.transform.<locals>.<listcomp>d   s$    GGG$TZZ--GGGr   r   )
r8   getsourcelinesr:   lenr/   r   source_lineslineno_offsetvisitr7   )r   rG   begin_lineno
begin_lines       r   	transformzOriginInfoAttacher.transform`   s    %,%;DI%F%F"l!!_
j//C
0A0A0C0C,D,DDGG,GGG)A-

49r   c                 J   t          |t          j                  r| j                            |           t          |dd           |                     |           |                     |           t          |t          j                  r| j                                         |S )Nr   )	
isinstancer   FunctionDefr=   appendgetattr_attach_origin_infogeneric_visitpopr   nodes     r   rI   zOriginInfoAttacher.visith   s    dD,-- 	+$$T***44((4$$T***4   dD,-- 	$!!###r   c                 ~   t          |t          j                  sJ t          |d          sJ |                     |          }|                     |          }t          | j        ||          }| j        d         j	        }| j
        |j        dz
           }t          |||          }t          |t          |           d S )Nr   r   )rN   r   ASThasattr_abs_lineno_abs_col_offsetr
   r   r=   namerG   r   r#   setattrr   )r   rV   r   r   loc	func_name	code_lineorigin_infos           r   rR   z&OriginInfoAttacher._attach_origin_infos   s    $)))))tX&&&&&!!$''))$//
t}fj99%b).	%dkAo6	 i;;k;/////r   c                      | j         |j        z   S r   )rH   r   rU   s     r   r[   zOriginInfoAttacher._abs_lineno   s    !DK//r   c                      | j         |j         z   S r   )r   rU   s     r   r\   z"OriginInfoAttacher._abs_col_offset   s    00r   N)
r   r   r   r   r   rL   rI   rR   r[   r\   r!   r   r   r5   r5   T   sx             	 	 	0 0 00 0 01 1 1 1 1r   r5   Tc                    i }t          j        t          j        |                    }t	          j        |          }t          ||          }t          | |          D ]\  }}t          |          t          |          k    s+J dt          |           dt          |           d            t          |t          d          }t          |t          d          }	||	|	j        j        }
|                    |
          }|6|j        j        |j        j        k    r|j        j        |j        j        k    r|||
<   t                               |           |rt           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)textwrapdedentr8   r<   r   parseattach_origin_infoast_walktyperQ   r   r%   r   getr   r   global_origin_info_mapupdate)transformed_nodestatic_func	is_globalorigin_info_mapstatic_sourcestatic_nodet_nodes_nodedygraph_infostatic_info
static_locexist_origin_infos               r   !create_and_update_origin_info_mapr~      s    OOG$5k$B$BCCM*]++K$[+>>K"#3[AA 3 3F||tF||+++ CtTZ||  C  Cswx~ss  C  C  C ,++ v{D99fk488;#6 )7
+//
;;(!*1(/0 0 !*5(34 4 &2
##!!/222 &%%r   c                 N    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.
    )r5   rL   )ast_noder:   resolvers      r   rl   rl      s*     "(D11HOr   c              #   n  K   d } ||           } ||          }|rt          |          t          |          k    sJ |                                }|                                }t          |          t          |          k    rMt          |t          j        t          j        f          s&t          |t          j        t          j        f          rt          |          t          |          k    s+J dt          |           dt          |           d            ||fV  |j        D ] }t          ||          }t          ||          }	t          |t          j	                  r+|
                    |           |
                    |	           ht          |t          t          f          r|t          |          t          |	          k    sJ t          ||	          D ]I\  }
}t          |
t          j	                  r*|
                    |
           |
                    |           J|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                 X    | g S t          | t                    rt          |           n| gS r   )rN   r   list)xs    r   _as_listzast_walk.<locals>._as_list   s-    9I$Q11:tAwwws:r   rf   rg   rh   N)rF   rT   rn   rN   r   LoadParam_fieldsrQ   rY   rP   r   tuplezip)rr   rw   r   transformed_node_liststatic_node_listrx   ry   fieldt_node_childs_node_childd_items_items               r   rm   rm      sk     ; ; ;
 %H%566x,,
 8())S1A-B-BBBBB&**,,!%%''<<4<<'' &49dj"9:: jDJ/? ?  F||tF||+++ CtTZ||  C  Cswx~ss  C  C  C ,++ fn^ 	8 	8E"6511L"6511L,11 8%,,\::: ''5555L4-88 8<((C,=,=====&),&E&E 8 8NFF!&$(33 8-44V<<<(//777?   8 8 8 8 8r   c                   
 d }
fd
t           j        }|                                }t                      rO|                                 } 
|          }|D ],}|                    |          r ||j                  |_        -nx| j        D ]p}t          |j	                  D ]Y\  }}|                    |          r?|
                    |          }	 ||	          }		 |                    ||	           R#  Y VxY wZq| S )ze
    Replaces op callstack information about transformed static code with original dygraph code.
    c                 P   t          |           dz  dk    sJ t          dt          |           d          D ]}| |                             d                              d          }|d         dd         }t	          |d         dd                   }|d         d	d         }| |dz                                d          }t          ||          }t                              |j                  }|r|	                                \  }}}}d
| d| d| | |<   d| | |dz   <   | 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   r-   ,   rX   r   N   z  File "r+   r,   z    )
rF   ranger/   splitintr
   rp   ro   r   r3   )		callstacki	file_liner   r   funcnamecoder_   dygraph_func_infos	            r   get_new_op_callstackzBupdate_op_callstack_with_origin_info.<locals>.get_new_op_callstack  sG     9~~!Q&&&&q#i..!,, 	- 	-A!!++C0066s;;I |AbD)H1abb)**F |ABB'HQU#**3//D8V,,C 6 : :3;L M M  P3D3M3M3O3O0&(DOhOOOOXOOIaL,d}}Ia!er   c                     g }| j         D ]<}|                    |           |                                D ]}| |          z  }=|S r   )opsrP   blocks)blockr   op	sub_blockget_all_pir_block_opss       r   r   zCupdate_op_callstack_with_origin_info.<locals>.get_all_pir_block_ops(  sa    ) 	8 	8BJJrNNNYY[[ 8 8	,,Y7778
r   )r   op_proto_and_checker_makerkOpCreationCallstackAttrNamer   global_blockhas_attrr   r   	enumerater   attr	_set_attr)programr   op_makercallstack_var_namer   r   r   r   r   r   r   s             @r   $update_op_callstack_with_origin_infor      s\   
! ! !F     .H!>>@@}} ++--##L11 	B 	BB{{-.. B33BLAA	B ^ 	 	E"59--  2;;122 
 "(: ; ;I 4 4Y ? ?I %7CCCC
 Ns   C66C:)T)r8   ri   collections.abcr   paddle.baser   paddle.frameworkr   paddle.utilsr   utilsr   __all__r
   r#   NodeTransformerr5   rp   r~   rl   rm   r   r!   r   r   <module>r      sV     $ $ $ $ $ $       ( ( ( ( ( (            
, , , , , , , ,0
 
 
 
 
 
 
 
@01 01 01 01 01- 01 01 01f   .21 1 1 1h   /8 /8 /8dG G G G Gr   