
    ϑi"                         S SK r S SKrS SKrS SKrS SKJr  S SKJr  S SKJ	r	  S SK
r
S r " S S\5      r\" 5       rS rS	 rS
\S\S\\   S\4S jrS rS rS rS rS rS rS rSS jrg)    N)OrderedDict)ForkingPickler)register_after_forkc                  .    [         R                  S:w  a  gg)NlinuxFT)sysplatform     j/var/www/html/banglarbhumi/venv/lib/python3.13/site-packages/paddle/incubate/multiprocessing/reductions.py_supported_checkr      s    
||w r   c                   @   ^  \ rS rSrS rS rU 4S jrU 4S jrSrU =r	$ )_LRUSharedCache(   c                 f    SU l         U R                  5         [        U [        R                  5        g )N   )limit_after_forkr   r   selfs    r   __init___LRUSharedCache.__init__)   s%    
D/"="=>r   c                 8    [         R                  " 5       U l        g N)	threadingLocklockr   s    r   r   _LRUSharedCache._after_fork.   s    NN$	r   c                    > U R                       [        TU ]	  U5      n[        TU ]  X5        UsS S S 5        $ ! [         a     S S S 5        g f = f! , (       d  f       g = fr   )r   superpop__setitem__KeyErrorr   keyvalue	__class__s      r   get_LRUSharedCache.get1   sW    YYC(#C/	 Y
   Y
 Ys&   A9
AAAA
Ac                    > U R                       [        TU ]	  U5        [        TU ]  X5        S S S 5        g ! [         a)    [	        U 5      U R
                  :  a  [        TU ]  SS9   NJf = f! , (       d  f       g = f)NF)last)r   r    __delitem__r#   lenr   popitemr"   r$   s      r   r"   _LRUSharedCache.__setitem__:   sj    YY0#C( G+ Y  0t9

*GOO/0 Ys+   A-7A-0A*'A-)A**A--
A;)r   r   )
__name__
__module____qualname____firstlineno__r   r   r(   r"   __static_attributes____classcell__)r'   s   @r   r   r   (   s    ?
%, ,r   r   c                 8    [         R                  U 5      nUc  g U$ r   )shared_cacher(   )r%   	lodtensors     r   _cuda_from_cacher9   G   s!      %Ir   c                 l   U [         R                  R                  R                  :X  aw  [         R                  R                  R                  " UR	                  5       UR                  5       40 UD6nUR                  5       R                  5       R                  U5        U$ Uu  pE[         R                  R                  R                  R                  5       nUR                  5       (       a   UR                  5       R                  U5        O#[         R                  " / UR                  5       S9nXSl        U$ )N)dtype)paddlebase	frameworkEagerParamBaseshape_dtyper&   
get_tensor_share_data_withcoreeagerTensor_is_initialized	to_tensorstop_gradient)clsr8   metadatatensorsizerI   s         r   _rebuild_tensorrN   N   s    
fkk##222&&55OOy//1
5=
 	!!#44Y? M '!!''..0$$&&00;%%b	0@0@0BCF,Mr   blob	dtype_idxdimsdevicec                 .    U R                  XX4U45      nU$ r   )_new_shared_cuda)rJ   rO   rP   rQ   lodrR   r8   s          r   _rebuild_vmm_tensorrV   _   s!     $$dt&%IJIr   c                    U R                  5       nU R                  (       d  U R                  (       d  [        S5      eU R                  R                  5       (       d]  U R                  R                  5       (       d>  U R                  R                  5       (       d  U R                  R                  5       (       a}  [        U 5      [        R                  R                  R                  :X  a!  [        R                  " U R                   5      nOU R"                  U R                  4n[$        [        U 5      X44$ ['        SU R                   S35      e)NzQRefusing to serialize non-leaf tensor which not stop_gradient, you can detach it!z?Only support tensors of CPU/CUDA/CUDAPinned Place, Not support z	 for now!)rB   rI   is_leafRuntimeErrorplaceis_cpu_placeis_gpu_placeis_cuda_pinned_placeis_xpu_placetyper<   r=   r>   r?   copydeepcopy__dict__rM   rN   
ValueError)rL   r8   rK   s      r   _reduce_tensorrd   f   s    !!#I_
 	

 	!!##<<$$&&<<,,..<<$$&&<6;;00???}}V__5HV%9%9:H$v,	!DEEMfll^[de
 	
