o
    iOC                     @   s"  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 zd dlZ W n	 ey;   Y nw G dd dZG dd	 d	eZe Zd
d Zdd Zdd Zdd Zdd Zdd Zdd Zdd Zd*ddZdd Zdd Zd d! Zd"d# Zd$d% Z d&d' Z!d(d) Z"dS )+    N)ForkingPickler)register_after_fork)Union)check_serializing_named_tensorc                   @   sL   e Zd ZdZddgZdd Zedd Zdd	 Zd
d Z	dd Z
d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                 C   s   |  | _tjj| _d S N)Z	_weak_refr   torchStorager   )selfstorage r   o/var/www/html/eduruby.in/lip-sync/lip-sync-env/lib/python3.10/site-packages/torch/multiprocessing/reductions.py__init__   s   
zStorageWeakRef.__init__c                 C   s   |  | }||_tjj|_|S r	   )__new__r   r
   r   r   )clsr   instancer   r   r   from_weakref%   s   

zStorageWeakRef.from_weakrefc                 C   s   t j| jS r	   )r
   r   Z_expiredr   r   r   r   r   expired,      zStorageWeakRef.expiredc                 C   s   |  | j d S r	   )r   r   r   r   r   r   __del__/      zStorageWeakRef.__del__c                 C   s   | j S r	   )r   r   r   r   r   __hash__2      zStorageWeakRef.__hash__c                 C   s    t | t |kr
