
    x-jN=                    `    d dl mZ d dlZd dlZd dlmZ dZdZdZ	d Z
d Z G d	 d
          ZdS )    )annotationsN)corei   i   i   @c                    | dk    rdS | t           k     r| t          z  ddS | t          k     r| t           z  ddS | t          z  ddS )Nr   z0 Bz.2fz KBz MBz GB)MBKBGB)
size_bytess    b/var/www/html/banglarbhumi/venv/lib/python3.11/site-packages/paddle/device/cuda/memory_analyzer.pyformat_sizer      sd    QuBr/*****Br/*****2o&&&&&    c           
        |sd S d |D             }|D ]]}t          |          D ]K\  }}|t          |          k     r3t          ||         t          t          |                              ||<   L^d |D             }dd                    d |D                       z   dz   }dd                    d |D                       z   dz   }dd                    d |D                       z   dz   }	t          d|             t          |           t          dd                    d	 t          ||          D                       z   dz              t          |           t          |          D ]s\  }}t           |j        d
 |D                         | dk    r2|dz   dz  dk    r&|t          |          dz
  k    rt          |	           ^| dk    rt          |	           t| dk    rt          |           d S d S )Nc                F    g | ]}t          t          |                    S  )lenstr).0hs     r
   
<listcomp>zprint_table.<locals>.<listcomp>*   s$    ///!#c!ff++///r   c                    g | ]}|d z   S )   r   r   ws     r
   r   zprint_table.<locals>.<listcomp>/   s    ,,,A!a%,,,r   |c                    g | ]}d | d	S )z{:^}r   r   s     r
   r   zprint_table.<locals>.<listcomp>2   s     ???qlQlll???r   +c                    g | ]}d |z  S )=r   r   s     r
   r   zprint_table.<locals>.<listcomp>3   s     = = =Qq = = =r   c                    g | ]}d |z  S )-r   r   s     r
   r   zprint_table.<locals>.<listcomp>4   s    <<<Aa<<<r   z
### c                "    g | ]\  }}|d | S )^r   )r   r   r   s      r
   r   zprint_table.<locals>.<listcomp>9   s&    III11qIIIr   c                ,    g | ]}t          |          S r   )r   )r   cs     r
   r   zprint_table.<locals>.<listcomp>>   s    333!s1vv333r   Block Size Distribution   r   r   )	enumerater   maxr   joinprintzipformat)
titleheadersrows
col_widthsrowicellrow_fmt
header_sep	inner_seps
             r
   print_tabler7   &   s^    //w///J C C ~~ 	C 	CGAt3z??"" #JqM3s4yy>> B B
1	C -,,,,J CHH??J???@@@3FGsxx = =* = = =>>>DJchh<<<<<===CI	
5

	*	chhIIGZ0H0HIIIJJJSP   
*D// 	 	3ngn33s3334555...Q!q  SYY]"")///))))j *)r   c                      e Zd Zd Ze	 ddd            Ze	 ddd	            Ze	 ddd            Zeddd            Zeddd            Z	eddd            Z
dS )MemoryAnalysisToolc                     t          d          )Nz)Utility class should not be instantiated.)	TypeError)selfs    r
   __init__zMemoryAnalysisTool.__init__L   s    CDDDr   N	device_id
int | Nonereturntuple[int, int]c                    d}t          j                    st          d| d          ||nt          j                    }t          j        |          S )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r<   r>   names      r
   rH   z$MemoryAnalysisTool.vmm_max_free_sizeO   sy     6*,, 	 N4  N  N  N  
 $ I022 	
 %i000r   list[list[tuple[int, int]]]c                    d}t          j                    st          d| d          ||nt          j                    }t          j        |          S )Nz&paddle.device.cuda.vmm_free_block_inforC   rD   )r   rE   rF   rG   vmm_free_block_inforI   s      r
   rM   z&MemoryAnalysisTool.vmm_free_block_info_   sy    
 8*,, 	 N4  N  N  N  
 $ I022 	
 '	222r   !list[list[tuple[int, int, bool]]]c                    d}t          j                    st          d| d          ||nt          j                    }t          j        |          S )Nz%paddle.device.cuda.vmm_all_block_inforC   rD   )r   rE   rF   rG   vmm_all_block_inforI   s      r
   rP   z%MemoryAnalysisTool.vmm_all_block_infop   sy    
 7*,, 	 N4  N  N  N  
 $ I022 	
 &y111r   Nonec                $   ||nt          j                    }d}	 dd l}d}n# t          $ r d}Y nw xY wdt          z  dt          z  dt          z  dt          z  dt          z  d	t          z  d
t          z  dt          z  dt
          z  dt
          z  dt
          z  g}g d}|                     |          }t          j        j	        
                                }t          j        j	                                        }t          j        j	                                        }	t          j        j	                                        }
|	dk    r|	|z
  |	z  nd}d}|r	 |                                 |                    |          }|                    |          }t#          |j                  }t#          |j                  }n.# t(          $ r}d}d}Y d }~nd }~ww xY wt+          d           d}d}g d}t-          |          t#          |          t#          |          t#          |	          t#          |
          |d|dz   |z   gg}t/          d||           g }g }t1          |          D ]~\  }}d| }t-          |          }d}d}d}d}d}d t3          t-          |                    D             }|D ]\  }} }!||z  }|!r|dz  }||z  }t5          ||          }nt5          ||          }t-          |          }"t1          |          D ]\  }#}$||$k     r|#}" n||"         |!rdndxx         dz  cc<   ||z
  }%||z
  }&|                    |||%|t#          |          t#          |&          t#          |          t#          |          t#          |          g	           |                    |dgd |D             z              |                    |dgd |D             z              g d}'t/          d |'|           d!d"g|}(t/          d#|(|           d S )$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_c                    g | ]}d d gS r   r   )r   _s     r
   r   z5MemoryAnalysisTool.memory_summary.<locals>.<listcomp>   s    AAA!1vAAAr   zFree Blocksc                    g | ]
}|d          S r^   r   r   bs     r
   r   z5MemoryAnalysisTool.memory_summary.<locals>.<listcomp>       2I2I2IA1Q42I2I2Ir   zUsed Blocksc                    g | ]
}|d          S )r&   r   ra   s     r
   r   z5MemoryAnalysisTool.memory_summary.<locals>.<listcomp>#  rc   r   )	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   rG   
nvidia_smiImportErrorr   r   rP   paddledevicecudamemory_allocatedmax_memory_allocatedmemory_reservedmax_memory_reservednvmlInitnvmlDeviceGetHandleByIndexnvmlDeviceGetMemoryInfor   usedtotal	Exceptionr*   r   r7   r'   ranger(   append))r<   r>   nvidia_smi_AVAILABLErf   
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_idxr2   tused_blocks	used_sizesum_headersdist_headerss)                                            r
   memory_summaryz!MemoryAnalysisTool.memory_summary   s/    $ I022 	
  %	)#'   	) 	) 	)#(   	) FGG"H"H"H"H"HFFF


 
 
 11I1FF *;;=="M.CCEE})99;;!=-AACC a ..,>> 	  	,
&##%%%#>>yII!99&AA*4955 +DJ 7 7 & & &$ %& w   +L+M
 
 
 O$$M**-..L)),--!''u$}4

 	,nkJJJ 	$_55 5	 5	KC/#//N v;;LKJIMMAAuS-?-?'@'@AAAG'- 9 9#dGd"
 =1$K%I$'t$<$<MM$'t$<$<M J%j11  DAqaxx !   G2qq333q83333&4K"Y.I " 
++	**	**....
   /2I2I2I2I2II   /2I2I2I2I2II   


 

 

 	2KNNN&E}E-|YGGGGGs%   " 11A&F; ;
GGG output_filepathr   c           
     0   |st          d           d S t          dt          |           d           g d}g }g }|                    d                    |                     |D ]}t	          |d                   |d         rdnd	t	          |d
                   t	          |d                   t	          |d                   t	          |d                   g}d                    |          }|                    |           	 t          |dd          5 }|                    d                    |                     d d d            n# 1 swxY w Y   t          d|            d S # t          $ r }	t          d| d|	            Y d }	~	d S d }	~	ww xY w)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   rW         r   zutf-8)encoding
zData successfully written to: zError writing to file z: )r*   r   rv   r)   r   openwriteOSError)
r<   datar   r.   formatted_row	all_linesr1   linefr   s
             r
   allocate_record_tablez(MemoryAnalysisTool.allocate_record_table7  s    	'(((FA3t99AAABBB
 
 
 	7++,,, 
	# 
	#CCF!!f0

&CFCFCFCFM 99]++DT""""	CosW=== .		),,---. . . . . . . . . . . . . . .D?DDEEEEE 	C 	C 	CA?AAaAABBBBBBBBB	Cs<   E+ )E E+ EE+ EE+ +
F5FF	save_pathc                z	   	 dd l m} ddlm} n# t          $ r t	          d          w xY w|st          d           d S t          dt          |           d           t          j        |          }|d d df         }|dk    }||         }|d d df         }|d d df         }	|d d d	f         }
|d d d
f         }|d d df         }d|j	        
                    d           |                    dddddd	dgdd          \  }\  }}|                    |	|
dddd           |                    |	|
ddddddd	  	         |                    dddd            |                    d!d"dd#$           |                    %           |                    d&d'           |                    |                                d()           |                    |	|d*d+d,d-d./           |                    |	d|d*d01           |                    |	|d2ddd3d4/           |                    |	d|d2d51           |                                 |                    |                                d6z             |                    d7d8dd            |                    d9           |                    d&d:d;           |                    |                                d()           fd<}|                    |          }|j                            |           |j                            |           ||fD ]c}|                                                                }|vr|                               |                    t?          |                     d||fD ]b}|j         !                                D ],}|"                    d=           |#                    d,           -|                    d:d>d=dd?           c|$                                \  }}|$                                \  }}|%                    ||z   ||z   d@dddd3d=d(A	  	         |&                                 |'                    dBC           |(                    |           |)                                 t          dD|            d S )ENr   )tickerzYmatplotlib is required but not installed. Please install it using: pip install matplotlibzNo data to plot.r   z, start plotting...r&   r   rW   r   r   zseaborn-v0_8-whitegridT)   rS   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)   boldrS   )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)   r   both)r   whichr   c                ~    t          |           }|dz  k    rdS t          |                              dd          S )Nr   0 r   )absr   replace)r   posvalLOG_START_VALUEs      r
   y_axis_formatterzAMemoryAnalysisTool.allocate_record_plot.<locals>.y_axis_formatter  sA    a&&Co+++ss##++C4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   rg   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tolistrv   
set_ytickssortedspinesvaluesset_edgecolorset_linewidthget_legend_handles_labelslegendtight_layoutsubplots_adjustsavefigclose)r<   r   r   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_plotz'MemoryAnalysisTool.allocate_record_plot\  s   	++++++))))))) 	 	 	k  	
  	$%%%FA3t99AAABBB(4..aaadm!Q&+&$QQQT]aaadm1AAAqDM	111a4=	.///,,+,a&A>> ' 
 
Zc3 	i3!3 	 	
 	
 	
 	  	 
	
 
	
 
	
 	)	 	 	
 	
 	
 	3	 	 	
 	
 	
 	O,,,S+++$$&&666 	# 	 	
 	
 	
 	a#FFF$ 	 	
 	
 	
 	a)4HHHX\\^^c)?;;;$	 	 	
 	
 	
 	rSq999$$&&666	5 	5 	5 	5 	5 (()9::		%%i000	%%i000* 	1 	1BMMOO2244Mm33$$_555MM&//0000 * 	 	B))++ ) )##G,,,##C((((NN77#a      77997799

VOg 	 
	
 
	
 
	
 	4(((I		44455555s    *)N)r>   r?   r@   rA   )r>   r?   r@   rK   )r>   r?   r@   rN   )r>   r?   r@   rQ   )r   )r   r   )r   r   )__name__
__module____qualname__r=   classmethodrH   rM   rP   r   r   r'  r   r   r
   r9   r9   K   s)       E E E &*1 1 1 1 [1  !%3 3 3 3 [3   !%2 2 2 2 [2  sH sH sH sH [sHj "C "C "C "C ["CH U6 U6 U6 U6 [U6 U6 U6r   r9   )
__future__r   numpyr   rh   paddle.baser   r   r   r   r   r7   r9   r   r   r
   <module>r/     s    # " " " " "            
' ' '" " "Jg6 g6 g6 g6 g6 g6 g6 g6 g6 g6r   