o
    * iq6                     @  s  d dl mZ d dlZd dlZd dlZd dlZd dl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mZmZmZmZ d dlmZ d dlZd dlZd d	lmZmZmZ d d
lmZm Z  ddl!m"Z"m#Z#m$Z$ ddl%m&Z&m'Z'm(Z( erd dl)mZ d dl*m+Z+ edZ,edZ-edZ.edZ/e0e1e2e3e4de5fZ6G dd de4Z7G dd dZ8G dd dZ9e9 a:edd Z;dd Z<G dd de7d Z=G d!d" d"e7d Z>d#d$ Z?d%d& Z@d'd( ZAd)d* ZBed+d, ZCd-d. ZDd/d0 ZEd1d2 ZFd3d4 ZGd5d6 ZHd7d8 ZId9d: ZJd;d< ZKd~dGdHZLdIdJ ZMdKdL ZNdMdN ZOG dOdP dPZPdQdR ZQdSdT ZRdUdV ZSdd\d]ZTdd^d_ZUd`da ZVG dbdc dce7d ZWddde ZXdfdg ZYG dhdi diZZG djdk dke7d Z[dldm Z\dndo Z]ddsdtZ^ddwdxZ_ddzd{Z`dd|d}ZadS )    )annotationsN)OrderedDict)contextmanager)is_dataclass)	lru_cache)TYPE_CHECKINGAnyCallableTypeVar)WeakValueDictionary)TransformOptionsdataclass_as_dictdataclass_from_dict)flattenmap_structure   )ENV_SOT_LOG_LEVELENV_SOT_SPECIALIZED_DIM_NUMBERSENV_STRICT_MODE)break_graph_functionspaddle_api_listpaddle_api_module_prefix)r	   )NestedStructureTT1T2T3c                      s,   e Zd ZU i Zded< d fddZ  ZS )		Singletonzdict[Any, Any]
_instancesargsr   kwargsc                   s,   | | j vrt j|i || j | < | j |  S N)r   super__call__)clsr   r    	__class__ f/home/app/PaddleOCR-VL-test/.venv_paddleocr/lib/python3.10/site-packages/paddle/jit/sot/utils/utils.pyr#   C   s   

zSingleton.__call__)r   r   r    r   )__name__
__module____qualname__r   __annotations__r#   __classcell__r'   r'   r%   r(   r   @   s   
 r   c                   @  s&   e Zd Zdd Zdd Zdd	d
ZdS )NameGeneratorc                 C  s   d| _ || _d S Nr   )counterprefix)selfr1   r'   r'   r(   __init__J   s   
zNameGenerator.__init__c                 C  s"   | j t| j }|  jd7  _|S Nr   )r1   strr0   r2   namer'   r'   r(   nextN   s   zNameGenerator.nextr7   r5   returnboolc                 C  s   | | jS r!   )
startswithr1   r6   r'   r'   r(   
match_nameS      zNameGenerator.match_nameN)r7   r5   r9   r:   )r)   r*   r+   r3   r8   r<   r'   r'   r'   r(   r.   I   s    r.   c                   @  s2   e Zd Zdd ZdddZddd	Zdd
dZdS )SymbolRegistryc                 C  s&   t dd| _t | _t | _i | _d S )NZ___t_)r1   )r.   symbol_generatorr   Ztmp_names_recordsetdeclared_symbolssymbol_tabler2   r'   r'   r(   r3   X   s   
zSymbolRegistry.__init__r9   r5   c                 C  s
   | j  S r!   )r?   r8   rC   r'   r'   r(   next_symbol^      
zSymbolRegistry.next_symbolexprc                 C  s*   || j v r
| j | S |  }|| j |< |S r!   )rB   rD   )r2   rF   symbolr'   r'   r(   request_symbola   s
   


zSymbolRegistry.request_symbolc                 C  s8   | j | }|| jv r|S | j| d| d|  dS )N(z := (z)))rB   rA   add)r2   rF   Zgen_expr_fnrG   r'   r'   r(   gen_exprh   s
   

zSymbolRegistry.gen_exprNr9   r5   )rF   r5   r9   r5   )rF   r5   )r)   r*   r+   r3   rD   rH   rK   r'   r'   r'   r(   r>   W   s
    

