a
    hdZ                     @   sJ  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
 zd dlZ W n eyn   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 Zdd Zdd Zd d! Zd"d# Zd$d% Zd6d&d'Zd(d) Z d*d+ Z!d,d- Z"d.d/ Z#d0d1 Z$d2d3 Z%d4d5 Z&dS )7    N)	reduction)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   N/var/www/auris/lib/python3.9/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.   s    zStorageWeakRef.expiredc                 C   s   |  | j d S r	   )r   r   r   r   r   r   __del__1   s    zStorageWeakRef.__del__c                 C   s   | j S r	   )r   r   r   r   r   __hash__4   s    zStorageWeakRef.__hash__c                 C   s    t | t |krdS | j|jkS )NT)idr   )r   otherr   r   r   __eq__7   s    zStorageWeakRef.__eq__N)__name__
__module____qualname____doc__	__slots__r   classmethodr   r   r   r   r   r   r   r   r   r      s   
r   c                   @   s>   e Zd ZdZddddZdd Zdd	 Zd
d Zdd ZdS )SharedCachez:Dictionary from multiprocessing handles to StorageWeakRef.N)returnc                 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&   J   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+   M   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-   Q   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,   r0   r   r   r   r/   W   s    
z SharedCache.free_dead_references)	r   r   r   r   r   r&   r+   r-   r/   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_evente   s    r:   c                 C   s   |   }t| j|ffS r	   )Z
ipc_handler:   r8   )eventr9   r   r   r   reduce_eventi   s    r<   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	Parameterr>   )r   r   metadatastorage_offsetsizestrider>   tr   r   r   rebuild_tensorn   s    rI   c           
      C   s\   t j|dd}t j||dd}t j||||}	| t jjjkrRt jjj|	|d}	n||	_|	S )Nmetar8   Twrap_storagedtype	_internalr=   )	r
   UntypedStorageTypedStorager?   r@   rA   rB   rC   r>   )

tensor_clstensor_sizetensor_stridetensor_offsetrN   storage_size_bytesr>   untyped_storageZtyped_storagerH   r   r   r   rebuild_meta_tensor{   s    	rX   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)rN   r8   rO   rK   rL   r=   )storage_from_cacher
   r6   Z
_lazy_initZ_new_shared_cudar   shared_cacheZ_release_ipc_counter
isinstancerP   _untyped_storager?   r@   r   rQ   rA   rB   rC   r>   )rR   rS   rT   rU   Zstorage_clsrN   Zstorage_deviceZstorage_handlerV   storage_offset_bytesr>   ref_counter_handleref_counter_offsetevent_handleevent_sync_requiredr   Z_storagerH   r   r   r   rebuild_cuda_tensor   sJ    


