
    Χg|Z                     H   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 n# e$ r Y nw xY w 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'dS )!    N)ForkingPickler)register_after_fork)Union)check_serializing_named_tensorc                   N    e Zd ZdZddgZd Zed             Zd Zd Z	d Z
d	 Zd
S )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                 d    |                                 | _        t          j        j        | _        d S N)	_weak_refr	   torchStorager
   )selfstorages     \/var/www/html/ai-engine/env/lib/python3.11/site-packages/torch/multiprocessing/reductions.py__init__zStorageWeakRef.__init__!   s*    &&((
 $m:    c                 j    |                      |           }||_        t          j        j        |_        |S r   )__new__r	   r   r   r
   )clsr	   instances      r   from_weakrefzStorageWeakRef.from_weakref'   s,    ;;s##"'-">r   c                 J    t           j                            | j                  S r   )r   r   _expiredr	   r   s    r   expiredzStorageWeakRef.expired.   s    }%%dj111r   c                 :    |                      | j                   d S r   )r
   r	   r   s    r   __del__zStorageWeakRef.__del__1   s    DJ'''''r   c                     | j         S r   )r	   r   s    r   __hash__zStorageWeakRef.__hash__4   s
    zr   c                 f    t          |           t          |          k    rdS | j        |j        k    S )NT)idr	   )r   others     r   __eq__zStorageWeakRef.__eq__7   s-    d88r%yy  4zU[((r   N)__name__
__module____qualname____doc__	__slots__r   classmethodr   r   r   r!   r%    r   r   r   r      s          *+I; ; ;   [2 2 2( ( (  ) ) ) ) )r   r   c                   2    e Zd ZdZd	dZd Zd Zd Zd ZdS )
SharedCachez:Dictionary from multiprocessing handles to StorageWeakRef.returnNc                 p    d| _         |                                  t          | t          j                   d S )N   )limit_after_forkr   r.   r   s    r   r   zSharedCache.__init__@   s8     
 	D+"9:::::r   c                 6    t          j                    | _        d S r   )	threadingLocklockr   s    r   r3   zSharedCache._after_forkJ   s    N$$			r   c                 z    | j         5  t                              | |          cd d d            S # 1 swxY w Y   d S r   )r7   dictget)r   keys     r   r:   zSharedCache.getM   s    Y 	' 	'88D#&&	' 	' 	' 	' 	' 	' 	' 	' 	' 	' 	' 	' 	' 	' 	' 	' 	' 	's   044c                     | j         5  t                              | ||           t          |           | j        k    r|                                  d d d            d S # 1 swxY w Y   d S r   )r7   r9   __setitem__lenr2   free_dead_references)r   r;   storage_refs      r   r=   zSharedCache.__setitem__Q   s    Y 	, 	,T34444yy4:%%))+++	, 	, 	, 	, 	, 	, 	, 	, 	, 	, 	, 	, 	, 	, 	, 	, 	, 	,s   A	AA"%A"c                     d}t          |                                           D ]"\  }}|                                r| |= |dz  }#t          d|dz            | _        d S )Nr      r1      )listitemsr   maxr2   )r   liver;   r@   s       r   r?   z SharedCache.free_dead_referencesW   sl     $TZZ\\ 2 2 	 	C""$$ II	dQh''


r   )r/   N)	r&   r'   r(   r)   r   r3   r:   r=   r?   r,   r   r   r.   r.   =   sj        DD; ; ; ;% % %' ' ', , ,( ( ( ( (r   r.   c                 L    t           j        j                            | |          S r   )r   cudaEventfrom_ipc_handle)devicehandles     r   rebuild_eventrN   e   s    :++FF;;;r   c                 J    |                                  }t          | j        |ffS r   )
ipc_handlerN   rL   )eventrM   s     r   reduce_eventrR   i   s%    FEL&122r   c                     |\  }}}}t           j                            ||||          }| t           j        j        j        k    r't           j        j                            ||          }n||_        |S )Nrequires_grad)r   _utils_rebuild_tensornn	parameter	ParameterrU   )r   r   metadatastorage_offsetsizestriderU   ts           r   rebuild_tensorr`   n   sk    2:/ND&-$$WndFKKA
eh *** H((-(HH'Hr   c                 4   t          j        |d          }t          j        ||d          }t           j                            ||||          }	| t           j        j        j        k    r't           j        j                            |	|          }	n||	_        |	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MMMO&$ET  M 	$$		 	A UX'111 H((-(HH'Hr   c           
         ||dk    r |d||d          }nt          |||	f          }|Tt          j                                         |                    ||||	||||          }t          |          t          ||	f<   n|                    |||           t          |t          j	                  r|n|j
        }t          j                            t          j                            ||d          |||          }| t          j        j        j        k    r't          j        j                            ||
          }n|
|_        |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!8+au^tTTT$.*>?
 
 ?J!!###!22"$""#	 	G DRD DL.*>?@@
 ,,"$6~ -    gu344	&%  	$$""QU"VV		 	A UX'111 H((-(HH'Hr   c                    | j         r| j        st          d          t          |            t          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        dk    r|                                \  }}}}}}}	}
|                                 }t5          |          t6          |<   t8          t/          |           |                                 |                                 |t/          |          | j        ||||| j         |||	|
ffS |j        j        j        dk    rt@          t/          |           |                                 |                                 |                                 | j        | !                                                                | j         ffS |                                 |                                 |                                 | 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      s{    
FN 
B
 
 	
 #6***	K''///~ BAAAAA ,
6< @ @ ,#F+++}   $F+++##%%G&+v55   ""		
 --//-g66V  VW"$$""#
 	
( 
	!	(	-	7	7V%%''&&((--//$
 	
 		H T&\\7H=>>r   c                 X     | | } || }	 || }
 || }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     sS     ! "56F 23E""$89G""$89G.vugwOOOr   c           	      D   t          |                                           \  }}t          |                                           \  }}t          |                                           \  }}t          |                                           \  }}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/I,,-:2;Q;Q;S;S-T-T**1>
!!##2 2.. 2?
))++2 2..
 	    		
 r   c                 F     | | } || }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     s:     #"$89G  "56F"7FEUUUUr   c                 R     | | } || }	 || }
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     sX     98	( /.0JKM  "56F)M65   r   c           	         | j         t          j        u rnt          |                                           \  }}t          |                                           \  }}t          ||||| j        |                                 ffS | j         t          j	        t          j
        hv r)|                                 }|                                 }n\| j         t          j        t          j        hv r)|                                 }|                                 }nt#          | j                   t          |          \  }}t          |          \  }	}
t          |                                           \  }}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     s   }(((5B6??CTCT5U5U223@AQAQ3R3R00%$$####%%

 
	
 =U-u/?@@@!'!4!4!6!6"..00MM]u/1ABBB!'!4!4!6!6"..00MM%fm444 ,--	
++ANB
 B
>"$> 4A3Q3Q00,//**##	
 	
r   c                 F    t          j        |           }|j        |j        fS r   )osfstatst_inost_dev)fdstats     r   fd_idr     s      8B<<DK%%r   c                     t                               |          }|d S t          j                            |j                  S r   )rv   r:   r   rh   _new_with_weak_ptrr	   )r   r;   r@   s      r   rs   rs     s:    ""3''Kt22;3DEEEr   c                 h   |                                 }	 t          | t          |                    }||t          j        |           S |                     ||          }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     s    	B$S%))44
 		 ((T22"0"9"9U2YY
s    B ;B B1c                    t          | |          }||                                S |"t          j                            |||          }nZ|t          j                            |          z  }t          j                            |||          }t          j        ||d          }t          |          t          |<   |                                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V5<55e<<<	 99'69UU 	 $(
 
 
 *'22L!!###r   c                      |             S r   r,   )r   s    r   rebuild_storage_emptyr   =  s    355Lr   c                 F    t           j                            | |d          S r   )r   r   ri   )r   rf   s     r   rebuild_typed_storager   A  s     =%%7%SW%XXXr   c                 ,    t           | j        | j        ffS r   )r   ry   rf   r   s    r   reduce_typed_storager   F  s    !G$<gm#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    ''*BDMM)RSSr   c                    ddl m} | j        rt          d          | j        j        dk    rt          d           |            dk    r]|                                 }|d         }t          }t          | t          j
                  r|| j        fz  }|                                  n|                                 dk    rt          t          |           ffS |                                 \  }}t           j                            |          }t'          |          }||f}t(          }t+          |           t,          |<   |t          |           f|z   fS )NrB   )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	0//11QK	*gu122 	)((H    	1		 &W'788))++D&,,R00"II	:$,W55Ld7mm%011r   c                  "   t          j        t          j        j        t
                     t          j        D ]B} | j        dk    rt          j        | t                     (t          j        | t                     Ct          j        t          j
        j        t                     t          j        D ]} t          j        | t                     t          j        t          j        t                     ddlm} t          j        |t                     d S )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    EJ,l;;;# C C:)))#A~6666#A'ABBBBEM68LMMM" 2 2=1111 EL-888,,,,,,I}55555r   r   )(r   r   r5   multiprocessing.reductionr   multiprocessing.utilr   typingr   r   torch._namedtensor_internalsr   multiprocessing.resource_sharerImportErrorr   r9   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 4 4 4 4 4 4 4 4 4 4        G G G G G G	 +**** 	 	 	D	") ") ") ") ") ") ") ")J!( !( !( !( !($ !( !( !(J {}}< < <3 3 3

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