
    ϑi                      S SK Jr  S SKrS SKrS SKrS SKrS SKrS SKrS SKJ	r	J
r
JrJrJr  S SKJrJr  S SKrS SKJs  Jr  S SKJrJr  S SKJrJr  S SKJr  S SKJrJrJr  S S	K J!r!J"r"  S S
K#J$r$  S SK%J&r&  S SK'J(r(J)r)  SSK*J+r+J,r,  SSK-J.r.J/r/J0r0J1r1  SSK2J3r3J4r4J5r5  SSK6J7r7J8r8  SSK9J7r:J8r;  SSK<J=r=  SSK>J?r?J@r@JArAJBrBJCrCJDrDJErEJFrFJGrGJHrHJIrIJJrJJKrKJLrL  \	(       a  S SKMJNrN  S SKOJPrPJQrQ  S SKRJSrS  \" S5      rT\" S5      rU/ rVSrWS rXS rYSAS jrZ " S S5      r[\R                  " 5       r]\[" 5       r^S r_ " S  S!5      r`S" ra " S# S$\\U\T4   5      rbS% rc " S& S'\b5      rd " S( S)\b\U\T4   5      reS* rf " S+ S,5      rg " S- S.5      rhS/ ri " S0 S15      rj " S2 S35      rk " S4 S5\;5      rl " S6 S7\;5      rm " S8 S95      rn " S: S;\85      ro " S< S=5      rpSBS> jrq\\r" 5       \r" 5       S?S?4S@ j5       rsg)C    )annotationsN)TYPE_CHECKINGAnyCallableGenericTypeVar)	ParamSpecSelf)decomposition	get_flags)core	framework)
check_type)param_guardswitch_to_static_graphto_static_mode_guard)in_dynamic_modeuse_pir_api)layers)Value)flattengast   )errorlogging_utils)FunctionSpec_hash_spec_namesget_buffersget_parameters)attach_origin_info!create_and_update_origin_info_map$update_op_callstack_with_origin_info)PartialProgramLayerPartialProgramLayerHook)DygraphToStaticAst)ALREADY_D2SNO_SHAPE_VAR_TYPETransformOptionsast_to_funcbackend_guard*cuda_pinned_tensors_move_to_excepted_placefunc_to_source_codegraph_tracing_guardinput_specs_compatibleis_paddle_funcmake_hashableprim_or_cinn_is_enabled	type_nameuse_specialized_device)NestedSequence)	InputSpecProgram)
AmpOptions_RetT_InputT
   c                H   ^  [         R                  " 5       T l        U 4S jnU$ )Nc                 b   > TR                      T" U 0 UD6sS S S 5        $ ! , (       d  f       g = fN)__lock__)argskwargsfuncs     g/var/www/html/banglarbhumi/venv/lib/python3.13/site-packages/paddle/jit/dy2static/program_translator.py	lock_funcsynchronized.<locals>.lock_func_   s!    ]](( ]]s    