dS | j|jkS )NT)idr   )r   otherr   r   r   __eq__5   s   zStorageWeakRef.__eq__N)__name__
__module____qualname____doc__	__slots__r   classmethodr   r   r   r   r   r   r   r   r   r      s    
r   c                   @   s8   e Zd ZdZdd Zdd Zdd Zdd	 Zd
d ZdS )SharedCachez9dictionary from multiprocessing handles to StorageWeakRefc                 C   s   d| _ |   t| tj d S )N   )limit_after_forkr   r%   r   r   r   r   r   >   s   zSharedCache.__init__c                 C   s   t  | _d S r	   )	threadingLocklockr   r   r   r   r(   H   r   zSharedCache._after_forkc                 C   s6   | j  t| |W  d    S 1 sw   Y  d S r	   )r+   dictget)r   keyr   r   r   r-   K   s   
$zSharedCache.getc                 C   s`   | j # t| || t| | jkr|   W d    d S W d    d S 1 s)w   Y  d S r	   )r+   r,   __setitem__lenr'   free_dead_references)r   r.   storage_refr   r   r   r/   O   s   
"zSharedCache.__setitem__c                 C   sF   d}t |  D ]\}}| r| |= q|d7 }qtd|d | _d S )Nr      r&      )listitemsr   maxr'   )r   liver.   r2   r   r   r   r1   U   s   
z SharedCache.free_dead_referencesN)	r   r    r!   r"   r   r(   r-   r/   r1   r   r   r   r   r%   ;   s    
r%   c                 C   s   t jj| |S r	   )r
   cudaEventZfrom_ipc_handle)devicehandler   r   r   rebuild_eventc   r   r=   c                 C   s   |   }t| j|ffS r	   )Z
ipc_handler=   r;   )eventr<   r   r   r   reduce_eventg   s   r?   c                 C   sL   |\}}}}t j||||}| t jjjkr!t jjj||d}|S ||_|S )Nrequires_grad)r
   _utils_rebuild_tensornn	parameter	ParameterrA   )r   r   metadatastorage_offsetsizestriderA   tr   r   r   rebuild_tensorl   s   rL   c              
   C   s   |d u s|dkr|d||dd}n-t |||	f}|d u r6tj  |||||	||||}t|t||	f< n|j|||d t|tj	rF|n|j
}tjtjj||dd|||}| tjjjkrktjjj||
d}|S |
|_|S )Nr   T)dtyper;   	_internal)r;   wrap_storagerM   rN   r@   )storage_from_cacher
   r9   Z
_lazy_initZ_new_shared_cudar   shared_cacheZ_release_ipc_counter
isinstanceUntypedStorage_untyped_storagerB   rC   r   TypedStoragerD   rE   rF   rA   )Z
tensor_clsZtensor_sizeZtensor_stridetensor_offsetZstorage_clsrM   Zstorage_deviceZstorage_handlestorage_size_bytesstorage_offset_bytesrA   ref_counter_handleref_counter_offsetevent_handleevent_sync_requiredr   Z_storagerK   r   r   r   rebuild_cuda_tensory   sL   


r^   c                 C   s   |   }| jr| jstdt|  tjj|  |j	j
jdkrS| \}}}}}}}}	|  }
t|t|< tt| |  |  |
t|| j||||| j||||	ffS |  |  |  | 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).r9   )Z_typed_storagerA   Zis_leafRuntimeErrorr   r
   utilshooksZwarn_if_has_hooksrU   r;   typeZ_share_cuda_rH   r   rR   r^   rI   rJ   rM   rL   )Ztensorr   r;   r<   rX   rY   rZ   r[   r\   r]   rW   rG   r   r   r   reduce_tensor   sX   \
rc   c                 C   s   t | }|j|jfS r	   )osfstatst_inost_dev)fdstatr   r   r   fd_idS  s   
rj   c                 C   s$   t |}|d u rd S tj|jS r	   )rR   r-   r
   rT   Z_new_with_weak_ptrr   )r   r.   r2   r   r   r   rQ   [  s   
rQ   c              	   C   sh   |  }z)t| t|}|d ur|W t| S | ||}t|tt|< |W t| S t| w r	   )detachrQ   rj   rd   closeZ_new_shared_fd_cpur   rR   )r   dfrI   rh   r   r   r   r   rebuild_storage_fdb  s   rn   c                 C   sx   t | |}|d ur| S |d u rtj|||}n|tj| }tj|||}tj||dd}t|t	|< | S NTrO   )
rQ   Z_shared_decrefr
   rT   Z_new_shared_filename_cpurB   Z_element_sizerV   r   rR   )r   managerr<   rI   rM   r   Z	byte_sizeZuntyped_storager   r   r   rebuild_storage_filenameo  s   rq   c                 C   s   |  S r	   r   )r   r   r   r   rebuild_storage_empty  r   rr   c                 C   s   t jj| |ddS ro   )r
   r   rV   )r   rM   r   r   r   rebuild_typed_storage     rs   c                 C   s   t | j| jffS r	   )rs   rU   rM   r   r   r   r   reduce_typed_storage  r   rv   c                 C   s   || ddS )NT)rP   rN   r   )r   Zstorage_typer   r   r   rebuild_typed_storage_child  s   rw   c                 C   s   t | jt| ffS r	   )rw   rU   rb   ru   r   r   r   reduce_typed_storage_child  rt   rx   c                 C   s   ddl m} | jrtd| dkr-|  }|d }t}t| tjr(|| j	f7 }| 
  n#|  dkr:tt| ffS |  \}}tj|}t|}||f}t}t| t|< |t| f| fS )Nr3   )get_sharing_strategyz>Cannot pickle CUDA storage; try pickling a CUDA tensor insteadZfile_systemr   ) ry   Zis_cudar_   Z_share_filename_cpu_rq   rS   r
   rV   rM   Z_shared_increfrI   rr   rb   Z_share_fd_cpu_multiprocessingZ	reductionZDupFdrj   rn   r   rR   )r   ry   rG   	cache_keyZrebuildrh   rI   rm   r   r   r   reduce_storage  s*   

r}   c                  C   s   t tjjt tjD ]} | jdkrt | t qt | t	 qt tj
jt tjD ]} t | t q+t tjt t tjjjt d S )NrT   )r   registerr
   r9   r:   r?   Z_storage_classesr   r}   rx   r   rV   rv   Z_tensor_classesrc   ZTensorrD   rE   rF   )rK   r   r   r   init_reductions  s   


r   r	   )#r{   rd   r)   Zmultiprocessing.reductionr   Zmultiprocessing.utilr   typingr   r
   Ztorch.utils.hooksZtorch._namedtensor_internalsr   Zmultiprocessing.resource_sharerImportErrorr   r,   r%   rR   r=   r?   rL   r^   rc   rj   rQ   rn   rq   rr   rs   rv   rw   rx   r}   r   r   r   r   r   <module>   sD    $%D 
