
    Wj;                        d dl Z d dlmZ d dlZd dlmZ  e j        e          Zd Zd Z	 G d de
          Zd Zd	eej                 eej        d
f         z  deej                 eej        d
f         z  fdZ	 ddedededeeef         fdZ	 ddedededeeee         f         fdZe G d d                      ZdS )    N)	dataclass)fxc                 Z    g fd}t           j                            | |          }|fS )z\
    Flatten the args into a list form and detach the tensors from computational graph.
    c                     t          | t          j                  rC|                                                     | j                  }                    |           |S                     |            | S N)
isinstancetorchTensordetachrequires_grad_requires_gradappend)avalflat_detached_argss     h/home/longshao/multi-rider-rag/.venv/lib/python3.11/site-packages/torch/distributed/pipelining/_utils.pyextract_tensor_argsz0flatten_args_detach.<locals>.extract_tensor_args   sf    a&& 	((**++AO<<C%%c***J%%a(((H    r   nodemap_aggregate)argsr   new_argsr   s      @r   flatten_args_detachr      sQ          w$$ H
 '''r   c                 V    g fd}t           j                            | |           S )z,
    Flatten the args into a list form.
    c                 2                         |            | S r   )r   )r   	flat_argss    r   r   z)flatten_args.<locals>.extract_tensor_args,   s    r   r   )r   r   r   s     @r   flatten_argsr   &   sM     I    
 G  
 r   c                       e Zd ZdZdS )PipeliningShapeErrorz5Shape mismatch between configured and runtime values.N)__name__
__module____qualname____doc__ r   r   r    r    9   s        ????r   r    c                    |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givens      r   validate_tensor_metadatar-   =   s    >U[(("ZZX^ZZU[ZZ
 
 	
 >U[(("ZZX^ZZU[ZZ
 
 	
 ??.."aahoo6G6GaaQVQ]Q]Q_Q_aa
 
 	
 /.r   expected_tensors.actual_tensorsc           	      ,   t          |          t          |          k    r2t          |  dt          |           dt          |           d          t          t          |                    D ]$}t          |  d| ||         ||                    %d S )Nz: Number of values (z") does not match expected number ()z: value )lenr    ranger-   )r*   r.   r/   is       r   validate_tensors_metadatar5   L   s    
 N 3 333"xx^)<)<xx`cdt`u`uxxx
 
 	
 3'(()) 
 
   Q  "21"5~a7H	
 	
 	
 	

 
r   looppp_size
num_stagesstylereturnc                 8   i }|dk    rt          |          D ]
}|| z  ||<   nv|dk    r]|| z  dk    rt          d| d|  d          d}t          |          D ]+}|||<   |dz   | z  dk    r|| z  dz  dk    r|dz  }&|dz  },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.
    r6   vr   znum_stages z% must be evenly divisible by pp_size z for V schedules      zStyle z is not supported.)r3   
ValueError)r7   r8   r9   mappingstage_index
rank_indexs         r   generate_stage_to_rank_mappingrC   [   s    G ,, 	9 	9K#.#8GK  	9	#1$$hjhhwhhh   
 ,, 	  	 K#-GK a7*a//w&!+q00a

a

	  ;%;;;<<<Nr   c                     t          | ||          }i }|                                D ])\  }}||vrg ||<   ||                             |           *|                                D ]}|                                 |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.
    )rC   itemsr   valuessort)r7   r8   r9   stage_to_rankrank_to_stagesstage_idrankstagess           r   generate_rank_to_stage_mappingrM   }   s     37JNNM ,.N'--// . .$~%%#%N4 t##H---- !''))  r   c                   <    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).
    graphr8   has_loss_and_backwardN)	r!   r"   r#   r$   r   Graph__annotations__intboolr%   r   r   rO   rO      s>           8OOOOOOr   rO   )r6   )loggingdataclassesr   r	   r   	getLoggerr!   loggerr   r   RuntimeErrorr    r-   listr
   tupler5   rT   strdictrC   rM   rO   r%   r   r   <module>r_      s    ! ! ! ! ! !        
	8	$	$( ( (0  &@ @ @ @ @< @ @ @
 
 

5<(5s1B+CC
 &u|S/@)AA
 
 
 
  17 !*-	#s(^   F 17 !*-	#tCy.   4                    r   