o
    ei;                     @   s   d dl 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deej eejdf B deej eejdf B fddZ	ddedededeeef fddZ	ddedededeeee f fddZeG dd dZdS )    N)	dataclass)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                    s8   t | tjr|  | j} | |S  |  | S N)
isinstancetorchTensordetachrequires_grad_requires_gradappend)avalflat_detached_args m/var/www/addictedbytheproject.nl/epg/venv/lib/python3.10/site-packages/torch/distributed/pipelining/_utils.pyextract_tensor_args   s   

z0flatten_args_detach.<locals>.extract_tensor_argsr   node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   	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s(t|  d|j d|j | | ks@t|  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   dtypestride)descexpectedgivenr   r   r   validate_tensor_metadata=   s   r'   expected_tensors.actual_tensorsc                 C   sf   t |t |krt|  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
|S |dkrS||  dkr*td| d|  dd}t |D ] }|||< |d |  dkr?q0||  d dkrL|d7 }q0|d8 }q0|S td	| d
)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)r0   r1   r2   mappingstage_index
rank_indexr   r   r   generate_stage_to_rank_mapping[   s(   	

r;   c                 C   sZ   t | ||}i }| D ]\}}||vrg ||< || | q| D ]}|  q$|S )a  
    Compute the rank to stage id mapping for either a looped or V-style schedule.

    This function inverts the stage_to_rank_mapping to get which stages are assigned to each rank.

    Returns a dictionary mapping rank -> list of stage indices assigned to that rank.
    )r;   itemsr   valuessort)r0   r1   r2   stage_to_rankrank_to_stagesstage_idrankstagesr   r   r   generate_rank_to_stage_mapping}   s   

rD   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).
    graphr1   has_loss_and_backwardN)	r   r   r   r    r   Graph__annotations__intboolr   r   r   r   rE      s
   
 
rE   )r/   )loggingdataclassesr   r   r   	getLoggerr   loggerr   r   RuntimeErrorr   r'   listr   tupler.   rJ   strdictr;   rD   rE   r   r   r   r   <module>   sH   



#