r   c           	      V    U R                  UUUUUUU45      nUR                  5         U$ r   )_new_shared_filename_shared_decref	rJ   ipc_name	shared_fdrM   type_idxrQ   rU   dataloader_use_file_descriptorr8   s	            r   _rebuild_lodtensor_filenamerm      sB     ((*	

I r   c           	      v    UR                  5       nU R                  UUUUUUU45      nUR                  5         U$ r   )detachrf   rg   rh   s	            r   !_rebuild_lodtensor_filedescriptorrp      sP       "I((*	

I r   c           	          [        X45      nUc!  U R                  XX4XVU45      n	U	[        X4'   U	$ [        R                  R
                  R                  5       n	U	R                  XXEXg45        U	$ r   )r9   rT   r7   r<   r=   rD   DenseTensor_share_buffer_with
rJ   handleoffset_bytesrM   rk   rQ   rU   
device_idxcache_tensorr8   s
             r   _rebuild_cuda_tensorry      s     $V$:;L((44jI
	 09f+,  KK$$002	$$A	
 r   c           	          [        X45      nUc!  U R                  XX4XVU45      n	U	[        X4'   U	$ [        R                  R
                  R                  5       n	U	R                  XXEXg45        U	$ r   )r9   _new_shared_xpur7   r<   r=   rD   rr   rs   rt   s
             r   _rebuild_xpu_tensorr|      s     $V$:;L''44jI
	 09f+,  KK$$002	$$A	
 r   c                     U " 5       $ r   r
   )rJ   s    r   _rebuild_lodtensor_emptyr~      s     5Lr   c                 $   U R                  5       R                  5       (       d#  U R                  5       R                  5       (       a  U R                  5        H  nUS:X  d  M  [        [        U 5      44s  $    [        R                  R                  R                  5       S   nU R                  U5      nU(       aB  [        U5      n[        R                  R                  US   5      US'   [        U5      n[         nO["        nU R%                  5         GO8U R                  5       R'                  5       (       a  [        R                  R                  R)                  5       nU R                  5       R+                  5       nXg:w  a)  [        R                  R                  R-                  U5         U R/                  5       n[1        U5      S:X  a  [2        nO[4        nXg:w  a)  [        R                  R                  R-                  U5        OEU R                  5       R7                  5       (       a  U R9                  5       n[:        nO[=        S5      eU[        U 5      /UQ74$ ! Xg:w  a*  [        R                  R                  R-                  U5        f f = f)Nr   $FLAGS_dataloader_use_file_descriptor      z3We only support pass cpu/gpu/xpu lodtensor for now!)_placer[   r]   r@   r~   r_   r<   r=   rD   globals_share_filenamelistmultiprocessing	reductionDupFdtuplerp   rm   _shared_increfr\   get_cuda_current_device_idgpu_device_idset_cuda_current_device_id_share_cudar-   rV   ry   r^   
_share_xpur|   rY   )r8   dimrl   rK   metalistrebuildprev_idcur_ids           r   _reduce_lodtensorr      s   ''))2244??$Cax04	?2DEE % *0)9)9)A)A)C2*
& ,,*
 *H~H)3399(1+FHQKXH7G1G  "					(	(	*	*++""==?!!#113KK77?	E ,,.H8}!-.   ;;GD					(	(	*	*'')%A
 	
 d9o1122    ;;GD !s   -,I 1Jc                     [        5       (       d  g [        R                  " [        R                  [
        5        [        R                  " [        R                  R                  R                  R                  [
        5        [        R                  " [        R                  R                  R                  [
        5        [        R                  " [        R                  R                  R                  [        5        g r   )r   r   registerr<   rF   rd   r=   rD   rE   r>   r?   rr   r   r
   r   r   init_reductionsr     s    FMM>:FKK,,2299>J,,n FKK,,88:KLr   )returnN)r`   r   r   r   collectionsr   multiprocessing.reductionr   multiprocessing.utilr   r<   r   r   r7   r9   rN   bytesintr   rV   rd   rm   rp   ry   r|   r~   r   r   r
   r   r   <module>r      s     
   # 4 4 ,k ,8  "!$,0IDG
424..03f	Mr   