a
    dA                     @   s"  d dl Z d dlZ d dlmZ 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 zd dlZW n eyv   Y n0 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)check_serializing_named_tensor)register_after_fork)ForkingPickler)Unionc                   @   s8   e Zd ZdZ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.c                 C   s   |  | _tjj| _d S N)Z	_weak_refcdatatorchStorage_free_weak_ref)selfstorage r   i/var/www/html/stable-diffusion-webui/venv/lib/python3.9/site-packages/torch/multiprocessing/reductions.py__init__   s    
zStorageWeakRef.__init__c                 C   s   t j| jS r   )r	   r
   Z_expiredr   r   r   r   r   expired"   s    zStorageWeakRef.expiredc                 C   s   |  | j d S r   )r   r   r   r   r   r   __del__%   s    zStorageWeakRef.__del__c                 C   s   | j S r   )r   r   r   r   r   __hash__(   s    zStorageWeakRef.__hash__c                 C   s    t | t |krdS | j|jkS )NT)idr   )r   otherr   r   r   __eq__+   s    zStorageWeakRef.__eq__N)	__name__
__module____qualname____doc__r   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   4   s    zSharedCache.__init__c                 C   s   t  | _d S r   )	threadingLocklockr   r   r   r   r   >   s    zSharedCache._after_forkc                 C   s6   | j  t| |W  d    S 1 s(0    Y  d S r   )r"   dictget)r   keyr   r   r   r$   A   s    zSharedCache.getc                 C   sN   | j 4 t| || t| | jkr,|   W d    n1 s@0    Y  d S r   )r"   r#   __setitem__lenr   free_dead_references)r   r%   storage_refr   r   r   r&   E   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   Zliver%   r)   r   r   r   r(   K   s    
z SharedCache.free_dead_referencesN)	r   r   r   r   r   r   r$   r&   r(   r   r   r   r   r   1   s   
r   c                 C   s   t jj| |S r   )r	   cudaEventZfrom_ipc_handle)devicehandler   r   r   rebuild_eventY   s    r3   c                 C   s   |   }t| j|ffS r   )Z
ipc_handler3   r1   )eventr2   r   r   r   reduce_event]   s    r5   c                 C   sJ   |\}}}}t j||||}| t jjjkr@t jjj||d}n||_|S )Nrequires_grad)r	   _utils_rebuild_tensornn	parameter	Parameterr7   )clsr   metadatastorage_offsetsizestrider7   tr   r   r   rebuild_tensorb   s    rC   c              
   C   s   |d u s|dkr"|d||dd}nZt |||	f}|d u rltj  |||||	||||}t|t||	f< n|j|||d t|tj	r|n|j
}tjtjj||dd|||}| tjjjkrtjjj||
d}n|
|_|S )Nr   T)dtyper1   	_internal)r1   wrap_storagerD   rE   r6   )storage_from_cacher	   r/   Z
_lazy_initZ_new_shared_cudar   shared_cacheZ_release_ipc_counter
isinstanceUntypedStorage_untyped_storager8   r9   r   TypedStorager:   r;   r<   r7   )Z
tensor_clsZtensor_sizeZtensor_stridetensor_offsetZstorage_clsrD   Zstorage_deviceZstorage_handlestorage_size_bytesstorage_offset_bytesr7   ref_counter_handleref_counter_offsetevent_handleevent_sync_requiredr   Z_storagerB   r   r   r   rebuild_cuda_tensoro   s4    
	rU   c                 C   s   |   }| jr| jstdt|  tjj|  |j	j
jdkr| \}}}}}}}}	|  }
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).r/   )Z_typed_storager7   Zis_leafRuntimeErrorr   r	   utilshooksZwarn_if_has_hooksrL   r1   typeZ_share_cuda_r?   r   rI   rU   r@   rA   rD   rC   )Ztensorr   r1   r2   rO   rP   rQ   rR   rS   rT   rN   r>   r   r   r   reduce_tensor   sR    \rZ   c                 C   s   t | }|j|jfS r   )osfstatst_inost_dev)fdstatr   r   r   fd_id#  s    
ra   c                 C   s$   t |}|d u rd S tj|jS r   )rI   r$   r	   rK   Z_new_with_weak_ptrr   )r=   r%   r)   r   r   r   rH   +  s    
rH   c              	   C   sl   |  }zRt| t|}|d ur0|W t| S | ||}t|tt|< |W t| S t| 0 d S r   )detachrH   ra   r[   closeZ_new_shared_fd_cpur   rI   )r=   dfr@   r_   r   r   r   r   rebuild_storage_fd2  s    

re   c                 C   sx   t | |}|d ur| S |d u r4tj|||}n0|tj| }tj|||}tj||dd}t|t	|< | S NTrF   )
rH   Z_shared_decrefr	   rK   Z_new_shared_filename_cpur8   Z_element_sizerM   r   rI   )r=   managerr2   r@   rD   r   Z	byte_sizeZuntyped_storager   r   r   rebuild_storage_filename?  s    
rh   c                 C   s   |  S r   r   )r=   r   r   r   rebuild_storage_emptyP  s    ri   c                 C   s   t jj| |ddS rf   )r	   r   rM   )r   rD   r   r   r   rebuild_typed_storageS  s    rj   c                 C   s   t | j| jffS r   )rj   rL   rD   r   r   r   r   reduce_typed_storageW  s    rl   c                 C   s   || ddS )NT)rG   rE   r   )r   Zstorage_typer   r   r   rebuild_typed_storage_childZ  s    rm   c                 C   s   t | jt| ffS r   )rm   rL   rY   rk   r   r   r   reduce_typed_storage_child^  s    rn   c                 C   s   ddl m} | jrtdn| dkr\|  }|d }t}t| tjrR|| j	f7 }| 
  nF|  dkrvtt| ffS |  \}}tj|}t|}||f}t}t| t|< |t| f| fS )Nr*   )get_sharing_strategyz>Cannot pickle CUDA storage; try pickling a CUDA tensor insteadZfile_systemr   ) ro   Zis_cudarV   Z_share_filename_cpu_rh   rJ   r	   rM   rD   Z_shared_increfr@   ri   rY   Z_share_fd_cpu_multiprocessingZ	reductionZDupFdra   re   r   rI   )r   ro   r>   	cache_keyZrebuildr_   r@   rd   r   r   r   reduce_storagea  s&    


rs   c                  C   s   t tjjt tjD ](} | jdkr2t | t qt | t	 qt tj
jt tjD ]} t | t qVt tjt t tjjjt d S )NrK   )r   registerr	   r/   r0   r5   Z_storage_classesr   rs   rn   r   rM   rl   Z_tensor_classesrZ   ZTensorr:   r;   r<   )rB   r   r   r   init_reductions{  s    


ru   )N)#r	   Ztorch.utils.hooksZtorch._namedtensor_internalsr   r[   r    rq   Zmultiprocessing.utilr   Zmultiprocessing.reductionr   typingr   Zmultiprocessing.resource_sharerImportErrorr   r#   r   rI   r3   r5   rC   rU   rZ   ra   rH   re   rh   ri   rj   rl   rm   rn   rs   ru   r   r   r   r   <module>   s@   %( 
