
    ^jY                        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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)	reduction)register_after_forkc                   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     k/var/www/ramen.bs-engineer-server.com/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__/   s    DJJ'r   c                     | j                   S r
   )r   r   s    r   __hash__zStorageWeakRef.__hash__2   s    zzr   c                 d    t        |       t        |      k(  ry| j                  |j                  k(  S )NT)idr   )r   others     r   __eq__zStorageWeakRef.__eq__5   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   r0   zSharedCache._after_forkH   s    NN$	r   c                 r    | j                   5  t        j                  | |      cd d d        S # 1 sw Y   y xY wr
   )r4   dictget)r   keys     r   r7   zSharedCache.getK   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
   )r4   r6   __setitem__lenr/   free_dead_references)r   r8   storage_refs      r   r:   zSharedCache.__setitem__O   sL    YY 	,T344y4::%))+	, 	, 	,s   A AAc                     d}t        | j                               D ]  \  }}|j                         r| |= |dz  }  t        d|dz        | _        y )Nr      r.      )listitemsr   maxr/   )r   liver8   r=   s       r   r<   z SharedCache.free_dead_referencesU   sV     $TZZ\ 2 	C""$I			
 dQh'
r   )returnN)	r$   r%   r&   r'   r   r0   r7   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_eventrL   c   s    ::++FF;;r   c                 J    | j                         }t        | j                  |ffS r
   )
ipc_handlerL   rJ   )eventrK   s     r   reduce_eventrP   g   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	ParameterrS   )r   r   metadatastorage_offsetsizestriderS   ts           r   rebuild_tensorr^   l   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rJ   Twrap_storagedtype	_internalrR   )	r   UntypedStorageTypedStoragerT   rU   rV   rW   rX   rS   )

tensor_clstensor_sizetensor_stridetensor_offsetrd   storage_size_bytesrS   untyped_storagetyped_storager]   s
             r   rebuild_meta_tensorro   y   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)rd   rJ   re   ra   rb   rR   )storage_from_cacher   rG   
_lazy_init_new_shared_cudar   shared_cache_release_ipc_counter
isinstancerf   _untyped_storagerT   rU   r   rg   rV   rW   rX   rS   )rh   ri   rj   rk   storage_clsrd   storage_devicestorage_handlerl   storage_offset_bytesrS   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        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|j/                         \  }}}}}}}	}
| j1                         }t3        |      t4        |<   t6        t-        |       | j9                         | j;                         |t-        |      | j<                  ||||| j                   |||	|
ffS |j(                  j*                  j,                  dk(  rrt>        t-        |       | j9                         | j;                         | j1                         | j<                  | jA                         j9                         | j                   ffS | j1                         | j9                         | j;                         | j                   f}tB        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   )NestedTensorrG   r`   )"rS   is_leafRuntimeErrorr   utilshookswarn_if_has_hooks$torch.nested._internal.nested_tensorr   	is_nestedrv   reduce_nested_tensorlayout
sparse_coo
sparse_csr
sparse_bsr
sparse_csc
sparse_bscreduce_sparse_tensor_typed_storagerw   rJ   type_share_cuda_rZ   r   rt   r   r[   r\   rd   ro   rm   r^   )tensorr   r   rJ   rK   rl   r{   r|   r}   r~   r   rk   rY   s                r   reduce_tensorr      s<   FNNB
 	
 
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
   )rt   r7   r   rf   _new_with_weak_ptrr   )r   r8   r=   s      r   rq   rq     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
   )detachrq   r   r   close_new_shared_fd_cpur   rt   )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 NTrb   )
rq   _shared_decrefr   rf   _new_shared_filename_cpurT   _element_sizerg   r   rt   )r   managerrK   r[   rd   r   	byte_sizerm   s           r   rebuild_storage_filenamer   &  s    9KCQW9XG%%''}&&??QUV5<<55e<<	  99'69U 	 $$(
 *'2L!!##r   c                      |        S r
   r*   )r   s    r   rebuild_storage_emptyr   8  s	    5Lr   c                 F    t         j                  j                  | |d      S r   )r   r   rg   )r   rd   s     r   rebuild_typed_storager   <  s    ==%%7%SW%XXr   c                 >    t         | j                  | j                  ffS r
   )r   rw   rd   r   s    r   reduce_typed_storager   A  s    !G$<$<gmm#LMMr   c                      || d      S )NT)rc   re   r*   )r   storage_types     r   rebuild_typed_storage_childr   E  s    W==r   c                 <    t         | j                  t        |       ffS r
   )r   rw   r   r   s    r   reduce_typed_storage_childr   J  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 )Nr?   )get_sharing_strategyz>Cannot pickle CUDA storage; try pickling a CUDA tensor insteadr`   z>Cannot pickle meta storage; try pickling a meta tensor insteadfile_systemr   ) r   is_cudar   rJ   r   _share_filename_cpu_r   rv   r   rg   rd   _shared_increfr[   r   _share_fd_cpu_multiprocessingr   DupFdr   r   r   rt   )r   r   rY   	cache_keyrebuildr   r[   r   s           r   reduce_storager   N  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 )Nrf   r   )rX   )r   registerr   rG   rH   rP   _storage_classesr$   r   r   r   rg   r   _tensor_classesr   Tensortorch.nn.parameterrX   )r]   rX   s     r   init_reductionsr   o  s    uzz''6## >::))q.1q"<=	> u}}113GH"" -1m,- u||]3,y-0r   r
   )#r   r   r2   r   multiprocessing.utilr   r   multiprocessing.resource_sharerImportErrorr   r6   r,   rt   rL   rP   r^   ro   r   r   r   r   r   r   r   r   rq   r   r   r   r   r   r   r   r   r   r*   r   r   <module>r     s     	  % 4 	 +
") ")J!($ !(J }<3

@AHp?fP"2
V(,
^&F
$$Y
N>
T2B1{  		s   B B
	B
