o
    "j&                     @  s   d Z ddlmZ ddlZddlmZmZ ddlmZm	Z	 ddl
mZmZmZmZ G dd	 d	ZG d
d dZG dd dZG dd deZG dd deZG dd deZG dd deZG dd dZeG dd dZeG dd dZdS )zF
THIS FILE IS PRIVATE !!

use interface in symbolic_context.py first.
    )annotationsN)AnyCallable)is_sequencemap_structure   )NameGenerator
OrderedSet	Singletonflatten_extendc                   @  s   e Zd Zdd Zdd ZdS )	Referencec                 C  s(   || _ |du rt|| _d S || _d S NT)is_weakweakrefref)selfvaluer    r   e/var/www/html/Deteccion_Ine/venv/lib/python3.10/site-packages/paddle/jit/sot/symbolic/statement_ir.py__init__   s   
zReference.__init__c                 C  s   | j du r	|  S | jS r   )r   r   r   r   r   r   __call__&   s   
zReference.__call__N)__name__
__module____qualname__r   r   r   r   r   r   r      s    r   c                   @  sD   e Zd ZdZdddZdd Zdd	 Zd
d Zdd ZdddZ	dS )SymbolzG
    Symbol is used to distinguish a string and a `math variable`.
    namestrc                 C  s
   || _ d S Nr   r   r   r   r   r   r   2      
zSymbol.__init__c                 C     | j S r   r   r   r   r   r   __str__5   s   zSymbol.__str__c                 C  s   t | S r   )r   r   r   r   r   __repr__8      zSymbol.__repr__c                 C  s    t |tr
| j|kS | j|jkS r   )
isinstancer   r   )r   otherr   r   r   __eq__;   s   

zSymbol.__eq__c                 C  
   t | jS r   )hashr   r   r   r   r   __hash__@   r!   zSymbol.__hash__Nc                 C  r)   r   )r   r   )r   memor   r   r   __deepcopy__C   r!   zSymbol.__deepcopy__r   r   r   )
r   r   r   __doc__r   r#   r$   r(   r+   r-   r   r   r   r   r   -   s    
r   c                   @  s*   e Zd ZdZdd
dZdd Zdd ZdS )	Statementz
    Statement is used to represent a sentence of code for building the neural network model,
    which has four types: "call", "api", "method", and "layer".

    Note:
        Statement temporarily does not support control flow.
    typer   r   inputslist[Symbol]outputsstacks	list[str]c                 C  s.   |dv sJ || _ || _|| _|| _|| _d S )N)callapimethodlayer)r   r2   r4   Z
stmt_stackr1   )r   r1   r   r2   r4   r5   r   r   r   r   P   s   
zStatement.__init__c                 C  s:   dd }d | jddt| j   || j| j|| jS )Nc                 S  s2   t | ts	t| s|  S dd | D } d| S )Nc                 s  s    | ]}|  V  qd S r   r#   .0xr   r   r   	<genexpr>e   s    z7Statement.__str__.<locals>.to_string.<locals>.<genexpr>z, )r&   r   r   r#   join)Zinpsr   r   r   	to_stringb   s   
z$Statement.__str__.<locals>.to_stringz{} || {} = {} ({})  
   )formatr1   lenr4   r   r2   )r   rA   r   r   r   r#   a   s   zStatement.__str__c                 C     |   S r   r;   r   r   r   r   r$   o   r%   zStatement.__repr__N)
r1   r   r   r   r2   r3   r4   r3   r5   r6   )r   r   r   r/   r   r#   r$   r   r   r   r   r0   G   s
    
r0   c                         e Zd Zd
 fdd	Z  ZS )CallStatementr   r   r2   r3   r4   r5   r6   c                      t  d|||| || _d S )Nr7   )superr   Zsir_namer   r   r2   r4   r5   	__class__r   r   r   t      
