
    sg|Z                     2   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	Z	d dl
mZ 	 d dlZ  G d d      Z G d d	e      Z e       Zd
 Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd dZ d Z!d Z"d Z#d Z$d Z%d Z&d Z'y# e$ r Y gw xY w)!    N)ForkingPickler)register_after_fork)Union)check_serializing_named_tensorc                   F    e Zd ZdZddgZd Zed        Zd Zd Z	d Z
d	 Zy
)StorageWeakRefzA weak reference to a Storage.

    The cdata member is a Python number containing the integer representation of
    the Storage pointer.
    cdata_free_weak_refc                 l    |j                         | _        t        j                  j                  | _        y N)	_weak_refr	   torchStorager
   )selfstorages     S/var/www/html/venv/lib/python3.12/site-packages/torch/multiprocessing/reductions.py__init__zStorageWeakRef.__init__!   s&    &&(
 $mm::    c                 t    | j                  |       }||_        t        j                  j                  |_        |S r   )__new__r	   r   r   r
   )clsr	   instances      r   from_weakrefzStorageWeakRef.from_weakref'   s.    ;;s#"'--">">r   c                 T    t         j                  j                  | j                        S r   )r   r   _expiredr	   r   s    r   expiredzStorageWeakRef.expired.   s    }}%%djj11r   c                 :    | j                  | j                         y r   )r
   r	   r   s    r   __del__zStorageWeakRef.__del__1   s    DJJ'r   c                     | j                   S r   )r	   r   s    r   __hash__zStorageWeakRef.__hash__4   s    zzr   c                 d    t        |       t        |      k(  ry| j                  |j                  k(  S )NT)idr	   )r   others     r   __eq__zStorageWeakRef.__eq__7   s(    d8r%y zzU[[((r   N)__name__
__module____qualname____doc__	__slots__r   classmethodr   r   r   r!   r%    r   r   r   r      sA     *+I;  2()r   r   c                   0    e Zd ZdZddZd Zd Zd Zd Zy)	SharedCachez:Dictionary from multiprocessing handles to StorageWeakRef.Nc                 f    d| _         | j                          t        | t        j                         y )N   )limit_after_forkr   r.   r   s    r   r   zSharedCache.__init__@   s)     
 	D+"9"9:r   c                 6    t        j                         | _        y r   )	threadingLocklockr   s    r   r2   zSharedCache._after_forkJ   s    NN$	r   c                 r    | j                   5  t        j                  | |      cd d d        S # 1 sw Y   y xY wr   )r6   dictget)r   keys     r   r9   zSharedCache.getM   s,    YY 	'88D#&	' 	' 	's   -6c                     | j                   5  t        j                  | ||       t        |       | j                  kD  r| j                          d d d        y # 1 sw Y   y xY wr   )r6   r8   __setitem__lenr1   free_dead_references)r   r:   storage_refs      r   r<   zSharedCache.__setitem__Q   sL    YY 	,T344y4::%))+	, 	, 	,s   A AAc                     d}t        | j                               D ]  \  }}|j                         r| |= |dz  }  t        d|dz        | _        y )Nr      r0      )listitemsr   maxr1   )r   liver:   r?   s       r   r>   z SharedCache.free_dead_referencesW   sV     $TZZ\ 2 	C""$I			
 dQh'
