
    '}hU                     4   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
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dZ d Z!d Z"d Z#d Z$d Z%d Z&d Z'y# e$ r Y dw 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     _/var/www/html/test/engine/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__0   s    DJJ'r   c                     | j                   S r   )r	   r   s    r   __hash__zStorageWeakRef.__hash__3   s    zzr   c                 d    t        |       t        |      k(  ry| j                  |j                  k(  S )NT)idr	   )r   others     r   __eq__zStorageWeakRef.__eq__6   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                   .    e Zd ZdZd Zd Zd Zd Zd Zy)SharedCachez:Dictionary from multiprocessing handles to StorageWeakRef.c                 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_forkI   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.getL   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__P   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_referencesV   sV     $TZZ\ 2 	C""$I			
 dQh'
r   N)	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_eventrM   d   s    ::++FF;;r   c                 J    | j                         }t        | j                  |ffS r   )
ipc_handlerM   rK   )eventrL   s     r   reduce_eventrQ   h   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	ParameterrT   )r   r   metadatastorage_offsetsizestriderT   ts           r   rebuild_tensorr_   m   sx    2:/ND&-$$WndFKA
ehh  *** 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)dtyperK   	_internal)rK   wrap_storagera   rb   rS   )storage_from_cacher   rH   
_lazy_init_new_shared_cudar   shared_cache_release_ipc_counter
isinstanceUntypedStorage_untyped_storagerU   rV   r   TypedStoragerW   rX   rY   rT   )
tensor_clstensor_sizetensor_stridetensor_offsetstorage_clsra   storage_devicestorage_handlestorage_size_bytesstorage_offset_bytesrT   ref_counter_handleref_counter_offsetevent_handleevent_sync_requiredr   _storager^   s                     r   rebuild_cuda_tensorr|   z   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 | j3                         | j;                         | j=                         | j                   f}t@        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   )NestedTensorrH   )!rT   is_leafRuntimeErrorr   r   utilshookswarn_if_has_hooks$torch.nested._internal.nested_tensorr~   	is_nestedrj   reduce_nested_tensorlayout
sparse_coo
sparse_csr
sparse_bsr
sparse_csc
sparse_bscreduce_sparse_tensor_typed_storagerl   rK   type_share_cuda_r[   r   rh   r|   r\   r]   ra   r_   )tensorr~   r   rK   rL   ru   rv   rw   rx   ry   rz   rq   rZ   s                r   reduce_tensorr      s   FNNB
 	
 #6*	KK''/~ B
6< @#F++}}  $F++##%G&&++v5   "		
 --/-g6V  VW"$$$""#
 	
. 		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   e  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   v  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   )rh   r9   r   rk   _new_with_weak_ptrr	   )r   r:   r?   s      r   re   re     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   )detachre   r   r   close_new_shared_fd_cpur   rh   )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 NTrc   )
re   _shared_decrefr   rk   _new_shared_filename_cpurU   _element_sizerm   r   rh   )r   managerrL   r\   ra   r   	byte_sizeuntyped_storages           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   rm   )r   ra   s     r   rebuild_typed_storager     s    ==%%7%SW%XXr   c                 >    t         | j                  | j                  ffS r   )r   rl   ra   r   s    r   reduce_typed_storager     s    !G$<$<gmm#LMMr   c                      || d      S )NT)rd   rb   r,   )r   storage_types     r   rebuild_typed_storage_childr     s    W==r   c                 <    t         | j                  t        |       ffS r   )r   rl   r   r   s    r   reduce_typed_storage_childr   !  s    ''*B*BDM)RSSr   c                    ddl m} | j                  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 insteadfile_systemr   ) r   is_cudar   _share_filename_cpu_r   rj   r   rm   ra   _shared_increfr\   r   r   _share_fd_cpu_multiprocessing	reductionDupFdr   r   r   rh   )r   r   rZ   	cache_keyrebuildr   r\   r   s           r   reduce_storager   %  s    &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                     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               t        j                  t        j                   j"                  j$                  t               y )Nrk   )r   registerr   rH   rI   rQ   _storage_classesr&   r   r   r   rm   r   _tensor_classesr   TensorrW   rX   rY   )r^   s    r   init_reductionsr   B  s    EJJ,,l;## C::))##A~6##A'AB	C EMM668LM"" 2=12 ELL-8EHH..88-Hr   r   )(r   r   r4   multiprocessing.reductionr   multiprocessing.utilr   typingr   r   torch.utils.hookstorch._namedtensor_internalsr   multiprocessing.resource_sharerImportErrorr   r8   r.   rh   rM   rQ   r_   r|   r   r   r   r   r   r   r   re   r   r   r   r   r   r   r   r   r   r,   r   r   <module>r     s     	  4 4    G	 +
") ")J!($ !(J }<3

AHd?NP"2
V(,
^&F
$(Y
N>
T2:I_  		s   B BB