
    ΑiN=                    X    S SK Jr  S SKrS SKrS SKJr  SrSrSr	S r
S r " S	 S
5      rg)    )annotationsN)corei   i   i   @c                    U S:X  a  gU [         :  a  U [        -  S S3$ U [        :  a  U [         -  S S3$ U [        -  S S3$ )Nr   z0 Bz.2fz KBz MBz GB)MBKBGB)
size_bytess    b/var/www/html/banglarbhumi/venv/lib/python3.13/site-packages/paddle/device/cuda/memory_analyzer.pyformat_sizer      sV    QBr/#&c**Br/#&c**2oc"#&&    c                v   U(       d  g U Vs/ s H  n[        [        U5      5      PM     nnU HJ  n[        U5       H8  u  pgU[        U5      :  d  M  [        XF   [        [        U5      5      5      XF'   M:     ML     U Vs/ s H  oS-   PM	     nnSSR	                  U Vs/ s H	  nSU S3PM     sn5      -   S-   n	SSR	                  U Vs/ s H  nSU-  PM
     sn5      -   S-   n
SSR	                  U Vs/ s H  nSU-  PM
     sn5      -   S-   n[        SU  35        [        U
5        [        SSR	                  [        X5       VVs/ s H  u  p8US	U 3 PM     snn5      -   S-   5        [        U
5        [        U5       H{  u  pe[        U	R                  " U Vs/ s H  n[        U5      PM     sn6 5        U S
:X  a+  US-   S-  S:X  a  U[        U5      S-
  :w  a  [        U5        Mh  U S
:w  d  Mp  [        U5        M}     U S
:X  a  [        U
5        g g s  snf s  snf s  snf s  snf s  snf s  snnf s  snf )N   |z{:^}+=-z
### ^Block Size Distribution   r   )lenstr	enumeratemaxjoinprintzipformat)titleheadersrowsh
col_widthsrowicellwrow_fmt
header_sep	inner_sepcs                r
   print_tabler,   &   s   './w!#c!f+wJ/ ~GA3z?" #JM3s4y> B
 &  ",,Aa%J, CHHJ?JqQCrlJ?@@3FGsxx* =*Qq* =>>DJchh<Aa<==CI	F5'
	*	chhG0HI0H1QqcE(0HIJJSP 
*D/gnns3s!s1vs345..Q!q SY]")//) " ))j *; 0
 - @ =<
 J
 4s)   HH!H!
H&
8H+
H0H6c                      \ rS rSrS r\ S   SS jj5       r\ S   SS jj5       r\ S   SS jj5       r\SSS jj5       r	\SSS jj5       r
\SSS	 jj5       rS
rg)MemoryAnalysisToolK   c                    [        S5      e)Nz)Utility class should not be instantiated.)	TypeError)selfs    r
   __init__MemoryAnalysisTool.__init__L   s    CDDr   Nc                    Sn[         R                  " 5       (       d  [        SU S35      eUb  UO[         R                  " 5       n[         R                  " U5      $ )Nz$paddle.device.cuda.vmm_max_free_sizeThe API l is not supported in CPU-only PaddlePaddle. Please reinstall PaddlePaddle with GPU support to call this API.)r   is_compiled_with_cuda
ValueErrorget_cuda_current_device_idvmm_max_free_sizer2   	device_idnames      r
   r;   $MemoryAnalysisTool.vmm_max_free_sizeO   si     6**,,4&  !M  N 
 $ 002 	
 %%i00r   c                    Sn[         R                  " 5       (       d  [        SU S35      eUb  UO[         R                  " 5       n[         R                  " U5      $ )Nz&paddle.device.cuda.vmm_free_block_infor6   r7   )r   r8   r9   r:   vmm_free_block_infor<   s      r
   rA   &MemoryAnalysisTool.vmm_free_block_info_   si    
 8**,,4&  !M  N 
 $ 002 	
 ''	22r   c                    Sn[         R                  " 5       (       d  [        SU S35      eUb  UO[         R                  " 5       n[         R                  " U5      $ )Nz%paddle.device.cuda.vmm_all_block_infor6   r7   )r   r8   r9   r:   vmm_all_block_infor<   s      r
   rD   %MemoryAnalysisTool.vmm_all_block_infop   si    
 7**,,4&  !M  N 
 $ 002 	
 &&y11r   c                   Ub  UO[         R                  " 5       nSn SS KnSnS[        -  S[        -  S[        -  S[        -  S[        -  S	[        -  S
[        -  S[        -  S[
        -  S[
        -  S[
        -  /n/ SQnU R                  US9n[        R                  R                  R                  5       n[        R                  R                  R                  5       n[        R                  R                  R                  5       n	[        R                  R                  R                  5       n
U	S:  a  X-
  U	-  OSnSnU(       a^   WR                  5         UR                  U5      nUR!                  U5      n[#        UR$                  5      n[#        UR&                  5      nO[+        S5        SnSn/ SQn[-        U5      [#        U5      [#        U5      [#        U	5      [#        U
5      US US-   U-   //n[/        SUU5        / n/ n[1        U5       GHk  u  nnSU 3n[-        U5      nSnSnSnSnSn[3        [-        U5      5       Vs/ s H  nSS/PM	     nnU Hy  u  n n!n"UU -  nU"(       a  US-  nUU -  n[5        UU 5      nO[5        UU 5      n[-        U5      n#[1        U5       H  u  n$n%U U%:  d  M  U$n#  O   UU#   U"(       a  SOS==   S-  ss'   M{     UU-
  n&UU-
  n'UR7                  UUU&U[#        U5      [#        U'5      [#        U5      [#        U5      [#        U5      /	5        UR7                  US/U V(s/ s H  n(U(S   PM
     sn(-   5        UR7                  US/U V(s/ s H  n(U(S   PM
     sn(-   5        GMn     / SQn)[/        SU)U5        SS/UQn*[/        S U*U5        g ! [         a    Sn GNf = f! [(         a  nSnSn S nAGNS nAff = fs  snf s  sn(f s  sn(f )!NFr   Tr   
   2   d      i  iX  i   r      )z[0B,1M)z[1M,10M)z	[10M,50M)z
[50M,100M)z[100M,200M)z[200M,400M)z[400M,600M)z[600M,800M)z	[800M,1G)z[1G,2G)z[2G,3G)z	[3G,+INF))r=   g        zN/AErrzePlace install nvidia-smi to check real memory usage, pip install command: `pip install nvidia-ml-py3`zNo nvidia_smi)
Allocators	Allocatedz	Max AllocReservedzMax ReservedzMax Frag RatezPhy GPU Used / Totalz.2%z / zGlobal Memory Snapshot
Allocator_zFree BlockszUsed Blocks)	IDzTot Blksz	Used Blksz	Free BlkszTot Sizez	Used Sizez	Free SizezMax UsedzMax FreezAllocator Summary StatisticszAllocator IDz
Block Typer   )r   r:   
nvidia_smiImportErrorr   r   rD   paddledevicecudamemory_allocatedmax_memory_allocatedmemory_reservedmax_memory_reservednvmlInitnvmlDeviceGetHandleByIndexnvmlDeviceGetMemoryInfor   usedtotal	Exceptionr   r   r,   r   ranger   append)+r2   r=   nvidia_smi_AVAILABLErR   
THRESHOLDSRANGE_HEADERSallocator_listsmem_allocatedmax_mem_allocatedmem_reservedmax_mem_reservedmax_alloc_ratephy_used_strhandleinfophy_total_streglobal_headersglobal_rowssummary_rows	dist_rowsidxblocksallocator_nametotal_blocksfree_blocks
total_size	free_sizemax_free_sizemax_used_size_bucketssizeaddris_freeb_idxr%   tused_blocks	used_sizebsum_headersdist_headerss+                                              r
   memory_summary!MemoryAnalysisTool.memory_summary   s<    $ 002 	
  %	)#' 
 FGG"H"H"H"H"HFFF


 11I1F **;;="MM..CCE}}))99;!==--AAC a .,> 	 
&##%#>>yI!99&A*4995 +DJJ 7 w +L+M
 O$M*-.L),-!#&u$}4

 	,nkJ 	$_5KC)#/N v;LKJIMM',S-?'@A'@!1v'@GA'-#dGd"
1$K%I$'t$<M$'t$<M J%j1DAqax ! 2 Gq3q83 (." '4K"Y.I " 
+	*	*..
 /2IA1Q42II /2IA1Q42IIg 6p

 	2KN&E}E-|YGO  	)#( 	)v  &$ %&` BN 3J 3Js<   N* 
AN= O>O(O#*N:9N:=
OOOc           
        U(       d  [        S5        g [        S[        U5       S35        / SQn/ n/ nUR                  SR                  U5      5        U Hu  n[	        US   5      US   (       a  SOS	[	        US
   5      [	        US   5      [	        US   5      [	        US   5      /nSR                  U5      nUR                  U5        Mw      [        USSS9 nUR                  SR                  U5      5        S S S 5        [        SU 35        g ! , (       d  f       N= f! [         a  n	[        SU SU	 35         S n	A	g S n	A	ff = f)NzNo data to display.Record data size: z, start printing...)Allocator_InstanceIs_AllocateSeq_IDReq_Size	Cur_AllocCur_Rsrv	r   r   AllocateFreer   rK         r'   zutf-8)encoding
zData successfully written to: zError writing to file z: )r   r   rb   r   r   openwriteOSError)
r2   dataoutput_filepathr    formatted_row	all_linesr$   linefrp   s
             r
   allocate_record_table(MemoryAnalysisTool.allocate_record_table7  s8   '("3t9+-@AB
 	7+,CCF!!f
&CFCFCFCFM 99]+DT" 	CosW=		),- >2?2CDE >=  	C*?*;2aSABB	Cs0   D$ !D<D$ 
D!D$ $
E	.EE	c                  ^  SS K Jn  SSKJn  U(       d  [        S5        g [        S[        U5       S35        [        R                  " U5      nUS S 2S4   nUS:H  nXW   nUS S 2S4   nUS S 2S4   n	US S 2S	4   n
US S 2S
4   nUS S 2S4   nSmUR                  R                  S5        UR                  SSSSSS	S/SS.S9u  nu  pUR                  XSSSSS9  UR                  U	U
SSSSSSSS9	  UR                  SSSSS 9  UR                  S!S"SS#S$9  UR!                  TS%9  UR#                  S&SS'9  UR%                  UR'                  5       S(S)9  UR                  U	US*S+S,S-S.S/9  UR)                  U	SUS*S0S19  UR                  U	US2SSS3S4S/9  UR)                  U	SUS2S5S19  UR+                  5         UR!                  UR-                  5       S6-  T5        UR                  S7S8SSS 9  UR/                  S95        UR#                  S&S:SS;9  UR%                  UR'                  5       S(S)9  U4S< jnUR1                  U5      nUR2                  R5                  U5        UR2                  R5                  U5        X4 HR  nUR7                  5       R9                  5       nTU;  a  UR;                  T5        UR=                  [?        U5      5        MT     X4 HY  nUR@                  RC                  5        H%  nURE                  S=5        URG                  S,5        M'     UR#                  S:S>S=SSS?9  M[     URI                  5       u  nnURI                  5       u  nnURK                  UU-   UU-   S@SSSS3S=S(SA9	  URM                  5         URO                  SBSC9  URQ                  U5        URS                  5         [        SDU 35        g ! [         a    [	        S5      ef = f)ENr   )tickerzYmatplotlib is required but not installed. Please install it using: pip install matplotlibzNo data to plot.r   z, start plotting...r   r   rK   r   r   zseaborn-v0_8-whitegridT)   rG   x   )height_ratioshspace)sharexfigsizedpigridspec_kwz#2ca02cr   g333333?)color	linestyle	linewidthalpha<   g      ?whiteg      ?zRequest Size)r   sr   
edgecolorsr   labelzorderzRequest Size (Linear Scale)   boldrG   )fontsize
fontweightlabelpadz%Paddle GPU Memory Allocation Analysisr      )r   r   pad)bottomx)axislengthF)visiblez#d62728z--      ?g?zReserved (Pool))r   r   r   r   r   g?)r   r   z#1f77b4g?zAllocated (Used)g333333?g      @zPool Status (Inverted)    both)r   whichr   c                d   > [        U 5      nUTS-  ::  a  g[        U5      R                  SS5      $ )Nr   0 r   )absr   replace)r   posvalLOG_START_VALUEs      r
   y_axis_formatterAMemoryAnalysisTool.allocate_record_plot.<locals>.y_axis_formatter  s4    a&Co++s#++C44r   blackmajor)r   r   colorswidthr   zupper right)locr   frameon	facecolor
framealpha	edgecolorshadowg?)r   zAnalysis plot saved to: )*matplotlib.pyplotpyplot
matplotlibr   rS   r   r   nparraystyleusesubplotsplotscatter
set_ylabel	set_titleset_ylimtick_paramssetpget_xticklabelsfill_betweeninvert_yaxisr   
set_xlabelFuncFormatteryaxisset_major_formatter
get_ytickstolistrb   
set_ytickssortedspinesvaluesset_edgecolorset_linewidthget_legend_handles_labelslegendtight_layoutsubplots_adjustsavefigclose)r2   r   	save_pathpltr   data_npis_allocatefilter_maskallocator_instanceidssizes	allocatedreservedfigax1ax2r   	formatteraxcurrent_ticksspinelines1labels1lines2labels2r   s                            @r
   allocate_record_plot'MemoryAnalysisTool.allocate_record_plot\  sS   	+) $%"3t9+-@AB((4.adm!Q&&$QT]adm1AqDM	1a4=		./,,+,a&A> ' 
Zc 	i3!3 	 	
 	  	 
	
 	)	 	 	
 	3	 	 	
 	O,S+$$&6 	# 	 	
 	a#F$ 	 	
 	a)4HX\\^c)?;$	 	 	
 	rSq9$$&6	5 (()9:			%%i0		%%i0*BMMO224Mm3$$_5MM&/0	  *B))+##G,##C( , NN77#a  	  779779

VOg 	 
	
 	4(I		(45c  	k 	s   O O )N)r=   
int | Nonereturnztuple[int, int])r=   r  r  zlist[list[tuple[int, int]]])r=   r  r  z!list[list[tuple[int, int, bool]]])r=   r  r  None)r   )r   r   )r  r   )__name__
__module____qualname____firstlineno__r3   classmethodr;   rA   rD   r   r   r  __static_attributes__r  r   r
   r.   r.   K   s    E &*1#1	1 1  !%33 
%3 3   !%22 
+2 2  sH sHj "C "CH U6 U6r   r.   )
__future__r   numpyr   rT   paddle.baser   r   r   r   r   r,   r.   r  r   r
   <module>r'     s;    #    
'"Jg6 g6r   