
    i8Z                     &   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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)	reduction)register_after_fork)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     Z/var/www/html/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                   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   r1   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   )r5   dictget)r   keys     r   r8   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   )r5   r7   __setitem__lenr0   free_dead_references)r   r9   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      r/      )listitemsr   maxr0   )r   liver9   r>   s       r   r=   z SharedCache.free_dead_referencesV   sV     $TZZ\ 2 	C""$I			
 dQh'
r   )returnN)	r%   r&   r'   r(   r   r1   r8   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                 ^   t        j                  |d      }t        j                  ||d      }t         j                  j	                  ||||      }	| t         j
                  j                  j                  k(  r-t         j
                  j                  j                  |	|      }	|	S ||	_        |	S )NmetarK   Twrap_storagedtype	_internalrS   )	r   UntypedStorageTypedStoragerU   rV   rW   rX   rY   rT   )

tensor_clstensor_sizetensor_stridetensor_offsetre   storage_size_bytesrT   untyped_storagetyped_storager^   s
             r   rebuild_meta_tensorrp   z   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)re   rK   rf   rb   rc   rS   )storage_from_cacher   rH   
_lazy_init_new_shared_cudar   shared_cache_release_ipc_counter
isinstancerg   _untyped_storagerU   rV   r   rh   rW   rX   rY   rT   )ri   rj   rk   rl   storage_clsre   storage_devicestorage_handlerm   storage_offset_bytesrT   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   )NestedTensorrH   ra   )#rT   is_leafRuntimeErrorr   r   utilshookswarn_if_has_hooks$torch.nested._internal.nested_tensorr   	is_nestedrw   reduce_nested_tensorlayout
sparse_coo
sparse_csr
sparse_bsr
sparse_csc
sparse_bscreduce_sparse_tensor_typed_storagerx   rK   type_share_cuda_r[   r   ru   r   r\   r]   re   rp   rn   r_   )tensorr   r   rK   rL   rm   r|   r}   r~   r   r   rl   rZ   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   )ru   r8   r   rg   _new_with_weak_ptrr   )r   r9   r>   s      r   rr   rr     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   )detachrr   r   r   close_new_shared_fd_cpur   ru   )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   )
rr   _shared_decrefr   rg   _new_shared_filename_cpurU   _element_sizerh   r   ru   )r   managerrL   r\   re   r   	byte_sizern   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   :  s	    5Lr   c                 F    t         j                  j                  | |d      S r   )r   r   rh   )r   re   s     r   rebuild_typed_storager   >  s    ==%%7%SW%XXr   c                 >    t         | j                  | j                  ffS r   )r   rx   re   r   s    r   reduce_typed_storager   C  s    !G$<$<gmm#LMMr   c                      || d      S )NT)rd   rf   r+   )r   storage_types     r   rebuild_typed_storage_childr   G  s    W==r   c                 <    t         | j                  t        |       ffS r   )r   rx   r   r   s    r   reduce_typed_storage_childr   L  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 insteadra   z>Cannot pickle meta storage; try pickling a meta tensor insteadfile_systemr   ) r   is_cudar   rK   r   _share_filename_cpu_r   rw   r   rh   re   _shared_increfr\   r   _share_fd_cpu_multiprocessingr   DupFdr   r   r   ru   )r   r   rZ   	cache_keyrebuildr   r\   r   s           r   reduce_storager   P  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 )Nrg   r   )rY   )r   registerr   rH   rI   rQ   _storage_classesr%   r   r   r   rh   r   _tensor_classesr   Tensortorch.nn.parameterrY   )r^   rY   s     r   init_reductionsr  q  s    uzz''6## >::))q.1q"<=	> u}}113GH"" -1m,- u||]3,y-0r   r   )%r   r   r3   r   multiprocessing.utilr   r   torch._namedtensor_internalsr   multiprocessing.resource_sharerImportErrorr   r7   r-   ru   rM   rQ   r_   rp   r   r   r   r   r   r   r   r   rr   r   r   r   r   r   r   r   r   r  r+   r   r   <module>r     s     	  % 4  G	 +
") ")J!($ !(J }<3

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