o
    pi3!                     @  sL  d dl mZ d dlZd dlZd dlmZ d dlmZ d dlm	Z	 d dl
mZ d dlZd dlmZ d dlmZ d	d
lm
Z dgZdd Z						d9ddZe	G dd dZG dd dZe	G dd dZedd Zd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<d1d2Z"d;d3d4Z#d5d6 Z$d7d8 Z%dS )=    annotationsN)OrderedDict)contextmanager)	dataclass)Callable)apy_to_axpr_json)	InputSpec   )typingcompilec                 O  sT   t | }i }t|D ]}tdd |D }t| |g|R i |||< q
tt|S )Nc                 s      | ]}|j V  qd S Ndtype).0
input_spec r   b/home/app/PaddleOCR-VL/.venv_paddleocr/lib/python3.10/site-packages/paddle/incubate/cc/compiler.py	<genexpr><       zcompile.<locals>.<genexpr>)_get_input_annotations_get_input_spec_liststuple_compileOverloadedFuncFuncOverloadCtx)funcargskwargsr   dtypes2funcinput_specsdtypesr   r   r   r   8   s   F /tmp/paddle/apcudapaddlePCCc                 C  s   |d usJ t jtj d}t|| |rJ dt j|dd tj	 }	|dv s/J t
||< tjj| ||	d|d}
|sG|
  n|
  |
j|d|
 i\}}|
 |_|rc|jj}n|jj}|W  d    S 1 ssw   Y  d S )Nz/apyzonly support inference nowT)exist_ok)ZCINNr'   )r   build_strategyZ
full_graphbackendZis_train)ospathdirnamer&   __file__r   ZPyToAxprmakedirsZstaticZBuildStrategy_ap_envsZjitZ	to_staticevaltrainZget_concrete_programZ_is_train_modeZtrainingZtrain_programZforward_programZinfer_program)r   r!   r2   ap_pathap_workspace_dirZbackend_deviceZtarget_frameworkZcompile_engineZap_root_pathr)   Z	static_fnZconcrete_programZpartial_program_layer_r   r   r   r   A   s<   




$r   c                   @  s   e Zd ZU ded< dS )r   z"dict[list[paddle.dtype], Callable]r    N__name__
__module____qualname____annotations__r   r   r   r   r   l   s   
 r   c                   @  s&   e Zd Zd
ddZdd Zdd Zd	S )r   func_overload_ctxr   c                 C  s
   || _ d S r   )r;   )selfr;   r   r   r   __init__r   s   
zOverloadedFunc.__init__c                 G  sF   t dd |D }| jj|d }|d usJ | ||g |dS )Nc                 s  r   r   r   )r   Ztensorr   r   r   r   v   r   z*OverloadedFunc.__call__.<locals>.<genexpr>)Zinputs)r   r;   r    getmismatched_debug_info)r<   r   r"   r   r   r   r   __call__u   s   zOverloadedFunc.__call__c                 C  s0   d dd t| jj D }d| d| S )Nz; c                 s  s0    | ]\}}|d  }d|d  d| V  qdS )r   [r
   z] Nr   )r   idxpairr"   r   r   r   r   |   s    
z7OverloadedFunc.mismatched_debug_info.<locals>.<genexpr>zinput signature z# mismatched, valid signatures are: )join	enumerater;   r    items)r<   r"   Zvalid_signaturesr   r   r   r?   {   s   

z$OverloadedFunc.mismatched_debug_infoN)r;   r   )r7   r8   r9   r=   r@   r?   r   r   r   r   r   q   s    
r   c                   @  s   e Zd ZU ded< ded< dS )InputSpecMakeCtxzdict[str, int]name2dtype_num_candidatesname2dtype_candidate_idxNr6   r   r   r   r   rG      s   
 rG   c                 c  s    t jtj d}t jtj d}t jd}t jd}| d|  d| d|d ur0|nd t jd< |t jd< tdg}t|}d|d< t	| d V  |d ur\|t jd< nt jd= |d urj|t jd< nt jd= t	| d S )	Nz/apy/sysz/apy/matmul_passZAP_PATHZAP_WORKSPACE_DIR:r#   ZFLAGS_enable_apT)
