a
    h                     @   s   d dl Z d dlmZ d dlmZ d dlZd dlmZ e eZ	dd Z
dd ZG d	d
 d
eZdd Zeeej eejdf f eeej eejdf f dddZdeeeeeef dddZeG dd dZdS )    N)	dataclass)Union)fxc                    s&   g   fdd}t j| |}| fS )z\
    Flatten the args into a list form and detach the tensors from computational graph.
    c                    s<   t | tjr*|  | j} | |S  |  | S d S N)
isinstancetorchTensordetachZrequires_grad_Zrequires_gradappend)avalZflat_detached_args Q/var/www/auris/lib/python3.9/site-packages/torch/distributed/pipelining/_utils.pyextract_tensor_args   s    

z0flatten_args_detach.<locals>.extract_tensor_argsr   nodeZmap_aggregate)argsr   new_argsr   r   r   flatten_args_detach   s    
r   c                    s"   g   fdd}t j| |  S )z,
    Flatten the args into a list form.
    c                    s     |  | S r   )r
   )r   Z	flat_argsr   r   r   ,   s    
z)flatten_args.<locals>.extract_tensor_argsr   )r   r   r   r   r   flatten_args&   s    r   c                   @   s   e Zd ZdZdS )PipeliningShapeErrorz5Shape mismatch between configured and runtime values.N)__name__
__module____qualname____doc__r   r   r   r   r   9   s   r   c                 C   s   |j |j ks(t|  d|j  d|j  |j|jksPt|  d|j d|j | | kst|  d|  d|  d S )Nz  has a shape mismatch: expected z actual z  has a dtype mismatch: expected z! has a stride mismatch: expected )shaper   ZdtypeZstride)descexpectedgivenr   r   r   validate_tensor_metadata=   s    r!   .)expected_tensorsactual_tensorsc                 C   sf   t |t |kr2t|  dt | dt | dtt |D ]"}t|  d| || ||  q>d S )Nz: Number of values (z") does not match expected number ()z: value )lenr   ranger!   )r   r"   r#   ir   r   r   validate_tensors_metadataL   s    r(   loop)pp_size
num_stagesstylereturnc                 C   s   i }|dkr(t |D ]}||  ||< qn|dkr||  dkrRtd| d|  dd}t |D ]@}|||< |d |  dkr|q^||  d dkr|d7 }q^|d8 }q^ntd	| d
|S )z
    Compute the stage id to rank mapping for either a looped or V-style schedule.

    Most commonly num_stages == pp_size * 2, but this function can be used to
    compute the mapping for any number of stages per rank.
    r)   vr   znum_stages z% must be evenly divisible by pp_size z for V schedules      zStyle z is not supported.)r&   
ValueError)r*   r+   r,   mappingZstage_indexZ
rank_indexr   r   r   generate_stage_to_rank_mapping[   s&    	
r3   c                   @   s,   e Zd ZU dZejed< eed< eed< dS )PipeInfoz>
    Captures information for a pipeline (`Pipe` object).
    graphr+   Zhas_loss_and_backwardN)	r   r   r   r   r   ZGraph__annotations__intboolr   r   r   r   r4   }   s   

r4   )r)   )loggingZdataclassesr   typingr   r   r   	getLoggerr   loggerr   r   RuntimeErrorr   r!   listr   tupler(   r7   strdictr3   r4   r   r   r   r   <module>   s&   
 
"