o
    h                     @   s   U d Z ddlmZ ddlmZmZmZmZmZ ddl	Z	ddl	m
Z
 ddlmZmZmZmZmZmZ g dZee ed	< ee
 ZG d
d dZG dd de	jjZG dd de	jjZdS )zkAutograd functions for stream-aware CUDA copy. It is used to overlap copy
and computation on the same GPU.
    )deque)DequeListOptionalTupleSequenceN)Tensor   )AbstractStreamcurrent_stream
get_devicerecord_stream
use_streamwait_stream)ContextCopyWait__all__c                   @   s   e Zd ZU eed< eed< dS )r   prev_streamnext_streamN)__name__
__module____qualname__r
   __annotations__ r   r   Z/var/www/html/ai/venv/lib/python3.10/site-packages/torch/distributed/pipeline/sync/copy.pyr      s   
 r   c                	   @   T   e Zd ZdZededededefddZedede	de
ee	 d	f fd
dZdS )r   z#Copies tensors on specific streams.ctxr   r   returnc              	   G   s   || _ || _g }tt|}t|Q t|0 |D ]%}t|r:|jt|dd}|| t	|| t	|| q|| qW d    n1 sJw   Y  W d    t
|S W d    t
|S 1 sfw   Y  t
|S )NTnon_blocking)r   r   r   r   r   torch	is_tensortoappendr   tuple)r   r   r   inputoutputoutput_streamxyr   r   r   forward!   s&   


(zCopy.forwardgrad_output.c           	   	   G   s   | j }| j}tt|d}tt|}t|; t|' t|D ]}|jt|dd}|	| t
|| t
|| q!W d    n1 sFw   Y  W d    n1 sUw   Y  d}|t| S )N)maxlenTr   NN)r   r   r   lenr   r   r   reversedr#   
appendleftr   r%   )	r   r,   r   r   
grad_inputinput_streamr)   r*   grad_streamsr   r   r   backward:   s    

 zCopy.backwardNr   r   r   __doc__staticmethodr   r
   Tensorsr+   r   r   r   r5   r   r   r   r   r      s    (r   c                	   @   r   )r   zSynchronizes a stream to another stream.

    Place it just before you want to start an operation on the next stream,
    provided that all operations on the previous stream are done.

    r   r   r   r   c                 G   s(   || _ || _t|| tdd |D S )Nc                 s   s&    | ]}t |r| n|V  qd S )N)r!   r"   detach).0r)   r   r   r   	<genexpr>a   s   $ zWait.forward.<locals>.<genexpr>)r   r   r   r%   )r   r   r   r&   r   r   r   r+   Y   s   
zWait.forwardr2   .c                 G   s"   | j }| j}t|| d}|| S )Nr.   )r   r   r   )r   r2   r   r   r4   r   r   r   r5   c   s
   
zWait.backwardNr6   r   r   r   r   r   Q   s    (r   )r7   collectionsr   typingr   r   r   r   r   r!   r   streamr
   r   r   r   r   r   r   strr   r9   r   autogradFunctionr   r   r   r   r   r   <module>   s    3