.)	threadingLockr>   )rA   rC   s   ` rB   synchronizedrG   \   s    NN$DM)     c                    U R                   nSS1nSn[        U5       H7  u  pEUR                  5       U;   d  M  SR                  XS-   S  5      nSU S3nM9     [	        U SU R                  5        S	35      e)
Nzoutputs = static_func(*inputs)z)outputs = static_func(*inputs, **_kwargs) 
r   zIn transformed code:

z

z0Sorry about what's happened. In to_static mode, aL  's output variable is a viewed Tensor in dygraph. This will result in inconsistent calculation behavior between dynamic and static graphs. You must find the location of the strided ops be called, and call paddle.assign() before inplace input. If you certainly make sure it's safe, you can set env stride_in_no_check_dy2st_diff to 1.)	callstack	enumeratestripjoin
ValueErrorname)opop_callstacktarget_linesop_callstack_messageindexlineop_callstack_results          rB   show_op_callstackrY   f   s    <<L(3L  .::<<'"&))L,E"F*+>*?tD ! / 
  PQSQXQXQZP[ \d 	e rH   c                   / SQnS nS nU R                  5       nU H  n/ n[        UR                  5       5       H  n[        R                  R
                  R                  U5      nU" Xu5      (       a  UR                  U5        U" XuU5      (       d  MZ  UR                  5       U;   a  Mp  UR                  5       R                  5       [        R                  ;   a  [        U5        [        U5      S:X  a  M  [        U5        M     M     g)z
check viewed value used by inplace op in pir mode.

Two scenarios will raise ValueError:
    # one
    a = transpose(b)
    a.add_(c)
    # two
    a = transpose(b)
    b.add_(c)
)zpd_op.set_value_zpd_op.set_value_with_tensor_zpd_op.share_data_c                    U R                  5       [        R                  ;   =(       a     U R                  S5      R	                  U5      $ Nr   )rQ   r   
stride_opsoperand_sourceis_same)rR   vals     rB   val_is_used_by_stride_op@check_view_api_used_by_inplace.<locals>.val_is_used_by_stride_op   s9    wwyI000 R5F5F6

'#,	rH   c                   ^ ^ T R                  5       R                  S5      =(       a$    [        U U4S jUR                  5        5       5      $ )N_c              3  d   >#    U  H%  nTR                  U5      R                  T5      v   M'     g 7fr=   )r^   r_   ).0rV   rR   r`   s     rB   	<genexpr>Pcheck_view_api_used_by_inplace.<locals>.is_used_by_inplace_op.<locals>.<genexpr>   s-      /
?LeBe$,,S11}s   -0)rQ   endswithanyvalues)rR   r`   infos   `` rB   is_used_by_inplace_op=check_view_api_used_by_inplace.<locals>.is_used_by_inplace_op   s;    wwy!!#& 
3 /
?C{{}/
 ,
 	
rH   r   N)	list_varsreversedall_used_opspaddler   pirget_op_inplace_infoappendrQ   get_defining_opr   r]   rY   len)	programskipped_inplace_opsra   rm   all_vars_listvalueused_by_stride_opsrR   inplace_infos	            rB   check_view_api_used_by_inplacer~   {   s    


 %%'M5--/0B!;;??>>rBL'22"))"-$R==779 33((*//1Y5I5II%b))*a/!"% 1 rH   c                  0    \ rS rSrSrS rS rS rS rSr	g)	FunctionCache   zW
Caches the transformed functions to avoid redundant conversions of the same function.
c                d    [         R                  " 5       U l        0 U l        [	        5       U l        g r=   )weakrefWeakKeyDictionary_converted_static_func_caches_code_to_ast_cachesr%   _dygraph_to_staticselfs    rB   __init__FunctionCache.__init__   s&    -4-F-F-H*#% "4"6rH   c                    U R                   R                  US5      nUc  U R                  U5      nX R                   U'   U$ )zW
Returns the cached static function or converts it when first encounters the function.
N)r   get_convert)r   rA   static_funcs      rB   convert_with_cache FunctionCache.convert_with_cache   sD    
 88<<T4H---K7B..t4rH   c                J   [         R                  " U5      n[        U5      nX R                  ;   a  U R                  U   nOJ[        R
                  " U5      n[        X15      nU R                  R                  U5      nX0R                  U'   [        X15      u  pE[        X45        U$ )a  
Converts dygraph function into static function. For two functions with same dedent code,
the second function will reuse the transformed ast node of previous one.

For example:
    # A.py
    def foo(x, y):
        z = x + y
        return z

    # B.py
    def foo(x, y):
        z = x + y
        return z

If the conversion of A.foo happens after B.foo, it will reuse the transformed ast node of B.foo
to speed up the conversion.
)inspectunwrapr,   r   r   parser    r   get_static_astr)   r!   )r   rA   source_coderootr   	file_names         rB   r   FunctionCache._convert   s    & ~~d#)$/ 222++K8D::k*D%d1D**99$?D48$$[1 "-T!8)$<rH   c                    XR                   ;   $ r=   )r   )r   rA   s     rB   existFunctionCache.exist   s    9999rH   )r   r   r   N)
__name__
__module____qualname____firstlineno____doc__r   r   r   r   __static_attributes__ rH   rB   r   r      s    7&P:rH   r   c                   [        U [        S5      (       a  U $ [        R                  " U [        R                  R
                  5      (       + =(       d    [        U 5      nU(       a)  [        R                  " U 5      (       a  U R                  $ U $ [           [        R                  U 5      n[        U[        S5        UsSSS5        $ ! , (       d  f       g= f)a  
Transforms function of dygraph into static function using the cache mechanism.

Note(dev): It will return function.__func__ if encountering class method.

Args:
    function(callable): The function with dygraph layers that will be converted into static layers.
NT)getattrr&   r(   check_fn_need_transformToStaticModeASTr/   r   ismethod__func___CACHE_LOCK_FUNCTION_CACHEr   setattr)function	need_skipr   s      rB   convert_to_staticr      s     xd++
 44&3377
 	
" 
	!	 
 $+$4$4X$>$>x  LHL	%88B[$/ 
s   (C
Cc                  N    \ rS rSrSr/ SQrS r\S 5       rS r	S r
S rS	 rS
rg)CacheKeyi  z
Cached key for ProgramCache.
)function_specinput_args_with_specinput_kwargs_with_specclass_instancer@   _spec_names_id
_pir_flagsc                    Xl         X l        X0l        X@l        XPl        [        X#5      U l        [        S5      S   =(       d    [        S5      S   U l        g)a  
Initializes a cache key.

Args:
    functions_spec(FunctionSpec): a FunctionSpec instance of decorated function.
    input_args_with_spec(list[InputSpec]): actual input args with some arguments replaced by InputSpec.
    input_kwargs_with_spec(list[{string:InputSpec}]): actual input kwargs with some arguments replaced by InputSpec.
    class_instance(object): a instance of class `Layer`.
    **kwargs(dict): manage other arguments used for better scalability
FLAGS_enable_pir_in_executor!FLAGS_enable_pir_with_pt_in_dy2stN)	r   r   r   r   r@   r   r   r   r   )r   r   r   r   r   r@   s         rB   r   CacheKey.__init__  sc    $ +$8!&<#,. 
 45.  <=3	 	rH   c                    U(       a'  [        US   [        R                  5      (       a  USS nUR                  X#5      u  pVUR	                  XV5      u  nn[        UUUU5      $ )a/  
Generated a CacheKey instance by given inputs.

Args:
    functions_spec(FunctionSpec): a FunctionSpec instance of decorated function.
    args(tuple): tuple of actual inputs arguments.
    kwargs(dict): dict of actual inputs keyword arguments.
    class_instance(object): a instance of class `Layer`.
r   r   N)
isinstancer   Layerunified_args_and_kwargsargs_to_input_specr   )	clsr   r?   r@   r   _args_kwargsr   r   s	            rB   from_func_and_argsCacheKey.from_func_and_argsB  sn     JtAw558D&>>tL ,,U<	
 "  "	
 	
rH   c                `   SnU R                   R                  SS5      nU R                   R                  SS5      n[        [        U R                  5      [        U R                  U5      [        U R                  U5      U R                  U R                  UUU R                  [        5       4	5      $ )NzoArguments to a `@paddle.jit.to_static` must be a hashable Python objects (or nested structures of these types).	with_hookFis_train)r@   r   hashidr   r0   r   r   r   r   r   r   )r   	error_msgr   r   s       rB   __hash__CacheKey.__hash___  s     F	KKOOK7	;;??:u54%%&d77Cd999E####

 	
rH   c                j    [        U 5      [        U5      L =(       a    [        U 5      [        U5      :H  $ r=   )typer   r   others     rB   __eq__CacheKey.__eq__q  s'    T
d5k)HtDzT%[/HHrH   c                    X:X  + $ r=   r   r   s     rB   __neq__CacheKey.__neq__t  s      rH   c                    S[        U R                  5       SU R                   SU R                   SU R                   3$ )Nzid(function_spec): z, input_args_with_spec: z, input_kwargs_with_spec: z, class_instance: )r   r   r   r   r   r   s    rB   __repr__CacheKey.__repr__w  sv    $R(:(:%;$<<TUYUnUnTo  pJ  KO  Kf  Kf  Jg  gy  z~  zM  zM  yN  O  	OrH   )r   r   r   r   r   r   r@   N)r   r   r   r   r   	__slots__r   classmethodr   r   r   r   r   r   r   rH   rB   r   r     s>    I"
H 
 
8
$I!OrH   r   c                    / nU n [        U[        5      (       aI  UR                  U5        UR                  nUb  UR                  R                  U5      nOUR                  nO X4$ Md  )zO
Unwraps a decorated function and returns the decorator list and inner target.
)r   StaticFunctionru   r   dygraph_function__get__)rA   
decoratorscurinstances       rB   unwrap_decoratorsr   {  sn     J
C
c>**c"))H#**228<**? rH   c                     \ rS rSrSS jr\SS j5       rSS jrSS jrS r	S S jr
S!S	 jrSS
 jr      S!S jrS r      S"S jrS rS r\S#S j5       r\S 5       r\S$S j5       r\S%S j5       r   S&     S'S jjrS$S jrS r\S(S j5       r\S(S j5       r\S)S j5       r\S*S j5       r\S+S j5       rSrg),r   i  Nc                Z   [         R                  " U5      (       a  UR                  U l        [        R
                  " UR                  5      U l        [        U R                  S5      (       d  [        S5      eU R                  U R                  R                  UR                  '   OXl        SU l        X l        [        X5      U l        [!        5       U l        [        R$                  " 5       U l        [)        5       U l        X0l        SU l        UR1                  SS5      U l        SU l        g)aJ  
Initializes a `StaticFunction`.

Args:
    function(callable): A function or method that will be converted into static program.
    input_spec(list[InputSpec]): list of InputSpec to specify the `shape/dtype/name` information for each input argument, default None.
    **kwargs(dict): other arguments like `build_strategy` et.al.
_original_funcscWhen using 'to_static' to convert method of a class, please ensure the class inherits from nn.LayerNTpropertyF)r   r   r   _dygraph_functionr   ref__self___class_instancehasattrr   	TypeErrorr   r   _input_specr   _function_specProgramCache_program_cacher   _descriptor_cacheProgramTranslator_program_transr   	_trainingr   	_property_patched_name)r   r   
input_specr@   s       rB   r   StaticFunction.__init__  s     H%%%-%6%6D"#*;;x/@/@#AD 4..0ABBE 
 && //0A0AB &."#'D %*8@*n!(!:!:!</1J6!rH   c                    U R                   $ r=   )r   r   s    rB   is_propertyStaticFunction.is_property  s     ~~rH   c                    [        U R                  [        R                  5      (       a2  U R                  R                  SL a  [        SU R                   S35      eSU l        g )NFzFailed to switch train mode. zD is a Layer's method, please use Layer.train() to switch train mode.Tr   r   r   r   trainingRuntimeErrorr   r   r   s    rB   trainStaticFunction.train  s^    t**FLL99##,,5/0E0E/F GA A  rH   c                    [        U R                  [        R                  5      (       a2  U R                  R                  SL a  [        SU R                   S35      eSU l        g )NTzFailed to switch eval mode. zB is a Layer's method, please use Layer.eval() to switch eval mode.Fr  r   s    rB   evalStaticFunction.eval  s\    t**FLL99##,,4.t/D/D.E F? ?  rH   c                   XR                   ;  a  Uc  U $ U R                  5       n[        U[        R                  5      (       ap  [        US5      (       a_  U R                  R                  UR                  R                  5       ;  a-  U R                  UR                  U R                  R                  '   [        R                  " U5      Ul        X0R                   U'   U R                   U   $ )a>  
Overrides this method to parse the class instance and call bound method correctly.

For example:

    '''
    class Net(Layer):
        def __init__(self):
            pass

        @paddle.jit.to_static
        def forward(self, x, y):
            return x + y

    net = Net()
    out = net(x, y)
    '''

In above case, `net(x, y)` will call `net.forward(x, y)` firstly that is a bound method
of `Net` instance. After decorated by `@paddle.jit.to_static`, it will firstly to call `__get__`
to parse the class instance correctly instead of the `StaticFunction` instance.
r   )r   _cloner   r   r   r   r   r   r   keysr   r   r   )r   r   ownernew_static_layers       rB   r   StaticFunction.__get__  s    . 111  ${{}8V\\22H&788**33//4467 ** (()?)?)H)HI 07{{8/D,/?""8,%%h//rH   c                f    U R                   " U R                  U R                  40 U R                  D6$ r=   )	__class__r   r   r   r   s    rB   r
  StaticFunction._clone  s0    ~~!!4#3#3
7;||
 	
rH   c                B   U R                   (       a  U R                  " U0 UD6$ U R                  R                  (       d(  [        R
                  " S5        U R                  " U0 UD6$ [        5       (       d  [        SU R                   S35      eU R                  " U0 UD6$ )a/  
Supports to call the returned instance with input `args` and `kwargs` directly.

Args:
    *args(tuple): tuple of all input arguments from original decorated function.
    **kwargs(dict): dict of all input keyword arguments from original decorated function.

Return:
    Outputs of decorated function.
zThe decorator '@paddle.jit.to_static' does NOT work when setting 'paddle.jit.enable_to_static' to False. We will just return dygraph output. If you would like to get static graph output, please call API paddle.jit.enable_to_static(True)z"Failed to run the callable object z decorated by '@paddle.jit.to_static', because it is NOT in dynamic mode. Please disable the static graph mode to enter dynamic mode with the following API: paddle.disable_static().)
r   _call_dygraph_functionr   enable_to_staticr   warnr   r  r   _perform_callr   r?   r@   s      rB   __call__StaticFunction.__call__  s     >>..??? ""33
 4
 ..???  4T5J5J4K L: :  !!42622rH   c                    U R                   b<  [        U R                   S5      (       d  [        S5      eU R                   R                  $ U R                  $ )Nr  r   )r   r   r   r  r   r   s    rB   _is_train_modeStaticFunction._is_train_mode&  sR    *4..
;;E  &&///>>!rH   c                &    U R                   " U0 UD6$ )a  
Calls dygraph function directly and returns the outputs.

Args:
    *args(tuple): tuple of all input arguments from original decorated function.
    **kwargs(dict): dict of all input keyword arguments from original decorated function.

Return:
    Outputs of dygraph function.
)r   r  s      rB   r  %StaticFunction._call_dygraph_function1  s     $$d5f55rH   c                <    U R                   (       a  [        S5      eg)zkraise RuntimeError when property=True

Raises:
    RuntimeError: can not call this func when property=True
z)Can not call the func when property=True.N)r   r  r   s    rB   _raise_when_property#StaticFunction._raise_when_property@  s     JKK rH   c                    [        S5      eNzNot implemented yet.NotImplementedErrorr  s      rB   get_concrete_program#StaticFunction.get_concrete_programI  s     ""899rH   c                    [        S5      er#  r$  )r   
cached_keys     rB   #get_concrete_program_with_cache_key2StaticFunction.get_concrete_program_with_cache_keyN      !"899rH   c                    [        S5      er#  r$  r   s    rB   get_traced_countStaticFunction.get_traced_countQ  r,  rH   c                    [        S5      er#  r$  r   s    rB   codeStaticFunction.codeT      !"899rH   c                v    U R                   c  g U R                  5       c  [        S5      eU R                  5       $ )NzFThe instance of class has been deleted, please re-create the instance.)r   r  r   s    rB   r   StaticFunction.class_instanceX  sA    '!)X  ##%%rH   c                ~    U R                   b%  U R                  R                  U R                   5      $ U R                  $ )z*
Returns the original decorated function.
)r   r   r   r   s    rB   r   StaticFunction.dygraph_functionb  s9    
 *))11$2E2EFF)))rH   c                    [        S5      er#  r$  r   s    rB   concrete_programStaticFunction.concrete_programl  r3  rH   c                    [        S5      er#  r$  )r   r   r   is_prim_infers       rB   #concrete_program_specify_input_spec2StaticFunction.concrete_program_specify_input_specp  s     ""899rH   c                   U R                   c  U R                  $ U R                  b  U R                  OU R                  R                  nXR                   R                  ;   d"   SU SU R                   R
                   S35       eU R                   R                  U   n[        U R                   XR                  U R                   5      5        [        U R                   U5      $ )aG  
Rollback into original dygraph functions for current class instance.

Returns:
    Function or Method

Examples:
    .. code-block:: python

        >>> # doctest: +SKIP('`paddle.jit.to_static` can not run in xdoctest')
        >>> import paddle

        >>> class Net(paddle.nn.Layer):
        ...     def __init__(self):
        ...         super().__init__()
        ...
        ...     def forward(self, x, flag=True):
        ...         if flag:
        ...             out = x + 1
        ...         else:
        ...             out = x - 1
        ...         return out
        ...
        >>> x = paddle.randn([10, 1], 'float32')
        >>> net = paddle.jit.to_static(Net())  # convert into static graph mode
        >>> out = net(x)

        >>> net.forward.rollback()  # rollback into dygraph mode
        >>> out = net(x)
zNot Found function 'z' in class 'z'.)	r   r   r   r   r   r  r   r   r   )r   fn_namerA   s      rB   rollbackStaticFunction.rollbackx  s    @ &)))
 !!- ''00 	
 --=== 	
