
    ϑi+                         S SK r S SKrS SKJr  S SKJr  S SKJr  S SKJ	r	  SSK
Jr  / r " S S	5      r " S
 S5      r " S S\	R                  5      r0 r SS jrS rS rS rg)    N)Sequence)core)use_pir_api)gast   )ORIGIN_INFOc                   <    \ rS rSrSrSrS	S jrS r\S 5       r	Sr
g)
Location   z&
Location information of source code.
filepathlineno
col_offsetNc                 (    Xl         X l        X0l        g Nr   )selfr   r   r   s       `/var/www/html/banglarbhumi/venv/lib/python3.13/site-packages/paddle/jit/dy2static/origin_info.py__init__Location.__init__'   s     $    c                 T    SU R                    SU R                   SU R                   3$ )Nz
location: :r   r   s    r   __str__Location.__str__,   s(    DMM?!DKK=$//9JKKr   c                 2    U R                   U R                  4$ r   )r   r   r   s    r   line_locationLocation.line_location/   s    t{{++r   )r   r   r   r   )__name__
__module____qualname____firstlineno____doc__	__slots__r   r   propertyr   __static_attributes__ r   r   r
   r
      s-    I%
L , ,r   r
   c                   4    \ rS rSrSrSrS rS rS rS r	Sr
g	)

OriginInfo4   z&
Original information of source code.
locationfunction_namesource_codec                 (    Xl         X l        X0l        g r   r+   )r   r,   r-   r.   s       r   r   OriginInfo.__init__?   s     *&r   c                 T    U R                    SU R                   SU R                   S3$ )Nz 
source_code: z  in function z
  )r,   r.   r-   r   s    r   r   OriginInfo.__str__D   s0    -- 01A1A0B.QUQcQcPddhiir   c                     SnSU R                   R                   SU R                   R                   SU R                   SU SU R                  R                  5        3
$ )Nz(* user code *)z
    File "", line , in  z
	r,   r   r   r-   r.   lstrip)r   flag_for_origin_infos     r   formatted_messageOriginInfo.formatted_messageG   s    0DMM2238DMM<P<P;QQVW[WiWiVjjk  mA  lB  BF  GK  GW  GW  G^  G^  G`  Fa  b  	br   c                     U R                   R                  U R                   R                  U R                  U R                  R                  5       4$ r   r7   r   s    r   as_frameOriginInfo.as_frameK   sA    MM""MM  ##%	
 	
r   )r-   r,   r.   N)r   r    r!   r"   r#   r$   r   r   r:   r=   r&   r'   r   r   r)   r)   4   s$    I'
jb
r   r)   c                   <    \ rS rSrSrS rS rS rS rS r	S r
S	rg
)OriginInfoAttacherT   zR
Attach original source information to AST node according corresponding function.
c                     Xl         [        R                  " U5      U l        [        R                  " U R                  5      U l        [        R                  " U R                  5      U l        / U l        g r   )	rootinspectunwrapfuncgetsourcefiler   	getsourcer.   current_func)r   rC   rF   s      r   r   OriginInfoAttacher.__init__Y   sL    	NN4(	--dii8",,TYY7r   c                 F   [         R                  " U R                  5      u  pUS   n[        U5      [        UR	                  5       5      -
  U l        U Vs/ s H  oDR                  S5      PM     snU l        US-
  U l        U R                  U R                  5        g s  snf )Nr   
r   )rD   getsourcelinesrF   lenr8   r   stripsource_lineslineno_offsetvisitrC   )r   rP   begin_lineno
begin_linelines        r   	transformOriginInfoAttacher.transform`   s    %,%;%;DII%F"!!_
j/C
0A0A0C,DD:FG,$ZZ-,G)A-

499 Hs   Bc                 L   [        U[        R                  5      (       a  U R                  R	                  U5        [        USS 5      b  U R                  U5        U R                  U5        [        U[        R                  5      (       a  U R                  R                  5         U$ )Nr   )	
isinstancer   FunctionDefrI   appendgetattr_attach_origin_infogeneric_visitpopr   nodes     r   rR   OriginInfoAttacher.visith   s~    dD,,--$$T*44(4$$T*4 dD,,--!!#r   c                    [        U[        R                  5      (       d   e[        US5      (       d   eU R	                  U5      nU R                  U5      n[        U R                  X#5      nU R                  S   R                  nU R                  UR                  S-
     n[        XEU5      n[        U[        U5        g )Nr   r   )rY   r   ASThasattr_abs_lineno_abs_col_offsetr
   r   rI   namerP   r   r)   setattrr   )r   ra   r   r   loc	func_name	code_lineorigin_infos           r   r]   &OriginInfoAttacher._attach_origin_infos   s    $))))tX&&&&!!$'))$/
t}}f9%%b)..	%%dkkAo6	 ;k;/r   c                 4    U R                   UR                  -   $ r   )rQ   r   r`   s     r   rg   OriginInfoAttacher._abs_lineno   s    !!DKK//r   c                 4    U R                   UR                   -   $ r   )r   r`   s     r   rh   "OriginInfoAttacher._abs_col_offset   s    00r   )r   rI   r   rF   rQ   rC   r.   rP   N)r   r    r!   r"   r#   r   rV   rR   r]   rg   rh   r&   r'   r   r   r@   r@   T   s%    	001r   r@   c                    0 n[         R                  " [        R                  " U5      5      n[        R
                  " U5      n[        XQ5      n[        X5       H  u  pg[        U5      [        U5      :X  d    S[        U5       S[        U5       S35       e[        U[        S5      n[        U[        S5      n	Ub  U	c  Mg  U	R                  R                  n
