
     Ti                        S SK Jr  S SKrS SKrSS jr " S S5      rS r " S S5      r S       SS	 jjrSSS
 jjr	g)    )annotationsNc                d    SSK nUR                  U 5      nUR                  5       R                  nU$ )z
Returns the physical memory used by a process.

Args:
    pid: Process id, current one is `os.getpid()`.

Returns:
     Physical memory.

It relies on the module *psutil*.
r   N)psutilProcessmemory_inforss)pidr   processmems       [/var/www/html/ai-image-ml/venv/lib/python3.13/site-packages/onnxscript/tools/memory_peak.pyget_memory_rssr   
   s.     nnS!G




#
#CJ    c                  V    \ rS rSrS rS
SS jjrSS jrSS jrS r\	SS j5       r
Srg	)Monitor   c                J    SU l         SU l        SU l        SU l        SU l        g )Nr   )max_peakaverage
n_measuresbeginendselfs    r   __init__Monitor.__init__   s%      
r   c                    [        U5      n[        U R                  U-  U R                  S-  U R                  -  U-  U R                  U-  U R
                  U-  U R                  U-  S9$ )Ng      ?)peakmeannr   r   )floatdictr   r   r   r   r   )r   unitfunits      r   to_dictMonitor.to_dict%   sb    d&#doo5=oo%**u$5 
 	
r   c                    U R                   R                   SU R                   SU R                   SU R                   S3$ )Nz(peak=z
, average=z, n=))	__class____name__r   r   r   r   s    r   __repr__Monitor.__repr__/   sA    ~~&&'vdmm_ =||nD(9<	
r   c                    U R                   S:X  a  Xl        [        XR                  5      U l        U =R                  U-  sl        Xl        U =R                   S-  sl         g )Nr      )r   r   maxr   r   r   )r   r   s     r   updateMonitor.update5   sD    ??aJC/1r   c                   UR                  U R                  5        UR                  U R                  5        UR                  U R                  5        UR                  U R                  5        UR                  U R
                  5        g N)sendr   r   r   r   r   )r   conns     r   r3   Monitor.send=   sR    		$-- 		$,,		$//"		$**		$((r   c                    U " 5       nUR                  5       Ul        UR                  5       Ul        UR                  5       Ul        UR                  5       Ul        UR                  5       Ul        U$ r2   )recvr   r   r   r   r   )clsr4   ms      r   r7   Monitor.recvD   sO    EYY[
IIK	yy{))+		r   )r   r   r   r   r   N)r-   )r"   intreturndict[str, float])r<   str)r   r    )r<   r   )r)   
__module____qualname____firstlineno__r   r$   r*   r/   r3   classmethodr7   __static_attributes__ r   r   r   r      s/    

  r   r   c                z  ^^ U R                  S5        U R                  5       nU R                  5       nU R                  5       nSS KnUR                  U5      nU(       ah  SSKJnJnJmJnJ	n	  U" 5         U" 5       n
[        U
5       Vs/ s H
  o" U5      PM     snmUU4S jn[        U
5       Vs/ s H  n[        5       PM     nnO/ n[        5       nU R                  S5         UR                  5       R                  nUR                  U5        U(       a,  [        W" 5       U5       H  u  nnUR                  U5        M     U R!                  US9(       a  U R                  5       nUS:X  a  OM  UR                  5       R                  nUR                  U5        U(       a,  [        W" 5       U5       H  u  nnUR                  U5        M     UR                  U 5        U R                  [#        U5      5        U H  nUR                  U 5        M     U(       a  W	" 5         U R%                  5         g s  snf s  snf )Nr   )nvmlDeviceGetCountnvmlDeviceGetHandleByIndexnvmlDeviceGetMemoryInfonvmlInitnvmlShutdownc                 T   > T V s/ s H  n T" U 5      R                   PM     sn $ s  sn f r2   )used)hhandlesrI   s    r   gpu_used%_process_memory_spy.<locals>.gpu_usedm   s'    =DEW+A.33WEEEs   %)timeout)r3   r7   r   r   pynvmlrG   rH   rI   rJ   rK   ranger   r   r   r/   zippolllenclose)r4   r	   rR   cudar   r
   rG   rH   rJ   rK   n_gpusirP   gpuscpur   rgcoder   rO   rI   s                       @@r   _process_memory_spyrb   O   s   IIbM ))+C iikG 99;DnnS!G	
 	
 	
#%:?-H-Q-a0-H	F $)=1=a	=1
)CIIbM !!#''

3HJ-1 .99W9%99;Drz  



#
#CJJsO
D)DAqHHQK * HHTNIIc$i	t JJLO I
 2s   	H31H8c                  :    \ rS rSrSrSS	S jjrS
S jrSS jrSrg)	MemorySpy   z
Information about the spy. It class method `start`.
Method `stop` can be called to end the measure.

Args:
    pid: process id  of the process to spy on
    delay: spy on every delay seconds
    cuda: enable cuda monitoring
c                H    Xl         X l        X0l        U R                  5         g r2   )r	   delayrZ   start)r   r	   rg   rZ   s       r   r   MemorySpy.__init__   s    
	