"7)<8K8K8U8U7VVXY	
= ""227;##Wll4;N;N.OPt**G44rH   c                   U R                   b  [        U 5      " U R                  U R                  40 U R                  D6nU R
                  Ul        U R                  Ul        U R                  Ul        U R                  Ul        UR                  U[        U R                   5         [        U R                   5      5      $ U R                  $ )a  
Customized behavior for copy.deepcopy, return a new StaticFunction instance.

Examples:
    .. code-block:: python

        >>> import copy
        >>> import paddle

        >>> class Net(paddle.nn.Layer):
        ...     def __init__(self):
        ...         super().__init__()
        ...
        ...     def forward(self, x, flag=True):
        ...         if flag:
        ...             out = x + 1
        ...         else:
        ...             out = x - 1
        ...         return out
        ...
        >>> x = paddle.randn([10, 1], 'float32')
        >>> net = paddle.jit.to_static(Net())  # convert into static graph mode

        >>> copy_net = copy.deepcopy(net)      # still in static graph mode
)r   r   r   r   r   r   r   r   r   r   r   )r   memocopied_static_fns      rB   __deepcopy__StaticFunction.__deepcopy__  s    4 *#Dz&&(8(8 <@LL  *.&.2.A.A+151G1G.-1-?-?*#++R++,-tD4G4G/H  )))rH   c                    [        S5      er#  r$  r   s    rB   inputsStaticFunction.inputs  r3  rH   c                    [        S5      er#  r$  r   s    rB   outputsStaticFunction.outputs  r3  rH   c                    [        S5      er#  r$  r   s    rB   main_programStaticFunction.main_program  r3  rH   c                    [        S5      er#  r$  r   s    rB   program_cacheStaticFunction.program_cache  r3  rH   c                    [        S5      er#  r$  r   s    rB   r   StaticFunction.function_spec  r3  rH   )r   r   r   r   r   r   r   r   r   r   r   r=   )returnbool)rV  None)rV  r
   )r?   _InputT.argsr@   _InputT.kwargsrV  r8   r?   rY  r@   rZ  rV  z.tuple[ConcreteProgram, PirPartialProgramLayer]rV  str)rV  zCallable[_InputT, _RetT]rV  ConcreteProgramNFF)r    NestedSequence[InputSpec] | Noner   rW  r<  rW  rV  z	list[Any]rV  r6   rV  r   rV  r   )r   r   r   r   r   r   r   r  r  r   r
  r  r  r  r   r&  r*  r.  r1  r   r   r9  r=  rA  rF  rI  rL  rO  rR  r   r   r   rH   rB   r   r     sr   %"N  		)0V