UR                  U
5      nUb`  UR                  R                  UR                  R                  :  a  M  UR                  R                  UR                  R                  ::  a  M  XU
'   M     [         R#                  U5        U(       a  [         $ U$ )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dedentrD   rH   r   parseattach_origin_infoast_walktyper\   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      sa    OOOG$5$5k$BCM**]+K$[>K"#3AF|tF|+ 	
NtTZ|n\rswx~s  sA  AB  C	
+ v{D9fk48;#6 ))77
+//
;(!**11((//0 !**55((334 &2
#1 B4 !!/2%%r   c                 <    [        X5      nUR                  5         U $ )aT  
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@   rV   )ast_noderF   resolvers      r   r{   r{      s     "(1HOr   c              #   *  #    S nU" U 5      nU" U5      nU(       Ga  [        U5      [        U5      :X  d   eUR                  5       nUR                  5       n[        U5      [        U5      :w  a`  [        U[        R
                  [        R                  45      (       d/  [        U[        R
                  [        R                  45      (       a  M  [        U5      [        U5      :X  d    S[        U5       S[        U5       S35       eXV4v   UR                   H  n[        XW5      n[        Xg5      n	[        U[        R                  5      (       a$  UR                  U5        UR                  U	5        M\  [        U[        [        45      (       d  My  [        U5      [        U	5      :X  d   e[        X5       HH  u  p[        U
[        R                  5      (       d  M&  UR                  U
5        UR                  U5        MJ     M     U(       a  GM  gg7f)z
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                 R    U c  / $ [        U [        5      (       a  [        U 5      $ U /$ r   )rY   r   list)xs    r   _as_listast_walk.<locals>._as_list   s)    9I$Q11tAw:s:r   ru   rv   rw   N)rN   r_   r}   rY   r   LoadParam_fieldsr\   re   r[   r   tuplezip)r   r   r   transformed_node_liststatic_node_listr   r   fieldt_node_childs_node_childd_items_items               r   r|   r|      s    ;
 %%56,
()S1A-BBBB&**,!%%'<4<' &499djj"9::jDJJ/? ? F|tF|+ 	
NtTZ|n\rswx~s  sA  AB  C	
+ n^^E"61L"61L,11%,,\: ''5L4-88<(C,====&),&ENF!&$((33-44V<(//7 'F $'  
s   FHAH1HHc                   ^
 S nU
4S jm
[         R                  nUR                  5       n[        5       (       aR  U R	                  5       nT
" U5      nU H2  nUR                  U5      (       d  M  U" UR                  5      Ul        M4     U $ U R                   Hd  n[        UR                  5       HH  u  pUR                  U5      (       d  M  UR                  U5      n	U" U	5      n	 UR                  X95        MJ     Mf     U $ !    MW  = f)z]
Replaces op callstack information about transformed static code with original dygraph code.
c                    [        U 5      S-  S:X  d   e[        S[        U 5      S5       H  nX   R                  S5      R                  S5      nUS   SS n[	        US   SS 5      nUS   S	S nXS-      R                  S5      n[        X45      n[        R                  UR                  5      nU(       a  UR                  5       u  p4pVS
U SU SU 3X'   SU 3XS-   '   M     U $ )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   r6   ,   rd   r   N   z  File "r4   r5   z    )
rN   ranger8   splitintr
   r   r~   r   r=   )		callstacki	file_liner   r   funcnamecoderk   dygraph_func_infos	            r   get_new_op_callstackBupdate_op_callstack_with_origin_info.<locals>.get_new_op_callstack  s     9~!Q&&&q#i.!,A!++C066s;I |Ab)H1ab)*F |AB'HU#**3/D8,C 6 : :3;L;L M 3D3M3M3O0(%hZxxuXJOIL!%dV}I!e -  r   c                    > / nU R                    H6  nUR                  U5        UR                  5        H  nUT" U5      -  nM     M8     U$ r   )opsr[   blocks)blockr   op	sub_blockget_all_pir_block_opss       r   r   Cupdate_op_callstack_with_origin_info.<locals>.get_all_pir_block_ops(  sH    ))BJJrNYY[	,Y77 )  
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B{{-..3BLLA " N ^^E"599-;;122 "(: ;I 4Y ?I %7C . $ Ns   'DD)T)rD   rx   collections.abcr   paddle.baser   paddle.frameworkr   paddle.utilsr   utilsr   __all__r
   r)   NodeTransformerr@   r   r   r{   r|   r   r'   r   r   <module>r      sm      $  (  
, ,0
 
@01-- 01f   .21h /8dGr   