zCallStatement.__init__r   r   r2   r3   r4   r3   r5   r6   r   r   r   r   __classcell__r   r   rL   r   rH   s       rH   c                      rG   )ApiStatementr8   r   r2   r3   r4   r5   r6   c                   s$   t  dd|j ||| || _d S )Nr8   zpaddle.)rJ   r   r   r8   )r   r8   r2   r4   r5   rL   r   r   r      s   
zApiStatement.__init__)r8   r   r2   r3   r4   r3   r5   r6   rP   r   r   rL   r   rS      rR   rS   c                      rG   )MethodStatementr   r   r2   r3   r4   r5   r6   c                   rI   )Nr9   )rJ   r   r9   rK   rL   r   r   r      rN   zMethodStatement.__init__rO   rP   r   r   rL   r   rT      rR   rT   c                      rG   )LayerStatementr:   r   r2   r3   r4   r5   r6   c                   s"   t  d|jj||| || _d S )Nr:   )rJ   r   rM   r   r:   )r   r:   r2   r4   r5   rL   r   r   r      s   
zLayerStatement.__init__)r:   r   r2   r3   r4   r3   r5   r6   rP   r   r   rL   r   rU      rR   rU   c                   @  sd   e Zd ZdZdddZdd Zdd	d
Zdd Zdd Zdd Z	dd Z
dd Zdd Zdd ZdS )StatementIRa  
    StatementIR is the carrier that records the code for building the neural network model.It is
    a representation of a purely computational structure, and does not care about specific values.
    The function converted from StatementIR can ensure that it can be turned into a static state.
    In this way, we can reuse the original `to_static` function to realize the execution of the static graph.

    Note:
        Don't create by yourself, just use the StatementIRCache.get()
    r   r   c                 C  s   || _ g | _g | _g | _d S r   )r   r2   r4   
statementsr    r   r   r   r      s   
zStatementIR.__init__c                 C  r)   r   )rE   rW   r   r   r   r   __len__   r!   zStatementIR.__len__Nc                 C  s2   t | j}t| j|_t| j|_t| j|_|S r   )rV   r   listr2   r4   rW   )r   r,   Znew_sirr   r   r   r-      s
   
zStatementIR.__deepcopy__c                 C     | j | d S r   )r2   append)r   inputr   r   r   	add_input      zStatementIR.add_inputc                 C  rZ   r   )r4   r[   )r   outputr   r   r   
add_output   r^   zStatementIR.add_outputc                 C  s   t |tsJ | j| d S r   )r&   r0   rW   r[   )r   Z	statementr   r   r   add_statement   s   zStatementIR.add_statementc                 C  s|   t  }t  }| jD ]*}t|jD ]}t|tr ||vr || qt|jD ]}t|tr2|| q&q	t|dd d}|S )Nc                 S  r"   r   r   r>   r   r   r   <lambda>       z,StatementIR.analyse_inputs.<locals>.<lambda>)key)	r	   rW   r   r2   r&   r   addr4   sorted)r   Zused_symbolsZgenerated_symbolsstmtinpoutinput_symbolsr   r   r   analyse_inputs   s   



zStatementIR.analyse_inputsc                 C  s|   g }| d| j  | dtdd | j  | dtdd | j  | d | jD ]
}| d|  q.d	|S )
NzStatmentIR: %sz
  inputs: c                 S  r"   r   r   rb   r   r   r   rc      rd   z%StatementIR.__str__.<locals>.<lambda>z  outputs: c                 S  r"   r   r   rb   r   r   r   rc      rd   z  statements: z    
)r[   r   r   r2   r4   rW   r@   )r   strsrh   r   r   r   r#      s   