"3H	"6!6-;6	6L:!:-;:	7:
:: : : & & * * : :
 8<#	:4: : 	:.5`&*P : : : : : : : : : :rH   r   c                   ^  U 4S jnU$ )Nc                 8   > Sn[        UR                  TS95      e)Nz[Can't call {func} when full_graph=False. Use paddle.jit.to_static(full_graph=True) instead.)rA   )r  format)r?   r@   error_templatefunc_strs      rB   _raise_error*raise_error_template.<locals>._raise_error  s)    A 	 >00h0?@@rH   r   )rj  rk  s   ` rB   raise_error_templaterm    s    A rH   c                     ^  \ rS rSrSU 4S jjrS r\S 5       r\S 5       r\	" S5      r
\	" S5      r\	" S5      r\	" S	5      r\S
 5       r\S 5       r\S 5       r\S 5       r\S 5       rSrU =r$ )SymbolicStaticFunctioni  c                h   > Ub  [         R                  " S5        [        TU ]  " X40 UD6  S U l        g )Nzfull_graph=False don't support input_spec arguments. It will not produce any effect.
You can set full_graph=True, then you can assign input spec.
)warningsr  superr   last_call_input_specr   r   r   r@   r  s       rB   r   SymbolicStaticFunction.__init__  s7    !MMQ 	88$(!rH   c                   SSK Jn  U R                  R                  X5      u  p[	        5       (       d  [        U5        U R                  R                  X5      u  nnX@l        U R                  R                  SS 5      nU R                  R                  SS 5      nU" U R                  UU R                  5       US9nU R                  b  U R                  /UQ7nU" U0 UD6$ )N   )symbolic_translatebuild_strategybackend)ry  r  rz  )sotrx  r   r   r3   r+   r   rs  r   r   r   r  r   )	r   r?   r@   rx  r   r   ry  rz  
traced_funs	            rB   r  $SymbolicStaticFunction._perform_call  s    ,**BB4P%''6t<
 224@	
 "$8!))*:DA,,""9d3'"")((*	

 *''/$/D4*6**rH   c                $    [        S5      " 5         g )Nr1  rm  r   s    rB   r1  SymbolicStaticFunction.code  s    V$&rH   c                $    [        S5      " 5         g )Nr9  r  r   s    rB   r9  'SymbolicStaticFunction.concrete_program  s    /02rH   r=  r&  r*  r.  c                $    [        S5      " 5         g )NrI  r  r   s    rB   rI  SymbolicStaticFunction.inputs$  s    X&(rH   c                $    [        S5      " 5         g )NrL  r  r   s    rB   rL  SymbolicStaticFunction.outputs(  s    Y')rH   c                $    [        S5      " 5         g )NrO  r  r   s    rB   rO  #SymbolicStaticFunction.main_program,  s    ^,.rH   c                $    [        S5      " 5         g )NrR  r  r   s    rB   rR  $SymbolicStaticFunction.program_cache0      _-/rH   c                $    [        S5      " 5         g )Nr   r  r   s    rB   r   $SymbolicStaticFunction.function_spec4  r  rH   )rs  r=   )r   r   r   r   r   r  r   r1  r9  rm  r=  r&  r*  r.  rI  rL  rO  rR  r   r   __classcell__r  s   @rB   ro  ro    s    )+2 ' ' 3 3 +?-+' 00FG*>-+' ,,>?) ) * * / / 0 0 0 0rH   ro  c                    ^  \ rS rSrSrSU 4S jjrS r      SS jr    SS jrSS jr	\
SS j5       r\
SS	 j5       r   S       SS
 jjr\
SS j5       r\
SS j5       r\
SS j5       r\
SS j5       r\
SS j5       rSrU =r$ )ASTStaticFunctioni9  zD
Wrapper class to Manage program conversion of decorated function.

c                (   > [         TU ]  " X40 UD6  g r=   )rr  r   rt  s       rB   r   ASTStaticFunction.__init__?  s    88rH   c                   U R                   R                  X5      u  p U R                  " U0 UDSU R                  5       0D6u  p4[	        U R
                  [        R                  5      (       a  U R
                  R                  Ul        OU R                  Ul         U" U5      $ ! [         a?  n[        U[        R                  5      (       d  [        R                  " USS9  e  S nAg S nAff = f! [         aa  n[        U[        R                  S 5      nU(       a  UR!                  5          S nAg ["        R$                  " S['        U5       S35        UeS nAff = f)Nr   T)
in_runtimezaPlease file an issue at 'https://github.com/PaddlePaddle/Paddle/issues' if you can't handle this z
 yourself.)r   r   r&  r  r   r   r   r   r  r   	Exceptionr   r   
ERROR_DATAattach_error_datar   raise_new_exceptionr   r  r   )r   r?   r@   rd   partial_program_layere
error_datas          rB   r  ASTStaticFunction._perform_callB  s6   **BB4P	'+'@'@((*.*=*=*?($A $--v||<<151D1D1M1M%.15%.,T22 q%"2"233++A$? 4
  		 E$4$4d;J..00""115a	E 		sB   A<C0 B$ $
C-.5C(#C0 (C--C0 0
E:2E1%EEc                   U R                  5         UR                  SS5      nUR                  SS5      nUR                  SS5      nSU;   a  UR                  S5        SU;   a  UR                  S5        SU;   a  UR                  S5        [        U5      [        U R                  R
                  5      :w  a  U R                  R                  X5      u  pU R                  R                  X5      u  nn[        U R                  UUU R                  40 U R                  DUUS.D6nU(       a!  U R                  R                  U5      u  n	n
X4$ U R                  U   u  pX4$ )z
Returns traced concrete program and inner executable partial layer.

Args:
    *args(tuple): input arguments values or InputSpec
    **kwargs(dict) : input kwargs values.

Returns:
    Traced ConcreteProgram and executable translated Layer.
r   Fr   Tr<  )r   r   )r   r   poprw   r   	args_namer   r   r   r   r   r   get_program_without_cache)r   r?   r@   r   r   r<  r   r   	cache_keyr9  r  s              rB   r&  &ASTStaticFunction.get_concrete_programc  si    	!!#JJ{E2	::j$/

?E:JJz"& JJ{#f$JJ't9D//99::..FFLD 224@	
 "  "	

 ll
  
	  ##==iH %  66 7;6I6I73  66rH   c                d    U R                  5         U R                  R                  U5      u  nnX#4$ )z
Returns traced concrete program and inner executable partial layer by cached key.

Args:
    cached_key(CacheKey): The cached key use to get concrete program.

Returns:
    Traced ConcreteProgram and executable translated Layer.
)r   r   r  )r   r)  r9  r  s       rB   r*  5ASTStaticFunction.get_concrete_program_with_cache_key  s:     	!!# 99*E	
!66rH   c                ,    [        U R                  5      $ )zC
Returns the number of traced programs for the decorated function.
)rw   r   r   s    rB   r.  "ASTStaticFunction.get_traced_count  s     4&&''rH   c                F    [        U R                  5      n[        U5      nU$ )zG
Returns the source code of transformed static function for debugging.
)r   r   r,   )r   r   r   s      rB   r1  ASTStaticFunction.code  s$    
 ((=(=>)+6rH   c                     U R                  SS9$ )a#  
Returns recent ConcreteProgram instance of decorated function.

Examples:
    .. code-block:: python

        >>> # doctest: +SKIP('`paddle.jit.to_static` can not run in xdoctest')
        >>> import paddle
        >>> from paddle.jit import to_static
        >>> from paddle.static import InputSpec

        >>> paddle.disable_static()

        >>> def foo(x, y):
        ...     z = x + y
        ...     return z
        ...
        >>> # usage 1:
        >>> decorated_foo = to_static(foo, input_spec=[InputSpec([10], name='x'), InputSpec([10], name='y')])
        >>> print(decorated_foo.concrete_program)

        >>> # usage 2:
        >>> decorated_foo = to_static(foo)
        >>> out_foo = decorated_foo(paddle.rand([10]), paddle.rand([10]))
        >>> print(decorated_foo.concrete_program)
N)r   )r=  r   s    rB   r9  "ASTStaticFunction.concrete_program  s    8 7747HHrH   c                   U R                  5         [        U R                  5      nUnU R                  R                  b  Ub]  [        [        U5      [        U R                  R                  5      5      (       d&  [        SU SU R                  R                   S35      eU R                  R                  nUb  [        R                  " SU SU S35        USLnU(       a&  U R                  " UUU R                  5       US.6u  pxU$ US	:w  a  [        R                  " S
5        US:  a'  [        R                  " SU R                   SU S35        U R                  R                  n	U(       a  SU	R                  S'   U(       a  U R                  U	5      u  nnU$ U R                  U	   u  pxU$ [        SU R                   S35      e)a  
Returns recent ConcreteProgram instance of decorated function while
specifying input_spec. If the self._function_spec already has
input_spec, it will check the compatibility of input input_spec and
the self._function_spec.input_spec. If input input_spec=None, then
this method uses self._function_spec.input_spec

args:
    input_spec (list[InputSpec], optional): Describes the input of
        the translate function.
NzThe `input_spec`: zG used to construct concrete_program is conflict with the `input_spec`: z in `@paddle.jit.to_static`z

You have specified `input_spec` both in function definition (higher priority) and `paddle.jit.save` (will be ignored.)

	 Using: z

	 Ignore: rK   )r   r   r<  r   z3No input_spec is found, save cached program insteadr   zCurrent z$ has more than one cached programs: z4, the last traced program will be return by default.Tr   z!No valid transformed program for z|.
	    Please specific `input_spec` in `@paddle.jit.to_static` or feed input tensor to call the decorated function at once.
)r   rw   r   r   r   r.   r   rP   r   r  r&  r  _recent_cache_keyr@   r*  )
r   r   r   r<  cached_program_lendesired_input_spechas_input_specr9  rd   r  s
             rB   r=  5ASTStaticFunction.concrete_program_specify_input_spec  s<   " 	!!# !!4!45 ())5%.D
#WT-@-@-K-K%L/ / !(4{  }A  }P  }P  }[  }[  |\  \w  x 
 "&!4!4!?!?%"" _  `r  _s  sB  CM  BN  NP  Q ,47"&";";##,,.+	# $#!Q&""I &)!&&"4#6#6"77[\n[o  pd  e !//AA	48I$$[1  @@K(++*.*=*=i*H'$++ !78K8K7L  ML  M rH   c                    U R                  5         U R                  n[        UR                  5       Vs/ s H,  n[	        U[
        R                  [        45      (       d  M*  UPM.     nnU$ s  snf )z;
Returns input tensors of recent converted static program.
)r   r9  r   rI  r   r   Variabler   )r   r9  varrI  s       rB   rI  ASTStaticFunction.inputs*  sk    
 	!!#00 /667