r   )returnN)	r&   r'   r(   r)   r   r2   r9   r<   r>   r,   r   r   r.   r.   =   s    D;%',(r   r.   c                 V    t         j                  j                  j                  | |      S r   )r   cudaEventfrom_ipc_handle)devicehandles     r   rebuild_eventrN   e   s    ::++FF;;r   c                 J    | j                         }t        | j                  |ffS r   )
ipc_handlerN   rL   )eventrM   s     r   reduce_eventrR   i   s$    FELL&122r   c                    |\  }}}}t         j                  j                  ||||      }| t         j                  j                  j
                  k(  r-t         j                  j                  j                  ||      }|S ||_        |S )Nrequires_grad)r   _utils_rebuild_tensornn	parameter	ParameterrU   )r   r   metadatastorage_offsetsizestriderU   ts           r   rebuild_tensorr`   n   sx    2:/ND&-$$WndFKA
ehh  *** HH((-(H H (Hr   c                 ^   t        j                  |d      }t        j                  ||d      }t         j                  j	                  ||||      }	| t         j
                  j                  j                  k(  r-t         j
                  j                  j                  |	|      }	|	S ||	_        |	S )NmetarL   Twrap_storagedtype	_internalrT   )	r   UntypedStorageTypedStoragerV   rW   rX   rY   rZ   rU   )

tensor_clstensor_sizetensor_stridetensor_offsetrf   storage_size_bytesrU   untyped_storagetyped_storager_   s
             r   rebuild_meta_tensorrq   {   s     **+=fMO&&$ETM 	$$		A UXX''111 HH((-(H H (Hr   c           
         ||dk(  r |d||d      }not        |||	f      }|Kt        j                  j                          |j	                  ||||	||||      }t        |      t        ||	f<   n|j                  |||       t        |t        j                        r|n|j                  }t        j                  j                  t        j                  j                  ||d      |||      }| t        j                  j                   j"                  k(  r-t        j                  j                   j#                  ||
      }|S |
|_        |S )Nr   T)rf   rL   rg   rc   rd   rT   )storage_from_cacher   rI   
_lazy_init_new_shared_cudar   shared_cache_release_ipc_counter
isinstancerh   _untyped_storagerV   rW   r   ri   rX   rY   rZ   rU   )rj   rk   rl   rm   storage_clsrf   storage_devicestorage_handlern   storage_offset_bytesrU   ref_counter_handleref_counter_offsetevent_handleevent_sync_requiredr   _storager_   s                     r   rebuild_cuda_tensorr      s^   $ !3q!8au^tT$.*>?
 ?JJ!!#!22"$""#	G DRDL.*>?@
 ,,"$6~ -  gu334 	%%  	$$""QU"V		A UXX''111 HH((-(H H (Hr   c                    | j                   r| j                  st        d      t        |        t        j
                  j                  j                  |        ddlm	} | j                  rt        | |      st        |       S | j                  t        j                  t        j                  t        j                   t        j"                  t        j$                  hv rt'        |       S | j)                         }|j*                  j,                  j.                  dk(  r|j1                         \  }}}}}}}	}
| j3                         }t5        |      t6        |<   t8        t/        |       | j;                         | j=                         |t/        |      | j>                  ||||| j                   |||	|
ffS |j*                  j,                  j.                  dk(  rrt@        t/        |       | j;                         | j=                         | j3                         | j>                  | jC                         j;                         | j                   ffS | j3                         | j;                         | j=                         | j                   f}tD        t/        |       ||ffS )NzCowardly refusing to serialize non-leaf tensor which requires_grad, since autograd does not support crossing process boundaries.  If you just want to transfer the data, call detach() on the tensor before serializing (e.g., putting it on the queue).r   )NestedTensorrI   rb   )#rU   is_leafRuntimeErrorr   r   utilshookswarn_if_has_hooks$torch.nested._internal.nested_tensorr   	is_nestedrx   reduce_nested_tensorlayout
sparse_coo
sparse_csr
sparse_bsr
sparse_csc
sparse_bscreduce_sparse_tensor_typed_storagery   rL   type_share_cuda_r\   r   rv   r   r]   r^   rf   rq   ro   r`   )tensorr   r   rL   rM   rn   r}   r~   r   r   r   rm   r[   s                r   reduce_tensorr      sD   FNNB
 	
 #6*	KK''/~ B
6< @#F++}}  $F++##%G&&++v5   "		
 --/-g6V  VW"$$$""#
 	
( 
	!	!	(	(	-	-	7V%%'&&(--/$$
 	
 		H T&\7H=>>r   c                 Z     | | } || }	 || }
 || }t        j                  ||	|
|      S r   )r   _nested_view_from_buffer_copy)rebuild_buffer_funcrebuild_buffer_argsrebuild_sizes_funcrebuild_sizes_argsrebuild_strides_funcrebuild_strides_argsrebuild_offsets_funcrebuild_offsets_argsbuffersizesstridesoffsetss               r   rebuild_nested_tensorr     sG     !"56F 23E"$89G"$89G..vugwOOr   c           	         t        | j                               \  }}t        | j                               \  }}t        | j                               \  }}t        | j	                               \  }}t
        ||||||||ffS r   )r   values_nested_tensor_size_nested_tensor_strides_nested_tensor_storage_offsetsr   )	ntr   r   r   r   r   r   r   r   s	            r   r   r     s    /<RYY[/I,,-:2;Q;Q;S-T**1>
!!#2.. 2?
))+2..
 	    		
 r   c                 H     | | } || }t        j                  ||||      S )N)is_coalesced)r   sparse_coo_tensor)rebuild_indices_funcrebuild_indices_argsrebuild_values_funcrebuild_values_argsshaper   indicesr   s           r   rebuild_sparse_coo_tensorr     s1     #$89G "56F""7FEUUr   c                 T     | | } || }	 || }