r+   r,   r-   r&   r.   environr>   Z	get_flagsdictZ	set_flags)r3   r4   Zap_sys_pathZmatmul_pathZold_ap_pathZold_ap_workspace_dirZ	old_flagsflagsr   r   r   r0      s(   $

r0   c                   s   t |   fdd jD S )Nc                   s   g | ]	} j | }|qS r   r   )r   Zarg_namepct_typeZfull_arg_specr   r   
<listcomp>   s
    
z*_get_input_annotations.<locals>.<listcomp>)inspectgetfullargspecr   )r   r   rO   r   r      s   

r   c                 c  s    t | }t|jdksJ dd |j D }dd |j D }tdd |D }|D ]}t|D ]\}}||j|| < q3t| |V  q-d S )Nr   c                 S     g | ]}|d  qS r   r   r   rC   r   r   r   rP          z)_get_input_spec_lists.<locals>.<listcomp>c                 S  rS   )r
   r   rU   r   r   r   rP      rV   c                 S     g | ]}t |qS r   )range)r   Znum_candidatesr   r   r   rP          )!_create_empty_input_spec_make_ctxlenrH   rF   _cartesian_productrE   rI   _get_input_specs)r   ctxZdtype_var_namesZdtype_num_candidatesZ dtype_candidate_idx_compositionsZidx_compositionZarg_idxcandidate_idxr   r   r   r      s$   r   c                 C  s   t t t }t| | |S r   )rG   r   _init_empty_input_spec_make_ctxr   r^   r   r   r   rZ      s   
rZ   mut_ctxc                 C  s   | D ]}t || qd S r   )3_init_input_spec_make_ctx_name2dtype_num_candidates)r   rb   rN   r   r   r   r`      s   r`   c                 C  sj   t | jtjsJ dt| j d| jj}||jv r*|j| t| jjks(J d S t| jj|j|< d S )Nz)pct_type.dtype should be a DTypeVar, but z were given.)	
isinstancer   pctDTypeVartypenamerH   r[   
candidates)rN   rb   rh   r   r   r   rc      s   

rc   r^   c                   s    fdd| D S )Nc                   s   g | ]}t | qS r   )_get_input_spec)r   rN   r^   r   r   rP      s    z$_get_input_specs.<locals>.<listcomp>r   ra   r   rk   r   r]      s   r]   c                 C  s(   t | tjsJ tt| |t| |dS )N)shaper   )rd   re   ZTensorr	   _get_input_spec_shape_get_input_spec_dtyperN   r^   r   r   r   rj      s
   rj   c                 C  s   dd | j D S )Nc                 S  rW   r   )_get_input_spec_shape_dim)r   dim_varr   r   r   rP      rY   z)_get_input_spec_shape.<locals>.<listcomp>)rl   ro   r   r   r   rm      s   rm   rq   
pct.DimVarc                 C  s4   t | tr| S t | tjsJ t | jtr| jS d S r   )rd   intre   ZDimVarZname_or_value)rq   r   r   r   rp      s   
rp   c                 C  s0   t | jtjs	J | jj}|j| }| jj| S r   )rd   r   re   rf   rh   rI   ri   )rN   r^   rh   r_   r   r   r   rn      s   
rn   c                 C  s   t | dksJ tdg| S )Nr   r   r[   _cartesian_product_impl
lst_of_lstr   r   r   r\      s   r\   c                   s4   t  dkr| S  fdd| D } t|  dd  S )Nr   c                   s(   g | ]} d  D ]	}g ||R qqS rT   r   )r   xyrv   r   r   rP     s   ( z+_cartesian_product_impl.<locals>.<listcomp>r
   rt   )Zcollect_lstrw   r   rv   r   ru     s   ru   )Fr#   r$   r%   r&   r'   )rb   rG   )r^   rG   )rq   rr   )&
__future__r   rQ   r+   collectionsr   
contextlibr   dataclassesr   r   r   r&   Zpaddle.incubate.cc.toolsr   Zpaddle.staticr	   r#   re   __all__r   r   r   r   rG   r0   r   r   rZ   r`   rc   r]   rj   rm   rp   rn   r\   ru   r   r   r   r   <module>   sN   
+
	






	