r>   c                  c  s    t } t a d V  | a d S r!   )_symbol_registryr>   )Zoriginal_registryr'   r'   r(   switch_symbol_registrys   s
   rN   c                   C  s   t S r!   )rM   r'   r'   r'   r(   current_symbol_registry|   s   rO   c                   @  s   e Zd ZdddZdd ZdS )	ResumeFnNameFactoryr9   Nonec                 C  s   t d| _d S )NZresume_)r.   genrC   r'   r'   r(   r3         zResumeFnNameFactory.__init__c                 C  s   | j  }|S r!   )rR   r8   r6   r'   r'   r(   r8      s   
zResumeFnNameFactory.nextN)r9   rQ   )r)   r*   r+   r3   r8   r'   r'   r'   r(   rP      s    
rP   )	metaclassc                   @  s$   e Zd Zdd Zdd Zdd ZdS )SIRToCodeMapc                 C  s
   i | _ d S r!   )_maprC   r'   r'   r(   r3      rE   zSIRToCodeMap.__init__c                 C  s   || j |j< d S r!   )rV   r7   )r2   sircoder'   r'   r(   register   s   zSIRToCodeMap.registerc                 C  s   | j |jS r!   )rV   getr7   )r2   rW   r'   r'   r(   rZ      rS   zSIRToCodeMap.getN)r)   r*   r+   r3   rY   rZ   r'   r'   r'   r(   rU      s    rU   c                 G  s(   t  }| |krt|ddd d S d S N T)endflush)r   rZ   print)levelr   	cur_levelr'   r'   r(   log   s   rb   c                 C  s   t  }| |kr|  d S d S r!   r   rZ   )r`   fnra   r'   r'   r(   log_do   s   
re   c                 G  s,   t  }| |krt|j| ddd d S d S r[   )r   rZ   r_   format)r`   r5   r   ra   r'   r'   r(   
log_format   s   rg   c                 C  s   | t  kS r!   rc   )r`   r'   r'   r(   log_enabled   r=   rh   c                 C  s   t | dd d S )NT)r^   )r_   )msgr'   r'   r(   log_once   s   rj   c                       fdd}|S )Nc               	     sP   t jjd }zz	 | i |}W n    W t jj| |S t jj| w r!   )paddleZ	frameworkcoreZset_eval_frame)r   r    Zold_cbretvalfuncr'   r(   no_eval_frame_func   s   z)no_eval_frame.<locals>.no_eval_frame_funcr'   )rp   rq   r'   ro   r(   no_eval_frame   s   
rr   c                 C  s   | dv S )N)z
<listcomp>z
<dictcomp>z	<setcomp>z	<genexpr>r'   )r7   r'   r'   r(   is_comprehensive_name      rs   c                 C  s>   t | tjjr	dS t| drdS t| rdS t| p| tv S )NF__self__)	
isinstancerl   nnZLayerhasattrinspectisclassin_paddle_moduler   ro   r'   r'   r(   is_paddle_api   s   
r|   c                 C  s   t | rdS t| tjj S NT)r|   r   Zcheck_fn_need_transformZToStaticModeZSOT)rd   r'   r'   r(   +already_unified_in_dynamic_and_static_graph   s
   r~   c                 C  sT   t jg}| |v r
dS t| tjrdS ttD ]\}}|| u r't|tr' dS qdS NTF)	weakrefrefrv   typesBuiltinFunctionTypery   
getmembersbuiltinstype)rd   Zspecial_builtin_fnsmember_namememberr'   r'   r(   is_builtin_fn   s   r   c                 C  s   t | dr9| j}|d u rdS tdd|d t | dr"tdd| jd tdd tD ]}||r8tdd	  d
S q)tdd dS )Nr*   F   z&find paddle function with __module__: 
r)   z&                     with __name__  : z&                     with results   : z True
Tz False
)rx   r*   rb   r)   r   r;   )rp   
module_strr1   r'   r'   r(   r{      s"   






r{   c                 C  s   | t v S r!   )r   ro   r'   r'   r(   is_break_graph_api   rt   r   c                 C  s:   t | sdS t| tsdS h d}tt| }||S )NF>   _fields_make_replace_asdict)ry   rz   
issubclasstupler@   dirissubset)r$   Znamedtuple_attrsZ	cls_attrsr'   r'   r(   is_namedtuple_class   s   


r   
structuresNestedStructure[T1]predCallable[[T1], bool]true_fnCallable[[T1], T2]false_fnCallable[[T1], T3]r9   NestedStructure[T2 | T3]c                   s     fdd}t |g|R  S )Nc                    s   |  r|  S  |  S r!   r'   r   r   r   r   r'   r(   replace
  s   zmap_if.<locals>.replace)r   )r   r   r   r   r   r'   r   r(   map_if  s   r   c                 c  s>    t | D ]}t|tr|jV  |jV  |jV  q|V  qd S r!   )r   rv   slicestartstopstep)	structureitemr'   r'   r(   flatten_extend  s   