t        j                  ||	|
||      S )N)r   )r   sparse_compressed_tensor)rebuild_compressed_indices_funcrebuild_compressed_indices_argsrebuild_plain_indices_funcrebuild_plain_indices_argsr   r   r   r   compressed_indicesplain_indicesr   s              r    rebuild_sparse_compressed_tensorr     sH     9	( /0JKM "56F))M65 r   c           	         | j                   t        j                  u r^t        | j	                               \  }}t        | j                               \  }}t        ||||| j                  | j                         ffS | j                   t        j                  t        j                  hv r!| j                         }| j                         }nb| j                   t        j                  t        j                  hv r!| j                         }| j!                         }nt#        | j                         t        |      \  }}t        |      \  }	}
t        | j%                               \  }}t&        |||	|
||| j                  | j                   ffS r   )r   r   r   r   _indices_valuesr   r   r   r   r   crow_indicescol_indicesr   r   ccol_indicesrow_indicesNotImplementedErrorr   r   )sparser   r   r   r   r   r   r   r   r   r   s              r   r   r     sq   }}(((5B6??CT5U223@AQ3R00%$$####%

 
	
 ==U--u/?/?@@!'!4!4!6"..0M]]u//1A1ABB!'!4!4!6"..0M%fmm44 ,-	
++ANB
>"$> 4A3Q00,//**##	
 	
r   c                 \    t        j                  |       }|j                  |j                  fS r   )osfstatst_inost_dev)fdstats     r   fd_idr     s$     88B<DKK%%r   c                     t         j                  |      }|y t        j                  j	                  |j
                        S r   )rv   r9   r   rh   _new_with_weak_ptrr	   )r   r:   r?   s      r   rs   rs     s8    ""3'K22;3D3DEEr   c                 <   |j                         }	 t        | t        |            }||t        j                  |       S | j                  ||      }t        |      t        t        |      <   |t        j                  |       S # t        j                  |       w xY wr   )detachrs   r   r   close_new_shared_fd_cpur   rv   )r   dfr]   r   r   s        r   rebuild_storage_fdr     sy    	B$S%)4
 		 ((T2"0"9U2Y
s   B  .B Bc                    t        | |      }||j                         S |"t        j                  j	                  |||      }n[|t        j
                  j                  |      z  }t        j                  j	                  |||      }t        j                  ||d      }t        |      t        |<   |j                         S NTrd   )
rs   _shared_decrefr   rh   _new_shared_filename_cpurV   _element_sizeri   r   rv   )r   managerrM   r]   rf   r   	byte_sizero   s           r   rebuild_storage_filenamer   )  s    ?QV@G %%''}&&??QUV5<<55e<<	  99'69U 	 $$(
 *'2L!!##r   c                      |        S r   r,   )r   s    r   rebuild_storage_emptyr   =  s	    5Lr   c                 F    t         j                  j                  | |d      S r   )r   r   ri   )r   rf   s     r   rebuild_typed_storager   A  s    ==%%7%SW%XXr   c                 >    t         | j                  | j                  ffS r   )r   ry   rf   r   s    r   reduce_typed_storager   F  s    !G$<$<gmm#LMMr   c                      || d      S )NT)re   rg   r,   )r   storage_types     r   rebuild_typed_storage_childr   J  s    W==r   c                 <    t         | j                  t        |       ffS r   )r   ry   r   r   s    r   reduce_typed_storage_childr   O  s    ''*B*BDM)RSSr   c                 b   ddl m} | j                  rt        d      | j                  j
                  dk(  rt        d       |       dk(  rV| j                         }|d   }t        }t        | t        j                        r|| j                  fz  }| j                          nl| j                         dk(  rt        t        |       ffS | j                         \  }}t         j"                  j%                  |      }t'        |      }||f}t(        }t+        |       t,        |<   |t        |       f|z   fS )NrA   )get_sharing_strategyz>Cannot pickle CUDA storage; try pickling a CUDA tensor insteadrb   z>Cannot pickle meta storage; try pickling a meta tensor insteadfile_systemr   ) r   is_cudar   rL   r   _share_filename_cpu_r   rx   r   ri   rf   _shared_increfr]   r   _share_fd_cpu_multiprocessing	reductionDupFdr   r   r   rv   )r   r   r[   	cache_keyrebuildr   r]   r   s           r   reduce_storager   S  s   &L
 	
 
			&L
 	
 
	=	0//1QK	*gu112((H 	1	 &W'788))+D&&,,R0"I	:$,W5Ld7m%011r   c                  l   t        j                  t        j                  j                  t
               t        j                  D ]F  } | j                  dk(  rt        j                  | t               -t        j                  | t               H t        j                  t        j                  j                  t               t        j                  D ]  } t        j                  | t                t        j                  t        j                  t               ddlm} t        j                  |t               y )Nrh   r   )rZ   )r   registerr   rI   rJ   rR   _storage_classesr&   r   r   r   ri   r   _tensor_classesr   Tensortorch.nn.parameterrZ   )r_   rZ   s     r   init_reductionsr  t  s    EJJ,,l;## C::))##A~6##A'AB	C EMM668LM"" 2=12 ELL-8,I}5r   r   )(r   r   r4   multiprocessing.reductionr   multiprocessing.utilr   typingr   r   torch._namedtensor_internalsr   multiprocessing.resource_sharerImportErrorr   r8   r.   rv   rN   rR   r`   rq   r   r   r   r   r   r   r   r   rs   r   r   r   r   r   r   r   r   r  r,   r   r   <module>r
     s     	  4 4   G	 +
") ")J!($ !(J }<3

@AHq?hP"2
V(,
^&F
$(Y
N>
T2B6A  		s   B BB