rb   c                 C   s^  | j r| jstdt|  tjj|  ddlm	} | j
rNt| |sNt| S | jtjtjtjtjtjhv rtt| S |  }|jjjdkr| \}}}}}}}	}
|  }t|t|< tt| |  |  |t|| j||||| j |||	|
ffS |jjjdkr2t 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   )NestedTensorr6   rJ   )#r>   Zis_leafRuntimeErrorr   r
   utilshooksZwarn_if_has_hooksZ$torch.nested._internal.nested_tensorrc   Z	is_nestedr[   reduce_nested_tensorlayout
sparse_coo
sparse_csr
sparse_bsr
sparse_csc
sparse_bscreduce_sparse_tensorZ_typed_storager\   r8   typeZ_share_cuda_rE   r   rZ   rb   rF   rG   rN   rX   rW   rI   )Ztensorrc   r   r8   r9   rV   r]   r^   r_   r`   ra   rU   rD   r   r   r   reduce_tensor   s    _

rp   c                 C   s0   | | }|| }	|| }
|| }t ||	|
|S r	   )r
   Z_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offsetsr   r   r   rebuild_nested_tensor  s
    
r}   c           	   	   C   sX   t |  \}}t |  \}}t |  \}}t |  \}}t||||||||ffS r	   )rp   valuesZ_nested_tensor_sizeZ_nested_tensor_stridesZ_nested_tensor_storage_offsetsr}   )	ntrq   rr   rs   rt   ru   rv   rw   rx   r   r   r   rg     s&    rg   c                 C   s"   | | }|| }t j||||dS )N)is_coalesced)r
   Zsparse_coo_tensor)rebuild_indices_funcrebuild_indices_argsrebuild_values_funcrebuild_values_argsshaper   indicesr~   r   r   r   rebuild_sparse_coo_tensor  s    r   c                 C   s,   | | }|| }	|| }
t j||	|
||dS )N)rh   )r
   Zsparse_compressed_tensor)rebuild_compressed_indices_funcrebuild_compressed_indices_argsrebuild_plain_indices_funcrebuild_plain_indices_argsr   r   r   rh   compressed_indicesplain_indicesr~   r   r   r    rebuild_sparse_compressed_tensor  s    

r   c              	   C   s   | j tju rFt|  \}}t|  \}}t||||| j|  ffS | j tj	tj
hv rj|  }|  }n.| j tjtjhv r|  }|  }n
t| j t|\}}t|\}	}
t|  \}}t|||	|
||| j| j ffS d S r	   )rh   r
   ri   rp   Z_indicesZ_valuesr   r   r   rj   rk   Zcrow_indicesZcol_indicesrl   rm   Zccol_indicesZrow_indicesNotImplementedErrorr~   r   )sparser   r   r   r   r   r   r   r   r   r   r   r   r   rn     sL    


rn   c                 C   s   t | }|j|jfS r	   )osfstatst_inost_dev)fdstatr   r   r   fd_id  s    
r   c                 C   s$   t |}|d u rd S tj|jS r	   )rZ   r+   r
   rP   Z_new_with_weak_ptrr   )r   r,   r0   r   r   r   rY     s    
rY   c              	   C   sl   |  }zRt| t|}|d ur0|W t| S | ||}t|tt|< |W t| S t| 0 d S r	   )detachrY   r   r   closeZ_new_shared_fd_cpur   rZ   )r   dfrF   r   r   r   r   r   rebuild_storage_fd  s    

r   c                 C   sx   t | |}|d ur| S |d u r4tj|||}n0|tj| }tj|||}tj||dd}t|t	|< | S NTrL   )
rY   Z_shared_decrefr
   rP   Z_new_shared_filename_cpur?   Z_element_sizerQ   r   rZ   )r   managerr9   rF   rN   r   Z	byte_sizerW   r   r   r   rebuild_storage_filename)  s    r   c                 C   s   |  S r	   r   )r   r   r   r   rebuild_storage_empty=  s    r   c                 C   s   t jj| |ddS r   )r
   r   rQ   )r   rN   r   r   r   rebuild_typed_storageA  s    r   c                 C   s   t | j| jffS r	   )r   r\   rN   r   r   r   r   reduce_typed_storageF  s    r   c                 C   s   || ddS )NT)rM   rO   r   )r   Zstorage_typer   r   r   rebuild_typed_storage_childJ  s    r   c                 C   s   t | jt| ffS r	   )r   r\   ro   r   r   r   r   reduce_typed_storage_childO  s    r   c                 C   s   ddl m} | jrtdn| jjdkr2tdn| dkrr|  }|d }t}t| t	j
rh|| jf7 }|   nF|  dkrtt| ffS |  \}}tj|}t|}||f}t}t| t|< |t| f| fS )Nr1   )get_sharing_strategyz>Cannot pickle CUDA storage; try pickling a CUDA tensor insteadrJ   z>Cannot pickle meta storage; try pickling a meta tensor insteadZfile_systemr   ) r   Zis_cudard   r8   ro   Z_share_filename_cpu_r   r[   r
   rQ   rN   Z_shared_increfrF   r   Z_share_fd_cpu_multiprocessingr   ZDupFdr   r   r   rZ   )r   r   rD   	cache_keyZrebuildr   rF   r   r   r   r   reduce_storageS  s2    

r   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 ddlm} t |t d S )NrP   r   )rC   )r   registerr
   r6   r7   r<   Z_storage_classesr   r   r   r   rQ   r   Z_tensor_classesrp   ZTensorZtorch.nn.parameterrC   )rH   rC   r   r   r   init_reductionst  s    


r   )N)'r   r   r'   r   Zmultiprocessing.utilr   typingr   r
   Ztorch._namedtensor_internalsr   Zmultiprocessing.resource_sharerImportErrorr   r*   r"   rZ   r:   r<   rI   rX   rb   rp   r}   rg   r   r   rn   r   rY   r   r   r   r   r   r   r   r   r   r   r   r   r   <module>   sJ   %% D 5/
!