r   c                   s,   fdd} fdd}t | || dS )z8support extended structures like slice and SliceVariablec                   s,   t | trdS t| rt | tsdS  | S r}   )rv   r   r   r   )xr   r'   r(   wrapped_pred  s
   
z#map_if_extend.<locals>.wrapped_predc                   sn   t | tr| j| j| jg}t| }t| S t| r3t | ts3t| }t| }t	t| |S | S r!   )
rv   r   r   r   r   map_if_extendr   r   r   r   )r   lZdt_dictr   r'   r(   wrapped_true_fn&  s   
z&map_if_extend.<locals>.wrapped_true_fn)r   r   r   )r   )r   r   r   r   r   r   r'   r   r(   r     s
   r   c                   s$    fdd}t tt|g|R  S )Nc                    s    |  rdS dS )Nr   r   r'   r   r   r'   r(   is_true9  s   zcount_if.<locals>.is_true)sumr   r   )r   r   r   r'   r   r(   count_if8  s   r   c                   @  s6   e Zd ZdddZdd Zdd Zdd	 Zd
d ZdS )CacheFc                 C  s$   |si | _ nt | _ d| _|| _d S r/   )cacher   hit_numcopy)r2   Zweakr   r'   r'   r(   r3   B  s
   
zCache.__init__c                 O  s   | j |i |}t|s| j|i |S || jv r6tdd|d |  jd7  _| j| }| jr4t|}|S | j|i |}|| j|< |S )Nr   zcache hit: r   r   )key_fnhashablevalue_fnr   rb   r   r   deepcopy)r2   r   r    	cache_keyZ
cache_itemvaluer'   r'   r(   r#   J  s   



zCache.__call__c                 C  s   | j   d| _d S r/   )r   clearr   rC   r'   r'   r(   r   Y  s   

zCache.clearc                 O     t r!   NotImplementedErrorr2   r   r    r'   r'   r(   r   ]     zCache.key_fnc                 O  r   r!   r   r   r'   r'   r(   r   `  r   zCache.value_fnN)FF)r)   r*   r+   r3   r#   r   r   r   r'   r'   r'   r(   r   A  s    
r   c                   rk   )Nc                    s4   t   } | i |}t   }|| }td| |S )NzExecute time:)timer_   )r   r    
start_timeresultend_timeZexecution_timero   r'   r(   wrappere  s   
zexecute_time.<locals>.wrapperr'   )rp   r   r'   ro   r(   execute_timed  s   r   c                 C  s   d|  d| d| dS )Nz(shape: z	, dtype: z, stop_gradient: )r'   )shapeZdtypeZstop_gradientr'   r'   r(   meta_strp     r   c                   C  s   t  S r!   )r   rZ   r'   r'   r'   r(   is_strict_modet  rt   r   li	list[Any]r   r   intc                 C  s   dd | D  t|S )Nc                 S     g | ]}t |qS r'   id.0itr'   r'   r(   
<listcomp>y      z)list_find_index_by_id.<locals>.<listcomp>)indexr   r   r   r'   r'   r(   list_find_index_by_idx  r   r   c                 C  s   t |dd | D v S )Nc                 S  r   r'   r   r   r'   r'   r(   r   }  r   z&list_contain_by_id.<locals>.<listcomp>r   r   r'   r'   r(   list_contain_by_id|  s   r   c                 C  s   t | j|S r!   )getattrr&   )objr7   r'   r'   r(   get_unbound_method  s   r   c                   @  s   e Zd ZdS )SotUndefinedVarN)r)   r*   r+   r'   r'   r'   r(   r     s    r   c              
   C  6   zt |  W dS  ty } zW Y d }~dS d }~ww r   )hash	TypeErrorr   er'   r'   r(   r        r   c              
   C  r   r   )r5   	Exceptionr   r'   r'   r(   	printable  r   r   c                   @  s    e Zd ZdZdd Zdd ZdS )StepInfo   c                 C  s
   d| _ d S N)
step_countrC   r'   r'   r(   r3     rE   zStepInfo.__init__c                 C  s   | j | jk S r!   )r   BACK_TRACE_STEPSrC   r'   r'   r(   need_back_trace  r=   zStepInfo.need_back_traceN)r)   r*   r+   r   r3   r   r'   r'   r'   r(   r     s    r   c                   @  s@   e Zd Zdd Zedd Zedd Zedd Zd	d
 Z	dS )StepInfoManagerc                 C  s   i | _ d | _d | _d S r!   )step_recordcurrent_codecurrent_step_inforC   r'   r'   r(   r3     s   
zStepInfoManager.__init__c                 c  sn    z.| j }| j}|| _ || jvrt | j|< | j| | _| j jd7  _d V  W || _ || _d S || _ || _w r4   )r   r   r   r   r   )r2   rX   Zold_codeZold_infor'   r'   r(   
step_guard  s   

zStepInfoManager.step_guardc                 C  s   | j d uo	| j  S r!   )r   r   rC   r'   r'   r(   r     s   
zStepInfoManager.need_back_tracec                 C  s   | j jS r!   )r   r   rC   r'   r'   r(   current_step  s   zStepInfoManager.current_stepc                 C  s   | j   d | _d| _d S r   )r   r   r   r   rC   r'   r'   r(   r     s   

zStepInfoManager.clearN)
r)   r*   r+   r3   r   r   propertyr   r   r   r'   r'   r'   r(   r     s    


r   c                 C  sV   | j }| j}t|dkr)tj| }t||r|d | S |dd }t|dksd S )Nr   .)r)   r*   lensysmodulesrx   
rpartition)apiZapi_namer   moduler'   r'   r(   get_api_fullname  s   

r   c                  C  sF   dd t tdd D } tdd | }tdd | }t|t|fS )Nc                 S  s   g | ]\}}|qS r'   r'   )r   _ufuncr'   r'   r(   r     s    z$get_numpy_ufuncs.<locals>.<listcomp>c                 S  s   t | tjS r!   )rv   npr  )r   r'   r'   r(   <lambda>  s    z"get_numpy_ufuncs.<locals>.<lambda>c                 S  
   | j dkS r4   Zninr  r'   r'   r(   r       
 c                 S  r  )N   r  r  r'   r'   r(   r    r  )ry   r   r  filterlist)ZufuncsZunary_ufuncsZbinary_ufuncsr'   r'   r(   get_numpy_ufuncs  s   r  rd   Callable[[], T]list[T]c                 C  s:   ddl m} g }	 z||   W n
 |y   Y |S w q	)Nr   )SotCapturedStopIteration)Zpaddle.jit.sot.utils.exceptionsr  append)rd   r  resr'   r'   r(   do_until_stop_iteration  s   r  original_listnew_contentsc                 C  s   |    | | | S r!   )r   extend)r  r  r'   r'   r(   update_list_inplace  s   
r  r5   c                 C  sh   t | dr| jS t | dr| jS | jj}t | dr/t | jdr/| jj}|dvr/| d| dS | dS )Nr+   r)   r&   r*   )__main__r   r   z())rx   r+   r)   r&   r*   )r   
class_namer   r'   r'   r(   get_obj_stable_repr  s   


r  c                  C  s6   t   } | dv sJ d|  dddd}||  S )N)no0Z01z%Unsupported specialized_dim_numbers: r   r   r  )r   rZ   lower)Zspecialized_dim_numbers_raw_strZto_min_non_specialized_numberr'   r'   r(   get_min_non_specialized_number  s   

r  )
r   r   r   r   r   r   r   r   r9   r   )r   r   r   r   r9   r   )rd   r  r9   r  )r  r  r  r  r9   r  rL   )r9   r   )b
__future__r   r   r   ry   r   r   r   r   collectionsr   
contextlibr   dataclassesr   	functoolsr   typingr   r   r	   r
   r   numpyr  rl   Zpaddle.jit.dy2static.utilsr   r   r   Zpaddle.utilsr   r   Zenvsr   r   r   Zpaddle_api_configr   r   r   collections.abcZpaddle._typingr   r   r   r   r   r   floatr5   r:   r   bytesZ
ConstTypesr   r.   r>   rM   rN   rO   rP   rU   rb   re   rg   rh   rj   rr   rs   r|   r~   r   r{   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r  r  r  r  r  r'   r'   r'   r(   <module>   s   	
	


	#


(


