
    ϑiC.                       S SK Jr  S SKrS SKrS SKJrJr  S SKrS SKJrJ	r	J
r
  S SKJr  S SKJrJr  \(       a  S SKJr  S SKJr  S S	KJr  \" S
\\\   5      r\
R,                   S       SS jj5       r\
R,                   S       SS jj5       r\
R,                  \" 5       \" 5       SS4           SS jj5       rg)    )annotationsN)TYPE_CHECKINGTypeVar)backwardcore	framework)prim_config)primxutils)Sequence)Tensor)Block_TensorOrTensorsTc                  ^	 [         R                  " 5       (       d  [        S5      e[        U [        R
                  [        R                  [        R                  R                  45      (       d  [        S[        U 5       S35      e[        U[        R
                  [        R                  [        R                  R                  45      (       d  [        S[        U5       S35      e[         R                  " U 5      [         R                  " U5      [         R                  " U5      pTn[        R                  " 5       R                  5       m	[!        U	4S jXC-    5       5      (       a  [        S5      e["        R$                  " T	5        ["        R&                  " US   R(                  5      nUR+                  XCU5      u  px[        U [        R
                  [        R                  R                  45      (       a  US   $ U$ )a  Forward mode of automatic differentiation.

Note:
    **ONLY available in the static graph mode and primitive operators.**

Args:
    outputs(Tensor|Sequence[Tensor]): The output tensor or tensors.
    inputs(Tensor|Sequence[Tensor]): The input tensor or tensors.
    grad_inputs(Tensor|Sequence[Tensor]): Optional, the gradient Tensor or
        Tensors of inputs which has the same shape with inputs, Defaults to
        None, in this case is equivalent to all ones.

Returns:
    grad_outputs(Tensor|Sequence[Tensor]): The gradients for outputs.

Examples:

    .. code-block:: python

        >>> # doctest: +SKIP('Transform NOT has linearize')
        >>> import numpy as np
        >>> import paddle

        >>> paddle.enable_static()
        >>> paddle.incubate.autograd.enable_prim()

        >>> startup_program = paddle.static.Program()
        >>> main_program = paddle.static.Program()

        >>> with paddle.static.program_guard(main_program, startup_program):
        ...     x = paddle.static.data('x', shape=[1], dtype='float32')
        ...     y = x * x
        ...     y_grad = paddle.incubate.autograd.forward_grad(y, x)
        ...     paddle.incubate.autograd.prim2orig()
        ...
        >>> exe = paddle.static.Executor()
        >>> exe.run(startup_program)
        >>> y_grad = exe.run(main_program, feed={'x': np.array([2.]).astype('float32')}, fetch_list=[y_grad])
        >>> print(y_grad)
        [array([4.], dtype=float32)]

        >>> paddle.incubate.autograd.disable_prim()
        >>> paddle.disable_static()
zRforward_grad must be running on primitiveoperators, use enable_prim to turn it on.5Expected outputs is Tensor|Sequence[Tensor], but got .4Expected inputs is Tensor|Sequence[Tensor], but got c              3  @   >#    U  H  oR                   T:g  v   M     g 7fNblock.0xr   s     `/var/www/html/banglarbhumi/venv/lib/python3.13/site-packages/paddle/incubate/autograd/primapi.py	<genexpr>forward_grad.<locals>.<genexpr>r   s     
-W77eWs   zMVariable in inputs and targets should exist in current block of main program.r   )r   prim_enabledRuntimeError
isinstancer   Variabletypingr   paddlepirValue	TypeErrortype
as_tensorsdefault_main_programcurrent_blockanyr
   	orig2prim	Transformr   	linearize)
outputsinputsgrad_inputsysxsxs_dotad_ys_dotr   s
            @r   forward_gradr8   #   s   d 8
 	

 )$$foovzz7G7GH  G}oQ(
 	

 ##V__fjj6F6FG  F|nA'
 	
 	! % B **,::<E

-RW
---
 	

 
OOE	A	%BRV,IA g	 2 2FJJ4D4DEFF 	q	     c                N  ^ [         R                  " 5       (       d  [        R                  " XU5      n[	        U[
        R                  [        R                  R                  45      (       a3  [	        U[        R                  5      (       a  [        U5      S:  a  US   $ U$ [	        U [
        R                  [        R                  [        R                  R                  45      (       d  [        S[        U 5       S35      e[	        U[
        R                  [        R                  [        R                  R                  45      (       d  [        S[        U5       S35      e[         R                  " U 5      [         R                  " U5      [         R                  " U5      pen[
        R                   " 5       R#                  5       m[%        U4S jXT-    5       5      (       a  ['        S5      e[(        R*                  " T5        [(        R,                  " T5      nUR/                  XT5      u  p[%        S U	 5       5      (       a  ['        S5      eUR1                  XU5      u  pj/ nU HS  nUc  M  TR2                  R5                  UR6                  5      nUS:  a  [9        S	U S35      eUR;                  U5        MU     UR=                  [?        U5      5        URA                  U5        [	        U[
        R                  [        R                  R                  45      (       a  U
S   $ U
$ )
a&  Reverse mode of automatic differentiation.

Note:
    **ONLY available in the static graph mode and primitive operators**

Args:
    outputs(Tensor|Sequence[Tensor]): The output Tensor or Tensors.
    inputs(Tensor|Sequence[Tensor]): The input Tensor or Tensors.
    grad_outputs(Tensor|Sequence[Tensor]): Optional, the gradient Tensor or
        Tensors of outputs which has the same shape with outputs, Defaults
        to None, in this case is equivalent to all ones.

Returns:
    grad_inputs(Tensor|Tensors): The gradients for inputs.

Examples:

    .. code-block:: python

        >>> # doctest: +SKIP('Transform NOT has linearize')
        >>> import numpy as np
        >>> import paddle

        >>> paddle.enable_static()
        >>> paddle.incubate.autograd.enable_prim()

        >>> startup_program = paddle.static.Program()
        >>> main_program = paddle.static.Program()
        >>> with paddle.static.program_guard(main_program, startup_program):
        ...     x = paddle.static.data('x', shape=[1], dtype='float32')
        ...     x.stop_gradients = False
        ...     y = x * x
        ...     x_grad = paddle.incubate.autograd.grad(y, x)
        ...     paddle.incubate.autograd.prim2orig()
        ...
        >>> exe = paddle.static.Executor()
        >>> exe.run(startup_program)
        >>> x_grad = exe.run(main_program, feed={'x': np.array([2.]).astype('float32')}, fetch_list=[x_grad])
        >>> print(x_grad)
        [array([4.], dtype=float32)]

        >>> paddle.incubate.autograd.disable_prim()
        >>> paddle.disable_static()
r   r   r   r   c              3  V   >#    U  H  oS L=(       a    UR                   T:g  v   M      g 7fr   r   r   s     r   r   grad.<locals>.<genexpr>   s$     
AATM.agg..s   &)zQVariable in inputs and outputs should be None or in current block of main programc              3  (   #    U  H  oS L v   M
     g 7fr    )r   vars     r   r   r<      s     
)&3$;&s   zEGrads cannot be computed. The given outputs does not depend on inputsz<op_index should be greater than or equal to 0, but op_index=)!r   r   r   	gradientsr    r   r!   r#   r$   r%   r"   r   lenr&   r'   r(   r)   r*   r+   r   r
   r,   r-   r.   	transposeopsindexop
ValueErrorappend	erase_opssorted
erase_dots)r/   r0   grad_outputsr1   r2   r3   ys_barr5   r4   r7   xs_bar
op_indexesr?   op_indexr   s                 @r   gradrP      s   d ((,G
 v	 2 2FJJ4D4DEFF;88K 1$q>!)$$foovzz7G7GH  G}oQ(
 	

 ##V__fjj6F6FG  F|nA'
 	
 	! & B
 **,::<E

A
AAA_
 	
 
OOE		B\\")NF

)&
)))S
 	
 \\&&9NF J?yysvv.H!| RS[R\\]^  h'  LL
#$MM& fy116::3C3CDEE 	q	 r9   c                  ^^ [         R                  " 5       (       d  g[        U [        R                  R
                  R                  5      (       a#  [        R                  " S5        U R                  nO[        U [        R                  5      (       ad  U  HN  n[        U[        R                  R
                  R                  5      (       a  M8  [        S[        U5       S35      e   U S   R                  nO[        S[        U 5       S35      e[        T[        [        45      (       d  [        S[        T5       S35      e[        T[        [        45      (       d  [        S[        T5       S35      e[         S	   T-  m[
        R"                  " U5         [        R                  " S
5        [%        T5      S:  a  [%        T5      S:  a  UU4S jnOM[%        T5      S:  a  [%        T5      S:X  a  U4S jnO([%        T5      S:X  a  [%        T5      S:  a  U4S jnOS n[&        R(                  " U UUUS9  [         S   n[        R                  " SU 35        SSS5        g! , (       d  f       g= f)ak  Search nonbasic ops which have be registered composite rules and replace them with primitive ops.
The operators in blacklist will be excluded from program when lowering into primitives, and only the
operators in whitelist will be lowering. The priority of blacklist is higher than whitelist, it means
an operator both in blacklist and whitelist will not be lowering.

The finally set that will be lowering is:
    (blocks.ops & ops have decomposite rule & whitelist) - blacklist

Args:
    blacklist(frozenset): The Operators that will be exclude when lowering into primitives.
    whitelist(frozenset): Only the operators in whitelist will be lowering into primitives.
    start_idx(int): If start_idx exceeds -1, ops[start_idx:] will be processed. Default: -1.
    backward_length(int): If backward_length exceeds -1, ops[:-backward_length] will be processed. Default: -1.
Nz,Atomize composite op to primitive ops begin.z:Expect block or sequence of blocks, but sequence contains r   r   z,Expect block or sequence of blocks, but got z5Expected type of blacklist is set|frozenset, but got z5Expected type of whitelist is set|frozenset, but got forward_blacklistz'Lowering composite forward ops begin...c                N   > U R                   T;   =(       a    U R                   T;  $ r   r'   )r   	blacklist	whitelists    r   <lambda>to_prim.<locals>.<lambda>5  s    ) 3 Oi8O Or9   c                "   > U R                   T;  $ r   rU   )r   rV   s    r   rX   rY   7  s    i 7r9   c                "   > U R                   T;   $ r   rU   )r   rW   s    r   rX   rY   9  s    ) 3r9   c                    g)NTr>   )r   s    r   rX   rY   ;  s    r9   )	start_idxbackward_lengthcomposite_ops_recordz'Lowering composite forward ops finish: )r   _is_fwd_prim_enabledr    r#   baser   r   logginginfoprogramr"   r   r&   r'   set	frozensetr	   program_guardrA   r
   _lower_composite)	blocksrV   rW   r]   r^   main_programitemfilter_replace_opss	    ``      r   to_primrn      s   , $$&&&&++//5566CD~~	FFOO	,	,DdFKK$9$9$?$?@@PQUVZQ[P\\]^  
 ay((:4<.J
 	
 i#y!122CDOCTTUV
 	
 i#y!122CDOCTTUV
 	
 /09<I		 	 	.>?y>A#i.1"4OG^aC	Na$77G^q S^a%73G$G+		
 ""89>{mLM% 
/	.	.s   CI##
I1r   )r/   r   r0   r   r1   _TensorOrTensorsT | Nonereturnr   )r/   r   r0   r   rK   ro   rp   r   )ri   zBlock | Sequence[Block]rV   set[str] | frozenset[str]rW   rq   r]   intr^   rr   rp   None)
__future__r   rb   r"   r   r   r#   paddle.baser   r   r   paddle.base.corer	   paddle.incubate.autogradr
   r   collections.abcr   r   paddle.base.frameworkr   r   static_onlyr8   rP   rf   rn   r>   r9   r   <module>r{      sK   #   )  1 1 ( 1(+ 3VXf=MN  -1\\\ *\ 	\ \~  .2yyy +y 	y yx  ,5;+4;BN#BN(BN )BN 	BN
 BN 
BN BNr9   