7#	 2 2E:; 7 	 

 
   )A+!A+c                    U R                  5         U R                  n[        UR                  5       Vs/ s H,  n[	        U[
        R                  [        45      (       d  M*  UPM.     nnU$ s  snf )z<
Returns output tensors of recent converted static program.
)r   r9  r   rL  r   r   r  r   )r   r9  r  rL  s       rB   rL  ASTStaticFunction.outputs8  sk    
 	!!#00 /778
8#	 2 2E:; 8 	 
 
r  c                V    U R                  5         U R                  nUR                  nU$ )z/
Returns recent converted static main program.
)r   r9  rO  )r   r9  rO  s      rB   rO  ASTStaticFunction.main_programG  s-    
 	!!#00'44rH   c                    U R                   $ r=   )r   r   s    rB   rR  ASTStaticFunction.program_cacheQ      """rH   c                    U R                   $ r=   )r   r   s    rB   r   ASTStaticFunction.function_specU  r  rH   r   r=   r[  )r)  r   rV  zDtuple[ConcreteProgram, PartialProgramLayer | PirPartialProgramLayer])rV  intr\  r^  r`  )r   ra  r   rW  r<  rW  rV  r_  rb  rc  rd  re  )r   r   r   r   r   r   r  r&  r*  r.  r   r1  r9  r=  rI  rL  rO  rR  r   r   r  r  s   @rB   r  r  9  s   
9B67!67-;67	767p7"7	M7&(   I I> 8<#	M4M M 	M
 
M^       # # # #rH   r  c                    [        U [        R                  5      (       a!  U R                  (       d  [	        SU  S35      egg)z7
Verifies the instance is initialized in dynamic mode.
z `paddle.jit.to_static` is only available in dynamic mode. Please call `paddle.disable_static()` before initializing your Layer class `zr` . Because parameters of Layer class should be initialized firstly in dynamic mode while applying transformation.N)r   r   r   _init_in_dynamic_moder  )r   s    rB   _verify_init_in_dynamic_moder  Z  sM     .&,,//3322@1A BAA  4 0rH   c                  .    \ rS rSrSrSS jrS rS rSrg)	
HookHelperig  z
Only For converting pre/post hooks operation in outermost layer while jit.save.
Because hooks in sublayer have been processed automatically.
c                    Xl         X l        X0l        U=(       a:    [        U R                  [        R
                  5      =(       a    UR                  S:H  U l        g )Nforward)rA   r   r   r   r   r   r   need_apply_hook)r   rA   r   r   s       rB   r   HookHelper.__init__m  sG    	," +4..=+* 	rH   c                   U R                   (       d  U$ USS nU R                  R                  R                  5        H5  nU" U R                  U5      nUc  M  [	        U[
        5      (       d  U4nUnM7     U R                  /[        U5      Q$ )z/
Apply _forward_pre_hooks from outermost layer
r   N)r  r   _forward_pre_hooksrk   r   tuplelist)r   rI  forward_pre_hookhook_results       rB   apply_pre_hooksHookHelper.apply_pre_hooksw  s     ##M $ 3 3 F F M M O*4+>+>GK&!+u55#..K$ !P ##3d6l33rH   c                    U R                   (       d  U$ USS nU R                  R                  R                  5        H  nU" U R                  X5      nUc  M  UnM     UR	                  SU R                  5        U$ )z0
Apply _forward_post_hooks from outermost layer
r   Nr   )r  r   _forward_post_hooksrk   insert)r   rI  rL  forward_post_hookr  s        rB   apply_post_hooksHookHelper.apply_post_hooks  s~     ##N   44;;= +##VK &% > 	a,,-rH   )r   rA   r  r   N)F)	r   r   r   r   r   r   r  r  r   r   rH   rB   r  r  g  s    