zStatementIR.__str__c                 C  rF   r   r;   r   r   r   r   r$      r%   zStatementIR.__repr__c                 C  s"   dd | j D }t| j t| S )Nc                 S  s   g | ]	}|j d kr|qS )r:   )r1   r<   r   r   r   
<listcomp>   s    z*StatementIR.graph_size.<locals>.<listcomp>)rW   rE   )r   Zcall_layersr   r   r   
graph_size   s   zStatementIR.graph_sizer.   r   )r   r   r   r/   r   rX   r-   r]   r`   ra   rl   r#   r$   rp   r   r   r   r   rV      s    


rV   c                   @  s:   e Zd ZdZdd Zdd ZdddZd	d
 Zdd ZdS )StatementIRFactoryz-
    It is used to create a StatementIR.
    c                 C  s   i | _ td| _d S )NZSIR_)cacher   name_generatorr   r   r   r   r         zStatementIRFactory.__init__c                 C  
   | j | S r   rr   r   re   r   r   r   __getitem__   r!   zStatementIRFactory.__getitem__Nc                 C  s*   |r|}n| j  }t|}|| j|< |S r   )rs   nextrV   rr   )r   Z
input_namer   Zsirr   r   r   create   s   

zStatementIRFactory.createc                 C  s   |j }|| j|< d S r   )r   rr   )r   Zstmt_irr   r   r   r   update  rt   zStatementIRFactory.updatec                   s.    fdd j  D }|D ]} j |= qd S )Nc                   s   g | ]
} j |r|qS r   )rs   Z
match_name)r=   re   r   r   r   ro   
  s    
z,StatementIRFactory.clear.<locals>.<listcomp>rr   keys)r   Z
want_clearre   r   r   r   clear	  s   

zStatementIRFactory.clearr   )	r   r   r   r/   r   rx   rz   r{   r~   r   r   r   r   rq      s    

rq   c                   @  sf   e Zd ZdZdd Zdd Zdd
dZdddZdddZd ddZ	d!ddZ
d!ddZd!ddZdS )"SIRRuntimeCachezI
    It is used to cache the runtime information of the StatementIR.
    c                 C  s
   i | _ d S r   rv   r   r   r   r   r     r!   zSIRRuntimeCache.__init__c                 C  ru   r   rv   rw   r   r   r   rx      r!   zSIRRuntimeCache.__getitem__re   r   returnboolc                 C  s   || j  v S )zK
        has_key is used to check whether the key is in the cache.
        r|   rw   r   r   r   has_key#  s   zSIRRuntimeCache.has_keyr2   r   c                 C  sH   || j  v r| j | }||d |d f| j |< dS |ddf| j |< dS )z<
        Set Cache origin Inputs of the StatementIR
           r   Nr|   )r   re   r2   valr   r   r   set_origin_inputs)     
z!SIRRuntimeCache.set_origin_inputsr4   c                 C  sH   || j  v r| j | }|d ||d f| j |< dS d|df| j |< dS )z=
        Set Cache origin outputs of the StatementIR
        r   r   Nr|   )r   re   r4   r   r   r   r   set_origin_outputs3  r   z"SIRRuntimeCache.set_origin_outputs	free_varsc                 C  sH   || j  v r| j | }|d |d |f| j |< dS dd|f| j |< dS )z=
        Set Cache free variables of the StatementIR
        r   r   Nr|   )r   re   r   r   r   r   r   set_free_vars=  r   zSIRRuntimeCache.set_free_varsc                 C      || j  v r| j | d S dS )z;
        Get the origin inputs of the StatementIR.
        r   Nr|   rw   r   r   r   get_origin_inputsG     z!SIRRuntimeCache.get_origin_inputsc                 C  r   )z<
        Get the origin outputs of the StatementIR.
        r   Nr|   rw   r   r   r   get_origin_outputsP  r   z"SIRRuntimeCache.get_origin_outputsc                 C  r   )z<
        Get the free variables of the StatementIR.
        r   Nr|   rw   r   r   r   get_free_varsY  r   zSIRRuntimeCache.get_free_varsN)re   r   r   r   )re   r   r2   r   )re   r   r4   r   )re   r   r   r   )re   r   )r   r   r   r/   r   rx   r   r   r   r   r   r   r   r   r   r   r   r     s    








		r   )r/   
__future__r   r   typingr   r   Zpaddle.utilsr   r   utilsr   r	   r
   r   r   r   r0   rH   rS   rT   rU   rV   rq   r   r   r   r   r   <module>   s$   ,G$