
    x-j                         d dl Z d dlZd dlZd dlmZ d dlmZmZ d dlm	Z	 d dl
mZ d dlmZ d dlmZmZ dd	lmZmZ  G d
 d          Z G d d          ZdS )    N)pir)Variablein_pir_mode)build_pipe_for_pylayer)LayerHelper)static_pylayer)flattenpack_sequence_as   )convert_to_staticunwrap_decoratorsc                   R     e Zd Zd Zdedef fdZd Zd Zd Z	d Z
defd	Z xZS )
StaticPyLayerContextc                 ^    g | _         d | _        t                      rd| _        d| _        d S d S )Nzcf.tuple_pushzcf.tuple_pop)
saved_varssaved_vars_structurer   tuple_push_op_nametuple_pop_op_name)selfs    ]/var/www/html/banglarbhumi/venv/lib/python3.11/site-packages/paddle/jit/dy2static/py_layer.py__init__zStaticPyLayerContext.__init__   s=    $(!== 	4&5D#%3D"""	4 	4    attrvaluec                     ddg}t                      rC||vr?t          |t          j                  r%t	          t          j        d| d                    t                                          ||           d S )Nr   r   z                `ctx.a   = tensor` is not allowed in static mode, please use `ctx.save_for_backward(tensor)` instead.

                For example:

                    >>> class ExamplePyLayer(PyLayer):
                    ...     @staticmethod
                    ...     def forward(ctx, x):
                    ...         # ctx.x = x  # This is not allowed in static mode, Replace it with `ctx.save_for_backward(x)`
                    ...         ctx.save_for_backward(x)
                    ...         x1 = paddle.tanh(x)
                    ...         return x1

                    ...     @staticmethod
                    ...     def backward(ctx, grad):
                    ...         # x = ctx.x  # Same as above, replace it with `x, = ctx.saved_tensor()`
                    ...         x, = ctx.saved_tensor()
                    ...         x_grad = grad * (1 - paddle.square(x))
                    ...         return x_grad
                )	r   
isinstancer   ValueAttributeErrortextwrapdedentsuper__setattr__)r   r   r   attr_allow_list	__class__s       r   r"   z StaticPyLayerContext.__setattr__&   s    ')?@MM	O++5#),, , !     0 	D%(((((r   c                 h   t                      rm|| _        t          |          }t          t	          d |                    }t          j                    }|                                }t          ||           d S |D ]3}t          |t                    sJ | j                            |           4d S )Nc                 6    t          | t          j                  S N)r   r   r   )xs    r   <lambda>z8StaticPyLayerContext.save_for_backward.<locals>.<lambda>L   s    Asy!9!9 r   )r   r   r	   listfilterr   get_current_insertion_pointblockr   r   r   r   append)r   tensorsflatten_tensorstensor_elementscurrent_insert_pointcurrent_blocktensors          r   save_for_backwardz&StaticPyLayerContext.save_for_backwardG   s    == 	/(/D%%g..O"99?KK O $'#B#D#D 06688M"=/BBBBB! / /!&(33333&&v..../ /r   c                    t                      rt          j                    }|                                }g }|j        D ]E}|                                | j        k    r&|                                                                }F| j	        jt          | j	                  }d}t          |          D ]/\  }}t          |t          j                  r||         ||<   |dz  }0t          | j	        |          }n]t          d          }	g }| j        D ]D}
|	                    |
j        |
j        |
j        |
j                  }|                    |           E|S )Nr   r   r   )namedtypeshapetype)r   r   r,   r-   opsr7   r   as_tuple_pop_oppop_all_valuesr   r	   	enumerater   r   r
   r   r   create_variabler8   r9   r:   r.   )r   r2   r3   out_listopflattened_structurevalue_cursorir4   helper	saved_varouts               r   saved_tensorz!StaticPyLayerContext.saved_tensorV   sn   == 	%#&#B#D#D 06688MH#' E E7799 666!1133BBDDH(4&-d.G&H&H# !*+>!?!? * *IAv!&#)44 *19,1G+A.$)+-/B  !!788FH!_ % %	,,"#/#/"	 -   $$$$r   c                     t           r'   NotImplementedErrorr   argss     r   mark_not_inplacez%StaticPyLayerContext.mark_not_inplacew       !!r   c                     t           r'   rJ   rL   s     r   mark_non_differentiablez,StaticPyLayerContext.mark_non_differentiable{   rO   r   c                     t           r'   rJ   )r   r   s     r   set_materialize_gradsz*StaticPyLayerContext.set_materialize_grads   rO   r   )__name__
__module____qualname__r   strobjectr"   r5   rH   rN   rQ   boolrS   __classcell__)r$   s   @r   r   r      s        4 4 4) )F ) ) ) ) ) )B/ / /  B" " "" " ""4 " " " " " " " "r   r   c                       e Zd Zd Zd ZdS )StaticPyLayerc                 n   || _         t          |j                  \  }| _        t          |j                  \  }| _        t                      | _        t          j	        t          | j                  | j                  | _        t          j	        t          | j                  | j                  | _        d S r'   )dyfunc_selfr   forwardorig_forward_fnbackwardorig_backward_fnr   static_pylayer_context	functoolspartialr   forward_fn_with_ctxbackward_fn_with_ctx)r   r^   _s      r   r   zStaticPyLayer.__init__   s    &"3K4G"H"H4#4[5I#J#J 4 &:&<&<##,#4d233T5P$
 $
  %.$5d344'%
 %
!!!r   c                 *   t          j        | j        j                  } |j        | j        g|R i |}|                                 d t          |j                                                  D             }t          | j
        || j                  S )Nc                 $    g | ]\  }}|d k    |S )r    ).0rD   items      r   
<listcomp>z'StaticPyLayer.apply.<locals>.<listcomp>   s-     
 
 
41uu uur   )
forward_fninputsbackward_fn)inspect	signaturer^   r_   bindapply_defaultsr>   	argumentsvaluesr   rf   rg   )r   rM   kwargs
dyfunc_sig
bound_args
input_argss         r   applyzStaticPyLayer.apply   s    &t'7'?@@
$Z_T%5GGGGGG
!!###
 
$Z%9%@%@%B%BCC
 
 

 /1
 
 
 	
r   N)rT   rU   rV   r   r|   rk   r   r   r\   r\      s2        
 
 

 
 
 
 
r   r\   )rd   rr   r   paddler   paddle.base.frameworkr   r   paddle.base.libpaddle.pirr   paddle.common_ops_importr   paddle.static.nnr   paddle.utilsr	   r
   program_translatorr   r   r   r\   rk   r   r   <module>r      s               7 7 7 7 7 7 7 7 < < < < < < 0 0 0 0 0 0 + + + + + + 2 2 2 2 2 2 2 2 D D D D D D D Dc" c" c" c" c" c" c" c"L
 
 
 
 
 
 
 
 
 
r   