4"rH   r  c                  t    \ rS rSr/ SQr S
SS.S jjr\S 5       r\\S 5       5       r	\\S 5       5       r
S	rg)r_  i  )rI  rL  rO  startup_program
parametersr   r@   constraintsN)r  c               p    Xl         X l        Ub  UO/ U l        XPl        X`l        X0l        X@l        Xl        g r=   )rI  rL  r  rO  r  r  r   r@   )	r   rI  rL  r  r   rO  r  r  r@   s	            rB   r   ConcreteProgram.__init__  s8     *5*A;r(.$ rH   c                    [        U 5      n / nU  Hd  n[        US5      (       d  / s  $ [        UR                  5      (       d  M4  UR                   H   nUR	                  UR
                  U45        M"     Mf     U$ )z&
Extract constraints from input_specs
ranges)r   r   rw   r  ru   rQ   )input_specsr  r   ranges       rB   extract_constraints#ConcreteProgram.extract_constraints  so    
 k*%J:x00	:$$%%'..E&&
'?@ /	 & rH   c                b   US   n[        U5        U R                  n[        U5      n[        XcUR	                  SS5      5      n[
        R                  " 5       [
        R                  " 5       p[        R                  R                  5       R                  U	l
        [        R                  R                  5       R                  U
l
        [
        R                  " X5         [        U	5       nU R                  X5      u  pU R                  X)5      u  pU(       a  U/[        U5      Q7nU/[        U5      Q7n[!        [#        US5      5         [!        [%        US5      5         ['        U5          UR)                  U5      nU(       a	  U" U0 UD6nOU" U6 nUR+                  UU5      n SSS5        SSS5        SSS5        Wb:  [9        U[:        [        45      (       + =(       d    [=        U5      S:H  nU(       a  U/nSSS5        SSS5        [?        U	5      n	[@        RB                  R	                  SS5      S	:X  d  [E        U	5        [F        RI                  U5      n[G        SWWWRK                  5       UU	U
US
.UD6$ ! [,         aN  n[.        R0                  " U5        [3        U[.        R4                  S5      nU(       a  UR7                  5         e SnAff = f! , (       d  f       GN7= f! , (       d  f       GNA= f! , (       d  f       GNK= f! , (       d  f       GN= f! , (       d  f       GN"= f)
Builds the main_program with specialized inputs and returns outputs
of program as fetch_list.

Args:
    func_spec(FunctionSpec): A FunctionSpec instance for decorated function.
    input_spec(list[InputSpec]):
rz  r   FTNr   stride_in_no_check_dy2st_diff01)rI  rL  r  r   rO  r  r  r   )&r  r   r   r  r   	ir_staticr6   rr   staticdefault_main_programrandom_seeddefault_startup_programprogram_guardr-   pir_to_static_inputs_with_specr  r   r   r   r*   r  r  BaseExceptionr   r  r   r  r  r   r  rw   r"   osenvironr~   r_  r  get_params_with_values)	func_specr   input_kwargs_specr   r@   rz  r   r   hook_helperrO  r  ctxstatic_inputsprogram_inputsr   rd   rI  rL  r  r  need_wrap_into_listr  s                         rB   pir_from_func_spec"ConcreteProgram.pir_from_func_spec  s    #$^4 %55'(89 fjje.L
 )2(9(9(;Y=N=N=Po
 MM..0<< 	  MM113?? 	#
 ##LB- 88 *M
 #AA!JG "!-(!
 #".)" N>4@AK=>g&(88GF"-v"A"A"-v"6)::67KG ' ? B( ""7UDM::Oc'la>O $ '&iGa . Cf <LIzz~~=sCsJ*<8%99*E 	
!113%%+#	
 	
 		
) % ++A.!(E,<,<d!CJ!"668 '& ?> BA/ .- CBs   L)A!L
K; K)	,K.8I<
'K)	/K;7AL<L<
KA	KKK
K&!K)	)
K83K;;
L
L
L	L
L.c           
        [        U5        U R                  n[        U5      n[        XSUR	                  SS5      5      n[
        R                  " 5       [
        R                  " 5       p[        R                  R                  5       R                  Ul
        [        R                  R                  5       R                  U	l
        [        R                  5       R                  R                  5         [
        R                   " X5         [#        SS9   U R%                  X5      n
U R%                  X(5      nU(       a  U/['        U
5      Q7n
[)        [+        US5      5         [)        [-        US5      5          UR/                  U
5      nU(       a	  U" U0 UD6nOU" U6 nUR1                  X5      n SSS5        SSS5        [        R                  5       R>                  RA                  U5      nWb:  [C        U[D        [&        45      (       + =(       d    [G        U5      S:H  nU(       a  U/nSSS5        SSS5        [I        U5      n[K        SW
WWUUU	S.UD6$ ! [2         aN  n[4        R6                  " U5        [9        U[4        R:                  S5      nU(       a  UR=                  5         e SnAff = f! , (       d  f       GN	= f! , (       d  f       GN= f! , (       d  f       N= f! , (       d  f       N= f)	r  r   FT)is_to_staticNr   )rI  rL  r  r   rO  r  r   )&r  r   r   r  r   r   r6   rr   r  r  r  r  r   get_instance_amp_recordsclearr  r   to_static_inputs_with_specr  r   r   r   r  r  r  r   r  r   r  r  _params_recorderr  r   r  rw   r"   r_  )r  r   r  r   r@   r   r   r  rO  r  r  r   rI  rL  r  r  all_parameters_and_buffersr  s                     rB   from_func_specConcreteProgram.from_func_spec7  s    	%^4 %55'(89 fjje.L
 )2(9(9(;Y=N=N=Po
 MM..0<< 	  MM113?? 	# 	&&(55;;= ##LB d3 &@@M  ::!G "!-(! N>4@AK=>(88GF"-v"A"A"-v"6)::6KG ? B* "..0AAEE  ' ""7UDM::Oc'la>O $ '&iGa 4 Cf <LI 
 1%%+
 
 	
1 % ++A.!(E,<,<d!CJ!"668 ?> BA# 43 CBsu   
K#AKK 2J.	47I,K 4A2K&K#
J+A	J&&J++J.	.
J=8K  
K
K
K 	K##
K1)r  r   rI  r@   rO  rL  r  r  r=   )r   r   r   r   r   r   staticmethodr  r   r  r  r   r   rH   rB   r_  r_    sn    	I$  ,   d
  d
N a
  a
rH   r_  c                    [        U 5      $ )z\
because program is not deleted while calling from_func_spec.
so it's ok to use id(program)
)r   )rx   s    rB   _program_hashr	    s    
 g;rH   c                  0    \ rS rSrS r\S 5       rS rSrg)ParametersRecorderi  c                    0 U l         g r=   params_dictr   s    rB   r   ParametersRecorder.__init__  
    rH   c                    [        U5      nX0R                  ;  a  [        5       U R                  U'   U R                  U   nUR                  U5        gz@use the default_program as key, append param the parameter list.N)r	  r  setadd)r   rx   paramkeyparamss        rB   r  ParametersRecorder.add  sH     G$&&&$'EDS!!!#&

5rH   c                    U R                   R                  [        U5      5      nUc  / $ U R                   [        U5      	 [        U5      nUR	                  S S9  U$ )Nc                    U R                   $ r=   )rQ   )xs    rB   <lambda>(ParametersRecorder.pop.<locals>.<lambda>  s    !&&rH   )r  )r  r   r	  r  sort)r   rx   r  s      rB   r  ParametersRecorder.pop  sW    !!%%mG&<=>I]734f()rH   r  N)	r   r   r   r   r   rG   r  r  r   r   rH   rB   r  r    s       rH   r  c                  <    \ rS rSrS r\S 5       rS rS rS r	Sr
g)	
InplaceMapi  c                    0 U l         g r=   r  r   s    rB   r   InplaceMap.__init__  r  rH   c                |    [        U5      nX@R                  ;  a  0 U R                  U'   U R                  U   nX5U'   gr  )r	  r  )r   rx   r   r  r  r  s         rB   r  InplaceMap.add  sA     G$&&&$&DS!!!#&r
rH   c                   U R                   R                  [        U5      5      nUc  g X#;  a  g X2   n/ nUR                  R	                  5       UR                  5       ;   a[  UR                  U5        X4R                  R	                  5          nUR                  R	                  5       UR                  5       ;   a  M[  U H  nXCUR                  R	                  5       '   M!     U$ r=   )r  r   r	  descr   r  ru   )r   rx   r   r  root_varsavedr  s          rB   r   InplaceMap.get  s    !!%%mG&<=>:mm FKKM1LL"mm..01H mm FKKM1 C$,388;;=! rH   c                    Xl         g r=   r  )r   
checkpoints     rB   restore_checkpointInplaceMap.restore_checkpoint  s
     &rH   c                    0 nU R                   R                  5        H"  u  p#[        UR                  5       5      nXAU'   M$     U$ r=   )r  itemsdict)r   ckp
program_idr  
new_paramss        rB   save_checkpointInplaceMap.save_checkpoint  sA    "&"2"2"8"8":Jflln-J(
O #; 
rH   r  N)r   r   r   r   r   rG   r  r   r-  r5  r   r   rH   rB   r!  r!    s*      	&rH   r!  c                  ,    \ rS rSrS rS rS rS rSrg)PirPrimHookeri  c                   X l         [        5       U l        [        U R                   5         [        R
                  " 5       (       a[  UR                  5       R                   Vs1 s H0  n[        R                  " U5      (       d  M   UR                  5       iM2     snU l        S S S 5        g s  snf ! , (       d  f       g = fr=   )
rz  r  custom_vjpsr*   r   _is_all_prim_enabledglobal_blockopshas_custom_vjprQ   r   original_programrz  rR   s       rB   r   PirPrimHooker.__init__  s    54<<(((** /;;=AA$A**2. BGGIA$  )($ )(s#   7B/"B*B*B/*B//
B=c                    [        U R                  5         [        R                  " 5       (       a+  [        R
                  " XU R                  S9nX4sS S S 5        $ X4sS S S 5        $ ! , (       d  f       g = fN)	blacklist)r*   rz  r   _is_fwd_prim_enabledr   	decomposer:  )r   forward_programsrc_varsdst_varss       rB   before_append_backward$PirPrimHooker.before_append_backward  s\    4<<(((**(22#9I9I '0 )( #, )((s   <A(A((
A6c                   [        U R                  5         [        R                  " 5       (       a  [	        U R
                  5      S:w  a  [	        UR                  5       R                  5      U-
  n[        R                  " UUU R
                  US9n[	        UR                  5       R                  5      U-
  n	[        R                  " UUU R
                  U	S9nXU4sS S S 5        $ XU4sS S S 5        $ ! , (       d  f       g = f)Nr   )	whitelist	end_index)rM  start_index)
r*   rz  r   rE  rw   r:  r<  r=  r   rF  )
r   whole_programrI  rH  grad_outputsforward_end_idxbackward_start_idxbackward_lengthrI  new_start_indexs
             rB   after_append_backward#PirPrimHooker.after_append_backward  s     4<<(((**s43C3C/D/I224889OK   )22!"..-	 224889OK   )22!".. /	 %x?- )(. !8;/ )((s   CC2%C22
D c                b   [        U R                  5         [        R                  " 5       (       ae  [        R
                  " UR                  UR                  5      nX!l        S[        UR                  R                  5       R                  5      4Ul         S S S 5        g ! , (       d  f       g = fr\   )r*   rz  r   rE  r   rF  rx   
out_valuesrw   r<  r=  forward_range)r   infer_programtargetss      rB   after_inferPirPrimHooker.after_infer#  s    4<<(((**'11!))=+C+C ,3(--::<@@A/+  )((s   BB  
B.rz  r:  N	r   r   r   r   r   rJ  rV  r]  r   r   rH   rB   r8  r8    s    	- <DrH   r8  c                  *    \ rS rSrSS jrS rS rSrg)PirAutoRecomputeHookeri1  Nc                    Xl         g r=   recompute_ops)r   re  s     rB   r   PirAutoRecomputeHooker.__init__2  s    *rH   c                    X4$ r=   r   )r   rG  rH  s      rB   rJ  -PirAutoRecomputeHooker.before_append_backward5  s    ((rH   c                x    [         R                  " 5       (       a  [        R                  " UUUUUU5      u  pXU4$ r=   )r   _enable_auto_recomputer   auto_recompute)r   rP  rI  rH  rQ  rR  rS  s          rB   rV  ,PirAutoRecomputeHooker.after_append_backward8  sE     &&((-:-I-I".*M x77rH   rd  r=   )r   r   r   r   r   rJ  rV  r   r   rH   rB   rb  rb  1  s    +)8rH   rb  c                  N    \ rS rSrSrS rS rS rS rS r	S r
S	 rS
 rS rSrg)r   iM  zF
Wrapper class for the program functions defined by dygraph function.
c                T    [         R                  " 5       U l        S U l        S U l        g r=   )collectionsOrderedDict_caches_recent_keyr  r   s    rB   r   ProgramCache.__init__R  s"    "..0!%rH   c                   [        5       (       aN  [        R                  " S	UR                  UR                  UR
                  UR                  S.UR                  D6nOM[        R                  " S	UR                  UR                  UR
                  UR                  S.UR                  D6nUR                  S   n[        5       (       d  [        UR                  S   U5      (       ay  UR                  R                  5        H[  nUR                  [        ;  d  M  SUR                  ;   d  M+  [        R                   " SUR"                   SUR                   35        M]     [        5       (       a  SSKJn  U" X!R                  S L5      nOSSKJn  U" X!R                  S L5      n[+        U5         [,        R.                  " 5       (       aZ  [        5       (       a&  UR1                  [3        UR                  U5      5        O%UR5                  [7        UR                  U5      5        [        5       (       a3  [,        R8                  " 5       (       a  UR1                  [;        5       5        S S S 5        X&4$ ! , (       d  f       X&4$ = f)
N)r  r   r  r   rz  ry  z@Now prim and cinn do not support -1 shape, but the shape of var z is r   )partial_program_fromr   )r   r_  r  r   r   r   r   r@   r  r1   rO  ro   r   r'   shaperq  r  rQ   pir_partial_programrv  partial_programr*   r   rE  
add_hookerr8  
set_hooker
PrimHookerrj  rb  )r   r  r9  rz  r  rv  ry  s          rB   _build_onceProgramCache._build_onceY  s$   ==.AA  #11$99"+"B"B(77	 
 ""   /==  #11$99"+"B"B(77	 
 ""  ""9-}}&  !12G  ,88BBDCxx'88R399_ ^_b_g_g^hhlmpmvmvlwx E ==A2 ":":$"FO >2 ":":$"FO 7#((**==#..%&6&C&CWM $.."#3#@#@'J }}!<!<!>!>**+A+CD $  00 $#  00s   /B7I11
Jc                   [        U[        5      (       d  [        S[        U5       35      e[	        U5      nXl        X l        X R                  ;  a^  U R                  U5      U R                  U'   [        U R                  5      nU[        :  a!  [        R                  " SU S[         S35        U R                  U   $ )Nz,type(item) should be CacheKey, but received zCurrent traced program number: z > `max_tracing_count`:z. Too much cached programs will bring expensive overhead. The reason may be: (1) passing tensors with different shapes, (2) passing python objects instead of tensors.)r   r   rP   r2   r   r  rr  rq  r}  rw   MAX_TRACED_PROGRAM_COUNTr   r  )r   itemitem_idcurrent_tracing_counts       rB   __getitem__ProgramCache.__getitem__  s    $))>y>OP  t*!%",,&$($4$4T$:DLL!$'$5!$'??""56K5LLcd|c} ~C C
 ||G$$rH   c                     U R                  US9$ )N)r  )r}  )r   r  s     rB   r  &ProgramCache.get_program_without_cache  s    )44rH   c                    [        U[        5      (       d  [        S[        U5       35      e[	        U5      nX R
                  ;  a  [        S5      eU R
                  U   $ )Nz7Input item's type should be FunctionSpec, but received zaFailed to find program for input item, please decorate input function by `@paddle.jit.to_static`.)r   r   rP   r2   r   rq  r  )r   r  r  s      rB   get_programProgramCache.get_program  sb    $))I)TX/IZ[  t*,,&s  ||G$$rH   c                    [        U R                  5      S:  d   S5       eU R                  c   eU R                  U R                  U R                     4$ )Nr   z(No valid cached program in ProgramCache.)rw   rq  rr  r   s    rB   lastProgramCache.last  sW    4<< A% 	
6	
% +++d.>.>!???rH   c                ,    [        U R                  5      $ r=   )rw   rq  r   s    rB   __len__ProgramCache.__len__  s    4<<  rH   c                x    U R                   R                  5        VVVs/ s H
  u  nu  p#UPM     snnn$ s  snnnf r=   )rq  r0  )r   r  cprd   s       rB   concrete_programsProgramCache.concrete_programs  s/    '+||'9'9';<';|sGR';<<<s   5c                8    [         R                  " 5       U l        g r=   )ro  rp  rq  r   s    rB   r  ProgramCache.clear  s    "..0rH   )rq  r  rr  N)r   r   r   r   r   r   r}  r  r  r  r  r  r  r  r   r   rH   rB   r   r   M  s6    &61p%(5
%@!=1rH   r   c                  ,    \ rS rSrS rS rS rS rSrg)r|  i  c                   X l         [        5       U l        [        U R                   5         [        R
                  " 5       (       ab  UR                  S5      R                   Vs1 s H6  n[        R                  " UR                  5      (       d  M*  UR                  iM8     snU l        S S S 5        g s  snf ! , (       d  f       g = fr\   )
rz  r  r:  r*   r   r;  blockr=  has_comp_grad_op_makerr   r?  s       rB   r   PrimHooker.__init__  s    54<<(((** /44Q7;;$;22277; BGG;$  )($ )(s#   8B6#)B1B1 B61B66
Cc                    [        U R                  5         [        R                  " 5       (       a  [	        UR
                  U R                  S9  UsS S S 5        $ ! , (       d  f       g = frC  )r*   rz  r   rE  _to_primblocksr:  )r   rG  s     rB   rJ  !PrimHooker.before_append_backward  sB    4<<(((**//4;K;KL" )((s   :A
A(c                   [        U R                  5         [        UR                  S5      R                  5      U-
  n[
        R                  " 5       (       a-  [        U R                  5      S:w  a  [        UR                  US9  [        UR                  S5      R                  5      U-
  nUS:  a  [        UR                  US9  X4sS S S 5        $ ! , (       d  f       g = f)Nr   )rT  )	start_idx)
r*   rz  rw   r  r=  r   rE  r:  r  r  )r   rP  rS  rT  rU  s        rB   rV   PrimHooker.after_append_backward  s    4<<(M''*../2DD  ((**s43C3C/D/I--O!-"5"5a"8"<"<=OO"--I 1 )((s   B2C
C c                    [        U R                  5         [        R                  " 5       (       a  [	        UR                  S5      5        UsS S S 5        $ ! , (       d  f       g = fr\   )r*   rz  r   rE  r  r  )r   r[  s     rB   r]  PrimHooker.after_infer  s?    4<<(((**,,Q/0  )((s   6A
A$r_  Nr`  r   rH   rB   r|  r|    s    	#2!rH   r|  c                  z    \ rS rSrSr\R                  " 5       rSr\	S 5       r
\S 5       r\S 5       rS rS rS	rg)
r   i  a  
Class to translate dygraph function into static graph function. The object
of this class is a singleton.

