
    x-jRM                       d dl m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
 d dlmZ ddlmZmZ dZd	Zd
ZdZdZdZdZdZdZdZdZdZdZdZdZdZdZ d Z! G d dej"                  Z#d Z$d Z%d Z&d Z'd  Z(d! Z)d" Z*d# Z+d$ Z, ej-                    d%fd&Z.d' Z/d( Z0d) Z1 G d* d+          Z2 G d, d-ej"                  Z3dS ).    )annotationsN)unique_name)ast_to_source_code)gast   )PADDLE_MODULE_PREFIXis_api_in_module_helperget_argsset_args__argstrue_fnfalse_fn__for_loop_var_index__for_loop_iter_tuple__for_loop_iter_target__for_loop_iter_iterator__for_loop_iter_tuple_index__for_loop_var_len__for_loop_iter_var__for_loop_iter_zipwhile_condition
while_bodyfor_loop_conditionfor_loop_bodyc                P    	 |                      |          S # t          $ r Y dS w xY wN)index
ValueError)
array_listitems     g/var/www/html/banglarbhumi/venv/lib/python3.11/site-packages/paddle/jit/dy2static/transformers/utils.pyindex_in_listr#   3   s=    %%%   rrs    
%%c                      e Zd ZdZd Zd ZdS )BaseNodeVisitorz
    Implement customized NodeVisitor inherited from gast.NodeVisitor.
    Ancestor nodes are traced to easily support more operations of currently
    visited node.
    c                    g | _         d S N)ancestor_nodesselfs    r"   __init__zBaseNodeVisitor.__init__B   s         c                    | j                             |           d|j        j        z   }t	          | || j                  } ||          }| j                                          |S )zVisit a node.visit_)r(   append	__class____name__getattrgeneric_visitpop)r*   nodemethodvisitorrets        r"   visitzBaseNodeVisitor.visitE   sc    ""4(((DN33$(:;;gdmm!!!
r,   N)r1   
__module____qualname____doc__r+   r9    r,   r"   r%   r%   ;   s<         ! ! !    r,   r%   c                P    |  d|  d}t          j        |          j        d         S )Nz = _jst.UndefinedVar('z')r   )r   parsebody)name	func_codes     r"   create_undefined_varrC   P   s0    77t777I:i  %a((r,   c                |    t          |t                    sJ |  d| }t          j        |          j        d         S )z1
    Create a assign stmt for name = value .
    z = r   )
isinstanceboolr   r?   r@   )rA   valuer5   s      r"   create_bool_noderH   U   sE     eT"""""uD:d ##r,   c                p    i }t          j        |           D ]}t          j        |          D ]}|||<   |S r'   )r   walkiter_child_nodes)root	to_parentr5   childs       r"   get_parent_mappingrO   ^   sP    *,I	$ $ $*400 	$ 	$E#Ie	$r,   c                r    | sdS d | D             }d                     d                    |                    S )z(
    Return "('x', 'y')" for [x, y]
    Nonec                `    g | ]+}d                      |                    dd                    ,S )z'{}''z\')formatreplace).0rA   s     r"   
<listcomp>z#create_name_str.<locals>.<listcomp>m   s2    NNNTt||C7788NNNr,   z({}, ),)rT   join)name_ids	names_strs     r"   create_name_strr\   f   sB      vNNXNNNI??388I..///r,   c           	     $   t          j         |           } |r6|                     t          j        t	          |                               n(|                     t          j        d                     t          j        ||| g ddg           }|S )zh
    Wrapper all statements of nodes into one ast.FunctionDef, which can be
    called by ast.Call.
    )rG   N)rA   argsr@   decorator_listreturnstype_commenttype_params)copyr/   r   Returngenerate_name_nodeFunctionDef)nodesrA   
input_argsreturn_name_idsfunc_def_nodes        r"   create_function_def_noderk   q   s    
 IeE .T['9/'J'JKKKLLLLT[t,,,---$  M r,   c                ~    t          | t          j                              }t          j        |g|d          }||fS )zT
    Creates a `gast.Assign` node by given name_id as target and node as value.
    )ctxN)targetsrG   ra   )re   r   StoreAssign)rA   r5   rn   assign_nodes       r"   create_assign_noderr      sJ     !4:<<888G+	  K
 Kr,   c                   d }t          | t          t          f          sJ t          |           }| s
 |            S |g k    rd}nt	          |d                   }d}|                    t          j        t                    |d	                    |                     }t          j        t          j        |                    j        d         S )z{
    Create get_args function as follows:

        def get_args_0():
            nonlocal x, y
            return x, y
    c                     dt          j        t                     d} t          j        t          j        |                     j        d         S )N
        def z():
            return
        r   )r   generateGET_ARGS_FUNC_PREFIXr   r?   textwrapdedentr@   func_defs    r"   
empty_nodez(create_get_args_node.<locals>.empty_node   sK    !"677   z(/(33449!<<r,   
r   zK
    def {func_name}():
        {nonlocal_vars}
        return {vars},
    rX   )	func_namenonlocal_varsvars)rE   listtuplecreate_nonlocal_stmt_nodesr   rT   r   rv   rw   rY   r   r?   rx   ry   r@   namesr|   r5   r   templater{   s         r"   create_get_args_noder      s    = = = edE]+++++%e,,D z||rzz*4733H
 &';<<#XXe__   H
 :hoh//005a88r,   c                   d }t          | t          t          f          sJ t          |           }| s
 |            S |g k    rd}nt	          |d                   }d}|                    t          j        t                    t          |d
                    |                     }t          j        t          j        |                    j        d         S )z
    Create set_args function as follows:

        def set_args_0(__args):
            nonlocal x, y
            x, y = __args
    c                     dt          j        t                     dt           d} t	          j        t          j        |                     j        d         S )Nru   (z):
            pass
        r   )	r   rv   SET_ARGS_FUNC_PREFIX	ARGS_NAMEr   r?   rx   ry   r@   rz   s    r"   r|   z(create_set_args_node.<locals>.empty_node   sX    !"677 :C   z(/(33449!<<r,   r}   r   zS
    def {func_name}({args}):
        {nonlocal_vars}
        {vars}, = {args}
    rX   )r~   r^   r   r   )rE   r   r   r   r   rT   r   rv   r   r   rY   r   r?   rx   ry   r@   r   s         r"   create_set_args_noder      s    = = = edE]+++++%e,,D z||rzz*4733H
 &';<<#XXe__	   H :hoh//005a88r,   c                z   t          | t          t          f          sJ t          t          d |                     }t          t          d |                    }t	          ||j                  } | sg S d                    d                    |                     }t          j	        |          j
        d         gS )Nc                
    d| vS N.r=   ns    r"   <lambda>z,create_nonlocal_stmt_nodes.<locals>.<lambda>   
    3a< r,   c                
    d| vS )N[r=   r   s    r"   r   z,create_nonlocal_stmt_nodes.<locals>.<lambda>   r   r,   )keyznonlocal {}rX   r   )rE   r   r   filtersortedr   rT   rY   r   r?   r@   )r   mappedrB   s      r"   r   r      s    edE]+++++&//7788F&//8899FFL  E  	$$SXXe__55IJy!!&q)**r,   Fc                J   t          | t                    r| g} t          | t          t          t          f          st          dt          |                      fdfd| D             }t          |          dk    r|s	|d         }nt          j	        |          }|S )am  
    If name_ids is list or tuple or set with multiple strings, this function
    generates gast.Tuple of gast.Name.
    If the name_ids is single string or contains only 1 string, this function
    returns gast.Name if gen_tuple_if_single==False else returns gast.Tuple
    with only one gast.Name

    This function is used at several gast.Return statements.
    z4name_ids must be list or tuple or set, but received c                    d| vrt          j        | d d           S t          j        |           j        d         j        S )Nr   )idrm   
annotationra   r   )r   Namer?   r@   rG   )rA   rm   s    r"   create_node_for_namez0generate_name_node.<locals>.create_node_for_name   sK    d??9ST    z$$Q'--r,   c                &    g | ]} |          S r=   r=   )rV   name_idr   s     r"   rW   z&generate_name_node.<locals>.<listcomp>  s%    HHHG&&w//HHHr,      r   )eltsrm   )
rE   strr   r   set	TypeErrortypelenr   Tuple)rZ   rm   gen_tuple_if_single
gast_names	name_noder   s    `   @r"   re   re      s     (C   :huc 233 
S4>>SS
 
 	
. . . . . IHHHxHHHJ
:!$7qM		JJC888	r,   c                    t          | t          j                  s
J d            t          |                                           S )Nz3Input non-Attribute node to get attribute full name)rE   r   	Attributer   stripr5   s    r"   get_attribute_full_namer     sG    dDN++  = + d##))+++r,   c                   t          | t          j                  s
J d            | j        }t          |t          j                  r!|j        }t          |t          j                  !t	          |                                          }	 dd l}dd lmc m	} ddlm
} t          |||d}t          ||          }t          ||          S # t          $ r Y dS w xY w)Nz(Input non-Call node for is_api_in_moduler   )	to_tensor)nppaddle_jstr   F)rE   r   Callfuncr   r   r   paddle.jit.dy2staticjit	dy2staticr   r   evalr	   	Exception)	r5   module_prefix	func_nodefunc_strr   r   r   globalsfns	            r"   is_api_in_moduler     s   dDI&&  2 & 	I
Y	
*
* #N	 Y	
*
* # "),,2244H+++++++++$$$$$$ "	
 
 (G$$&r=999   uus   	>C 
CCc                ,    t          | t                    S r'   )r   r   r   s    r"   is_paddle_apir   3  s    D"6777r,   c                  P    e Zd Zd Zd Zd Zd Zd Zd Zd Z	d Z
d	 Zd
 Zd ZdS )	NameScopec                    t                      | _        t                      | _        t                      | _        d| _        t                      | _        t                      | _        t                      | _        dS )a(  
        A NameScope is a object which manager all the variable names.
        only FunctionDef and Controlflow node will have a namescope property.

        type can be "function" and "controlflow"

        we don't analyze the read only variable because they don't affect the analysis.
        N)r   r   	nonlocalsr^   fatherw_varscreatedpush_pop_varsr)   s    r"   r+   zNameScope.__init__8  sV     uuEE	eeuu !UUr,   c                    || _         d S r'   )r   )r*   r   s     r"   
set_fatherzNameScope.set_fatherK  s    r,   c                |    | j         | j        z
  | j        z
  | j        z
  }t	          t          d |                    S )zWvars existing in current scope.
        they must not contain qualified names.
        c                
    d| vS r   r=   )xs    r"   r   z(NameScope.existed_vars.<locals>.<lambda>S  s
    CqL r,   )r   r   r   r^   r   r   )r*   
local_varss     r"   existed_varszNameScope.existed_varsN  s<     [4</$.@49L
600*==>>>r,   c                    | j         S r'   )r   r)   s    r"   created_varszNameScope.created_varsU  s
    |r,   c                    | j         S r'   )r   r)   s    r"   modified_varszNameScope.modified_varsX  s
    {r,   c           
         g }| j         D ]`}|                     |          r4|                     |          rt          j        d| d| d| d           K|                    |           at          |          S )a  
        At present, we do not support global append, such as

        import numpy as np
        a = []
        def func():
            a.append() # global names `a`, we will raise a warning.
            p.append(a, 1) # global names `np`, we will raise a warning.
        zFind variable `z$` defined in global scope and call `z.append() or zJ.pop()`, which will be ignored and never be transferred into tensor array.)r   _is_simple_nameis_global_varwarningswarnr/   r   )r*   non_global_push_pop_namesvars      r"   variadic_length_varszNameScope.variadic_length_vars\  s     %'!% 		6 		6C##C(( 6T-?-?-D-D 6&c & &"%& &47& & &    *005555,---r,   c                J    | j         }| j        j        |z  f}|| j         |z
  dS )N)globalnonlocal)r   r   global_vars)r*   valid_namestmps      r"   control_flow_varszNameScope.control_flow_varss  s0    k{&464;+<===r,   c                    d|v sd|v rdS dS )Nr   r   FTr=   r*   rA   s     r"   r   zNameScope._is_simple_namex  s    $;;#++5tr,   c                    |                      |          s
J d            | }|'||j        v rdS ||j        |j        z  v rdS |j        }|'dS )a  
        Return whether the name is a var created in global scope.
        Search from bottom to top. If it is not created or modified,
        it means global vars; otherwise, it means local vars.
        Only valid after FunctionNameLivenessAnalysis visitor.
        z5is_global_var accept a simple name, but get `{name}`.NTF)r   r   r   r   r   )r*   rA   ancestors      r"   r   zNameScope.is_global_var}  s     ##D)) 	
 	
C	
 	
) "x'''t*X_<==uH " tr,   c                .    |                      |           S r'   )r   r   s     r"   is_local_varzNameScope.is_local_var  s    %%d++++r,   c                    | xj         |j         z  c_         | xj        |j        z  c_        | xj        |j        z  c_        | xj        |j        z  c_        | xj        |j        z  c_        d S r'   )r   r   r^   r   r   )r*   
name_scopes     r"   
merge_fromzNameScope.merge_from  sj    
***..		Z_$		z((j66r,   N)r1   r:   r;   r+   r   r   r   r   r   r   r   r   r   r   r=   r,   r"   r   r   7  s        # # #&  ? ? ?    . . ..> > >
  
  &, , ,7 7 7 7 7r,   r   c                      e Zd ZdZd Zd Zd Zd Zd Zd Z	d Z
d	 Zd
 Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd ZdS )FunctionNameLivenessAnalysisa  analyze the liveness of a function.

    every variables stored in this scope will be collected,
    in addition with global/nonlocal information and
    push_pop information.

    1. global variable is stored in node.var_globals.
    2. nonlocal variable is stored in node.var_nonlocals.
    3. arguments is stored in node.var_args.
    4. if a variable's push and pop attribute is called,
       it will be collected in push_pop_vars. They are
       used for transformation to tensor_array.
       NOTE: push_pop_vars **may not** in w_vars.
       a.push(0) don't modify the variable a, but the content
       of a.

    For example:

    def func(*args, **kargs):
        a = 12
        global i,j
        nonlocal x,y
        print(a)
        i = k
        b = []
        c = [1,2,3]
        for m in range(10):
            q = 12
            b.push(1)
            c.pop()

    After this visitor we have:
    # node is the FunctionDef node with name: "func"
    node.pd_scope = NameScope(
        globals = ['i', 'j'],
        nonlocals = ['x', 'y'],
        args = ['args', 'kargs'],
        wr_vars = ['a', 'i', 'q', 'm', 'c', 'b']
        push_pop_vars = ['b', 'c']
    )
    c                >    g | _         |                     |           d S r'   )scope_node_stackr9   )r*   	root_nodes     r"   r+   z%FunctionNameLivenessAnalysis.__init__  s"     "

9r,   c                ,    t                      |_        d S r'   )r   pd_scoper*   r5   s     r"   _reset_name_scopez.FunctionNameLivenessAnalysis._reset_name_scope  s    !r,   c                V    t          |d          st                      |_        |j        S )Nr   )hasattrr   r   r   s     r"   _get_name_scopez,FunctionNameLivenessAnalysis._get_name_scope  s'    tZ(( 	(%KKDM}r,   c                B    |                      | j        d                   S r   )r   r   r)   s    r"   _current_name_scopez0FunctionNameLivenessAnalysis._current_name_scope  s    ##D$9"$=>>>r,   c                v    t          | j                  dk    rd S |                     | j        d                   S )Nr   )r   r   r   r)   s    r"   _father_name_scopez/FunctionNameLivenessAnalysis._father_name_scope  s9    t$%%**4##D$9"$=>>>r,   c                    t          | j                  dk    rd S | j        dd d         D ]3}t          |t          j                  r|                     |          c S 4d S )Nr   r  r   )r   r   rE   r   rf   r   r   s     r"   _nearest_function_scopez4FunctionNameLivenessAnalysis._nearest_function_scope  sx    t$%%**4)"&b&1 	2 	2D$ 011 2++D111112	2 	2r,   c                    dS )z[ i for i in range(10) ]
        In this case, `i` will not created in FunctionScope.
        We don't collect `i` by not calling generic_visit.
        Nr=   r   s     r"   visit_ListCompz+FunctionNameLivenessAnalysis.visit_ListComp  s	    
 	r,   c                    dS )zthe same as ListComp.Nr=   r   s     r"   visit_DictCompz+FunctionNameLivenessAnalysis.visit_DictComp  s    r,   c                   |                      |           t          j        t          j        t          j        f}t          |j        |          r3|                                 j        	                    |j
                   d S d S r'   )r3   r   ro   AugStoreDelrE   rm   r   r   addr   r*   r5   write_contexts      r"   
visit_Namez'FunctionNameLivenessAnalysis.visit_Name  sq    4   T]DH=dh.. 	;$$&&-11$':::::	; 	;r,   c                P      fd} fd}                      ||           d S )Nc                                                      xj        t                                                   z  c_        d S r'   )r   r^   r   _get_argument_namesr5   r*   s   r"   pre_funcz@FunctionNameLivenessAnalysis.visit_FunctionDef.<locals>.pre_func  sF    $$&&++s((..0 0 ++++r,   c                    t           t           t          t          t          t          gfd}                                 r~ |             rv                                xj                                        j        z  c_                                        xj                                        j        z  c_        dS dS dS )a  NOTE: why we need merge w_vars and push_pop_vars here ?
            because we do ifelse_transformer after loop_transformer. Loops will changed into functions. but we know this function will be called in if. so we add w_vars to father function scope.
            c                 L    D ]} j                             |           r dS  dS )NTF)rA   
startswith)prefixcontrol_flow_function_defr5   s    r"   is_control_flow_def_nodezcFunctionNameLivenessAnalysis.visit_FunctionDef.<locals>.post_func.<locals>.is_control_flow_def_node  s;    7 $ $Fy++F33 $#tt$ur,   N)	WHILE_BODY_PREFIXFOR_CONDITION_PREFIXFOR_BODY_PREFIXTRUE_FUNC_PREFIXFALSE_FUNC_PREFIXr  r   r   r   )r  r  r5   r*   s    @r"   	post_funczAFunctionNameLivenessAnalysis.visit_FunctionDef.<locals>.post_func  s    
 "!$ !)%      &&(( -E-E-G-G ''))00,,..500 ''))77,,..<7777	   r,   _visit_scope_noder*   r5   r  r!  s   ``  r"   visit_FunctionDefz.FunctionNameLivenessAnalysis.visit_FunctionDef  s`    	 	 	 	 	 	
	 	 	 	 	 	6 	tXy99999r,   c                b   |                      |           | j                            |           |                                                     |                                            |r
 |             |                     |           |r
 |             | j                                         dS )zQscope node main visit logic.
        pre_func and post_func is callbacks
        N)r   r   r/   r   r   r  r3   r4   r$  s       r"   r#  z.FunctionNameLivenessAnalysis._visit_scope_node  s     	t$$$$$T***  ""--d.J.J.L.LMMM 	HJJJ4    	IKKK!!#####r,   c                P      fd} fd}                      ||           d S )Nc                                                                                                                                                                                                                                                                                    j        z
                                  _                                        xj                                        j        z  c_        d S r'   )r  r   r   r  r   before_createdr   r  s   r"   r!  zGFunctionNameLivenessAnalysis._visit_controlflow_node.<locals>.post_func%  s    ##%%001I1I1K1KLLL((**55((**   ,,..;;==%& $$&&.
 ((**22((**22222r,   c                 ^                                                                      _        d S r'   )r  r   r)  r  s   r"   r  zFFunctionNameLivenessAnalysis._visit_controlflow_node.<locals>.pre_func3  s)    "&">">"@"@"M"M"O"ODr,   r"  )r*   r5   r!  r  s   ``  r"   _visit_controlflow_nodez4FunctionNameLivenessAnalysis._visit_controlflow_node$  sf    	 	 	 	 	 		P 	P 	P 	P 	P 	P 	tXy99999r,   c                0    |                      |           d S r'   r+  r   s     r"   	visit_Forz&FunctionNameLivenessAnalysis.visit_For8      $$T*****r,   c                0    |                      |           d S r'   r-  r   s     r"   visit_Whilez(FunctionNameLivenessAnalysis.visit_While;  r/  r,   c                0    |                      |           d S r'   r-  r   s     r"   visit_Ifz%FunctionNameLivenessAnalysis.visit_If>  r/  r,   c                n    |                                  xj        t          |j                  z  c_        d S r'   )r   r   r   r   r   s     r"   visit_Globalz)FunctionNameLivenessAnalysis.visit_GlobalA  s0      ""**c$*oo=****r,   c                n    |                                  xj        t          |j                  z  c_        d S r'   )r   r   r   r   r   s     r"   visit_Nonlocalz+FunctionNameLivenessAnalysis.visit_NonlocalD  s0      "",,DJ?,,,,r,   c                >   |                      |           t          j        t          j        t          j        f}t          |j        |          rOt          |                                          }| 	                                j
                            |           d S d S r'   )r3   r   ro   r  r  rE   rm   r   r   r   r   r  )r*   r5   r  rA   s       r"   visit_Attributez,FunctionNameLivenessAnalysis.visit_AttributeG  s    4   T]DH=dh.. 	8%d++1133D$$&&-11$77777	8 	8r,   c                   |                      |           t          j        t          j        t          j        f}t          |j        |          rt          |j        t          j                  r&|j        }t          |j        t          j                  &t          |j        t          j	                  r:| 
                                j                            |j        j                   d S d S d S r'   )r3   r   ro   r  r  rE   rm   rG   	Subscriptr   r   r   r  r   r  s      r"   visit_Subscriptz,FunctionNameLivenessAnalysis.visit_SubscriptN  s    4   T]DH=dh.. 	ETZ88 "z TZ88 "$*di00 E((**155djmDDDDD		E 	EE Er,   c                H   |                      |           t          |j        t          j                  sd S ddg}|j        j        |vrd S t          |j        j                                                  }| 	                                j
                            |           d S )Nr/   r4   )r3   rE   r   r   r   attrr   rG   r   r   r   r  )r*   r5   variadic_length_methodrA   s       r"   
visit_Callz'FunctionNameLivenessAnalysis.visit_CallW  s    4   $)T^44 	F"*E!29>!777F!$)/2288::  ""044T:::::r,   c                   t          |t          j                  s
J d            t          |j        j                  }|                    |j        j                   |                    |j        j                   d |D             }|S )zget all arguments name in the functiondef node.
        this node is local to the function and shouldn't
        be created.
        z&Input node is not function define nodec                     g | ]}||j         S r'   )r   )rV   is     r"   rW   zDFunctionNameLivenessAnalysis._get_argument_names.<locals>.<listcomp>m  s    666!r,   )rE   r   rf   r   r^   r/   varargkwarg)r*   r5   r   s      r"   r  z0FunctionNameLivenessAnalysis._get_argument_namesb  s    
 $ 011 	
 	
4	
 	
1 TY^$$TY%&&&TY_%%%66u666r,   N)r1   r:   r;   r<   r+   r   r   r   r  r  r  r	  r  r%  r#  r+  r.  r1  r3  r5  r7  r9  r<  r@  r  r=   r,   r"   r   r     sa       ( (T  $ $ $  
? ? ?? ? ?
2 2 2    ; ; ;!: !: !:F$ $ $: : :(+ + ++ + ++ + +> > >@ @ @8 8 8E E E	; 	; 	;    r,   r   )4
__future__r   rc   rx   r   numpyr   paddle.baser   paddle.jit.dy2static.ast_utilsr   paddle.utilsr   utilsr   r	   rw   r   r   r  r   FOR_ITER_INDEX_PREFIXFOR_ITER_TUPLE_PREFIXFOR_ITER_TARGET_PREFIXFOR_ITER_ITERATOR_PREFIXFOR_ITER_TUPLE_INDEX_PREFIXFOR_ITER_VAR_LEN_PREFIXFOR_ITER_VAR_NAME_PREFIXFOR_ITER_ZIP_TO_LIST_PREFIXWHILE_CONDITION_PREFIXr  r  r  r#   NodeVisitorr%   rC   rH   rO   r\   rk   rr   r   r   r   Loadre   r   r   r   r   r   r=   r,   r"   <module>rW     sz   # " " " " "        # # # # # # = = = = = =       A A A A A A A A! ! 	  . / 1 5 ; . 0 3 *   + !      d&   *) ) )
$ $ $  0 0 0  .
  
  
 "9 "9 "9J#9 #9 #9L+ + + &/TY[[e    @, , ,  <8 8 8a7 a7 a7 a7 a7 a7 a7 a7HS S S S S4#3 S S S S Sr,   