
    rh
                     t   d Z ddlmZmZ ddlmZ ddlmZmZm	Z	m
Z
mZmZ ddlmZ 	 ddeee	f   ded	ed
ee   deeee         f
dZ G d d      Zej(                   edd      ej*                   edd      ej,                   edd      ej.                   eddd      iZ	 ddeeee         dee   ddfdZy)z
This visualizer requires matplotlib to be installed.

Example usage:

ops = get_schedule_ops("InterleavedZeroBubble", 4, 8)
visualize_schedule(ops, "test.png")
    )OptionalUnion)mock)_Action_ComputationType_PipelineScheduleget_schedule_classPipelineScheduleMultiPipelineScheduleSingle)PipelineStageNschedule	pp_degreenum_microbatchesnum_stages_per_rankreturnc                 V   t        | t              rt        |       }n#t        |       t        k(  r| }nt        d|        t        j                  t        d      }d|_	        ||_
        d|_        t        |t              r|d}|dk(  sJ |}||z  |_        nSt        |t              r5|d}|dk\  sJ t!        |      D cg c]  }| }}|D ]  }||z  |_         nt        d|        |||      }	g }
t!        |      D ]   }|
j#                  |	j$                  |          " |
S c c}w )a[  
    Get all actions for a given schedule, pp_degree, and num_microbatches. The actions are returned in a list of lists
    where each inner list represents a rank and each element in the inner list represents an action.

    The schedule can be specified as a string which is passed into get_schedule_class() or a _PipelineSchedule instance.
    zInvalid schedule: T)instancer   N      )
isinstancestrr	   typer   
ValueErrorr   create_autospecr   
group_rank
group_sizesubmod
issubclassr   
num_stagesr
   rangeappendpipeline_order)r   r   r   r   schedule_classmock_pipeline_stagestages_stageschedule_instanceall_actionsranks               /var/www/html/ai-insurance-compliance-backend/venv/lib/python3.12/site-packages/torch/distributed/pipelining/_schedule_visualizer.pyget_schedule_opsr,      sl    (C +H5	h,	,!-hZ899 ..}tL%&"%."!% ."89&"#"a'''$/);	N$9	:&"#"a'''/45H/IJ!%JJ 	?E2Y>E	? -n-=>?? 'v/?@ Ki  C,;;DABC ! Ks    	D&c                   &    e Zd Z	 	 ddededefdZy)_ComputationTypeColorcolortextwidthc                 .    || _         || _        || _        y N)r/   r1   r0   )selfr/   r0   r1   s       r+   __init__z_ComputationTypeColor.__init__T   s     

	    N) r   )__name__
__module____qualname__r   intr5    r6   r+   r.   r.   S   s+     	  	r6   r.   blueForwardtealzBackward InputgreenzBackward WeightorangezFull Backwardr   filenamec                    ddl m} ddlm} d|j                  d<   t        |       }t        d | D              }|j                  |dz   |dz   f      \  }}d	}t        ||      d
z   }	t               }
t        |       D ]  \  }}d}|D ]  }|t        j                  |j                  t        d            }|
j                  |j                         |j                   }|j"                  } ||||z
  dz
  f|d|d      }|j%                  |       |j'                  ||dz  z   ||z
  dz
  dz   t)        |      dd|	d       ||z  }n|dz  }t        ||      }  |j+                  d|dz          |j-                  d|dz          |j/                  t1        |      D cg c]
  }||z
  dz
   c}       |j3                  t1        |      D cg c]  }d| 	 c}|	       |j5                  g        |j7                  d       |
D cg c]2  } |dddt        |   j                   dt        |   j&                        4 }}|j9                  |d|	       |r|j;                  |d       y|j=                          yc c}w c c}w c c}w )ao  
    Visualize the schedule using matplotlib.
    The schedule is a list of lists where each inner list represents a rank and each element in the inner list represents an action.
    The actions are represented as rectangles with different colors based on their computation type.
    The filename is optional and if provided, the plot will be saved to that file.
    r   N)	RectanglezDejaVu Sanszfont.familyc              3   2   K   | ]  }t        |        y wr3   )len).0r*   s     r+   	<genexpr>z%visualize_schedule.<locals>.<genexpr>y   s     5Dc$i5s   r   )figsize   blackr   )	facecolor	edgecolorg      ?centerwhite)havafontsizer/   g      zRank )rS   F)r   r   )rM   rN   labelzupper right)handleslocrS   tight)bbox_inches)matplotlib.pyplotpyplotmatplotlib.patchesrD   rcParamsrF   maxsubplotsminset	enumerateaction_type_to_color_mappinggetcomputation_typer.   addr/   r1   	add_patchr0   r   set_xlimset_ylim
set_yticksr    set_yticklabelsset_xticklabelsgridlegendsavefigshow)r   rB   pltrD   	num_ranksmax_actionsfigaxmax_draw_position	font_sizeused_computationrank_idxactionsdraw_positionactioncomp_type_colorr/   r1   recti	comp_typelegend_elementss                         r+   visualize_scheduler   h   s    $, 	 LL HI5H55K llK!OY]#ClDGCK+a/Iu&x0 !F' 	FF!">"B"B++-B7-K# !$$V%<%<='--'-- "I$81$<=#% T"!EAI-(1,s2K&!   &" #$5} E?	F!FD KK'!+,KKi#o&MMeI>NO(9x'#-OPU9-=>%s>Sr GGEN *
  	29=CC.y9>>	

O 
 IIo=9IMH'2
/ P>
s   I7I<7Jr3   )__doc__typingr   r   unittestr   &torch.distributed.pipelining.schedulesr   r   r   r	   r
   r   "torch.distributed.pipelining.stager   r   r;   listr,   r.   FORWARDBACKWARD_INPUTBACKWARD_WEIGHTFULL_BACKWARDrb   r   r<   r6   r+   <module>r      s   #   = *.	6C**+66 6 "#	6
 
$x 
!"6r	 	 3FIF##%:6CS%T$$&;GEV&W""$9(OUV$W	   HLU4)*+U7?}U	Ur6   