Args:
    None.

Returns:
    ProgramTranslator: the singleton object.

Examples:
    .. code-block:: python

        >>> import paddle

        >>> # Two methods get same object because ProgramTranslator is a singleton
        >>> paddle.jit.dy2static.program_translator.ProgramTranslator()
        >>> paddle.jit.dy2static.program_translator.ProgramTranslator.get_instance()

Nc                    U R                   c0  [        R                  " U /UQ70 UD6U l         SU R                   l        U R                   $ NF)	_instanceobject__new___initialized)r   r?   r@   s      rB   r  ProgramTranslator.__new__  s<    == "NN3@@@CM).CMM&}}rH   c                    U R                   c-  U R                     U " 5       U l         S S S 5        U R                   $ U R                   $ ! , (       d  f       U R                   $ = fr=   )r  _singleton_lockr   s    rB   r  ProgramTranslator.get_instance  sI    == $$ # %}}s}} %$}}s   A
A c                v    U R                   b,  SU R                   l        U R                   R                  5         g g r  )r  r  r   r  s    rB   resetProgramTranslator.reset  s-    ==$).CMM&MM""$ %rH   c                    U R                   (       a  g SU l         [        5       U l        [        5       U l        [        5       U l        0 U l        SU l        g )NT)	r  r   r   r  r  r!  _inplace_mapr   r  r   s    rB   r   ProgramTranslator.__init__  sE     *n 2 4&LJL $rH   c                4    [        US[        S5        Xl        g )Nr  zProgramTranslator.enable)r   rW  r  )r   r  s     rB   enableProgramTranslator.enable$  s    &		
 !1rH   )r   r  r  r  r   r  )r   r   r   r   r   rE   rF   r  r  rG   r  r   r  r  r   r  r   r   rH   rB   r   r     s^    *  nn&OI    % %
	%1rH   r   c                ^    [        U S[        S5        [        5       nUR                  U 5        g)a  
Enable or disable the converting from imperative to static graph by
ProgramTranslator globally.

Args:
    enable_to_static_bool (bool): True or False to enable or disable converting to static.

Returns:
    None.

Examples:
    .. code-block:: python

        >>> import paddle
        >>> @paddle.jit.to_static
        >>> def func(x):
        ...     if paddle.mean(x) > 0:
        ...         x_v = x - 1
        ...     else:
        ...         x_v = x + 1
        ...     return x_v
        ...
        >>> paddle.jit.enable_to_static(False)

        >>> x = paddle.ones([1, 2])
        >>> # ProgramTranslator is disabled so the func is run in dygraph
        >>> print(func(x))
        Tensor(shape=[1, 2], dtype=float32, place=Place(cpu), stop_gradient=True,
        [[0., 0.]])

enable_to_static_boolzpaddle.jit.enable_to_staticN)r   rW  r   r  )r  r   s     rB   r  r  .  s2    @ %	 '(N/0rH   ru  c                6    SSK Jn  UR                  U UUUUS9  g)z(Switch to static graph and call to_prim.r   )primapi)rD  rM  r  rT  N)paddle.incubate.autogradr  to_prim)r  rD  rM  r  rT  r  s         rB   r  r  X  s'     1OO'  rH   )rx   zpaddle.pir.ProgramrV  rX  )r  rW  rV  rX  )t
__future__r   ro  r   r  rE   rq  r   typingr   r   r   r   r   typing_extensionsr	   r
   rr   paddle.pir.corers   r   r  r   r   paddle.baser   paddle.base.data_feederr   paddle.base.dygraph.baser   r   r   paddle.frameworkr   r   paddle.nn.layerr   
paddle.pirr   paddle.utilsr   r   rJ   r   r   r   r   r   r   r   origin_infor    r!   r"   ry  r#   r$   rx  PirPartialProgramLayerPirPartialProgramLayerHooktransformersr%   utilsr&   r'   r(   r)   r*   r+   r,   r-   r.   r/   r0   r1   r2   r3   paddle._typingr4   paddle.staticr5   r6   paddle.static.amp.fp16_utilsr7   r8   r9   __all__r  rG   rY   r~   r   rF   r   r   r   r   r   r   rm  ro  r  r  r  r_  r	  r  r!  r8  rb  r   r|  r   r  	frozensetr  r   rH   rB   <module>r     s   #   	    A A -  # # + ' . 
 : "  & "  
 J -   " -07
I

  *,&^B: B:J nn/8iO iOX(S:WWe^, S:l
F0^ F0R^#w~6 ^#B	
3 3l}
 }
@ .- -`B. BJ87 88u1 u1p$!( $!N?1 ?1D'1T  kk rH   