o
    pi                     @  s   d dl mZ d dlZd dlmZmZmZmZmZ d dl	m
Z
 d dlZd dlmZ ddlmZ er6d dlmZ ed	Ze
d
ZG dd deZed'ddZed(d)ddZd*ddZ	d*dddd+ddZ	d*d,dd Zd-d%d&ZdS ).    )annotationsN)TYPE_CHECKINGCallableProtocolTypeVaroverload)	ParamSpec)DYNAMIC_DIMS_ATTR_NAME   )TransformOptions)Sequence_RetT_InputTc                   @  s*   e Zd Zed
ddZeddddZd	S )_NotToStaticDecoratorfuncCallable[_InputT, _RetT]returnc                 C     d S N selfr   r   r   X/home/app/PaddleOCR-VL/.venv_paddleocr/lib/python3.10/site-packages/paddle/jit/marker.py__call__(      z_NotToStaticDecorator.__call__.Nonec                 C  r   r   r   r   r   r   r   r   -      Nr   r   r   r   .r   r   r   r   )__name__
__module____qualname__r   r   r   r   r   r   r   '   s
    r   r   r   r   c                 C  r   r   r   r   r   r   r   not_to_static1   r   r$   .r   c                 C  r   r   r   r#   r   r   r   r$   7   r   c                 C  s   t | dddS )a  
    A Decorator to suppresses the convention of a function.

    Args:
        func(callable): The function to decorate.

    Returns:
        callable: A function which won't be converted in Dynamic-to-Static.

    Examples:
        .. code-block:: python

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

            >>> @paddle.jit.not_to_static
            ... def func_not_to_static(x):
            ...     res = x - 1
            ...     return res

            >>> @paddle.jit.to_static
            ... def func(x):
            ...     if paddle.mean(x) < 0:
            ...         out = func_not_to_static(x)
            ...     else:
            ...         out = x + 1
            ...     return out
            ...
            >>> x = paddle.ones([1, 2], dtype='float32')
            >>> out = func(x)
            >>> print(out)
            Tensor(shape=[1, 2], dtype=float32, place=Place(cpu), stop_gradient=True,
            [[2., 2.]])
    FTfor_sotfor_ast)unifiedr#   r   r   r   r$   <   s   #Tr%   fn7Callable[_InputT, _RetT] | type[paddle.nn.Layer] | Noner&   boolr'   c                  s0   d
dd | du r fddS  | d	S )aV  
    Mark a function already unified in dygraph and static mode. So
    that it won't be transformed again in SOT or AST mode.

    Args:
        fn(callable): The function to decorate.
        for_sot(bool): Whether to mark the function as unified in SOT mode.
        for_ast(bool): Whether to mark the function as unified in AST mode.
    r&   r+   r'   c                S  sB   t j }|r|t jjO }|r|t jjO }t |d}||  | S )N)Zskip_transform_mode)r   ZToStaticModeZNilZSOTASTattach)r)   r&   r'   modeoptionsr   r   r   _mark_as_unifiedr   s   

z!unified.<locals>._mark_as_unifiedNc                   s    | dS )Nr%   r   )r)   r0   r'   r&   r   r   <lambda>   s    zunified.<locals>.<lambda>r%   )r&   r+   r'   r+   r   )r)   r&   r'   r   r1   r   r(   b   s   
r(   c                 C  sh   ddl m} t| rt| tjjr|jj	
|  | S t| r)|jj	|  | S tdt| j d)z
    Mark a function or paddle.nn.Layer to be executed in dynamic mode, it will
    break the graph and prevent it from being converted to static mode.
    r   )sotz0Expected a callable or paddle.nn.Layer, but got .)Z
paddle.jitr3   inspectisclass
issubclasspaddlennZLayerutilsZpaddle_api_configZadd_break_graph_layer_class
isfunctionZadd_break_graph_function	TypeErrortyper    )r)   r3   r   r   r   force_dynamic   s   
r>   tensorpaddle.Tensordimsint | Sequence[int]c                 C  sv   t | tjstdt| j dt |tr|f}t |ttfs$tdt	dd |D s1tdt
| tt| dS )as  
    Mark a tensor as having dynamic dimensions.
    This is used to indicate that the tensor's shape may change dynamically
    during execution, which is particularly useful in dynamic-to-static
    conversion scenarios.
    Args:
        tensor (paddle.Tensor): The tensor to mark as dynamic.
        dims (int | Sequence[int]): The dimensions to mark as dynamic.
    z"Expected a paddle.Tensor, but got r4   z#Dimensions must be a list or tuple.c                 s  s    | ]}t |tV  qd S r   )
isinstanceint).0dimr   r   r   	<genexpr>   s    zdynamic_dims.<locals>.<genexpr>z All dimensions must be integers.N)rC   r8   ZTensorr<   r=   r    rD   listtupleallsetattrr	   )r?   rA   r   r   r   dynamic_dims   s   
rL   r   r   r   r   )r)   r*   r&   r+   r'   r+   r   r   )r)   r*   r   r   )r?   r@   rA   rB   r   r   )
__future__r   r5   typingr   r   r   r   r   Ztyping_extensionsr   r8   Zpaddle.jit.dy2static.utilsr	   Zdy2static.utilsr   collections.abcr   r   r   r   r$   r(   r>   rL   r   r   r   r   <module>   s2   

'"