r   c                |   [         R                  " 5       u  U l        U l        [         R                  " [
        U R                  4S9U l        U R                  R                  5         U R                  R                  5       nUS:w  a  [        SU S35      eU R                  R                  U R                  5        U R                  R                  U R                  5        U R                  R                  U R                  (       a  SOS5        U R                  R                  5       nUS:w  a  [        SU S35      eU $ )z+Starts another process and tells it to spy.)targetargsrF   z0The child processing is supposed to send -2 not .r-   r   z6The child processing is supposed to send -2 again not )multiprocessingPipeparent_conn
child_connr   rb   child_processrh   r7   RuntimeErrorr3   r	   rg   rZ   )r   datas     r   rh   MemorySpy.start   s   ,;,@,@,B)$/,44&doo-?
 	  "$$&2:!QRVQWWXYZZdhh'djj)499a!4$$&2:HaP  r   c                   U R                   R                  S5        [        R                  U R                   5      /nU R                   R                  5       n/ n[	        U5       H1  nUR                  [        R                  U R                   5      5        M3     U R                   R                  5         U R                  R                  5         [        US9nU R                  (       a  X5S'   U$ )zStops spying on.rS   )r^   r]   )rp   r3   r   r7   rU   appendrY   rr   joinr!   rZ   )r   r^   r[   r]   _ress         r   stopMemorySpy.stop   s    b!||D,,-.!!&&(vAKKT%5%567  	 !sm99K
r   )rq   rr   rZ   rg   rp   r	   N){Gz?F)r	   r;   rg   r    rZ   bool)r<   rd   )r<   dict[str, list[Monitor]])	r)   r?   r@   rA   __doc__r   rh   r{   rC   rD   r   r   rd   rd      s    (r   rd   c                J    U c  [         R                  " 5       n [        XU5      $ )a  Starts the memory spy. The function starts another
process spying on the one sent as an argument.

Example::

.. code-block:: python

    from onnxscript.tools.memory_peak import start_spying_on, flatten

    p = start_spying_on()
    # ...
    # code to measure
    # ...
    stat = p.stop()
    print(stat)
    print(flatten(stat))

Args:
    pid: process id to spy or the the current one.
    delay: delay between two measures.
    cuda: True or False to get memory for cuda devices
)osgetpidrd   )r	   rg   rZ   s      r   start_spying_onr      s!    2 {iikS&&r   c                6   U S   S   R                  SS9nSU ;   aG  [        U S   5       H5  u  p4UR                  SS9R                  5        H  u  pVXbSU SU 3'   M     M7     U(       a)  UR                  5        VVs0 s H  u  pVU U 3U_M     nnnU$ s  snnf )z9Flattens a dictionary produced by :meth:`MemorySpy.stop`.r^   r   i   )r"   r]   gpury   )r$   	enumerateitems)psprefixobsr\   r`   kvs          r   flattenr      s    
U)A,

E

*C|bj)DA		u	-335%&c!AaSM" 6 * -0YY[9[TQ&!~q [9J :s   =B)r	   r;   r<   r;   )Nr}   F)r	   z
int | Nonerg   r    rZ   r~   r<   rd   ) )r   r   r   r>   r<   r=   )

__future__r   rn   r   r   r   rb   rd   r   r   rD   r   r   <module>r      sb    #  	&/ /dCL5 5r ?D'	'#('7;''<	r   