o
    ki8Z                     @   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Zd dlmZ zd dl	Z W n	 e
y1   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 Zdd Zdd Zdd  Zd!d" Zd#d$ Zd5d%d&Zd'd( Zd)d* Zd+d, Z d-d. Z!d/d0 Z"d1d2 Z#d3d4 Z$dS )6    N)	reduction)register_after_fork)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)	_weak_refr   torchStorager   )selfstorage r   j/var/www/addictedbytheproject.nl/epg/venv/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   _expiredr   r   r   r   r   expired-      zStorageWeakRef.expiredc                 C   s   |  | j d S r   )r   r   r   r   r   r   __del__0      zStorageWeakRef.__del__c                 C   s   | j S r   )r   r   r   r   r   __hash__3      zStorageWeakRef.__hash__c                 C   s    t | t |kr
dS | j|jkS )NT)idr   )r   otherr   r   r   __eq__6   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Zdd Zdd	 Zd
d Zdd ZdS )SharedCachez:Dictionary from multiprocessing handles to StorageWeakRef.returnNc                 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*   I   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/   L   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   r0   storage_refr   r   r   r1   P   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   liver0   r4   r   r   r   r3   V   s   
z SharedCache.free_dead_references)r'   N)	r    r!   r"   r#   r   r*   r/   r1   r3   r   r   r   r   r&   <   s    

r&   c                 C   s   t jj| |S r   )r
   cudaEventfrom_ipc_handle)devicehandler   r   r   rebuild_eventd   r   r@   c                 C   s   |   }t| j|ffS r   )
ipc_handler@   r>   )eventr?   r   r   r   reduce_eventh   s   rC   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	ParameterrE   )r   r   metadatastorage_offsetsizestriderE   tr   r   r   rebuild_tensorm   s   rP   c           
      C   s^   t j|dd}t j||dd}t j||||}	| t jjjkr*t jjj|	|d}	|	S ||	_|	S )Nmetar>   Twrap_storagedtype	_internalrD   )	r
   UntypedStorageTypedStoragerF   rG   rH   rI   rJ   rE   )

tensor_clstensor_sizetensor_stridetensor_offsetrU   storage_size_bytesrE   untyped_storagetyped_storagerO   r   r   r   rebuild_meta_tensorz   s   	r`   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)rU   r>   rV   rR   rS   rD   )storage_from_cacher
   r;   
_lazy_init_new_shared_cudar   shared_cache_release_ipc_counter
isinstancerW   _untyped_storagerF   rG   r   rX   rH   rI   rJ   rE   )rY   rZ   r[   r\   storage_clsrU   storage_devicestorage_handler]   storage_offset_bytesrE   ref_counter_handleref_counter_offsetevent_handleevent_sync_requiredr   _storagerO   r   r   r   rebuild_cuda_tensor   sL   


rq   c                 C   s\  | 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rx| \}}}}}}}	}
|  }t|t|< tt| |  |  |t|| j||||| j |||	|
ffS |jjjdkrt 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   )NestedTensorr;   rQ   )#rE   is_leafRuntimeErrorr   r
   utilshookswarn_if_has_hooks$torch.nested._internal.nested_tensorrr   	is_nestedrf   reduce_nested_tensorlayout
sparse_coo
sparse_csr
sparse_bsr
sparse_csc
sparse_bscreduce_sparse_tensor_typed_storagerg   r>   type_share_cuda_rL   r   rd   rq   rM   rN   rU   r`   r^   rP   )tensorrr   r   r>   r?   r]   rk   rl   rm   rn   ro   r\   rK   r   r   r   reduce_tensor   s   _

r   c                 C   s0   | | }|| }	|| }
|| }t ||	|
|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offsetsr   r   r   rebuild_nested_tensor  s
   
r   c           	   	   C   sX   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   r   r   r   rz     s&   rz   c                 C   s"   | | }|| }t j||||dS )N)is_coalesced)r
   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)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   r   r   r    rebuild_sparse_compressed_tensor  s   

r   c              	   C   s   | j tju r#t|  \}}t|  \}}t||||| j|  ffS | j tj	tj
hv r5|  }|  }n| j tjtjhv rG|  }|  }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   r   r   r   r     sL   


r   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   )rd   r/   r
   rW   _new_with_weak_ptrr   )r   r0   r4   r   r   r   ra     s   
ra   c              	   C   sh   |  }z)t| t|}|d ur|W t| S | ||}t|tt|< |W t| S t| w r   )detachra   r   r   close_new_shared_fd_cpur   rd   )r   dfrM   r   r   r   r   r   rebuild_storage_fd  s   r   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 NTrS   )
ra   _shared_decrefr
   rW   _new_shared_filename_cpurF   _element_sizerX   r   rd   )r   managerr?   rM   rU   r   	byte_sizer^   r   r   r   rebuild_storage_filename(  s   
r   c                 C   s   |  S r   r   )r   r   r   r   rebuild_storage_empty:  r   r   c                 C   s   t jj| |ddS r   )r
   r   rX   )r   rU   r   r   r   rebuild_typed_storage>     r   c                 C   s   t | j| jffS r   )r   rg   rU   r   r   r   r   reduce_typed_storageC  r   r   c                 C   s   || ddS )NT)rT   rV   r   )r   storage_typer   r   r   rebuild_typed_storage_childG  s   r   c                 C   s   t | jt| ffS r   )r   rg   r   r   r   r   r   reduce_typed_storage_childL  r   r   c                 C   s   ddl m} | jrtd| jjdkrtd| dkr7|  }|d }t}t| t	j
r2|| jf7 }|   n#|  dkrDtt| ffS |  \}}tj|}t|}||f}t}t| t|< |t| f| fS )Nr5   )get_sharing_strategyz>Cannot pickle CUDA storage; try pickling a CUDA tensor insteadrQ   z>Cannot pickle meta storage; try pickling a meta tensor insteadfile_systemr   ) r   is_cudart   r>   r   _share_filename_cpu_r   rf   r
   rX   rU   _shared_increfrM   r   _share_fd_cpu_multiprocessingr   DupFdr   r   r   rd   )r   r   rK   	cache_keyrebuildr   rM   r   r   r   r   reduce_storageP  s2   

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 ddlm} t |t d S )NrW   r   )rJ   )r   registerr
   r;   r<   rC   _storage_classesr    r   r   r   rX   r   _tensor_classesr   Tensortorch.nn.parameterrJ   )rO   rJ   r   r   r   init_reductionsq  s   


r   r   )%r   r   r+   r   multiprocessing.utilr   r
   torch._namedtensor_internalsr   multiprocessing.resource_sharerImportErrorr   r.   r&   rd   r@   rC   rP   r`   rq   r   r   rz   r   r   r   r   ra   r   r   r   r   r   r   r   r   r   r   r   r   r   <module>   sL   %% D 5/
!